def test_fit26(self):
     n_channels = 32
     irf_position_p = 2.0
     irf_position_s = 18.0
     irf_width = 0.25
     irf, time_axis = model_irf(
         n_channels=n_channels,
         period=32.,
         irf_position_p=irf_position_p,
         irf_position_s=irf_position_s,
         irf_width=irf_width
     )
     pattern_1 = np.copy(irf)
     pattern_2 = np.array(
         [2.19319033e-08, 1.84413639e-08, 3.34901093e-05, 1.26870040e-02,
          9.39283373e-02, 1.22572899e-01, 9.04619781e-02, 6.48528685e-02,
          4.76001206e-02, 3.55488448e-02, 2.68771272e-02, 2.04940827e-02,
          1.57172794e-02, 1.21005483e-02, 9.34005838e-03, 7.22159597e-03,
          5.58990082e-03, 4.33007676e-03, 3.35581222e-03, 2.60158411e-03,
          2.01729163e-03, 1.56443990e-03, 1.21335530e-03, 9.41114763e-04,
          7.29984906e-04, 5.66234218e-04, 4.39223436e-04, 3.40705850e-04,
          2.64287576e-04, 2.05010412e-04, 1.59029028e-04, 1.23360965e-04,
          1.86812539e-08, 1.77852830e-07, 4.86291035e-04, 2.41480990e-02,
          5.93758154e-02, 6.60604491e-02, 5.44150218e-02, 4.37331561e-02,
          3.46979744e-02, 2.73087097e-02, 2.13834235e-02, 1.66888768e-02,
          1.29973503e-02, 1.01084281e-02, 7.85456846e-03, 6.09967166e-03,
          4.73504656e-03, 3.67479642e-03, 2.85148543e-03, 2.21239392e-03,
          1.71641883e-03, 1.33157038e-03, 1.03297999e-03, 8.01329509e-04,
          6.21619684e-04, 4.82208333e-04, 3.74060853e-04, 2.90167144e-04,
          2.25088434e-04, 1.74605311e-04, 1.35444470e-04, 1.05066633e-04]
     )
     data = np.array(
         [0, 0, 0, 1, 9, 7, 5, 5, 5, 2, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 2, 3, 0, 1, 0,
         1, 1, 1, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
     )
     settings = {
         'pattern_1': pattern_1,
         'pattern_2': pattern_2,
         'verbose': False
     }
     fit26 = fit2x.Fit26(**settings)
     x1 = 0.5
     x = np.array([x1])
     fixed = np.array([0])
     r = fit26(
         data=data,
         initial_values=x,
         fixed=fixed
     )
     # import pylab as p
     # p.plot(fit26.model)
     # p.plot(fit26.data)
     # p.plot(fit26.irf)
     # p.show()
     best_fraction_1 = r['x'][0]
     self.assertEqual(best_fraction_1 < 0.1, True)
Exemple #2
0
    def test_modelf23(self):
        # parameters used in different tests
        n_channels = 32
        n_photons = 60
        irf_position_p = 2.0
        irf_position_s = 18.0
        irf_width = 0.25
        period, g, l1, l2, conv_stop = 32, 1.0, 0.1, 0.1, 255
        tau, gamma, r0, rho = 2.0, 0.01, 0.38, 1.2

        # setup some parameters
        irf_np, time_axis = model_irf(n_channels=n_channels,
                                      period=period,
                                      irf_position_p=irf_position_p,
                                      irf_position_s=irf_position_s,
                                      irf_width=irf_width)
        dt = time_axis[1] - time_axis[0]
        conv_stop = len(time_axis) / 2 - 1
        param = np.array([tau, gamma, r0, rho])
        corrections = np.array([period, g, l1, l2, conv_stop])
        # compute a model function that is later used as "data"
        model = np.zeros_like(time_axis)
        bg = np.zeros_like(time_axis)
        fit2x.modelf23(param, irf_np, bg, dt, corrections, model)
        model_ref = np.array([
            2.19319033e-08, 1.84413639e-08, 3.34901093e-05, 1.26870040e-02,
            9.39283373e-02, 1.22572899e-01, 9.04619781e-02, 6.48528685e-02,
            4.76001206e-02, 3.55488448e-02, 2.68771272e-02, 2.04940827e-02,
            1.57172794e-02, 1.21005483e-02, 9.34005838e-03, 7.22159597e-03,
            5.58990082e-03, 4.33007676e-03, 3.35581222e-03, 2.60158411e-03,
            2.01729163e-03, 1.56443990e-03, 1.21335530e-03, 9.41114763e-04,
            7.29984906e-04, 5.66234218e-04, 4.39223436e-04, 3.40705850e-04,
            2.64287576e-04, 2.05010412e-04, 1.59029028e-04, 1.23360965e-04,
            1.86812539e-08, 1.77852830e-07, 4.86291035e-04, 2.41480990e-02,
            5.93758154e-02, 6.60604491e-02, 5.44150218e-02, 4.37331561e-02,
            3.46979744e-02, 2.73087097e-02, 2.13834235e-02, 1.66888768e-02,
            1.29973503e-02, 1.01084281e-02, 7.85456846e-03, 6.09967166e-03,
            4.73504656e-03, 3.67479642e-03, 2.85148543e-03, 2.21239392e-03,
            1.71641883e-03, 1.33157038e-03, 1.03297999e-03, 8.01329509e-04,
            6.21619684e-04, 4.82208333e-04, 3.74060853e-04, 2.90167144e-04,
            2.25088434e-04, 1.74605311e-04, 1.35444470e-04, 1.05066633e-04
        ])
        # import pylab as p
        # p.plot(model)
        # p.show()
        self.assertEqual(np.allclose(model, model_ref), True)
        # add poisson noise to model and use as data
        np.random.seed(0)
        data = np.random.poisson(model * n_photons)
        data_ref = np.array([
            0, 0, 0, 1, 9, 7, 5, 5, 5, 2, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 2, 3, 0, 1, 0,
            1, 1, 1, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        ])
        self.assertEqual(np.allclose(data, data_ref), True)
Exemple #3
0
    def test_fconv_per(self):
        period = 13.0
        lifetime_spectrum = np.array([1.0, 4.1])
        irf, time_axis = model_irf(n_channels=32,
                                   period=period,
                                   irf_position_p=2.0,
                                   irf_position_s=2.0,
                                   irf_width=0.15)
        irf[irf < 0.001] = 0.0
        dt = time_axis[1] - time_axis[0]

        model_fconv_per = np.zeros_like(irf)
        fit2x.fconv_per(fit=model_fconv_per,
                        irf=irf,
                        x=lifetime_spectrum,
                        period=period,
                        start=0,
                        stop=-1,
                        dt=dt)
        ref = np.array([
            0.14280653, 0.13579708, 0.12913168, 0.12279343, 0.1167663,
            0.11103499, 0.105585, 0.10097891, 0.13309064, 0.50999418,
            1.31833782, 1.83959874, 1.85633556, 1.76860169, 1.68179244,
            1.5992441, 1.52074752, 1.44610384, 1.37512393, 1.30762796,
            1.24344494, 1.18241225, 1.12437526, 1.06918693, 1.01670744,
            0.96680383, 0.91934967, 0.87422472, 0.83131467, 0.79051079,
            0.75170972, 0.71481314, 0.67972757, 0.64636413, 0.61463828,
            0.58446966, 0.55578181, 0.52850207, 0.50256131, 0.47789382,
            0.45443709, 0.43213171, 0.41092115, 0.39075168, 0.3715722,
            0.35333412, 0.33599122, 0.31949958, 0.30381741, 0.28890497,
            0.27472449, 0.26124004, 0.24841745, 0.23622424, 0.22462951,
            0.2136039, 0.20311946, 0.19314964, 0.18366917, 0.17465403,
            0.16608139, 0.15792953, 0.15017779, 0.14280653
        ])
        print(model_fconv_per)
        # import pylab as plt
        # plt.semilogy(time_axis, model_fconv_per)
        # plt.semilogy(time_axis, irf)
        # plt.show()

        self.assertEqual(np.allclose(model_fconv_per, ref), True)

        # AVX wont be supported on Apple -> M1
        if platform != "darwin":
            model_fconv_avx = np.zeros_like(irf)
            fit2x.fconv_per_avx(fit=model_fconv_avx,
                                irf=irf,
                                x=lifetime_spectrum,
                                period=period,
                                start=0,
                                stop=-1,
                                dt=dt)
            self.assertEqual(np.allclose(model_fconv_avx, model_fconv_per),
                             True)
Exemple #4
0
    def test_fit23(self):
        # setup some parameters
        n_channels = 32
        irf_position_p = 2.0
        irf_position_s = 18.0
        irf_width = 0.25
        period, g, l1, l2, conv_stop = 32, 1.0, 0.1, 0.1, 255
        tau, gamma, r0, rho = 2.0, 0.01, 0.38, 1.2
        np.random.seed(0)

        irf_np, time_axis = model_irf(n_channels=n_channels,
                                      period=period,
                                      irf_position_p=irf_position_p,
                                      irf_position_s=irf_position_s,
                                      irf_width=irf_width)
        dt = time_axis[1] - time_axis[0]
        conv_stop = min(len(time_axis) // 2 - 1, conv_stop)
        corrections = np.array([period, g, l1, l2, conv_stop])

        # compute a model function that is later used as "data"
        data = [
            0, 0, 0, 1, 9, 7, 5, 5, 5, 2, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 2, 3, 0, 1, 0,
            1, 1, 1, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        ]
        bg = np.zeros_like(irf_np)

        # create MParam structure that contains all parameters for fitting
        m_param = fit2x.CreateMParam(irf=irf_np,
                                     background=bg,
                                     corrections=corrections,
                                     dt=dt,
                                     data=data)
        bifl_scatter = -1
        p_2s = 0
        tau = 4.2
        x = np.zeros(8, dtype=np.float64)
        x[:6] = [tau, gamma, r0, rho, bifl_scatter, p_2s]
        fixed = np.array([0, 1, 1, 1], dtype=np.int16)  # lifetime fitted
        twoIstar = fit2x.fit23(x, fixed, m_param)
        fit_res = np.array(
            [1.79364115, 0.01, 0.38, 1.2, -1., 0., 0.25974026, 0.25974026])
        # m = np.array([m for m in m_param.get_model()])
        # import pylab as p
        # p.plot(m)
        # p.plot(data)
        # p.plot([x for x in m_param.get_data()])
        # p.show()
        self.assertEqual(np.allclose(fit_res, x), True)
        self.assertAlmostEqual(twoIstar, 0.5309172991292537)
Exemple #5
0
 def test_modelf23(self):
     # import fit2x
     # import numpy as np
     # import pylab as plt
     period, g, l1, l2, conv_stop = 32., 1.0, 0.1, 0.1, 63
     irf, time_axis = model_irf(n_channels=64,
                                period=period,
                                irf_position_p=2.0,
                                irf_position_s=18.0,
                                irf_width=0.25)
     bg = np.zeros_like(irf) + 0.2
     tau, gamma, r0, rho = 4.0, 0.01, 0.38, 10000.2
     np.random.seed(0)
     param = np.array([tau, gamma, r0, rho])
     conv_stop = min(len(time_axis), conv_stop)
     corrections = np.array([period, g, l1, l2, conv_stop])
     dt = time_axis[1] - time_axis[0]
     out = np.zeros_like(bg)
     fit2x.modelf23(param, irf, bg, dt, corrections, out)
     out_ref = np.array([
         0.0020231, 0.00202169, 0.00202036, 0.00201916, 0.00202142,
         0.00213487, 0.00355841, 0.01042463, 0.02419025, 0.03623856,
         0.0399039, 0.03863233, 0.03646924, 0.03436654, 0.03239031,
         0.03053472, 0.02879244, 0.02715653, 0.02562052, 0.02417828,
         0.02282411, 0.02155263, 0.02035877, 0.01923782, 0.0181853,
         0.01719705, 0.01626915, 0.01539789, 0.01457984, 0.01381173,
         0.01309053, 0.01241336, 0.01177754, 0.01118053, 0.01061999,
         0.01009366, 0.00959948, 0.00913547, 0.00869978, 0.00829071,
         0.00790661, 0.00754596, 0.00720733, 0.00688938, 0.00659084,
         0.00631053, 0.00604734, 0.00580021, 0.00556818, 0.00535031,
         0.00514575, 0.00495367, 0.00477333, 0.00460399, 0.004445,
         0.00429571, 0.00415554, 0.00402392, 0.00390034, 0.00378431,
         0.00367537, 0.00357307, 0.00347702, 0.00338684, 0.00201,
         0.00200939, 0.00200882, 0.00200846, 0.00201797, 0.00222095,
         0.00378474, 0.00859881, 0.01498067, 0.01852531, 0.01874061,
         0.01785824, 0.01689628, 0.0159871, 0.01513332, 0.01433166,
         0.01357893, 0.01287214, 0.0122085, 0.01158537, 0.01100027,
         0.01045089, 0.00993504, 0.00945068, 0.00899589, 0.00856886,
         0.00816789, 0.0077914, 0.00743789, 0.00710596, 0.00679429,
         0.00650164, 0.00622686, 0.00596885, 0.00572659, 0.00549911,
         0.00528553, 0.00508498, 0.00489667, 0.00471985, 0.00455383,
         0.00439794, 0.00425157, 0.00411413, 0.00398509, 0.00386392,
         0.00375014, 0.00364331, 0.003543, 0.00344882, 0.00336038,
         0.00327734, 0.00319937, 0.00312616, 0.00305742, 0.00299288,
         0.00293227, 0.00287536, 0.00282193, 0.00277176, 0.00272465,
         0.00268042, 0.00263888, 0.00259989
     ])
     self.assertEqual(np.allclose(out, out_ref), True)
 def test_fit25(self):
     n_channels = 32
     irf_position_p = 2.0
     irf_position_s = 18.0
     irf_width = 0.25
     irf, time_axis = model_irf(
         n_channels=n_channels,
         period=32.,
         irf_position_p=irf_position_p,
         irf_position_s=irf_position_s,
         irf_width=irf_width
     )
     data = np.array(
         [0, 0, 0, 1, 9, 7, 5, 5, 5, 2, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 2, 3, 0, 1, 0,
         1, 1, 1, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
     )
     bg = np.zeros_like(irf)
     settings = {
         'dt': time_axis[1] - time_axis[0],
         'g_factor': 1.0,
         'l1': 0.1,
         'l2': 0.1,
         'period': 32.0,
         'convolution_stop': 31,
         'irf': irf,
         'background': bg,
         'verbose': True
     }
     fit25 = fit2x.Fit25(**settings)
     # import pylab as p
     # p.plot(s)
     # p.plot(model_ref)
     # p.show()
     r0 = 0.38
     tau1, tau2, tau3, tau4 = 0.5, 1.0, 2.0, 4.0
     gamma = 0.02
     x = np.array([tau1, tau2, tau3, tau4, gamma, r0])
     fixed = np.array([0, 0, 0, 0, 1, 1])
     r = fit25(
         data=data,
         initial_values=x,
         fixed=fixed
     )
     best_tau = r['x'][0]
     self.assertEqual(best_tau, 2.0)
Exemple #7
0
    def test_fconv_per_cs(self):
        period = 13.0
        lifetime_spectrum = np.array([1.0, 4.1])
        irf, time_axis = model_irf(n_channels=32,
                                   period=period,
                                   irf_position_p=2.0,
                                   irf_position_s=2.0,
                                   irf_width=0.15)
        irf[irf < 0.001] = 0.0
        dt = time_axis[1] - time_axis[0]
        print("dt:", dt)

        model_fconv_per_cs = np.zeros_like(irf)
        fit2x.fconv_per_cs(fit=model_fconv_per_cs,
                           irf=irf,
                           x=lifetime_spectrum,
                           period=period,
                           stop=-1,
                           conv_stop=14,
                           dt=dt)
        ref = np.array([
            0.13579708, 0.12913168, 0.12279343, 0.1167663, 0.11103499,
            0.105585, 0.10040251, 0.0960508, 0.12840442, 0.50553797,
            1.31410034, 1.83556925, 1.85250385, 1.76495806, 1.67832765,
            1.59594937, 1.51761451, 1.4431246, 1.37229093, 1.30493402,
            1.24088322, 1.17997627, 1.12205885, 1.06698422, 1.01461284,
            0.96481204, 0.91745564, 0.87242366, 0.82960201, 0.7888822,
            0.75016106, 0.7133405, 0.67832721, 0.6450325, 0.61337202,
            0.58326554, 0.5546368, 0.52741326, 0.50152594, 0.47690927,
            0.45350087, 0.43124144, 0.41007458, 0.38994666, 0.37080669,
            0.35260619, 0.33529902, 0.31884136, 0.30319149, 0.28830978,
            0.27415851, 0.26070184, 0.24790567, 0.23573758, 0.22416674,
            0.21316384, 0.202701, 0.19275172, 0.18329078, 0.17429422,
            0.16573924, 0.15760417, 0.1498684, 0.14251232
        ])

        print(model_fconv_per_cs)
        # import pylab as plt
        # plt.semilogy(model_fconv_per_cs)
        # plt.semilogy(irf)
        # plt.show()

        self.assertEqual(np.allclose(ref, model_fconv_per_cs), True)
Exemple #8
0
    def test_fconv(self):
        period = 12.0
        lifetime_spectrum = np.array([1.0, 4.1])
        irf, time_axis = model_irf(n_channels=32,
                                   period=period,
                                   irf_position_p=2.0,
                                   irf_position_s=2.0,
                                   irf_width=0.25)
        dt = time_axis[1] - time_axis[0]

        irf += 0.0
        model_ref = np.array([
            3.84934940e-15, 1.28147164e-12, 2.39814805e-10, 2.51323110e-08,
            1.48317225e-06, 4.95259533e-05, 9.43470390e-04, 1.03962151e-02,
            6.77549488e-02, 2.70166361e-01, 6.91596363e-01, 1.21273196e+00,
            1.58572312e+00, 1.71429416e+00, 1.69244968e+00, 1.62476106e+00,
            1.55186473e+00, 1.48146393e+00, 1.41421431e+00, 1.35001612e+00,
            1.28873217e+00, 1.23023021e+00, 1.17438394e+00, 1.12107282e+00,
            1.07018175e+00, 1.02160088e+00, 9.75225335e-01, 9.30955008e-01,
            8.88694331e-01, 8.48352077e-01, 8.09841158e-01, 7.73078441e-01,
            7.37984567e-01, 7.04483778e-01, 6.72503757e-01, 6.41975467e-01,
            6.12833009e-01, 5.85013471e-01, 5.58456801e-01, 5.33105670e-01,
            5.08905353e-01, 4.85803609e-01, 4.63750568e-01, 4.42698624e-01,
            4.22602332e-01, 4.03418311e-01, 3.85105148e-01, 3.67623310e-01,
            3.50935060e-01, 3.35004373e-01, 3.19796859e-01, 3.05279689e-01,
            2.91421526e-01, 2.78192454e-01, 2.65563916e-01, 2.53508649e-01,
            2.42000631e-01, 2.31015019e-01, 2.20528099e-01, 2.10517232e-01,
            2.00960808e-01, 1.91838198e-01, 1.83129708e-01, 1.74816540e-01
        ])
        model_fconv = np.zeros_like(irf)
        fit2x.fconv(fit=model_fconv, irf=irf, x=lifetime_spectrum, dt=dt)

        self.assertEqual(np.allclose(model_ref, model_fconv), True)

        # AVX wont be supported on Apple -> M1
        if platform != "darwin":
            model_fconv_avx = np.zeros_like(irf)
            fit2x.fconv_avx(fit=model_fconv_avx,
                            irf=irf,
                            x=lifetime_spectrum,
                            dt=dt)
            self.assertEqual(np.allclose(model_fconv_avx, model_fconv), True)
from __future__ import division

import unittest
import numpy as np

import fit2x
from compute_irf import model_irf


irf, time_axis = model_irf(
    n_channels=64,
    period=32.,
    irf_position_p=2.0,
    irf_position_s=18.0,
    irf_width=0.25
)
bg = np.zeros_like(irf) + 0.2


class Tests(unittest.TestCase):

    def test_fit25(self):
        n_channels = 32
        irf_position_p = 2.0
        irf_position_s = 18.0
        irf_width = 0.25
        irf, time_axis = model_irf(
            n_channels=n_channels,
            period=32.,
            irf_position_p=irf_position_p,
            irf_position_s=irf_position_s,
Exemple #10
0
    def test_target23(self):
        n_channels = 32
        irf_position_p = 2.0
        irf_position_s = 18.0
        irf_width = 0.25
        period, g, l1, l2, conv_stop = 32, 1.0, 0.05, 0.05, 128
        tau, gamma, r0, rho = 4.0, 0.01, 0.38, 0.2

        irf, time_axis = model_irf(n_channels=n_channels,
                                   period=period,
                                   irf_position_p=irf_position_p,
                                   irf_position_s=irf_position_s,
                                   irf_width=irf_width)
        dt = time_axis[1] - time_axis[0]
        conv_stop = min(len(time_axis) // 2 - 1, conv_stop)
        corrections = np.array([period, g, l1, l2, conv_stop])
        bg = np.zeros_like(time_axis)
        data = [
            0, 0, 0, 1, 9, 7, 5, 5, 5, 2, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 2, 3, 0, 1, 0,
            1, 1, 1, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        ]
        # create MParam structure that contains all parameters for fitting
        bifl_scatter = 1  # if smaller than zero use soft-bifl fit
        p_2s = 0  # if bigger than zero use p2s_twoIstar
        x = np.zeros(8, dtype=np.float64)
        x[:6] = [tau, gamma, r0, rho, bifl_scatter, p_2s]

        # test targetf23
        m_param = fit2x.CreateMParam(
            irf=irf,  # numpy array, float; instrument response function
            background=bg,  # numpy array, float; background pattern
            data=data,  # numpy array, integer; experimental data / decay
            corrections=corrections,  # numpy array, float; e.g. g-factor
            dt=dt  # float; time interval between micro time bins
        )
        fit2x.compute_signal_and_background(m_param)
        fit2x.targetf23(x, m_param)
        s = np.array([v for v in m_param.get_model()])
        model_ref = np.array([
            1.66519507e-03, 1.46615080e-03, 2.28617854e-03, 3.78693150e-01,
            2.92975666e+00, 4.66579661e+00, 4.16974650e+00, 3.56147676e+00,
            3.04670169e+00, 2.61052067e+00, 2.24027400e+00, 1.92543973e+00,
            1.65725865e+00, 1.42842712e+00, 1.23284490e+00, 1.06540818e+00,
            9.21839314e-01, 7.98546811e-01, 6.92509823e-01, 6.01182921e-01,
            5.22417392e-01, 4.54396131e-01, 3.95579697e-01, 3.44661551e-01,
            3.00530863e-01, 2.62241546e-01, 2.28986446e-01, 2.00075786e-01,
            1.74919135e-01, 1.53010290e-01, 1.33914598e-01, 1.17258284e-01,
            1.50297466e-03, 1.32827434e-03, 1.40551369e-02, 6.42147385e-01,
            1.62959724e+00, 1.92890467e+00, 1.75911255e+00, 1.59382873e+00,
            1.43975395e+00, 1.29717655e+00, 1.16603916e+00, 1.04604027e+00,
            9.36711296e-01, 8.37475283e-01, 7.47691217e-01, 6.66687042e-01,
            5.93783899e-01, 5.28313546e-01, 4.69630533e-01, 4.17120352e-01,
            3.70204542e-01, 3.28343503e-01, 2.91037617e-01, 2.57827136e-01,
            2.28291191e-01, 2.02046204e-01, 1.78743905e-01, 1.58069115e-01,
            1.39737410e-01, 1.23492763e-01, 1.09105210e-01, 9.63685926e-02
        ])
        # import pylab as p
        # p.semilogy(s, label='Model')
        # p.semilogy(model_ref, label='Reference')
        # p.legend()
        # p.show()
        self.assertEqual(np.allclose(model_ref, s), True)
    def test_fit24(self):
        model = np.array([
            0.01571078, 0.01571031, 0.01570988, 0.01570959, 0.01572048,
            0.01609258, 0.02058282, 0.04054985, 0.07322865, 0.08840347,
            0.07745669, 0.05963912, 0.04647572, 0.03811999, 0.0328345,
            0.02942218, 0.02715495, 0.02559208, 0.02446654, 0.02361628,
            0.02294272, 0.02238568, 0.0219082, 0.02148738, 0.02110889,
            0.02076354, 0.02044534, 0.02015024, 0.01987537, 0.01961861,
            0.01937834, 0.01915323, 0.01894215, 0.01874414, 0.01855832,
            0.01838392, 0.0182202, 0.0180665, 0.01792219, 0.01778671,
            0.0176595, 0.01754006, 0.01742791, 0.01732261, 0.01722374,
            0.01713091, 0.01704375, 0.0169619, 0.01688505, 0.0168129,
            0.01674515, 0.01668153, 0.0166218, 0.01656572, 0.01651306,
            0.01646361, 0.01641718, 0.01637359, 0.01633266, 0.01629423,
            0.01625814, 0.01622426, 0.01619244, 0.01616257, 0.01571054,
            0.01571009, 0.01570967, 0.01571062, 0.01578379, 0.01724953,
            0.02796797, 0.05699103, 0.08457921, 0.08515608, 0.06828081,
            0.05234779, 0.04181477, 0.03518192, 0.03094818, 0.0281781,
            0.02630526, 0.02498664, 0.02401429, 0.02326186, 0.02265236,
            0.02213868, 0.02169176, 0.02129351, 0.0209325, 0.02060134,
            0.02029511, 0.02001042, 0.01974484, 0.01949651, 0.01926396,
            0.019046, 0.01884157, 0.01864976, 0.01846974, 0.01830077,
            0.01814213, 0.01799321, 0.01785338, 0.0177221, 0.01759884,
            0.0174831, 0.01737443, 0.0172724, 0.0171766, 0.01708664,
            0.01700218, 0.01692287, 0.01684841, 0.01677849, 0.01671284,
            0.0166512, 0.01659332, 0.01653897, 0.01648795, 0.01644003,
            0.01639505, 0.0163528, 0.01631314, 0.0162759, 0.01624093,
            0.0162081, 0.01617727, 0.01614833
        ])
        n_photons = 5000000
        model *= n_photons / model.sum()
        data = np.random.poisson(model)
        period, g, l1, l2, conv_stop = 32, 1.0, 0.1, 0.1, 63
        irf, time_axis = model_irf(n_channels=64,
                                   period=period,
                                   irf_position_p=2.0,
                                   irf_position_s=18.0,
                                   irf_width=0.25)
        corrections = np.array([period, g, l1, l2, conv_stop])
        dt = time_axis[1] - time_axis[0]
        bg = np.zeros_like(irf) + 0.2

        tau1 = 4.5
        gamma = 0.01
        tau2 = 1.0
        a2 = 0.5
        offset = 1

        # create MParam structure that contains all parameters for fitting
        m_param = fit2x.CreateMParam(irf=irf,
                                     background=bg,
                                     corrections=corrections,
                                     dt=dt,
                                     data=data)
        bifl_scatter = -1
        x = np.zeros(8, dtype=np.float64)
        x[:6] = [tau1, gamma, tau2, a2, offset, bifl_scatter]
        # tau1 free, gamma fixed, tau2 free, a2 free, offset free
        fixed = np.array([0, 0, 0, 0, 0], dtype=np.int16)
        twoIstar = fit2x.fit24(x, fixed, m_param)
        tau1_target = 4.0
        gamma_target = 0.01
        tau2_target = 0.5
        a2_target = 0.9
        offset_target = 1
        self.assertAlmostEqual(x[0], tau1_target, delta=0.1)
        self.assertAlmostEqual(x[1], gamma_target, delta=0.1)
        self.assertAlmostEqual(x[2], tau2_target, delta=0.1)
        self.assertAlmostEqual(x[3], a2_target, delta=0.1)
        self.assertAlmostEqual(x[4], offset_target, delta=0.1)
 def test_fit24_2(self):
     np.random.seed(0)
     model = np.array([
         0.01571078, 0.01571031, 0.01570988, 0.01570959, 0.01572048,
         0.01609258, 0.02058282, 0.04054985, 0.07322865, 0.08840347,
         0.07745669, 0.05963912, 0.04647572, 0.03811999, 0.0328345,
         0.02942218, 0.02715495, 0.02559208, 0.02446654, 0.02361628,
         0.02294272, 0.02238568, 0.0219082, 0.02148738, 0.02110889,
         0.02076354, 0.02044534, 0.02015024, 0.01987537, 0.01961861,
         0.01937834, 0.01915323, 0.01894215, 0.01874414, 0.01855832,
         0.01838392, 0.0182202, 0.0180665, 0.01792219, 0.01778671,
         0.0176595, 0.01754006, 0.01742791, 0.01732261, 0.01722374,
         0.01713091, 0.01704375, 0.0169619, 0.01688505, 0.0168129,
         0.01674515, 0.01668153, 0.0166218, 0.01656572, 0.01651306,
         0.01646361, 0.01641718, 0.01637359, 0.01633266, 0.01629423,
         0.01625814, 0.01622426, 0.01619244, 0.01616257, 0.01571054,
         0.01571009, 0.01570967, 0.01571062, 0.01578379, 0.01724953,
         0.02796797, 0.05699103, 0.08457921, 0.08515608, 0.06828081,
         0.05234779, 0.04181477, 0.03518192, 0.03094818, 0.0281781,
         0.02630526, 0.02498664, 0.02401429, 0.02326186, 0.02265236,
         0.02213868, 0.02169176, 0.02129351, 0.0209325, 0.02060134,
         0.02029511, 0.02001042, 0.01974484, 0.01949651, 0.01926396,
         0.019046, 0.01884157, 0.01864976, 0.01846974, 0.01830077,
         0.01814213, 0.01799321, 0.01785338, 0.0177221, 0.01759884,
         0.0174831, 0.01737443, 0.0172724, 0.0171766, 0.01708664,
         0.01700218, 0.01692287, 0.01684841, 0.01677849, 0.01671284,
         0.0166512, 0.01659332, 0.01653897, 0.01648795, 0.01644003,
         0.01639505, 0.0163528, 0.01631314, 0.0162759, 0.01624093,
         0.0162081, 0.01617727, 0.01614833
     ])
     n_photons = 5000000
     model *= n_photons / model.sum()
     data = np.random.poisson(model)
     period, g, l1, l2, conv_stop = 32, 1.0, 0.1, 0.1, 63
     irf, time_axis = model_irf(n_channels=64,
                                period=period,
                                irf_position_p=2.0,
                                irf_position_s=18.0,
                                irf_width=0.25)
     bg = np.zeros_like(irf) + 0.2
     settings = {
         'dt': time_axis[1] - time_axis[0],
         'g_factor': 1.0,
         'l1': 0.1,
         'l2': 0.2,
         'period': 32.0,
         'convolution_stop': 31,
         'irf': irf,
         'background': bg,
         'verbose': True
     }
     fit24 = fit2x.Fit24(**settings)
     tau1, gamma, tau2, a2, background = 3.5, 0.01, 1.2, 0.4, 1.
     x0 = np.array([tau1, gamma, tau2, a2, background])
     fixed = np.array([0, 0, 0, 0, 0], dtype=np.int16)
     r = fit24(data=data, initial_values=x0, fixed=fixed)
     # for _ in range(1000): r = fit24(data=data, initial_values=x0, fixed=fixed)
     x = r['x']
     tau1_target = 4.0
     gamma_target = 0.01
     tau2_target = 0.5
     a2_target = 0.9
     offset_target = 1
     self.assertAlmostEqual(x[0], tau1_target, delta=0.1)
     self.assertAlmostEqual(x[1], gamma_target, delta=0.1)
     self.assertAlmostEqual(x[2], tau2_target, delta=0.1)
     self.assertAlmostEqual(x[3], a2_target, delta=0.1)
     self.assertAlmostEqual(x[4], offset_target, delta=0.1)
     self.assertEqual(True, r['twoIstar'] < 1.5)