medcat.components.addons.meta_cat
Modules:
-
data_utils– -
mctokenizers– -
meta_cat– -
ml_utils– -
models–
Classes:
-
MetaAnnotationValue– -
MetaCAT–The MetaCAT class used for training 'Meta-Annotation' models,
-
MetaCATAddon–
Functions:
MetaAnnotationValue
MetaCAT
MetaCAT(tokenizer: Optional[TokenizerWrapperBase] = None, embeddings: Optional[Union[Tensor, ndarray]] = None, config: Optional[ConfigMetaCAT] = None, _model_state_dict: Optional[dict[str, Any]] = None, save_dir_path: Optional[str] = None)
Bases: AbstractSerialisable
The MetaCAT class used for training 'Meta-Annotation' models, i.e. annotations of clinical concept annotations. These are also known as properties or attributes of recognise entities sin similar tools such as MetaMap and cTakes.
This is a flexible model agnostic class that can learns any meta-annotation task, i.e. any multi-class classification task for recognised terms.
Parameters:
-
(tokenizerTokenizerWrapperBase, default:None) –The Huggingface tokenizer instance. This can be a pre-trained tokenzier instance from a BERT-style model, or trained from scratch for the Bi-LSTM (w. attention) model that is currentl used in most deployments.
-
(embeddings(Tensor, ndarray), default:None) –embedding mapping (sub)word input id n-dim (sub)word embedding.
-
(configConfigMetaCAT, default:None) –the configuration for MetaCAT. Param descriptions available in ConfigMetaCAT docs.
Methods:
-
batch_generator–Generator for batch of documents.
-
eval–Evaluate from json.
-
get_ents– -
get_hash–A partial hash trying to catch differences between models.
-
get_init_attrs– -
get_model–Get the model
-
get_model_card–A minimal model card.
-
ignore_attrs– -
include_properties– -
prepare_document–Prepares document.
-
train_from_json–Train or continue training a model give a json_path containing
-
train_raw–Train or continue training a model given raw data. It will continue
Attributes:
-
config– -
embeddings– -
model– -
name– -
save_dir_path– -
tokenizer–
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
embeddings
instance-attribute
embeddings = tensor(embeddings, dtype=float32) if embeddings is not None else None
model
instance-attribute
model = get_model(embeddings=embeddings)
name
class-attribute
instance-attribute
name = 'meta_cat'
save_dir_path
instance-attribute
save_dir_path = save_dir_path
batch_generator
staticmethod
batch_generator(stream: Iterable[MutableDocument], batch_size_chars: int) -> Iterable[list[MutableDocument]]
Generator for batch of documents.
Parameters:
-
(streamIterable[MutableDocument]) –The document stream
-
(batch_size_charsint) –Number of characters per batch
Yields:
-
Iterable[list[MutableDocument]]–list[MutableDocument]: The batch of documents.
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
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 | |
eval
eval(json_path: str) -> EvalModelResults
Evaluate from json.
Parameters:
-
(json_pathstr) –The json file ath
Returns:
-
EvalModelResults(EvalModelResults) –The resulting model dict
Raises:
-
AssertionError–If self.tokenizer
-
Exception–If the category name does not exist
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 | |
get_ents
get_ents(doc: MutableDocument) -> Iterable[MutableEntity]
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
683 684 685 | |
get_hash
get_hash() -> str
A partial hash trying to catch differences between models.
Returns:
-
str(str) –The hex hash.
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
387 388 389 390 391 392 393 394 395 396 397 398 399 | |
get_init_attrs
classmethod
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
295 296 297 298 | |
get_model
get_model(embeddings: Optional[Tensor]) -> Module
Get the model
Parameters:
-
(embeddingsOptional[Tensor]) –The embedding densor
Raises:
-
ValueError–If the meta model is not LSTM or BERT
Returns:
-
Module–nn.Module: The module
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
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 378 379 380 381 382 383 384 385 | |
get_model_card
A minimal model card.
Parameters:
-
(as_dictbool, default:False) –Return the model card as a dictionary instead of a str. Defaults to
False.
Returns:
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 | |
ignore_attrs
classmethod
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
300 301 302 | |
include_properties
classmethod
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
304 305 306 | |
prepare_document
prepare_document(doc: MutableDocument, input_ids: list, offset_mapping: list, lowercase: bool) -> tuple[dict, list]
Prepares document.
Parameters:
-
(docDoc) –The document
-
(input_idslist) –Input ids
-
(offset_mappinglist) –Offset mappings
-
(lowercasebool) –Whether to use lower case replace center
Returns:
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
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 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 | |
train_from_json
train_from_json(json_path: Union[str, list], save_dir_path: Optional[str] = None, data_oversampled: Optional[list] = None, overwrite: bool = False) -> dict
Train or continue training a model give a json_path containing a MedCATtrainer export. It will continue training if an existing model is loaded or start new training if the model is blank/new.
Parameters:
-
(json_pathUnion[str, list]) –Path/Paths to a MedCATtrainer export containing the meta_annotations we want to train for.
-
(save_dir_pathOptional[str], default:None) –In case we have aut_save_model (meaning during the training the best model will be saved) we need to set a save path. Defaults to
None. -
(data_oversampledOptional[list], default:None) –In case of oversampling being performed, the data will be passed in the parameter allowing the model to be trained on original + synthetic data.
-
(overwritebool, default:False) –Whether to allow overwriting the file if/when appropriate.
Returns:
-
dict(dict) –The resulting report.
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
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 | |
train_raw
train_raw(data_loaded: dict, save_dir_path: Optional[str] = None, data_oversampled: Optional[list] = None, overwrite: bool = False) -> dict
Train or continue training a model given raw data. It will continue training if an existing model is loaded or start new training if the model is blank/new.
The raw data is expected in the following format:
{
'projects': [ # list of projects
{
'name': '
Parameters:
-
(data_loadeddict) –The raw data we want to train for.
-
(save_dir_pathOptional[str], default:None) –In case we have aut_save_model (meaning during the training the best model will be saved) we need to set a save path. Defaults to
None. -
(data_oversampledOptional[list], default:None) –In case of oversampling being performed, the data will be passed in the parameter allowing the model to be trained on original + synthetic data. The format of which is expected: [[['text','of','the','document'], [index of medical entity], "label" ], ['text','of','the','document'], [index of medical entity], "label" ]]
-
(overwritebool, default:False) –Whether to allow overwriting the file if/when appropriate.
Returns:
-
dict(dict) –The resulting report.
Raises:
-
Exception–If no save path is specified, or category name not in data.
-
AssertionError–If no tokeniser is set
-
FileNotFoundError–If phase_number is set to 2 and model.dat file is not found
-
KeyError–If phase_number is set to 2 and model.dat file contains mismatched architecture
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
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 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 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 | |
MetaCATAddon
MetaCATAddon(config: ConfigMetaCAT, base_tokenizer: BaseTokenizer, meta_cat: Optional[MetaCAT])
Bases: AddonComponent
Methods:
-
create_new–Factory method to create a new MetaCATAddon instance.
-
create_new_component– -
deserialise_from– -
get_hash– -
get_init_attrs– -
get_output_key_val– -
get_strategy– -
ignore_attrs– -
include_properties– -
load– -
load_existing–Factory method to load an existing MetaCATAddon from disk.
-
save– -
serialise_to–
Attributes:
-
DEFAULT_TOKENIZER– -
addon_type– -
config(ConfigMetaCAT) – -
include_in_output(bool) – -
mc(MetaCAT) – -
name(str) – -
output_key–
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
69 70 71 72 73 74 | |
DEFAULT_TOKENIZER
class-attribute
instance-attribute
DEFAULT_TOKENIZER = 'spacy'
addon_type
class-attribute
instance-attribute
addon_type = 'meta_cat'
output_key
class-attribute
instance-attribute
output_key = 'meta_anns'
create_new
classmethod
create_new(config: ConfigMetaCAT, base_tokenizer: BaseTokenizer, tknzer_preprocessor: TokenizerPreprocessor = None) -> MetaCATAddon
Factory method to create a new MetaCATAddon instance.
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
82 83 84 85 86 87 88 89 90 91 | |
create_new_component
classmethod
create_new_component(cnf: ComponentConfig, tokenizer: BaseTokenizer, cdb: CDB, vocab: Vocab, model_load_path: Optional[str]) -> MetaCATAddon
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
deserialise_from
classmethod
deserialise_from(folder_path: str, **init_kwargs) -> MetaCATAddon
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
get_hash
get_hash() -> str
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
256 257 258 259 260 | |
get_init_attrs
classmethod
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
244 245 246 | |
get_output_key_val
get_output_key_val(ent: MutableEntity) -> tuple[str, dict[str, MetaAnnotationValue]]
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
172 173 174 175 176 177 | |
get_strategy
get_strategy() -> SerialisingStrategy
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
241 242 | |
ignore_attrs
classmethod
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
248 249 250 | |
include_properties
classmethod
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
252 253 254 | |
load
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
126 127 128 129 130 131 | |
load_existing
classmethod
load_existing(cnf: ConfigMetaCAT, base_tokenizer: BaseTokenizer, load_path: str) -> MetaCATAddon
Factory method to load an existing MetaCATAddon from disk.
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
110 111 112 113 114 115 116 117 | |
save
save(folder_path: str) -> None
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
serialise_to
serialise_to(folder_path: str) -> None
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
181 182 183 | |
get_meta_annotations
get_meta_annotations(entity: MutableEntity) -> dict[str, MetaAnnotationValue]
Source code in medcat-v2/medcat/components/addons/meta_cat/meta_cat.py
263 264 265 | |