Ejemplo n.º 1
0
def test_update_params():
    # We need some lines
    L1 = LorentzianLine("Lorentzian1", (-5.0, 5.0),
                        x0=0.0,
                        width=0.4,
                        c=0.0,
                        weight=2)
    L2 = LorentzianLine(name="Lorentzian2",
                        domain=(-5.0, 5.0),
                        x0=-1.0,
                        width=0.4,
                        c=0.02,
                        weight=1)
    # Contruct a SqE model
    sqe = SqE(lines=(L1, L2), lam=6.0, dlam=0.12, lSD=3.43, T=20)
    # init energycutoff
    decf = DetectorEfficiencyCorrectionFactor(sqe, ne=10000, nlam=20)
    pprint(decf.export_to_dict())
    tdict = dict(T=30,
                 lam=8.0,
                 x0_Lorentzian2=-0.5,
                 weight_Lorentzian1=5,
                 nlam=55)
    decf.update_params(**tdict)
    pprint(decf.export_to_dict())
Ejemplo n.º 2
0
def test_export_load():
    # We need some lines
    L1 = LorentzianLine("Lorentzian1", (-5.0, 5.0),
                        x0=0.0,
                        width=0.4,
                        c=0.0,
                        weight=2)
    L2 = LorentzianLine(name="Lorentzian2",
                        domain=(-5.0, 5.0),
                        x0=-1.0,
                        width=0.4,
                        c=0.02,
                        weight=1)
    # Contruct a SqE model
    sqe = SqE(lines=(L1, L2), lam=6.0, dlam=0.12, lSD=3.43, T=20)
    new_domain = (-1 * energy_from_lambda(6.0), UPPER_INTEGRATION_LIMIT)
    sqe.update_domain(new_domain)
    # init energycutoff
    decf = DetectorEfficiencyCorrectionFactor(sqe, ne=10000, nlam=20)

    # exports
    corrdict = decf.export_to_dict()
    decf.export_to_jsonfile(
        f"{testdir}/resources/test_correction_export_load_file.json")

    # loading
    decf_from_dict = decf.load_from_dict(**corrdict)
    print(decf_from_dict.export_to_dict())
    print("", "Loading successful: ", decf, decf_from_dict, sep='\n')
    decf_from_jsonfile = decf.load_from_jsonfile(
        f"{testdir}/resources/test_correction_export_load_file.json")
    print("Loading successful: ", decf, decf_from_jsonfile, sep='\n')
Ejemplo n.º 3
0
def test_DetectorEfficiency_cancel():
    # We need some lines
    L1 = LorentzianLine("Lorentzian1", (-5.0, 5.0),
                        x0=0.0,
                        width=0.4,
                        c=0.0,
                        weight=2)
    L2 = LorentzianLine(name="Lorentzian2",
                        domain=(-5.0, 5.0),
                        x0=-1.0,
                        width=0.4,
                        c=0.02,
                        weight=1)
    # Contruct a SqE model
    sqe = SqE(lines=(L1, L2), lam=6.0, dlam=0.12, lSD=3.43, T=20)
    new_domain = (-1 * energy_from_lambda(6.0), UPPER_INTEGRATION_LIMIT)
    sqe.update_domain(new_domain)
    # init energycutoff
    decf = DetectorEfficiencyCorrectionFactor(sqe, ne=10, nlam=5)

    ee, ll = energy_lambda_nrange(15.0, 6.0, 0.12, 10000, 20)

    #    print(detector_efficiency(ee, ll, 1) * decf(ee, ll))
    print(
        trapz(trapz(decf(ee, ll) * decf.legacy_calc(ee, ll, 0), ee, axis=0),
              ll[0]))
    #    print(trapz(trapz(ones(ll.shape), ee, axis=0), ll[0]))
    print(trapz(trapz(decf.legacy_calc(ee, ll, 1), ee, axis=0), ll[0]))
    print(trapz(trapz(decf.legacy_calc(ee, ll, 0), ee, axis=0), ll[0]))
    print(
        trapz(
            trapz(decf.legacy_calc(ee, ll, 0), ee, axis=0) /
            trapz(trapz(decf.legacy_calc(ee, ll, 1), ee, axis=0), ll[0]),
            ll[0]))
