pyerrors.input.misc

  1import os
  2import fnmatch
  3import re
  4import struct
  5import numpy as np  # Thinly-wrapped numpy
  6import matplotlib.pyplot as plt
  7from matplotlib import gridspec
  8from ..obs import Obs
  9from ..fits import fit_lin
 10
 11
 12def fit_t0(t2E_dict, fit_range, plot_fit=False):
 13    zero_crossing = np.argmax(np.array(
 14        [o.value for o in t2E_dict.values()]) > 0.0)
 15
 16    x = list(t2E_dict.keys())[zero_crossing - fit_range:
 17                              zero_crossing + fit_range]
 18    y = list(t2E_dict.values())[zero_crossing - fit_range:
 19                                zero_crossing + fit_range]
 20    [o.gamma_method() for o in y]
 21
 22    fit_result = fit_lin(x, y)
 23
 24    if plot_fit is True:
 25        plt.figure()
 26        gs = gridspec.GridSpec(2, 1, height_ratios=[3, 1], wspace=0.0, hspace=0.0)
 27        ax0 = plt.subplot(gs[0])
 28        xmore = list(t2E_dict.keys())[zero_crossing - fit_range - 2: zero_crossing + fit_range + 2]
 29        ymore = list(t2E_dict.values())[zero_crossing - fit_range - 2: zero_crossing + fit_range + 2]
 30        [o.gamma_method() for o in ymore]
 31        ax0.errorbar(xmore, [yi.value for yi in ymore], yerr=[yi.dvalue for yi in ymore], fmt='x')
 32        xplot = np.linspace(np.min(x), np.max(x))
 33        yplot = [fit_result[0] + fit_result[1] * xi for xi in xplot]
 34        [yi.gamma_method() for yi in yplot]
 35        ax0.fill_between(xplot, y1=[yi.value - yi.dvalue for yi in yplot], y2=[yi.value + yi.dvalue for yi in yplot])
 36        retval = (-fit_result[0] / fit_result[1])
 37        retval.gamma_method()
 38        ylim = ax0.get_ylim()
 39        ax0.fill_betweenx(ylim, x1=retval.value - retval.dvalue, x2=retval.value + retval.dvalue, color='gray', alpha=0.4)
 40        ax0.set_ylim(ylim)
 41        ax0.set_ylabel(r'$t^2 \langle E(t) \rangle - 0.3 $')
 42        xlim = ax0.get_xlim()
 43
 44        fit_res = [fit_result[0] + fit_result[1] * xi for xi in x]
 45        residuals = (np.asarray([o.value for o in y]) - [o.value for o in fit_res]) / np.asarray([o.dvalue for o in y])
 46        ax1 = plt.subplot(gs[1])
 47        ax1.plot(x, residuals, 'ko', ls='none', markersize=5)
 48        ax1.tick_params(direction='out')
 49        ax1.tick_params(axis="x", bottom=True, top=True, labelbottom=True)
 50        ax1.axhline(y=0.0, ls='--', color='k')
 51        ax1.fill_between(xlim, -1.0, 1.0, alpha=0.1, facecolor='k')
 52        ax1.set_xlim(xlim)
 53        ax1.set_ylabel('Residuals')
 54        ax1.set_xlabel(r'$t/a^2$')
 55
 56        plt.draw()
 57    return -fit_result[0] / fit_result[1]
 58
 59
 60def read_pbp(path, prefix, **kwargs):
 61    """Read pbp format from given folder structure.
 62
 63    Parameters
 64    ----------
 65    r_start : list
 66        list which contains the first config to be read for each replicum
 67    r_stop : list
 68        list which contains the last config to be read for each replicum
 69
 70    Returns
 71    -------
 72    result : list[Obs]
 73        list of observables read
 74    """
 75
 76    ls = []
 77    for (dirpath, dirnames, filenames) in os.walk(path):
 78        ls.extend(filenames)
 79        break
 80
 81    if not ls:
 82        raise Exception('Error, directory not found')
 83
 84    # Exclude files with different names
 85    for exc in ls:
 86        if not fnmatch.fnmatch(exc, prefix + '*.dat'):
 87            ls = list(set(ls) - set([exc]))
 88    if len(ls) > 1:
 89        ls.sort(key=lambda x: int(re.findall(r'\d+', x[len(prefix):])[0]))
 90    replica = len(ls)
 91
 92    if 'r_start' in kwargs:
 93        r_start = kwargs.get('r_start')
 94        if len(r_start) != replica:
 95            raise Exception('r_start does not match number of replicas')
 96        # Adjust Configuration numbering to python index
 97        r_start = [o - 1 if o else None for o in r_start]
 98    else:
 99        r_start = [None] * replica
