Skip to content

Commit

Permalink
20240327, intro convert_eval_expt_name_to_tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
matheme-justyn committed Mar 26, 2024
1 parent 804220b commit af165e8
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions PETsARD/reporter/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import re

from PETsARD.error import ConfigError


def convert_full_expt_tuple_to_name(expt_tuple: tuple) -> str:
"""
Expand Down Expand Up @@ -35,22 +37,48 @@ def convert_full_expt_name_to_tuple(expt_name: str) -> tuple:
Args:
expt_name (str): A string representation of the experiment configuration,
formatted as `ModuleName[ExperimentName]` for single-step experiments or
formatted as
`ModuleName[ExperimentName]` for single-step experiments or
`ModuleName[ExperimentName]_AnotherModuleName[AnotherExperimentName]`
for multi-step experiments.
Examples:
- 'Loader[default]'
- 'Loader[default]_Preprocessor[default]'
- A single step experiment: 'Loader[default]'
- A multi-step experiment: 'Loader[default]_Preprocessor[default]'
Returns:
tuple: A tuple representing the full experiment configuration, where each
pair consists of a module name followed by its corresponding experiment
name. The tuple can contain multiple such pairs, indicating a sequence
of module and experiment steps.
Examples:
(tuple): A tuple representing a full experiment configuation.
Each pair within the tuple should consist of a module name
followed by its corresponding experiment name.
The tuple can contain multiple such pairs,
indicating a sequence of module and experiment steps. e.g.
- A single step experiment: ('Loader', 'default'),
- A multi-step experiment: ('Loader', 'default', 'Preprocessor', 'default')
"""
pattern = re.compile(r'(?:^|_)(\w+)\[([\w-]+)\]')
matches = pattern.findall(expt_name)
return tuple([item for match in matches for item in match])
return tuple([item for match in matches for item in match])


def convert_eval_expt_name_to_tuple(expt_name: str) -> tuple:
"""
Converts an Evaluator/Describer experiment name to a tuple.
Args:
expt_name (str):
A string representation of the evaluation experiment configuration,
formatted as f"{eval_name}_[{granularity}]". e.g.
- 'sdmetrics-qual_[global]'
Returns:
(tuple): A tuple representing a evaluation experiment configuation.
formatted as ({eval_name}, {granularity}). e.g.
- ('sdmetrics-qual', 'global')
Raises:
ConfigError: If the experiment name does not match the expected pattern.
"""
pattern = re.compile(r'([A-Za-z0-9_-]+)\_\[([\w-]+)\]')
match = pattern.match(expt_name)
if match:
return match.groups()
else:
return ConfigError

0 comments on commit af165e8

Please sign in to comment.