Ejemplo n.º 4
0
def test_transformer_init():
    ### Creating a SqE model for transformation
    # We need some lines
    L1 = LorentzianLine("Lorentzian1", (-5.0, 5.0),
                        x0=0.0,
                        width=0.4,
                        c=0.0,
                        weight=2)
    L2 = LorentzianLine(name="Lorentzian2",
                        domain=(-5.0, 5.0),
                        x0=-1.0,
                        width=0.4,
                        c=0.0,
                        weight=1)
    L3 = F_ILine("FI1", (-energy_from_lambda(6.0), 15),
                 x0=-0.1,
                 width=0.008,
                 A=350.0,
                 q=0.02,
                 kappa=0.01,
                 c=0.0,
                 weight=1)
    # Contruct a SqE model
    sqe1 = SqE(lines=(L2, ), lam=6.0, dlam=0.12, lSD=3.43, T=20)
    sqe2 = SqE(lines=(L1, L2, L3), lam=6.0, dlam=0.12, lSD=3.43, T=20)

    ### Instantiate a transformer
    SqtTransformer(sqe2, nlam=20, ne=10000)
Ejemplo n.º 5
0
def test_sqe_normalization():
    # We need some lines
    L1 = LorentzianLine("Lorentzian1", (-5.0, 5.0),
                        x0=0.0,
                        width=0.4,
                        c=0.0,
                        weight=2)
    L2 = LorentzianLine(name="Lorentzian2",
                        domain=(-5.0, 5.0),
                        x0=-1.0,
                        width=0.4,
                        c=0.02,
                        weight=1)
    # Contruct a SqE model
    sqe = SqE(lines=(L1, L2), lam=6.0, dlam=0.12, l_SD=3.43, T=20)
    new_domain = (-1 * energy_from_lambda(6.0), UPPER_INTEGRATION_LIMIT)
    sqe.update_domain(new_domain)

    # integrate over domain
    from scipy.integrate import quad

    to_integrate = lambda x: sqe(x)
    valdom, errdom = quad(to_integrate, *new_domain)
    valover, errover = quad(to_integrate, -15, 20)
    print(
        f"Integration value over domain from {new_domain[0]:.5f} to {UPPER_INTEGRATION_LIMIT}: {valdom:.5f} +- {errdom:.5f}"
    )  #   |   normalization factor: {n:.5f}")
    print(
        f"Integration value beyond domain from -15.0 to 20.0: {valover:.5f} +- {errover:.5f}"
    )
Ejemplo n.º 6
0
def test_get_param_names():
    L1 = LorentzianLine(name="Lorentzian",
                        domain=(-5.0, 5.0),
                        x0=-0.5,
                        width=0.4,
                        c=0.2)
    F_c1 = F_cLine(name="Fc1", domain=(-5.0, 5.0), x0=-0.0, width=0.4, c=0.2)
    print(L1.get_param_names())
    print(F_c1.get_param_names())
Ejemplo n.º 7
0
def test_Lorentzian_normalization():
    Lorentzian = LorentzianLine("Lorentzian1", (-5., 5), x0=0.0, width=0.5, c=0.0)
    n = Lorentzian.normalize()

    from scipy.integrate import quad
    to_integrate = lambda x: Lorentzian(x)
    val, err = quad(to_integrate, min(Lorentzian.domain), max(Lorentzian.domain))
    print(f"Integration value: {val:.5f} +- {err:.5f}   |   normalization factor: {n:.5f}")
    print(f"Normalized Line area: {val*n}")
Ejemplo n.º 8
0
def test_domain_enforcement():
    Lorentzian = LorentzianLine("Lorentzian1", (-5., 5), x0=-0.3, width=0.5, c=0.2)

    e_in_domain = np.linspace(-5.0, 5.0, 11)
    e_beyond_domain = np.linspace(-10.0, 10.0, 21)

    p1 = Lorentzian(e_in_domain)
    p2 = Lorentzian(e_beyond_domain)

    assert np.all(p1 == p2[Lorentzian.within_domain(e_beyond_domain)])
Ejemplo n.º 9
0
def test_domain_enforcement_visually():

    Lorentzian_short = LorentzianLine("Lorentzian_short", (-energy_from_lambda(6.0 * 0.88), 15), x0=-0.3, width=0.5, c=0.2)
    Lorentzian_mid = LorentzianLine("Lorentzian_mid", (-energy_from_lambda(6.0), 15), x0=-0.3, width=0.5, c=0.2)
    Lorentzian_long = LorentzianLine("Lorentzian_long", (-energy_from_lambda(6.0 * 1.12), 15), x0=-0.3, width=0.5, c=0.2)

    e = np.linspace(-10.0, 20.0, 1201)

    plt.plot(e, Lorentzian_short(e))
    plt.plot(e, Lorentzian_mid(e), ls="--", lw=2.0)
    plt.plot(e, Lorentzian_long(e), ls="dotted", lw=4.0)