100
101    if 'r_stop' in kwargs:
102        r_stop = kwargs.get('r_stop')
103        if len(r_stop) != replica:
104            raise Exception('r_stop does not match number of replicas')
105    else:
106        r_stop = [None] * replica
107
108    print(r'Read <bar{psi}\psi> from', prefix[:-1], ',', replica, 'replica', end='')
109
110    print_err = 0
111    if 'print_err' in kwargs:
112        print_err = 1
113        print()
114
115    deltas = []
116
117    for rep in range(replica):
118        tmp_array = []
119        with open(path + '/' + ls[rep], 'rb') as fp:
120
121            t = fp.read(4)  # number of reweighting factors
122            if rep == 0:
123                nrw = struct.unpack('i', t)[0]
124                for k in range(nrw):
125                    deltas.append([])
126            else:
127                if nrw != struct.unpack('i', t)[0]:
128                    raise Exception('Error: different number of factors for replicum', rep)
129
130            for k in range(nrw):
131                tmp_array.append([])
132
133            # This block is necessary for openQCD1.6 ms1 files
134            nfct = []
135            for i in range(nrw):
136                t = fp.read(4)
137                nfct.append(struct.unpack('i', t)[0])
138            print('nfct: ', nfct)  # Hasenbusch factor, 1 for rat reweighting
139
140            nsrc = []
141            for i in range(nrw):
142                t = fp.read(4)
143                nsrc.append(struct.unpack('i', t)[0])
144
145            # body
146            while True:
147                t = fp.read(4)
148                if len(t) < 4:
149                    break
150                if print_err:
151                    config_no = struct.unpack('i', t)
152                for i in range(nrw):
153                    tmp_nfct = 1.0
154                    for j in range(nfct[i]):
155                        t = fp.read(8 * nsrc[i])
156                        t = fp.read(8 * nsrc[i])
157                        tmp_rw = struct.unpack('d' * nsrc[i], t)
158                        tmp_nfct *= np.mean(np.asarray(tmp_rw))
159                        if print_err:
160                            print(config_no, i, j, np.mean(np.asarray(tmp_rw)), np.std(np.asarray(tmp_rw)))
161                            print('Sources:', np.asarray(tmp_rw))
162                            print('Partial factor:', tmp_nfct)
163                    tmp_array[i].append(tmp_nfct)
164
165            for k in range(nrw):
166                deltas[k].append(tmp_array[k][r_start[rep]:r_stop[rep]])
167
168    rep_names = []
169    for entry in ls:
170        truncated_entry = entry.split('.')[0]
171        idx = truncated_entry.index('r')
172        rep_names.append(truncated_entry[:idx] + '|' + truncated_entry[idx:])
173    print(',', nrw, r'<bar{psi}\psi> with', nsrc, 'sources')
174    result = []
175    for t in range(nrw):
176        result.append(Obs(deltas[t], rep_names))
177
178    return result
def fit_t0(t2E_dict, fit_range, plot_fit=False):
13def fit_t0(t2E_dict, fit_range, plot_fit=False):
14    zero_crossing = np.argmax(np.array(
15        [o.value for o in t2E_dict.values()]) > 0.0)
16
17    x = list(t2E_dict.keys())[zero_crossing - fit_range:
18                              zero_crossing + fit_range]
19    y = list(t2E_dict.values())[zero_crossing - fit_range:
20                                zero_crossing + fit_range]
21    [o.gamma_method() for o in y]
22
23    fit_result = fit_lin(x, y)
24
25    if plot_fit is True:
26        plt.figure()
27        gs = gridspec.GridSpec(2, 1, height_ratios=[3, 1], wspace=0.0, hspace=0.0)
28        ax0 = plt.subplot(gs[0])
29        xmore = list(t2E_dict.keys())[zero_crossing - fit_range - 2: zero_crossing + fit_range + 2]
30        ymore = list(t2E_dict.values())[zero_crossing - fit_range - 2: zero_crossing + fit_range + 2]
31        [o.gamma_method() for o in ymore]
32        ax0.errorbar(xmore, [yi.value for yi in ymore], yerr=[yi.dvalue for yi in ymore], fmt='x')
33        xplot = np.linspace(np.min(x), np.max(x))
34        yplot = [fit_result[0] + fit_result[1] * xi for xi in xplot]
35        [yi.gamma_method() for yi in yplot]
36        ax0.fill_between(xplot, y1=[yi.value - yi.dvalue for yi in yplot], y2=[yi.value + yi.dvalue for yi in yplot])
37        retval = (-fit_result[0] / fit_result[1])
38        retval.gamma_method()
39        ylim = ax0.get_ylim()
40        ax0.fill_betweenx(ylim, x1=retval.value - retval.dvalue, x2=retval.value + retval.dvalue, color='gray', alpha=0.4)
41        ax0.set_ylim(ylim)
42        ax0.set_ylabel(r'$t^2 \langle E(t) \rangle - 0.3 $')
43        xlim = ax0.get_xlim()
44
45        fit_res = [fit_result[0] + fit_result[1] * xi for xi in x]
46        residuals = (np.asarray([o.value for o in y]) - [o.value for o in fit_res]) / np.asarray([o.dvalue for o in y])
47        ax1 = plt.subplot(gs[1])
48        ax1.plot(x, residuals, 'ko', ls='none', markersize=5)
49        ax1.tick_params(direction='out')
50        ax1.tick_params(axis="x", bottom=True, top=True, labelbottom=True)
51        ax1.axhline(y=0.0, ls='--', color='k')
52        ax1.fill_between(xlim, -1.0, 1.0, alpha=0.1, facecolor='k')
53        ax1.set_xlim(xlim)
54        ax1.set_ylabel('Residuals')
55        ax1.set_xlabel(r'$t/a^2$')
56
57        plt.draw()
58    return -fit_result[0] / fit_result[1]
def read_pbp(path, prefix, **kwargs):
 61def read_pbp(path, prefix, **kwargs):
 62    """Read pbp format from given folder structure.
 63
 64    Parameters
 65    ----------
 66    r_start : list
 67        list which contains the first config to be read for each replicum
 68    r_stop : list
 69        list which contains the last config to be read for each replicum
 70
 71    Returns
 72    -------
 73    result : list[Obs]
 74        list of observables read
 75    """
 76
 77    ls = []
 78    for (dirpath, dirnames, filenames) in os.walk(path):
 79        ls.extend(filenames)
 80        break
 81
 82    if not ls:
 83        raise Exception('Error, directory not found')
 84
 85    # Exclude files with different names
 86    for exc in ls:
 87        if not fnmatch.fnmatch(exc, prefix + '*.dat'):
 88            ls = list(set(ls) - set([exc]))
 89    if len(ls) > 1:
 90        ls.sort(key=lambda x: int(re.findall(r'\d+', x[len(prefix):])[0]))
 91    replica = len(ls)
 92
 93    if 'r_start' in kwargs:
 94        r_start = kwargs.get('r_start')
 95        if len(r_start) != replica:
 96            raise Exception('r_start does not match number of replicas')
 97        # Adjust Configuration numbering to python index
 98        r_start = [o - 1 if o else None for o in r_start]
 99    else:
