def run(opts): for label, distr in zip( labels, distrs ): data = N.zeros( shape=opts.n, dtype=[ ('x', 'd'), ('y', 'd'), ('z', 'd') ] ) for i in xrange( opts.n ): data[i] = tuple(distr.toymc.data()) distr.nextSample() plot( label, data, opts ) fig = P.figure() ax = P.subplot( 111 ) ax.minorticks_on() ax.set_xlabel( 'x, y, z' ) ax.set_ylabel( 'x, y, z' ) ax.set_title( 'Covmat' ) c=ax.matshow( cov ) add_colorbar( c ) savefig( opts.output, suffix='_cov' ) P.close() fig = P.figure() ax = P.subplot( 111 ) ax.minorticks_on() ax.set_xlabel( 'x, y, z' ) ax.set_ylabel( 'x, y, z' ) ax.set_title( 'Cormat' ) c = ax.matshow( cor ) add_colorbar( c ) savefig( opts.output, suffix='_cor' ) P.close()
def matshow(self, mat, title, suffix): fig = self.figure() ax = plt.subplot( 111 ) c = plt.matshow(mat, fignum=False) add_colorbar(c) ax = plt.gca() ax.set_title(title) self.savefig(suffix, self.info.index)
def imshow_hist2(h, *args, **kwargs): """Plot 2-dimensinal histogram using pyplot.imshow executes pyplot.imshow(x, y, C, *args, **kwargs) with first two arguments overridden all other arguments passes as is. Options: mask=F - exclude value F from plotting (set mask=0 to avoid plotting 0.0) colorbar - if true, plot colorbar with height aligned to the axes height returns pyplot.imshow()[, pyplot.colorbar] result """ mask = kwargs.pop('mask', None) colorbar = kwargs.pop('colorbar', None) xax = h.GetXaxis() yax = h.GetYaxis() if xax.GetXbins().GetSize() > 0 or yax.GetXbins().GetSize() > 0: print('Can not draw 2D a histogram with variable bin widths') print('Use pcolormesh method or draweHist2Dmesh function instead') return extent = [xax.GetXmin(), xax.GetXmax(), yax.GetXmin(), yax.GetXmax()] buf = R2N.get_buffer_hist2(h, mask=mask).copy() kwargs.setdefault('origin', 'lower') res = P.imshow(buf, *args, extent=extent, **kwargs) if colorbar: cbar = helpers.add_colorbar(res) return res, cbar return res
def pcolormesh_hist2(h, *args, **kwargs): """Plot 2-dimensinal histogram using pyplot.pcolormesh executes pyplot.pcolormesh(x, y, C, *args, **kwargs) with first two arguments overridden all other arguments passes as is. Options: mask=F - exclude value F from plotting (set mask=0 to avoid plotting 0.0) colorbar - if true, plot colorbar with height aligned to the axes height returns pyplot.pcolormesh()[, pyplot.colorbar] result """ mask = kwargs.pop('mask', None) colorbar = kwargs.pop('colorbar', None) x1 = R2N.get_bin_edges_axis(h.GetXaxis()) y1 = R2N.get_bin_edges_axis(h.GetYaxis()) x, y = N.meshgrid(x1, y1) buf = R2N.get_buffer_hist2(h, mask=mask).copy() res = P.pcolormesh(x, y, buf, *args, **kwargs) if colorbar: cbar = helpers.add_colorbar(res) return res, cbar return res
def pcolorfast_hist2(h, *args, **kwargs): """Plot 2-dimensinal histogram using ax.pcolorfast executes ax.pcolorfast(x, y, C, *args, **kwargs) with first two arguments overridden all other arguments passes as is. Options: mask=F - exclude value F from plotting (set mask=0 to avoid plotting 0.0) colorbar - if true, plot colorbar with height aligned to the axes height returns ax.pcolorfast()[, pyplot.colorbar] result """ mask = kwargs.pop('mask', None) colorbar = kwargs.pop('colorbar', None) xax = h.GetXaxis() yax = h.GetYaxis() if xax.GetXbins().GetSize() > 0 or yax.GetXbins().GetSize() > 0: print('Can not draw 2D a histogram with variable bin widths') print('Use pcolormesh method instead') return x = [xax.GetXmin(), xax.GetXmax()] y = [yax.GetXmin(), yax.GetXmax()] buf = R2N.get_buffer_hist2(h, mask=mask).copy() ax = P.gca() res = ax.pcolorfast(x, y, buf, *args, **kwargs) if colorbar: cbar = helpers.add_colorbar(res) return res, cbar return res
def colorbar_or_not(res, cbaropt): if not cbaropt: return res if not isinstance(cbaropt, dict): cbaropt = {} cbar = helpers.add_colorbar(res, **cbaropt) return res, cbar
def plot_matrix(mat): fig = plt.figure() ax = plt.subplot( 111 ) ax.minorticks_on() ax.set_xlabel( 'Source bins' ) ax.set_ylabel( 'Target bins' ) ax.set_title( 'Bin edges conversion matrix' ) ax.set_aspect('equal') ax.set_xlim(orig[0], orig[-1]) ax.set_ylim(orig[-1], orig[0]) mmat = np.ma.array(mat, mask=mat==0.0) c = ax.matshow(mmat, extent=[orig[0], orig[-1], orig[-1], orig[0]]) add_colorbar(c) savefig(opts.output, suffix='matrix_0') ax.plot(xfine, fcn_direct(xfine), '--', color='white') savefig(opts.output, suffix='matrix_1')
def imshow_matrix(self, *args, **kwargs): """Plot TMatrixT using pyplot.imshow""" mask = kwargs.pop('mask', None) colorbar = kwargs.pop('colorbar', None) buf = R2N.get_buffer_matrix(self, mask=mask) res = P.imshow(buf) if colorbar: cbar = helpers.add_colorbar(res) return res, cbar return res
def matshow_matrix(self, *args, **kwargs): """Plot TMatrixT using pyplot.matshow""" mask = kwargs.pop('mask', None) colorbar = kwargs.pop('colorbar', None) kwargs.setdefault('fignum', False) buf = R2N.get_buffer_matrix(self, mask=mask) res = P.matshow(buf, **kwargs) if colorbar: cbar = helpers.add_colorbar(res) return res, cbar return res
def plot_matrix(mat, title, suffix): fig = plt.figure() ax = plt.subplot( 111 ) ax.minorticks_on() ax.set_xlabel( 'Source bins' ) ax.set_ylabel( 'Target bins' ) ax.set_title( 'Bin edges conversion matrix: {}'.format(title) ) mmat = np.ma.array(mat, mask=mat==0.0) c = ax.matshow(mmat, extent=[edges[0], edges[-1], edges[-1], edges[0]]) add_colorbar(c) savefig(opts.output, suffix='matrix_'+suffix) fig = plt.figure() ax = plt.subplot(111, xlabel='Edges', ylabel='', title='Check sum: {}'.format(title)) ax.minorticks_on() rsum = mat.sum(axis=0) ax.plot(centers, rsum, 'o') ax.axhline(1.0) savefig(opts.output, suffix='norm_'+suffix)
def plot_matrix(mat, curve_x=None, curve_y=None, reshapen=None): nbins = mat.shape[0] title = 'Bin edges conversion matrix {}x{}'.format(nbins, nbins) suffix = 'matrix_{}'.format(nbins) if reshapen: nbins_orig, nbins = nbins, nbins // reshapen title = 'Bin edges conversion matrix {}x{} from {}x{}'.format( nbins, nbins, nbins_orig, nbins_orig) suffix = 'matrix_{}_{}'.format(nbins, nbins_orig) mat = reduce_matrix(mat, reshapen) fig = plt.figure() ax = plt.subplot(111) ax.minorticks_on() ax.set_xlabel('Source bins') ax.set_ylabel('Target bins') ax.set_title(title) ax.set_aspect('equal') ax.set_xlim(orig[0], orig[-1]) ax.set_ylim(orig[-1], orig[0]) mmat = np.ma.array(mat, mask=mat == 0.0) c = ax.matshow(mmat, extent=[orig[0], orig[-1], orig[-1], orig[0]]) add_colorbar(c) savefig(opts.output, suffix=suffix) if curve_x is None or curve_y is None: return ax.plot(curve_x, curve_y, '--', color='white') savefig(opts.output, suffix='{}_c'.format(suffix)) return mat
def plot_boxes(dataall=None, title=None, scale=False, low=None, high=None, output=None, suffix=()): if dataall is None: assert low is not None assert high is not None data = None else: low, high, data = dataall['scan'] split = dataall['split'] xlabel = 'E low' ylabel = 'E high' eminimal = low[0] emaximal = high[-1] if scale: fwd_x, inv_x = MakeEqualScale(low) fwd_y, inv_y = MakeEqualScale(high) # Prepare data L, H = np.meshgrid(low, high, indexing='ij') W = np.around(H[1:, 1:] - L[:-1, :-1], 6) Center = np.around(H[1:, 1:] + L[:-1, :-1], 6) * 0.5 emin, emax, ew = 1.5, 4.0, 0.5 gridopts = dict(color='red', alpha=0.4, linestyle='dashed') # # Make figure # fig = plt.figure(figsize=(8, 6)) ax = plt.subplot(111, xlabel=xlabel, ylabel=ylabel, title='Energy limits map: ' + title) if scale: ax.set_xscale('function', functions=(fwd_x, inv_x)) ax.set_yscale('function', functions=(fwd_y, inv_y)) ax.xaxis.set_tick_params(top=True, labeltop=True, which='both') ax.set_xticks(low) ax.set_yticks(high) # # Plot stuff # zorder = 10 if data is None: # Helper rectangle rpos = (0.60, 0.05) rwidth, rheight = 0.1, 0.1 hlen = rwidth * 0.1 hwidth = hlen rcenter_x = rpos[0] + rwidth * 0.5 rcenter_y = rpos[1] + rheight * 0.5 rect_example = Rectangle(rpos, rwidth, rheight, color='white', zorder=zorder, transform=ax.transAxes) ax.add_artist(rect_example) # Arrows opts = dict(zorder=zorder + 2, head_width=hwidth, head_length=hlen, ec='black', fc='black', transform=ax.transAxes) ax.arrow(rpos[0], rcenter_y, rwidth * 0.4 - hlen, 0.0, **opts) ax.arrow(rcenter_x, rcenter_y + rheight * 0.1, 0.0, rheight * 0.4 - hlen, **opts) # # # Highlight sides opts = dict(linewidth=2.5, color='black', zorder=zorder + 1, transform=ax.transAxes, head_width=0.0, head_length=0.0) ax.arrow(rpos[0], rpos[1], 0.0, rheight, **opts) ax.arrow(rpos[0], rpos[1] + rheight, rwidth, 0.0, **opts) if data is None: # Total rect_total = Rectangle((eminimal, 10.0), 0.3, 2.0, color='magenta', zorder=zorder) ax.add_artist(rect_total) # Minimal if data is None: rect_min = Rectangle((emin, emax - ew), ew, ew, color='green', zorder=zorder) ax.add_artist(rect_min) goodlineopts = dict(zorder=zorder + 1, linestyle='--', color='yellow', alpha=0.5) goodline = ax.vlines(emin, emax, emaximal, **goodlineopts) ax.hlines(emax, eminimal, emin, **goodlineopts) else: # rect_min = Rectangle((emin, emax-ew), ew, ew, fc='none', ec='yellow', linestyle='dashed', zorder=zorder) pass if data is None: # # Legend # rect_forbidden = Rectangle((emin, emax - ew), ew, ew, color=cm(0)) rect_bad = Rectangle((emin, emax - ew), ew, ew, color=cm(1)) rect_acceptable = Rectangle((emin, emax - ew), ew, ew, color=cm(2)) handles = [ rect_example, rect_total, rect_min, rect_acceptable, rect_forbidden, rect_bad ] labels = [ 'Example', 'Total: {:.1f}$-${:.0f}'.format(eminimal, 12.0), 'Minimal: {:.1f}$-${:.0f}'.format(emin, emax), 'Acceptable', 'Acceptable', 'Forbidden', 'Bad' ] ax.legend(handles, labels, loc='lower right') # # Example plot # data = np.ones_like(L, dtype='i') * 2.0 data[L > emin] = 1 data[H < emax - ew] = 1 data[L > H] = 0 data = data[:-1, :-1] ax.pcolormesh(L, H, data, vmin=0.1, cmap=cm) ax.grid(**gridopts) return # # Data # Data = np.ma.array(np.zeros_like(L, dtype='d'), mask=np.zeros_like(L, dtype='i'))[:-1, :-1] for emin, emax, fun, success in data: imin = np.searchsorted(low, emin) imax = np.searchsorted(high, emax) - 1 Data[imin, imax] = fun Data.mask[imin, imax] = not success if fun > 12.5: # Data[imin, imax] = -1 print('Strange value below:') print('{index1:02d} {emin} in ({emin1}, {emin2})' '\t' '{index2:02d} {emax} in ({emax1}, {emax2})' '\t' '{fun}'.format( index1=imin, emin=emin, emin1=low[imin] if len(low) > imin else -1, emin2=low[imin + 1] if len(low) - 1 > imin else -1, index2=imax, emax=emax, emax1=high[imax] if len(high) > imax else -1, emax2=high[imax + 1] if len(high) - 1 > imax else -1, fun=fun)) c = ax.pcolormesh(L, H, Data, vmin=0.1) add_colorbar(c, label=dchi2) if scale: show_values(c, fontsize='x-small') ax.grid(**gridopts) lines2opts = dict(color='red', linewidth=1, linestyle='-') ax.axvline(low[1], **lines2opts) ax.axhline(high[-2], **lines2opts) axd = ax ax.set_xlim(right=3.4) savefig(output, suffix=suffix + ('zoom', )) if not scale: return axd, None, None # # Test data # # fig = plt.figure() # ax = plt.subplot(111, xlabel=xlabel, ylabel=ylabel, title='Range, MeV') # ax.minorticks_on() # ax.grid() # ax.set_xscale('function', functions=(fwd_x, inv_x)) # ax.set_yscale('function', functions=(fwd_y, inv_y)) # ax.set_xticks(low) # ax.set_yticks(high) # c = ax.pcolormesh(L, H, W) # add_colorbar(c) # show_values(c, fontsize='x-small') # # Combination # fig = plt.figure() axc = plt.subplot(111, xlabel='E split, MeV', ylabel=dchi2, title='Split test: ' + title) axc.xaxis.set_tick_params(top=True, labeltop=True, which='both') axc.minorticks_on() axc.grid() left_x, left_y = split['left'] right_x, right_y = split['right'] left_x = np.around(left_x, 6) axc.plot(left_x, left_y, label='left: [0.7, x] MeV') axc.plot(right_x, right_y, label='right: [x, 12] MeV') idx_right = np.in1d(right_x, left_x) idx_left = np.in1d(left_x, right_x) both_x = left_x[idx_left] both_y = left_y[idx_left] + right_y[idx_right] axc.plot(both_x, both_y, label='combined: uncorrelated sum') idx = np.argmin(both_y) worst_split_low_idx = np.argwhere(low == both_x[idx])[0, 0] worst_split_high_idx = np.argwhere(high == both_x[idx])[0, 0] - 1 worst_dchi2 = both_x[idx] worst_split = both_y[idx] axc.axvline(both_x[idx], linestyle='--', alpha=0.5, label='worst split: {}={:.2f} at {} MeV'.format( dchi2, worst_split, worst_dchi2)) axc.legend() savefig(output, suffix=suffix + ('split', )) # # Moving window # fig = plt.figure() ax = plt.subplot(111, xlabel='Window center', ylabel=dchi2, title='Moving window: ' + title) ax.xaxis.set_tick_params(top=True, labeltop=True, which='both') ax.minorticks_on() ax.grid() plt.subplots_adjust(right=0.81) Wunique = np.unique(W) maxpath_x = [] maxpath_y = [] maxpath_fun = [] counter = 0 for w in Wunique: if w <= 0.0: continue mask = W == w x = Center[mask] y = Data[mask] imax = np.argmax(y) xmax, ymax = x[imax], y[imax] if mask.sum() >= 3: maxpath_fun.append(ymax) if mask.sum() >= 3: color = ax.plot(x, y, label=str(w))[0].get_color() # if mask.sum()>=4: # eopts=dict(alpha=0.4, linewidth=3) # else: eopts = dict(alpha=1, linewidth=0.5, capsize=3) ax.errorbar([xmax], [ymax], xerr=w * 0.5, fmt='o-', markerfacecolor='none', color=color, **eopts) counter += 1 if counter % 5 == 0: handles, labels = ax.get_legend_handles_labels() ax.legend(handles[::-1], labels[::-1], title='Width:', bbox_to_anchor=(1.0, 1.15), loc='upper left', labelspacing=0.1) savefig(output, suffix=suffix + ('window', str(counter))) if counter == 8: ax.axhline(worst_dchi2, linestyle='--', label='Min split', alpha=0.6, color='blue') ax.axhline(np.max(Data), linestyle='--', label='Full') handles, labels = ax.get_legend_handles_labels() ax.legend(handles[::-1], labels[::-1], title='Width:', bbox_to_anchor=(1.0, 1.15), loc='upper left', labelspacing=0.1) savefig(output, suffix=suffix + ('window', )) # # Max path on the main plot # plt.sca(axd) maxpath_fun = sorted(maxpath_fun) fun_prev = None while True: fun = maxpath_fun[-1] mask = np.isclose(Data, fun) idxs = np.argwhere(mask) cmpto = [] for idx in idxs: if idx[0] > 0: app = Data[idx[0] - 1, idx[1]] if app != fun and app != fun_prev: cmpto.append(app) if idx[1] < Data.shape[1] - 1: app = Data[idx[0], idx[1] + 1] if app != fun and app != fun_prev: cmpto.append(app) if not cmpto: break maxpath_fun.append(max(cmpto)) fun_prev = fun Lcenter = 0.85 * L[1:, 1:] + 0.15 * L[:-1, 1:] Hcenter = 0.80 * H[:-1, 1:] + 0.20 * H[:-1, :-1] for fun in maxpath_fun: mask = np.isclose(Data, fun) maxpath_x.append(Lcenter[mask][0]) maxpath_y.append(Hcenter[mask][0]) # Max path xworst = [Lcenter[0, 0], Lcenter[worst_split_low_idx, -1]] yworst = [Hcenter[0, worst_split_high_idx], Hcenter[0, -1]] equiv_idx = np.argmin((Data - worst_dchi2)**2) equiv_idx = np.unravel_index(equiv_idx, Data.shape) equiv_idx1 = np.argmin((maxpath_fun - worst_dchi2)**2) x = [Lcenter[equiv_idx[0], equiv_idx[1]], maxpath_x[equiv_idx1]] y = [Hcenter[equiv_idx[0], equiv_idx[1]], maxpath_y[equiv_idx1]] # plot axd.plot(xworst, yworst, 'o', color='cyan', markerfacecolor='none', label='Worst split') axd.legend(bbox_to_anchor=(1.2, -0.15), loc='lower right', ncol=3, fontsize='small', numpoints=2) savefig(output, suffix=suffix + ('zoom_2', )) axd.plot(maxpath_x, maxpath_y, 'o', color='red', markerfacecolor='none', label='Moving window maximum position') axd.plot(x, y, 'o', color='red', alpha=0.8, markerfacecolor='red', label='Closest to worst split') axd.legend(bbox_to_anchor=(1.2, -0.15), loc='lower right', ncol=3, fontsize='small', numpoints=2) savefig(output, suffix=suffix + ('zoom_3', ))
color = lines[0].get_color() _, ev_m = nlfcn(ev) heights = smeared[smeared > 0.45] ax.vlines(ev, 0.0, heights, alpha=0.7, color='red', linestyle='--') ax.vlines(ev_m, 0.0, heights, alpha=0.7, color='green', linestyle='--') savefig(opts.output, suffix='_evis') fig = P.figure() ax1 = P.subplot(111) ax1.minorticks_on() ax1.grid() ax1.set_xlabel('Source bins') ax1.set_ylabel('Target bins') ax1.set_title('Energy non-linearity matrix') mat = convert(nl.getDenseMatrix(), 'matrix') print('Col sum', mat.sum(axis=0)) mat = N.ma.array(mat, mask=mat == 0.0) c = ax1.matshow(mat, extent=[edges[0], edges[-1], edges[-1], edges[0]]) add_colorbar(c) ax1.plot(edges, edges_m_plot, '--', linewidth=0.5, color='white') ax1.set_ylim(edges[-1], edges[0]) savefig(opts.output, suffix='_mat') if opts.show: P.show()
def plot_boxes(low, high, data=None, title=None, scale=False): fig = plt.figure(figsize=(8, 6)) if title is None: title = 'Energy limits map' else: title = 'Energy limits map: ' + title ax = plt.subplot(111, xlabel='E low', ylabel='E high', title=title) # ax.minorticks_on() if scale: fwd_x, inv_x = MakeEqualScale(low) ax.set_xscale('function', functions=(fwd_x, inv_x)) fwd_y, inv_y = MakeEqualScale(high) ax.set_yscale('function', functions=(fwd_y, inv_y)) ax.set_xticks(low) ax.set_yticks(high) eminimal = low[0] emaximal = high[-1] L, H = np.meshgrid(low, high, indexing='ij') emin, emax, ew = 1.5, 4.0, 0.5 gridopts = dict(color='red', alpha=0.4, linestyle='dashed') # # Plot stuff # zorder = 10 if data is None: # Helper rectangle rpos = (0.60, 0.05) rwidth, rheight = 0.1, 0.1 hlen = rwidth * 0.1 hwidth = hlen rcenter_x = rpos[0] + rwidth * 0.5 rcenter_y = rpos[1] + rheight * 0.5 rect_example = Rectangle(rpos, rwidth, rheight, color='white', zorder=zorder, transform=ax.transAxes) ax.add_artist(rect_example) # Arrows opts = dict(zorder=zorder + 2, head_width=hwidth, head_length=hlen, ec='black', fc='black', transform=ax.transAxes) ax.arrow(rpos[0], rcenter_y, rwidth * 0.4 - hlen, 0.0, **opts) ax.arrow(rcenter_x, rcenter_y + rheight * 0.1, 0.0, rheight * 0.4 - hlen, **opts) # # # Highlight sides opts = dict(linewidth=2.5, color='black', zorder=zorder + 1, transform=ax.transAxes, head_width=0.0, head_length=0.0) ax.arrow(rpos[0], rpos[1], 0.0, rheight, **opts) ax.arrow(rpos[0], rpos[1] + rheight, rwidth, 0.0, **opts) if data is None: # Total rect_total = Rectangle((eminimal, 10.0), 0.3, 2.0, color='magenta', zorder=zorder) ax.add_artist(rect_total) # Minimal if data is None: rect_min = Rectangle((emin, emax - ew), ew, ew, color='green', zorder=zorder) ax.add_artist(rect_min) else: # rect_min = Rectangle((emin, emax-ew), ew, ew, fc='none', ec='yellow', linestyle='dashed', zorder=zorder) pass goodlineopts = dict(zorder=zorder + 1, linestyle='--', color='yellow', alpha=0.5) goodline = ax.vlines(emin, emax, emaximal, **goodlineopts) ax.hlines(emax, eminimal, emin, **goodlineopts) if data is None: # # Legend # rect_forbidden = Rectangle((emin, emax - ew), ew, ew, color=cm(0)) rect_bad = Rectangle((emin, emax - ew), ew, ew, color=cm(1)) rect_acceptable = Rectangle((emin, emax - ew), ew, ew, color=cm(2)) handles = [ rect_example, rect_total, rect_min, goodline, rect_acceptable, rect_forbidden, rect_bad ] labels = [ 'Example', 'Total: {:.1f}$-${:.0f}'.format(eminimal, 12.0), 'Minimal: {:.1f}$-${:.0f}'.format(emin, emax), 'Acceptable', 'Acceptable', 'Forbidden', 'Bad' ] ax.legend(handles, labels, loc='lower right') # # Example plot # data = np.ones_like(L, dtype='i') * 2.0 data[L > emin] = 1 data[H < emax - ew] = 1 data[L > H] = 0 data = data[:-1, :-1] ax.pcolormesh(L, H, data, vmin=0.1, cmap=cm) ax.grid(**gridopts) return # # Data # Data = np.ma.array(np.zeros_like(L, dtype='d'), mask=np.zeros_like(L, dtype='i')) for emin, emax, fun, success in data: imin = np.searchsorted(low, emin) imax = np.searchsorted(high, emax) - 1 Data[imin, imax] = fun Data.mask[imin, imax] = not success if fun > 12.5: # Data[imin, imax] = -1 print('Strange value below:') print('{index1:02d} {emin} in ({emin1}, {emin2})' '\t' '{index2:02d} {emax} in ({emax1}, {emax2})' '\t' '{fun}'.format( index1=imin, emin=emin, emin1=low[imin] if len(low) > imin else -1, emin2=low[imin + 1] if len(low) - 1 > imin else -1, index2=imax, emax=emax, emax1=high[imax] if len(high) > imax else -1, emax2=high[imax + 1] if len(high) - 1 > imax else -1, fun=fun)) c = ax.pcolormesh(L, H, Data, vmin=0.1) add_colorbar(c) if scale: show_values(c, fontsize='x-small') ax.grid(**gridopts)
def test_energyresolution_v01(tmp_path): def axes( title, ylabel='' ): fig = plt.figure() ax = plt.subplot( 111 ) ax.minorticks_on() ax.grid() ax.set_xlabel( L.u('evis') ) ax.set_ylabel( ylabel ) ax.set_title( title ) return ax def singularities( values, edges ): indices = np.digitize( values, edges )-1 phist = np.zeros( edges.size-1 ) phist[indices] = 1.0 return phist # # Define the parameters in the current namespace # ns = env.globalns('test_energyresolution_v01') weights = [ 'Eres_'+s for s in 'abc' ] wvals = [0.016, 0.081, 0.026] percent = 0.01 ns.defparameter(weights[0], central=wvals[0], relsigma=30*percent ) par = ns.defparameter(weights[1], central=wvals[1], relsigma=30*percent ) ns.defparameter(weights[2], central=wvals[2], relsigma=30*percent ) ns.printparameters() values = [] def pop_value(): values, par par.set(values.pop()) def push_value(v): values, par values.append(par.value()) par.set(v) # # Define bin edges # binwidth=0.05 edges = np.arange( 0.0, 12.0001, binwidth ) efine = np.arange( edges[0], edges[-1]+1.e-5, 0.005 ) for eset in [ [ [1.025], [3.025], [6.025], [9.025] ], [ [ 1.025, 5.025, 9.025 ] ], [ [ 6.025, 7.025, 8.025, 8.825 ] ], ]: for i, e in enumerate(eset): ax = axes( 'Energy resolution impact' ) phist = singularities( e, edges ) hist = C.Histogram( edges, phist ) edges_o = R.HistEdges(hist) with ns: eres = C.EnergyResolution(weights, True) eres.matrix.Edges( hist ) eres.smear.Ntrue( hist ) path = os.path.join(str(tmp_path), 'eres_graph_%i.png'%i) savegraph(hist, path) allure_attach_file(path) smeared = eres.smear.Nrec.data() diff = phist.sum()-smeared.sum() print( 'Sum check for {} (diff): {}'.format( e, diff ) ) assert diff<1.e-9 lines = plot_hist( edges, smeared, label='default' ) color = lines[0].get_color() ax.vlines( e, 0.0, smeared.max(), linestyle='--', color=color ) if len(e)>1: color='green' for e in e: ax.plot( efine, binwidth*norm.pdf( efine, loc=e, scale=eres.relativeSigma(e)*e ), linestyle='--', color='green' ) sprev = smeared.copy() push_value(0.162) assert eres.smear.tainted() smeared = eres.smear.Nrec.data() assert not np.all(smeared==sprev) plot_hist( edges, smeared, label='modified', color='red', alpha=0.5) pop_value() ax.legend() path = os.path.join(str(tmp_path), 'eres_test_{:02d}.png'.format(i)) savefig(path, density=300) allure_attach_file(path) plt.close() smeared = eres.smear.Nrec.data() ax = axes( 'Relative energy uncertainty', ylabel=L.u('eres_sigma_rel') ) ax.set_ylim(0, 13.0) ax.set_xlim(0.5, 12.0) x = np.arange( 0.5, 12.0, 0.01 ) fcn = np.frompyfunc( eres.relativeSigma, 1, 1 ) y = fcn( x ) ax.plot( x, y*100. ) path = os.path.join(str(tmp_path), 'eres_sigma.png') savefig(path, density=300) allure_attach_file(path) plt.close() fig = plt.figure() ax = plt.subplot( 111 ) ax.minorticks_on() ax.grid() ax.set_xlabel( '' ) ax.set_ylabel( '' ) ax.set_title( 'Energy resolution convertsion matrix (class)' ) mat = convert(eres.getDenseMatrix(), 'matrix') mat = np.ma.array( mat, mask= mat==0.0 ) c = ax.matshow( mat, extent=[ edges[0], edges[-1], edges[-1], edges[0] ] ) add_colorbar( c ) path = os.path.join(str(tmp_path), 'eres_matc.png') savefig(path, density=300) allure_attach_file(path) plt.close() fig = plt.figure() ax = plt.subplot( 111 ) ax.minorticks_on() ax.grid() ax.set_xlabel( '' ) ax.set_ylabel( '' ) ax.set_title( 'Energy resolution convertsion matrix (trans)' ) eres.matrix.FakeMatrix.plot_matshow(colorbar=True, mask=0.0, extent=[edges[0], edges[-1], edges[-1], edges[0]]) path = os.path.join(str(tmp_path), 'eres_mat.png') savefig(path, density=300) allure_attach_file(path) plt.close()
def plot(label, distr, opts): print( label ) it = zip(['x', 'y', 'z'], mu, sigma.A1) for k, m, s in it: if label=='Poisson': s = m**0.5 fig = P.figure() ax = P.subplot( 111 ) ax.minorticks_on() ax.grid() ax.set_xlabel( k ) ax.set_ylabel( 'entries' ) ax.set_title( label ) print( ' mean', k, distr[k].mean() ) print( ' std', k, distr[k].std() ) ax.hist( distr[k], bins=100, range=(m-5*s, m+5*s), histtype='stepfilled' ) ax.axvline( m, linestyle='--', color='black' ) ax.axvspan( m-s, m+s, facecolor='green', alpha=0.5, edgecolor='none' ) savefig( opts.output, suffix=' %s %s'%(label, k) ) P.close() for (k1, m1, s1), (k2, m2, s2) in I.combinations( it, 2 ): if label=='Poisson': s1 = m1**0.5 s2 = m2**0.5 fig = P.figure() ax = P.subplot( 111 ) ax.minorticks_on() # ax.grid() ax.set_xlabel( k1 ) ax.set_ylabel( k2 ) ax.set_title( label ) cc = N.corrcoef( distr[k1], distr[k2] ) print( ' cor coef', k1, k2, cc[0,1]) c,xe,ye,im=ax.hist2d( distr[k1], distr[k2], bins=100, cmin=0.5, range=[(m1-5*s1, m1+5*s1), (m2-5*s2, m2+5*s2)] ) add_colorbar( im ) ax.axvline( m1, linestyle='--', color='black' ) ax.axhline( m2, linestyle='--', color='black' ) savefig( opts.output, suffix=' %s %s %s'%(label, k1, k2) ) P.close() if label=='Poisson': continue fig = P.figure() ax = P.subplot( 111 ) ax.minorticks_on() # ax.grid() ax.set_xlabel( k1 ) ax.set_ylabel( k2 ) ax.set_title( label ) cc = N.corrcoef( distr[k1], distr[k2] ) print( ' cor coef', k1, k2, cc[0,1]) c=ax.hexbin( distr[k1], distr[k2], gridsize=30, mincnt=1, linewidths=0.0, edgecolor='none' ) add_colorbar( c ) ax.axvline( m1, linestyle='--', color='black' ) ax.axhline( m2, linestyle='--', color='black' ) savefig( opts.output, suffix=' %s %s %s hex'%(label, k1, k2) ) P.close()
def main(opts): global savefig if opts.output and opts.output.endswith('.pdf'): pdfpages = PdfPages(opts.output) pdfpagesfilename=opts.output savefig_old=savefig pdf=pdfpages.__enter__() def savefig(*args, **kwargs): close = kwargs.pop('close', False) if opts.individual and args and args[0]: savefig_old(*args, **kwargs) pdf.savefig() if close: P.close() else: pdf = None pdfpagesfilename = '' pdfpages = None cfg = NestedDict( bundle = dict( name='energy_nonlinearity_birks_cherenkov', version='v01', nidx=[ ('r', 'reference', ['R1', 'R2']) ], major=[], ), stopping_power='data/data_juno/energy_model/2019_birks_cherenkov_v01/stoppingpower.txt', annihilation_electrons=dict( file='data/data_juno/energy_model/2019_birks_cherenkov_v01/hgamma2e.root', histogram='hgamma2e_1KeV', scale=1.0/50000 # events simulated ), pars = uncertaindict( [ ('birks.Kb0', (1.0, 'fixed')), ('birks.Kb1', (15.2e-3, 0.1776)), # ('birks.Kb2', (0.0, 'fixed')), ("cherenkov.E_0", (0.165, 'fixed')), ("cherenkov.p0", ( -7.26624e+00, 'fixed')), ("cherenkov.p1", ( 1.72463e+01, 'fixed')), ("cherenkov.p2", ( -2.18044e+01, 'fixed')), ("cherenkov.p3", ( 1.44731e+01, 'fixed')), ("cherenkov.p4", ( 3.22121e-02, 'fixed')), ("Npescint", (1341.38, 0.0059)), ("kC", (0.5, 0.4737)), ("normalizationEnergy", (11.9999999, 'fixed')) ], mode='relative' ), integration_order = 2, correlations_pars = [ 'birks.Kb1', 'Npescint', 'kC' ], correlations = [ 1.0, 0.94, -0.97, 0.94, 1.0, -0.985, -0.97, -0.985, 1.0 ], fill_matrix=True, labels = dict( normalizationEnergy = 'Pessimistic' ), ) ns = env.globalns('energy') quench = execute_bundle(cfg, namespace=ns) print() normE = ns['normalizationEnergy'].value() # # Input bins # evis_edges_full_input = N.arange(0.0, 15.0+1.e-6, 0.001) evis_edges_full_hist = C.Histogram(evis_edges_full_input, labels='Evis bin edges') evis_edges_full_hist >> quench.context.inputs.evis_edges_hist['00'] # # Python energy model interpolation function # lsnl_x = quench.histoffset.histedges.points_truncated.data() lsnl_y = quench.positron_model_relative.single().data() lsnl_fcn = interp1d(lsnl_x, lsnl_y, kind='quadratic', bounds_error=False, fill_value='extrapolate') # # Energy resolution # def eres_sigma_rel(edep): return 0.03/edep**0.5 def eres_sigma_abs(edep): return 0.03*edep**0.5 # # Oscprob # baselinename='L' ns = env.ns("oscprob") import gna.parameters.oscillation gna.parameters.oscillation.reqparameters(ns) ns.defparameter(baselinename, central=52.0, fixed=True, label='Baseline, km') # # Define energy range # data = Data(N.arange(1.8, 15.0, 0.001), lsnl_fcn=lsnl_fcn, eres_fcn=eres_sigma_abs) # Initialize oscillation variables enu = C.Points(data.enu, labels='Neutrino energy, MeV') component_names = C.stdvector(['comp0', 'comp12', 'comp13', 'comp23']) with ns: R.OscProbPMNSExpressions(R.Neutrino.ae(), R.Neutrino.ae(), component_names, ns=ns) labels=['Oscillation probability|%s'%s for s in ('component 12', 'component 13', 'component 23', 'full', 'probsum')] oscprob = R.OscProbPMNS(R.Neutrino.ae(), R.Neutrino.ae(), baselinename, labels=labels) enu >> oscprob.full_osc_prob.Enu enu >> (oscprob.comp12.Enu, oscprob.comp13.Enu, oscprob.comp23.Enu) unity = C.FillLike(1, labels='Unity') enu >> unity.fill.inputs[0] with ns: op_sum = C.WeightedSum(component_names, [unity.fill.outputs[0], oscprob.comp12.comp12, oscprob.comp13.comp13, oscprob.comp23.comp23], labels='Oscillation probability sum') oscprob.printtransformations() env.globalns.printparameters(labels=True) ns = env.globalns('oscprob') data.set_dm_par(ns['DeltaMSqEE']) data.set_nmo_par(ns['Alpha']) data.set_psur_fcn(op_sum.single().data) data.build() # # Plotting # xmax = 12.0 # # Positron non-linearity # fig = P.figure() ax = P.subplot(111, xlabel='Edep, MeV', ylabel='Evis/Edep', title='Positron energy nonlineairty') ax.minorticks_on(); ax.grid() quench.positron_model_relative.single().plot_vs(quench.histoffset.histedges.points_truncated, label='definition range') quench.positron_model_relative_full.plot_vs(quench.histoffset.histedges.points, '--', linewidth=1., label='full range', zorder=0.5) ax.vlines(normE, 0.0, 1.0, linestyle=':') ax.legend(loc='lower right') ax.set_ylim(0.8, 1.05) ax.set_xlim(0.0, xmax) savefig(opts.output, suffix='_total_relative', close=not opts.show_all) # # Positron non-linearity derivative # fig = P.figure() ax = P.subplot(111, xlabel='Edep, MeV', ylabel='dEvis/dEdep', title='Positron energy nonlineairty derivative') ax.minorticks_on(); ax.grid() e = quench.histoffset.histedges.points_truncated.single().data() f = quench.positron_model_relative.single().data()*e ec = (e[1:] + e[:-1])*0.5 df = (f[1:] - f[:-1]) dedf = (e[1:] - e[:-1])/df ax.plot(ec, dedf) ax.legend(loc='lower right') ax.set_ylim(0.975, 1.01) ax.set_xlim(0.0, xmax) savefig(opts.output, suffix='_total_derivative', close=not opts.show_all) # # Positron non-linearity effect # fig = P.figure() ax = P.subplot(111, xlabel='Edep, MeV', ylabel='Evis/Edep', title='Positron energy nonlineairty') ax.minorticks_on(); ax.grid() es = N.arange(1.0, 3.1, 0.5) esmod = es*lsnl_fcn(es) esmod_shifted = esmod*(es[-1]/esmod[-1]) ax.vlines(es, 0.0, 1.0, linestyle='--', linewidth=2, alpha=0.5, color='green', label='Edep') ax.vlines(esmod, 0.0, 1.0, linestyle='-', color='red', label='Edep quenched') ax.legend() savefig(opts.output, suffix='_quenching_effect_0') ax.vlines(esmod_shifted, 0.0, 1.0, linestyle=':', color='blue', label='Edep quenched, scaled') ax.legend() savefig(opts.output, suffix='_quenching_effect_1', close=not opts.show_all) # # Energy resolution # fig = P.figure() ax = P.subplot(111, xlabel='Edep, MeV', ylabel=r'$\sigma/E$', title='Energy resolution') ax.minorticks_on(); ax.grid() ax.plot(data.edep, eres_sigma_rel(data.edep), '-') ax.set_xlim(0.0, xmax) savefig(opts.output, suffix='_eres_rel', close=not opts.show_all) # # Energy resolution # fig = P.figure() ax = P.subplot(111, xlabel= 'Edep, MeV', ylabel= r'$\sigma$', title='Energy resolution') ax.minorticks_on(); ax.grid() ax.plot(data.edep, eres_sigma_abs(data.edep), '-') ax.set_xlim(0.0, xmax) savefig(opts.output, suffix='_eres_abs', close=not opts.show_all) # # Survival probability vs Enu # fig = P.figure() ax = P.subplot(111, xlabel='Enu, MeV', ylabel='Psur', title='Survival probability') ax.minorticks_on(); ax.grid() ax.plot(data.enu, data.data_no.psur[data.dmmid_idx], label=r'full NO') ax.plot(data.enu, data.data_io.psur[data.dmmid_idx], label=r'full IO') ax.plot(data.data_no.data_enu.psur_e[data.dmmid_idx], data.data_no.data_enu.psur[data.dmmid_idx], '^', markerfacecolor='none') ax.legend() ax.set_xlim(0.0, xmax) savefig(opts.output, suffix='_psur_enu') ax.set_xlim(2.0, 4.5) savefig(opts.output, suffix='_psur_enu_zoom', close=not opts.show_all) # # Survival probability vs Edep # fig = P.figure() ax = P.subplot(111, xlabel='Edep, MeV', ylabel='Psur', title='Survival probability') ax.minorticks_on(); ax.grid() ax.plot(data.edep, data.data_no.psur[data.dmmid_idx], label=r'full NO') ax.plot(data.edep, data.data_io.psur[data.dmmid_idx], label=r'full IO') ax.plot(data.data_no.data_edep.psur_e[data.dmmid_idx], data.data_no.data_edep.psur[data.dmmid_idx], '^', markerfacecolor='none') ax.legend() ax.set_xlim(0.0, xmax) savefig(opts.output, suffix='_psur_edep') ax.set_xlim(1.2, 3.7) savefig(opts.output, suffix='_psur_edep_zoom', close=not opts.show_all) # # Survival probability vs Edep_lsnl # fig = P.figure() ax = P.subplot(111, xlabel='Edep quenched, MeV', ylabel='Psur', title='Survival probability') ax.minorticks_on(); ax.grid() ax.plot(data.edep_lsnl, data.data_no.psur[data.dmmid_idx], label=r'full NO') ax.plot(data.edep_lsnl, data.data_io.psur[data.dmmid_idx], label=r'full IO') ax.plot(data.data_no.data_edep_lsnl.psur_e[data.dmmid_idx], data.data_no.data_edep_lsnl.psur[data.dmmid_idx], '^', markerfacecolor='none') ax.legend() ax.set_xlim(0.0, xmax) savefig(opts.output, suffix='_psur_edep_lsnl') ax.set_xlim(1.2, 3.7) savefig(opts.output, suffix='_psur_edep_lsnl_zoom', close=not opts.show_all) # # Distance between nearest peaks vs Enu, single # fig = P.figure() ax = P.subplot(111, xlabel='Enu, MeV', ylabel='Dist, MeV', title='Nearest peaks distance') ax.minorticks_on(); ax.grid() ax.plot(data.data_no.data_enu.diff_x[data.dmmid_idx], data.data_no.data_enu.diff[data.dmmid_idx], label=r'NO') ax.plot(data.data_io.data_enu.diff_x[data.dmmid_idx], data.data_io.data_enu.diff[data.dmmid_idx], label=r'IO') ax.legend() ax.set_xlim(0.0, xmax) ax.set_ylim(bottom=0.0) savefig(opts.output, suffix='_dist_enu') ax.set_xlim(2.0, 5.0) ax.set_ylim(top=0.5) savefig(opts.output, suffix='_dist_enu_zoom', close=not opts.show_all) # # Distance between nearest peaks vs Edep, single # fig = P.figure() ax = P.subplot(111, xlabel='Edep, MeV', ylabel='Dist, MeV', title='Nearest peaks distance') ax.minorticks_on(); ax.grid() ax.plot(data.data_no.data_edep.diff_x[data.dmmid_idx], data.data_no.data_edep.diff[data.dmmid_idx], label=r'NO') ax.plot(data.data_io.data_edep.diff_x[data.dmmid_idx], data.data_io.data_edep.diff[data.dmmid_idx], label=r'IO') ax.legend() ax.set_xlim(0.0, xmax) ax.set_ylim(bottom=0.0) savefig(opts.output, suffix='_dist_edep') ax.set_xlim(1.2, 4.2) ax.set_ylim(top=0.5) savefig(opts.output, suffix='_dist_edep_zoom', close=not opts.show_all) # # Distance between nearest peaks vs Edep, single # fig = P.figure() ax = P.subplot(111, xlabel='Edep quenched, MeV', ylabel='Dist, MeV', title='Nearest peaks distance') ax.minorticks_on(); ax.grid() ax.plot(data.data_no.data_edep_lsnl.diff_x[data.dmmid_idx], data.data_no.data_edep_lsnl.diff[data.dmmid_idx], label=r'NO') ax.plot(data.data_io.data_edep_lsnl.diff_x[data.dmmid_idx], data.data_io.data_edep_lsnl.diff[data.dmmid_idx], label=r'IO') ax.legend() ax.set_xlim(0.0, xmax) ax.set_ylim(bottom=0.0) savefig(opts.output, suffix='_dist_edep_lsnl') ax.set_xlim(1.2, 4.2) ax.set_ylim(top=0.5) savefig(opts.output, suffix='_dist_edep_lsnl_zoom') poly = N.polynomial.polynomial.Polynomial([0, 1, 0]) x = data.data_no.data_edep_lsnl.diff_x[data.dmmid_idx] pf = poly.fit(x, data.data_no.data_edep_lsnl.diff[data.dmmid_idx], 2) print(pf) ax.plot(x, pf(x), label=r'NO fit') ax.legend() savefig(opts.output, suffix='_dist_edep_lsnl_fit', close=not opts.show_all) # # Distance between nearest peaks vs Edep, multiple # fig = P.figure() ax = P.subplot(111, xlabel='Edep quenched, MeV', ylabel='Dist, MeV', title='Nearest peaks distance') ax.minorticks_on(); ax.grid() ax.plot(data.data_no.data_edep_lsnl.diff_x[data.dmmid_idx], data.data_no.data_edep_lsnl.diff[data.dmmid_idx], label=r'NO') ax.plot(data.data_io.data_edep_lsnl.diff_x[data.dmmid_idx], data.data_io.data_edep_lsnl.diff[data.dmmid_idx], '--', label=r'IO') for idx in (0, 5, 15, 20): ax.plot(data.data_io.data_edep_lsnl.diff_x[idx], data.data_io.data_edep_lsnl.diff[idx], '--') ax.legend() ax.set_xlim(0.0, xmax) savefig(opts.output, suffix='_dist_edep_lsnl_multi', close=not opts.show_all) # # Distance between nearest peaks difference # fig = P.figure() ax = P.subplot(111, xlabel='Edep, MeV', ylabel='Dist(IO) - Dist(NO), MeV', title='Nearest peaks distance diff: IO-NO') ax.minorticks_on(); ax.grid() ax.plot(data.e, data.diffs.edep[data.dmmid_idx], '-', markerfacecolor='none', label='Edep') ax.plot(data.e, data.diffs.edep_lsnl[data.dmmid_idx], '-', markerfacecolor='none', label='Edep quenched') ax.plot(data.e, data.diffs.enu[data.dmmid_idx], '-', markerfacecolor='none', label='Enu') ax.legend() savefig(opts.output, suffix='_dist_diff') ax.plot(data.e, data.eres, '-', markerfacecolor='none', label='Resolution $\\sigma$') ax.legend() savefig(opts.output, suffix='_dist_diff_1') # # Distance between nearest peaks difference relative to sigma # fig = P.figure() ax = P.subplot(111, xlabel='Edep, MeV', ylabel='(Dist(IO) - Dist(NO))/$\\sigma$', title='Nearest peaks distance diff: IO-NO') ax.minorticks_on(); ax.grid() ax.plot(data.e, data.diffs_rel.edep[data.dmmid_idx], '-', markerfacecolor='none', label='Edep') ax.plot(data.e, data.diffs_rel.edep_lsnl[data.dmmid_idx], '-', markerfacecolor='none', label='Edep quenched') i_edep=N.argmax(data.diffs_rel.edep[data.dmmid_idx]) i_edep_lsnl=N.argmax(data.diffs_rel.edep_lsnl[data.dmmid_idx]) ediff = data.e[i_edep] - data.e[i_edep_lsnl] ax.axvline(data.e[i_edep], linestyle='dashed', label='Max location: %.3f MeV'%(data.e[i_edep])) ax.axvline(data.e[i_edep_lsnl], linestyle='dashed', label='Max location: %.3f MeV'%(data.e[i_edep_lsnl])) ax.axvspan(data.e[i_edep], data.e[i_edep_lsnl], alpha=0.2, label='Max location diff: %.3f MeV'%(ediff)) ax.legend() savefig(opts.output, suffix='_dist_diff_rel') # # Distance between nearest peaks difference relative to sigma # fig = P.figure() ax = P.subplot(111, xlabel='Edep, MeV', ylabel='(Dist(IO) - Dist(NO))/$\\sigma$', title='Nearest peaks distance diff: IO-NO') ax.minorticks_on(); ax.grid() ledep = ax.plot(data.e, data.diffs_rel.edep[data.dmmid_idx], '--', markerfacecolor='none', label='Edep')[0] lquench = ax.plot(data.e, data.diffs_rel.edep_lsnl[data.dmmid_idx], '-', color=ledep.get_color(), markerfacecolor='none', label='Edep quenched')[0] kwargs=dict(alpha=0.8, linewidth=1.5, markerfacecolor='none') for idx in (0, 5, 15, 20): l = ax.plot(data.e, data.diffs_rel.edep[idx], '--', **kwargs)[0] ax.plot(data.e, data.diffs_rel.edep_lsnl[idx], '-', color=l.get_color(), **kwargs) ax.legend() savefig(opts.output, suffix='_dist_diff_rel_multi', close=not opts.show_all) # # Distance between nearest peaks difference relative to sigma # fig = P.figure() ax = P.subplot(111, ylabel=r'$\Delta m^2_\mathrm{ee}$', xlabel='Edep quenched, MeV', title='Nearest peaks distance diff: IO-NO/$\\sigma$') ax.minorticks_on(); ax.grid() formatter = ax.yaxis.get_major_formatter() formatter.set_useOffset(False) formatter.set_powerlimits((-2,2)) formatter.useMathText=True c = ax.pcolormesh(data.mesh_e.T, data.mesh_dm.T, data.diffs_rel.edep_lsnl.T) from mpl_tools.helpers import add_colorbar add_colorbar(c, rasterized=True) c.set_rasterized(True) savefig(opts.output, suffix='_dist_diff_rel_heatmap') if pdfpages: pdfpages.__exit__(None,None,None) print('Write output figure to', pdfpagesfilename) # savegraph(quench.histoffset.histedges.points_truncated, opts.graph, namespace=ns) if opts.show or opts.show_all: P.show()
def test_covariated_prediction(syst1, syst2, tmp_path): covbase_diag = not bool(syst1) cov_diag = not bool(syst2) and covbase_diag n = 10 start = 10 data = np.arange(start, start + n, dtype='d') stat2 = data.copy() fullcovmat = np.diag(stat2) if syst1: syst1 = np.ones((n, n), dtype='d') * 1.5 fullcovmat += syst1 cov = fullcovmat.copy() covbase = cov else: cov = stat2 covbase = fullcovmat.copy() Data = C.Points(data, labels='Data') Covmat = C.Points(cov, labels='Input covariance matrix') if syst2: syst2 = np.ones((n, n), dtype='d') * 0.5 Syst = C.Points(syst2, labels='Input systematic matrix') fullcovmat += syst2 else: Syst = None Cp = C.CovariatedPrediction(labels=[ 'Concatenated prediction', 'Base covariance matrix', 'Full cov mat Cholesky decomposition' ]) Cp.append(Data) Cp.covariate(Covmat, Data, 1, Data, 1) if Syst: Cp.addSystematicCovMatrix(Syst) Cp.covbase.covbase >> Cp.cov.covbase Cp.finalize() Cp.printtransformations() suffix = 'covariated_prediction_{}_{}'.format( syst1 is not None and 'baseblock' or 'basediag', syst2 is not None and 'syst' or 'nosyst') fig = plt.figure() ax = plt.subplot(111, xlabel='X', ylabel='Y', title='Covariance matrix base') ax.minorticks_on() ax.grid() if covbase_diag: Cp.covbase.covbase.plot_hist(label='diag') ax.legend() else: Cp.covbase.covbase.plot_matshow(colorbar=True) path = os.path.join(str(tmp_path), suffix + '_covbase.png') savefig(path, dpi=300) allure_attach_file(path) plt.close() fig = plt.figure() ax = plt.subplot(111, xlabel='X', ylabel='Y', title='Covariance matrix full') ax.minorticks_on() ax.grid() c = plt.matshow(np.ma.array(fullcovmat, mask=fullcovmat == 0.0), fignum=False) add_colorbar(c) path = os.path.join(str(tmp_path), suffix + '_cov.png') savefig(path, dpi=300) allure_attach_file(path) plt.close() path = os.path.join(str(tmp_path), suffix + '_graph.png') savegraph([Data.single(), Cp.covbase.covbase], path, verbose=False) allure_attach_file(path) data_o = Cp.prediction.prediction.data() covbase_o = Cp.covbase.covbase.data() if cov_diag: L_o = Cp.cov.L.data() L_expect = stat2**0.5 else: L_o = np.tril(Cp.cov.L.data()) L_expect = np.linalg.cholesky(fullcovmat) fig = plt.figure() ax = plt.subplot(111, xlabel='X', ylabel='Y', title='L: covariance matrix decomposition') ax.minorticks_on() ax.grid() if cov_diag: Cp.cov.L.plot_hist(label='diag') ax.legend() else: c = plt.matshow(np.ma.array(L_o, mask=L_o == 0.0), fignum=False) add_colorbar(c) path = os.path.join(str(tmp_path), suffix + '_L.png') savefig(path, dpi=300) allure_attach_file(path) plt.close() assert (data == data_o).all() if covbase_diag: assert (covbase_o == data).all() else: assert (covbase == covbase_o).all() assert np.allclose(L_o, L_expect)
def test_covariated_prediction_blocks(tmp_path): ns = [1, 3, 4, 1] start = 10 dataset = [np.arange(start, start + n, dtype='d') for n in ns] stat2set = [data.copy() for data in dataset] fullcovmat = [np.diag(stat2) for stat2 in stat2set] it = 1.0 crosscovs = [[None] * len(ns) for i in range(len(ns))] Crosscovs = [[None] * len(ns) for i in range(len(ns))] for i in range(len(ns)): for j in range(i + 1): if i == j: block = np.zeros((ns[i], ns[j]), dtype='d') else: block = np.ones((ns[i], ns[j]), dtype='d') * it * 0.5 it += 1 Crosscovs[i][j] = C.Points(block, labels='Cross covariation %i %i' % (i, j)) crosscovs[i][j] = block if i != j: crosscovs[j][i] = block.T fulldata = np.concatenate(dataset) fullsyst = np.block(crosscovs) fullcovmat = np.diag(fulldata) + fullsyst Dataset = [ C.Points(data, labels='Data %i' % i) for i, data in enumerate(dataset) ] Statset = [ C.Points(data, labels='Stat %i' % i) for i, data in enumerate(dataset) ] Cp = C.CovariatedPrediction(labels=[ 'Concatenated prediction', 'Base covariance matrix', 'Full cov mat Cholesky decomposition' ]) for Data, Stat in zip(Dataset, Statset): Cp.append(Data) Cp.covariate(Stat, Data, 1, Data, 1) for i in range(len(ns)): for j in range(i): Cp.covariate(Crosscovs[i][j], Dataset[i], 1, Dataset[j], 1) Cp.covbase.covbase >> Cp.cov.covbase Cp.finalize() Cp.printtransformations() suffix = 'covariated_prediction' fig = plt.figure() ax = plt.subplot(111, xlabel='X', ylabel='Y', title='Covariance matrix base') ax.minorticks_on() ax.grid() Cp.covbase.covbase.plot_matshow(colorbar=True, mask=0.0) path = os.path.join(str(tmp_path), suffix + '_covbase.png') savefig(path, dpi=300) allure_attach_file(path) plt.close() path = os.path.join(str(tmp_path), suffix + '_graph.png') savegraph([Cp.prediction, Cp.covbase.covbase], path, verbose=False) allure_attach_file(path) data_o = Cp.prediction.prediction.data() covbase_o = Cp.covbase.covbase.data() L_o = np.tril(Cp.cov.L.data()) L_expect = np.linalg.cholesky(fullcovmat) fig = plt.figure() ax = plt.subplot(111, xlabel='X', ylabel='Y', title='L: covariance matrix decomposition') ax.minorticks_on() ax.grid() c = plt.matshow(np.ma.array(L_o, mask=L_o == 0.0), fignum=False) add_colorbar(c) path = os.path.join(str(tmp_path), suffix + '_L.png') savefig(path, dpi=300) allure_attach_file(path) plt.close() assert (fulldata == data_o).all() assert (fullcovmat == covbase_o).all() assert np.allclose(L_o, L_expect)
def test_energyresolutioninput_v01(tmp_path): def axes( title, ylabel='' ): fig = plt.figure() ax = plt.subplot( 111 ) ax.minorticks_on() ax.grid() ax.set_xlabel( L.u('evis') ) ax.set_ylabel( ylabel ) ax.set_title( title ) return ax def singularities( values, edges ): indices = np.digitize( values, edges )-1 phist = np.zeros( edges.size-1 ) phist[indices] = 1.0 return phist # # Define the parameters in the current namespace # wvals = [0.016, 0.081, 0.026] # # Define bin edges # binwidth=0.05 edges = np.arange( 0.0, 12.0001, binwidth ) efine = np.arange( edges[0], edges[-1]+1.e-5, 0.005 ) centers = 0.5*(edges[1:]+edges[:-1]) def RelSigma(e): a, b, c = wvals return (a**2+ (b**2)/e + (c/e)**2)**0.5 relsigma = RelSigma(centers) for eset in [ [ [1.025], [3.025], [6.025], [9.025] ], [ [ 1.025, 5.025, 9.025 ] ], [ [ 6.025, 7.025, 8.025, 8.825 ] ], ]: for i, e in enumerate(eset): ax = axes( 'Energy resolution (input) impact' ) phist = singularities( e, edges ) relsigma_i = relsigma.copy() relsigma_p = C.Points(relsigma_i) hist = C.Histogram( edges, phist ) edges_o = R.HistEdges(hist) eres = C.EnergyResolutionInput(True) hist >> eres.matrix.Edges relsigma_p >> eres.matrix.RelSigma hist >> eres.smear.Ntrue path = os.path.join(str(tmp_path), 'eres_graph_%i.png'%i) savegraph(hist, path) allure_attach_file(path) smeared = eres.smear.Nrec.data() diff = phist.sum()-smeared.sum() print( 'Sum check for {} (diff): {}'.format( e, diff ) ) assert diff<1.e-9 lines = plot_hist( edges, smeared, label='default' ) color = lines[0].get_color() ax.vlines( e, 0.0, smeared.max(), linestyle='--', color=color ) if len(e)>1: color='green' for e in e: ax.plot( efine, binwidth*norm.pdf( efine, loc=e, scale=RelSigma(e)*e ), linestyle='--', color='green' ) sprev = smeared.copy() icut = relsigma_i.size//2 relsigma_i[icut:]*=2 relsigma_p.set(relsigma_i, relsigma_i.size) smeared = eres.smear.Nrec.data() shouldchange = phist[icut:].any() assert not np.all(smeared==sprev)==shouldchange plot_hist( edges, smeared, label='modified', color='red', alpha=0.5) ax.legend() path = os.path.join(str(tmp_path), 'eres_test_{:02d}.png'.format(i)) savefig(path, density=300) allure_attach_file(path) plt.close() relsigma_p.set(relsigma, relsigma.size) smeared = eres.smear.Nrec.data() ax = axes( 'Relative energy uncertainty', ylabel=L.u('eres_sigma_rel') ) ax.set_xlim(0.5, 12.0) ax.set_ylim(0, 13.0) ax.plot( centers, relsigma*100. ) path = os.path.join(str(tmp_path), 'eres_sigma.png') savefig(path, density=300) allure_attach_file(path) plt.close() fig = plt.figure() ax = plt.subplot( 111 ) ax.minorticks_on() ax.grid() ax.set_xlabel( '' ) ax.set_ylabel( '' ) ax.set_title( 'Energy resolution convertsion matrix (class)' ) mat = convert(eres.getDenseMatrix(), 'matrix') mat = np.ma.array( mat, mask= mat==0.0 ) c = ax.matshow( mat, extent=[ edges[0], edges[-1], edges[-1], edges[0] ] ) add_colorbar( c ) path = os.path.join(str(tmp_path), 'eres_matc.png') savefig(path, density=300) allure_attach_file(path) plt.close() fig = plt.figure() ax = plt.subplot( 111 ) ax.minorticks_on() ax.grid() ax.set_xlabel( '' ) ax.set_ylabel( '' ) ax.set_title( 'Energy resolution convertsion matrix (trans)' ) eres.matrix.FakeMatrix.plot_matshow(colorbar=True, mask=0.0, extent=[edges[0], edges[-1], edges[-1], edges[0]]) path = os.path.join(str(tmp_path), 'eres_mat.png') savefig(path, density=300) allure_attach_file(path) plt.close()