diff --git a/autonerves/json_prior/generate.py b/autonerves/json_prior/generate.py deleted file mode 100644 index 8ce05c5..0000000 --- a/autonerves/json_prior/generate.py +++ /dev/null @@ -1,70 +0,0 @@ -import inspect -import json -import logging -import os -from importlib import util -from pathlib import Path - -from .config import make_config_for_class - -logger = logging.getLogger(__name__) - - -def for_file(module_path: str) -> dict: - """ - Generate JSON priors for all classes in a file, using default - prior configuration for each constructor argument. - - Parameters - ---------- - module_path - The path to the file. - - Returns - ------- - JSON configuration, where class names are mapped to their prior configs. - """ - spec = util.spec_from_file_location("module.name", module_path) - module = util.module_from_spec(spec) - spec.loader.exec_module(module) - classes = inspect.getmembers(module) - - return { - name: make_config_for_class(obj)[1] - for name, obj in classes - if inspect.isclass(obj) - } - - -def generate(directory: str): - """ - Generate prior configuration for a given directory, recursively. - - A directory "priors" is created if it does not exists. A new JSON file is created - in priors for each python module found that contains at least one class. - - If an output file already exists then prior generation is skipped. - - Parameters - ---------- - directory - The directory for which prior are generated - """ - cwd = Path.cwd() - try: - (cwd / "priors").mkdir() - except FileExistsError: - pass - for directory, _, files in os.walk(directory): - directory = Path(directory) - for file in files: - if file.endswith(".py"): - full_path = directory / file - spec = for_file(full_path) - config_path = cwd / "priors" / file.replace(".py", ".json") - if len(spec) > 0: - if config_path.exists(): - logger.info(f"{config_path} already exists") - continue - with open(config_path, "w+") as f: - json.dump(spec, f) diff --git a/eden.yaml b/eden.yaml deleted file mode 100644 index 38e1d00..0000000 --- a/eden.yaml +++ /dev/null @@ -1,2 +0,0 @@ -name: autonerves -eden_prefix: VIS_CTI diff --git a/priors/subconfig.json b/priors/subconfig.json deleted file mode 100644 index bffe257..0000000 --- a/priors/subconfig.json +++ /dev/null @@ -1 +0,0 @@ -{"SubClass": {"variable": {"type": "Uniform", "lower_limit": 0.0, "upper_limit": 1.0, "width_modifier": {"type": "Absolute", "value": 0.2}, "limits": {"lower": 0.0, "upper": 1.0}}}} \ No newline at end of file diff --git a/scripts/convert_config.py b/scripts/convert_config.py deleted file mode 100755 index 47ddaaa..0000000 --- a/scripts/convert_config.py +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python -""" -Converts the configuration files and directories in a given directory into YAML configs. - -Usage: -./convert_config.py path/to/directory -""" - -import os -import shutil -import sys -from pathlib import Path - -import yaml - -from autonerves.directory_config import RecursiveConfig, YAMLConfig - -target_path = Path(sys.argv[1]) - -config = RecursiveConfig(str(target_path)) - -for key in config.keys(): - value = config[key] - if isinstance(value, YAMLConfig): - continue - - d = value.dict() - path = target_path / key - with open(path.with_suffix(".yaml"), "w") as f: - yaml.dump(d, f) - - try: - os.remove(path.with_suffix(".ini")) - except FileNotFoundError: - pass - - shutil.rmtree(path, ignore_errors=True) diff --git a/scripts/convert_prior_configs.py b/scripts/convert_prior_configs.py deleted file mode 100755 index 28fe401..0000000 --- a/scripts/convert_prior_configs.py +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env python -""" -Converts JSON prior configs to YAML equivalent. - -Usage: -./convert_prior_configs.py /path/to/prior/directory -""" -import json -import os -import sys -from pathlib import Path - -import oyaml as yaml - -ORDER = [ - "type", - "mean", - "sigma", - "lower_limit", - "upper_limit", - "width_modifier", - "limits", -] - -for path in Path(sys.argv[1]).rglob("*.json"): - with open(path) as f: - d = json.load(f) - - with open(path.with_suffix(".yaml"), "w") as f: - yaml.dump(d, f) - - os.remove(path) - - -def sort_dict(obj): - if isinstance(obj, dict): - return { - key: sort_dict(value) - for key, value in sorted( - obj.items(), - key=lambda item: ORDER.index(item[0]) if item[0] in ORDER else 999, - ) - } - if isinstance(obj, list): - return list(map(sort_dict, obj)) - return obj - - -for path in Path(sys.argv[1]).rglob("*.yaml"): - with open(path) as f: - d = yaml.safe_load(f) - - print(sort_dict(d)) - with open(path, "w") as f: - yaml.dump(sort_dict(d), f) diff --git a/scripts/edenise.py b/scripts/edenise.py deleted file mode 100755 index 19fff62..0000000 --- a/scripts/edenise.py +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env python - -from configparser import ConfigParser -from sys import argv - -from autofit.tools import edenise - - -def main(root_directory): - try: - config = ConfigParser() - config.read(f"{root_directory}/eden.ini") - - edenise.edenise( - root_directory, config.get("eden", "name"), config.get("eden", "prefix") - ) - except ValueError: - print("Usage: ./edenise.py root_directory") - exit(1) - - -if __name__ == "__main__": - main(argv[1]) diff --git a/scripts/generate_priors.py b/scripts/generate_priors.py deleted file mode 100755 index c0f6e59..0000000 --- a/scripts/generate_priors.py +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env python -""" -Generate prior configuration for all classes in a package, recursively -""" - -from sys import argv - -from autonerves.json_prior import generate - -if __name__ == "__main__": - try: - generate.generate(argv[1]) - except KeyError: - print("Usage: ./generate_priors.py /path/to/project") diff --git a/test_autonerves/json_prior/test_generate.py b/test_autonerves/json_prior/test_generate.py deleted file mode 100644 index 63321fa..0000000 --- a/test_autonerves/json_prior/test_generate.py +++ /dev/null @@ -1,61 +0,0 @@ -import json -import os -from pathlib import Path - -import pytest - -from autonerves.json_prior import generate as g - -directory = Path(__file__).parent -package_directory = directory / "source_code" -module_path = package_directory / "module.py" - - -@pytest.fixture(name="prior_json") -def make_prior_json(): - return { - "MyClass": { - "simple": { - "limits": {"lower": 0.0, "upper": 1.0}, - "lower_limit": 0.0, - "type": "Uniform", - "upper_limit": 1.0, - "width_modifier": {"type": "Absolute", "value": 0.2}, - }, - "tup_0": { - "limits": {"lower": 0.0, "upper": 1.0}, - "lower_limit": 0.0, - "type": "Uniform", - "upper_limit": 1.0, - "width_modifier": {"type": "Absolute", "value": 0.2}, - }, - "tup_1": { - "limits": {"lower": 0.0, "upper": 1.0}, - "lower_limit": 0.0, - "type": "Uniform", - "upper_limit": 1.0, - "width_modifier": {"type": "Absolute", "value": 0.2}, - }, - } - } - - -@pytest.fixture(autouse=True) -def cleanup(): - yield - try: - os.remove("priors/module.json") - except FileNotFoundError: - pass - - -def test_generate_for_file(prior_json): - result = g.for_file(module_path) - assert result == prior_json - - -def test_generate(prior_json): - g.generate(package_directory) - - with open(f"priors/module.json") as f: - assert json.load(f) == prior_json diff --git a/test_autonerves/test_config.py b/test_autonerves/test_config.py index 28a749a..9414e12 100644 --- a/test_autonerves/test_config.py +++ b/test_autonerves/test_config.py @@ -1,5 +1,3 @@ -import os -import shutil from pathlib import Path import pytest @@ -41,28 +39,18 @@ def test_exception(self, label_config): label_config["superscript"].family(MockClass) -BAD_PATH = "bad/path" - - -def remove_path(): - shutil.rmtree( - BAD_PATH, - ignore_errors=True, - ) - - @pytest.fixture(name="config") def make_config(): return conf.Config() -def test_path_does_not_exist(config): - remove_path() +def test_path_does_not_exist(config, tmp_path): with pytest.raises(ConfigException): - config.push(BAD_PATH) + config.push(str(tmp_path / "does_not_exist")) -def test_path_empty(config): - os.makedirs(BAD_PATH, exist_ok=True) +def test_path_empty(config, tmp_path): + empty = tmp_path / "empty" + empty.mkdir() with pytest.raises(ConfigException): - config.push(BAD_PATH) + config.push(str(empty))