100        r_start = [None] * replica
101
102    if 'r_stop' in kwargs:
103        r_stop = kwargs.get('r_stop')
104        if len(r_stop) != replica:
105            raise Exception('r_stop does not match number of replicas')
106    else:
107        r_stop = [None] * replica
108
109    print(r'Read <bar{psi}\psi> from', prefix[:-1], ',', replica, 'replica', end='')
110
111    print_err = 0
112    if 'print_err' in kwargs:
113        print_err = 1
114        print()
115
116    deltas = []
117
118    for rep in range(replica):
119        tmp_array = []
120        with open(path + '/' + ls[rep], 'rb') as fp:
121
122            t = fp.read(4)  # number of reweighting factors
123            if rep == 0:
124                nrw = struct.unpack('i', t)[0]
125                for k in range(nrw):
126                    deltas.append([])
127            else:
128                if nrw != struct.unpack('i', t)[0]:
129                    raise Exception('Error: different number of factors for replicum', rep)
130
131            for k in range(nrw):
132                tmp_array.append([])
133
134            # This block is necessary for openQCD1.6 ms1 files
135            nfct = []
136            for i in range(nrw):
137                t = fp.read(4)
138                nfct.append(struct.unpack('i', t)[0])
139            print('nfct: ', nfct)  # Hasenbusch factor, 1 for rat reweighting
140
141            nsrc = []
142            for i in range(nrw):
143                t = fp.read(4)
144                nsrc.append(struct.unpack('i', t)[0])
145
146            # body
147            while True:
148                t = fp.read(4)
149                if len(t) < 4:
150                    break
151                if print_err:
152                    config_no = struct.unpack('i', t)
153                for i in range(nrw):
154                    tmp_nfct = 1.0
155                    for j in range(nfct[i]):
156                        t = fp.read(8 * nsrc[i])
157                        t = fp.read(8 * nsrc[i])
158                        tmp_rw = struct.unpack('d' * nsrc[i], t)
159                        tmp_nfct *= np.mean(np.asarray(tmp_rw))
160                        if print_err:
161                            print(config_no, i, j, np.mean(np.asarray(tmp_rw)), np.std(np.asarray(tmp_rw)))
162                            print('Sources:', np.asarray(tmp_rw))
163                            print('Partial factor:', tmp_nfct)
164                    tmp_array[i].append(tmp_nfct)
165
166            for k in range(nrw):
167                deltas[k].append(tmp_array[k][r_start[rep]:r_stop[rep]])
168
169    rep_names = []
170    for entry in ls:
171        truncated_entry = entry.split('.')[0]
172        idx = truncated_entry.index('r')
173        rep_names.append(truncated_entry[:idx] + '|' + truncated_entry[idx:])
174    print(',', nrw, r'<bar{psi}\psi> with', nsrc, 'sources')
175    result = []
176    for t in range(nrw):
177        result.append(Obs(deltas[t], rep_names))
178
179    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