Ejemplo n.º 10
0
def test_transformer_init():
    ### Creating a SqE model for transformation
    # We need some lines
    L1 = LorentzianLine("Lorentzian1", (-5.0, 5.0), x0=0.0, width=0.4, c=0.0, weight=2)
    L2 = LorentzianLine(name="Lorentzian2", domain=(-5.0, 5.0), x0=-1.0, width=0.4, c=0.0, weight=1)
    # Contruct a SqE model
    sqe1 = SqE(lines=(L2,), lam=6.0, dlam=0.12, l_SD=3.43, T=20)
    SqE(lines=(L1, L2), lam=6.0, dlam=0.12, l_SD=3.43, T=20)

    ### Instantiate a transformer
    SqtTransformer(sqe1, n_lam=20, n_e=10000)
Ejemplo n.º 11
0
def test_from_Minuit_and_adjust_it():
    ### Creating a SqE model for transformation
    # We need some lines
    L1 = LorentzianLine("Lorentzian1", (-5.0, 5.0),
                        x0=0.0,
                        width=0.4,
                        c=0.0,
                        weight=1)
    L2 = LorentzianLine(name="Lorentzian2",
                        domain=(-5.0, 5.0),
                        x0=1.5,
                        width=0.4,
                        c=0.0,
                        weight=2)
    # Contruct a SqE model
    sqe1 = SqE((L1, L2), lam=6.0, dlam=0.12, lSD=3.43, T=20)

    ### Instantiate a transformer
    sqt1 = SqtTransformer(
        sqe1,
        corrections=(
        ),  #(DetectorEfficiencyCorrectionFactor(sqe1, ne=15000, nlam=20),),
        ne=10000,
        nlam=20,
        lSD=3.43)

    ### Values for transformation
    datataus = array([
        2.0e-6, 6.0e-6, 4.1e-5, 2.5e-4, 5.5e-4, 1.0e-3, 1.32e-3, 1.7e-3,
        2.2e-3, 2.63e-3, 2.77e-3, 3.3e-3, 4.27e-3, 5.11e-3, 6.77e-3, 8.96e-3,
        2.0e-2
    ])
    x = MIEZE_DeltaFreq_from_time(datataus * 1.0e-9, 3.43, 6.0)

    ### create artificial data via TRANSFORM!!
    y = array([sqt1(freq) for freq in x])
    yerr = 0.02 * random.randn(len(y))

    ### initialize Minuits!
    m = FitModelCreator.from_transformer(sqt1, x, y + yerr, yerr,
                                         [0.45, 0.0, 1.2, -1.5, 0.3, 0.0, 2.2])
    m.fixed["c_Lorentzian1"] = True
    m.fixed["c_Lorentzian2"] = True

    # Adjust fit behavior -> reinitialize another Minuit obj
    m2 = FitModelCreator.from_Minuit(minuitobj=m,
                                     limit_weight_Lorentzian1=(0, None),
                                     limit_width_Lorentzian1=(0, None),
                                     limit_weight_Lorentzian2=(0, None),
                                     limit_width_Lorentzian2=(0, None),
                                     fix_c_Lorentzian1=True,
                                     fix_c_Lorentzian2=True)
Ejemplo n.º 12
0
def test_from_transformer():
    ### Creating a SqE model for transformation
    # We need some lines
    L1 = LorentzianLine("Lorentzian1", (-5.0, 5.0),
                        x0=0.0,
                        width=0.4,
                        c=0.0,
                        weight=1)
    L2 = LorentzianLine(name="Lorentzian2",
                        domain=(-5.0, 5.0),
                        x0=1.5,
                        width=0.4,
                        c=0.0,
                        weight=2)
    L3 = F_ILine("FI1", (-energy_from_lambda(6.0), 15),
                 x0=-0.1,
                 width=0.008,
                 A=350.0,
                 q=0.02,
                 kappa=0.01,
                 c=0.0,
                 weight=1)
    # Contruct a SqE model
    sqe1 = SqE((L1, L2, L3), lam=6.0, dlam=0.12, lSD=3.43, T=20)

    ### Instantiate a transformer
    sqt1 = SqtTransformer(sqe1,
                          corrections=(DetectorEfficiencyCorrectionFactor(
                              sqe1, ne=10000, nlam=20), ),
                          ne=10000,
                          nlam=20,
                          lSD=3.43)

    ### Values for transformation
    datataus = array([
        2.0e-6, 6.0e-6, 4.1e-5, 2.5e-4, 5.5e-4, 1.0e-3, 1.32e-3, 1.7e-3,
        2.2e-3, 2.63e-3, 2.77e-3, 3.3e-3, 4.27e-3, 5.11e-3, 6.77e-3, 8.96e-3,
        2.0e-2
    ])
    x = MIEZE_DeltaFreq_from_time(datataus * 1.0e-9, 3.43, 6.0)

    ### create artificial data via TRANSFORM!!
    y = array([sqt1(freq) for freq in x])
    yerr = 0.02 * random.randn(len(y))

    ### instntiate Minuit obj!
    m = FitModelCreator.from_transformer(sqt1, x, y + yerr, yerr, [
        0.45, 0.0, 1.2, -1.5, 0.3, 0.0, 2.2, -0.5, 0.01, 350.0, 0.02, 0.01,
        0.0, 1.0
    ])
