medcat.components.addons.relation_extraction.rel_dataset
Classes:
-
RelData–
Attributes:
-
logger–
RelData
RelData(tokenizer: BaseTokenizerWrapper, config: ConfigRelCAT, cdb: CDB = CDB(Config()))
Bases: Dataset
Use this class to create a dataset for relation annotations from CSV exports, MedCAT exports or Spacy Documents (assuming the documents got generated by MedCAT, if they did not then please set the required parameters manually to match MedCAT output, see /medcat/cat.py#_get_entity)
If you are using this to create relations from CSV it is assumed that your entities/concepts of interest are surrounded by the special tokens, see create_base_relations_from_csv doc.
Parameters:
-
(tokenizerBaseTokenizerWrapper) –tokenizer used to generate token ids from input text
-
(configConfigRelCAT) –same config used in RelCAT
-
(cdbCDB, default:CDB(Config())) –Optional, used to add concept ids and types to detected ents, useful when creating datasets from MedCAT output. Defaults to CDB().
Methods:
-
create_base_relations_from_csv–Assumes the columns are as follows -
create_base_relations_from_doc–Creates a list of tuples based on pairs of entities detected
-
create_relations_from_export–Args:
-
generate_base_relations–Util function, should be used if you want to train from spacy docs
-
get_labels–This is used to update labels in config with unencountered
Attributes:
-
cdb(CDB) – -
config(ConfigRelCAT) – -
dataset(dict[Any, Any]) – -
name– -
tokenizer(BaseTokenizerWrapper) –
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_dataset.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
name
class-attribute
instance-attribute
name = 'rel_dataset'
create_base_relations_from_csv
create_base_relations_from_csv(csv_path: str, keep_source_text: bool = False)
Assumes the columns are as follows
["relation_token_span_ids",
"ent1_ent2_start", "ent1", "ent2", "label",
"label_id", "ent1_type", "ent2_type",
"ent1_id", "ent2_id", "ent1_cui", "ent2_cui", "doc_id", "sents"],
last column is the actual source text.
The entities inside the text MUST be annotated with special
tokens i.e:
...text..[s1] first ent [e1].....[s2] second ent [e2]........
You have to store the start position, aka index position of token
[e1] and also of token [e2] in the (ent1_ent2_start) column.
Parameters:
-
(csv_pathstr) –Path to csv file, must have specific columns, tab separated.
-
(keep_source_textbool, default:False) –If the text clumn should be retained in the 'sents' df column, used for debugging or creating custom datasets.
Returns:
-
dict–{ "output_relations": relation_instances,
NOTE: see create_base_relations_from_doc/csv
for data columns"nclasses": self.config.model.padding_idx, # dummy class "labels2idx": {}, "idx2label": {}}
-
–
}
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_dataset.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
create_base_relations_from_doc
create_base_relations_from_doc(doc: Union[MutableDocument, str], doc_id: str, ent1_ent2_tokens_start_pos: Union[list, tuple] = (-1, -1)) -> dict
Creates a list of tuples based on pairs of entities detected (relation, ent1, ent2) for one spacy document or text string.
Parameters:
-
(docUnion[MutableDocument, str]) –SpacyDoc or string of text, each will get handled slightly differently
-
(doc_idstr) –Document id
-
(ent1_ent2_tokens_start_posUnion[list, tuple], default:(-1, -1)) –Start of [s1][s2] tokens, if left default we assume we are dealing with a SpacyDoc. Defaults to (-1, -1).
Returns:
-
dict(dict) –{
NOTE: see create_base_relations_from_doc/csv
for data columns
"output_relations": relation_instances, "nclasses": self.config.model.padding_idx, # dummy class "labels2idx": {}, "idx2label": {}}
-
dict–}
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_dataset.py
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 | |
create_relations_from_export
Parameters:
-
(datadict) –MedCAT Export data.
Returns:
-
dict–{
NOTE: see create_base_relations_from_doc/csv
for data columns"output_relations": relation_instances, "nclasses": self.config.model.padding_idx, # dummy class "labels2idx": {}, "idx2label": {}}
-
–
}
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_dataset.py
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 | |
generate_base_relations
generate_base_relations(docs: Iterable[MutableDocument]) -> list[dict]
Util function, should be used if you want to train from spacy docs
Parameters:
-
(docsIterable[MutableDocument]) –Generate relations from Spacy CAT docs.
Returns:
-
output_relations(list[dict]) –list[dict] : [] "output_relations": relation_instances,
NOTE: see create_base_relations_from_doc/csv
for data columns"nclasses": self.config.model.padding_idx # dummy class "labels2idx": {}, "idx2label": {}} ]
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_dataset.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
get_labels
classmethod
get_labels(relation_labels: list[str], config: ConfigRelCAT) -> tuple[int, dict[str, int], dict[int, str]]
This is used to update labels in config with unencountered classes/labels ( if any are encountered during training).
Parameters:
-
(relation_labelslist[str]) –new labels to add
-
(configConfigRelCAT) –config
Returns:
-
tuple[int, dict[str, int], dict[int, str]]–tuple[int, dict[str, int], dict[int, str]]: label count, labesl2idx mapping, idx2labels mapping
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_dataset.py
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 | |