def do_average_plot_2d(self): '''Plot the histogram for iteration self.n_iter''' idim0 = self.dimensions[0]['idim'] idim1 = self.dimensions[1]['idim'] n_iters = self.input_h5['n_iter'][...] iiter_start = np.searchsorted(n_iters, self.iter_start) iiter_stop = np.searchsorted(n_iters, self.iter_stop) binbounds_0 = self.input_h5['binbounds_{}'.format(idim0)][...] midpoints_0 = self.input_h5['midpoints_{}'.format(idim0)][...] binbounds_1 = self.input_h5['binbounds_{}'.format(idim1)][...] midpoints_1 = self.input_h5['midpoints_{}'.format(idim1)][...] for iiter in range(iiter_start, iiter_stop): iter_hist = sum_except_along(self.input_h5['histograms'][iiter], [idim0, idim1]) if iiter == iiter_start: hist = iter_hist else: hist += iter_hist normhistnd(hist, [binbounds_0, binbounds_1]) self._do_2d_output(hist, [idim0, idim1], [midpoints_0, midpoints_1], [binbounds_0, binbounds_1])
def _remote_bin_iter(iiter, n_iter, dsspec, wt_dsspec, initpoint, binbounds, ignore_out_of_range): iter_hist_shape = tuple(len(bounds) - 1 for bounds in binbounds) iter_hist = np.zeros(iter_hist_shape, dtype=np.float64) dset = dsspec.get_iter_data(n_iter) if dset is None: return iiter, n_iter, iter_hist else: npts = dset.shape[1] weights = wt_dsspec.get_iter_data(n_iter)[:, 0, 0] # dset = dset[:,initpoint:,:] for ipt in range(npts - initpoint): histnd(dset[:, ipt, :], binbounds, weights, out=iter_hist, binbound_check=False, ignore_out_of_range=ignore_out_of_range) del weights, dset # normalize histogram normhistnd(iter_hist, binbounds) return iiter, n_iter, iter_hist
def do_instant_plot_1d(self): '''Plot the histogram for iteration self.n_iter''' idim = self.dimensions[0]['idim'] n_iters = self.input_h5['n_iter'][...] iiter = np.searchsorted(n_iters, self.n_iter) binbounds = self.input_h5['binbounds_{}'.format(idim)][...] midpoints = self.input_h5['midpoints_{}'.format(idim)][...] hist = self.input_h5['histograms'][iiter] # Average over other dimensions hist = sum_except_along(hist, idim) normhistnd(hist, [binbounds]) self._do_1d_output(hist, idim, midpoints)
def do_average_plot_1d(self): '''Plot the average histogram for iterations self.iter_start to self.iter_stop''' idim = self.dimensions[0]['idim'] n_iters = self.input_h5['n_iter'][...] iiter_start = np.searchsorted(n_iters, self.iter_start) iiter_stop = np.searchsorted(n_iters, self.iter_stop) binbounds = self.input_h5['binbounds_{}'.format(idim)][...] midpoints = self.input_h5['midpoints_{}'.format(idim)][...] # hist = self.input_h5['histograms'][iiter_start:iiter_stop] for iiter in range(iiter_start, iiter_stop): iter_hist = sum_except_along(self.input_h5['histograms'][iiter], idim) if iiter == iiter_start: hist = iter_hist else: hist += iter_hist del iter_hist normhistnd(hist, [binbounds]) self._do_1d_output(hist, idim, midpoints)
def go(self): '''Plot the evolution of the histogram for iterations self.iter_start to self.iter_stop''' idim = self.dimensions[0]['idim'] n_iters = self.input_h5['n_iter'][...] iiter_start = np.searchsorted(n_iters, self.iter_start) iiter_stop = np.searchsorted(n_iters, self.iter_stop) binbounds = self.input_h5['binbounds_{}'.format(idim)][...] midpoints = self.input_h5['midpoints_{}'.format(idim)][...] hists_ds = self.input_h5['histograms'] itercount = self.iter_stop - self.iter_start # We always round down, so that we don't have a dangling partial block at the end nblocks = itercount // self.iter_step block_iters = np.empty((nblocks, 2), dtype=n_iters.dtype) blocked_hists = np.zeros((nblocks, hists_ds.shape[1 + idim]), dtype=hists_ds.dtype) for iblock, istart in enumerate(range(iiter_start, iiter_start + nblocks * self.iter_step, self.iter_step)): istop = min(istart + self.iter_step, iiter_stop) histslice = hists_ds[istart:istop] # Sum over time histslice = np.add.reduce(histslice, axis=0) # Sum over other dimensions blocked_hists[iblock] = sum_except_along(histslice, idim) # Normalize normhistnd(blocked_hists[iblock], [binbounds]) block_iters[iblock, 0] = n_iters[istart] block_iters[iblock, 1] = n_iters[istop - 1] + 1 # enehists = -np.log(blocked_hists) enehists = self._ener_zero(blocked_hists) log10hists = np.log10(blocked_hists) if self.hdf5_output_filename: with h5py.File(self.hdf5_output_filename, 'w') as output_h5: h5io.stamp_creator_data(output_h5) output_h5.attrs['source_data'] = os.path.abspath(self.input_h5.filename) output_h5.attrs['source_dimension'] = idim output_h5['midpoints'] = midpoints output_h5['histograms'] = blocked_hists output_h5['n_iter'] = block_iters if self.plot_output_filename: if self.plotscale == 'energy': plothist = enehists label = r'$\Delta F(x)\,/\,kT$' + '\n' + r'$\left[-\ln\,P(x)\right]$' elif self.plotscale == 'log10': plothist = log10hists label = r'$\log_{10}\ P(x)$' else: plothist = blocked_hists label = r'$P(x)$' try: vmin, vmax = self.plotrange except TypeError: vmin, vmax = None, None pyplot.figure() norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax) ax = pyplot.gca() nui = NonUniformImage( ax, extent=(midpoints[0], midpoints[-1], block_iters[0, -1], block_iters[-1, -1]), origin='lower', norm=norm ) # not sure why plothist works but plothist.T doesn't, and the opposite is true # for _do_2d_output nui.set_data(midpoints, block_iters[:, -1], plothist) ax.images.append(nui) ax.set_xlim(midpoints[0], midpoints[-1]) ax.set_ylim(block_iters[0, -1], block_iters[-1, -1]) cb = pyplot.colorbar(nui) cb.set_label(label) pyplot.xlabel(self.dimensions[0]['label']) pyplot.xlim(self.dimensions[0].get('lb'), self.dimensions[0].get('ub')) pyplot.ylabel('WE Iteration') if self.plottitle: pyplot.title(self.plottitle) if self.postprocess_function: self.postprocess_function(plothist, midpoints, binbounds) pyplot.savefig(self.plot_output_filename)