: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.FilterStrategy medcat.utils.regression.targeting.FilterType medcat.utils.regression.targeting.TypedFilter medcat.utils.regression.targeting.FilterOptions medcat.utils.regression.targeting.CUIWithChildFilter 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) 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) .. py:method:: targets_for(cui) .. py:method:: all_targets(all_cuis, all_names, all_types) Get a generator of all target information objects. This is the starting point for checking cases. :param all_cuis: The set of all CUIs to be queried :type all_cuis: Set[str] :param all_names: The set of all names to be queried :type all_names: Set[str] :param all_types: The set of all type IDs to be queried :type all_types: Set[str] :Yields: *Iterator[Tuple[str, str]]* -- The iterator of the target info .. 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:: get_parents_of(found_cuis, cui, depth = 1) Get the parents of the specifeid CUI in the listed CUIs (if they exist). If needed, higher order parents (i.e grandparents) can be queries for. This uses the `get_children_of` method intenrnally. That is, if any of the found CUIs have the specified CUI as a child of the specified depth, the found CUIs have a parent of the specified depth. :param found_cuis: The list of CUIs to look in :type found_cuis: Iterable[str] :param cui: The target child CUI :type cui: str :param depth: The depth to carry out the search for :type depth: int :Returns: **List[str]** -- The list of parents 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:: FilterStrategy Bases: :py:obj:`enum.Enum` Describes the filter strategy. I.e whether to match all or any of the filters specified. .. py:attribute:: ALL :value: 1 Specified that all filters must be satisfied .. py:attribute:: ANY :value: 2 Specified that any of the filters must be satisfied .. py:method:: match_str(name) :classmethod: Find a loose string match. :param name: The name of the enum :type name: str :Returns: **FilterStrategy** -- The matched FilterStrategy .. py:class:: FilterType Bases: :py:obj:`enum.Enum` The types of targets that can be specified .. py:attribute:: TYPE_ID :value: 1 Filters by specified type_ids .. py:attribute:: CUI :value: 2 Filters by specified CUIs .. py:attribute:: NAME :value: 3 Filters by specified names .. py:attribute:: CUI_AND_CHILDREN :value: 4 Filter by CUI but also allow children, up to a specified distance .. py:method:: match_str(name) :classmethod: Case insensitive matching for FilterType :param name: The naeme to be matched :type name: str :Returns: **FilterType** -- The matched FilterType .. py:class:: TypedFilter Bases: :py:obj:`pydantic.BaseModel` A filter with multiple values to filter against. .. py:attribute:: type :type: FilterType .. py:attribute:: values :type: List[str] .. py:method:: get_applicable_targets(translation, in_gen) Get all applicable targets for this filter :param translation: The translation layer :type translation: TranslationLayer :param in_gen: The input generator / iterator :type in_gen: Iterator[Tuple[str, str]] :Yields: *Iterator[Tuple[str, str]]* -- The output generator .. py:method:: one_from_input(target_type, vals) :classmethod: Get one typed filter from the input target type and values. The values can either a be a string for a single target, a list of strings for multiple targets, or a dict in some more complicated cases (i.e CUI_AND_CHILDREN). :param target_type: The target type as string :type target_type: str :param vals: The values :type vals: Union[str, list, dict] :raises ValueError: If the values are malformed :Returns: **TypedFilter** -- The parsed filter .. py:method:: to_dict() Convert the TypedFilter to a dict to be serialised. :Returns: **dict** -- The dict representation .. py:method:: list_to_dicts(filters) :staticmethod: Create a list of dicts from list of TypedFilters. :param filters: The list of typed filters :type filters: List[TypedFilter] :Returns: **List[dict]** -- The list of dicts .. py:method:: list_to_dict(filters) :staticmethod: Create a single dict from the list of TypedFilters. :param filters: The list of typed filters :type filters: List[TypedFilter] :Returns: **dict** -- The dict .. py:method:: from_dict(input) :classmethod: Construct a list of TypedFilter from a dict. The assumed structure is: {: } or {: [, ]} There can be multiple filter types defined. :param input: The input dict. :type input: Dict[str, Any] :Returns: **List[TypedFilter]** -- The list of constructed TypedFilter .. py:class:: FilterOptions Bases: :py:obj:`pydantic.BaseModel` A class describing the options for the filters .. py:attribute:: strategy :type: FilterStrategy .. py:attribute:: onlyprefnames :type: bool :value: False .. py:method:: to_dict() Convert the FilterOptions to a dict. :Returns: **dict** -- The dict representation .. py:method:: from_dict(section) :classmethod: Construct a FilterOptions instance from a dict. The assumed structure is: {'strategy': <'all' or 'any'>, 'prefname-only': 'true'} Both strategy and prefname-only are optional. :param section: The dict to parse :type section: Dict[str, str] :Returns: **FilterOptions** -- The resulting FilterOptions .. py:class:: CUIWithChildFilter Bases: :py:obj:`TypedFilter` A filter with multiple values to filter against. .. py:attribute:: delegate :type: TypedFilter .. py:attribute:: depth :type: int .. py:attribute:: values :type: List[str] :value: [] .. py:method:: get_applicable_targets(translation, in_gen) Get all applicable targets for this filter :param translation: The translation layer :type translation: TranslationLayer :param in_gen: The input generator / iterator :type in_gen: Iterator[Tuple[str, str]] :Yields: *Iterator[Tuple[str, str]]* -- The output generator .. py:method:: get_children_of(translation, cui, cur_depth) .. py:method:: to_dict() Convert this CUIWithChildFilter to a dict. :Returns: **dict** -- The dict representation