def build(self):
        from gna.expression.expression_v01 import Expression_v01, ExpressionContext_v01
        from gna.bundle import execute_bundles

        # Initialize the expression and indices
        self.expression = Expression_v01(self.formula, self.nidx)

        # Dump the information
        if self.opts.verbose:
            print(self.expression.expressions_raw)
            print(self.expression.expressions)

        # Parse the expression
        self.expression.parse()
        # The next step is needed to name all the intermediate variables.
        self.expression.guessname(self.lib, save=True)

        if self.opts.verbose>2:
            print('Expression tree:')
            self.expression.dump_all(True)
            print()
        elif self.opts.verbose>1:
            print('Expression tree:')
            self.expression.tree.dump(True)
            print()

        # Put the expression into context
        self.context = ExpressionContext_v01(self.cfg, ns=self.namespace)
        self.expression.build(self.context)
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)
Example #3
0
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)
Example #4
0
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)
Example #5
0
    def build(self):
        from gna.expression.expression_v01 import Expression_v01, ExpressionContext_v01
        self.expression = Expression_v01(self.cfg.expr, self.nidx)

        if self.cfg.verbose:
            print(self.expression.expressions)

        self.expression.parse()
        self.expression.guessname(self.cfg.lib, save=True)

        if self.cfg.verbose > 1:
            self.expression.tree.dump(True)

        self.expr_context = ExpressionContext_v01(self.cfg.bundles,
                                                  ns=self.namespace,
                                                  inputs=self.context.inputs,
                                                  outputs=self.context.outputs)

        self.expression.build(self.expr_context)
Example #6
0
    def build(self):
        # Initialize the expression and indices
        l = list(chain.from_iterable(self.formula.values()))
        self.expression = Expression_v01(l, self.nidx)

        # Dump the information
        if self.opts.verbose:
            print(self.expression.expressions_raw)
            print(self.expression.expressions)

        # Parse the expression
        self.expression.parse()
        # The next step is needed to name all the intermediate variables.
        lib = self.libs
        self.expression.guessname(lib, save=True)

        if self.opts.verbose and self.opts.verbose > 1:
            print('Expression tree:')
            self.expression.tree.dump(True)
            print()

        # Put the expression into context
        self.context = ExpressionContext_v01(self.cfg, ns=self.namespace)
        self.expression.build(self.context)
        self.correlate_escale_and_eff()

        if self.opts.verbose and self.opts.verbose > 1:
            width = 40
            print('Outputs:')
            print(self.context.outputs.__str__(nested=True, width=width))

            print()
            print('Inputs:')
            print(self.context.inputs.__str__(nested=True, width=width))

            print()

        if self.opts.verbose or self.opts.stats:
            print('Parameters:')
            self.stats = dict()
            self.namespace.printparameters(labels=True, stats=self.stats)
Example #7
0
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)
Example #8
0
# Indices and their variants
# Each line has a syntax: 'short name', 'long name', 'index variants'
#
indices = [('n', 'num', ['1', '2']), ('a', 'alph', ['a', 'b']),
           ('z', 'zyx', ['X', 'Y']), ('b', 'bkg', ['b1', 'b2'])]

#
# Lib is a dictionary, that tells how the intermediate expressions should be named
#
#
lib = dict()

# The expression is a forumla
expr = ['res=vara * ta() * tb()']
# In order to parse a formula the Expression needs to know how to iterate over indices
a = Expression(expr, indices=indices)

# Print the expression formula before and after preprocessing
print(a.expressions_raw)
print(a.expressions)

# Parse the formula. The following line will execute the expression as python code and create all the objects, needed for its functioning.
a.parse()
# Assign the names for all the intermediate products, sums, etc.
a.guessname(lib, save=True)
# Print the tree to the terminal
a.tree.dump(True)

print()
# Provide the configuration. The configuration tells the Expression on how to build each variable/output.
cfg = NestedDict(
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)
Example #10
0
from mpl_tools import bindings

R.GNAObject

parser = ArgumentParser()
parser.add_argument('--dot',
                    nargs='+',
                    default=(),
                    help='write graphviz output')
args = parser.parse_args()

indices = [('k', 'kin', ['a', 'b', 'c'])]
lib = dict(scaled_e=dict(expr='weight*evis'), )

expr = 'integral[k]'
a = Expression_v01(expr, indices)

print(a.expressions_raw)
print(a.expressions)

a.parse()
a.guessname(lib, save=True)
a.tree.dump(True)

print()
cfg = NestedDict(kinint=NestedDict(bundle=dict(name='integral_1d_v02'),
                                   variable='evis',
                                   edges=N.linspace(0.0, 12.0, 241, dtype='d'),
                                   orders=3,
                                   labels=dict(
                                       sampler='GL Sampler',