Skip to content

Commit da0a4cc

Browse files
authored
Feat/idl func (#275)
* outsource function to get idl for append mode * Expose option to define function for idl by the user. * clean up args * add tests * lint
1 parent e0076cc commit da0a4cc

File tree

5 files changed

+2618
-16
lines changed

5 files changed

+2618
-16
lines changed

pyerrors/input/sfcf.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
sep = "/"
1111

1212

13-
def read_sfcf(path, prefix, name, quarks='.*', corr_type="bi", noffset=0, wf=0, wf2=0, version="1.0c", cfg_separator="n", silent=False, **kwargs):
13+
def read_sfcf(path, prefix, name, quarks='.*', corr_type="bi", noffset=0, wf=0, wf2=0, version="1.0c", cfg_separator="n", cfg_func=None, silent=False, **kwargs):
1414
"""Read sfcf files from given folder structure.
1515
1616
Parameters
@@ -71,11 +71,11 @@ def read_sfcf(path, prefix, name, quarks='.*', corr_type="bi", noffset=0, wf=0,
7171
"""
7272
ret = read_sfcf_multi(path, prefix, [name], quarks_list=[quarks], corr_type_list=[corr_type],
7373
noffset_list=[noffset], wf_list=[wf], wf2_list=[wf2], version=version,
74-
cfg_separator=cfg_separator, silent=silent, **kwargs)
74+
cfg_separator=cfg_separator, cfg_func=cfg_func, silent=silent, **kwargs)
7575
return ret[name][quarks][str(noffset)][str(wf)][str(wf2)]
7676

7777

78-
def read_sfcf_multi(path, prefix, name_list, quarks_list=['.*'], corr_type_list=['bi'], noffset_list=[0], wf_list=[0], wf2_list=[0], version="1.0c", cfg_separator="n", silent=False, keyed_out=False, **kwargs):
78+
def read_sfcf_multi(path, prefix, name_list, quarks_list=['.*'], corr_type_list=['bi'], noffset_list=[0], wf_list=[0], wf2_list=[0], version="1.0c", cfg_separator="n", cfg_func=None, silent=False, keyed_out=False, **kwargs):
7979
"""Read sfcf files from given folder structure.
8080
8181
Parameters
@@ -245,6 +245,16 @@ def read_sfcf_multi(path, prefix, name_list, quarks_list=['.*'], corr_type_list=
245245
for key in needed_keys:
246246
internal_ret_dict[key] = []
247247

248+
def _default_idl_func(cfg_string, cfg_sep):
249+
return int(cfg_string.split(cfg_sep)[-1])
250+
251+
if cfg_func is None:
252+
print("Default idl function in use.")
253+
cfg_func = _default_idl_func
254+
cfg_func_args = [cfg_separator]
255+
else:
256+
cfg_func_args = kwargs.get("cfg_func_args", [])
257+
248258
if not appended:
249259
for i, item in enumerate(ls):
250260
rep_path = path + '/' + item
@@ -268,7 +278,7 @@ def read_sfcf_multi(path, prefix, name_list, quarks_list=['.*'], corr_type_list=
268278
for cfg in sub_ls:
269279
try:
270280
if compact:
271-
rep_idl.append(int(cfg.split(cfg_separator)[-1]))
281+
rep_idl.append(cfg_func(cfg, *cfg_func_args))
272282
else:
273283
rep_idl.append(int(cfg[3:]))
274284
except Exception:
@@ -351,7 +361,7 @@ def read_sfcf_multi(path, prefix, name_list, quarks_list=['.*'], corr_type_list=
351361
for rep, file in enumerate(name_ls):
352362
rep_idl = []
353363
filename = path + '/' + file
354-
T, rep_idl, rep_data = _read_append_rep(filename, pattern, intern[name]['b2b'], cfg_separator, im, intern[name]['single'])
364+
T, rep_idl, rep_data = _read_append_rep(filename, pattern, intern[name]['b2b'], im, intern[name]['single'], cfg_func, cfg_func_args)
355365
if rep == 0:
356366
intern[name]['T'] = T
357367
for t in range(intern[name]['T']):
@@ -581,12 +591,7 @@ def _read_compact_rep(path, rep, sub_ls, intern, needed_keys, im):
581591
return return_vals
582592

583593

584-
def _read_chunk(chunk, gauge_line, cfg_sep, start_read, T, corr_line, b2b, pattern, im, single):
585-
try:
586-
idl = int(chunk[gauge_line].split(cfg_sep)[-1])
587-
except Exception:
588-
raise Exception("Couldn't parse idl from directory, problem with chunk around line ", gauge_line)
589-
594+
def _read_chunk_data(chunk, start_read, T, corr_line, b2b, pattern, im, single):
590595
found_pat = ""
591596
data = []
592597
for li in chunk[corr_line + 1:corr_line + 6 + b2b]:
@@ -595,10 +600,10 @@ def _read_chunk(chunk, gauge_line, cfg_sep, start_read, T, corr_line, b2b, patte
595600
for t, line in enumerate(chunk[start_read:start_read + T]):
596601
floats = list(map(float, line.split()))
597602
data.append(floats[im + 1 - single])
598-
return idl, data
603+
return data
599604

600605

601-
def _read_append_rep(filename, pattern, b2b, cfg_separator, im, single):
606+
def _read_append_rep(filename, pattern, b2b, im, single, idl_func, cfg_func_args):
602607
with open(filename, 'r') as fp:
603608
content = fp.readlines()
604609
data_starts = []
@@ -634,7 +639,11 @@ def _read_append_rep(filename, pattern, b2b, cfg_separator, im, single):
634639
start = data_starts[cnfg]
635640
stop = start + data_starts[1]
636641
chunk = content[start:stop]
637-
idl, data = _read_chunk(chunk, gauge_line, cfg_separator, start_read, T, corr_line, b2b, pattern, im, single)
642+
try:
643+
idl = idl_func(chunk[gauge_line], *cfg_func_args)
644+
except Exception:
645+
raise Exception("Couldn't parse idl from file", filename, ", problem with chunk of lines", start + 1, "to", stop + 1)
646+
data = _read_chunk_data(chunk, start_read, T, corr_line, b2b, pattern, im, single)
638647
rep_idl.append(idl)
639648
rep_data.append(data)
640649

0 commit comments

Comments
 (0)