pyerrors.input.misc

  1import os
  2import fnmatch
  3import re
  4import struct
  5import numpy as np  # Thinly-wrapped numpy
  6from ..obs import Obs
  7
  8
  9def read_pbp(path, prefix, **kwargs):
 10    """Read pbp format from given folder structure.
 11
 12    Parameters
 13    ----------
 14    r_start : list
 15        list which contains the first config to be read for each replicum
 16    r_stop : list
 17        list which contains the last config to be read for each replicum
 18
 19    Returns
 20    -------
 21    result : list[Obs]
 22        list of observables read
 23    """
 24
 25    ls = []
 26    for (dirpath, dirnames, filenames) in os.walk(path):
 27        ls.extend(filenames)
 28        break
 29
 30    if not ls:
 31        raise Exception('Error, directory not found')
 32
 33    # Exclude files with different names
 34    for exc in ls:
 35        if not fnmatch.fnmatch(exc, prefix + '*.dat'):
 36            ls = list(set(ls) - set([exc]))
 37    if len(ls) > 1:
 38        ls.sort(key=lambda x: int(re.findall(r'\d+', x[len(prefix):])[0]))
 39    replica = len(ls)
 40
 41    if 'r_start' in kwargs:
 42        r_start = kwargs.get('r_start')
 43        if len(r_start) != replica:
 44            raise Exception('r_start does not match number of replicas')
 45        # Adjust Configuration numbering to python index
 46        r_start = [o - 1 if o else None for o in r_start]
 47    else:
 48        r_start = [None] * replica
 49
 50    if 'r_stop' in kwargs:
 51        r_stop = kwargs.get('r_stop')
 52        if len(r_stop) != replica:
 53            raise Exception('r_stop does not match number of replicas')
 54    else:
 55        r_stop = [None] * replica
 56
 57    print(r'Read <bar{psi}\psi> from', prefix[:-1], ',', replica, 'replica', end='')
 58
 59    print_err = 0
 60    if 'print_err' in kwargs:
 61        print_err = 1
 62        print()
 63
 64    deltas = []
 65
 66    for rep in range(replica):
 67        tmp_array = []
 68        with open(path + '/' + ls[rep], 'rb') as fp:
 69
 70            t = fp.read(4)  # number of reweighting factors
 71            if rep == 0:
 72                nrw = struct.unpack('i', t)[0]
 73                for k in range(nrw):
 74                    deltas.append([])
 75            else:
 76                if nrw != struct.unpack('i', t)[0]:
 77                    raise Exception('Error: different number of factors for replicum', rep)
 78
 79            for k in range(nrw):
 80                tmp_array.append([])
 81
 82            # This block is necessary for openQCD1.6 ms1 files
 83            nfct = []
 84            for i in range(nrw):
 85                t = fp.read(4)
 86                nfct.append(struct.unpack('i', t)[0])
 87            print('nfct: ', nfct)  # Hasenbusch factor, 1 for rat reweighting
 88
 89            nsrc = []
 90            for i in range(nrw):
 91                t = fp.read(4)
 92                nsrc.append(struct.unpack('i', t)[0])
 93
 94            # body
 95            while True:
 96                t = fp.read(4)
 97                if len(t) < 4:
 98                    break
 99                if print_err:
100                    config_no = struct.unpack('i', t)
101                for i in range(nrw):
102                    tmp_nfct = 1.0
103                    for j in range(nfct[i]):
104                        t = fp.read(8 * nsrc[i])
105                        t = fp.read(8 * nsrc[i])
106                        tmp_rw = struct.unpack('d' * nsrc[i], t)
107                        tmp_nfct *= np.mean(np.asarray(tmp_rw))
108                        if print_err:
109                            print(config_no, i, j, np.mean(np.asarray(tmp_rw)), np.std(np.asarray(tmp_rw)))
110                            print('Sources:', np.asarray(tmp_rw))
111                            print('Partial factor:', tmp_nfct)
112                    tmp_array[i].append(tmp_nfct)
113
114            for k in range(nrw):
115                deltas[k].append(tmp_array[k][r_start[rep]:r_stop[rep]])
116
117    rep_names = []
118    for entry in ls:
119        truncated_entry = entry.split('.')[0]
120        idx = truncated_entry.index('r')
121        rep_names.append(truncated_entry[:idx] + '|' + truncated_entry[idx:])
122    print(',', nrw, r'<bar{psi}\psi> with', nsrc, 'sources')
123    result = []
124    for t in range(nrw):
125        result.append(Obs(deltas[t], rep_names))
126
127    return result
def read_pbp(path, prefix, **kwargs):
 10def read_pbp(path, prefix, **kwargs):
 11    """Read pbp format from given folder structure.
 12
 13    Parameters
 14    ----------
 15    r_start : list
 16        list which contains the first config to be read for each replicum
 17    r_stop : list
 18        list which contains the last config to be read for each replicum
 19
 20    Returns
 21    -------
 22    result : list[Obs]
 23        list of observables read
 24    """
 25
 26    ls = []
 27    for (dirpath, dirnames, filenames) in os.walk(path):
 28        ls.extend(filenames)
 29        break
 30
 31    if not ls:
 32        raise Exception('Error, directory not found')
 33
 34    # Exclude files with different names
 35    for exc in ls:
 36        if not fnmatch.fnmatch(exc, prefix + '*.dat'):
 37            ls = list(set(ls) - set([exc]))
 38    if len(ls) > 1:
 39        ls.sort(key=lambda x: int(re.findall(r'\d+', x[len(prefix):])[0]))
 40    replica = len(ls)
 41
 42    if 'r_start' in kwargs:
 43        r_start = kwargs.get('r_start')
 44        if len(r_start) != replica:
 45            raise Exception('r_start does not match number of replicas')
 46        # Adjust Configuration numbering to python index
 47        r_start = [o - 1 if o else None for o in r_start]
 48    else:
 49        r_start = [None] * replica
 50
 51    if 'r_stop' in kwargs:
 52        r_stop = kwargs.get('r_stop')
 53        if len(r_stop) != replica:
 54            raise Exception('r_stop does not match number of replicas')
 55    else:
 56        r_stop = [None] * replica
 57
 58    print(r'Read <bar{psi}\psi> from', prefix[:-1], ',', replica, 'replica', end='')
 59
 60    print_err = 0
 61    if 'print_err' in kwargs:
 62        print_err = 1
 63        print()
 64
 65    deltas = []
 66
 67    for rep in range(replica):
 68        tmp_array = []
 69        with open(path + '/' + ls[rep], 'rb') as fp:
 70
 71            t = fp.read(4)  # number of reweighting factors
 72            if rep == 0:
 73                nrw = struct.unpack('i', t)[0]
 74                for k in range(nrw):
 75                    deltas.append([])
 76            else:
 77                if nrw != struct.unpack('i', t)[0]:
 78                    raise Exception('Error: different number of factors for replicum', rep)
 79
 80            for k in range(nrw):
 81                tmp_array.append([])
 82
 83            # This block is necessary for openQCD1.6 ms1 files
 84            nfct = []
 85            for i in range(nrw):
 86                t = fp.read(4)
 87                nfct.append(struct.unpack('i', t)[0])
 88            print('nfct: ', nfct)  # Hasenbusch factor, 1 for rat reweighting
 89
 90            nsrc = []
 91            for i in range(nrw):
 92                t = fp.read(4)
 93                nsrc.append(struct.unpack('i', t)[0])
 94
 95            # body
 96            while True:
 97                t = fp.read(4)
 98                if len(t) < 4:
 99                    break
100                if print_err:
101                    config_no = struct.unpack('i', t)
102                for i in range(nrw):
103                    tmp_nfct = 1.0
104                    for j in range(nfct[i]):
105                        t = fp.read(8 * nsrc[i])
106                        t = fp.read(8 * nsrc[i])
107                        tmp_rw = struct.unpack('d' * nsrc[i], t)
108                        tmp_nfct *= np.mean(np.asarray(tmp_rw))
109                        if print_err:
110                            print(config_no, i, j, np.mean(np.asarray(tmp_rw)), np.std(np.asarray(tmp_rw)))
111                            print('Sources:', np.asarray(tmp_rw))
112                            print('Partial factor:', tmp_nfct)
113                    tmp_array[i].append(tmp_nfct)
114
115            for k in range(nrw):
116                deltas[k].append(tmp_array[k][r_start[rep]:r_stop[rep]])
117
118    rep_names = []
119    for entry in ls:
120        truncated_entry = entry.split('.')[0]
121        idx = truncated_entry.index('r')
122        rep_names.append(truncated_entry[:idx] + '|' + truncated_entry[idx:])
123    print(',', nrw, r'<bar{psi}\psi> with', nsrc, 'sources')
124    result = []
125    for t in range(nrw):
126        result.append(Obs(deltas[t], rep_names))
127
128    return result

Read pbp format from given folder structure.

Parameters
  • r_start (list): list which contains the first config to be read for each replicum
  • r_stop (list): list which contains the last config to be read for each replicum
Returns
  • result (list[Obs]): list of observables read