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
mydict = dict( mylabel = 'my text', anotherlabel = 'another text') reg_dictionary( 'mydict', mydict ) print("Printing labels") print( L(' my label: {mylabel}') ) print( L(' two labels: {mylabel} and {anotherlabel}') ) print( L(' unknown key representation: {unknown}') ) print( L(' {1} {0} arguments: {mylabel}', 'positional', 'using')) print() print("Simple acecss to labels") print(' by short key:', L.s('mylabel')) print(' by short key:', L.base('mylabel')) print() print("Simple modifications") print( L(' capitalize: {^mylabel}') ) print( L(' indirect access to label from key "var": {$var}', var='mylabel') ) print( L(' capitalize indirect: {^$var}', var='mylabel') ) print() print( 'Units and labels:' ) print(' name, unit, default offset:', L.u('dm32')) print(' name, unit, custom offset:', L.u('dm32', offset=-5)) print(' name, unit, force no offset:', L.u('dm32', offset=0)) print() print(' name, no unit:', L.u('theta13')) print(' name, no unit, custom offset:', L.u('theta13', offset=-2))
enew[reg2] = edges[reg2] * corr[reg2] enew[reg3] = edges[reg3] * corr[reg3] return corr, enew binwidth = 0.05 edges = N.arange(0.0, 12.0001, binwidth) corr, edges_m = nlfcn(edges) edges_m_plot = N.ma.array(edges_m, mask=edges_m <= -1.e100) fig = P.figure() ax = P.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel(L.u('edep')) ax.set_ylabel('Correction') ax.set_title('Non-linearity correction') ax.plot(edges, corr) savefig(opts.output, suffix='_corr') fig = P.figure() ax = P.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel(L.u('edep')) ax.set_ylabel(L.u('evis')) ax.set_title('Non-linearity correction')
except Exception as e: print( '\033[31mFailed to plot dot\033[0m' ) # # Make plots # from mpl_tools.helpers import plot_hist from gna.labelfmt import formatter as L axes=() for i, hist in enumerate(hists_list): P.figure(), ax = P.subplot( 111 ) axes+=ax, ax.minorticks_on() ax.grid() ax.set_xlabel( L.u('evis') ) ax.set_ylabel( 'Entries' ) ax.set_title( cfg.detector.detectors[i] ) data = hist.hist.hist.data() print( 'Sum data %i=%i'%( i, data.sum() ) ) plot_hist(edges, data, label='original') for bundle in b.bundles.values(): for i, (oname, out) in enumerate( bundle.outputs.items() ): P.sca(axes[i]) data = out.data() print( 'Sum data %s (%s) %i=%f'%( type(bundle).__name__, oname, i, data.sum() ) ) plot_hist(out.datatype().edges, data, label=type(bundle).__name__)
points_t.points.setLabel('E (integr)') shared=NestedDict( points=points_t.single() ) ns = env.globalns('testexp') """Execute bundle""" b, = execute_bundles( cfg=cfg, common_namespace=ns, shared=shared ) env.globalns.printparameters( labels=True ) """Plot result""" fig = P.figure() ax = P.subplot( 111 ) ax.minorticks_on() ax.grid() ax.set_xlabel( L.u('enu') ) ax.set_ylabel( L.u('anu_yield') ) ax.set_title( '' ) # ax.vlines(cfg.edges, 0.0, 2.5, linestyles='--', alpha=0.5, colors='blue') for name, output in b.outputs.items(): data=output.data().copy() ax.plot( points, N.ma.array(data, mask=data==0.0), label=L.s(name) ) if opts.set or opts.rset: for var, value in opts.set: par=ns[var] par.set(float(value)) for var, value in opts.rset: par=ns[var]
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()
lines = plot_hist(edges, smeared) 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=color) savefig(opts.output, suffix='_test_%i' % i) ax = axes('Relative energy uncertainty', ylabel=L.u('eres_sigma_rel')) x = N.arange(0.5, 12.0, 0.01) fcn = N.frompyfunc(eres.relativeSigma, 1, 1) y = fcn(x) ax.plot(x, y * 100.) savefig(opts.output, suffix='_sigma') fig = P.figure() ax = P.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel('') ax.set_ylabel('') ax.set_title('Energy resolution convertsion matrix')
ns = env.globalns('testexp') """Execute bundle""" bundles = execute_bundles( cfg=cfg, common_namespace=ns, shared=shared ) snf = bundles[-1] offeq = bundles[-2] env.globalns.printparameters( labels=True ) """Plot result""" fig = P.figure() ax = P.subplot( 111 ) ax.minorticks_on() ax.grid() ax.set_xlabel( L.u('enu') ) ax.set_ylabel( '' ) ax.set_title( '' ) ax.plot( snf.edges, snf.ratio ) # ax.vlines(cfg.edges, 0.0, 2.5, linestyles='--', alpha=0.5, colors='blue') # for name, output in b.outputs.items(): # ax.plot( points, output.data().copy(), label=L.s(name) ) # if opts.set or opts.rset: # for var, value in opts.set: # par=ns[var] # par.set(float(value)) # for var, value in opts.rset:
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()