medcat.components.linking.vector_context_model
Classes:
-
ContextModel–Used to learn embeddings for concepts and calculate similarities
-
DisambPreprocessor– -
PerDocumentTokenCache–
Functions:
Attributes:
-
logger–
ContextModel
ContextModel(cui2info: dict[str, CUIInfo], name2info: dict[str, NameInfo], weighted_average_function: Callable[[int], float], vocab: Vocab, config: Linking, name_separator: str, disamb_preprocessors: list[DisambPreprocessor] = [])
Bases: AbstractSerialisable
Used to learn embeddings for concepts and calculate similarities in new documents.
Parameters:
-
(cui2infodict[str, CUIInfo]) –The CUI to info mapping.
-
(name2infodict[str, NameInfo]) –The name to info mapping.
-
(weighted_average_functionCallable[[int], float]) –The weighted average function.
-
(vocabVocab) –The vocabulary
-
(configLinking) –The config to be used
-
(name_separatorstr) –The name separator
Methods:
-
disambiguate– -
get_all_similarities– -
get_context_tokens–Get context tokens for an entity, this will skip anything that
-
get_context_vectors–Given an entity and the document it will return the context
-
similarity–Calculate the similarity between the learnt context for this CUI
-
train–Update the context representation for this CUI, given it's correct
-
train_using_negative_sampling–
Attributes:
-
config– -
cui2info– -
name2info– -
name_separator– -
vocab– -
weighted_average_function–
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
disambiguate
disambiguate(cuis: list[str], entity: MutableEntity, name: str, doc: MutableDocument, per_doc_valid_token_cache: PerDocumentTokenCache) -> tuple[Optional[str], float]
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
308 309 310 311 312 313 314 | |
get_all_similarities
get_all_similarities(cuis: list[str], entity: MutableEntity, name: str, doc: MutableDocument, per_doc_valid_token_cache: PerDocumentTokenCache) -> tuple[Union[list[str], list[None]], list[float], int]
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
get_context_tokens
get_context_tokens(entity: MutableEntity, doc: MutableDocument, size: int, per_doc_valid_token_cache: PerDocumentTokenCache, fill_centre_tokens: bool = True) -> tuple[list[MutableToken], list[MutableToken], list[MutableToken]]
Get context tokens for an entity, this will skip anything that is marked as skip in token._.to_skip
Parameters:
-
(entityBaseEntity) –The entity to look for.
-
(docBaseDocument) –The document look in.
-
(sizeint) –The size of the entity.
-
(per_doc_valid_token_cachePerDocumentTokenCache) –Per document cache for token validation.
Returns:
-
tuple[list[MutableToken], list[MutableToken], list[MutableToken]]–tuple[list[BaseToken], list[BaseToken], list[BaseToken]]: The tokens on the left, centre, and right.
Source code in medcat-v2/medcat/components/linking/vector_context_model.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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
get_context_vectors
get_context_vectors(entity: MutableEntity, doc: MutableDocument, per_doc_valid_token_cache: PerDocumentTokenCache, cui: Optional[str] = None) -> dict[str, ndarray]
Given an entity and the document it will return the context representation for the given entity.
Parameters:
-
(entityBaseEntity) –The entity to look for.
-
(docBaseDocument) –The document to look in.
-
(per_doc_valid_token_cachePerDocumentTokenCache) –Per documnet cache for token validation
-
(cuiOptional[str], default:None) –The CUI or None if not specified.
Returns:
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
similarity
similarity(cui: str, entity: MutableEntity, doc: MutableDocument, per_doc_valid_token_cache: PerDocumentTokenCache) -> float
Calculate the similarity between the learnt context for this CUI
and the context in the given doc.
Parameters:
-
(cuistr) –The CUI.
-
(entityBaseEntity) –The entity to look for.
-
(docBaseDocument) –The document to look in.
-
(per_doc_valid_token_cachePerDocumentTokenCache) –Per document cache for valid tokens
Returns:
-
float(float) –The similarity.
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
train
train(cui: str, entity: MutableEntity, doc: MutableDocument, per_doc_valid_token_cache: PerDocumentTokenCache, negative: bool = False, names: Union[list[str], dict] = []) -> None
Update the context representation for this CUI, given it's correct location (entity) in a document (doc).
Parameters:
-
(cuistr) –The CUI to train.
-
(entityBaseEntity) –The entity we're at.
-
(docBaseDocument) –The document within which we're working.
-
(per_doc_valid_token_cachePerDocumentTokenCache) –Per document cache for token validation.
-
(negativebool, default:False) –Whether or not the example is negative. Defaults to False.
-
(nameslist[str] / dict, default:[]) –Optionally used to update the
statusof a name-cui pair in the CDB.
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
316 317 318 319 320 321 322 323 324 325 326 327 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 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 | |
train_using_negative_sampling
train_using_negative_sampling(cui: str) -> None
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
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 | |
DisambPreprocessor
Bases: Protocol
PerDocumentTokenCache
Bases: dict[MutableToken, bool]
get_lr_linking
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
457 458 459 460 461 462 463 464 465 | |
get_similarity
get_similarity(cur_vectors: dict[str, ndarray], other: dict[str, ndarray], weights: dict[str, float], cui: str, cui2info: dict[str, CUIInfo]) -> float
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
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 | |
get_updated_average_confidence
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
536 537 538 | |
update_context_vectors
update_context_vectors(to_update: dict[str, ndarray], cui: str, new_vecs: dict[str, ndarray], lr: float, negative: bool) -> None
Source code in medcat-v2/medcat/components/linking/vector_context_model.py
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 | |