medcat.utils.regression.checking
Classes:
-
MalformedRegressionCaseException– -
MetaData–The metadata for the regression suite.
-
RegressionCase–A regression case that has a name, defines options, filters and phrases.
-
RegressionSuite–The regression checker.
Functions:
-
fix_np_float64–Fix numpy.float64 in dictionary for yaml saving purposes.
-
get_ontology_and_version–Attempt to get ontology (and its version) from a model card dict.
Attributes:
UNKNOWN_METADATA
module-attribute
UNKNOWN_METADATA = 'Unknown'
MalformedRegressionCaseException
MalformedRegressionCaseException(*args: object)
Bases: ValueError
Source code in medcat-v2/medcat/utils/regression/checking.py
534 535 | |
MetaData
Bases: BaseModel
The metadata for the regression suite.
This should define which ontology (e.g UMLS or SNOMED) as well as which version was used when generating the regression suite.
The metadata may contain further information as well, this may include the annotator(s) involved when converting from MCT export or other relevant data.
Methods:
-
from_modelcard–Generate a MetaData object from a model card.
-
unknown–
Attributes:
-
extra(dict) – -
ontology(str) – -
ontology_version(str) – -
regr_suite_creation_date(str) –
regr_suite_creation_date
class-attribute
instance-attribute
from_modelcard
classmethod
from_modelcard(model_card: dict) -> MetaData
Generate a MetaData object from a model card.
This involves reading ontology info and version from the model card.
It must be noted that the model card should be provided as a dict not a string.
Parameters:
-
(model_carddict) –The CAT modelcard
Returns:
-
MetaData(MetaData) –The resulting MetaData
Source code in medcat-v2/medcat/utils/regression/checking.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
unknown
classmethod
unknown() -> MetaData
Source code in medcat-v2/medcat/utils/regression/checking.py
318 319 320 321 322 | |
RegressionCase
Bases: BaseModel
A regression case that has a name, defines options, filters and phrases.
Methods:
-
check_specific_for_phrase–Checks whether the specific target along with the specified phrase
-
estimate_num_of_diff_subcases– -
from_dict–Construct the regression case from a dict.
-
get_distinct_cases–Gets the various distinct sub-case iterators.
-
to_dict–Converts the RegressionCase to a dict for serialisation.
Attributes:
check_specific_for_phrase
check_specific_for_phrase(cat: CAT, target: FinalTarget, translation: TranslationLayer) -> tuple[Finding, Optional[str]]
Checks whether the specific target along with the specified phrase is able to be identified using the specified model.
Parameters:
-
(catCAT) –The model
-
(targetFinalTarget) –The final target configuration
-
(translationTranslationLayer) –The translation layer
Raises:
-
MalformedRegressionCaseException–If there are too many placeholders in phrase.
Returns:
-
tuple[Finding, Optional[str]]–tuple[Finding, Optional[str]]: The nature to which the target was (or wasn't) identified
Source code in medcat-v2/medcat/utils/regression/checking.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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 78 79 80 | |
estimate_num_of_diff_subcases
estimate_num_of_diff_subcases() -> int
Source code in medcat-v2/medcat/utils/regression/checking.py
82 83 | |
from_dict
classmethod
from_dict(name: str, in_dict: dict) -> RegressionCase
Construct the regression case from a dict.
The expected structure: { 'targeting': { [ # the placeholder to be replaced 'placeholder': '[DIAGNOSIS]' 'cuis': ['cui1', 'cui2'] 'prefname-only': 'false', # optional ] }, 'phrases': ['phrase %s'] # possible multiple }
Parameters:
Raises:
-
ValueError–If the input dict does not have the 'targeting' section
-
ValueError–If there are no phrases defined
Returns:
-
RegressionCase(RegressionCase) –The constructed regression cases.
Source code in medcat-v2/medcat/utils/regression/checking.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
get_distinct_cases
get_distinct_cases(translation: TranslationLayer, edit_distance: tuple[int, int, int], use_diacritics: bool) -> Iterator[Iterator[FinalTarget]]
Gets the various distinct sub-case iterators.
The sub-cases are those that can be determine without the translation layer. However, the translation layer is included here since it streamlines the operation.
Parameters:
-
(translationTranslationLayer) –The translation layer.
-
(edit_distancetuple[int, int, int]) –The edit distance(s) to try.
-
(use_diacriticsbool) –Whether to use diacritics for edit distance.
Yields:
-
Iterator[FinalTarget]–Iterator[Iterator[FinalTarget]]: The iterator of iterators of different sub cases.
Source code in medcat-v2/medcat/utils/regression/checking.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
to_dict
to_dict() -> dict
Converts the RegressionCase to a dict for serialisation.
Returns:
-
dict(dict) –The dict representation
Source code in medcat-v2/medcat/utils/regression/checking.py
162 163 164 165 166 167 168 169 170 171 | |
RegressionSuite
The regression checker. This is used to check a bunch of regression cases at once against a model.
Parameters:
-
(caseslist[RegressionCase]) –The list of regression cases
-
(metadataMetaData) –The metadata for the regression suite
-
(use_reportbool) –Whether or not to use the report functionality. Defaults to False.
Methods:
-
check_model–Checks model and generates a report
-
estimate_total_distinct_cases– -
from_dict–Construct a RegressionChecker from a dict.
-
from_mct_export– -
from_yaml–Constructs a RegressionChcker from a YAML file.
-
get_all_distinct_cases–Gets all the distinct cases for this regression suite.
-
iter_subcases–Iterate over all the sub-cases.
-
to_dict–Converts the RegressionChecker to dict for serialisation.
-
to_yaml–Convert the RegressionChecker to YAML string.
Attributes:
-
cases(list[RegressionCase]) – -
metadata– -
report–
Source code in medcat-v2/medcat/utils/regression/checking.py
356 357 358 359 360 361 362 | |
check_model
check_model(cat: CAT, translation: TranslationLayer, edit_distance: tuple[int, int, int] = (0, 0, 0), use_diacritics: bool = False) -> MultiDescriptor
Checks model and generates a report
Parameters:
-
(catCAT) –The model to check against
-
(translationTranslationLayer) –The translation layer
-
(edit_distancetuple[int, int, int], default:(0, 0, 0)) –The edit distance of the names. Defaults to (0, 0, 0).
-
(use_diacriticsbool, default:False) –Whether to use diacritics for edit distance.
Returns:
-
MultiDescriptor(MultiDescriptor) –A report description
Source code in medcat-v2/medcat/utils/regression/checking.py
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |
estimate_total_distinct_cases
estimate_total_distinct_cases() -> int
Source code in medcat-v2/medcat/utils/regression/checking.py
390 391 | |
from_dict
classmethod
from_dict(in_dict: dict, name: str) -> RegressionSuite
Construct a RegressionChecker from a dict.
Most of the parsing is handled in RegressionChecker.from_dict. This just assumes that each key in the dict is a name and each value describes a RegressionCase.
Parameters:
Returns:
-
RegressionChecker(RegressionSuite) –The built regression checker
Source code in medcat-v2/medcat/utils/regression/checking.py
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 | |
from_mct_export
classmethod
from_mct_export(file_name: str) -> RegressionSuite
Source code in medcat-v2/medcat/utils/regression/checking.py
523 524 525 526 527 528 529 | |
from_yaml
classmethod
from_yaml(file_name: str) -> RegressionSuite
Constructs a RegressionChcker from a YAML file.
The from_dict method is used for the construction from the dict.
Parameters:
-
(file_namestr) –The file name
Returns:
-
RegressionChecker(RegressionSuite) –The constructed regression checker
Source code in medcat-v2/medcat/utils/regression/checking.py
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | |
get_all_distinct_cases
get_all_distinct_cases(translation: TranslationLayer, edit_distance: tuple[int, int, int], use_diacritics: bool) -> Iterator[tuple[RegressionCase, Iterator[FinalTarget]]]
Gets all the distinct cases for this regression suite.
While distinct cases can be determined without the translation layer, including it here simplifies the process.
Parameters:
-
(translationTranslationLayer) –The translation layer.
-
(edit_distancetuple[int, int, int]) –The edit distance(s) to try. Defaults to (0, 0, 0).
-
(use_diacriticsbool) –Whether to use diacritics for edit distance.
Yields:
-
tuple[RegressionCase, Iterator[FinalTarget]]–Iterator[tuple[RegressionCase, Iterator[FinalTarget]]]: The generator of the regression case along with its corresponding sub-cases.
Source code in medcat-v2/medcat/utils/regression/checking.py
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
iter_subcases
iter_subcases(translation: TranslationLayer, show_progress: bool = True, edit_distance: tuple[int, int, int] = (0, 0, 0), use_diacritics: bool = False) -> Iterator[tuple[RegressionCase, FinalTarget]]
Iterate over all the sub-cases.
Each sub-case present a unique target (phrase, concept, name) on the corresponding regression case.
Parameters:
-
(translationTranslationLayer) –The translation layer.
-
(show_progressbool, default:True) –Whether to show progress. Defaults to True.
-
(edit_distancetuple[int, int, int], default:(0, 0, 0)) –The edit distance(s) to try. Defaults to (0, 0, 0).
-
(use_diacriticsbool, default:False) –Whether to use diacritics for edit distance.
Yields:
-
tuple[RegressionCase, FinalTarget]–Iterator[tuple[RegressionCase, FinalTarget]]: The generator of the regression case along with each of the final target sub-cases.
Source code in medcat-v2/medcat/utils/regression/checking.py
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 | |
to_dict
to_dict() -> dict
Converts the RegressionChecker to dict for serialisation.
Returns:
-
dict(dict) –The dict representation
Source code in medcat-v2/medcat/utils/regression/checking.py
450 451 452 453 454 455 456 457 458 459 460 461 462 | |
to_yaml
to_yaml() -> str
Convert the RegressionChecker to YAML string.
Returns:
-
str(str) –The YAML representation
Source code in medcat-v2/medcat/utils/regression/checking.py
464 465 466 467 468 469 470 | |
fix_np_float64
Fix numpy.float64 in dictionary for yaml saving purposes.
These types of objects are unable to be cleanly serialized using yaml. So we need to convert them to the corresponding floats.
The changes will be made within the dictionary itself as well as dictionaries within, recursively.
Parameters:
-
(ddict) –The input dict
Source code in medcat-v2/medcat/utils/regression/checking.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | |
get_ontology_and_version
get_ontology_and_version(model_card: dict) -> tuple[str, str]
Attempt to get ontology (and its version) from a model card dict.
If no ontology is found, 'Unknown' is returned. The version is always returned as the first source ontology. That is, unless the specified location does not exist in the model card, in which case 'Unknown' is returned.
The ontology is assumed to be described at
model_card['Source Ontology'][0] (or model_card['Source Ontology'] if it's a string instead of a list)
The ontology version is read from
model_card['Source Ontology'][0] (or model_card['Source Ontology'] if it's a string instead of a list)
Currently, only SNOMED-CT, UMLS and ICD are supported / found.
Parameters:
-
(model_carddict) –The input model card.
Returns:
-
tuple[str, str]–tuple[str, str]: The ontology (if found) or 'Unknown'; and the version (if found) or 'Unknown'
Source code in medcat-v2/medcat/utils/regression/checking.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |