pyerrors.input.misc

View Source
  0import os
  1import fnmatch
  2import re
  3import struct
  4import numpy as np  # Thinly-wrapped numpy
  5from ..obs import Obs
  6
  7
  8def read_pbp(path, prefix, **kwargs):
  9    """Read pbp format from given folder structure. Returns a list of length nrw
 10
 11    Parameters
 12    ----------
 13    r_start : list
 14        list which contains the first config to be read for each replicum
 15    r_stop : list
 16        list which contains the last config to be read for each replicum
 17    """
 18
 19    extract_nfct = 1
 20
 21    ls = []
 22    for (dirpath, dirnames, filenames) in os.walk(path):
 23        ls.extend(filenames)
 24        break
 25
 26    if not ls:
 27        raise Exception('Error, directory not found')
 28
 29    # Exclude files with different names
 30    for exc in ls:
 31        if not fnmatch.fnmatch(exc, prefix + '*.dat'):
 32            ls = list(set(ls) - set([exc]))
 33    if len(ls) > 1:
 34        ls.sort(key=lambda x: int(re.findall(r'\d+', x[len(prefix):])[0]))
 35    replica = len(ls)
 36
 37    if 'r_start' in kwargs:
 38        r_start = kwargs.get('r_start')
 39        if len(r_start) != replica:
 40            raise Exception('r_start does not match number of replicas')
 41        # Adjust Configuration numbering to python index
 42        r_start = [o - 1 if o else None for o in r_start]
 43    else:
 44        r_start = [None] * replica
 45
 46    if 'r_stop' in kwargs:
 47        r_stop = kwargs.get('r_stop')
 48        if len(r_stop) != replica:
 49            raise Exception('r_stop does not match number of replicas')
 50    else:
 51        r_stop = [None] * replica
 52
 53    print(r'Read <bar{psi}\psi> from', prefix[:-1], ',', replica, 'replica', end='')
 54
 55    print_err = 0
 56    if 'print_err' in kwargs:
 57        print_err = 1
 58        print()
 59
 60    deltas = []
 61
 62    for rep in range(replica):
 63        tmp_array = []
 64        with open(path + '/' + ls[rep], 'rb') as fp:
 65
 66            t = fp.read(4)  # number of reweighting factors
 67            if rep == 0:
 68                nrw = struct.unpack('i', t)[0]
 69                for k in range(nrw):
 70                    deltas.append([])
 71            else:
 72                if nrw != struct.unpack('i', t)[0]:
 73                    raise Exception('Error: different number of factors for replicum', rep)
 74
 75            for k in range(nrw):
 76                tmp_array.append([])
 77
 78            # This block is necessary for openQCD1.6 ms1 files
 79            nfct = []
 80            if extract_nfct == 1:
 81                for i in range(nrw):
 82                    t = fp.read(4)
 83                    nfct.append(struct.unpack('i', t)[0])
 84                print('nfct: ', nfct)  # Hasenbusch factor, 1 for rat reweighting
 85            else:
 86                for i in range(nrw):
 87                    nfct.append(1)
 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 0 < 1:
 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):
View Source
  9def read_pbp(path, prefix, **kwargs):
 10    """Read pbp format from given folder structure. Returns a list of length nrw
 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
 20    extract_nfct = 1
 21
 22    ls = []
 23    for (dirpath, dirnames, filenames) in os.walk(path):
 24        ls.extend(filenames)
 25        break
 26
 27    if not ls:
 28        raise Exception('Error, directory not found')
 29
 30    # Exclude files with different names
 31    for exc in ls:
 32        if not fnmatch.fnmatch(exc, prefix + '*.dat'):
 33            ls = list(set(ls) - set([exc]))
 34    if len(ls) > 1:
 35        ls.sort(key=lambda x: int(re.findall(r'\d+', x[len(prefix):])[0]))
 36    replica = len(ls)
 37
 38    if 'r_start' in kwargs:
 39        r_start = kwargs.get('r_start')
 40        if len(r_start) != replica:
 41            raise Exception('r_start does not match number of replicas')
 42        # Adjust Configuration numbering to python index
 43        r_start = [o - 1 if o else None for o in r_start]
 44    else:
 45        r_start = [None] * replica
 46
 47    if 'r_stop' in kwargs:
 48        r_stop = kwargs.get('r_stop')
 49        if len(r_stop) != replica:
 50            raise Exception('r_stop does not match number of replicas')
 51    else:
 52        r_stop = [None] * replica
 53
 54    print(r'Read <bar{psi}\psi> from', prefix[:-1], ',', replica, 'replica', end='')
 55
 56    print_err = 0
 57    if 'print_err' in kwargs:
 58        print_err = 1
 59        print()
 60
 61    deltas = []
 62
 63    for rep in range(replica):
 64        tmp_array = []
 65        with open(path + '/' + ls[rep], 'rb') as fp:
 66
 67            t = fp.read(4)  # number of reweighting factors
 68            if rep == 0:
 69                nrw = struct.unpack('i', t)[0]
 70                for k in range(nrw):
 71                    deltas.append([])
 72            else:
 73                if nrw != struct.unpack('i', t)[0]:
 74                    raise Exception('Error: different number of factors for replicum', rep)
 75
 76            for k in range(nrw):
 77                tmp_array.append([])
 78
 79            # This block is necessary for openQCD1.6 ms1 files
 80            nfct = []
 81            if extract_nfct == 1:
 82                for i in range(nrw):
 83                    t = fp.read(4)
 84                    nfct.append(struct.unpack('i', t)[0])
 85                print('nfct: ', nfct)  # Hasenbusch factor, 1 for rat reweighting
 86            else:
 87                for i in range(nrw):
 88                    nfct.append(1)
 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 0 < 1:
 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. Returns a list of length nrw

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