:py:mod:`medcat.utils.regression.targeting` =========================================== .. py:module:: medcat.utils.regression.targeting Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: medcat.utils.regression.targeting.TranslationLayer medcat.utils.regression.targeting.TargetPlaceholder medcat.utils.regression.targeting.PhraseChanger medcat.utils.regression.targeting.TargetedPhraseChanger medcat.utils.regression.targeting.FinalTarget medcat.utils.regression.targeting.OptionSet Attributes ~~~~~~~~~~ .. autoapisummary:: medcat.utils.regression.targeting.logger medcat.utils.regression.targeting.logger .. py:data:: logger .. py:data:: logger .. py:class:: TranslationLayer(cui2names, name2cuis, cui2type_ids, cui2children, cui2preferred_names, separator, whitespace = ' ') The translation layer for translating: - CUIs to names - names to CUIs - type_ids to CUIs - CUIs to chil CUIs The idea is to decouple these translations from the CDB instance in case something changes there. :param cui2names: The map from CUI to names :type cui2names: Dict[str, Set[str]] :param name2cuis: The map from name to CUIs :type name2cuis: Dict[str, List[str]] :param cui2type_ids: The map from CUI to type_ids :type cui2type_ids: Dict[str, Set[str]] :param cui2children: The map from CUI to child CUIs :type cui2children: Dict[str, Set[str]] .. py:method:: __init__(cui2names, name2cuis, cui2type_ids, cui2children, cui2preferred_names, separator, whitespace = ' ') .. py:method:: get_names_of(cui, only_prefnames) Get the preprocessed names of a CUI. This method preporcesses the names by replacing the separator (generally `~`) with the appropriate whitespace (` `). If the concept is not in the underlying CDB, an empty list is returned. :param cui: The concept in question. :type cui: str :param only_prefnames: Whether to only return a preferred name. :type only_prefnames: bool :Returns: **List[str]** -- The list of names. .. py:method:: get_preferred_name(cui) Get the preferred name of a concept. If no preferred name is found, the random 'first' name is selected. :param cui: The concept ID. :type cui: str :Returns: **str** -- The preferred name. .. py:method:: get_first_name(cui) Get the preprocessed (potentially) arbitrarily first name of the given concept. If the concept does not exist, the CUI itself is returned. PS: The "first" name may not be consistent across runs since it relies on set order. :param cui: The concept ID. :type cui: str :Returns: **str** -- The first name. .. py:method:: get_direct_children(cui) Get the direct children of a concept. This means only the children, but not grandchildren. If the underlying CDB doesn't list children for this CUI, an empty list is returned. :param cui: The concept in question. :type cui: str :Returns: **List[str]** -- The (potentially empty) list of direct children. .. py:method:: get_direct_parents(cui) Get the direct parent(s) of a concept. PS: This method can be quite a CPU heavy one since it relies on running through all the parent-children relationships since the child->parent(s) relationship isn't normally kept track of. :param cui: _description_ :type cui: str :Returns: **List[str]** -- _description_ .. py:method:: get_children_of(found_cuis, cui, depth = 1) Get the children of the specifeid CUI in the listed CUIs (if they exist). :param found_cuis: The list of CUIs to look in :type found_cuis: Iterable[str] :param cui: The target parent CUI :type cui: str :param depth: The depth to carry out the search for :type depth: int :Returns: **List[str]** -- The list of children found .. py:method:: from_CDB(cdb) :classmethod: Construct a TranslationLayer object from a context database (CDB). This translation layer will refer to the same dicts that the CDB refers to. While there is no obvious reason these should be modified, it's something to keep in mind. :param cdb: The CDB :type cdb: CDB :Returns: **TranslationLayer** -- The subsequent TranslationLayer .. py:class:: TargetPlaceholder Bases: :py:obj:`pydantic.BaseModel` A class describing the options for a specific placeholder. .. py:attribute:: placeholder :type: str .. py:attribute:: target_cuis :type: List[str] .. py:attribute:: onlyprefnames :type: bool :value: False .. py:class:: PhraseChanger Bases: :py:obj:`pydantic.BaseModel` The phrase changer. This is class used as a preprocessor for phrases with multiple placeholders. It allows swapping in the rest of the placeholders while leaving in the one that's being tested for. .. py:attribute:: preprocess_placeholders :type: List[Tuple[str, str]] .. py:method:: __call__(phrase) .. py:method:: empty() :classmethod: Gets the empty phrase changer. That is a phrase changer that makes no changes to the phrase. :Returns: **PhraseChanger** -- The empty phrase changer. .. py:class:: TargetedPhraseChanger Bases: :py:obj:`pydantic.BaseModel` The target phrase changer. It includes the phrase changer (for preprocessing) along with the relevant concept and the placeholder it will replace. .. py:attribute:: changer :type: PhraseChanger .. py:attribute:: placeholder :type: str .. py:attribute:: cui :type: str .. py:attribute:: onlyprefnames :type: bool .. py:class:: FinalTarget Bases: :py:obj:`pydantic.BaseModel` The final target. This involves the final phrase (which (potentially) has other placeholder replaced in it), the placeholder to be replaced, and the CUI and specific name being used. .. py:attribute:: placeholder :type: str .. py:attribute:: cui :type: str .. py:attribute:: name :type: str .. py:attribute:: final_phrase :type: str .. py:class:: OptionSet Bases: :py:obj:`pydantic.BaseModel` The targeting option set. This describes all the target placeholders and concepts needed. .. py:attribute:: options :type: List[TargetPlaceholder] .. py:attribute:: allow_any_combinations :type: bool :value: False .. py:method:: from_dict(section) :classmethod: Construct a OptionSet instance from a dict. The assumed structure is: { 'placeholders': [ { 'placeholder': , 'cuis': , 'prefname-only': 'true' }, ], 'any-combination': } The prefname-only key is optional. :param section: The dict to parse :type section: Dict[str, Any] :raises ProblematicOptionSetException: If incorrect number of CUIs when not allowing any combination :raises ProblematicOptionSetException: If placeholders not a list :raises ProblematicOptionSetException: If multiple placehodlers with same place holder :Returns: **OptionSet** -- The resulting OptionSet .. py:method:: to_dict() Convert the OptionSet to a dict. :Returns: **dict** -- The dict representation .. py:method:: _get_all_combinations(cur_opts, other_opts, translation) .. py:method:: estimate_num_of_subcases() Get the number of distinct subcases. This includes ones that can be calculated without the knowledge of the underlying CDB. I.e it doesn't care for the number of names involved per CUI but only takes into account what is described in the option set itself. If any combination is allowed, then the answer is the combination of the number of target concepts per option. If any combination is not allowed, then the answer is simply the number of target concepts for an option (they should all have the same number). :Returns: **int** -- _description_ .. py:method:: get_preprocessors_and_targets(translation) Get the targeted phrase changers. :param translation: The translaton layer. :type translation: TranslationLayer :Yields: *Iterator[TargetedPhraseChanger]* -- Thetarget phrase changers. .. py:exception:: ProblematicOptionSetException(*args) Bases: :py:obj:`ValueError` Inappropriate argument value (of correct type). .. py:method:: __init__(*args) Initialize self. See help(type(self)) for accurate signature.