Ejemplo n.º 1
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.º 2
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.º 3
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.º 4
0
def slow_vis_fitting():
    ### 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.0,
                 width=0.008,
                 A=350.0,
                 q=0.02,
                 kappa=0.01,
                 c=0.0,
                 weight=1)
    # Contruct a SqE model
    sqe1 = SqE((L2, L3), lam=6.0, dlam=0.12, lSD=3.43, T=20)

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

    ### Creating a logger for debugging:
    import logging
    # Create and configure logger
    logging.basicConfig(filename=f"{testdir}/resources/fit.log",
                        level=logging.INFO,
                        filemode="w")
    logger = logging.getLogger()

    ### Values for transformation
    taus = logspace(-6, -1, 101)
    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)
    longx = MIEZE_DeltaFreq_from_time(taus * 1.0e-9, 3.43, 6.0)

    ### TRANSFORM!!
    y = array([sqt1(freq) for freq in x])
    sqt1vals = array([sqt1(freq) for freq in longx])
    yerr = 0.03 * random.randn(len(y))

    ### FIT!
    m = FitModelCreator.from_transformer(
        sqt1,
        x,
        abs(y + yerr),
        yerr, [-1.5, 0.2, 0.0, 3.0, 0.01, 350.0, 0.02, 0.015, 0.0, 1.0],
        logger=logger)
    # m.fixed["c_Lorentzian1"] = True
    m.fixed["c_Lorentzian2"] = True
    print("fcn for m:\n", m.fcn)

    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),
        limit_weight_FI1=(0.0, None),
        limit_kappa_FI1=(0.0, None),
        #        fix_c_Lorentzian1=True,
        fix_c_Lorentzian2=True,
        #        fix_weight_Lorentzian1=True
        fix_c_FI1=True,
        fix_weight_FI1=True,
        fix_A_FI1=True,
        fix_q_FI1=True)
    print("fcn for m2:\n", m2.fcn)

    #    fmin, res = m.migrad(ncall=1000)
    #    print(fmin)
    #    print(res)
    fmin, res = m2.migrad(ncall=1000)
    print(fmin)
    print(res)

    ### Gather Fit results
    # dL1res1 = {
    #     "x0" : 0.0,
    #     "width" : res[0].value,
    #     "c" : res[1].value,
    #     "weight" : res[2].value
    # }
    # dL2res1 = {
    #     "x0" : res[3].value,
    #     "width" : res[4].value,
    #     "c" : res[5].value,
    #     "weight" : res[6].value
    # }
    dL2res1 = {
        "x0": res[0].value,
        "width": res[1].value,
        "c": res[2].value,
        "weight": res[3].value
    }
    dFI1res = {
        "width": res[4].value,
        "A": res[5].value,
        "q": res[6].value,
        "kappa": res[7].value,
        "c": res[8].value,
        "weight": res[9].value
    }

    #    L1.update_line_params(**dL1res1)
    L2.update_line_params(**dL2res1)
    L3.update_line_params(**dFI1res)
    sqtres1vals = array([sqt1(freq) for freq in longx])

    # ### Gather Fit results
    # dL1res2 = {
    #     "x0" : 0.0,
    #     "width" : res2[0].value,
    #     "c" : res2[1].value,
    #     "weight" : res2[2].value
    # }
    # dL2res2 = {
    #     "x0" : res2[3].value,
    #     "width" : res2[4].value,
    #     "c" : res2[5].value,
    #     "weight" : res2[6].value
    # }

    # L1.update_line_params(**dL1res2)
    # L2.update_line_params(**dL2res2)
    # sqtres2vals = array([sqt1(freq) for freq in longx])

    ### Calculate init guess
    # dL1ig = {
    #     "x0" : 0.0,
    #     "width" : 0.6,
    #     "c" : 0,
    #     "weight" : 1.0
    # }
    # dL2ig = {
    #     "x0" : -1.5,
    #     "width" : 0.2,
    #     "c" : 0.0,
    #     "weight" : 3.0
    # }
    dL2ig = {"x0": -1.5, "width": 0.2, "c": 0.0, "weight": 3.0}
    dFI1ig = {
        "x0": 0.0,
        "width": 0.01,
        "A": 350,
        "q": 0.02,
        "kappa": 0.015,
        "c": 0.0,
        "weight": 1.0
    }

    # L1.update_line_params(**dL1ig)
    L2.update_line_params(**dL2ig)
    L3.update_line_params(**dFI1ig)
    sqtigvals = array([sqt1(freq) for freq in longx])

    plt.plot(taus, sqt1vals, label="orig. curve", ls="--", color="C0")
    plt.plot(taus, sqtres1vals, label="fit curve 1", ls="-", color="C1")
    # plt.plot(taus, sqtres2vals, label="fit curve 2", ls=":", color="C4")
    plt.plot(taus, sqtigvals, label="init guess", ls="-.", color="C2")
    plt.errorbar(datataus,
                 abs(y + yerr),
                 yerr,
                 label="data",
                 ls="",
                 marker="o",
                 color="C0")
    plt.xscale("log")
    plt.xlabel("$\\tau_{MIEZE}$ [ns]", fontsize=16.)
    plt.ylabel("S(q,t) [arb. u.]", fontsize=16.)
    plt.legend()
    plt.show()