def test_typeclass_ndim_v03(): objects = [ C.Histogram(np.arange(6), np.arange(5)), C.Points(np.arange(5)), C.Histogram2d(np.arange(6), np.arange(7)), C.Points(np.arange(12).reshape(3, 4)) ] outputs = [p.single() for p in objects] obj = C.DummyType() list(map(obj.add_input, outputs)) dt = R.TypeClasses.CheckNdimT(context.current_precision())(1, (0, 1)) R.SetOwnership(dt, False) dt.dump() print() obj.add_typeclass(dt) res = obj.process_types() assert res dt1 = R.TypeClasses.CheckNdimT(context.current_precision())(2, (-2, -1)) R.SetOwnership(dt1, False) dt1.dump() print() obj.add_typeclass(dt1) res = obj.process_types() assert res
def test_typeclass_kind_v01(): objects = [ C.Histogram(np.arange(6), np.arange(5)), C.Histogram2d(np.arange(6), np.arange(7)) ] outputs = [p.single() for p in objects] obj = C.DummyType() list(map(obj.add_input, outputs)) dt_points = R.TypeClasses.CheckKindT(context.current_precision())(1) R.SetOwnership(dt_points, False) dt_points.dump() print() dt_hist = R.TypeClasses.CheckKindT(context.current_precision())(2) R.SetOwnership(dt_hist, False) dt_hist.dump() print() obj.add_typeclass(dt_hist) res = obj.process_types() assert res obj.add_typeclass(dt_points) print('Exception expected: ', end='') res = obj.process_types() assert not res
def test_typeclass_passtype(): """Last input has another edges""" objects = [ C.Histogram2d(np.arange(4), np.arange(5)), C.Histogram(np.arange(4)), C.Points(np.arange(12).reshape(3,4)) ] outputs = [p.single() for p in objects] obj = C.DummyType() k = list(map(obj.add_input, outputs)) for i in range(5): obj.add_output() dt1 = R.TypeClasses.PassTypeT(context.current_precision())((0,), (0,1)) dt2 = R.TypeClasses.PassTypeT(context.current_precision())((1,), (2,-1)) R.SetOwnership(dt1, False) R.SetOwnership(dt2, False) dt1.dump(); print() dt2.dump(); print() obj.add_typeclass(dt1) obj.add_typeclass(dt2) res = obj.process_types(); assert res obj.print() dta = outputs[0].datatype() dtb = outputs[1].datatype() doutputs = obj.transformations.back().outputs assert doutputs[0].datatype()==dta assert doutputs[1].datatype()==dta assert doutputs[2].datatype()==dtb assert doutputs[3].datatype()==dtb assert doutputs[4].datatype()==dtb
def test_typeclass_passeach_02(): """Pass with step 2""" objects = [ C.Histogram2d(np.arange(4), np.arange(5)), C.Histogram(np.arange(4)), C.Points(np.arange(20).reshape(4,5)) ] outputs = [p.single() for p in objects] obj = C.DummyType() i = list(map(obj.add_input, outputs)) i1 = list(map(obj.add_input, outputs)) for i in range(3): obj.add_output() dt1 = R.TypeClasses.PassEachTypeT(context.current_precision())((0,-1,2), (0,-1)) R.SetOwnership(dt1, False) dt1.dump(); print() obj.add_typeclass(dt1) res = obj.process_types(); assert res obj.print() dta = outputs[0].datatype() dtb = outputs[1].datatype() dtc = outputs[2].datatype() doutputs = obj.transformations.back().outputs assert doutputs[0].datatype()==dta assert doutputs[1].datatype()==dtc assert doutputs[2].datatype()==dtb
def test_sumaxis_01(kind, axis): """Test ViewRear on Points (start, len)""" size = 4 inp = np.arange(12.0).reshape(3, 4) shouldbe = inp.sum(axis=axis) xedges = np.arange(inp.shape[0] + 1) yedges = np.arange(inp.shape[1] + 1) edges = (xedges, yedges) if kind == 'points': Inp = C.Points(inp) else: Inp = C.Histogram2d(xedges, yedges, inp) sum = C.SumAxis(axis, Inp) sum.printtransformations() res = sum.sumaxis.result.data() print('Input', inp) print('Result ({})'.format(axis), res) print('Should be', shouldbe) assert np.allclose(res, shouldbe, atol=0, rtol=0) if kind == 'hist': newedges = sum.sumaxis.result.datatype().edges select = 1 if axis == 0 else 0 print('Original edges', edges) print('New edges', newedges) assert np.allclose(edges[select], newedges)
def test_histogram_v02_2d(tmp_path): edgesx = np.logspace(0, 3, 6, base=2) edgesy = np.linspace(0, 10, 20) data = np.arange(1.0, (edgesx.size - 1) * (edgesy.size - 1) + 1, dtype='d').reshape(edgesx.size - 1, edgesy.size - 1) hist = C.Histogram2d(edgesx, edgesy, data) res = hist.hist.hist() edgesx_dt = np.array(hist.hist.hist.datatype().edgesNd[0]) edgesy_dt = np.array(hist.hist.hist.datatype().edgesNd[1]) # Plot fig = plt.figure() ax = plt.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel('X (column), log scale') ax.set_ylabel('Y row') ax.set_title('2d histogram example') ax.set_xscale('log') hist.hist.hist.plot_pcolor(colorbar=True) suffix = 'histogram2d' path = os.path.join(str(tmp_path), suffix + '.png') savefig(path, dpi=300) allure_attach_file(path) plt.close() fig = plt.figure() ax = plt.subplot(111, projection='3d') ax.minorticks_on() ax.grid() ax.set_xlabel('X (column)') ax.set_ylabel('Y (row)') ax.set_title('2d histogram example (3d)') ax.azim -= 70 hist.hist.hist.plot_bar3d(cmap=True, colorbar=True) suffix = 'histogram2d_3d' path = os.path.join(str(tmp_path), suffix + '.png') savefig(path, dpi=300) allure_attach_file(path) plt.close() path = os.path.join(str(tmp_path), suffix + '_graph.png') savegraph(hist.hist, path) allure_attach_file(path) plt.close() # Test consistency assert np.all(res == data) assert np.all(edgesx == edgesx_dt) assert np.all(edgesy == edgesy_dt)
def test_histogram_v02_TH2D(tmp_path): rhist = R.TH2D('testhist', 'testhist', 20, 0, 10, 24, 0, 12) xyg = R.TF2("xyg", "exp([0]*x)*exp([1]*y)", 0, 10, 0, 12) xyg.SetParameter(0, -1 / 2.) xyg.SetParameter(1, -1 / 8.) R.gDirectory.Add(xyg) rhist.FillRandom('xyg', 10000) hist = C.Histogram2d(rhist) buf = rhist.get_buffer().T res = hist.hist.hist() # Plot fig = plt.figure() ax = plt.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel('X label') ax.set_ylabel('Y label') ax.set_title('ROOT histogram') rhist.pcolorfast(colorbar=True) suffix = 'histogram2d' path = os.path.join(str(tmp_path), suffix + '.png') savefig(path, dpi=300) allure_attach_file(path) plt.close() fig = plt.figure() ax = plt.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel('X label') ax.set_ylabel('Y label') ax.set_title('GNA histogram') hist.hist.hist.plot_pcolorfast(colorbar=True) suffix = 'histogram2d' path = os.path.join(str(tmp_path), suffix + '.png') savefig(path, dpi=300) allure_attach_file(path) plt.close() path = os.path.join(str(tmp_path), suffix + '_graph.png') savegraph(hist.hist, path) allure_attach_file(path) plt.close() # Test consistency assert np.all(buf == res)
def test_typeclass_same_v04(): """Last input has another edges""" objects=[C.Histogram2d(np.arange(4), np.arange(5)) for i in range(5)] objects.append(C.Histogram2d(np.arange(1,5), np.arange(5))) outputs = [p.single() for p in objects] obj = C.DummyType() list(map(obj.add_input, outputs)) dt = R.TypeClasses.CheckSameTypesT(context.current_precision())((1,-1), 'shape') R.SetOwnership(dt, False) dt.dump(); print() obj.add_typeclass(dt) res = obj.process_types(); assert res dt1 = R.TypeClasses.CheckSameTypesT(context.current_precision())((1,-1),) R.SetOwnership(dt1, False) dt1.dump(); print() obj.add_typeclass(dt1) print('Exception expected: ',end='') res = obj.process_types(); assert not res
from gna.bindings import common from matplotlib import pyplot as plt # Create numpy array for data points xmin, ymin = 10, 20 nbinsx, nbinsy = 40, 50 edgesx = np.linspace(xmin, xmin + nbinsx * 0.5, nbinsx + 1) edgesy = np.linspace(ymin, ymin + nbinsy * 0.5, nbinsy + 1) # Create fake data array cx = (edgesx[1:] + edgesx[:-1]) * 0.5 cy = (edgesy[1:] + edgesy[:-1]) * 0.5 X, Y = np.meshgrid(cx, cy, indexing='ij') narray = np.exp(-0.5 * (X - 15.0)**2 / 10.0**2 - 0.5 * (Y - 30.0)**2 / 3.0**2) # Create a histogram instance with data, stored in `narray` # and edges, stored in `edges` hist = C.Histogram2d(edgesx, edgesy, narray) fig = plt.figure() ax = plt.subplot(111) ax.set_title('pcolorfast') ax.minorticks_on() ax.set_xlabel('x label') ax.set_ylabel('y label') hist.hist.hist.plot_pcolorfast(colorbar=True) savefig(tutorial_image_name('png', suffix='pcolorfast')) fig = plt.figure() ax = plt.subplot(111) ax.set_title('imshow')
R.gDirectory.Add(xyg) roothist2d.FillRandom('xyg', 10000) # Fill TMatrixD with for i, (i1, i2) in enumerate( I.product(range(rootmatrix.GetNrows()), range(rootmatrix.GetNcols()))): rootmatrix[i1, i2] = i # Create Points p1 = C.Points(roothist1d) p2 = C.Points(roothist2d) p3 = C.Points(rootmatrix) # Create Histograms h1d = C.Histogram(roothist1d) h2d = C.Histogram2d(roothist2d) # Check p1 print('Points from TH1D (underflow/overflow are ignored)') roothist1d.Print('all') print(p1.points.points()) fig = plt.figure() ax = plt.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel('X axis') ax.set_ylabel('Entries') ax.set_title('Points and TH1 comparison')
ns.printparameters(labels=True) print() # Define binning and integration orders x_nbins = 20 x_edges = np.linspace(-np.pi, np.pi, x_nbins + 1, dtype='d') x_widths = x_edges[1:] - x_edges[:-1] x_orders = 4 y_nbins = 30 y_edges = np.linspace(0, 2.0 * np.pi, y_nbins + 1, dtype='d') y_widths = y_edges[1:] - y_edges[:-1] y_orders = 3 # Initialize histogram hist = C.Histogram2d(x_edges, y_edges) # Initialize integrator integrator = R.Integrator2GL(x_nbins, x_orders, y_nbins, y_orders) integrator.points.edges(hist.hist.hist) int_points = integrator.points # Create integrable: a*sin(x) + b*cos(k*x) arg_t = C.WeightedSum(['a', 'b'], [int_points.xmesh, int_points.ymesh]) sin_t = R.Sin(arg_t.sum.sum) # integrator.add_input(sint_t.sin.result) integrator.hist.f(sin_t.sin.result) X, Y = integrator.points.xmesh.data(), integrator.points.ymesh.data() integrator.print()