From b4a1131fe3369f0a2d35719cb663f3a1c43a7a9f Mon Sep 17 00:00:00 2001 From: iback Date: Mon, 5 Aug 2024 12:08:12 +0000 Subject: [PATCH] added save_to_config_by_name --- panoptica/__init__.py | 2 +- panoptica/utils/config.py | 10 +++++++++- panoptica/utils/filepath.py | 9 +++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/panoptica/__init__.py b/panoptica/__init__.py index dca6768..2f02659 100644 --- a/panoptica/__init__.py +++ b/panoptica/__init__.py @@ -6,9 +6,9 @@ from panoptica.panoptica_evaluator import Panoptica_Evaluator from panoptica.panoptica_result import PanopticaResult from panoptica.utils.processing_pair import ( + InputType, SemanticPair, UnmatchedInstancePair, MatchedInstancePair, - InputType, ) from panoptica.metrics import Metric, MetricMode, MetricType diff --git a/panoptica/utils/config.py b/panoptica/utils/config.py index 3afc165..a0ca8ba 100644 --- a/panoptica/utils/config.py +++ b/panoptica/utils/config.py @@ -1,6 +1,6 @@ from ruamel.yaml import YAML from pathlib import Path -from panoptica.utils.filepath import config_by_name +from panoptica.utils.filepath import config_by_name, config_dir_by_name from abc import ABC, abstractmethod supported_helper_classes = [] @@ -130,6 +130,11 @@ def _save_to_config(obj, path: str | Path): Configuration.save_from_object(obj, path) +def _save_to_config_by_name(obj, name: str): + dir, name = config_dir_by_name(name) + _save_to_config(obj, dir.joinpath(name)) + + class SupportsConfig: """Metaclass that allows a class to save and load objects by yaml configs""" @@ -160,6 +165,9 @@ def load_from_config_name(cls, name: str): def save_to_config(self, path: str | Path): _save_to_config(self, path) + def save_to_config_by_name(self, name: str): + _save_to_config_by_name(self, name) + @classmethod def to_yaml(cls, representer, node): # cls._register_permanently() diff --git a/panoptica/utils/filepath.py b/panoptica/utils/filepath.py index f668b75..70467c0 100644 --- a/panoptica/utils/filepath.py +++ b/panoptica/utils/filepath.py @@ -26,11 +26,16 @@ def search_path(basepath: str | Path, query: str, verbose: bool = False, suppres return paths -# Find config path -def config_by_name(name: str) -> Path: +def config_dir_by_name(name: str) -> tuple[Path, str]: directory = Path(__file__.replace("////", "/").replace("\\\\", "/").replace("//", "/").replace("\\", "/")).parent.parent if not name.endswith(".yaml"): name += ".yaml" + return directory, name + + +# Find config path +def config_by_name(name: str) -> Path: + directory, name = config_dir_by_name(name) p = search_path(directory, query=f"**/{name}", suppress=True) assert len(p) == 1, f"Did not find exactly one config yaml with name {name} in directory {directory}, got {p}" return p[0]