add support for simple replacements in form of constants and replace variables

This commit is contained in:
Justus Kuhlmann 2025-06-30 09:54:54 +00:00
parent 2ff46ee56f
commit 01f1ec69c2

View file

@ -17,10 +17,37 @@ import datalad.api as dl
import os import os
from .input.implementations import codes as known_codes 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: 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: if 'project' not in d.keys() or 'measurements' not in d.keys() or len(list(d.keys())) > 4:
raise ValueError('There should only be two key on the top level, "project" and "measurements"!') 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'] project_data = d['project']
if 'url' not in project_data.keys(): if 'url' not in project_data.keys():
raise ValueError('project.url is missing!') 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'] project: dict = toml_dict['project']
if project['code'] not in known_codes: if project['code'] not in known_codes:
raise ValueError('Code' + project['code'] + 'has no import implementation!') raise ValueError('Code' + project['code'] + 'has no import implementation!')
constants: dict = toml_dict['constants']
measurements: dict = toml_dict['measurements'] 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']) check_measurement_data(measurements, project['code'])
aliases = project.get('aliases', None) aliases = project.get('aliases', None)
uuid = project.get('uuid', None) uuid = project.get('uuid', None)
@ -90,6 +121,7 @@ def import_toml(path: str, file: str, copy_file: bool=True) -> None:
else: else:
measurement = sfcf.read_data(path, uuid, md['path'], md['prefix'], param, measurement = sfcf.read_data(path, uuid, md['path'], md['prefix'], param,
version=md['version'], cfg_seperator=md['cfg_seperator'], sep='/') version=md['version'], cfg_seperator=md['cfg_seperator'], sep='/')
print(mname + " imported.") print(mname + " imported.")
elif project['code'] == 'openQCD': elif project['code'] == 'openQCD':
if md['measurement'] == 'ms1': if md['measurement'] == 'ms1':