Ejemplo n.º 13
0
def test_get_peak_domain_strategy():
    inelstrat = InelasticCalcStrategy(20.0)
    Lwide = LorentzianLine(name="Lwide",
                           domain=(-15.0, 15.0),
                           x0=-0.5,
                           width=0.4,
                           c=0.0)
    Lnarrow = LorentzianLine(name="Lnarrow",
                             domain=(-15.0, 15.0),
                             x0=-0.5,
                             width=0.07,
                             c=0.0)
    print(inelstrat.get_peak_domain(Lwide))
    print(inelstrat.get_peak_domain(Lnarrow))
Ejemplo n.º 14
0
def test_Lines():
    l1 = Line("LineBase", (-15., 15), x0=0, FWHM=0.5)
    # assert isinstance(l1, Line)
    # try:
    #     l1.check_params()
    # except KeyError:
    #     print("KeyError was caught and 'handled' in this test.")

    l2 = LorentzianLine("Lorentzian1", (-15., 15.), x0=0.2, width=0.3)
#    l2.check_params()
    l2.update_line_params(c=0.2)

    e = np.linspace(-1.5, 1.5, 16)
    print("Returned by 'l1.calc': ", l1.calc(e, **l1.line_params))
    print("Returned by 'l2(e)': ", l2(e))
Ejemplo n.º 15
0
def test_update_params():
    ### Creating a SqE model for transformation
    # We need some lines
    L1 = LorentzianLine("Lorentzian1", (-5.0, 5.0),
                        x0=0.0,
                        width=0.4,
                        c=0.0,
                        weight=2)
    L2 = LorentzianLine(name="Lorentzian2",
                        domain=(-5.0, 5.0),
                        x0=-1.0,
                        width=0.4,
                        c=0.0,
                        weight=1)
    L3 = F_ILine("FI1", (-energy_from_lambda(6.0), 15),
                 x0=-0.1,
                 width=0.008,
                 A=350.0,
                 q=0.02,
                 kappa=0.01,
                 c=0.0,
                 weight=1)
    # Contruct a SqE model
    sqe = SqE(lines=(L1, L2, L3), lam=6.0, dlam=0.12, lSD=3.43, T=20)
    # Add the detector efficiency correction
    decf = DetectorEfficiencyCorrectionFactor(sqe, ne=10000, nlam=20)
    # Add the energycutoff correction
    eccf = EnergyCutOffCorrectionFactor(sqe, ne=10000, nlam=20)

    ### Instantiate a transformer
    sqt = SqtTransformer(sqe,
                         corrections=(decf, eccf),
                         nlam=20,
                         ne=10000,
                         lSD=3.43)
    print("\n\nBefore update:")
    pprint(sqt.export_to_dict())
    tdict = dict(T=30,
                 lam=8.0,
                 x0_Lorentzian2=-0.5,
                 width_Lorentzian2=0.025,
                 weight_Lorentzian1=5,
                 nlam=55,
                 kappa_FI1=1.0,
                 some_wired_param=True)
    sqt.update_params(**tdict)
    print("\n\nAfter update:")
    pprint(sqt.export_to_dict())
Ejemplo n.º 16
0
def test_get_adaptive_integration_grid():
    ### Lines
    LL1 = LorentzianLine("LL1", (-energy_from_lambda(6.0), 15),
                         x0=0.048,
                         width=0.04,
                         c=0.0,
                         weight=0.0)
    F_c = F_cLine("F_c1", (-energy_from_lambda(6.0), 15),
                  x0=0.0,
                  width=0.02,
                  A=350.0,
                  q=0.02,
                  c=0.0,
                  weight=1)
    F_I = F_ILine("F_I1", (-energy_from_lambda(6.0), 15),
                  x0=-0.02,
                  width=0.008,
                  A=350.0,
                  q=0.02,
                  kappa=0.01,
                  c=0.0,
                  weight=1)

    ### CalcStrategy
    inel = InelasticCalcStrategy(610)
    quasi = QuasielasticCalcStrategy()
