def init(self): head = self.opts.plot[0] kwargs = dict(self.opts.options, joints=self.opts.joints) kwargs.setdefault('rankdir', 'LR') kwargs['subgraph']=self.opts.subgraph kwargs['include_only']=self.opts.include_only if self.opts.splines: kwargs['splines']=self.opts.splines if self.opts.namespace is not self.undefined: kwargs['namespace']=env.globalns(self.opts.namespace) graph = GNADot( head, **kwargs ) for output in self.opts.outputs: print( 'Write graph to:', output ) if output.endswith('.dot'): graph.write(output) else: graph.layout(prog='dot') graph.draw(output) if self.opts.stdout: graph.write() if self.opts.stderr: import sys graph.write( sys.stderr )
def test_variables_precision(function_name): from gna.env import env gns = env.globalns(function_name) ns = gns('namespace1') ns.defparameter('double', central=1, fixed=True, label='Double precision variable') with context.precision('float'): ns.defparameter('float', central=2, fixed=True, label='Float variable') var_dbl = ns['double'].getVariable() var_flt = ns['float'].getVariable() assert var_dbl.value() == 1.0 assert var_dbl.typeName() == 'double' assert var_flt.value() == 2.0 assert var_flt.typeName() == 'float' par_flt = ns['float'].getParameter() par_dbl = ns['double'].getParameter() par_flt.set(1.e-46) par_dbl.set(1.e-46) assert par_flt.value() == 0.0 assert par_dbl.value() != 0.0 par_flt.set(1.e39) par_dbl.set(1.e39) assert par_flt.value() == float('inf') assert par_dbl.value() != float('inf')
def test_variable_allocation(function_name): from gna.env import env ns = env.globalns(function_name) ndata = 10 allocator = R.arrayviewAllocatorSimple(context.current_precision())(ndata) with context.allocator(allocator): ns.defparameter('float1', central=1, fixed=True, label='Float variable 1') ns.defparameter('float2', central=2, fixed=True, label='Float variable 2') ns.defparameter('angle', central=3, label='Angle parameter', type='uniformangle') ns.defparameter('discrete', default='a', label='Discrete parameter', type='discrete', variants=OrderedDict([('a', 10.0), ('b', 20.0), ('c', 30.0)])) ns.printparameters(labels=True) print('Data (filled):', allocator.view()) print('Data (all):', allocator.viewall())
def test_filllike_v02(function_name): """Initialize inpnuts""" size = 5 arr1 = N.arange(0, size) """Initialize environment""" ns = env.globalns(function_name) p1 = ns.defparameter('w1', central=1.0, sigma=0.1) points1 = C.Points(arr1) with ns: ws = C.WeightedSum(['w1'], [points1.points.points]) ws.print() print() flvalue = 2.0 fl = C.FillLike(flvalue) ws >> fl.fill.inputs[0] out = fl.fill.outputs[0] data = out.data() print('data:', data) print() compare_filllike(data, [flvalue] * size, 'Data output failed') print('Change parameter') p1.set(-1.0) taintflag = fl.fill.tainted() print('data:', data) print('taintflag:', taintflag) compare_filllike(data, [flvalue] * size, 'Data output failed') compare_filllike(taintflag, False, 'Taintflag should be false')
def test_tree_manager(function_name): from gna.env import env gns = env.globalns(function_name) ndata = 200 with context.manager(ndata) as manager: ns = gns('namespace1') ns.defparameter('float1', central=1, fixed=True, label='Float variable 1') ns.defparameter('float2', central=2, fixed=True, label='Float variable 2') ns = gns('namespace2') ns.defparameter('angle', central=3, label='Angle parameter', type='uniformangle') ns.defparameter('discrete', default='a', label='Discrete parameter', type='discrete', variants=OrderedDict([('a', 10.0), ('b', 20.0), ('c', 30.0)])) ns = gns('namespace3') from gna.parameters.oscillation import reqparameters reqparameters(ns) with ns: ns.materializeexpressions() ns['Delta'].set(N.pi*0.25) pars = tuple(par.getVariable() for (name,par) in ns.walknames()) manager.setVariables(C.stdvector(pars)) gns.printparameters(labels=True) allocator = manager.getAllocator() varray = manager.getVarArray() data_v = varray.vararray.points.data() data_a = allocator.view() print('Data (filled):', data_a.size, data_a.dtype, data_a) print('Data (vararray):', data_v.size, data_v.dtype, data_v) mask = data_v==data_a assert mask is not False and mask.all()
def weightedsump_make(nsname): """Initialize inpnuts""" arr1 = N.arange(0, 5, dtype='d') arr2 = -arr1 zeros = N.zeros((5,), dtype='d') print( 'Data1:', arr1 ) print( 'Data2:', arr2 ) labels = [ 'arr1', 'arr2' ] weights = [ 'w1', 'w2' ] """Initialize environment""" ns=env.globalns(nsname) p1 = ns.defparameter( weights[0], central=1.0, sigma=0.1 ) p2 = ns.defparameter( weights[1], central=1.0, sigma=0.1 ) ns.printparameters() with ns: pp1 = VarArray([weights[0]]) pp2 = VarArray([weights[1]]) """Initialize transformations""" points1 = Points( arr1 ) points2 = Points( arr2 ) return arr1, p1, pp1, points1, arr2, p2, pp2, points2, zeros
def test_anue_free_spectra(tmp_path): """ Test implementation of a model of antineutrino spectra with free parameters in exponential parametrization. """ _enu = np.linspace(1.8, 8.0, 500, dtype='d') Enu = C.Points(_enu, labels='anue energy') indices = [ ('i', 'isotope', ['U235', 'U238', 'Pu239', 'Pu241']) ] expr = ['anuspec[i,r](enu())'] a = Expression_v01(expr, indices = NIndex.fromlist(indices)) a.parse() lib = dict() a.guessname(lib, save=True) ns_anuexpr = env.globalns('anue_expr') cfg = NestedDict( anuspec = NestedDict( bundle = dict(name='reactor_anu_spectra', version='v04'), name = 'anuspec', filename = ['data/reactor_anu_spectra/Huber/Huber_smooth_extrap_{isotope}_13MeV0.01MeVbin.dat', 'data/reactor_anu_spectra/Mueller/Mueller_smooth_extrap_{isotope}_13MeV0.01MeVbin.dat'], # strategy = dict( underflow='constant', overflow='extrapolate' ), varmode='log', varname='anu_weight_{index}', free_params=True, ns_name='spectral_weights', edges = np.concatenate( ( np.arange( 1.8, 8.7, 0.5 ), [ 12.3 ] ) ), ), enu = NestedDict( bundle = NestedDict(name='predefined', version='v01', major=''), name = 'enu', inputs = None, outputs = Enu.single(), ), ) context = ExpressionContext_v01(cfg, ns=ns_anuexpr) a.build(context) ns_anuexpr.printparameters(labels=True) u235_spec = context.outputs.anuspec.U235 u235_spec.plot_vs(Enu.single(), label='default pars') ns_anuexpr['spectral_weights.anu_weight_5'].set(0.3) ns_anuexpr['spectral_weights.anu_weight_7'].set(-0.3) plt.rcParams.update({'font.size': 14}) plt.rcParams.update({'text.usetex': True}) u235_spec.plot_vs(Enu.single(), label='update pars') plt.yscale('log') plt.xlabel(r'$E_{\nu}$, MeV') plt.ylabel('Anue per MeV') plt.legend() plt.title('Antineutrino spectrum') path = os.path.join(str(tmp_path), 'anuspec.png') savefig(path, dpi=300) allure_attach_file(path)
def check_condproduct(function_name, arrays): print('Test ', function_name, len(arrays), ':', sep='') for array in arrays: print(array) print() nprod = len(arrays)-1 truth1=1.0 truth2=1.0 for i, a in enumerate(arrays): truth1*=a if i<nprod: truth2*=a ns = env.globalns(function_name) condition = ns.defparameter('condition', central=1.0, fixed=True) points = [C.Points(array) for array in arrays] with ns: prod = C.ConditionalProduct(nprod, 'condition', outputs=[p.points.points for p in points]) calc1 = prod.single().data().copy() print('Result (1)', condition.value(), calc1, end='\n\n') condition.set(0.0) calc2 = prod.single().data().copy() print('Result (0)', condition.value(), calc2, end='\n\n') assert (calc1==truth1).all() assert (calc2==truth2).all()
def test_jacobian_v01(): ns = env.globalns("test_jacobian_v01") names = ['zero', 'one', 'two', 'three', 'four', 'five'] values = N.arange(len(names), dtype=context.current_precision_short()) jac = C.Jacobian() for name, value in zip(names, values): if value: par = ns.defparameter(name, central=value, relsigma=0.1) else: par = ns.defparameter(name, central=value, sigma=0.1) if name == 'five': break jac.append(par) five = par with ns: va = C.VarArray(names) va.vararray.points >> jac.jacobian.func vars = va.vararray.points.data() print('Python array:', values.shape, values) print('Array:', vars.shape, vars) res = jac.jacobian.jacobian.data() print('Jacobian:', res.shape, res) req = N.eye(len(names), len(names) - 1) assert N.allclose(res, req, atol=1.e-12) t = jac.jacobian tf1 = t.getTaintflag() tf0 = va.vararray.getTaintflag() assert not tf0.tainted() assert not tf1.tainted() assert tf1.frozen() five.set(12.0) assert tf0.tainted() assert not tf1.tainted() assert tf1.frozen() t.unfreeze() assert tf0.tainted() assert tf1.tainted() assert not tf1.frozen() res = jac.jacobian.jacobian.data() assert N.allclose(res, req, atol=1.e-12) assert not tf0.tainted() assert not tf1.tainted() assert tf1.frozen()
def test_offeq_correction_expression(tmp_path): """Same test but build bundle with expressions""" _enu = np.linspace(1., 8.0, 500, dtype='d') Enu = C.Points(_enu, labels='anue energy') indices = [('i', 'isotope', ['U235', 'U238', 'Pu239', 'Pu241']), ('r', 'reactor', ['DB1', 'DB2', 'LA1', 'LA2', 'LA3', 'LA4'])] expr = ['offeq_correction[i,r](enu())'] a = Expression_v01(expr, indices=NIndex.fromlist(indices)) a.parse() lib = dict() a.guessname(lib, save=True) ns_offeq = env.globalns('offeq_expr') cfg = NestedDict( offeq_correction=NestedDict( bundle=dict(name='reactor_offeq_spectra', version='v03', major='ir'), offeq_data= './data/reactor_anu_spectra/Mueller/offeq/mueller_offequilibrium_corr_{isotope}.dat', ), enu=NestedDict( bundle=NestedDict(name='predefined', version='v01', major=''), name='enu', inputs=None, outputs=Enu.single(), ), ) context = ExpressionContext_v01(cfg, ns=ns_offeq) a.build(context) ns_offeq.printparameters(labels=True) fig, ax = plt.subplots() for iso in indices[0][2]: corrected_spectra = context.outputs.offeq_correction[iso]['DB1'].data() ax.plot(Enu.single().data(), corrected_spectra - 1., label=iso) ax.set_title("Offequilibrium correction") ax.legend(loc='best') ax.grid() ax.set_xlabel("Antineutrino energy, MeV") ax.set_ylabel("(Corrected - nominal) / nominal") suffix = 'correction' path = os.path.join(str(tmp_path), suffix + '.png') savefig(path, dpi=300) savefig(path.replace('.png', '.pdf'), dpi=300) allure_attach_file(path) path = os.path.join(str(tmp_path), suffix + '_graph.png') savegraph(context.outputs.offeq_correction['U235']['DB1'], path) allure_attach_file(path)
def test_offeq_correction(tmp_path): """ Test implementation of off-equilibrium contribution to antineutrino spectra from nuclear fission in commercial reactors. The reason that contribution appears is the fact the electron conversion spectra measured at ILL doesn't take into account long-lived isotopes. That correction is required for precision measurement of oscillation parameters and is uncorrelated between reactors. It comes in a form of ratio of true spectra to nominal Huber-Mueller. """ _enu = np.linspace(1., 8.0, 500, dtype='d') Enu = C.Points(_enu, labels='anue energy') indices = [('i', 'isotope', ['U235', 'U238', 'Pu239', 'Pu241']), ('r', 'reactor', ['DB1', 'DB2', 'LA1', 'LA2', 'LA3', 'LA4'])] cfg = NestedDict( bundle=dict(name='reactor_offeq_spectra', version='v03', nidx=indices), offeq_data= './data/reactor_anu_spectra/Mueller/offeq/mueller_offequilibrium_corr_{isotope}.dat', ) ns = env.globalns('offeq') offeq, = execute_bundles(cfg, namespace=ns) ns.printparameters(labels=True) for iso in offeq.context.inputs.offeq_correction.values(): try: for _input in iso.values(): Enu >> _input.values() except AttributeError: Enu >> iso fig, ax = plt.subplots() for iso in indices[0][2]: corrected_spectra = offeq.context.outputs.offeq_correction[iso][ 'DB1'].data() ax.plot(Enu.single().data(), corrected_spectra - 1., label=iso) ax.set_title("Offequilibrium correction") ax.grid() ax.legend(loc='best') ax.set_xlabel("Antineutrino energy, MeV") ax.set_ylabel("(Corrected - nominal) / nominal") suffix = 'correction' path = os.path.join(str(tmp_path), suffix + '.png') savefig(path, dpi=300) savefig(path.replace('.png', '.pdf'), dpi=300) allure_attach_file(path) path = os.path.join(str(tmp_path), suffix + '_graph.png') savegraph(offeq.context.outputs.offeq_correction['U235']['DB1'], path) allure_attach_file(path)
def test_vararray_preallocated_v01(function_name): ns = env.globalns(function_name) names = ['zero', 'one', 'two', 'three', 'four', 'five'] values = N.arange(len(names), dtype=context.current_precision_short()) variables = R.vector('variable<%s>' % context.current_precision())() with context.allocator(100) as allocator: for name, value in zip(names, values): par = ns.defparameter(name, central=value, relsigma=0.1) variables.push_back(par.getVariable()) with ns: vsum = C.VarSum(names, 'sum', ns=ns) vsum_var = ns['sum'].get() variables.push_back(vsum_var.getVariable()) vprod = C.VarProduct(names, 'product', ns=ns) vprod_var = ns['product'].get() variables.push_back(vprod_var.getVariable()) va = C.VarArrayPreallocated(variables) pool = allocator.view() res = va.vararray.points.data() values_all = N.zeros(shape=values.size + 2, dtype=values.dtype) values_all[:-2] = values values_all[-2] = values_all[:-2].sum() values_all[-1] = values_all[:-2].prod() print('Python array:', values_all) print('VarArray (preallocated):', res) print('Pool:', pool) assert (values_all == res).all() assert (values_all == pool).all() assert (res == pool).all() for i, (val, name) in enumerate(enumerate(names, 2)): ns[name].set(val) values_all[i] = val values_all[-2] = values_all[:-2].sum() values_all[-1] = values_all[:-2].prod() res = va.vararray.points.data() print('Iteration', i) print(' Python array:', values_all) print(' VarArray (preallocated):', res) assert (values_all == res).all() assert (values_all == pool).all() assert (res == pool).all()
def test_snf_spectrum_expression(tmp_path): """Test for new version of Daya Bay fast neutron background bundle, updated for new integrators""" indices = [ ('s', 'site', ['EH1', 'EH2', 'EH3']), ('d', 'detector', ['AD11', 'AD12', 'AD21', 'AD22', 'AD31', 'AD32', 'AD33', 'AD34'], dict(short='s', name='site', map=OrderedDict([('EH1', ('AD11', 'AD12')), ('EH2', ('AD21', 'AD22')), ('EH3', ('AD31', 'AD32', 'AD33', 'AD34'))]))), ] expr = ['evis_edges()', 'fastn_shape[s]', 'bkg_spectrum_fastn[s]()' ] a = Expression_v01(expr, indices = NIndex.fromlist(indices)) a.parse() lib = dict() a.guessname(lib, save=True) ns = env.globalns('fastn') cfg = NestedDict( integral = NestedDict( bundle = dict(name='integral_2d1d', version='v03'), variables = ('evis', 'ctheta'), edges = np.linspace(0.0, 12.0, 241, dtype='d'), xorders = 4, yorder = 2, ), bkg_spectrum_fastn=NestedDict( bundle=dict(name='dayabay_fastn_power', version='v02', major='s'), parameter='fastn_shape', name='bkg_spectrum_fastn', normalize=(0.7, 12.0), bins='evis_edges', order=2, ), fastn_shape=NestedDict( bundle = dict(name="parameters", version = "v01"), parameter='fastn_shape', label='Fast neutron shape parameter for {site}', pars=uncertaindict( [ ('EH1', (67.79, 0.1132)), ('EH2', (58.30, 0.0817)), ('EH3', (68.02, 0.0997)) ], mode='relative', ), ), ) context = ExpressionContext_v01(cfg, ns=ns) a.build(context)
def test_variable_allocation_complex(): from gna.env import env gns = env.globalns('test_variable_allocation_complex') ndata = 200 allocator = R.arrayviewAllocatorSimple('double')(ndata) with context.allocator(allocator): ns = gns('namespace1') ns.defparameter('float1', central=1, fixed=True, label='Float variable 1') ns.defparameter('float2', central=2, fixed=True, label='Float variable 2') ns = gns('namespace2') ns.defparameter('angle', central=3, label='Angle parameter', type='uniformangle') ns.defparameter('discrete', default='a', label='Discrete parameter', type='discrete', variants=OrderedDict([('a', 10.0), ('b', 20.0), ('c', 30.0)])) ns = gns('namespace3') from gna.parameters.oscillation import reqparameters reqparameters(ns) with ns: ns.materializeexpressions() ns['Delta'].set(N.pi * 1.5) gns.printparameters(labels=True) allocdata = allocator.view() print('Data (filled):', allocdata) print('Data (all):', allocator.viewall()) for name, par in gns.walknames(): av = par.getVariable().values() offset = av.offset() size = av.size() avview = av.view() assert (allocdata[offset:offset + size] == avview).all()
def gpuargs_make(nsname, mat1, mat2): from gna.env import env ns = env.globalns(nsname) ns.reqparameter('par1', central=1.0, fixed=True, label='Dummy parameter 1') ns.reqparameter('par2', central=1.5, fixed=True, label='Dummy parameter 2') ns.reqparameter('par3', central=1.01e5, fixed=True, label='Dummy parameter 3') ns.printparameters(labels=True) points1, points2 = C.Points(mat1), C.Points(mat2) with ns: dummy = C.Dummy(4, "dummy", ['par1', 'par2', 'par3']) return dummy, points1, points2, ns
def test_variable_allocation2(function_name): from gna.env import env gns = env.globalns(function_name) ndata = 10 with context.allocator(ndata) as allocator: ns = gns('namespace1') ns.defparameter('float1', central=1, fixed=True, label='Float variable 1') ns.defparameter('float2', central=2, fixed=True, label='Float variable 2') ns = gns('namespace2') ns.defparameter('angle', central=3, label='Angle parameter', type='uniformangle') ns.defparameter('discrete', default='a', label='Discrete parameter', type='discrete', variants=OrderedDict([('a', 10.0), ('b', 20.0), ('c', 30.0)])) ns = gns('namespace3') ns.defparameter('float3', central=100, fixed=True, label='Float variable 3 (independent)') ns.defparameter('float4', central=200, fixed=True, label='Float variable 4 (independent)') gns.printparameters(labels=True) print('Data (filled):', allocator.view()) print('Data (all):', allocator.viewall())
def polyratio_prepare(nsname, nominator, denominator): inp = N.arange(1, 11, dtype='d') x = C.Points(inp) ns = env.globalns(nsname) nominator_names = [] denominator_names = [] nominator_exp = 0.0 if nominator else 1.0 cpower = 1.0 for i, nom in enumerate(nominator): name = 'nom%i' % i ns.defparameter(name, central=nom, fixed=True) nominator_names.append(name) nominator_exp += nom * cpower cpower *= inp denominator_exp = 0.0 if denominator else 1.0 cpower = 1.0 for i, denom in enumerate(denominator): name = 'denom%i' % i ns.defparameter(name, central=denom, fixed=True) denominator_names.append(name) denominator_exp += denom * cpower cpower *= inp with ns: pratio = C.PolyRatio(nominator_names, denominator_names) x >> pratio.polyratio.points res = pratio.polyratio.ratio.data() res_exp = nominator_exp / denominator_exp print('Nominator weights', nominator) print('Denominator weights', denominator) print('Result', res) print('Expected', res_exp) print() assert (N.allclose(res, res_exp))
def test_filllike_v01(function_name='test_filllike_v01'): """Initialize inpnuts""" size = 5 arr1 = N.arange(0, size) """Initialize environment""" ns = env.globalns(function_name) p1 = ns.defparameter('w1', central=1.0, sigma=0.1) points1 = C.Points(arr1) with ns: ws = C.WeightedSum(['w1'], [points1.points.points]) ws.print() print() dbg1 = R.DebugTransformation('wsum') dbg1.debug.source(ws.sum.sum) flvalue = 2.0 fl = C.FillLike(flvalue) fl.fill.inputs[0](dbg1.debug.target) dbg2 = R.DebugTransformation('fill') dbg2.debug.source(fl.fill.outputs[0]) data = dbg2.debug.target.data() print('data:', data) print() compare_filllike(data, [flvalue] * size, 'Data output failed') print('Change parameter') p1.set(-1.0) taintflag = dbg2.debug.tainted() data = dbg2.debug.target.data() print('data:', data) print('taintflag:', taintflag) compare_filllike(data, [flvalue] * size, 'Data output failed') compare_filllike(taintflag, False, 'Taintflag should be false')
def test_vararray_v01(function_name): ns = env.globalns(function_name) names = ['zero', 'one', 'two', 'three', 'four', 'five'] values = N.arange(len(names), dtype=context.current_precision_short()) for name, value in zip(names, values): ns.defparameter(name, central=value, relsigma=0.1) with ns: va = C.VarArray(names) res = va.vararray.points.data() print('Python array:', values) print('Array:', res) assert N.allclose(values, res) for i, (val, name) in enumerate(enumerate(names, 2)): ns[name].set(val) values[i] = val res = va.vararray.points.data() assert N.allclose(values, res)
def test_object_variables(function_name): ns = env.globalns(function_name) names = [ 'zero', 'one', 'two', 'three', 'four', 'five' ] values = N.arange(len(names), dtype=context.current_precision_short()) for name, value in zip(names, values): ns.defparameter(name, central=value, relsigma=0.1) with ns: va = C.VarArray(names) for i, (name, val_true) in enumerate(zip(names, values)): var = va.variables[i].getVariable() assert name==var.name() val = var.cast().value() assert val==val_true for i, (name, val_true) in enumerate(zip(names, values)): ns[name].set(val_true) var=va.variables[i].getVariable() val=var.cast().value() assert val==val_true
def test_geoneutrino_spectrum_v01(tmp_path): _enu = np.arange(1., 8.0+1.e-6, 0.01, dtype='d') Enu = C.Points(_enu, labels='anue energy') cfg = NestedDict( bundle = dict(name='geoneutrino_spectrum', version='v01'), data = 'data/data-common/geo-neutrino/2006-sanshiro/geoneutrino-luminosity_{isotope}_truncated.knt' ) ns = env.globalns('geonu') geonu, = execute_bundles(cfg, namespace=ns) ns.printparameters(labels=True) Enu >> geonu.context.inputs.values(nested=True) # Dump some info print(geonu.context.inputs) print(geonu.context.outputs) geonu.interp.values()[0].printtransformations() geonu.interp.values()[1].printtransformations() # Plot figures and graphs fig = plt.figure() ax = plt.subplot(111, xlabel=r'$E_{\nu}$, MeV', ylabel='N/MeV/s', title='Geo-neutrino luminosity (truncated at 1.7 MeV)') ax.minorticks_on() ax.grid() for k, v in geonu.context.outputs.items(): ax.plot(_enu, v.data(), label=k) ax.legend() plt.show() savefig(os.path.join(str(tmp_path), '_spectra.png')) savegraph(Enu, os.path.join(str(tmp_path), '_graph.png')) ns.printparameters(labels=True)
def test_arrsum(function_name): varname = 'out' ns = env.globalns(function_name) names = ["var1", "var2", "var3", "var4"] variables = [ ns.reqparameter(name, central=float(i), relsigma=0.1) for i, name in enumerate(names) ] with ns: var_arr = C.VarArray(names) print("Input var array ", var_arr.vararray.points.data()) sum_arr = C.ArraySum(varname, var_arr, ns=ns) # materialize variable ns[varname].get() output = var_arr.vararray.points print('Data:', output.data(), output.data().sum()) print("Value of %s evaluable immediately after initialization " % varname, ns[varname].value(), sum_arr.arrsum.sum.data()) print() assert (output.data().sum() == ns[varname].value()).all() # sum_arr.arrsum.arr(var_arr.vararray) # sum_arr.exposeEvaluable(var_arr.vararray) # print(sum_arr.arrsum.accumulated.data()) print("Change value of var1 variable to 10") ns['var1'].set(10) print('Data:', output.data(), output.data().sum()) ns[varname].dump() print("Sum should now be ", np.sum(var_arr.vararray.points.data())) print("Check the value %s of evaluable now: " % varname, ns['out'].value(), sum_arr.arrsum.sum.data()) assert (output.data().sum() == ns[varname].value()).all() print() ns.printparameters()
def normalizedconvolution_prepare(fcn, weights, scale=1.0, nsname=None): fcn = N.ascontiguousarray(fcn, dtype=context.current_precision_short()) weights = N.ascontiguousarray(weights, dtype=context.current_precision_short()) fcn_p, weights_p = C.Points(fcn), C.Points(weights) if nsname is not None: ns = env.globalns(nsname) ns.defparameter('scale', central=scale, fixed=True) with ns: nc = C.NormalizedConvolution('scale') else: scale = 1.0 nc = C.NormalizedConvolution() fcn_p >> nc.normconvolution.fcn weights_p >> nc.normconvolution.weights res = nc.normconvolution.result.data() prod = nc.normconvolution.product.data() product_e = fcn * weights res_e = scale * product_e.sum() / weights.sum() print('Scale', scale) print('Function', fcn) print('Weights', weights) print('Product', prod) print('Product expected', product_e) print('Result', res) print('Result expected', res_e) assert (prod == product_e).all() assert (res == res_e).all() print()
from load import ROOT as R import numpy as N from gna.configurator import NestedDict, uncertain, uncertaindict from gna.bundle import execute_bundles from gna.env import env, findname from matplotlib import pyplot as P from mpl_tools.helpers import plot_hist, plot_bar from collections import OrderedDict from argparse import ArgumentParser parser = ArgumentParser() parser.add_argument( '-s', '--set', nargs=2, action='append', help='set parameter' ) parser.add_argument( '-p', '--pack', action='store_true', help='pack same site histograms' ) opts = parser.parse_args() storage = env.globalns('storage') cfg = NestedDict() cfg.filename = 'output/sample_hists.root' cfg.detectors = [ 'D1', 'D2', 'D3', 'D4' ] cfg.groups=NestedDict( exp = { 'testexp': cfg.detectors }, det = { d: (d,) for d in cfg.detectors }, site = NestedDict([ ('G1', ['D1', 'D2']), ('G2', ['D3']), ('G3', ['D4']) ]) ) bkg = cfg('bkg')
# Plot commands # plot_projections() savefig(opts.output, suffix='_projections') plot_projections(relative=True) savefig(opts.output, suffix='_projections_rel') plotmat() savefig(opts.output, suffix='_matrix') plothist() savefig(opts.output, suffix='_hist') # Check impact of the parameters pars = env.globalns('lsnl_weight').items() plot_projections(relative=True) for name, par in pars: if name == 'nominal': continue par.set(1.0) plot_projections(update=True, label=' ' + name, relative='True') par.set(0.0) savefig(opts.output, suffix='_curves_rel') checktaint() if opts.show: plt.show()
def main(opts): global savefig cfg = NestedDict( bundle=dict( name='energy_nonlinearity_birks_cherenkov', version='v01', nidx=[('r', 'reference', ['R1', 'R2'])], major=[], ), stopping_power='stoppingpower.txt', annihilation_electrons=dict( file='input/hgamma2e.root', histogram='hgamma2e_1KeV', scale=1.0 / 50000 # event 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", (12.0, '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) ns.printparameters(labels=True) print() normE = ns['normalizationEnergy'].value() # # Input bins # evis_edges_full_input = N.arange(0.0, 15.0 + 1.e-6, 0.025) 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 # from scipy.interpolate import interp1d 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') # # Energy resolution # def eres_sigma_rel(edep): return 0.03 / edep**0.5 def eres_sigma_abs(edep): return 0.03 * edep**0.5 # # Energy offset # from physlib import pc edep_offset = pc.DeltaNP - pc.ElectronMass # # 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 # enu_input = N.arange(1.8, 15.0, 0.001) edep_input = enu_input - edep_offset edep_lsnl = edep_input * lsnl_fcn(edep_input) # Initialize oscillation variables enu = C.Points(enu_input, 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') psur = op_sum.single().data() from scipy.signal import argrelmin, argrelmax psur_minima, = argrelmin(psur) psur_maxima, = argrelmax(psur) def build_extrema(x): data_min_x = (x[psur_minima][:-1] + x[psur_minima][1:]) * 0.5 data_min_y = (x[psur_minima][1:] - x[psur_minima][:-1]) data_max_x = (x[psur_maxima][:-1] + x[psur_maxima][1:]) * 0.5 data_max_y = (x[psur_maxima][1:] - x[psur_maxima][:-1]) data_ext_x = N.vstack([data_max_x, data_min_x]).T.ravel() data_ext_y = N.vstack([data_max_y, data_min_y]).T.ravel() return data_ext_x, data_ext_y psur_ext_x_enu, psur_ext_y_enu = build_extrema(enu_input) psur_ext_x_edep, psur_ext_y_edep = build_extrema(edep_input) psur_ext_x_edep_lsnl, psur_ext_y_edep_lsnl = build_extrema(edep_lsnl) # # Plots and tests # 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): if opts.individual and args and args[0]: savefig_old(*args, **kwargs) pdf.savefig() else: pdf = None pdfpagesfilename = '' pdfpages = None fig = P.figure() ax = P.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel('Edep, MeV') ax.set_ylabel('Evis/Edep') ax.set_title('Positron energy nonlineairty') 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) savefig(opts.output, suffix='_total_relative') fig = P.figure() ax = P.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel('Edep, MeV') ax.set_ylabel(r'$\sigma/E$') ax.set_title('Energy resolution') ax.plot(edep_input, eres_sigma_rel(edep_input), '-') savefig(opts.output, suffix='_eres_rel') fig = P.figure() ax = P.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel('Edep, MeV') ax.set_ylabel(r'$\sigma$') ax.set_title('Energy resolution') ax.plot(edep_input, eres_sigma_abs(edep_input), '-') savefig(opts.output, suffix='_eres_abs') fig = P.figure() ax = P.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel('Enu, MeV') ax.set_ylabel('Psur') ax.set_title('Survival probability') op_sum.single().plot_vs(enu.single(), label='full') ax.plot(enu_input[psur_minima], psur[psur_minima], 'o', markerfacecolor='none', label='minima') ax.plot(enu_input[psur_maxima], psur[psur_maxima], 'o', markerfacecolor='none', label='maxima') savefig(opts.output, suffix='_psur_enu') fig = P.figure() ax = P.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel('Edep, MeV') ax.set_ylabel('Psur') ax.set_title('Survival probability') op_sum.single().plot_vs(edep_input, label='true') op_sum.single().plot_vs(edep_lsnl, label='with LSNL') ax.legend() savefig(opts.output, suffix='_psur_edep') fig = P.figure() ax = P.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel('Enu, MeV') ax.set_ylabel('Dist, MeV') ax.set_title('Nearest peaks distance') ax.plot(psur_ext_x_enu, psur_ext_y_enu, 'o-', markerfacecolor='none') savefig(opts.output, suffix='_dist_enu') fig = P.figure() ax = P.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel('Edep, MeV') ax.set_ylabel('Dist, MeV') ax.set_title('Nearest peaks distance') ax.plot(psur_ext_x_edep, psur_ext_y_edep, '-', markerfacecolor='none', label='true') ax.plot(psur_ext_x_edep_lsnl, psur_ext_y_edep_lsnl, '-', markerfacecolor='none', label='with LSNL') ax.plot(edep_input, eres_sigma_abs(edep_input), '-', markerfacecolor='none', label=r'$\sigma$') ax.legend(loc='upper left') savefig(opts.output, suffix='_dist') fig = P.figure() ax = P.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel('Edep, MeV') ax.set_ylabel(r'Dist/$\sigma$') ax.set_title('Resolution ability') x1, y1 = psur_ext_x_edep, psur_ext_y_edep / eres_sigma_abs(psur_ext_x_edep) x2, y2 = psur_ext_x_edep_lsnl, psur_ext_y_edep_lsnl / eres_sigma_abs( psur_ext_x_edep_lsnl) ax.plot(x1, y1, '-', markerfacecolor='none', label='true') ax.plot(x2, y2, '-', markerfacecolor='none', label='with LSNL') ax.legend(loc='upper left') savefig(opts.output, suffix='_ability') ax.set_xlim(3, 4) ax.set_ylim(5, 8) savefig(opts.output, suffix='_ability_zoom') fig = P.figure() ax = P.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel('Edep, MeV') ax.set_ylabel(r'Dist/$\sigma$') ax.set_title('Resolution ability difference (quenching-true)') y2fcn = interp1d(x2, y2) y2_on_x1 = y2fcn(x1) diff = y2_on_x1 - y1 from scipy.signal import savgol_filter diff = savgol_filter(diff, 21, 3) ax.plot(x1, diff) savefig(opts.output, suffix='_ability_diff') 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: P.show()
def test_reactor_spectrum_unc_v01(tmp_path): _enu = np.linspace(1.8, 10.0, 500, dtype='d') Enu = C.Points(_enu, labels='anue energy') indices = [('i', 'isotope', ['U235', 'U238', 'Pu239', 'Pu241']), ('r', 'reactor', ['DB1', 'LA1'])] expr = ['anuspec[i](enu())', 'offeq_correction[i,r]| enu(), anuspec[i]()'] expr.append("corrected_spectrum[i,r]|enu(), anuspec[i]()") a = Expression_v01(expr, indices=NIndex.fromlist(indices)) a.parse() print(a.expressions) lib = dict() a.guessname(lib, save=True) ns_anuexpr = env.globalns('anue_expr') cfg = NestedDict( anuspec=NestedDict( bundle=dict(name='reactor_anu_spectra', version='v04'), name='anuspec', filename=[ 'data/reactor_anu_spectra/Huber/Huber_smooth_extrap_{isotope}_13MeV0.01MeVbin.dat', 'data/reactor_anu_spectra/Mueller/Mueller_smooth_extrap_{isotope}_13MeV0.01MeVbin.dat' ], varmode='log', varname='anu_weight_{index}', free_params=True, ns_name='spectral_weights', edges=np.concatenate((np.arange(1.8, 8.7, 0.5), [12.3])), ), enu=NestedDict( bundle=NestedDict(name='predefined', version='v01', major=''), name='enu', inputs=None, outputs=Enu.single(), ), corrected_spectrum=NestedDict( bundle=dict(name='huber_mueller_spectra_uncertainty', version='v01', major='ir'), reac_idx='r', iso_idx='i', ns_name='hm_uncertainties', files_corr=[ 'output/anue_spectra/Huber_corrrelated_unc_extrap_{isotope}_13.0_0.05_MeV.txt', 'output/anue_spectra/Mueller_corrrelated_unc_extrap_{isotope}_13.0_0.05_MeV.txt' ], files_uncorr=[ 'output/anue_spectra/Huber_uncorrrelated_unc_extrap_{isotope}_13.0_0.05_MeV.txt', 'output/anue_spectra/Mueller_uncorrrelated_unc_extrap_{isotope}_13.0_0.05_MeV.txt' ], ), offeq_correction=NestedDict( bundle=dict(name='reactor_offeq_spectra', version='v04', major='ir'), offeq_data= './data/reactor_anu_spectra/Mueller/offeq/mueller_offequilibrium_corr_{isotope}.dat', ), ) context = ExpressionContext_v01(cfg, ns=ns_anuexpr) a.build(context) ns_anuexpr.printparameters(labels=True) u235_spec = context.outputs.corrected_spectrum.U235.DB1 suffix = 'hm_uncertainty_corrected_spec' # savefig(path.replace('.png','.pdf'), dpi=300) path = os.path.join(str(tmp_path), suffix + '_graph.png') savegraph(u235_spec, path) allure_attach_file(path) path = os.path.join(str(tmp_path), suffix + '_graph.pdf') savegraph(u235_spec, path) allure_attach_file(path)
def test_offeq_correction_expression(tmp_path): """Tests for new version of offequilibrium correction with snapshoting initial spectrum""" _enu = np.linspace(1., 8.0, 500, dtype='d') Enu = C.Points(_enu, labels='anue energy') indices = [('i', 'isotope', ['U235', 'U238', 'Pu239', 'Pu241']), ('r', 'reactor', ['DB1', 'DB2', 'LA1', 'LA2', 'LA3', 'LA4'])] expr = [ 'enu()', 'anuspec[i](enu())', 'offeq_correction[i,r]| enu(), anuspec[i]()' ] a = Expression_v01(expr, indices=NIndex.fromlist(indices)) a.parse() lib = dict() a.guessname(lib, save=True) ns_offeq = env.globalns('offeq_expr_v04') cfg = NestedDict( offeq_correction=NestedDict( bundle=dict(name='reactor_offeq_spectra', version='v04', major='ir'), offeq_data= './data/reactor_anu_spectra/Mueller/offeq/mueller_offequilibrium_corr_{isotope}.dat', ), enu=NestedDict( bundle=NestedDict(name='predefined', version='v01', major=''), name='enu', inputs=None, outputs=Enu.single(), ), anuspec=NestedDict( bundle=dict(name='reactor_anu_spectra', version='v04'), name='anuspec', filename=[ 'data/reactor_anu_spectra/Huber/Huber_smooth_extrap_{isotope}_13MeV0.01MeVbin.dat', 'data/reactor_anu_spectra/Mueller/Mueller_smooth_extrap_{isotope}_13MeV0.01MeVbin.dat' ], varmode='log', varname='anu_weight_{index}', free_params=True, ns_name='spectral_weights', edges=np.concatenate((np.arange(1.8, 8.7, 0.5), [12.3])), ), ) context = ExpressionContext_v01(cfg, ns=ns_offeq) a.build(context) ns_offeq.printparameters(labels=True) fig, ax = plt.subplots() for iso in indices[0][2]: corrected_spectra = context.outputs.offeq_correction[iso]['DB1'].data() initial_spectra = context.outputs.anuspec[iso].data() ax.plot(Enu.single().data(), (corrected_spectra - initial_spectra) / initial_spectra, label=iso) ax.set_title("Offequilibrium corrected spectra") ax.legend(loc='best') ax.grid() ax.set_xlabel("Antineutrino energy, MeV") ax.set_ylabel("(Corrected - nominal) / nominal") suffix = 'offeq' path = os.path.join(str(tmp_path), suffix + '.png') savefig(path.replace('.png', '.pdf'), dpi=300) allure_attach_file(path) path = os.path.join(str(tmp_path), suffix + '_graph.png') savegraph(context.outputs.offeq_correction['U235']['DB1'], path) allure_attach_file(path)
correlated = NestedDict( bundle = 'reactor_anu_corr_v01', uncname = 'isotopes_corr', parnames = '{isotope}_corr.uncn{index:02d}', uncertainties = ['data/reactor_anu_spectra/Huber/reac_anu_uncertainties_huber_{isotope}_{mode}.dat', 'data/reactor_anu_spectra/Mueller/reac_anu_uncertainties_mueller_{isotope}_{mode}.dat'] ) ) """Init inputs""" points = N.linspace( 0.0, 12.0, 241 ) points_t = C.Points(points) 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( '' )
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()