Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

Commit 6f4e9fa

Browse files
committed
CU-8699g3ajp v2 beta compatibility (#91)
* CU-8699g3ajp: Update backwards compatibility workflow. Will now fail upon any individual model failing * CU-8699g3ajp: Add method to remap module packages when loading model with dill * CU-8699g3ajp: Using fixed module/class name when deserialising with dill
1 parent bca7355 commit 6f4e9fa

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

medcat/storage/serialisers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from medcat.storage.serialisables import get_all_serialisable_members
1414
from medcat.storage.schema import load_schema, save_schema
1515
from medcat.storage.schema import DEFAULT_SCHEMA_FILE, IllegalSchemaException
16+
from medcat.utils.legacy.v2_beta import (
17+
fix_module_and_cls_name, RemappingUnpickler)
1618

1719

1820
logger = logging.getLogger(__name__)
@@ -146,6 +148,7 @@ def deserialise_manually(cls, folder_path: str, man_cls_path: str,
146148
**init_kwargs) -> Serialisable:
147149
logger.info("Deserialising manually based on %s", man_cls_path)
148150
module_name, cls_name = man_cls_path.rsplit(".", 1)
151+
module_name, cls_name = fix_module_and_cls_name(module_name, cls_name)
149152
rel_module = importlib.import_module(module_name)
150153
man_cls = getattr(rel_module, cls_name)
151154
if not issubclass(man_cls, ManualSerialisable):
@@ -253,7 +256,7 @@ def serialise(self, raw_parts: dict[str, Any], target_file: str) -> None:
253256

254257
def deserialise(self, target_file: str) -> dict[str, Any]:
255258
with open(target_file, 'rb') as f:
256-
return _dill.load(f)
259+
return RemappingUnpickler(f).load()
257260

258261

259262
_DEF_SER = AvailableSerialisers.dill

medcat/utils/legacy/v2_beta.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import dill
2+
3+
4+
class RemappingUnpickler(dill.Unpickler):
5+
# TODO: remove at some point - this only serves to load beta models
6+
def find_class(self, module: str, name: str):
7+
# Remap old module path to new one
8+
module, name = fix_module_and_cls_name(module, name)
9+
return super().find_class(module, name)
10+
11+
12+
def fix_module_and_cls_name(module_name: str, cls_name: str
13+
) -> tuple[str, str]:
14+
if module_name.startswith("medcat2"):
15+
module_name = module_name.replace("medcat2", "medcat", 1)
16+
return module_name, cls_name

tests/backwards_compatibility/check_backwards_compatibility.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# fail upon individual failure
2+
set -euo pipefail
13
# CONSTANTs/ shouldn't change
24
REGRESSION_MODULE="medcat.utils.regression.regression_checker"
35
REGRESSION_OPTIONS="--strictness STRICTEST --require-fully-correct"

0 commit comments

Comments
 (0)