76 lines
No EOL
2.9 KiB
Python
76 lines
No EOL
2.9 KiB
Python
import pyerrors.input.openQCD as input
|
|
import datalad.api as dl
|
|
import os
|
|
import fnmatch
|
|
from typing import Any
|
|
|
|
|
|
def read_param(path: str, project: str, file_in_project: str) -> dict[str, Any]:
|
|
file = path + "/projects/" + project + '/' + file_in_project
|
|
dl.get(file, dataset=path + "/projects/" + project)
|
|
with open(file, 'r') as fp:
|
|
lines = fp.readlines()
|
|
fp.close()
|
|
param = {}
|
|
param['rw_fcts'] = []
|
|
param['rand'] = {}
|
|
|
|
for i, line in enumerate(lines):
|
|
if line.startswith('#'):
|
|
continue
|
|
if line.startswith('\n'):
|
|
continue
|
|
if line.startswith("nrw"):
|
|
stline = line.strip()
|
|
num_rw = int(stline.split('#')[0].split()[1].strip())
|
|
param["rw_fcts"] = [{}]*num_rw
|
|
for i, line in enumerate(lines):
|
|
for nrw in range(num_rw):
|
|
param["rw_fcts"][nrw] = {}
|
|
if line == "[Reweighting factor " + str(nrw) + "]\n":
|
|
j=0
|
|
while not (lines[i+j] == "\n" or lines[i+j].startswith("[")):
|
|
j+=1
|
|
chunk = lines[i:j+i]
|
|
for cline in chunk:
|
|
cline = cline.strip()
|
|
for rwp in ["rwfact", "nsrc", "irp", "mu", "np"]:
|
|
if cline.startswith(rwp):
|
|
param["rw_fcts"][nrw][rwp] = cline.split()[1]
|
|
for nrw in range(num_rw):
|
|
if "nsrc" not in param["rw_fcts"][nrw]:
|
|
param["rw_fcts"][nrw]["nsrc"] = 1
|
|
if "mu" not in param["rw_fcts"][nrw]:
|
|
param["rw_fcts"][nrw]["mu"] = "None"
|
|
if "np" not in param["rw_fcts"][nrw]:
|
|
param["rw_fcts"][nrw]["np"] = "None"
|
|
if "irp" not in param["rw_fcts"][nrw]:
|
|
param["rw_fcts"][nrw]["irp"] = "None"
|
|
|
|
return param
|
|
|
|
|
|
def read_rwms(path: str, project: str, dir_in_project: str, param: dict[str, Any], prefix: str, postfix: str="ms1", version: str='2.0', names: list[str]=None, files: list[str]=None):
|
|
directory = path + "/projects/" + project + '/' + dir_in_project
|
|
if files is None:
|
|
files = []
|
|
for root, ds, fs in os.walk(directory):
|
|
for f in fs:
|
|
if fnmatch.fnmatch(f, prefix + "*" + postfix + ".dat"):
|
|
files.append(f)
|
|
dl.get([directory + "/" + f for f in files], dataset=path + "/projects/" + project)
|
|
kwargs = {}
|
|
if names is not None:
|
|
kwargs['names'] = names
|
|
if files is not None:
|
|
kwargs['files'] = files
|
|
rwms = input.read_rwms(directory, prefix, version, **kwargs)
|
|
rw_dict: dict[str, dict[str, Any]] = {}
|
|
rw_dict[param["type"]] = {}
|
|
for i in range(len(param["rw_fcts"])):
|
|
par_list= []
|
|
for k in param["rw_fcts"][i].keys():
|
|
par_list.append(str(param["rw_fcts"][i][k]))
|
|
pars = "/".join(par_list)
|
|
rw_dict[param["type"]][pars] = rwms[i]
|
|
return rw_dict |