medcat.utils.regression.results
Classes:
-
Finding–Describes whether or how the finding verified.
-
FindingDeterminer–A helper class to determine the type of finding.
-
MalformedFinding– -
MultiDescriptor–The descriptor of results over multiple different results (parts).
-
ResultDescriptor–The overarching result descriptor that handles multiple phrases.
-
SingleResultDescriptor–The result descriptor.
-
Strictness–The total strictness on which to judge the results.
Attributes:
-
STRICTNESS_MATRIX(dict[Strictness, set[Finding]]) –
STRICTNESS_MATRIX
module-attribute
STRICTNESS_MATRIX: dict[Strictness, set[Finding]] = {STRICTEST: {IDENTICAL}, STRICT: {IDENTICAL, FOUND_ANY_CHILD}, NORMAL: {IDENTICAL, FOUND_ANY_CHILD, FOUND_CHILD_PARTIAL, BIGGER_SPAN_RIGHT, BIGGER_SPAN_LEFT, BIGGER_SPAN_BOTH, SMALLER_SPAN, PARTIAL_OVERLAP}, LENIENT: {IDENTICAL, FOUND_ANY_CHILD, BIGGER_SPAN_RIGHT, BIGGER_SPAN_LEFT, BIGGER_SPAN_BOTH, SMALLER_SPAN, PARTIAL_OVERLAP, FOUND_DIR_PARENT, FOUND_DIR_GRANDPARENT}, ANYTHING: set(Finding)}
Finding
Bases: Enum
Describes whether or how the finding verified.
The idea is that we know where we expect the entity to be recognised and the enum constants describe how the recognition compared to the expectation.
In essence, we want to know the relative positions of the two pairs of numbers (character numbers): - Expected Start, Expected End - Recognised Start, Recognised End
We can model this as 4 numbers on the number line. And we want to know their position relative to each other. For example, if the expected positions are marked with * and recognised positions with #, we may have something like: __#_#_____ Which would indicate that there is a partial, but smaller span recognised.
Methods:
-
determine–Determine the finding type based on the input
-
has_correct_cui–Whether the finding found the correct concept.
Attributes:
-
BIGGER_SPAN_BOTH–The CUI is the same, but the recognised span is longer on both sides.
-
BIGGER_SPAN_LEFT–The CUI is the same, but the recognised span is longer on the left.
-
BIGGER_SPAN_RIGHT–The CUI is the same, but the recognised span is longer on the right.
-
FAIL–The concept was not recognised in any meaningful way.
-
FOUND_ANY_CHILD–The recognised CUI is a child of the expected CUI but the span is an exact match.
-
FOUND_CHILD_PARTIAL–The recognised CUI is a child yet the match is only partial (smaller/bigger/partial).
-
FOUND_DIR_GRANDPARENT–The recognised CUI is a grandparent of the expected CUI but the span is an exact match.
-
FOUND_DIR_PARENT–The recognised CUI is a parent of the expected CUI but the span is an exact match.
-
FOUND_OTHER–Found another CUI in the same span.
-
IDENTICAL–The CUI and the span recognised are identical to what was expected.
-
PARTIAL_OVERLAP–The CUI is the same, but the span overlaps partially.
-
SMALLER_SPAN–The CUI is the same, but the recognised span is smaller.
BIGGER_SPAN_BOTH
class-attribute
instance-attribute
BIGGER_SPAN_BOTH = auto()
The CUI is the same, but the recognised span is longer on both sides.
If we use the notation from the class doc string, e.g: #______#
BIGGER_SPAN_LEFT
class-attribute
instance-attribute
BIGGER_SPAN_LEFT = auto()
The CUI is the same, but the recognised span is longer on the left.
If we use the notation from the class doc string, e.g: #__#_
BIGGER_SPAN_RIGHT
class-attribute
instance-attribute
BIGGER_SPAN_RIGHT = auto()
The CUI is the same, but the recognised span is longer on the right.
If we use the notation from the class doc string, e.g: _#____#
FAIL
class-attribute
instance-attribute
FAIL = auto()
The concept was not recognised in any meaningful way.
FOUND_ANY_CHILD
class-attribute
instance-attribute
FOUND_ANY_CHILD = auto()
The recognised CUI is a child of the expected CUI but the span is an exact match.
FOUND_CHILD_PARTIAL
class-attribute
instance-attribute
FOUND_CHILD_PARTIAL = auto()
The recognised CUI is a child yet the match is only partial (smaller/bigger/partial).
FOUND_DIR_GRANDPARENT
class-attribute
instance-attribute
FOUND_DIR_GRANDPARENT = auto()
The recognised CUI is a grandparent of the expected CUI but the span is an exact match.
FOUND_DIR_PARENT
class-attribute
instance-attribute
FOUND_DIR_PARENT = auto()
The recognised CUI is a parent of the expected CUI but the span is an exact match.
FOUND_OTHER
class-attribute
instance-attribute
FOUND_OTHER = auto()
Found another CUI in the same span.
IDENTICAL
class-attribute
instance-attribute
IDENTICAL = auto()
The CUI and the span recognised are identical to what was expected.
PARTIAL_OVERLAP
class-attribute
instance-attribute
PARTIAL_OVERLAP = auto()
The CUI is the same, but the span overlaps partially.
If we use the notation from the class doc string, e.g: _#__# (starts between expected start and end, but ends beyond) #_#__ (start before expected start, but ends between expected start and end)
SMALLER_SPAN
class-attribute
instance-attribute
SMALLER_SPAN = auto()
The CUI is the same, but the recognised span is smaller.
If we use the notation from the class doc string, e.g: ##_ (neither start nor end match) ##__ (start matches, but end is before expected) __#_#_ (end matches, but start is after expected)
determine
classmethod
determine(exp_cui: str, exp_start: int, exp_end: int, tl: TranslationLayer, found_entities: dict[int, Entity], strict_only: bool = False, check_children: bool = True, check_parent: bool = True, check_grandparent: bool = True) -> tuple[Finding, Optional[str]]
Determine the finding type based on the input
Parameters:
-
(exp_cuistr) –Expected CUI.
-
(exp_startint) –Expected span start.
-
(exp_endint) –Expected span end.
-
(tlTranslationLayer) –The translation layer.
-
(found_entitiesdict[int, Entity]) –The entities found by the model.
-
(strict_onlybool, default:False) –Whether to use a strict-only mode (either identical or fail). Defaults to False.
-
(check_childrenbool, default:True) –Whether to check the children. Defaults to True.
-
(check_parentbool, default:True) –Whether to check for parent(s). Defaults to True.
-
(check_grandparentbool, default:True) –Whether to check for grandparent(s). Defaults to True.
Returns:
-
tuple[Finding, Optional[str]]–tuple['Finding', Optional[str]]: The type of finding determined, and the alternative.
Source code in medcat-v2/medcat/utils/regression/results.py
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 | |
has_correct_cui
has_correct_cui() -> bool
Whether the finding found the correct concept.
Returns:
-
bool(bool) –Whether the correct concept was found.
Source code in medcat-v2/medcat/utils/regression/results.py
76 77 78 79 80 81 82 83 84 85 86 87 | |
FindingDeterminer
FindingDeterminer(exp_cui: str, exp_start: int, exp_end: int, tl: TranslationLayer, found_entities: dict[int, Entity], strict_only: bool = False, check_children: bool = True, check_parent: bool = True, check_grandparent: bool = True)
A helper class to determine the type of finding.
This is mostly useful to split the responsibilities of looking at children/parents as well as to keep track of the already-checked children to avoid infinite recursion (which could happen in - e.g - a SNOMED model).
Parameters:
-
(exp_cuistr) –The expected CUI.
-
(exp_startint) –The expected span start.
-
(exp_endint) –The expected span end.
-
(tlTranslationLayer) –The translation layer.
-
(found_entitiesdict[str, Entity]) –The entities found by the model.
-
(strict_onlybool, default:False) –Whether to use strict-only mode (either identical or fail). Defaults to False.
-
(check_childrenbool, default:True) –Whether or not to check the children. Defaults to True.
-
(check_parentbool, default:True) –Whether to check for parent(s). Defaults to True.
-
(check_grandparentbool, default:True) –Whether to check for granparent(s). Defaults to True.
Methods:
-
determine–Determine the finding based on the given information.
Attributes:
-
check_children– -
check_grandparent– -
check_parent– -
exp_cui– -
exp_end– -
exp_start– -
found_entities– -
strict_only– -
tl–
Source code in medcat-v2/medcat/utils/regression/results.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
determine
Determine the finding based on the given information.
First, the strict check is done (either identical or not). Then, parents are checked (if required). After that, children are checked (if required).
Returns:
-
tuple[Finding, Optional[str]]–tuple[Finding, Optional[str]]: The appropriate finding, and the alternative (if applicable).
Source code in medcat-v2/medcat/utils/regression/results.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
MalformedFinding
MalformedFinding(*args: object)
Bases: ValueError
Source code in medcat-v2/medcat/utils/regression/results.py
775 776 | |
MultiDescriptor
Bases: BaseModel
The descriptor of results over multiple different results (parts).
The idea is that this would likely be used with a regression suite and it would incorporate all the different regression cases it describes.
Methods:
-
calculate_report–Calculate some of the major parts of the report.
-
get_report–Get the report associated with this descriptor
-
iter_examples–Iterate over all relevant examples.
-
model_dump–
Attributes:
-
findings(dict[Finding, int]) –The total findings.
-
name(str) –The name of the collection being checked
-
parts(list[ResultDescriptor]) –The parts kept track of
findings
property
calculate_report
calculate_report(phrases_separately: bool = False, hide_empty: bool = False, examples_strictness: Optional[Strictness] = STRICTEST, strictness: Strictness = NORMAL, phrase_max_len: int = 80) -> tuple[int, int, int, str, int]
Calculate some of the major parts of the report.
Parameters:
-
(phrases_separatelybool, default:False) –Whether to include per-phrase information
-
(hide_emptybool, default:False) –Whether to hide empty cases
-
(examples_strictnessOptional[STRICTEST], default:STRICTEST) –What level of strictness to show for examples. Set to None to disable examples. Defaults to Strictness.STRICTEST.
-
(strictnessStrictness, default:NORMAL) –The strictness of the success / fail overview. Defaults to Strictness.NORMAL.
-
(phrase_max_lenint, default:80) –The maximum length of the phrase in examples. Defaults to 80.
Returns:
-
tuple[int, int, int, str, int]–tuple[int, int, int, int, str]: The total number of examples, the total successes, the total failures, the delegated part, and the number of empty
Source code in medcat-v2/medcat/utils/regression/results.py
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 682 683 684 685 686 687 688 689 690 | |
get_report
get_report(phrases_separately: bool, hide_empty: bool = False, examples_strictness: Optional[Strictness] = STRICTEST, strictness: Strictness = NORMAL, phrase_max_len: int = 80) -> str
Get the report associated with this descriptor
Parameters:
-
(phrases_separatelybool) –Whether to include per-phrase information
-
(hide_emptybool, default:False) –Whether to hide empty cases
-
(examples_strictnessOptional[STRICTEST], default:STRICTEST) –What level of strictness to show for examples. Set to None to disable examples. Defaults to Strictness.STRICTEST.
-
(strictnessStrictness, default:NORMAL) –The strictness of the success / fail overview. Defaults to Strictness.NORMAL.
-
(phrase_max_lenint, default:80) –The maximum length of the phrase in examples. Defaults to 80.
Returns:
-
str(str) –The report string
Source code in medcat-v2/medcat/utils/regression/results.py
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 | |
iter_examples
iter_examples(strictness_threshold: Strictness) -> Iterable[tuple[FinalTarget, tuple[Finding, Optional[str]]]]
Iterate over all relevant examples.
Only examples that are not in the strictness matrix for the specified threshold will be used.
Parameters:
-
(strictness_thresholdStrictness) –The threshold of avoidance.
Yields:
-
Iterable[tuple[FinalTarget, tuple[Finding, Optional[str]]]]–Iterable[tuple[FinalTarget, tuple[Finding, Optional[str]]]]: The examples
Source code in medcat-v2/medcat/utils/regression/results.py
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 | |
model_dump
model_dump(**kwargs) -> dict
Source code in medcat-v2/medcat/utils/regression/results.py
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 | |
ResultDescriptor
Bases: SingleResultDescriptor
The overarching result descriptor that handles multiple phrases.
This class keeps track of the results on a per-phrase basis and can be used to get the overall report and/or iterate over examples.
Methods:
-
get_report–Get the report associated with this descriptor
-
iter_examples–Iterate suitable examples.
-
model_dump– -
report–Report a test case and its successfulness
Attributes:
per_phrase_results
class-attribute
instance-attribute
per_phrase_results: dict[str, SingleResultDescriptor] = {}
get_report
get_report(phrases_separately: bool = False) -> str
Get the report associated with this descriptor
Parameters:
-
(phrases_separatelybool, default:False) –Whether to output descriptor for each phrase separately
Returns:
-
str(str) –The report string
Source code in medcat-v2/medcat/utils/regression/results.py
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | |
iter_examples
iter_examples(strictness_threshold: Strictness) -> Iterable[tuple[FinalTarget, tuple[Finding, Optional[str]]]]
Iterate suitable examples.
The strictness threshold at which to include examples.
Any finding that is assumed to be "correct enough" according to the strictness matrix for this threshold will be withheld from examples.
In simpler terms, if the finding is NOT in the strictness matrix for this strictness, the example is recorded.
To disable example keeping, set the threshold to
Strictness.ANYTHING.
Parameters:
-
(strictness_thresholdStrictness) –The strictness threshold.
Yields:
-
Iterable[tuple[FinalTarget, tuple[Finding, Optional[str]]]]–Iterable[tuple[FinalTarget, tuple[Finding, Optional[str]]]]: The placeholder, phrase, finding, CUI, and name.
Source code in medcat-v2/medcat/utils/regression/results.py
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 | |
model_dump
model_dump(**kwargs) -> dict
Source code in medcat-v2/medcat/utils/regression/results.py
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 | |
report
Report a test case and its successfulness
Parameters:
-
(targetFinalTarget) –The final targe configuration
-
(findingtuple[Finding, Optional[str]]) –To what extent the concept was recognised
Source code in medcat-v2/medcat/utils/regression/results.py
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | |
SingleResultDescriptor
Bases: BaseModel
The result descriptor.
This class is responsible for keeping track of all the findings (i.e how many were found to be identical) as well as the examples of the finding on a per-target basis for further analysis.
Methods:
-
get_report–Get the report associated with this descriptor
-
json– -
model_dump– -
report_success–Report a test case and its successfulness.
Attributes:
-
examples(list[tuple[FinalTarget, tuple[Finding, Optional[str]]]]) –The examples of non-perfect alignment.
-
findings(dict[Finding, int]) –The description of failures
-
name(str) –The name of the part that was checked
examples
class-attribute
instance-attribute
The examples of non-perfect alignment.
findings
class-attribute
instance-attribute
The description of failures
get_report
get_report() -> str
Get the report associated with this descriptor
Returns:
-
str(str) –The report string
Source code in medcat-v2/medcat/utils/regression/results.py
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
json
json(**kwargs) -> str
Source code in medcat-v2/medcat/utils/regression/results.py
442 443 444 | |
model_dump
model_dump(**kwargs) -> dict
Source code in medcat-v2/medcat/utils/regression/results.py
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 | |
report_success
Report a test case and its successfulness.
Parameters:
-
(targetFinalTarget) –The target configuration
-
(foundtuple[Finding, Optional[str]]) –Whether or not the check was successful
Source code in medcat-v2/medcat/utils/regression/results.py
377 378 379 380 381 382 383 384 385 386 387 388 389 390 | |
Strictness
Bases: Enum
The total strictness on which to judge the results.
Attributes:
-
ANYTHING–Anything stricness allows ANY finding.
-
LENIENT–Lenient stictness also allows parents and grandparents.
-
NORMAL–Normal strictness also allows partial overlaps on target concept and children.
-
STRICT–A strict option which allows identical or children.
-
STRICTEST–The strictest option which only allows identical findings.
ANYTHING
class-attribute
instance-attribute
ANYTHING = auto()
Anything stricness allows ANY finding.
This would generally only be relevant when disabling examples for results descriptors.
LENIENT
class-attribute
instance-attribute
LENIENT = auto()
Lenient stictness also allows parents and grandparents.
NORMAL
class-attribute
instance-attribute
NORMAL = auto()
Normal strictness also allows partial overlaps on target concept and children.
STRICT
class-attribute
instance-attribute
STRICT = auto()
A strict option which allows identical or children.
STRICTEST
class-attribute
instance-attribute
STRICTEST = auto()
The strictest option which only allows identical findings.