def test_shifting_edges_2(): param = env.defparameter("param", central=0.1, fixed=True) placeholder = env.defparameter("placeholder", central=1, fixed=True) initial_binning = np.arange(1, 10, 0.5) initial_integrator = C.IntegratorGL(initial_binning, 4, labels = (('First Sampler', 'First Integrator'))) print("Integration edges from first integral") print(initial_integrator.points.xedges.data()) from gna.constructors import stdvector shifted = R.WeightedSum(-2*param.value(), stdvector(['placeholder']), stdvector(['inp'])) shifted.sum.inp(initial_integrator.points.xedges) print("After shift by -2*{}".format(param.value())) print(shifted.sum.sum.data()) expected = initial_binning - 2*param.value() assert np.allclose(expected, shifted.sum.sum.data())
def test_shifting_edges_1(): par = env.defparameter("par", central=0.1, fixed=True) dummy = env.defparameter("dummy", central=1, fixed=True) initial_binning = np.arange(1, 10, 0.5) initial_integrator = C.IntegratorGL(initial_binning, 4, labels = (('First Sampler', 'First Integrator'))) print("Integration edges from first integral") print(initial_integrator.points.xedges.data()) filled = C.FillLike(-2) initial_integrator.points.xedges >> filled.fill outputs = [par.single(), filled.single(), dummy.single(), initial_integrator.points.xedges] shifted = C.WeightedSumP(outputs) print("After shift by -2*{}".format(par.value())) print(shifted.sum.sum.data()) expected = initial_binning - 2*par.value() assert np.allclose(expected, shifted.sum.sum.data())
def test_shifting_edges_3(): param = env.defparameter("param1", central=0.1, fixed=True) placeholder = env.defparameter("placeholder1", central=1, fixed=True) initial_binning = np.arange(1, 10, 0.5) initial_integrator = C.IntegratorGL(initial_binning, 4, labels = (('First Sampler', 'First Integrator'))) print("Integration edges from first integral") print(initial_integrator.points.xedges.data()) param_point = C.Points([-2*param.value()]) inputs = [param_point.points.points, initial_integrator.points.xedges] shifted = C.SumBroadcast(inputs) print("After shift by -2*{}".format(param.value())) print(shifted.single().data()) expected = initial_binning - 2*param.value() assert np.allclose(expected, shifted.single().data())
def test_fixed(): env.defparameter('probe1', central=0., sigma=1.) env.defparameter('probe2', central=0., sigma=1.) env.defparameter('probe3', central=0., sigma=1.) env.defparameter('probe_fixed', central=0., sigma=1., fixed=True) print() msg = 'Testing that par {0} is created fixed' print(msg.format(env.parameters['probe_fixed'].name())) assert env.parameters['probe_fixed'].isFixed() == True print('It is!\n') print("Checks whether get_parameters() discards fixed params by default:") no_fixed = [_.name() for _ in get_parameters(['probe1', 'probe_fixed'])] assert 'probe1' in no_fixed print('True!\n') print("Checks that get_parameters(drop_fixed=False) keeps parameters:") with_fixed = [ par.name() for par in get_parameters(['probe1', 'probe_fixed'], drop_fixed=False) ] assert 'probe1' in with_fixed and 'probe_fixed' in with_fixed print('True!\n')
#!/usr/bin/env python from load import ROOT from gna.env import env from gna.parameters.parameter_loader import get_parameters from gna import parameters parameters.debug = True # Necessary evil, it triggers import of all other symbols from shared library ROOT.GNAObject Param_Double = ROOT.GaussianParameter('double') env.defparameter('absolute sigma 1', central=0., sigma=1.) env.defparameter('absolute sigma 2', central=2., sigma=0.1) env.defparameter('relative sigma 1', central=0., relsigma=0.1) env.defparameter('relative sigma 2', central=2., relsigma=0.1) print() env.defparameter('absolute sigma 3', central=2., uncertainty=0.1, uncertainty_type='absolute') env.defparameter('relative sigma 3', central=2., uncertainty=0.1, uncertainty_type='relative') gp = env.defparameter('absolute sigma with limits', central=2., relsigma=0.1, limits=[0.0, 3.0]) gp1 = env.defparameter('absolute sigma with limits 1',
ax.set_title(title) return ax def singularities(values, edges): indices = N.digitize(values, edges) - 1 phist = N.zeros(edges.size - 1) phist[indices] = 1.0 return phist # # Define the parameters in the current namespace # percent = 0.01 env.defparameter('Eres_a', central=0.016, relsigma=30 * percent) env.defparameter('Eres_b', central=0.081, relsigma=30 * percent) env.defparameter('Eres_c', central=0.026, relsigma=30 * percent) print_parameters(env.globalns) # # Define bin edges # binwidth = 0.05 edges = N.arange(0.0, 12.0001, binwidth) efine = N.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]],
default=1, type=int, help='number of diagonals') parser.add_argument( '-u', '--upper', action='store_true', help='force transformation to account for upper triangular matrix') parser.add_argument( '-o', '--offdiag', action='store_true', help='force transformation to account for upper triangular matrix') opts = parser.parse_args() env.defparameter('DiagScale', central=opts.value, relsigma=0.1) mat = N.matrix(N.arange(16.0).reshape(4, 4)) print('Raw matrix') print(mat) print('Sum over rows') print(mat.A.sum(axis=0)) print('Raw normalized matrix') print(mat.A / mat.A.sum(axis=0)) pmat = C.Points(mat) rd = R.RenormalizeDiag(opts.ndiag, int(opts.offdiag), int(opts.upper)) rd.renorm.inmat(pmat.points)
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 = N.digitize(values, edges) - 1 phist = N.zeros(edges.size - 1) phist[indices] = 1.0 return phist par = env.defparameter('DiagScale', central=1.0, relsigma=0.1) binwidth = 0.05 edges = N.arange(0.0, 12.0001, binwidth) n = 240 mat = 0.0 for i in range(n): if i < 4: scale = 1.0 - i * 0.2 else: scale = 0.00005 * (n - i) if i: mat += N.diag(N.full(n - i, scale), i) if not opts.upper: mat += N.diag(N.full(n - i, scale), -i) else:
def test_par_loader(): probe1 = env.defparameter('probe1', central=0., sigma=1.) probe2 = env.defparameter('probe2', central=0., sigma=1.) probe3 = env.defparameter('probe3', central=0., sigma=1.) test_ns = env.ns('test_ns') test1 = test_ns.defparameter('test1', central=1., sigma=0.1) test2 = test_ns.defparameter('test2', central=1., sigma=0.1) test3 = test_ns.defparameter('test3', central=1., sigma=0.1) test4 = test_ns.defparameter('test4', central=1., sigma=0.1) extra_test_ns = env.ns('extra_test_ns') extra1 = extra_test_ns.defparameter('extra1', central=1., sigma=0.1) for name, par in test_ns.walknames(): print("Par in namespace", name) print() print("Quering pars with get_parameters() ") par1 = get_parameters(['probe1']) assert (par1[0] == probe1) print('Got global parameter {}'.format(par1[0].name())) print() par2 = get_parameters(['test_ns.test1']) assert (par2[0] == test1) print('Got parameter {0} from namespace {1}'.format( par2[0].name(), test_ns.name)) print() par_in_namespace = get_parameters(['test_ns']) assert (par_in_namespace == [test1, test2, test3, test4]) print() print('Got parameters {0} from ns {ns}'.format( [_.name() for _ in par_in_namespace], ns=test_ns.name)) print() par_two_namespaces = get_parameters(['test_ns', 'extra_test_ns']) assert (par_two_namespaces == [test1, test2, test3, test4, extra1]) print() print('Got parameters {0} from nses {ns}'.format( [_.name() for _ in par_two_namespaces], ns=[test_ns.name, extra_test_ns.name])) print() par_mixed_ns_and_global = get_parameters(['test_ns', 'probe1']) print('Got parameters {0} from nses {ns} and global'.format( [_.name() for _ in par_mixed_ns_and_global], ns=test_ns.name)) assert (par_mixed_ns_and_global == [test1, test2, test3, test4, probe1]) print() wildcard = get_parameters(['test_ns*']) assert (wildcard == [test1, test2, test3, test4]) print('Got parameters {0} from by wildcard '.format( [_.name() for _ in wildcard])) print() #Asking for missing parameters, raise KeyError try: get_parameters(['missing']) except KeyError: print("KeyError for missing parameter is raised correctly")
from gna.env import env from gna.labelfmt import formatter as L from mpl_tools.helpers import savefig, plot_hist, add_colorbar from scipy.stats import norm from gna.converters import convert from argparse import ArgumentParser import gna.constructors as C P.rc('text', usetex=True) parser = ArgumentParser() parser.add_argument('-o', '--output') parser.add_argument('-s', '--show', action='store_true') opts = parser.parse_args() env.defparameter("E_0", central=0.165, fixed=True) env.defparameter("p0", central=-7.26624e+00, fixed=True) env.defparameter("p1", central=1.72463e+01, fixed=True) env.defparameter("p2", central=-2.18044e+01, fixed=True) env.defparameter("p3", central=1.44731e+01, fixed=True) env.defparameter("p4", central=3.22121e-02, fixed=True) energy = N.linspace(0.1, 10, num=200) gna_energy = C.Points(energy) cherenkov = R.Cherenkov_Borexino() cherenkov.cherenkov.energy(gna_energy) ch_response = cherenkov.cherenkov.ch_npe.data() fig = P.figure() ax = P.subplot(111) ax.plot(energy, ch_response)
def axes(title, ylabel=''): fig = P.figure() ax = P.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel(L.u('evis')) ax.set_ylabel(ylabel) ax.set_title(title) return ax # # Define the parameters in the current namespace # percent = 0.01 env.defparameter('Eres_a', central=0.0, fixed=True) par = env.defparameter('Eres_b', central=0.03, fixed=True) env.defparameter('Eres_c', central=0.0, fixed=True) print_parameters(env.globalns) # # Define bin edges # nbins = 40 edges = N.linspace(0.0, 2.0, nbins + 1) binwidth = edges[1] - edges[0] centers = (edges[1:] + edges[:-1]) * 0.5 efine = N.arange(edges[0], edges[-1] + 1.e-5, 0.005) ax = axes('Energy resolution impact') phist = N.ones(edges.size - 1)
def test_par_cov(): probe_1 = env.defparameter('probe1', central=0., sigma=1.) probe_2 = env.defparameter('probe2', central=0., sigma=1.) probe_3 = env.defparameter('probe3', central=0., sigma=1.) test_ns = env.ns('test_ns') test_ns.defparameter('test0', central=1., sigma=0.1) test_ns.defparameter('test1', central=1., sigma=0.1) test_ns.defparameter('test2', central=1., sigma=0.1) test_ns.defparameter('test3', central=1., sigma=0.1) extra_test_ns = env.ns('extra_test_ns') extra1 = extra_test_ns.defparameter('extra1', central=1., sigma=0.1) extra2 = extra_test_ns.defparameter('extra2', central=1., sigma=0.1) extra3 = extra_test_ns.defparameter('extra3', central=1., sigma=0.1) extra4 = extra_test_ns.defparameter('extra4', central=1., sigma=0.1) cov1 = 0.1 print("Setting covariance of probe_1 with probe_2 to {0}".format(cov1)) probe_1.setCovariance(probe_2, cov1) print("Check that they are mutually correlated now.") assert probe_1.isCorrelated(probe_2) and probe_2.isCorrelated(probe_1) print("Success") print("Get covariance from both -- {0} and {1}\n".format( probe_1.getCovariance(probe_2), probe_2.getCovariance(probe_1))) print("Checks that change of one propagates to another") cov2 = 0.2 probe_1.setCovariance(probe_2, cov2) assert (probe_1.getCovariance(probe_2) == cov2 and probe_2.getCovariance(probe_1) == cov2) print("Success\n") test_pars = get_parameters( ['test_ns.test0', 'test_ns.test1', 'test_ns.test2', 'test_ns.test3']) print("Test pars sequence is {}".format([_.name() for _ in test_pars])) cov_matrix1 = make_fake_covmat(4) print("Test covariance matrix is \n", cov_matrix1) ch.covariate_pars(test_pars, cov_matrix1) for first, second in itertools.combinations_with_replacement( range(len(test_pars)), 2): try: if first != second: assert test_pars[first].getCovariance( test_pars[second]) == cov_matrix1[first, second] else: assert test_pars[first].sigma() == np.sqrt(cov_matrix1[first, second]) except AssertionError: print((first, second), test_pars[first].getCovariance(test_pars[second])**2, cov_matrix1[first, second]) raise extra_pars = [extra1, extra2, extra3] cov_mat_extra = make_fake_covmat(3) cov_storage = ch.CovarianceStorage("extra_store", extra_pars, cov_mat_extra) ch.covariate_ns('extra_test_ns', cov_storage) for first, second in itertools.combinations_with_replacement( range(len(extra_pars)), 2): try: if first != second: assert test_pars[first].getCovariance( test_pars[second]) == cov_matrix1[first, second] else: assert test_pars[first].sigma() == np.sqrt(cov_matrix1[first, second]) except AssertionError: print((first, second), test_pars[first].getCovariance(test_pars[second])**2, cov_matrix1[first, second]) raise