medcat.utils.spacy_compatibility

This module attempts to read the spacy compatibilty of a model pack and (if necessary) compare it to the installed spacy version.

Module Contents

Functions

_is_spacy_model_folder(folder_name)

Check if a folder within a model pack contains a spacy model.

_find_spacy_model_folder(model_pack_folder)

Find the spacy model folder in a model pack folder.

get_installed_spacy_version()

Get the spacy version installed currently.

get_installed_model_version(model_name)

Get the version of a model installed in spacy.

_get_name_and_meta_of_spacy_model_in_medcat_modelpack(...)

Gets the name and meta information about a spacy model within a medcat model pack.

get_name_and_version_of_spacy_model_in_medcat_modelpack(...)

Get the name, version, and compatible spacy versions of a spacy model within a medcat model pack.

_is_spacy_version_within_range(spacy_version_range)

Checks whether the spacy version is within the specified range.

medcat_model_pack_has_compatible_spacy_model(...)

Checks whether a medcat model pack has a spacy model compatible with installed spacy version.

is_older_spacy_version(model_version)

Checks if the specified version is older than the installed version.

medcat_model_pack_has_semi_compatible_spacy_model(...)

Checks whether the spacy model within a medcat model pack is

Attributes

SPACY_MODEL_REGEX

medcat.utils.spacy_compatibility.SPACY_MODEL_REGEX
medcat.utils.spacy_compatibility._is_spacy_model_folder(folder_name)

Check if a folder within a model pack contains a spacy model.

The idea is to do this without loading the model. That is because the version of the model may be incompatible with what we’ve got. And as such, loading may not be possible.

Parameters:

folder_name (str) – The folder to check.

Returns:

bool – Whether the folder contains a spacy model.

Return type:

bool

medcat.utils.spacy_compatibility._find_spacy_model_folder(model_pack_folder)

Find the spacy model folder in a model pack folder.

Parameters:

model_pack_folder (str) – The model pack folder

Raises:

ValueError – If it’s ambiguous or there’s no model folder.

Returns:

str – The full path to the model folder.

Return type:

str

medcat.utils.spacy_compatibility.get_installed_spacy_version()

Get the spacy version installed currently.

Returns:

str – The currently installed spacy verison.

Return type:

str

medcat.utils.spacy_compatibility.get_installed_model_version(model_name)

Get the version of a model installed in spacy.

Parameters:

model_name (str) – The model name.

Returns:

str – The version of the installed model.

Return type:

str

medcat.utils.spacy_compatibility._get_name_and_meta_of_spacy_model_in_medcat_modelpack(model_pack_path)

Gets the name and meta information about a spacy model within a medcat model pack.

PS: This gets the raw (folder) name of the spacy model.

While this is usually (in models created after v1.2.4) identical to the spacy model version, that may not always be the case.

Parameters:

model_pack_path (str) – The model pack path.

Returns:

Tuple[str, dict] – The name of the spacy model, and the meta information.

Return type:

Tuple[str, dict]

medcat.utils.spacy_compatibility.get_name_and_version_of_spacy_model_in_medcat_modelpack(model_pack_path)

Get the name, version, and compatible spacy versions of a spacy model within a medcat model pack.

PS: This gets the real name of the spacy model.

While this is usually (in models created after v1.2.4) identical to the folder name, that may not always be the case.

Parameters:

model_pack_path (str) – The model pack path.

Returns:

Tuple[str, str, str] – The name of the spacy model, its version, and supported spacy version.

Return type:

Tuple[str, str, str]

medcat.utils.spacy_compatibility._is_spacy_version_within_range(spacy_version_range)

Checks whether the spacy version is within the specified range.

The expected format of the version range is similar to that used in requirements and/or pip installs. E.g:

  • >=3.1.0,<3.2.0

  • ==3.1.0

  • >=3.1.0

  • <3.20

Parameters:

spacy_version_range (str) – The requires spacy version range.

Returns:

bool – Whether the specified range is compatible.

Return type:

bool

medcat.utils.spacy_compatibility.medcat_model_pack_has_compatible_spacy_model(model_pack_path)

Checks whether a medcat model pack has a spacy model compatible with installed spacy version.

Parameters:

model_pack_path (str) – The model pack path.

Returns:

bool – Whether the spacy model in the model pack is compatible.

Return type:

bool

medcat.utils.spacy_compatibility.is_older_spacy_version(model_version)

Checks if the specified version is older than the installed version.

Parameters:

model_version (str) – The specified spacy version.

Returns:

bool – Whether the specified version is older.

Return type:

bool

medcat.utils.spacy_compatibility.medcat_model_pack_has_semi_compatible_spacy_model(model_pack_path)
Checks whether the spacy model within a medcat model pack is

compatible or older than the installed spacy version.

This method returns True if the spacy model is compatible or released with a lower version number compared to the spacy version currently installed.

We’ve found that most of the time older models will work with a newer version of spacy. Though there is a warning on spacy’s side and they do not guarantee 100% compatibility, we’ve not seen issues so far.

E.g for installed spacy 3.4.4 all the following will be suiable:
  • en_core_web_md-3.1.0

  • en_core_web_md-3.2.0

  • en_core_web_md-3.3.0

  • en_core_web_md-3.4.1

However, for the same version, the following would not be suitable:
  • en_core_web_md-3.5.0

  • en_core_web_md-3.6.0

  • en_core_web_md-3.7.1

Parameters:

model_pack_path (str) – The model pack path.

Returns:

bool – Whether the spacy model in the model pack is compatible.

Return type:

bool