Ejemplo n.º 17
0
def test_correctionFactor_dimensionality():
    # We need some lines
    L1 = LorentzianLine(name="Lorentzian1",
                        domain=(-16.0, 16.0),
                        x0=-1.0,
                        width=0.4,
                        c=0.0,
                        weight=1)
    # Contruct a SqE model
    sqe = SqE(lines=(L1, ), lam=6.0, dlam=0.12, lSD=3.43, T=20)
    # Instantiate a detector efficiency corr factor
    decf = DetectorEfficiencyCorrectionFactor(sqe)
    # Instantiate a energy cutoff corr factor
    eccf = EnergyCutOffCorrectionFactor(sqe)

    ne = 15
    nlam = 5
    lam = 6.0 * linspace(1 - 0.12 * 1.01, 1 + 0.12 * 1.01, nlam)
    lams = tile(lam, (ne, 1))

    a = -0.99999 * energy_from_lambda(lam)
    b = 15.0 + a
    es = linspace(a, b, ne)

    print(lams)
    print(es)
    print(decf.calc(es, lams))
    print(eccf.calc(es, lams))
Ejemplo n.º 18
0
def test_DetectorEfficiencyCorrectionFactor_compare_with_arg():
    # We need some lines
    L1 = LorentzianLine(name="Lorentzian1",
                        domain=(-16.0, 16.0),
                        x0=-1.0,
                        width=0.4,
                        c=0.0,
                        weight=1)
    # Contruct a SqE model
    sqe = SqE(lines=(L1, ), lam=6.0, dlam=0.12, lSD=3.43, T=20)
    # Instantiate a detector efficiency corr factor
    decf = DetectorEfficiencyCorrectionFactor(sqe)

    ne = 15000
    nlam = 20
    lam = 6.0 * linspace(1 - 0.12 * 1.01, 1 + 0.12 * 1.01, nlam)
    lams = tile(lam, (ne, 1))

    a = -0.99999 * energy_from_lambda(lam)
    b = 15.0 + a
    es = linspace(a, b, ne)

    test_decf_val = arg.DetFac_Eint_lamInt(arg.fqe_I, -1.0, 0.4, 15000, 20,
                                           6.0, 0.12, 1, 0.0, 1.0, 20, 0.5,
                                           0.00001, 350.)
    decf_val_man_int = decf(es, lams)
    decf_val = decf.correction(es, lams)
    print("arg res: ", test_decf_val)
    print("class calc res manualy integrated: ", decf_val_man_int)
    print("class res: ", decf_val)
Ejemplo n.º 19
0
def test_EnergyCutOffCorrectionFactor_vs_arg():
    # We need some lines
    L1 = LorentzianLine(name="Lorentzian1",
                        domain=(-16.0, 16.0),
                        x0=-1.0,
                        width=0.4,
                        c=0.0,
                        weight=1)
    # Contruct a SqE model
    sqe = SqE(lines=(L1, ), lam=6.0, dlam=0.12, lSD=3.43, T=20)
    # Instantiate a energy cutoff corr factor
    eccf = EnergyCutOffCorrectionFactor(sqe)

    ne = 15000
    nlam = 20
    lam = 6.0 * linspace(1 - 0.12 * 1.01, 1 + 0.12 * 1.01, nlam)
    lams = tile(lam, (ne, 1))

    a = -0.99999 * energy_from_lambda(lam)
    b = 15.0 + a
    es = linspace(a, b, ne)

    test_eccf_vals = arg.CutFac_Eint(arg.lorentzian, -1.0, 0.4, 15000, 20, 6.0,
                                     0.12, 1, 0.0, 1.0, 20, 0.5, 0.00001, 350.)

    eccf_vals = eccf.correction(es, lams)

    #    print(test_eccf_vals.shape)
    print(test_eccf_vals)

    #    print(eccf_vals.shape)
    print(eccf_vals)
