medcat.cdb

Submodules

Attributes

__all__

Classes

CDB

The abstract serialisable base class.

Package Contents

class medcat.cdb.CDB(config)

Bases: medcat.storage.serialisables.AbstractSerialisable

The abstract serialisable base class.

This defines some common defaults.

Parameters:

config (medcat.config.Config)

__init__(config)
Parameters:

config (medcat.config.Config)

Return type:

None

config
cui2info: dict[str, medcat.cdb.concepts.CUIInfo]
name2info: dict[str, medcat.cdb.concepts.NameInfo]
type_id2info: dict[str, medcat.cdb.concepts.TypeInfo]
token_counts: dict[str, int]
addl_info: dict[str, Any]
_subnames: set[str]
is_dirty = False
has_changed_names = False
classmethod get_init_attrs()
Return type:

list[str]

_reset_subnames()
has_subname(name)

Whether the CDB has the specified subname.

Parameters:

name (str) – The subname to check.

Returns:

bool – Whether the subname is present in this CDB.

Return type:

bool

get_name(cui)

Returns preferred name if it exists, otherwise it will return the longest name assigned to the concept.

Parameters:

cui (str) – Concept ID or unique identifier in this database.

Returns:

str – The name of the concept.

Return type:

str

weighted_average_function(step)

Get the weighted average for steop.

Parameters:

step (int) – The steop.

Returns:

float – The weighted average.

Return type:

float

add_types(types)

Add type info to CDB.

Parameters:

types (Iterable[tuple[str, str]]) – The raw type info.

Return type:

None

add_names(cui, names, name_status=ST.AUTOMATIC, full_build=False)

Adds a name to an existing concept.

Parameters:
  • cui (str) – Concept ID or unique identifier in this database, all concepts that have the same CUI will be merged internally.

  • names (dict[str, NameDescriptor]) –

    Names for this concept, or the value that if found in free text can be linked to this concept. Names is an dict like: `{name: {‘tokens’: tokens, ‘snames’: snames,

    ’raw_name’: raw_name}, …}`

    Names should be generated by helper function ‘medcat.preprocessing.cleaners.prepare_name’

  • name_status (str) – One of P, N, A. Defaults to ‘A’.

  • full_build (bool) – If True the dictionary self.addl_info will also be populated, contains a lot of extra information about concepts, but can be very memory consuming. This is not necessary for normal functioning of MedCAT (Default value False).

Return type:

None

_add_concept_names(cui, names, name_status)
Parameters:
Return type:

None

_add_full_build(cui, names, ontologies, description, type_ids)
Parameters:
Return type:

None

_add_concept(cui, names, ontologies, name_status, type_ids, description, full_build=False)

Add a concept to internal Concept Database (CDB). Depending on what you are providing this will add a large number of properties for each concept.

Parameters:
  • cui (str) – Concept ID or unique identifier in this database, all concepts that have the same CUI will be merged internally.

  • names (dict[str, NameDescriptor]) –

    Names for this concept, or the value that if found in free text can be linked to this concept. Names is a dict like: `{name: {‘tokens’: tokens, ‘snames’: snames,

    ’raw_name’: raw_name}, …}`

    Names should be generated by helper function ‘medcat.preprocessing.cleaners.prepare_name’

  • ontologies (set[str]) – ontologies in which the concept exists (e.g. SNOMEDCT, HPO)

  • name_status (str) – One of P, N, A

  • type_ids (set[str]) – Semantic type identifier (have a look at TUIs in UMLS or SNOMED-CT)

  • description (str) – Description of this concept.

  • full_build (bool) – If True the dictionary self.addl_info will also be populated, contains a lot of extra information about concepts, but can be very memory consuming. This is not necessary for normal functioning of MedCAT (Default Value False).

Return type:

None

reset_training()

Will remove all training efforts - in other words all embeddings that are learnt for concepts in the current CDB. Please note that this does not remove synonyms (names) that were potentially added during supervised/online learning.

Return type:

None

filter_by_cui(cuis_to_keep)

Subset the core CDB fields (dictionaries/maps).

Note that this will potenitally keep a bit more CUIs then in cuis_to_keep. It will first find all names that link to the cuis_to_keep and then find all CUIs that link to those names and keep all of them.

This also will not remove any data from cdb.addl_info - as this field can contain data of unknown structure.

Parameters:

cuis_to_keep (Collection[str]) – CUIs that will be kept, the rest will be removed (not completely, look above).

Raises:

Exception – If no snames and subsetting is not possible.

Return type:

None

remove_cui(cui)

This function takes a CUI and removes it the CDB.

It also removes the CUI from name specific per_cui_status maps as well as well as removes all the names that do not correspond to any CUIs after the removal of this one.

Parameters:

cui (str) – The CUI to remove.

Return type:

None

_remove_names(cui, names)

Remove names from an existing concept - effect is this name will never again be used to link to this concept. This will only remove the name from the linker (namely name2cuis and name2cuis2status), the name will still be present everywhere else. Why? Because it is bothersome to remove it from everywhere, but could also be useful to keep the removed names in e.g. cui2names.

Parameters:
  • cui (str) – Concept ID or unique identifier in this database.

  • names (Iterable[str]) – Names to be removed (e.g list, set, or even a dict (in which case keys will be used)).

Return type:

None

__eq__(other)
Parameters:

other (Any)

Return type:

bool

get_cui2count_train()
Return type:

dict[str, int]

get_name2count_train()
Return type:

dict[str, int]

get_hash()
Return type:

str

get_basic_info()
Return type:

medcat.data.model_card.CDBInfo

save(save_path, serialiser=AvailableSerialisers.dill, overwrite=False)

Save CDB at path.

Parameters:
  • save_path (str) – The path to save at.

  • serialiser (Union[ str, AvailableSerialisers], optional) – The serialiser. Defaults to AvailableSerialisers.dill.

  • overwrite (bool, optional) – Whether to allow overwriting existing files. Defaults to False.

Return type:

None

classmethod load(path)
Parameters:

path (str)

Return type:

CDB

get_strategy()
Return type:

SerialisingStrategy

classmethod ignore_attrs()
Return type:

list[str]

classmethod include_properties()
Return type:

list[str]

medcat.cdb.__all__ = ['CDB']