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