From 6ef081acef0b76498429332648c10d3b733d8374 Mon Sep 17 00:00:00 2001 From: jkuhl-uni Date: Fri, 16 Dec 2022 13:02:59 +0100 Subject: [PATCH 01/17] first version of skript to read xsf data --- pyerrors/input/hadrons.py | 4 ++- pyerrors/input/openQCD.py | 60 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/pyerrors/input/hadrons.py b/pyerrors/input/hadrons.py index dc8429e1..0892bf57 100644 --- a/pyerrors/input/hadrons.py +++ b/pyerrors/input/hadrons.py @@ -1,4 +1,5 @@ import os +import warnings from collections import Counter import h5py from pathlib import Path @@ -47,8 +48,9 @@ def _get_files(path, filestem, idl): idx = range(cnfg_numbers[0], cnfg_numbers[-1] + dc[0], dc[0]) elif idl: idx = idl + warnings.warn("Configurations are not evenly spaced.", RuntimeWarning) else: - raise Exception("Configurations are not evenly spaced. Provide an idl if you want to proceed with this set of configurations.") + raise Exception("Configurations are not evenly spaced.") return filtered_files, idx diff --git a/pyerrors/input/openQCD.py b/pyerrors/input/openQCD.py index 840ffc0e..0cf4762b 100644 --- a/pyerrors/input/openQCD.py +++ b/pyerrors/input/openQCD.py @@ -985,3 +985,63 @@ def read_qtop_sector(path, prefix, c, target=0, **kwargs): qtop = read_qtop(path, prefix, c, **kwargs) return qtop_projection(qtop, target=target) + + +def read_ms5_xsf(path, prefix, qc, corr): + + found = [] + files = [] + for (dirpath, dirnames, filenames) in os.walk(path + "/"): + found.extend(filenames) + break + for f in found: + if fnmatch.fnmatch(f, prefix + ".ms5_xsf_"+qc+".dat"): + files.append(f) + + files = sorted(files) + for file in files: + with open(path+"/"+file, "rb") as fp: + t = fp.read(8) + kappa = struct.unpack('d', t)[0] + t = fp.read(8) + csw = struct.unpack('d', t)[0] + t = fp.read(8) + dF = struct.unpack('d', t)[0] + t = fp.read(8) + zF = struct.unpack('d', t)[0] + + print("Parameters:\n kappa:",kappa, "csw:",csw,"dF:", dF,"zF:", zF) + + t = fp.read(4) + tmax = struct.unpack('i', t)[0] + t = fp.read(4) + bnd = struct.unpack('i', t)[0] + + print("T:", tmax) + t = fp.read(4) + cnfg = struct.unpack('i', t)[0] + print(cnfg) + placesBI = ["gS", "gP", "gA", "gV", "gVt", "lA", "lV", "lVt", + "lT", "lTt"] + placesBB = ["g1", "l1"] + if not corr in placesBB: + for i in range(placesBI.index(corr)): + t = fp.read(8*2*tmax) + + t = fp.read(8*2*tmax) + tmpcorr = struct.unpack('d'*2*tmax, t) + corrres = [[],[]] + for i in range(len(tmpcorr)): + corrres[i%2].append(tmpcorr[i]) + else: + # boundary to boundary correlators + for i in range(len(placesBI)): + t = fp.read(8*2*tmax) + for i in range(placesBB.index(corr)): + t = fp.read(8*2) + t = fp.read(8*2) + corrres = list(struct.unpack('d'*2, t)) + + + print(corrres) + From 3399b72bfb0be3a7b9fc0ecd7dd735980400cbe3 Mon Sep 17 00:00:00 2001 From: jkuhl-uni Date: Mon, 19 Dec 2022 18:29:51 +0100 Subject: [PATCH 02/17] new idea for reading ms5_xsf data --- pyerrors/input/openQCD.py | 78 ++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 22 deletions(-) diff --git a/pyerrors/input/openQCD.py b/pyerrors/input/openQCD.py index 0cf4762b..23a1b1dd 100644 --- a/pyerrors/input/openQCD.py +++ b/pyerrors/input/openQCD.py @@ -1001,6 +1001,7 @@ def read_ms5_xsf(path, prefix, qc, corr): files = sorted(files) for file in files: with open(path+"/"+file, "rb") as fp: + t = fp.read(8) kappa = struct.unpack('d', t)[0] t = fp.read(8) @@ -1018,30 +1019,63 @@ def read_ms5_xsf(path, prefix, qc, corr): bnd = struct.unpack('i', t)[0] print("T:", tmax) - t = fp.read(4) - cnfg = struct.unpack('i', t)[0] - print(cnfg) - placesBI = ["gS", "gP", "gA", "gV", "gVt", "lA", "lV", "lVt", - "lT", "lTt"] - placesBB = ["g1", "l1"] - if not corr in placesBB: - for i in range(placesBI.index(corr)): - t = fp.read(8*2*tmax) - t = fp.read(8*2*tmax) - tmpcorr = struct.unpack('d'*2*tmax, t) - corrres = [[],[]] - for i in range(len(tmpcorr)): - corrres[i%2].append(tmpcorr[i]) - else: - # boundary to boundary correlators - for i in range(len(placesBI)): - t = fp.read(8*2*tmax) - for i in range(placesBB.index(corr)): - t = fp.read(8*2) - t = fp.read(8*2) - corrres = list(struct.unpack('d'*2, t)) + placesBI = ["gS", "gP", + "gA", "gV", + "gVt", "lA", + "lV", "lVt", + "lT", "lTt"] + placesBB = ["g1", "l1"] + # bytes per config + # the chunks have the following structure: + # confignumber, 10x timedependent complex correlators, 2x timeindependent complex correlators + + chunksize = 4 +( 8 *2*tmax*10)+( 8 *2*2) + print(chunksize) + packstr ='i'+('d'*2*tmax*10)+('d'*2*2) + while True: + cnfgt = fp.read(chunksize) + if not cnfgt: + break + asascii=struct.unpack(packstr, cnfgt) + cnfg = asascii[0] + #cnfg = struct.unpack('i', t)[0] + print(cnfg) + + if not corr in placesBB: + #for i in range(placesBI.index(corr)): + # t = fp.read(8*2*tmax) + tmpcorr = asascii[1+2*tmax*placesBI.index(corr):1+2*tmax*placesBI.index(corr)+2*tmax] + #t = fp.read(8*2*tmax) + #tmpcorr = struct.unpack('d'*2*tmax, t) + corrres = [[],[]] + for i in range(len(tmpcorr)): + corrres[i%2].append(tmpcorr[i]) + # jump to next config + #t = fp.read(chunksize-(4+placesBI.index(corr)*8*2*tmax+2*8*2)) + else: + #for i in range(placesBI.index(corr)): + # t = fp.read(8*2*tmax) + tmpcorr = asascii[1+2*tmax*len(placesBI):1+2*tmax*len(placesBI)+2] + #t = fp.read(8*2*tmax) + #tmpcorr = struct.unpack('d'*2*tmax, t) + corrres = [[],[]] + for i in range(len(tmpcorr)): + corrres[i%2].append(tmpcorr[i]) + # jump to next config + #t = fp.read(chunksize-(4+placesBI.index(corr)*8*2*tmax+2*8*2)) + + # boundary to boundary correlators + #for i in range(len(placesBI)): + # t = fp.read(8*2*tmax) + #for i in range(placesBB.index(corr)): + # t = fp.read(8*2) + #t = fp.read(8*2) + #corrres = list(struct.unpack('d'*2, t)) + # jump to next config + #t = fp.read(chunksize-(4+len(placesBI)*8*2*tmax+(2-placesBB.index(corr))*8*2)) + print(corrres) From ed0b7cad3fc3fceca915065326f1ac422df23e7d Mon Sep 17 00:00:00 2001 From: jkuhl-uni Date: Tue, 20 Dec 2022 09:56:10 +0100 Subject: [PATCH 03/17] now able to read not just one config --- pyerrors/input/openQCD.py | 31 +++---------------------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/pyerrors/input/openQCD.py b/pyerrors/input/openQCD.py index 23a1b1dd..50d1255e 100644 --- a/pyerrors/input/openQCD.py +++ b/pyerrors/input/openQCD.py @@ -1029,53 +1029,28 @@ def read_ms5_xsf(path, prefix, qc, corr): # bytes per config # the chunks have the following structure: - # confignumber, 10x timedependent complex correlators, 2x timeindependent complex correlators + # confignumber, 10x timedependent complex correlators as doubles, 2x timeindependent complex correlators as doubles chunksize = 4 +( 8 *2*tmax*10)+( 8 *2*2) - print(chunksize) - packstr ='i'+('d'*2*tmax*10)+('d'*2*2) + packstr ='=i'+('d'*2*tmax*10)+('d'*2*2) while True: cnfgt = fp.read(chunksize) if not cnfgt: break asascii=struct.unpack(packstr, cnfgt) cnfg = asascii[0] - #cnfg = struct.unpack('i', t)[0] print(cnfg) if not corr in placesBB: - #for i in range(placesBI.index(corr)): - # t = fp.read(8*2*tmax) tmpcorr = asascii[1+2*tmax*placesBI.index(corr):1+2*tmax*placesBI.index(corr)+2*tmax] - #t = fp.read(8*2*tmax) - #tmpcorr = struct.unpack('d'*2*tmax, t) corrres = [[],[]] for i in range(len(tmpcorr)): corrres[i%2].append(tmpcorr[i]) - # jump to next config - #t = fp.read(chunksize-(4+placesBI.index(corr)*8*2*tmax+2*8*2)) else: - #for i in range(placesBI.index(corr)): - # t = fp.read(8*2*tmax) tmpcorr = asascii[1+2*tmax*len(placesBI):1+2*tmax*len(placesBI)+2] - #t = fp.read(8*2*tmax) - #tmpcorr = struct.unpack('d'*2*tmax, t) corrres = [[],[]] for i in range(len(tmpcorr)): corrres[i%2].append(tmpcorr[i]) - # jump to next config - #t = fp.read(chunksize-(4+placesBI.index(corr)*8*2*tmax+2*8*2)) - - # boundary to boundary correlators - #for i in range(len(placesBI)): - # t = fp.read(8*2*tmax) - #for i in range(placesBB.index(corr)): - # t = fp.read(8*2) - #t = fp.read(8*2) - #corrres = list(struct.unpack('d'*2, t)) - # jump to next config - #t = fp.read(chunksize-(4+len(placesBI)*8*2*tmax+(2-placesBB.index(corr))*8*2)) - - print(corrres) + print(corrres) From 0f440d1d6e51dfb68101767beaefae395ff682b8 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Tue, 20 Dec 2022 10:16:07 +0100 Subject: [PATCH 04/17] fix: reapplied changes to hadrons.get_cnfg_number. --- pyerrors/input/hadrons.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pyerrors/input/hadrons.py b/pyerrors/input/hadrons.py index 0892bf57..dc8429e1 100644 --- a/pyerrors/input/hadrons.py +++ b/pyerrors/input/hadrons.py @@ -1,5 +1,4 @@ import os -import warnings from collections import Counter import h5py from pathlib import Path @@ -48,9 +47,8 @@ def _get_files(path, filestem, idl): idx = range(cnfg_numbers[0], cnfg_numbers[-1] + dc[0], dc[0]) elif idl: idx = idl - warnings.warn("Configurations are not evenly spaced.", RuntimeWarning) else: - raise Exception("Configurations are not evenly spaced.") + raise Exception("Configurations are not evenly spaced. Provide an idl if you want to proceed with this set of configurations.") return filtered_files, idx From f74793b76526d1a10e2c96c9745b199a31f67f99 Mon Sep 17 00:00:00 2001 From: jkuhl-uni Date: Tue, 20 Dec 2022 13:36:08 +0100 Subject: [PATCH 05/17] Add docstring to read_ms5_xsf method --- pyerrors/input/openQCD.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pyerrors/input/openQCD.py b/pyerrors/input/openQCD.py index 50d1255e..e0902569 100644 --- a/pyerrors/input/openQCD.py +++ b/pyerrors/input/openQCD.py @@ -1039,7 +1039,7 @@ def read_ms5_xsf(path, prefix, qc, corr): break asascii=struct.unpack(packstr, cnfgt) cnfg = asascii[0] - print(cnfg) + #print(cnfg) if not corr in placesBB: tmpcorr = asascii[1+2*tmax*placesBI.index(corr):1+2*tmax*placesBI.index(corr)+2*tmax] @@ -1052,5 +1052,6 @@ def read_ms5_xsf(path, prefix, qc, corr): for i in range(len(tmpcorr)): corrres[i%2].append(tmpcorr[i]) - print(corrres) + #print(corrres) + From ae625486617749f7fa90c1af6f8abfa00e74410c Mon Sep 17 00:00:00 2001 From: jkuhl-uni Date: Tue, 20 Dec 2022 13:44:07 +0100 Subject: [PATCH 06/17] Add a better docstring to read_ms5_xsf method --- pyerrors/input/openQCD.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pyerrors/input/openQCD.py b/pyerrors/input/openQCD.py index e0902569..eb9fb99e 100644 --- a/pyerrors/input/openQCD.py +++ b/pyerrors/input/openQCD.py @@ -988,7 +988,25 @@ def read_qtop_sector(path, prefix, c, target=0, **kwargs): def read_ms5_xsf(path, prefix, qc, corr): + """ + Read and process data from files with a specific prefix and containing the given quark combination in the file name. + Parameters + ---------- + path : str + The path to the directory containing the data files. + prefix : str + The prefix of the data files to be processed. + qc : str + The quark combination to be used to filter the data files. + corr : str + The correlator to be extracted from the data files. + + Returns + ------- + list of list of float + A list of lists containing the real and imaginary parts of the specified correlator for each configuration. + """ found = [] files = [] for (dirpath, dirnames, filenames) in os.walk(path + "/"): From 134fe7d3f7b48a899dc3f0780819e5b8c93ab745 Mon Sep 17 00:00:00 2001 From: jkuhl-uni Date: Tue, 20 Dec 2022 15:08:34 +0100 Subject: [PATCH 07/17] first steps to output in the right format --- pyerrors/input/openQCD.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/pyerrors/input/openQCD.py b/pyerrors/input/openQCD.py index eb9fb99e..dc3cdf5d 100644 --- a/pyerrors/input/openQCD.py +++ b/pyerrors/input/openQCD.py @@ -8,6 +8,9 @@ import matplotlib.pyplot as plt from matplotlib import gridspec from ..obs import Obs from ..fits import fit_lin +from ..obs import CObs +from ..correlators import Corr + def read_rwms(path, prefix, version='2.0', names=None, **kwargs): @@ -989,7 +992,8 @@ def read_qtop_sector(path, prefix, c, target=0, **kwargs): def read_ms5_xsf(path, prefix, qc, corr): """ - Read and process data from files with a specific prefix and containing the given quark combination in the file name. + Read and process data from files produced by the ms5_xsf method + with a specific prefix and for a given quark combination. Parameters ---------- @@ -1051,25 +1055,36 @@ def read_ms5_xsf(path, prefix, qc, corr): chunksize = 4 +( 8 *2*tmax*10)+( 8 *2*2) packstr ='=i'+('d'*2*tmax*10)+('d'*2*2) + cnfgs = [] + realsamples = [] + imagsamples = [] + for t in range(tmax): + realsamples.append([]) + imagsamples.append([]) + while True: cnfgt = fp.read(chunksize) if not cnfgt: break asascii=struct.unpack(packstr, cnfgt) cnfg = asascii[0] - #print(cnfg) + cnfgs.append(cnfg) if not corr in placesBB: tmpcorr = asascii[1+2*tmax*placesBI.index(corr):1+2*tmax*placesBI.index(corr)+2*tmax] corrres = [[],[]] - for i in range(len(tmpcorr)): - corrres[i%2].append(tmpcorr[i]) + for i in range(len(tmpcorr)): corrres[i%2].append(tmpcorr[i]) + for t in range(tmax): realsamples[t].append(corrres[0][t]) + for t in range(tmax): imagsamples[t].append(corrres[1][t]) else: tmpcorr = asascii[1+2*tmax*len(placesBI):1+2*tmax*len(placesBI)+2] corrres = [[],[]] for i in range(len(tmpcorr)): corrres[i%2].append(tmpcorr[i]) - - #print(corrres) - - + realObs = [] + imagObs = [] + compObs = [] + for t in range(tmax): realObs.append(Obs(realsamples[t], names = prefix, idl = cnfgs)) + for t in range(tmax): imagObs.append(Obs(imagsamples[t], names = prefix, idl = cnfgs)) + for t in range(tmax): compObs.append(CObs(realObs[t], imagObs[t])) + return Corr(compObs) \ No newline at end of file From ef2323e9bd0d5472119eb91b5e741208db30073b Mon Sep 17 00:00:00 2001 From: jkuhl-uni Date: Tue, 20 Dec 2022 15:43:17 +0100 Subject: [PATCH 08/17] missed some brakets --- pyerrors/input/openQCD.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyerrors/input/openQCD.py b/pyerrors/input/openQCD.py index dc3cdf5d..0a88dc18 100644 --- a/pyerrors/input/openQCD.py +++ b/pyerrors/input/openQCD.py @@ -1084,7 +1084,7 @@ def read_ms5_xsf(path, prefix, qc, corr): realObs = [] imagObs = [] compObs = [] - for t in range(tmax): realObs.append(Obs(realsamples[t], names = prefix, idl = cnfgs)) - for t in range(tmax): imagObs.append(Obs(imagsamples[t], names = prefix, idl = cnfgs)) + for t in range(tmax): realObs.append(Obs([realsamples[t]], names = [prefix], idl = [cnfgs])) + for t in range(tmax): imagObs.append(Obs([imagsamples[t]], names = [prefix], idl = [cnfgs])) for t in range(tmax): compObs.append(CObs(realObs[t], imagObs[t])) return Corr(compObs) \ No newline at end of file From 3b797a05598e541e1e81fdc214ecd7b8ddca9765 Mon Sep 17 00:00:00 2001 From: jkuhl-uni Date: Tue, 20 Dec 2022 15:47:36 +0100 Subject: [PATCH 09/17] better indexing --- pyerrors/input/openQCD.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pyerrors/input/openQCD.py b/pyerrors/input/openQCD.py index 0a88dc18..0aa7ecb5 100644 --- a/pyerrors/input/openQCD.py +++ b/pyerrors/input/openQCD.py @@ -1074,8 +1074,8 @@ def read_ms5_xsf(path, prefix, qc, corr): tmpcorr = asascii[1+2*tmax*placesBI.index(corr):1+2*tmax*placesBI.index(corr)+2*tmax] corrres = [[],[]] for i in range(len(tmpcorr)): corrres[i%2].append(tmpcorr[i]) - for t in range(tmax): realsamples[t].append(corrres[0][t]) - for t in range(tmax): imagsamples[t].append(corrres[1][t]) + for t in range(int(len(tmpcorr)/2)): realsamples[t].append(corrres[0][t]) + for t in range(int(len(tmpcorr)/2)): imagsamples[t].append(corrres[1][t]) else: tmpcorr = asascii[1+2*tmax*len(placesBI):1+2*tmax*len(placesBI)+2] corrres = [[],[]] @@ -1084,7 +1084,7 @@ def read_ms5_xsf(path, prefix, qc, corr): realObs = [] imagObs = [] compObs = [] - for t in range(tmax): realObs.append(Obs([realsamples[t]], names = [prefix], idl = [cnfgs])) - for t in range(tmax): imagObs.append(Obs([imagsamples[t]], names = [prefix], idl = [cnfgs])) - for t in range(tmax): compObs.append(CObs(realObs[t], imagObs[t])) + for t in range(int(len(tmpcorr)/2)): realObs.append(Obs([realsamples[t]], names = [prefix], idl = [cnfgs])) + for t in range(int(len(tmpcorr)/2)): imagObs.append(Obs([imagsamples[t]], names = [prefix], idl = [cnfgs])) + for t in range(int(len(tmpcorr)/2)): compObs.append(CObs(realObs[t], imagObs[t])) return Corr(compObs) \ No newline at end of file From 4fb27462bd622f08cd2e8d5d2d9833013917d403 Mon Sep 17 00:00:00 2001 From: jkuhl-uni Date: Tue, 20 Dec 2022 16:04:15 +0100 Subject: [PATCH 10/17] almsot done, still need to fix Corr(CObs) for some reason --- pyerrors/input/openQCD.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyerrors/input/openQCD.py b/pyerrors/input/openQCD.py index 0aa7ecb5..7774277e 100644 --- a/pyerrors/input/openQCD.py +++ b/pyerrors/input/openQCD.py @@ -1081,6 +1081,7 @@ def read_ms5_xsf(path, prefix, qc, corr): corrres = [[],[]] for i in range(len(tmpcorr)): corrres[i%2].append(tmpcorr[i]) + # we have the data now... but we need to re format the whole thing and put it into Corr objects. realObs = [] imagObs = [] compObs = [] From 11ffd4571df91520db8f7ab1ac1c3cb91026bc48 Mon Sep 17 00:00:00 2001 From: jkuhl-uni Date: Tue, 20 Dec 2022 18:01:46 +0100 Subject: [PATCH 11/17] a bit of housekeeping --- pyerrors/input/openQCD.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/pyerrors/input/openQCD.py b/pyerrors/input/openQCD.py index 7774277e..35c9e255 100644 --- a/pyerrors/input/openQCD.py +++ b/pyerrors/input/openQCD.py @@ -1072,15 +1072,12 @@ def read_ms5_xsf(path, prefix, qc, corr): if not corr in placesBB: tmpcorr = asascii[1+2*tmax*placesBI.index(corr):1+2*tmax*placesBI.index(corr)+2*tmax] - corrres = [[],[]] - for i in range(len(tmpcorr)): corrres[i%2].append(tmpcorr[i]) - for t in range(int(len(tmpcorr)/2)): realsamples[t].append(corrres[0][t]) - for t in range(int(len(tmpcorr)/2)): imagsamples[t].append(corrres[1][t]) else: - tmpcorr = asascii[1+2*tmax*len(placesBI):1+2*tmax*len(placesBI)+2] - corrres = [[],[]] - for i in range(len(tmpcorr)): - corrres[i%2].append(tmpcorr[i]) + tmpcorr = asascii[1+2*tmax*len(placesBI)+2*placesBI.index(corr):1+2*tmax*len(placesBI)+2*placesBI.index(corr)+2] + corrres = [[],[]] + for i in range(len(tmpcorr)): corrres[i%2].append(tmpcorr[i]) + for t in range(int(len(tmpcorr)/2)): realsamples[t].append(corrres[0][t]) + for t in range(int(len(tmpcorr)/2)): imagsamples[t].append(corrres[1][t]) # we have the data now... but we need to re format the whole thing and put it into Corr objects. realObs = [] imagObs = [] From 785c590397e4fe41a9e47ef3998e3dda354b02c9 Mon Sep 17 00:00:00 2001 From: jkuhl-uni Date: Tue, 20 Dec 2022 18:08:22 +0100 Subject: [PATCH 12/17] small bug fix --- pyerrors/input/openQCD.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyerrors/input/openQCD.py b/pyerrors/input/openQCD.py index 35c9e255..3fcd6e7b 100644 --- a/pyerrors/input/openQCD.py +++ b/pyerrors/input/openQCD.py @@ -1073,7 +1073,7 @@ def read_ms5_xsf(path, prefix, qc, corr): if not corr in placesBB: tmpcorr = asascii[1+2*tmax*placesBI.index(corr):1+2*tmax*placesBI.index(corr)+2*tmax] else: - tmpcorr = asascii[1+2*tmax*len(placesBI)+2*placesBI.index(corr):1+2*tmax*len(placesBI)+2*placesBI.index(corr)+2] + tmpcorr = asascii[1+2*tmax*len(placesBI)+2*placesBB.index(corr):1+2*tmax*len(placesBI)+2*placesBB.index(corr)+2] corrres = [[],[]] for i in range(len(tmpcorr)): corrres[i%2].append(tmpcorr[i]) for t in range(int(len(tmpcorr)/2)): realsamples[t].append(corrres[0][t]) From 6d7fd3bcfbcaa311b68f9bef59c90ad1c30043da Mon Sep 17 00:00:00 2001 From: jkuhl-uni Date: Wed, 21 Dec 2022 14:57:08 +0100 Subject: [PATCH 13/17] xsf-method can now handle multiple replika --- pyerrors/input/openQCD.py | 75 +++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/pyerrors/input/openQCD.py b/pyerrors/input/openQCD.py index 3fcd6e7b..ca3c33fe 100644 --- a/pyerrors/input/openQCD.py +++ b/pyerrors/input/openQCD.py @@ -990,37 +990,28 @@ def read_qtop_sector(path, prefix, c, target=0, **kwargs): return qtop_projection(qtop, target=target) -def read_ms5_xsf(path, prefix, qc, corr): - """ - Read and process data from files produced by the ms5_xsf method - with a specific prefix and for a given quark combination. - - Parameters - ---------- - path : str - The path to the directory containing the data files. - prefix : str - The prefix of the data files to be processed. - qc : str - The quark combination to be used to filter the data files. - corr : str - The correlator to be extracted from the data files. - - Returns - ------- - list of list of float - A list of lists containing the real and imaginary parts of the specified correlator for each configuration. - """ +def read_ms5_xsf(path, prefix, qc, corr, sep = "r", **kwargs): found = [] files = [] + names = [] for (dirpath, dirnames, filenames) in os.walk(path + "/"): found.extend(filenames) break for f in found: - if fnmatch.fnmatch(f, prefix + ".ms5_xsf_"+qc+".dat"): + if fnmatch.fnmatch(f, prefix + "*.ms5_xsf_"+qc+".dat"): files.append(f) - + names.append(prefix+"|r"+f.split(".")[0].split(sep)[1]) files = sorted(files) + + if "names" in kwargs: + names = kwargs.get("names") + else: + names = sorted(names) + + cnfgs = [] + realsamples = [] + imagsamples = [] + repnum = 0 for file in files: with open(path+"/"+file, "rb") as fp: @@ -1033,14 +1024,11 @@ def read_ms5_xsf(path, prefix, qc, corr): t = fp.read(8) zF = struct.unpack('d', t)[0] - print("Parameters:\n kappa:",kappa, "csw:",csw,"dF:", dF,"zF:", zF) - t = fp.read(4) tmax = struct.unpack('i', t)[0] t = fp.read(4) bnd = struct.unpack('i', t)[0] - - print("T:", tmax) + placesBI = ["gS", "gP", "gA", "gV", @@ -1053,14 +1041,14 @@ def read_ms5_xsf(path, prefix, qc, corr): # the chunks have the following structure: # confignumber, 10x timedependent complex correlators as doubles, 2x timeindependent complex correlators as doubles - chunksize = 4 +( 8 *2*tmax*10)+( 8 *2*2) + chunksize = 4 +( 8 *2*tmax*10)+( 8 *2*2) packstr ='=i'+('d'*2*tmax*10)+('d'*2*2) - cnfgs = [] - realsamples = [] - imagsamples = [] + cnfgs.append([]) + realsamples.append([]) + imagsamples.append([]) for t in range(tmax): - realsamples.append([]) - imagsamples.append([]) + realsamples[repnum].append([]) + imagsamples[repnum].append([]) while True: cnfgt = fp.read(chunksize) @@ -1068,7 +1056,7 @@ def read_ms5_xsf(path, prefix, qc, corr): break asascii=struct.unpack(packstr, cnfgt) cnfg = asascii[0] - cnfgs.append(cnfg) + cnfgs[repnum].append(cnfg) if not corr in placesBB: tmpcorr = asascii[1+2*tmax*placesBI.index(corr):1+2*tmax*placesBI.index(corr)+2*tmax] @@ -1076,13 +1064,24 @@ def read_ms5_xsf(path, prefix, qc, corr): tmpcorr = asascii[1+2*tmax*len(placesBI)+2*placesBB.index(corr):1+2*tmax*len(placesBI)+2*placesBB.index(corr)+2] corrres = [[],[]] for i in range(len(tmpcorr)): corrres[i%2].append(tmpcorr[i]) - for t in range(int(len(tmpcorr)/2)): realsamples[t].append(corrres[0][t]) - for t in range(int(len(tmpcorr)/2)): imagsamples[t].append(corrres[1][t]) + for t in range(int(len(tmpcorr)/2)): realsamples[repnum][t].append(corrres[0][t]) + for t in range(int(len(tmpcorr)/2)): imagsamples[repnum][t].append(corrres[1][t]) + repnum += 1 + + s = "Read correlator "+ corr+ " from "+ str(repnum)+ " replika with "+str(len(realsamples[0][t])) + for rep in range(1,repnum): + s+=", "+str(len(realsamples[rep][t])) + s+=" samples" + print(s) + print("Asserted run parameters:\n T:", tmax, "kappa:", kappa, "csw:", csw, "dF:", dF, "zF:", zF) + # we have the data now... but we need to re format the whole thing and put it into Corr objects. + realObs = [] imagObs = [] compObs = [] - for t in range(int(len(tmpcorr)/2)): realObs.append(Obs([realsamples[t]], names = [prefix], idl = [cnfgs])) - for t in range(int(len(tmpcorr)/2)): imagObs.append(Obs([imagsamples[t]], names = [prefix], idl = [cnfgs])) + + for t in range(int(len(tmpcorr)/2)): realObs.append(Obs([realsamples[rep][t] for rep in range(repnum)], names = names, idl = cnfgs)) + for t in range(int(len(tmpcorr)/2)): imagObs.append(Obs([imagsamples[rep][t] for rep in range(repnum)], names = names, idl = cnfgs)) for t in range(int(len(tmpcorr)/2)): compObs.append(CObs(realObs[t], imagObs[t])) return Corr(compObs) \ No newline at end of file From f91c4adff4762b680aff5d72cd0ff583cbfb0645 Mon Sep 17 00:00:00 2001 From: jkuhl-uni Date: Wed, 21 Dec 2022 15:12:45 +0100 Subject: [PATCH 14/17] added a wonderful ChatGPT docstring --- pyerrors/input/openQCD.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/pyerrors/input/openQCD.py b/pyerrors/input/openQCD.py index ca3c33fe..ce6f181f 100644 --- a/pyerrors/input/openQCD.py +++ b/pyerrors/input/openQCD.py @@ -991,6 +991,40 @@ def read_qtop_sector(path, prefix, c, target=0, **kwargs): def read_ms5_xsf(path, prefix, qc, corr, sep = "r", **kwargs): + """ + Read data from files in the specified directory with the specified prefix and quark combination extension, and return a `Corr` object containing the data. + + Parameters + ---------- + path : str + The directory to search for the files in. + prefix : str + The prefix to match the files against. + qc : str + The quark combination extension to match the files against. + corr : str + The correlator to extract data for. + sep : str, optional + The separator to use when parsing the replika names. + **kwargs + Additional keyword arguments. The following keyword arguments are recognized: + + - names (List[str]): A list of names to use for the replicas. + + Returns + ------- + Corr + A complex valued `Corr` object containing the data read from the files. + + Raises + ------ + FileNotFoundError + If no files matching the specified prefix and quark combination extension are found in the specified directory. + IOError + If there is an error reading a file. + struct.error + If there is an error unpacking binary data. + """ found = [] files = [] names = [] From f9fe5c7ec9c8e22f48c0519b40f52f91839ac123 Mon Sep 17 00:00:00 2001 From: jkuhl-uni Date: Wed, 21 Dec 2022 15:34:14 +0100 Subject: [PATCH 15/17] small changes to rep names --- pyerrors/input/openQCD.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pyerrors/input/openQCD.py b/pyerrors/input/openQCD.py index ce6f181f..de972b9e 100644 --- a/pyerrors/input/openQCD.py +++ b/pyerrors/input/openQCD.py @@ -1034,7 +1034,10 @@ def read_ms5_xsf(path, prefix, qc, corr, sep = "r", **kwargs): for f in found: if fnmatch.fnmatch(f, prefix + "*.ms5_xsf_"+qc+".dat"): files.append(f) - names.append(prefix+"|r"+f.split(".")[0].split(sep)[1]) + if not sep == "": + names.append(prefix+"|r"+f.split(".")[0].split(sep)[1]) + else: + names.append(prefix) files = sorted(files) if "names" in kwargs: From 3fdbc371b2162269b1a8e44429c772bc37518e53 Mon Sep 17 00:00:00 2001 From: jkuhl-uni Date: Wed, 21 Dec 2022 17:47:01 +0100 Subject: [PATCH 16/17] linting... --- pyerrors/input/openQCD.py | 88 +++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/pyerrors/input/openQCD.py b/pyerrors/input/openQCD.py index de972b9e..7eb1f3ee 100644 --- a/pyerrors/input/openQCD.py +++ b/pyerrors/input/openQCD.py @@ -990,10 +990,10 @@ def read_qtop_sector(path, prefix, c, target=0, **kwargs): return qtop_projection(qtop, target=target) -def read_ms5_xsf(path, prefix, qc, corr, sep = "r", **kwargs): +def read_ms5_xsf(path, prefix, qc, corr, sep="r", **kwargs): """ Read data from files in the specified directory with the specified prefix and quark combination extension, and return a `Corr` object containing the data. - + Parameters ---------- path : str @@ -1008,14 +1008,14 @@ def read_ms5_xsf(path, prefix, qc, corr, sep = "r", **kwargs): The separator to use when parsing the replika names. **kwargs Additional keyword arguments. The following keyword arguments are recognized: - + - names (List[str]): A list of names to use for the replicas. - + Returns ------- Corr A complex valued `Corr` object containing the data read from the files. - + Raises ------ FileNotFoundError @@ -1025,17 +1025,19 @@ def read_ms5_xsf(path, prefix, qc, corr, sep = "r", **kwargs): struct.error If there is an error unpacking binary data. """ + found = [] files = [] names = [] for (dirpath, dirnames, filenames) in os.walk(path + "/"): found.extend(filenames) break + for f in found: - if fnmatch.fnmatch(f, prefix + "*.ms5_xsf_"+qc+".dat"): + if fnmatch.fnmatch(f, prefix + "*.ms5_xsf_" + qc + ".dat"): files.append(f) if not sep == "": - names.append(prefix+"|r"+f.split(".")[0].split(sep)[1]) + names.append(prefix + "|r" + f.split(".")[0].split(sep)[1]) else: names.append(prefix) files = sorted(files) @@ -1044,14 +1046,14 @@ def read_ms5_xsf(path, prefix, qc, corr, sep = "r", **kwargs): names = kwargs.get("names") else: names = sorted(names) - + cnfgs = [] realsamples = [] imagsamples = [] repnum = 0 for file in files: - with open(path+"/"+file, "rb") as fp: - + with open(path + "/" + file, "rb") as fp: + t = fp.read(8) kappa = struct.unpack('d', t)[0] t = fp.read(8) @@ -1060,16 +1062,15 @@ def read_ms5_xsf(path, prefix, qc, corr, sep = "r", **kwargs): dF = struct.unpack('d', t)[0] t = fp.read(8) zF = struct.unpack('d', t)[0] - + t = fp.read(4) tmax = struct.unpack('i', t)[0] t = fp.read(4) - bnd = struct.unpack('i', t)[0] - - - placesBI = ["gS", "gP", - "gA", "gV", - "gVt", "lA", + # bnd = struct.unpack('i', t)[0] + + placesBI = ["gS", "gP", + "gA", "gV", + "gVt", "lA", "lV", "lVt", "lT", "lTt"] placesBB = ["g1", "l1"] @@ -1078,47 +1079,54 @@ def read_ms5_xsf(path, prefix, qc, corr, sep = "r", **kwargs): # the chunks have the following structure: # confignumber, 10x timedependent complex correlators as doubles, 2x timeindependent complex correlators as doubles - chunksize = 4 +( 8 *2*tmax*10)+( 8 *2*2) - packstr ='=i'+('d'*2*tmax*10)+('d'*2*2) + chunksize = 4 + (8 * 2 * tmax * 10) + (8 * 2 * 2) + packstr = '=i' + ('d' * 2 * tmax * 10) + ('d' * 2 * 2) cnfgs.append([]) realsamples.append([]) imagsamples.append([]) for t in range(tmax): realsamples[repnum].append([]) imagsamples[repnum].append([]) - + while True: cnfgt = fp.read(chunksize) if not cnfgt: break - asascii=struct.unpack(packstr, cnfgt) + asascii = struct.unpack(packstr, cnfgt) cnfg = asascii[0] cnfgs[repnum].append(cnfg) - - if not corr in placesBB: - tmpcorr = asascii[1+2*tmax*placesBI.index(corr):1+2*tmax*placesBI.index(corr)+2*tmax] + + if corr not in placesBB: + tmpcorr = asascii[1 + 2 * tmax * placesBI.index(corr):1 + 2 * tmax * placesBI.index(corr) + 2 * tmax] else: - tmpcorr = asascii[1+2*tmax*len(placesBI)+2*placesBB.index(corr):1+2*tmax*len(placesBI)+2*placesBB.index(corr)+2] - corrres = [[],[]] - for i in range(len(tmpcorr)): corrres[i%2].append(tmpcorr[i]) - for t in range(int(len(tmpcorr)/2)): realsamples[repnum][t].append(corrres[0][t]) - for t in range(int(len(tmpcorr)/2)): imagsamples[repnum][t].append(corrres[1][t]) + tmpcorr = asascii[1 + 2 * tmax * len(placesBI) + 2 * placesBB.index(corr):1 + 2 * tmax * len(placesBI) + 2 * placesBB.index(corr) + 2] + + corrres = [[], []] + for i in range(len(tmpcorr)): + corrres[i % 2].append(tmpcorr[i]) + for t in range(int(len(tmpcorr) / 2)): + realsamples[repnum][t].append(corrres[0][t]) + for t in range(int(len(tmpcorr) / 2)): + imagsamples[repnum][t].append(corrres[1][t]) repnum += 1 - - s = "Read correlator "+ corr+ " from "+ str(repnum)+ " replika with "+str(len(realsamples[0][t])) - for rep in range(1,repnum): - s+=", "+str(len(realsamples[rep][t])) - s+=" samples" + + s = "Read correlator " + corr + " from " + str(repnum) + " replika with " + str(len(realsamples[0][t])) + for rep in range(1, repnum): + s += ", " + str(len(realsamples[rep][t])) + s += " samples" print(s) print("Asserted run parameters:\n T:", tmax, "kappa:", kappa, "csw:", csw, "dF:", dF, "zF:", zF) - + # we have the data now... but we need to re format the whole thing and put it into Corr objects. - + realObs = [] imagObs = [] compObs = [] - for t in range(int(len(tmpcorr)/2)): realObs.append(Obs([realsamples[rep][t] for rep in range(repnum)], names = names, idl = cnfgs)) - for t in range(int(len(tmpcorr)/2)): imagObs.append(Obs([imagsamples[rep][t] for rep in range(repnum)], names = names, idl = cnfgs)) - for t in range(int(len(tmpcorr)/2)): compObs.append(CObs(realObs[t], imagObs[t])) - return Corr(compObs) \ No newline at end of file + for t in range(int(len(tmpcorr) / 2)): + realObs.append(Obs([realsamples[rep][t] for rep in range(repnum)], names=names, idl=cnfgs)) + for t in range(int(len(tmpcorr) / 2)): + imagObs.append(Obs([imagsamples[rep][t] for rep in range(repnum)], names=names, idl=cnfgs)) + for t in range(int(len(tmpcorr) / 2)): + compObs.append(CObs(realObs[t], imagObs[t])) + return Corr(compObs) From aa8fa2c0c5a4651c142f59a1ced80b3e274b614f Mon Sep 17 00:00:00 2001 From: jkuhl-uni Date: Wed, 21 Dec 2022 18:20:13 +0100 Subject: [PATCH 17/17] last changes, last linting --- pyerrors/input/openQCD.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/pyerrors/input/openQCD.py b/pyerrors/input/openQCD.py index 7eb1f3ee..28e4eac9 100644 --- a/pyerrors/input/openQCD.py +++ b/pyerrors/input/openQCD.py @@ -12,7 +12,6 @@ from ..obs import CObs from ..correlators import Corr - def read_rwms(path, prefix, version='2.0', names=None, **kwargs): """Read rwms format from given folder structure. Returns a list of length nrw @@ -1014,7 +1013,11 @@ def read_ms5_xsf(path, prefix, qc, corr, sep="r", **kwargs): Returns ------- Corr - A complex valued `Corr` object containing the data read from the files. + A complex valued `Corr` object containing the data read from the files. In case of boudary to bulk correlators. + or + CObs + A complex valued `CObs` object containing the data read from the files. In case of boudary to boundary correlators. + Raises ------ @@ -1066,7 +1069,7 @@ def read_ms5_xsf(path, prefix, qc, corr, sep="r", **kwargs): t = fp.read(4) tmax = struct.unpack('i', t)[0] t = fp.read(4) - # bnd = struct.unpack('i', t)[0] + bnd = struct.unpack('i', t)[0] placesBI = ["gS", "gP", "gA", "gV", @@ -1075,7 +1078,6 @@ def read_ms5_xsf(path, prefix, qc, corr, sep="r", **kwargs): "lT", "lTt"] placesBB = ["g1", "l1"] - # bytes per config # the chunks have the following structure: # confignumber, 10x timedependent complex correlators as doubles, 2x timeindependent complex correlators as doubles @@ -1115,18 +1117,17 @@ def read_ms5_xsf(path, prefix, qc, corr, sep="r", **kwargs): s += ", " + str(len(realsamples[rep][t])) s += " samples" print(s) - print("Asserted run parameters:\n T:", tmax, "kappa:", kappa, "csw:", csw, "dF:", dF, "zF:", zF) + print("Asserted run parameters:\n T:", tmax, "kappa:", kappa, "csw:", csw, "dF:", dF, "zF:", zF, "bnd:", bnd) # we have the data now... but we need to re format the whole thing and put it into Corr objects. - realObs = [] - imagObs = [] compObs = [] for t in range(int(len(tmpcorr) / 2)): - realObs.append(Obs([realsamples[rep][t] for rep in range(repnum)], names=names, idl=cnfgs)) - for t in range(int(len(tmpcorr) / 2)): - imagObs.append(Obs([imagsamples[rep][t] for rep in range(repnum)], names=names, idl=cnfgs)) - for t in range(int(len(tmpcorr) / 2)): - compObs.append(CObs(realObs[t], imagObs[t])) - return Corr(compObs) + compObs.append(CObs(Obs([realsamples[rep][t] for rep in range(repnum)], names=names, idl=cnfgs), + Obs([imagsamples[rep][t] for rep in range(repnum)], names=names, idl=cnfgs))) + + if len(compObs) == 1: + return compObs[0] + else: + return Corr(compObs)