medcat.cat
Classes:
-
CAT–This is a collection of serialisable model parts.
-
OutOfDataException–
Attributes:
CAT
CAT(cdb: CDB, vocab: Union[Vocab, None] = None, config: Optional[Config] = None, model_load_path: Optional[str] = None, config_dict: Optional[dict] = None, addon_config_dict: Optional[dict[str, dict]] = None)
Bases: AbstractSerialisable
This is a collection of serialisable model parts.
Methods:
-
add_addon–Add the addon to the model pack an pipe.
-
attempt_unpack–Attempt unpack the zip to a folder and get the model pack path.
-
describe_pipeline– -
get_addon_output–Get the addon output for the entity.
-
get_addons–Get the list of all addons in this model pack.
-
get_addons_of_type–Get a list of addons of a specific type.
-
get_entities–Get the entities recognised and linked within the provided text.
-
get_entities_multi_texts–Get entities from multiple texts (potentially in parallel).
-
get_init_attrs– -
get_model_card–Get the model card either a (nested)
dictor a json string. -
get_required_plugins– -
ignore_attrs– -
load_addons–Load addons based on a model pack path.
-
load_cdb–Loads the concept database from the provided model pack path
-
load_model_card_off_disk–Load the model card off disk as a (nested)
dictor a json string. -
load_model_pack–Load the model pack from file.
-
save_entities_multi_texts–Saves the resulting entities on disk and allows multiprocessing.
-
save_model_card– -
save_model_pack–Save model pack.
Attributes:
-
FORCE_SPAWN_MP– -
cdb– -
config– -
pipe(Pipeline) – -
trainer–The trainer object.
-
usage_monitor– -
vocab–
Source code in medcat-v2/medcat/cat.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
FORCE_SPAWN_MP
class-attribute
instance-attribute
FORCE_SPAWN_MP = True
cdb
instance-attribute
cdb = cdb
config
instance-attribute
config = config
trainer
property
trainer
The trainer object.
vocab
instance-attribute
vocab = vocab
add_addon
add_addon(addon: AddonComponent) -> None
Add the addon to the model pack an pipe.
Parameters:
-
(addonAddonComponent) –The addon to add.
Source code in medcat-v2/medcat/cat.py
1112 1113 1114 1115 1116 1117 1118 1119 | |
attempt_unpack
classmethod
Attempt unpack the zip to a folder and get the model pack path.
If the folder already exists, no unpacking is done.
Parameters:
-
(zip_pathstr) –The ZIP path
Returns:
-
str(str) –The model pack path
Source code in medcat-v2/medcat/cat.py
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 | |
describe_pipeline
describe_pipeline() -> PipelineDescription
Source code in medcat-v2/medcat/cat.py
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 | |
get_addon_output
get_addon_output(ent: MutableEntity) -> dict[str, dict]
Get the addon output for the entity.
This includes a key-value pair for each addon that provides some. Sometimes same-type addons may combine their output under the same key.
Parameters:
-
(entMutableEntity) –The entity in quesiton.
Raises:
-
ValueError–If unable to merge multiple addon output.
Returns:
Source code in medcat-v2/medcat/cat.py
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 614 615 616 617 618 | |
get_addons
get_addons() -> list[AddonComponent]
Get the list of all addons in this model pack.
Returns:
-
list[AddonComponent]–list[AddonComponent]: The list of addons present.
Source code in medcat-v2/medcat/cat.py
1121 1122 1123 1124 1125 1126 1127 | |
get_addons_of_type
get_addons_of_type(addon_type: Type[AddonType]) -> list[AddonType]
Get a list of addons of a specific type.
Parameters:
Returns:
Source code in medcat-v2/medcat/cat.py
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 | |
get_entities
get_entities(text: str, only_cui: Literal[True] = True) -> OnlyCUIEntities
Get the entities recognised and linked within the provided text.
This will run the text through the pipeline and annotated the recognised and linked entities.
Parameters:
-
(textstr) –The text to use.
-
(only_cuibool, default:False) –Whether to only output the CUIs rather than the entire context. Defaults to False.
Returns:
-
Union[dict, Entities, OnlyCUIEntities]–Union[dict, Entities, OnlyCUIEntities]: The entities found and linked within the text.
Source code in medcat-v2/medcat/cat.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
get_entities_multi_texts
get_entities_multi_texts(texts: Union[Iterable[str], Iterable[tuple[str, str]]], only_cui: bool = False, n_process: int = 1, batch_size: int = -1, batch_size_chars: int = 1000000, save_dir_path: Optional[str] = None, batches_per_save: int = 20) -> Iterator[tuple[str, Union[dict, Entities, OnlyCUIEntities]]]
Get entities from multiple texts (potentially in parallel).
If n_process > 1, n_process - 1 new processes will be created
and data will be processed on those as well as the main process in
parallel.
Parameters:
-
(textsUnion[Iterable[str], Iterable[tuple[str, str]]]) –The input text. Either an iterable of raw text or one with in the format of
(text_index, text). -
(only_cuibool, default:False) –Whether to only return CUIs rather than other information like start/end and annotated value. Defaults to False.
-
(n_processint, default:1) –Number of processes to use. Defaults to 1.
-
(batch_sizeint, default:-1) –The number of texts to batch at a time. A batch of the specified size will be given to each worker process. Defaults to -1 and in this case the character count will be used instead.
-
(batch_size_charsint, default:1000000) –The maximum number of characters to process in a batch. Each process will be given batch of texts with a total number of characters not exceeding this value. Defaults to 1,000,000 characters. Set to -1 to disable.
-
(save_dir_pathOptional[str], default:None) –The path to where (if specified) the results are saved. The directory will have a
annotated_ids.picklefile containing the tuple[list[str], int] with a list of indices already saved and then umber of parts already saved. In addition there will be (usually multuple) files in thepart_<num>.pickleformat with the partial outputs. -
(batches_per_saveint, default:20) –The number of patches to save (if
save_dir_pathis specified) at once. Defaults to 20.
Yields:
-
tuple[str, Union[dict, Entities, OnlyCUIEntities]]–Iterator[tuple[str, Union[dict, Entities, OnlyCUIEntities]]]: The results in the format of (text_index, entities).
Source code in medcat-v2/medcat/cat.py
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 | |
get_init_attrs
classmethod
Source code in medcat-v2/medcat/cat.py
100 101 102 | |
get_model_card
Get the model card either a (nested) dict or a json string.
Parameters:
-
(as_dictbool, default:False) –Whether to return as dict. Defaults to False.
Returns:
Source code in medcat-v2/medcat/cat.py
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 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 | |
get_required_plugins
get_required_plugins() -> list[RequiredPluginDescription]
Source code in medcat-v2/medcat/cat.py
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 | |
ignore_attrs
classmethod
Source code in medcat-v2/medcat/cat.py
104 105 106 107 108 109 110 111 | |
load_addons
classmethod
load_addons(model_pack_path: str, addon_config_dict: Optional[dict[str, dict]] = None) -> list[tuple[str, AddonComponent]]
Load addons based on a model pack path.
Parameters:
-
(model_pack_pathstr) –path to model pack, zip or dir.
-
(addon_config_dictOptional[dict], default:None) –The Addon-specific config dict to merge in before pipe initialisation. If specified, it needs to have an addon dict per name. For instance,
{"meta_cat.Subject": {'general': {'device': 'cpu'}}}would apply to the specific MetaCAT.
Returns:
-
list[tuple[str, AddonComponent]]–List[tuple(str, AddonComponent)]: list of pairs of adddon names the addons.
Source code in medcat-v2/medcat/cat.py
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 | |
load_cdb
classmethod
load_cdb(model_pack_path: str) -> CDB
Loads the concept database from the provided model pack path
Parameters:
-
(model_pack_pathstr) –path to model pack, zip or dir.
Returns:
-
CDB(CDB) –The loaded concept database
Source code in medcat-v2/medcat/cat.py
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 | |
load_model_card_off_disk
classmethod
load_model_card_off_disk(model_pack_path: str, as_dict: Literal[True], avoid_unpack: bool = False) -> ModelCard
load_model_card_off_disk(model_pack_path: str, as_dict: Literal[False], avoid_unpack: bool = False) -> str
load_model_card_off_disk(model_pack_path: str, as_dict: bool = False, avoid_unpack: bool = False) -> Union[str, ModelCard]
Load the model card off disk as a (nested) dict or a json string.
Parameters:
-
(model_pack_pathstr) –The path to the model pack (zip or folder).
-
(as_dictbool, default:False) –Whether to return as dict. Defaults to False.
-
(avoid_unpackbool, default:False) –Whether to avoid unpacking the model pack if no previous unpacked path exists. Defaults to False.
Returns:
Source code in medcat-v2/medcat/cat.py
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 | |
load_model_pack
classmethod
load_model_pack(model_pack_path: str, config_dict: Optional[dict] = None, addon_config_dict: Optional[dict[str, dict]] = None) -> CAT
Load the model pack from file.
Parameters:
-
(model_pack_pathstr) –The model pack path.
-
(config_dictOptional[dict], default:None) –The model config to merge in before initialising the pipe. Defaults to None.
-
(addon_config_dictOptional[dict], default:None) –The Addon-specific config dict to merge in before pipe initialisation. If specified, it needs to have an addon dict per name. For instance,
{"meta_cat.Subject": {}}would apply to the specific MetaCAT.
Raises:
-
ValueError–If the saved data does not represent a model pack.
-
MissingPluginError–If required plugins are missing for this model pack.
Returns:
-
CAT(CAT) –The loaded model pack.
Source code in medcat-v2/medcat/cat.py
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 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 876 877 878 879 880 881 882 883 | |
save_entities_multi_texts
save_entities_multi_texts(texts: Union[Iterable[str], Iterable[tuple[str, str]]], save_dir_path: str, only_cui: bool = False, n_process: int = 1, batch_size: int = -1, batch_size_chars: int = 1000000, batches_per_save: int = 20) -> None
Saves the resulting entities on disk and allows multiprocessing.
This uses get_entities_multi_texts under the hood. But it is designed
to save the data on disk as it comes through.
Parameters:
-
(textsUnion[Iterable[str], Iterable[tuple[str, str]]]) –The input text. Either an iterable of raw text or one with in the format of
(text_index, text). -
(save_dir_pathstr) –The path where the results are saved. The directory will have a
annotated_ids.picklefile containing thetuple[list[str], int]with a list of indices already saved and the number of parts already saved. In addition there will be (usually multuple) files in thepart_<num>.pickleformat with the partial outputs. -
(only_cuibool, default:False) –Whether to only return CUIs rather than other information like start/end and annotated value. Defaults to False.
-
(n_processint, default:1) –Number of processes to use. Defaults to 1. The number of texts to batch at a time. A batch of the specified size will be given to each worker process. Defaults to -1 and in this case the character count will be used instead.
-
(batch_size_charsint, default:1000000) –The maximum number of characters to process in a batch. Each process will be given batch of texts with a total number of characters not exceeding this value. Defaults to 1,000,000 characters. Set to -1 to disable.
Source code in medcat-v2/medcat/cat.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | |
save_model_card
save_model_card(model_card_path: str) -> None
Source code in medcat-v2/medcat/cat.py
664 665 666 667 | |
save_model_pack
save_model_pack(target_folder: str, pack_name: str = DEFAULT_PACK_NAME, serialiser_type: Union[str, AvailableSerialisers] = 'dill', make_archive: bool = True, only_archive: bool = False, add_hash_to_pack_name: bool = True, change_description: Optional[str] = None) -> str
Save model pack.
The resulting model pack name will have the hash of the model pack in its name if (and only if) the default model pack name is used.
Parameters:
-
(target_folderstr) –The folder to save the pack in.
-
(pack_namestr, default:DEFAULT_PACK_NAME) –The model pack name. Defaults to DEFAULT_PACK_NAME.
-
(serialiser_typeUnion[str, AvailableSerialisers], default:'dill') –The serialiser type. Defaults to 'dill'.
-
(make_archivebool, default:True) –Whether to make the arhive /.zip file. Defaults to True.
-
(only_archivebool, default:False) –Whether to clear the non-compressed folder. Defaults to False.
-
(add_hash_to_pack_namebool, default:True) –Whether to add the hash to the pack name. This is only relevant if pack_name is specified. Defaults to True.
-
(change_descriptionOptional[str], default:None) –If provided, this the description will be added to the model description. Defaults to None.
Returns:
-
str(str) –The final model pack path.
Source code in medcat-v2/medcat/cat.py
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 | |
OutOfDataException
Bases: ValueError