diff --git a/docs/pyerrors/input/sfcf.html b/docs/pyerrors/input/sfcf.html index 5c1fa3f1..6a4f9e84 100644 --- a/docs/pyerrors/input/sfcf.html +++ b/docs/pyerrors/input/sfcf.html @@ -52,9 +52,15 @@
10def read_sfcf(path, prefix, name, quarks='.*', corr_type='bi', noffset=0, wf=0, wf2=0, version="1.0c", cfg_separator="n", silent=False, **kwargs): - 11 """Read sfcf files from given folder structure. - 12 - 13 Parameters - 14 ---------- - 15 path : str - 16 Path to the sfcf files. - 17 prefix : str - 18 Prefix of the sfcf files. - 19 name : str - 20 Name of the correlation function to read. - 21 quarks : str - 22 Label of the quarks used in the sfcf input file. e.g. "quark quark" - 23 for version 0.0 this does NOT need to be given with the typical " - " - 24 that is present in the output file, - 25 this is done automatically for this version - 26 corr_type : str - 27 Type of correlation function to read. Can be - 28 - 'bi' for boundary-inner - 29 - 'bb' for boundary-boundary - 30 - 'bib' for boundary-inner-boundary - 31 noffset : int - 32 Offset of the source (only relevant when wavefunctions are used) - 33 wf : int - 34 ID of wave function - 35 wf2 : int - 36 ID of the second wavefunction - 37 (only relevant for boundary-to-boundary correlation functions) - 38 im : bool - 39 if True, read imaginary instead of real part - 40 of the correlation function. - 41 names : list - 42 Alternative labeling for replicas/ensembles. - 43 Has to have the appropriate length - 44 ens_name : str - 45 replaces the name of the ensemble - 46 version: str - 47 version of SFCF, with which the measurement was done. - 48 if the compact output option (-c) was specified, - 49 append a "c" to the version (e.g. "1.0c") - 50 if the append output option (-a) was specified, - 51 append an "a" to the version - 52 cfg_separator : str - 53 String that separates the ensemble identifier from the configuration number (default 'n'). - 54 replica: list - 55 list of replica to be read, default is all - 56 files: list - 57 list of files to be read per replica, default is all. - 58 for non-compact output format, hand the folders to be read here. - 59 check_configs: list[list[int]] - 60 list of list of supposed configs, eg. [range(1,1000)] - 61 for one replicum with 1000 configs - 62 - 63 Returns - 64 ------- - 65 result: list[Obs] - 66 list of Observables with length T, observable per timeslice. - 67 bb-type correlators have length 1. - 68 """ - 69 if kwargs.get('im'): - 70 im = 1 - 71 part = 'imaginary' - 72 else: - 73 im = 0 - 74 part = 'real' - 75 - 76 if corr_type == 'bb': - 77 b2b = True - 78 single = True - 79 elif corr_type == 'bib': - 80 b2b = True - 81 single = False - 82 else: - 83 b2b = False - 84 single = False - 85 - 86 known_versions = ["0.0", "1.0", "2.0", "1.0c", "2.0c", "1.0a", "2.0a"] - 87 - 88 if version not in known_versions: - 89 raise Exception("This version is not known!") - 90 if (version[-1] == "c"): - 91 appended = False - 92 compact = True - 93 version = version[:-1] - 94 elif (version[-1] == "a"): - 95 appended = True - 96 compact = False - 97 version = version[:-1] - 98 else: - 99 compact = False -100 appended = False -101 ls = [] -102 if "replica" in kwargs: -103 ls = kwargs.get("replica") -104 else: -105 for (dirpath, dirnames, filenames) in os.walk(path): -106 if not appended: -107 ls.extend(dirnames) -108 else: -109 ls.extend(filenames) -110 break -111 if not ls: -112 raise Exception('Error, directory not found') -113 # Exclude folders with different names -114 for exc in ls: -115 if not fnmatch.fnmatch(exc, prefix + '*'): -116 ls = list(set(ls) - set([exc])) -117 -118 if not appended: -119 ls = sort_names(ls) -120 replica = len(ls) -121 -122 else: -123 replica = len([file.split(".")[-1] for file in ls]) // len(set([file.split(".")[-1] for file in ls])) -124 if not silent: -125 print('Read', part, 'part of', name, 'from', prefix[:-1], ',', replica, 'replica') -126 -127 if 'names' in kwargs: -128 new_names = kwargs.get('names') -129 if len(new_names) != len(set(new_names)): -130 raise Exception("names are not unique!") -131 if len(new_names) != replica: -132 raise Exception('names should have the length', replica) -133 -134 else: -135 ens_name = kwargs.get("ens_name") -136 if not appended: -137 new_names = _get_rep_names(ls, ens_name) -138 else: -139 new_names = _get_appended_rep_names(ls, prefix, name, ens_name) -140 new_names = sort_names(new_names) -141 -142 idl = [] -143 if not appended: -144 for i, item in enumerate(ls): -145 rep_path = path + '/' + item -146 if "files" in kwargs: -147 files = kwargs.get("files") -148 else: -149 files = [] -150 sub_ls = _find_files(rep_path, prefix, compact, files) -151 rep_idl = [] -152 no_cfg = len(sub_ls) -153 for cfg in sub_ls: -154 try: -155 if compact: -156 rep_idl.append(int(cfg.split(cfg_separator)[-1])) -157 else: -158 rep_idl.append(int(cfg[3:])) -159 except Exception: -160 raise Exception("Couldn't parse idl from directroy, problem with file " + cfg) -161 rep_idl.sort() -162 # maybe there is a better way to print the idls -163 if not silent: -164 print(item, ':', no_cfg, ' configurations') -165 idl.append(rep_idl) -166 # here we have found all the files we need to look into. -167 if i == 0: -168 # here, we want to find the place within the file, -169 # where the correlator we need is stored. -170 # to do so, the pattern needed is put together -171 # from the input values -172 if version == "0.0": -173 file = path + '/' + item + '/' + sub_ls[0] + '/' + name -174 else: -175 if compact: -176 file = path + '/' + item + '/' + sub_ls[0] -177 else: -178 file = path + '/' + item + '/' + sub_ls[0] + '/' + name -179 -180 pattern = _make_pattern(version, name, noffset, wf, wf2, b2b, quarks) -181 start_read, T = _find_correlator(file, version, pattern, b2b, silent=silent) -182 -183 # preparing the datastructure -184 # the correlators get parsed into... -185 deltas = [] -186 for j in range(T): -187 deltas.append([]) -188 -189 if compact: -190 rep_deltas = _read_compact_rep(path, item, sub_ls, start_read, T, b2b, name, im) -191 -192 for t in range(T): -193 deltas[t].append(rep_deltas[t]) -194 else: -195 for t in range(T): -196 deltas[t].append(np.zeros(no_cfg)) -197 for cnfg, subitem in enumerate(sub_ls): -198 with open(path + '/' + item + '/' + subitem + '/' + name) as fp: -199 for k, line in enumerate(fp): -200 if (k >= start_read and k < start_read + T): -201 floats = list(map(float, line.split())) -202 if version == "0.0": -203 deltas[k - start_read][i][cnfg] = floats[im - single] -204 else: -205 deltas[k - start_read][i][cnfg] = floats[1 + im - single] -206 -207 else: -208 if "files" in kwargs: -209 ls = kwargs.get("files") -210 else: -211 for exc in ls: -212 if not fnmatch.fnmatch(exc, prefix + '*.' + name): -213 ls = list(set(ls) - set([exc])) -214 ls = sort_names(ls) -215 pattern = _make_pattern(version, name, noffset, wf, wf2, b2b, quarks) -216 deltas = [] -217 for rep, file in enumerate(ls): -218 rep_idl = [] -219 filename = path + '/' + file -220 T, rep_idl, rep_data = _read_append_rep(filename, pattern, b2b, cfg_separator, im, single) -221 if rep == 0: -222 for t in range(T): -223 deltas.append([]) -224 for t in range(T): -225 deltas[t].append(rep_data[t]) -226 idl.append(rep_idl) -227 -228 if "check_configs" in kwargs: -229 if not silent: -230 print("Checking for missing configs...") -231 che = kwargs.get("check_configs") -232 if not (len(che) == len(idl)): -233 raise Exception("check_configs has to be the same length as replica!") -234 for r in range(len(idl)): -235 if not silent: -236 print("checking " + new_names[r]) -237 check_idl(idl[r], che[r]) -238 if not silent: -239 print("Done") -240 result = [] -241 for t in range(T): -242 result.append(Obs(deltas[t], new_names, idl=idl)) -243 return result +@@ -838,6 +885,384 @@ bb-type correlators have length 1.14def read_sfcf(path, prefix, name, quarks='.*', corr_type="bi", noffset=0, wf=0, wf2=0, version="1.0c", cfg_separator="n", silent=False, **kwargs): +15 """Read sfcf files from given folder structure. +16 +17 Parameters +18 ---------- +19 path : str +20 Path to the sfcf files. +21 prefix : str +22 Prefix of the sfcf files. +23 name : str +24 Name of the correlation function to read. +25 quarks : str +26 Label of the quarks used in the sfcf input file. e.g. "quark quark" +27 for version 0.0 this does NOT need to be given with the typical " - " +28 that is present in the output file, +29 this is done automatically for this version +30 corr_type : str +31 Type of correlation function to read. Can be +32 - 'bi' for boundary-inner +33 - 'bb' for boundary-boundary +34 - 'bib' for boundary-inner-boundary +35 noffset : int +36 Offset of the source (only relevant when wavefunctions are used) +37 wf : int +38 ID of wave function +39 wf2 : int +40 ID of the second wavefunction +41 (only relevant for boundary-to-boundary correlation functions) +42 im : bool +43 if True, read imaginary instead of real part +44 of the correlation function. +45 names : list +46 Alternative labeling for replicas/ensembles. +47 Has to have the appropriate length +48 ens_name : str +49 replaces the name of the ensemble +50 version: str +51 version of SFCF, with which the measurement was done. +52 if the compact output option (-c) was specified, +53 append a "c" to the version (e.g. "1.0c") +54 if the append output option (-a) was specified, +55 append an "a" to the version +56 cfg_separator : str +57 String that separates the ensemble identifier from the configuration number (default 'n'). +58 replica: list +59 list of replica to be read, default is all +60 files: list +61 list of files to be read per replica, default is all. +62 for non-compact output format, hand the folders to be read here. +63 check_configs: list[list[int]] +64 list of list of supposed configs, eg. [range(1,1000)] +65 for one replicum with 1000 configs +66 +67 Returns +68 ------- +69 result: list[Obs] +70 list of Observables with length T, observable per timeslice. +71 bb-type correlators have length 1. +72 """ +73 ret = read_sfcf_multi(path, prefix, [name], quarks_list=[quarks], corr_type_list=[corr_type], +74 noffset_list=[noffset], wf_list=[wf], wf2_list=[wf2], version=version, +75 cfg_separator=cfg_separator, silent=silent, **kwargs) +76 return ret[name][quarks][str(noffset)][str(wf)][str(wf2)]
79def 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): + 80 """Read sfcf files from given folder structure. + 81 + 82 Parameters + 83 ---------- + 84 path : str + 85 Path to the sfcf files. + 86 prefix : str + 87 Prefix of the sfcf files. + 88 name : str + 89 Name of the correlation function to read. + 90 quarks_list : list[str] + 91 Label of the quarks used in the sfcf input file. e.g. "quark quark" + 92 for version 0.0 this does NOT need to be given with the typical " - " + 93 that is present in the output file, + 94 this is done automatically for this version + 95 corr_type_list : list[str] + 96 Type of correlation function to read. Can be + 97 - 'bi' for boundary-inner + 98 - 'bb' for boundary-boundary + 99 - 'bib' for boundary-inner-boundary +100 noffset_list : list[int] +101 Offset of the source (only relevant when wavefunctions are used) +102 wf_list : int +103 ID of wave function +104 wf2_list : list[int] +105 ID of the second wavefunction +106 (only relevant for boundary-to-boundary correlation functions) +107 im : bool +108 if True, read imaginary instead of real part +109 of the correlation function. +110 names : list +111 Alternative labeling for replicas/ensembles. +112 Has to have the appropriate length +113 ens_name : str +114 replaces the name of the ensemble +115 version: str +116 version of SFCF, with which the measurement was done. +117 if the compact output option (-c) was specified, +118 append a "c" to the version (e.g. "1.0c") +119 if the append output option (-a) was specified, +120 append an "a" to the version +121 cfg_separator : str +122 String that separates the ensemble identifier from the configuration number (default 'n'). +123 replica: list +124 list of replica to be read, default is all +125 files: list +126 list of files to be read per replica, default is all. +127 for non-compact output format, hand the folders to be read here. +128 check_configs: list[list[int]] +129 list of list of supposed configs, eg. [range(1,1000)] +130 for one replicum with 1000 configs +131 +132 Returns +133 ------- +134 result: dict[list[Obs]] +135 dict with one of the following properties: +136 if keyed_out: +137 dict[key] = list[Obs] +138 where key has the form name/quarks/offset/wf/wf2 +139 if not keyed_out: +140 dict[name][quarks][offset][wf][wf2] = list[Obs] +141 """ +142 +143 if kwargs.get('im'): +144 im = 1 +145 part = 'imaginary' +146 else: +147 im = 0 +148 part = 'real' +149 +150 known_versions = ["0.0", "1.0", "2.0", "1.0c", "2.0c", "1.0a", "2.0a"] +151 +152 if version not in known_versions: +153 raise Exception("This version is not known!") +154 if (version[-1] == "c"): +155 appended = False +156 compact = True +157 version = version[:-1] +158 elif (version[-1] == "a"): +159 appended = True +160 compact = False +161 version = version[:-1] +162 else: +163 compact = False +164 appended = False +165 ls = [] +166 if "replica" in kwargs: +167 ls = kwargs.get("replica") +168 else: +169 for (dirpath, dirnames, filenames) in os.walk(path): +170 if not appended: +171 ls.extend(dirnames) +172 else: +173 ls.extend(filenames) +174 break +175 if not ls: +176 raise Exception('Error, directory not found') +177 # Exclude folders with different names +178 for exc in ls: +179 if not fnmatch.fnmatch(exc, prefix + '*'): +180 ls = list(set(ls) - set([exc])) +181 +182 if not appended: +183 ls = sort_names(ls) +184 replica = len(ls) +185 +186 else: +187 replica = len([file.split(".")[-1] for file in ls]) // len(set([file.split(".")[-1] for file in ls])) +188 if not silent: +189 print('Read', part, 'part of', name_list, 'from', prefix[:-1], ',', replica, 'replica') +190 +191 if 'names' in kwargs: +192 new_names = kwargs.get('names') +193 if len(new_names) != len(set(new_names)): +194 raise Exception("names are not unique!") +195 if len(new_names) != replica: +196 raise Exception('names should have the length', replica) +197 +198 else: +199 ens_name = kwargs.get("ens_name") +200 if not appended: +201 new_names = _get_rep_names(ls, ens_name) +202 else: +203 new_names = _get_appended_rep_names(ls, prefix, name_list[0], ens_name) +204 new_names = sort_names(new_names) +205 +206 idl = [] +207 +208 noffset_list = [str(x) for x in noffset_list] +209 wf_list = [str(x) for x in wf_list] +210 wf2_list = [str(x) for x in wf2_list] +211 +212 # setup dict structures +213 intern = {} +214 for name, corr_type in zip(name_list, corr_type_list): +215 intern[name] = {} +216 b2b, single = _extract_corr_type(corr_type) +217 intern[name]["b2b"] = b2b +218 intern[name]["single"] = single +219 intern[name]["spec"] = {} +220 for quarks in quarks_list: +221 intern[name]["spec"][quarks] = {} +222 for off in noffset_list: +223 intern[name]["spec"][quarks][off] = {} +224 for w in wf_list: +225 intern[name]["spec"][quarks][off][w] = {} +226 for w2 in wf2_list: +227 intern[name]["spec"][quarks][off][w][w2] = {} +228 intern[name]["spec"][quarks][off][w][w2]["pattern"] = _make_pattern(version, name, off, w, w2, intern[name]['b2b'], quarks) +229 +230 internal_ret_dict = {} +231 needed_keys = _lists2key(name_list, quarks_list, noffset_list, wf_list, wf2_list) +232 for key in needed_keys: +233 internal_ret_dict[key] = [] +234 +235 if not appended: +236 for i, item in enumerate(ls): +237 rep_path = path + '/' + item +238 if "files" in kwargs: +239 files = kwargs.get("files") +240 else: +241 files = [] +242 sub_ls = _find_files(rep_path, prefix, compact, files) +243 rep_idl = [] +244 no_cfg = len(sub_ls) +245 for cfg in sub_ls: +246 try: +247 if compact: +248 rep_idl.append(int(cfg.split(cfg_separator)[-1])) +249 else: +250 rep_idl.append(int(cfg[3:])) +251 except Exception: +252 raise Exception("Couldn't parse idl from directroy, problem with file " + cfg) +253 rep_idl.sort() +254 # maybe there is a better way to print the idls +255 if not silent: +256 print(item, ':', no_cfg, ' configurations') +257 idl.append(rep_idl) +258 # here we have found all the files we need to look into. +259 if i == 0: +260 if version != "0.0" and compact: +261 file = path + '/' + item + '/' + sub_ls[0] +262 for name in name_list: +263 if version == "0.0" or not compact: +264 file = path + '/' + item + '/' + sub_ls[0] + '/' + name +265 for key in _lists2key(quarks_list, noffset_list, wf_list, wf2_list): +266 specs = _key2specs(key) +267 quarks = specs[0] +268 off = specs[1] +269 w = specs[2] +270 w2 = specs[3] +271 # here, we want to find the place within the file, +272 # where the correlator we need is stored. +273 # to do so, the pattern needed is put together +274 # from the input values +275 start_read, T = _find_correlator(file, version, intern[name]["spec"][quarks][str(off)][str(w)][str(w2)]["pattern"], intern[name]['b2b'], silent=silent) +276 intern[name]["spec"][quarks][str(off)][str(w)][str(w2)]["start"] = start_read +277 intern[name]["T"] = T +278 # preparing the datastructure +279 # the correlators get parsed into... +280 deltas = [] +281 for j in range(intern[name]["T"]): +282 deltas.append([]) +283 internal_ret_dict[sep.join([name, key])] = deltas +284 +285 if compact: +286 rep_deltas = _read_compact_rep(path, item, sub_ls, intern, needed_keys, im) +287 for key in needed_keys: +288 name = _key2specs(key)[0] +289 for t in range(intern[name]["T"]): +290 internal_ret_dict[key][t].append(rep_deltas[key][t]) +291 else: +292 for key in needed_keys: +293 rep_data = [] +294 name = _key2specs(key)[0] +295 for subitem in sub_ls: +296 cfg_path = path + '/' + item + '/' + subitem +297 file_data = _read_o_file(cfg_path, name, needed_keys, intern, version, im) +298 rep_data.append(file_data) +299 print(rep_data) +300 for t in range(intern[name]["T"]): +301 internal_ret_dict[key][t].append([]) +302 for cfg in range(no_cfg): +303 internal_ret_dict[key][t][i].append(rep_data[cfg][key][t]) +304 else: +305 for key in needed_keys: +306 specs = _key2specs(key) +307 name = specs[0] +308 quarks = specs[1] +309 off = specs[2] +310 w = specs[3] +311 w2 = specs[4] +312 if "files" in kwargs: +313 ls = kwargs.get("files") +314 else: +315 name_ls = ls +316 for exc in name_ls: +317 if not fnmatch.fnmatch(exc, prefix + '*.' + name): +318 name_ls = list(set(name_ls) - set([exc])) +319 name_ls = sort_names(name_ls) +320 pattern = intern[name]['spec'][quarks][off][w][w2]['pattern'] +321 deltas = [] +322 for rep, file in enumerate(name_ls): +323 rep_idl = [] +324 filename = path + '/' + file +325 T, rep_idl, rep_data = _read_append_rep(filename, pattern, intern[name]['b2b'], cfg_separator, im, intern[name]['single']) +326 if rep == 0: +327 intern[name]['T'] = T +328 for t in range(intern[name]['T']): +329 deltas.append([]) +330 for t in range(intern[name]['T']): +331 deltas[t].append(rep_data[t]) +332 internal_ret_dict[key] = deltas +333 if name == name_list[0]: +334 idl.append(rep_idl) +335 +336 if kwargs.get("check_configs") is True: +337 if not silent: +338 print("Checking for missing configs...") +339 che = kwargs.get("check_configs") +340 if not (len(che) == len(idl)): +341 raise Exception("check_configs has to be the same length as replica!") +342 for r in range(len(idl)): +343 if not silent: +344 print("checking " + new_names[r]) +345 check_idl(idl[r], che[r]) +346 if not silent: +347 print("Done") +348 +349 result_dict = {} +350 if keyed_out: +351 for key in needed_keys: +352 result = [] +353 for t in range(intern[name]["T"]): +354 result.append(Obs(internal_ret_dict[key][t], new_names, idl=idl)) +355 result_dict[key] = result +356 else: +357 for name in name_list: +358 result_dict[name] = {} +359 for quarks in quarks_list: +360 result_dict[name][quarks] = {} +361 for off in noffset_list: +362 result_dict[name][quarks][off] = {} +363 for w in wf_list: +364 result_dict[name][quarks][off][w] = {} +365 for w2 in wf2_list: +366 key = _specs2key(name, quarks, off, w, w2) +367 result = [] +368 for t in range(intern[name]["T"]): +369 result.append(Obs(internal_ret_dict[key][t], new_names, idl=idl)) +370 result_dict[name][quarks][str(off)][str(w)][str(w2)] = result +371 return result_dict +
Read sfcf files from given folder structure.
+ +