From 01f1ec69c2ac3fc8b46c8a3699a450fdd0e6168e Mon Sep 17 00:00:00 2001 From: Justus Kuhlmann Date: Mon, 30 Jun 2025 09:54:54 +0000 Subject: [PATCH] add support for simple replacements in form of constants and replace variables --- corrlib/toml.py | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/corrlib/toml.py b/corrlib/toml.py index a65b807..07de93d 100644 --- a/corrlib/toml.py +++ b/corrlib/toml.py @@ -17,10 +17,37 @@ import datalad.api as dl import os from .input.implementations import codes as known_codes +def replace_string(string: str, name: str, val: str): + if '{' + name + '}' in string: + n = string.replace('{' + name + '}', val) + return n + else: + return string + +def replace_in_meas(measurements: dict, vars: dict[str, str]): + # replace global variables + for name, value in vars.items(): + for m in measurements.keys(): + for key in measurements[m].keys(): + measurements[m][key] = replace_string(measurements[m][key], name, value) + # replace ensemble name + for m in measurements.keys(): + for key in measurements[m].keys(): + if not key == 'ensemble': + measurements[m][key] = replace_string(measurements[m][key], 'ensemble', measurements[m]['ensemble']) + return measurements + +def fill_cons(measurements, constants): + for m in measurements.keys(): + for name, val in constants.items(): + if name not in measurements[m].keys(): + measurements[m][name] = val + return measurements + def check_project_data(d: dict) -> None: - if 'project' not in d.keys() or 'measurements' not in d.keys() or len(list(d.keys())) > 2: - raise ValueError('There should only be two key on the top level, "project" and "measurements"!') + if 'project' not in d.keys() or 'measurements' not in d.keys() or len(list(d.keys())) > 4: + raise ValueError('There should only be maximally be four keys on the top level, "project" and "measurements" are mandatory, "contants" is optional!') project_data = d['project'] if 'url' not in project_data.keys(): raise ValueError('project.url is missing!') @@ -68,7 +95,11 @@ def import_toml(path: str, file: str, copy_file: bool=True) -> None: project: dict = toml_dict['project'] if project['code'] not in known_codes: raise ValueError('Code' + project['code'] + 'has no import implementation!') + constants: dict = toml_dict['constants'] measurements: dict = toml_dict['measurements'] + measurements = fill_cons(measurements, constants) + measurements = replace_in_meas(measurements, toml_dict['replace'] if 'replace' in toml_dict else {}) + print(measurements) check_measurement_data(measurements, project['code']) aliases = project.get('aliases', None) uuid = project.get('uuid', None) @@ -90,6 +121,7 @@ def import_toml(path: str, file: str, copy_file: bool=True) -> None: else: measurement = sfcf.read_data(path, uuid, md['path'], md['prefix'], param, version=md['version'], cfg_seperator=md['cfg_seperator'], sep='/') + print(mname + " imported.") elif project['code'] == 'openQCD': if md['measurement'] == 'ms1':