Ejemplo n.º 20
0
def test_export_load():
    ### Creating a SqE model for transformation
    # We need some lines
    L1 = LorentzianLine("Lorentzian1", (-5.0, 5.0),
                        x0=0.0,
                        width=0.00005,
                        c=0.0,
                        weight=0.1)
    L2 = F_ILine("FI1", (-energy_from_lambda(6.0), 15),
                 x0=-0.01,
                 width=0.008,
                 A=350.0,
                 q=0.02,
                 kappa=0.01,
                 c=0.0,
                 weight=0.9)
    # Contruct a SqE model
    sqe = SqE(lines=(L1, L2), lam=6.0, dlam=0.12, lSD=3.43, T=628)
    # Add the detector efficiency correction
    decf = DetectorEfficiencyCorrectionFactor(sqe, ne=500, nlam=20)
    # Add the energycutoff correction
    eccf = EnergyCutOffCorrectionFactor(sqe, ne=500, nlam=20)

    ### Instantiate a transformer
    sqt = SqtTransformer(sqe,
                         corrections=(decf, eccf),
                         nlam=20,
                         ne=500,
                         lSD=3.43,
                         integ_mode="adaptive")

    ### Export
    sqt_dict = sqt.export_to_dict()
    pprint(sqt_dict["corrections"])
    sqt.export_to_jsonfile(
        f"{testdir}/resources/test_transformer_export_load_file.json")
    print("",
          "SqE's of the: ",
          f"- sqe: {sqe}",
          f"- decf: {decf.sqe}",
          f"- eccf: {eccf.sqe}",
          f"- sqt: {sqt.sqemodel}",
          sep="\n")

    ### Loading
    sqt_from_dict = sqt.load_from_dict(**sqt_dict)
    print("", "Loading successful: ", sqt, sqt_from_dict, sep='\n')
    sqt_from_file = sqt.load_from_jsonfile(
        f"{testdir}/resources/test_transformer_export_load_file.json")
    print("Loading successful: ", sqt, sqt_from_file, sep='\n')
    print("",
          "SqE's of the: ",
          f"- sqe: {sqe}",
          f"- decf: {sqt_from_dict.corrections[0].sqe}",
          f"- eccf: {sqt_from_dict.corrections[1].sqe}",
          f"- sqt: {sqt_from_dict.sqemodel}",
          sep="\n")

    print("\n\nThis is the sqt loaded from dict")
    pprint(sqt_from_file.export_to_dict())
Ejemplo n.º 21
0
def test_get_peak_domain():
    L = LorentzianLine(name="Lorentzian",
                       domain=(-15.0, 15.0),
                       x0=-0.0,
                       width=0.4,
                       c=0.2)
    print(f"The peak domain: {L.get_peak_domain()}")

    etotal = np.linspace(*L.domain, num=10000)
    epeak = np.linspace(*L.get_peak_domain(), num=1333)
    e = np.concatenate((np.linspace(L.domain[0],
                                    L.get_peak_domain()[0], 500), epeak,
                        np.linspace(L.get_peak_domain()[1], L.domain[1], 500)))

    print(f"Integral (trapz) over total domain: {np.trapz(L(etotal), etotal)}")
    print(
        f"Integral (trapz) over total (splitted) domain: {np.trapz(L(e), e)}")
    print(f"Integral (trapz) over peak domain: {np.trapz(L(epeak), epeak)}")
Ejemplo n.º 22
0
def test_DetectorEfficiencyCorrectionFactor():
    # We need some lines
    L1 = LorentzianLine("Lorentzian1", (-5.0, 5.0),
                        x0=0.0,
                        width=0.4,
                        c=0.0,
                        weight=2)
    L2 = LorentzianLine(name="Lorentzian2",
                        domain=(-5.0, 5.0),
                        x0=-1.0,
                        width=0.4,
                        c=0.02,
                        weight=1)
    # Contruct a SqE model
    sqe = SqE(lines=(L1, L2), lam=6.0, dlam=0.12, lSD=3.43, T=20)
    # Instantiate a detector efficiency corr factor
    decf = DetectorEfficiencyCorrectionFactor(sqe)

    ne = 10
    nlam = 5
    lam = 6.0 * linspace(1 - 0.12 * 1.01, 1 + 0.12 * 1.01, nlam)
    lams = tile(lam, (ne, 1))

    a = -0.99999 * energy_from_lambda(lam)
    b = 15.0 + a
    es = linspace(a, b, ne)

    deteff = detector_efficiency(es, lams, 1)
    tria = triangle_distribution(lams, 6.0, 0.12)

    print("Triangular wavelenght distr.: ", tria)
    print("Triangular wavelength distr. shape: ", tria.shape)
    print("Det. eff. values: ", deteff)
    print("Det. eff. values shape: :", deteff.shape)

    sqevals = sqe(es)

    print("Manual mult.: ", sqevals * deteff * tria)
    print("Class result: ", decf(es, lams))

    print("Are manual and deteffcorrfac identical?: ",
          all((sqevals * deteff * tria) == decf(es, lams)))
Ejemplo n.º 23
0
def test_Lines():
    l1 = Line("LineBase", (-15., 15), x0=0, FWHM=0.5)
    # assert isinstance(l1, Line)
    # try:
    #     l1.check_params()
    # except KeyError:
    #     print("KeyError was caught and 'handled' in this test.")

    l2 = LorentzianLine("Lorentzian1", (-15., 15.), x0=0.2, width=0.3)
    #    l2.check_params()
    l2.update_line_params(c=0.2)

    l3 = F_cLine("FcLine1", (-15, 15),
                 x0=0.0,
                 width=0.02,
                 A=350.0,
                 q=0.023,
                 c=0.0,
                 weight=1.0)
    l3.update_line_params(width=0.1)
    l3.update_domain((-1.0, 1.0))

    l4 = F_ILine("FILine1", (-15, 15),
                 x0=0.0,
                 width=0.02,
                 A=350.0,
                 q=0.023,
                 kappa=0.01,
                 c=0.0,
                 weight=1.0)
    l4.update_line_params(kappa=0.015)

    e = np.linspace(-1.5, 1.5, 16)
    print("Returned by 'l1.calc': ", l1.calc(e, **l1.line_params))
    print("Returned by 'l2(e)': ", l2(e))
    print("Returned by 'F_cLine': ", l3(e))
    print("Returned by 'F_ILine': ", l4(e))
