Skip to content

Commit

Permalink
CU-8694cd9t2: Allow merging config into model pack config before init (
Browse files Browse the repository at this point in the history
…#462)

* CU-8694cd9t2: Allow merging config into model pack config before init
  • Loading branch information
mart-r authored Aug 12, 2024
1 parent 005796a commit 76c2fa2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
7 changes: 6 additions & 1 deletion medcat/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ def load_model_pack(cls,
zip_path: str,
meta_cat_config_dict: Optional[Dict] = None,
ner_config_dict: Optional[Dict] = None,
medcat_config_dict: Optional[Dict] = None,
load_meta_models: bool = True,
load_addl_ner: bool = True,
load_rel_models: bool = True) -> "CAT":
Expand All @@ -373,6 +374,10 @@ def load_model_pack(cls,
A config dict that will overwrite existing configs in transformers ner.
e.g. ner_config_dict = {'general': {'chunking_overlap_window': 6}.
Defaults to None.
medcat_config_dict (Optional[Dict]):
A config dict that will overwrite existing configs in the main medcat config
before pipe initialisation. This can be useful if wanting to change something
that only takes effect at init time (e.g spacy model). Defaults to None.
load_meta_models (bool):
Whether to load MetaCAT models if present (Default value True).
load_addl_ner (bool):
Expand All @@ -395,7 +400,7 @@ def load_model_pack(cls,

# load config
config_path = os.path.join(model_pack_path, "config.json")
cdb.load_config(config_path)
cdb.load_config(config_path, medcat_config_dict)

# TODO load addl_ner

Expand Down
14 changes: 13 additions & 1 deletion medcat/cdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,17 @@ async def save_async(self, path: str) -> None:
}
await f.write(dill.dumps(to_save))

def load_config(self, config_path: str) -> None:
def load_config(self, config_path: str, config_dict: Optional[Dict] = None) -> None:
"""Load the config from disk.
Args:
config_path (str): The path to the config file.
config_dict (Optional[Dict]): A config to merge with.
Raises:
ValueError: If a config was not found in CDB nor as a separate json.
Or if a config was found both in CDB as well as a separate json.
"""
if not os.path.exists(config_path):
if not self._config_from_file:
# if there's no config defined anywhere
Expand Down Expand Up @@ -544,6 +554,8 @@ def load_config(self, config_path: str) -> None:
# new config, potentially new weighted_average_function to read
self._init_waf_from_config()
# mark config read from file
if config_dict:
self.config.merge_config(config_dict)
self._config_from_file = True

@classmethod
Expand Down

0 comments on commit 76c2fa2

Please sign in to comment.