Skip to content

medcat.pipeline

Modules:

Classes:

  • Pipeline

    The pipeline for the NLP process.

Pipeline

Pipeline(cdb: CDB, vocab: Optional[Vocab], model_load_path: Optional[str], old_pipe: Optional[Pipeline] = None, addon_config_dict: Optional[dict[str, dict]] = None)

The pipeline for the NLP process.

This class is responsible to initial creation of the NLP document, as well as running through of all the components and addons.

Methods:

Attributes:

Source code in medcat/medcat/pipeline/pipeline.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def __init__(self, cdb: CDB, vocab: Optional[Vocab],
             model_load_path: Optional[str],
             # NOTE: upon reload, old pipe can be useful
             old_pipe: Optional['Pipeline'] = None,
             addon_config_dict: Optional[dict[str, dict]] = None):
    self.cdb = cdb
    # NOTE: Vocab is None in case of DeID models and thats fine then,
    #       but it should be non-None otherwise
    self.vocab: Vocab = vocab  # type: ignore
    self.config = self.cdb.config
    self._tokenizer = self._init_tokenizer(model_load_path)
    self._components: list[CoreComponent] = []
    self._addons: list[AddonComponent] = []
    self._init_components(model_load_path, old_pipe, addon_config_dict)

cdb instance-attribute

cdb = cdb

config instance-attribute

config = config

tokenizer property

tokenizer: BaseTokenizer

The raw tokenizer (with no components).

tokenizer_with_tag property

tokenizer_with_tag: BaseTokenizer

The tokenizer with the tagging component.

vocab instance-attribute

vocab: Vocab = vocab

add_addon

add_addon(addon: AddonComponent) -> None
Source code in medcat/medcat/pipeline/pipeline.py
421
422
423
424
def add_addon(self, addon: AddonComponent) -> None:
    self._addons.append(addon)
    # mark clean as of adding
    addon.config.mark_clean()

entity_from_tokens

entity_from_tokens(tokens: list[MutableToken]) -> MutableEntity

Get the entity from the list of tokens.

This effectively turns a list of (consecutive) documents into an entity.

Parameters:

Returns:

Source code in medcat/medcat/pipeline/pipeline.py
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
def entity_from_tokens(self, tokens: list[MutableToken]) -> MutableEntity:
    """Get the entity from the list of tokens.

    This effectively turns a list of (consecutive) documents
    into an entity.

    Args:
        tokens (list[MutableToken]): The tokens to use.

    Returns:
        MutableEntity: The resulting entity.
    """
    warnings.warn(
        "The `medcat.pipeline.pipeline.Pipeline.entity_from_tokens` method is"
        "depreacated is subject to removal in a future release. Please use "
        "`medcat.pipeline.pipeline.Pipeline.entity_from_tokens_in_doc` "
        "instead.",
        DeprecationWarning,
        stacklevel=2
    )
    return self._tokenizer.entity_from_tokens(tokens)

entity_from_tokens_in_doc

entity_from_tokens_in_doc(tokens: list[MutableToken], doc: MutableDocument) -> MutableEntity

Get the entity from the list of tokens in a document.

This effectively turns a list of (consecutive) documents into an entity. But it is also designed to reuse existing instances on the document instead of creating new ones.

Parameters:

Returns:

Source code in medcat/medcat/pipeline/pipeline.py
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
def entity_from_tokens_in_doc(self, tokens: list[MutableToken],
                              doc: MutableDocument) -> MutableEntity:
    """Get the entity from the list of tokens in a document.

    This effectively turns a list of (consecutive) documents
    into an entity. But it is also designed to reuse existing
    instances on the document instead of creating new ones.

    Args:
        tokens (list[MutableToken]): The tokens to use.
        doc (MutableDocument): The document for these tokens.

    Returns:
        MutableEntity: The resulting entity.
    """
    return self._tokenizer.entity_from_tokens_in_doc(tokens, doc)

get_component

Get the core component by the component type.

Parameters:

Raises:

  • ValueError

    If no component by that type is found.

Returns:

  • CoreComponent ( CoreComponent ) –

    The corresponding core component.

Source code in medcat/medcat/pipeline/pipeline.py
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
def get_component(self, ctype: CoreComponentType) -> CoreComponent:
    """Get the core component by the component type.

    Args:
        ctype (CoreComponentType): The core component type.

    Raises:
        ValueError: If no component by that type is found.

    Returns:
        CoreComponent: The corresponding core component.
    """
    for comp in self._components:
        if not comp.is_core() or not isinstance(comp, CoreComponent):
            continue
        if comp.get_type() is ctype:
            return comp
    raise ValueError(f"No component found of type {ctype}")

get_doc

get_doc(text: str) -> MutableDocument

Get the document for this text.

This essentially runs the tokenizer over the text.

Parameters:

  • text

    (str) –

    The input text.

Returns:

Source code in medcat/medcat/pipeline/pipeline.py
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
def get_doc(self, text: str) -> MutableDocument:
    """Get the document for this text.

    This essentially runs the tokenizer over the text.

    Args:
        text (str): The input text.

    Returns:
        MutableDocument: The resulting document.
    """
    doc = self._tokenizer(text)
    for comp in self._components:
        logger.info("Running component %s for %d of text (%s)",
                    comp.full_name, len(text), id(text))
        doc = comp(doc)
        if doc is None:
            raise IncorrectCoreComponent(
                f"Core component {comp.full_name} returned None "
                "instead of the document."
            )
    for addon in self._addons:
        doc = addon(doc)
        if doc is None:
            raise IncorrectAddonComponent(
                f"Addon component {addon.full_name} returned None "
                "instead of the document."
            )
    return doc

iter_addons

iter_addons() -> Iterable[AddonComponent]
Source code in medcat/medcat/pipeline/pipeline.py
456
457
def iter_addons(self) -> Iterable[AddonComponent]:
    yield from self._addons

iter_all_components

iter_all_components() -> Iterable[BaseComponent]
Source code in medcat/medcat/pipeline/pipeline.py
450
451
452
453
454
def iter_all_components(self) -> Iterable[BaseComponent]:
    for component in self._components:
        yield component
    for addon in self._addons:
        yield addon

save_components

save_components(serialiser_type: Union[AvailableSerialisers, str], components_folder: str) -> None
Source code in medcat/medcat/pipeline/pipeline.py
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
def save_components(self,
                    serialiser_type: Union[AvailableSerialisers, str],
                    components_folder: str) -> None:
    for component in self.iter_all_components():
        if not isinstance(component, Serialisable):
            continue
        if not os.path.exists(components_folder):
            os.mkdir(components_folder)
        if isinstance(component, CoreComponent):
            comp_folder = os.path.join(
                components_folder,
                AbstractCoreComponent.NAME_PREFIX +
                component.get_type().name)
        elif isinstance(component, AddonComponent):
            comp_folder = os.path.join(
                components_folder,
                f"{AddonComponent.NAME_PREFIX}{component.addon_type}"
                f"{AddonComponent.NAME_SPLITTER}{component.name}")
        else:
            raise ValueError(
                f"Unknown component: {type(component)} - does not appear "
                "to be a CoreComponent or an AddonComponent")
        serialise(serialiser_type, component, comp_folder)