Ejemplo n.º 24
0
def test_domainenforcement():
    Lorentzian = LorentzianLine("Lorentzian1", (-5., 5),
                                x0=-0.3,
                                width=0.5,
                                c=0.2)
    F_c = F_cLine("FcLine1", (-15, 15),
                  x0=0.0,
                  width=0.02,
                  A=350.0,
                  q=0.023,
                  c=0.0,
                  weight=1.0)

    e_in_domain = np.linspace(-5.0, 5.0, 11)
    e_beyond_domain = np.linspace(-10.0, 10.0, 21)

    pL1 = Lorentzian(e_in_domain)
    pL2 = Lorentzian(e_beyond_domain)

    pF1 = F_c(e_in_domain)
    pF2 = F_c(e_beyond_domain)

    assert np.all(pL1 == pL2[Lorentzian.within_domain(e_beyond_domain)])
    assert np.all(pF1 == pF2[Lorentzian.within_domain(e_beyond_domain)])
Ejemplo n.º 25
0
def test_from_sqe():
    # We need some lines
    L1 = LorentzianLine("Lorentzian1", (-5.0, 5.0),
                        x0=0.0,
                        width=0.4,
                        c=0.0,
                        weight=2)
    L2 = LorentzianLine(name="Lorentzian2",
                        domain=(-5.0, 5.0),
                        x0=-1.0,
                        width=0.4,
                        c=0.0,
                        weight=1)
    L1g = LorentzianLine("Lorentzian1g", (-5.0, 5.0),
                         x0=0.0,
                         width=0.5,
                         c=0.0,
                         weight=1)
    L2g = LorentzianLine(name="Lorentzian2g",
                         domain=(-5.0, 5.0),
                         x0=-1.5,
                         width=0.3,
                         c=0.0,
                         weight=1)
    # Contruct a SqE model
    sqe1 = SqE((L1, L2), lam=6.0, dlam=0.12, lSD=3.43, T=20)
    sqe1g = SqE((L1g, L2g), lam=6.0, dlam=0.12, lSD=3.43, T=20)

    # Create some data and a Minuit obj from sqe
    x = linspace(-5, 5, 26)
    y = sqe1(x) * 1000
    yerr = y**0.5
    y += random.randn(len(x)) * yerr
    m = FitModelCreator.from_sqe(sqe1, x, y / 1000, yerr / 1000,
                                 [0.5, 0.0, 1.7, -1.5, 0.3, 0.0, 1])

    fmin, res = m.migrad()
    # print(fmin)
    # print(res)

    dL1res = {
        "x0": 0.0,
        "width": res[0].value,
        "c": res[1].value,
        "weight": res[2].value
    }
    dL2res = {
        "x0": res[3].value,
        "width": res[4].value,
        "c": res[5].value,
        "weight": res[6].value
    }

    L1res = LorentzianLine(name="Lorentzian1res", domain=(-5.0, 5.0), **dL1res)
    L2res = LorentzianLine(name="Lorentzian2res", domain=(-5.0, 5.0), **dL2res)
    sqe1res = SqE((L1res, L2res), lam=6.0, dlam=0.12, lSD=3.43, T=20)
Ejemplo n.º 26
0
def test_update_line_params():
    L = LorentzianLine(name="Lorentzian",
                       domain=(-5.0, 5.0),
                       x0=-0.5,
                       width=0.4,
                       c=0.2)
    print(L.export_to_dict())
    tdict = dict(x0=1.0, c=0.0)
    L.update_line_params(**tdict)
    print("After update: ", L.export_to_dict())
Ejemplo n.º 27
0
def test_export_load():
    L = LorentzianLine(
        name="Lorentzian",
        domain=(-5.0, 5.0),
        x0=-0.5,
        width=0.4,
        c=0.2
    )
    L.export_to_jsonfile(f"{testdir}/resources/test_export_load_file.json")

    L2 = LorentzianLine.load_from_jsonfile(f"{testdir}/resources/test_export_load_file.json")
    assert L.name == L2.name

    L3 = LorentzianLine.load_from_dict(**L2.export_to_dict())
