def test_config_roundtrip_disk(): cfg = Config().from_str(OPTIMIZER_CFG) with make_tempdir() as path: cfg_path = path / "config.cfg" cfg.to_disk(cfg_path) new_cfg = Config().from_disk(cfg_path) assert new_cfg.to_str().strip() == OPTIMIZER_CFG.strip()
def test_issue7055(): """Test that fill-config doesn't turn sourced components into factories.""" source_cfg = { "nlp": {"lang": "en", "pipeline": ["tok2vec", "tagger"]}, "components": { "tok2vec": {"factory": "tok2vec"}, "tagger": {"factory": "tagger"}, }, } source_nlp = English.from_config(source_cfg) with make_tempdir() as dir_path: # We need to create a loadable source pipeline source_path = dir_path / "test_model" source_nlp.to_disk(source_path) base_cfg = { "nlp": {"lang": "en", "pipeline": ["tok2vec", "tagger", "ner"]}, "components": { "tok2vec": {"source": str(source_path)}, "tagger": {"source": str(source_path)}, "ner": {"factory": "ner"}, }, } base_cfg = Config(base_cfg) base_path = dir_path / "base.cfg" base_cfg.to_disk(base_path) output_path = dir_path / "config.cfg" fill_config(output_path, base_path, silent=True) filled_cfg = load_config(output_path) assert filled_cfg["components"]["tok2vec"]["source"] == str(source_path) assert filled_cfg["components"]["tagger"]["source"] == str(source_path) assert filled_cfg["components"]["ner"]["factory"] == "ner" assert "model" in filled_cfg["components"]["ner"]
def save_config(config: Config, output_file: Path, is_stdout: bool = False, silent: bool = False) -> None: no_print = is_stdout or silent msg = Printer(no_print=no_print) if is_stdout: print(config.to_str()) else: if not output_file.parent.exists(): output_file.parent.mkdir(parents=True) config.to_disk(output_file, interpolate=False) msg.good("Saved config", output_file) msg.text("You can now add your data and train your pipeline:") variables = ["--paths.train ./train.spacy", "--paths.dev ./dev.spacy"] if not no_print: print( f"{COMMAND} train {output_file.parts[-1]} {' '.join(variables)}" )
def test_config_roundtrip_disk_respects_path_subclasses(pathy_fixture): cfg = Config().from_str(OPTIMIZER_CFG) cfg_path = pathy_fixture / "config.cfg" cfg.to_disk(cfg_path) new_cfg = Config().from_disk(cfg_path) assert new_cfg.to_str().strip() == OPTIMIZER_CFG.strip()