Skip to content

Commit

Permalink
added save_to_config_by_name
Browse files Browse the repository at this point in the history
  • Loading branch information
Hendrik-code committed Aug 5, 2024
1 parent d25fabc commit b4a1131
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion panoptica/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 9 additions & 1 deletion panoptica/utils/config.py
Original file line number Diff line number Diff line change
@@ -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 = []
Expand Down Expand Up @@ -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"""

Expand Down Expand Up @@ -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()
Expand Down
9 changes: 7 additions & 2 deletions panoptica/utils/filepath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

0 comments on commit b4a1131

Please sign in to comment.