Ejemplo n.º 28
0
def test_export_load():
    L = LorentzianLine(name="Lorentzian",
                       domain=(-5.0, 5.0),
                       x0=-0.5,
                       width=0.4,
                       c=0.2)
    L.export_to_jsonfile(f"{testdir}/resources/test_export_load_file.json")

    L2 = LorentzianLine.load_from_jsonfile(
        f"{testdir}/resources/test_export_load_file.json")
    assert L.name == L2.name

    L3 = LorentzianLine.load_from_dict(**L2.export_to_dict())

    F_c1 = LineFactory.create("f_c",
                              name="FcLine1",
                              domain=(-15, 15),
                              x0=0.0,
                              width=0.02,
                              A=350.0,
                              q=0.023,
                              c=0.0,
                              weight=1.0)
    F_c1.export_to_jsonfile(
        f"{testdir}/resources/test_export_load_F_c_file.json")
    F_c2 = F_cLine.load_from_jsonfile(
        f"{testdir}/resources/test_export_load_F_c_file.json")
    assert F_c1.line_params["width"] == F_c2.line_params["width"]
    F_c3 = F_cLine.load_from_dict(**F_c2.export_to_dict())
    assert min(F_c3.domain) == min(F_c1.domain)

    F_I1 = LineFactory.create("f_I",
                              name="FILine1",
                              domain=(-15, 15),
                              x0=0.0,
                              width=0.02,
                              A=350.0,
                              q=0.023,
                              c=0.0,
                              weight=1.0,
                              kappa=0.01)
    F_I1.export_to_jsonfile(
        f"{testdir}/resources/test_export_load_F_I_file.json")
    F_I2 = F_ILine.load_from_jsonfile(
        f"{testdir}/resources/test_export_load_F_I_file.json")
    assert F_I1.line_params["width"] == F_I2.line_params["width"]
    F_I3 = F_ILine.load_from_dict(**F_I2.export_to_dict())
    assert min(F_I3.domain) == min(F_I1.domain)
Ejemplo n.º 29
0
def visualize_Lines():

    Lorentzian = LorentzianLine("Lorentzian1", (-energy_from_lambda(6.0), 15),
                                x0=-0.3,
                                width=0.5,
                                c=0.0)
    F_c = F_cLine("F_c1", (-energy_from_lambda(6.0), 15),
                  x0=-0.3,
                  width=0.5,
                  A=350.0,
                  q=0.02,
                  c=0.0,
                  weight=1)
    F_I = F_ILine("F_I1", (-energy_from_lambda(6.0), 15),
                  x0=-0.3,
                  width=0.008,
                  A=350.0,
                  q=0.02,
                  kappa=0.01,
                  c=0.0,
                  weight=1)
    print(F_I.integrate())
    F_I2 = F_ILine("F_I2", (-energy_from_lambda(6.0), 15),
                   x0=-0.3,
                   width=0.1,
                   A=367.0,
                   q=0.124,
                   kappa=0.065,
                   c=0.0,
                   weight=1)
    print(F_I2.integrate())

    e = np.linspace(-0.5, 0.0, 1201)

    plt.plot(e, Lorentzian(e))
    plt.plot(e, F_c(e) * F_c.normalize(), ls="--", lw=2.0)
    plt.plot(e, F_I(e), ls="dotted", lw=4.0)
    plt.plot(e, F_I2(e), ls="-.", lw=1.0)
    plt.show()
Ejemplo n.º 30
0
def test_transformer_basics():
    ### Creating a SqE model for transformation
    # We need some lines
    L1 = LorentzianLine(name="Lorentzian1", domain=(-15.0, 15.0), x0=-1.0, width=0.4, c=0.0, weight=3)
    # Contruct a SqE model
    sqe1 = SqE(lines=(L1,), lam=6.0, dlam=0.12, l_SD=3.43, T=20)

    ### Instantiate a transformer
    sqt1 = SqtTransformer(
        sqe1,
        corrections=(DetectorEfficiencyCorrectionFactor(sqe1, n_e=15000, n_lam=20),),
        n_e=15000,
        n_lam=20,
        l_SD=3.43
    )

    ### Values for transformation
    taus = logspace(-6, -1, 11)
    freqs = MIEZE_DeltaFreq_from_time(taus*1.0e-9, 3.43, 6.0)

    ### TRANSFORM!!
    sqt1vals = [sqt1(freq) for freq in freqs]
    sqt1vals_arg = arg.Sqt(
        arg.fqe_I,
        freqs,
        -1.0,
        0.4,
        15000,
        20,
        3.43,
        6.0,
        0.12,
        0.0,
        1.0,
        20,
        0.00005,
        0.1,
        350.
    )