|
| 1 | +import numpy as np |
| 2 | +from utils.utils import * |
| 3 | + |
| 4 | +class CountBonds(object): |
| 5 | + """Class to obtain statistics about the average number of bonds <Nb> and |
| 6 | + the variance in the average number of bonds, <Nb^2> - <Nb>^2 |
| 7 | +
|
| 8 | + Args: |
| 9 | + image_set (array-like): |
| 10 | + Array of images for which <Nb> and <Delta_{N_b}^2> are calculated |
| 11 | + and averaged over. |
| 12 | + num_blocks (int, default=20): |
| 13 | + Number of blocks to be used for block resampling for bootstrap |
| 14 | + error analysis. |
| 15 | + save (bool, default=False): |
| 16 | + Whether or not to save the resulting bond_statistics data. |
| 17 | + verbose (bool, default=False): |
| 18 | + Whether or not to display information as the analysis is being |
| 19 | + performed. |
| 20 | + """ |
| 21 | + def __init__(self, image_set, num_blocks=20, save=False, |
| 22 | + verbose=False): |
| 23 | + # if len(image_set.shape) == 1: |
| 24 | + # self.reshape_image_set() |
| 25 | + self._image_set = image_set |
| 26 | + self._num_images, self._Lx, self._Ly = self._image_set.shape |
| 27 | + #self._block_val = block_val |
| 28 | + self._num_blocks = num_blocks |
| 29 | + self._verbose = verbose |
| 30 | + # self.bond_stats = None |
| 31 | + # if block_val is None: |
| 32 | + # self._wx = 2 * self._Lx |
| 33 | + # self._wy = 2 * self._Ly |
| 34 | + |
| 35 | + @staticmethod |
| 36 | + def reshape_image_set(image_set): |
| 37 | + """Method for reshaping images into a 2D array of pixels, if initally |
| 38 | + provided as flattened array.""" |
| 39 | + pass |
| 40 | + |
| 41 | + def _count_bonds(self, image): |
| 42 | + """ Count bonds on single image, and return Nb. """ |
| 43 | + if image.shape != (self._Lx, self._Ly): |
| 44 | + try: |
| 45 | + image = image.reshape(self._Lx, self._Ly) |
| 46 | + except ValueError: |
| 47 | + raise "Unable to properly reshape image." |
| 48 | + bond_idxs = [(i, j) for i in range(self._Lx) for j in range(self._Ly) |
| 49 | + if (i + j) % 2 == 1] |
| 50 | + Nb = np.sum([image[i] for i in bond_idxs]) |
| 51 | + return Nb |
| 52 | + |
| 53 | + def _calc_averages(self, data_set=None): |
| 54 | + """Calculate <Nb> and <Delta_{Nb}^2> by running _count_bonds method on |
| 55 | + each image in self._image_set.""" |
| 56 | + if data_set is None: |
| 57 | + data_set = self._image_set |
| 58 | + Nb_arr = np.array([self._count_bonds(image) |
| 59 | + for image in data_set]) |
| 60 | + Nb2_arr = Nb_arr ** 2 |
| 61 | + Nb_avg = np.mean(Nb_arr) |
| 62 | + Nb2_avg = np.mean(Nb2_arr) |
| 63 | + Nb_avg2 = Nb_avg ** 2 |
| 64 | + delta_Nb2 = Nb2_avg - Nb_avg2 |
| 65 | + return Nb_avg, delta_Nb2 |
| 66 | + |
| 67 | + def _count_bonds_with_err(self): |
| 68 | + """Calculate the average number of active bonds (Nb) for the boundary |
| 69 | + images using the previously defined methods.""" |
| 70 | + bond_stats = self._calc_averages() |
| 71 | + data_rs = block_resampling(self._image_set, self._num_blocks) |
| 72 | + bond_stats_rs = [] |
| 73 | + err = [] |
| 74 | + |
| 75 | + for block in data_rs: |
| 76 | + bond_stats_rs.append(self._calc_averages(block)) |
| 77 | + bond_stats_rs = np.array(bond_stats_rs) |
| 78 | + for idx in range(len(bond_stats)): |
| 79 | + _err = jackknife_err(y_i=bond_stats_rs[:, idx], |
| 80 | + y_full = bond_stats[idx], |
| 81 | + num_blocks=self._num_blocks) |
| 82 | + err.append(_err) |
| 83 | + return bond_stats, err |
| 84 | + |
| 85 | + def count_bonds(self): |
| 86 | + """Calculate bond statistics for entirety of self._image set data, |
| 87 | + including error analysis.""" |
| 88 | + val, err = self._count_bonds_with_err() |
| 89 | + bond_stats = np.array([val[0], err[0], val[1], err[1]]) |
| 90 | + #bond_stats.append(np.array([val[0], err[0], val[1], err[1]])) |
| 91 | + return bond_stats |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +# def count_bonds(image_set): |
| 102 | +# """Method for counting 'bonds' in a set of worm-type images.""" |
| 103 | +# w = image_set[0].shape[0] |
| 104 | +# bond_idxs = [(i, j) for i in range(w) |
| 105 | +# for j in range(w) |
| 106 | +# if (i + j) % 2 == 1] |
| 107 | +# bc_arr = np.array([np.sum([image[i] for i in bond_idxs]) |
| 108 | +# for image in image_set]) |
| 109 | +# bc2_arr = bc_arr ** 2 |
| 110 | +# Nb_avg = np.mean(bc_arr) |
| 111 | +# Nb2_avg = np.mean(bc2_arr) |
| 112 | +# Nb_avg2 = Nb_avg ** 2 |
| 113 | +# delta_Nb = Nb2_avg - Nb_avg2 |
| 114 | +# return Nb_avg, delta_Nb |
0 commit comments