medcat.components.addons.relation_extraction.rel_cat
Classes:
-
BalancedBatchSampler– -
RelCAT–The RelCAT class used for training 'Relation-Annotation' models, i.e.,
-
RelCATAddon–
Attributes:
-
logger–
BalancedBatchSampler
BalancedBatchSampler(dataset, classes, batch_size, max_samples, max_minority)
Bases: Sampler
Attributes:
-
batch_size– -
classes– -
dataset– -
indices– -
max_minority– -
max_samples_per_class– -
num_classes–
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
192 193 194 195 196 197 198 199 200 201 202 | |
batch_size
instance-attribute
batch_size = batch_size
classes
instance-attribute
classes = classes
dataset
instance-attribute
dataset = dataset
max_minority
instance-attribute
max_minority = max_minority
max_samples_per_class
instance-attribute
max_samples_per_class = max_samples
RelCAT
RelCAT(base_tokenizer: BaseTokenizer, cdb: CDB, config: ConfigRelCAT = ConfigRelCAT(), task: str = 'train', init_model: bool = False)
The RelCAT class used for training 'Relation-Annotation' models, i.e., annotation of relations between clinical concepts.
Parameters:
-
(cdbCDB) –cdb, this is used when creating relation datasets.
-
(tokenizerTokenizerWrapperBERT) –The Huggingface tokenizer instance. This can be a pre-trained tokenzier instance from a BERT-style model. For now, only BERT models are supported.
-
(configConfigRelCAT, default:ConfigRelCAT()) –the configuration for RelCAT. Param descriptions available in ConfigRelCAT class docs.
-
(taskstr, default:'train') –What task is this model supposed to handle. Defaults to "train"
-
(init_modelbool, default:False) –loads default model. Defaults to False.
Methods:
-
evaluate_– -
evaluate_results– -
load– -
pipe– -
predict_text_with_anns–Creates spacy doc from text and annotation input.
-
save– -
train–
Attributes:
-
addon_type– -
base_tokenizer– -
cdb– -
checkpoint_path(str) – -
component– -
device– -
is_cuda_available– -
output_key– -
task(str) –
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
addon_type
class-attribute
instance-attribute
addon_type = 'rel_cat'
base_tokenizer
instance-attribute
base_tokenizer = base_tokenizer
device
instance-attribute
device = device('cuda' if is_cuda_available and device != 'cpu' else 'cpu')
is_cuda_available
instance-attribute
is_cuda_available = is_available()
output_key
class-attribute
instance-attribute
output_key = 'rel_'
evaluate_
evaluate_(output_logits, labels, ignore_idx)
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 | |
evaluate_results
evaluate_results(data_loader, pad_id)
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 | |
load
classmethod
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
pipe
pipe(stream: Iterable[MutableDocument], *args, **kwargs) -> Iterator[MutableDocument]
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
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 876 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 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 | |
predict_text_with_anns
predict_text_with_anns(text: str, annotations: list[dict]) -> MutableDocument
Creates spacy doc from text and annotation input. Predicts using self.call
Parameters:
-
(textstr) –text
-
(annotationsdict) –dict containing the entities from NER (of your choosing), the format must be the following format: [ { "cui": "202099003", -this is optional "value": "discoid lateral meniscus", "start": 294, "end": 318 }, { "cui": "202099003", "value": "Discoid lateral meniscus", "start": 1905, "end": 1929, } ]
Returns:
-
Doc(MutableDocument) –spacy doc with the relations.
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 | |
save
save(save_path: str = './') -> None
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
285 286 | |
train
train(export_data_path: str = '', train_csv_path: str = '', test_csv_path: str = '', checkpoint_path: str = './')
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | |
RelCATAddon
RelCATAddon(config: ConfigRelCAT, rel_cat: RelCAT)
Bases: AddonComponent
Methods:
-
create_new–Factory method to create a new MetaCATAddon instance.
-
create_new_component– -
deserialise_from–Deserialise a RelCAT addon from disk.
-
get_init_attrs– -
get_strategy– -
ignore_attrs– -
include_properties– -
load_existing–Factory method to load an existing RelCAT addon from disk.
-
serialise_to–
Attributes:
-
DEFAULT_TOKENIZER– -
addon_type– -
config(ConfigRelCAT) – -
name(str) – -
output_key–
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
50 51 52 53 | |
DEFAULT_TOKENIZER
class-attribute
instance-attribute
DEFAULT_TOKENIZER = 'spacy'
addon_type
class-attribute
instance-attribute
addon_type = 'rel_cat'
output_key
class-attribute
instance-attribute
output_key = 'relations'
create_new
classmethod
create_new(config: ConfigRelCAT, base_tokenizer: BaseTokenizer, cdb: CDB) -> RelCATAddon
Factory method to create a new MetaCATAddon instance.
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
55 56 57 58 59 60 | |
create_new_component
classmethod
create_new_component(cnf: ComponentConfig, tokenizer: BaseTokenizer, cdb: CDB, vocab: Vocab, model_load_path: Optional[str]) -> RelCATAddon
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
62 63 64 65 66 67 68 69 70 71 72 73 74 | |
deserialise_from
classmethod
deserialise_from(folder_path: str, **init_kwargs) -> RelCATAddon
Deserialise a RelCAT addon from disk.
Mirrors MetaCATAddon.deserialise_from: when called via the
pipeline, tokenizer/cnf are supplied; when called standalone
(e.g. CAT.load_addons), they are inferred from disk so that
deserialisation works without full pipeline context.
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
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 | |
get_init_attrs
classmethod
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
174 175 176 | |
get_strategy
get_strategy() -> SerialisingStrategy
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
171 172 | |
ignore_attrs
classmethod
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
178 179 180 | |
include_properties
classmethod
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
182 183 184 | |
load_existing
classmethod
load_existing(cnf: ConfigRelCAT, base_tokenizer: BaseTokenizer, cdb: Optional[CDB], load_path: str) -> RelCATAddon
Factory method to load an existing RelCAT addon from disk.
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
76 77 78 79 80 81 82 83 84 85 86 | |
serialise_to
serialise_to(folder_path: str) -> None
Source code in medcat-v2/medcat/components/addons/relation_extraction/rel_cat.py
88 89 90 | |