def test_MatrixProductDVDt(n1, n2, sym, tmp_path): '''Matrix product of D V Dt''' size1, size2 = n1 * n2, n2 * n2 left_n = np.arange(size1, dtype='d').reshape(n1, n2) + 1.0 square_n = 40.0 + size2 - np.arange(size2, dtype='d').reshape(n2, n2) if sym: square_n = 0.5 * (square_n + square_n.T) left = C.Points(left_n) square = C.Points(square_n) dvdt = R.MatrixProductDVDt(left, square) result = dvdt.product.product() check = np.matmul(np.matmul(left_n, square_n), left_n.transpose()) diff = np.fabs(result - check) # print('Max diff ({}, {})'.format(n1, n2), diff.max()) assert (result == check).all() if sym: checksym = result - result.T assert not checksym.any() path = os.path.join(str(tmp_path), 'graph_{:d}_{:d}.png'.format(n1, n2)) savegraph(dvdt.product, path, verbose=False) allure_attach_file(path)
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_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_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)
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_interpolators(interpolator, strategy, tmp_path): '''Test various interpolators''' segments = np.arange(1.0, 10.1, 1.5, dtype='d') segments_t = C.Points(segments) points = np.stack([ np.linspace(0.0 + i, 12. + i, 61, dtype='d') for i in [0, -0.1, 0.1, 0.3, 0.5] ]).T points_t = C.Points(points) fcn = np.exp(-(segments - segments[0]) * 0.5) fcn = np.exp(segments**(-0.5)) fcn_t = C.Points(fcn) ie = interpolator() ie.set_underflow_strategy(strategy) ie.set_overflow_strategy(strategy) ie.interpolate(segments_t, fcn_t, points_t) seg_idx = ie.insegment.insegment.data() res = ie.interp.interp.data() fig = plt.figure() ax = plt.subplot(111) ax.minorticks_on() ax.grid() ax.set_xlabel('x') ax.set_ylabel('y') strategy_name = "Constant" if strategy is R.GNA.Interpolation.Strategy.Constant else "Extrapolate" title = ie.__class__.__name__.strip( 'Interp') + ", " + "Strategy: {}".format(strategy_name) ax.set_title(title) ax.plot(segments, fcn, 'o-', markerfacecolor='none', label='coarse function', linewidth=0.5, alpha=0.5) markers = 'os*^vh' for i, (p, r) in enumerate(zip(points.T, res.T)): ax.plot(p, r, '.', label='interpolation, col %i' % i, marker=markers[i], markersize=1.5) break ax.legend(loc='upper right') suffix = '{}_{}'.format(ie.__class__.__name__, strategy_name) 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(points_t.points, path, verbose=False) allure_attach_file(path) plt.close()