Ejemplo n.º 1
0
def test_satellite_and_annual_parallax_calculation():
    """test parallax calculation with Spitzer data"""
    model_with_par = mm.Model(
        {
            't_0': 2456836.22,
            'u_0': 0.922,
            't_E': 22.87,
            'pi_E_N': -0.248,
            'pi_E_E': 0.234
        },
        coords="17:47:12.25 -21:22:58.2")
    model_with_par.parallax(satellite=True,
                            earth_orbital=True,
                            topocentric=False)
    model_with_par.parameters.t_0_par = 2456836.2

    data_OGLE = mm.MulensData(file_name=SAMPLE_FILE_02)
    data_Spitzer = mm.MulensData(file_name=SAMPLE_FILE_03,
                                 ephemerides_file=SAMPLE_FILE_03_EPH)
    model_with_par.set_datasets([data_OGLE, data_Spitzer])

    ref_OGLE = np.loadtxt(SAMPLE_FILE_02_REF, unpack=True, usecols=[5])
    ref_Spitzer = np.loadtxt(SAMPLE_FILE_03_REF, unpack=True, usecols=[5])

    np.testing.assert_almost_equal(model_with_par.data_magnification[0],
                                   ref_OGLE,
                                   decimal=2)
    ratio = model_with_par.data_magnification[1] / ref_Spitzer
    np.testing.assert_almost_equal(ratio, [1.] * len(ratio), decimal=4)
Ejemplo n.º 2
0
def test_event_get_chi2_5():
    """
    De facto checks how information is passed between Event, Fit, and Model.
    Also checks simple relations between different Event.get_chi2().
    """
    kwargs = {'comments': ["\\", "|"]}
    data_1 = mm.MulensData(file_name=SAMPLE_FILE_03, **kwargs)
    data_2 = mm.MulensData(file_name=SAMPLE_FILE_04, phot_fmt='flux', **kwargs)
    params = {'t_0': 2452848.06, 'u_0': 0.1317, 't_E': 61.5}

    model = mm.Model(params)

    event_1 = mm.Event(data_1, model)
    event_2 = mm.Event(data_2, model)
    event_1_2 = mm.Event([data_1, data_2], model)
    event_2_1 = mm.Event([data_2, data_1], model)

    chi2_1 = event_1.get_chi2()
    chi2_2 = event_2.get_chi2()
    # The line below was failing when the test was written:
    chi2_1_2 = event_1_2.get_chi2()
    chi2_2_1 = event_2_1.get_chi2()

    np.testing.assert_almost_equal(chi2_1_2, chi2_1 + chi2_2)
    np.testing.assert_almost_equal(chi2_1_2, chi2_2_1)
Ejemplo n.º 3
0
def test_event_get_chi2_3():
    """test on ob08092 OGLE-IV data - MulensData.good & MulensData.bad"""
    t_0 = 5379.57091
    u_0 = 0.52298
    t_E = 17.94002

    bad = np.zeros(383, dtype='bool')
    bad[300:350] = True
    data_1 = mm.MulensData(file_name=SAMPLE_FILE_01, bad=bad)
    data_2 = mm.MulensData(file_name=SAMPLE_FILE_01, good=~bad)

    ev = mm.Event()
    mod = mm.Model({'t_0': t_0, 'u_0': u_0, 't_E': t_E})
    mod.set_datasets([data_1])
    ev.model = mod
    ev.datasets = [data_1]
    chi2 = ev.get_chi2()
    np.testing.assert_almost_equal(float(chi2),
                                   343.46567,
                                   decimal=4,
                                   err_msg='problem in resulting chi2')

    mod.set_datasets([data_2])
    ev.model = mod
    ev.datasets = [data_2]
    chi2 = ev.get_chi2()
    np.testing.assert_almost_equal(float(chi2),
                                   343.46567,
                                   decimal=4,
                                   err_msg='problem in resulting chi2')
Ejemplo n.º 4
0
def test_data_coords():
    coords = SkyCoord('18:00:00 -30:00:00', unit=(u.hourangle, u.deg))
    data_1 = mm.MulensData(file_name=SAMPLE_FILE_01,
                           coords='18:00:00 -30:00:00')
    assert isinstance(data_1.coords, SkyCoord)
    assert data_1.coords.ra == coords.ra
    assert data_1.coords.dec == coords.dec
    assert data_1.coords.dec.deg == -30.00

    data_3 = mm.MulensData(file_name=SAMPLE_FILE_01)
    data_3.coords = '17:00:00 -27:32:14'
    assert data_3.coords.to_string('hmsdms') == '17h00m00s -27d32m14s'
Ejemplo n.º 5
0
def generate_binary_source_datasets(model_1, model_2):
    """prepare fake data for binary source model"""
    time = np.linspace(4900., 5200., 600)
    mag_1 = model_1.get_magnification(time)
    mag_2 = model_2.get_magnification(time)
    flux = 100. * mag_1 + 300. * mag_2 + 50.
    data_1 = mm.MulensData(data_list=[time, flux, 1. + 0. * time],
                           phot_fmt='flux')
    flux = 20. * mag_1 + 30. * mag_2 + 50.
    data_2 = mm.MulensData(data_list=[time, flux, 1. + 0. * time],
                           phot_fmt='flux')

    return (data_1, data_2)
Ejemplo n.º 6
0
def test_binary_source_and_fluxes_for_bands():
    """
    Test if setting different flux ratios for different bands in binary
    source models works properly. The argument flux_ratio_constraint
    is set as string.
    """
    model = mm.Model({
        't_0_1': 5000.,
        'u_0_1': 0.05,
        't_0_2': 5100.,
        'u_0_2': 0.003,
        't_E': 30.
    })

    times_I = np.linspace(4900., 5200., 3000)
    times_V = np.linspace(4800., 5300., 250)
    (f_s_1_I, f_s_2_I, f_b_I) = (10., 20., 3.)
    (f_s_1_V, f_s_2_V, f_b_V) = (15., 5., 30.)
    q_f_I = f_s_2_I / f_s_1_I
    q_f_V = f_s_2_V / f_s_1_V
    (mag_1_I, mag_2_I) = model.magnification(times_I, separate=True)
    (mag_1_V, mag_2_V) = model.magnification(times_V, separate=True)
    effective_mag_I = (mag_1_I + mag_2_I * q_f_I) / (1. + q_f_I)
    effective_mag_V = (mag_1_V + mag_2_V * q_f_V) / (1. + q_f_V)
    flux_I = mag_1_I * f_s_1_I + mag_2_I * f_s_2_I + f_b_I
    flux_V = mag_1_V * f_s_1_V + mag_2_V * f_s_2_V + f_b_V
    data_I = mm.MulensData(data_list=[times_I, flux_I, flux_I / 100.],
                           phot_fmt='flux',
                           bandpass='******')
    data_V = mm.MulensData(data_list=[times_V, flux_V, flux_V / 1000.],
                           phot_fmt='flux',
                           bandpass='******')
    model.set_datasets([data_V, data_I])

    model.set_source_flux_ratio_for_band('I', q_f_I)
    model.set_source_flux_ratio_for_band('V', q_f_V)

    # Test Model.magnification()
    result_I = model.magnification(times_I, flux_ratio_constraint='I')
    result_V = model.magnification(times_V, flux_ratio_constraint='V')
    almost(result_I, effective_mag_I)
    almost(result_V, effective_mag_V)

    # Test Model.get_data_magnification()
    result_I = model.get_data_magnification(data_I)
    result_V = model.get_data_magnification(data_V)
    almost(result_I, effective_mag_I)
    almost(result_V, effective_mag_V)
Ejemplo n.º 7
0
def test_satellite_and_annual_parallax_calculation():
    """
    test that data magnifications are correctly retrieved for Spitzer data.
    """

    # Create Model
    model_parameters = {
        't_0': 2456836.22,
        'u_0': 0.922,
        't_E': 22.87,
        'pi_E_N': -0.248,
        'pi_E_E': 0.234,
        't_0_par': 2456836.2
    }
    coords = "17:47:12.25 -21:22:58.2"
    model_with_par = mm.Model(model_parameters, coords=coords)
    model_with_par.parallax(satellite=True,
                            earth_orbital=True,
                            topocentric=False)

    # Load Spitzer data and answers
    data_Spitzer = mm.MulensData(file_name=SAMPLE_FILE_03,
                                 ephemerides_file=SAMPLE_FILE_03_EPH)
    ref_Spitzer = np.loadtxt(SAMPLE_FILE_03_REF, unpack=True, usecols=[5])

    # Test FitData.data_magnification()
    my_fit = mm.FitData(dataset=data_Spitzer, model=model_with_par)
    ratio = my_fit.get_data_magnification() / ref_Spitzer
    np.testing.assert_almost_equal(ratio, [1.] * len(ratio), decimal=4)
Ejemplo n.º 8
0
def test_event_chi2_binary_source():
    """simple test if chi2 calculation for binary source works fine"""
    model = mm.Model({
        't_0_1': 5000.,
        'u_0_1': 0.05,
        't_0_2': 5100.,
        'u_0_2': 0.15,
        't_E': 25.
    })
    model_1 = mm.Model(model.parameters.source_1_parameters)
    model_2 = mm.Model(model.parameters.source_2_parameters)

    # prepare fake data:
    time = np.linspace(4900., 5200., 600)
    mag_1 = model_1.magnification(time)
    mag_2 = model_2.magnification(time)
    flux = 100. * mag_1 + 300. * mag_2 + 50.
    data = mm.MulensData(data_list=[time, flux, 1. + 0. * time],
                         phot_fmt='flux')

    # Calculate chi^2:
    event = mm.Event([data], model)
    np.testing.assert_almost_equal(event.get_chi2(), 0.)
    # Make sure Model.set_source_flux_ratio() is taken into account.
    model.set_source_flux_ratio(1.)
    np.testing.assert_almost_equal(model.magnification(time),
                                   (mag_1 + mag_2) / 2.)
    assert event.get_chi2() > 1.
    model.set_source_flux_ratio(3.)
    np.testing.assert_almost_equal(event.get_chi2(), 0.)
Ejemplo n.º 9
0
def test_chi2_vs_get_chi2():
    """get_chi2 always updates after something changes in the model, chi2 does
    not."""
    t_0 = 5379.57091
    u_0 = 0.52298
    t_E = 17.94002

    data = mm.MulensData(file_name=SAMPLE_FILE_01)

    ev = mm.Event()
    mod = mm.Model({'t_0': t_0, 'u_0': u_0, 't_E': t_E})
    ev.model = mod
    ev.datasets = [data]

    # New (v2.0) method of fixing the blending
    ev.fix_blend_flux = {}
    np.testing.assert_almost_equal(ev.get_chi2(), 427.20382, decimal=4)
    np.testing.assert_almost_equal(ev.chi2, 427.20382, decimal=4)

    fix_blend_flux = {}
    for dataset in ev.datasets:
        fix_blend_flux[dataset] = 0.

    ev.fix_blend_flux = fix_blend_flux
    np.testing.assert_almost_equal(ev.chi2, 427.20382, decimal=4)
    np.testing.assert_almost_equal(ev.get_chi2(), 459.09826, decimal=4)
    np.testing.assert_almost_equal(ev.chi2, 459.09826, decimal=4)
Ejemplo n.º 10
0
def test_chi2_gradient():
    """
    test that fit_fluxes/update must be run in order to update the gradient
    """
    data = mm.MulensData(file_name=SAMPLE_FILE_02)
    event = mm.Event(datasets=[data],
                     model=mm.Model(chi2_gradient_test_1.parameters),
                     fix_blend_flux={data: 0.})
    result = event.get_chi2_gradient(chi2_gradient_test_1.grad_params)
    reference = np.array([
        chi2_gradient_test_1.gradient[key]
        for key in chi2_gradient_test_1.grad_params
    ])
    np.testing.assert_almost_equal(reference / result, 1., decimal=4)

    # changing something w/o updating: gives old values
    event.model.parameters.t_0 += 0.1
    result_1 = event.chi2_gradient
    np.testing.assert_almost_equal(result_1 / result, 1.)

    # run fit_fluxes: gives new values
    event.fit_fluxes()
    result_2 = event.calculate_chi2_gradient(chi2_gradient_test_1.grad_params)
    assert all(result != result_2)

    # Go back to previous results and test that calculate_chi2_gradient gives
    # results that don't match anything.
    event.model.parameters.t_0 -= 0.1
    result_3 = event.calculate_chi2_gradient(chi2_gradient_test_1.grad_params)
    assert all(result != result_3)
    assert all(result_2 != result_3)
    result_4 = event.get_chi2_gradient(chi2_gradient_test_1.grad_params)
    np.testing.assert_almost_equal(result_4 / result, 1.)
Ejemplo n.º 11
0
def test_annual_parallax_calculation():
    """
    This is a high-level unit test for parallax. The "true" values were
    calculated from the sfit routine assuming fs=1.0, fb=0.0.
    """
    t_0 = 2457479.5  # April 1 2016, a time when parallax is large
    times = np.array([t_0 - 1., t_0, t_0 + 1., t_0 + 1.])
    true_with_par = [
        np.array([7.12376832, 10.0386009, 7.13323363, 7.13323363])
    ]

    model_with_par = mm.Model(
        {
            't_0': t_0,
            'u_0': 0.1,
            't_E': 10.,
            'pi_E': (0.3, 0.5)
        },
        coords='17:57:05 -30:22:59')
    model_with_par.parallax(satellite=False,
                            earth_orbital=True,
                            topocentric=False)
    ones = np.ones(len(times))
    data = mm.MulensData(data_list=[times, ones, ones])
    model_with_par.set_datasets([data])

    model_with_par.parameters.t_0_par = 2457479.

    np.testing.assert_almost_equal(model_with_par.data_magnification,
                                   true_with_par,
                                   decimal=4)
Ejemplo n.º 12
0
    def _set_event_PSPL(self):
        """
        Sets internal variable to MulensModel.Event instance
        that uses PSPL model.
        """
        datasets = []
        for band in self.bands:
            mask = (self._filter == band)
            if not np.any(mask):
                continue
            times = self._JD[mask]
            flux = self._simulated_flux[band]
            sigma_flux = self._sigma_flux[band]
            plot = {}
            data = MM.MulensData([times, flux, sigma_flux],
                                 phot_fmt='flux',
                                 bandpass=band,
                                 plot_properties={'label': 'LSST ' + band})
            datasets.append(data)

        if self._follow_up_Chilean is not None:
            if self._follow_up_Chilean.n_epochs > 0:
                datasets.append(self._follow_up_Chilean)

        if self._follow_up_nonChilean is not None:
            if self._follow_up_nonChilean.n_epochs > 0:
                datasets.append(self._follow_up_nonChilean)

        model = MM.Model(
            {p: self._parameters[p]
             for p in ['t_0', 'u_0', 't_E']})
        self._event_PSPL = MM.Event(datasets, model)
Ejemplo n.º 13
0
def test_event_get_chi2_3():
    """
    Test: If I change the model parameters, the chi2 should change.
    """
    t_0 = 5380.
    u_0 = 0.5
    t_E = 18.
    model = mm.Model({'t_0': t_0, 'u_0': u_0, 't_E': t_E})

    # Generate fake data
    times = np.arange(5320, 5420.)
    f_source = 0.1
    f_blend = 0.5
    mod_fluxes = f_source * model.magnification(times) + f_blend
    I_mag = mm.Utils.get_mag_from_flux(mod_fluxes)
    errors = 0.01 * np.ones(times.shape)
    data = mm.MulensData(data_list=[times, I_mag, errors])

    # Generate event and fit
    event = mm.Event(datasets=data, model=model)

    orig_chi2 = event.get_chi2()

    # Change the model
    event.model.parameters.t_0 = 5000.

    assert event.get_chi2() != orig_chi2
Ejemplo n.º 14
0
def do_annual_parallax_test(filename):
    """testing functions called by a few unit tests"""
    with open(filename) as data_file:
        lines = data_file.readlines()
    ulens_params = lines[3].split()
    event_params = lines[4].split()
    data = np.loadtxt(filename, dtype=None)
    model = mm.Model(
        {
            't_0': float(ulens_params[1]) + 2450000.,
            'u_0': float(ulens_params[3]),
            't_E': float(ulens_params[4]),
            'pi_E_N': float(ulens_params[5]),
            'pi_E_E': float(ulens_params[6])
        },
        coords=SkyCoord(event_params[1] + ' ' + event_params[2],
                        unit=(u.deg, u.deg)))
    model.parameters.t_0_par = float(ulens_params[2]) + 2450000.

    time = data[:, 0]
    dataset = mm.MulensData([time, 20. + time * 0., 0.1 + time * 0.],
                            add_2450000=True)
    model.set_datasets([dataset])
    model.parallax(satellite=False, earth_orbital=True, topocentric=False)
    return np.testing.assert_almost_equal(model.data_magnification[0] /
                                          data[:, 1],
                                          1.0,
                                          decimal=4)
Ejemplo n.º 15
0
    def _add_follow_up_nonChilean(self):
        """
        Add follow-up from observatories that are outside Chile.
        """
        if self._nonChilean_follow_up_data is None:
            temp = np.loadtxt(self._nonChilean_follow_up_file, unpack=True)
            self._nonChilean_follow_up_data = {
                'jd': temp[0],
                '5sigma_depth': temp[1]
            }

        start = self.detection_time + self._dt_shift
        stop = self._parameters[
            't_0'] + self._t_E_factor * self._parameters['t_E']
        mask = (self._nonChilean_follow_up_data['jd'] > start)
        mask *= (self._nonChilean_follow_up_data['jd'] < stop)
        times = self._nonChilean_follow_up_data['jd'][mask]

        mag_5sig = self._nonChilean_follow_up_data['5sigma_depth'][mask]
        mag_5sig -= self._d_5sigma
        sim = self._simulate_flux(times, mag_5sig, self._band_follow_up)

        self._follow_up_nonChilean = MM.MulensData(
            [times, sim[0], sim[1]],
            phot_fmt='flux',
            bandpass=self._band_follow_up,
            plot_properties={
                "label": "follow-up i",
                "zorder": -500
            })
Ejemplo n.º 16
0
        def gen_data(f_source_1,
                     f_blend,
                     q_flux,
                     times,
                     bandpass=None,
                     **kwargs):
            """generate perfect data for a given set of properties"""

            if bandpass == 'I':
                gamma = self.gamma['I']
            elif bandpass == 'V':
                gamma = self.gamma['V']
            else:
                gamma = None

            mag_1 = model_1.get_magnification(times)
            mag_2 = model_2.get_magnification(times, gamma=gamma)
            f_source_2 = f_source_1 * q_flux
            flux = f_source_1 * mag_1 + f_source_2 * mag_2 + f_blend
            err = np.zeros(len(times)) + 0.01
            data = mm.MulensData([times, flux, err],
                                 phot_fmt='flux',
                                 bandpass=bandpass,
                                 **kwargs)

            return data
Ejemplo n.º 17
0
def test_event_get_chi2_4():
    """test if best chi2 is remembered correctly"""
    t_0 = 5379.57091
    u_0 = 0.52298
    t_E = 17.94002

    data = mm.MulensData(file_name=SAMPLE_FILE_01)

    ev = mm.Event()
    params = {'t_0': t_0, 'u_0': u_0, 't_E': t_E * u.day}
    mod = mm.Model(params)
    mod.set_datasets([data])
    ev.model = mod
    ev.datasets = [data]

    chi2_1 = ev.get_chi2()  # This is the best model.

    ev.model.parameters.parameters['t_0'] += 1.
    ev.model.parameters.parameters['u_0'] += 0.1
    ev.model.parameters.parameters['t_E'] += 1. * u.day
    chi2_2 = ev.get_chi2()

    assert chi2_2 > chi2_1
    assert ev.best_chi2 == chi2_1
    assert ev.best_chi2_parameters == params
Ejemplo n.º 18
0
def test_chi2_gradient_2():
    """
    Double-up on the gradient test, i.e. duplicate the dataset and check that
    the gradient values double.
    """
    reference = np.array([
        chi2_gradient_test_1.gradient[key]
        for key in chi2_gradient_test_1.grad_params
    ])

    data = mm.MulensData(file_name=SAMPLE_FILE_02)
    event = mm.Event(datasets=[data, data],
                     model=mm.Model(chi2_gradient_test_1.parameters),
                     fix_blend_flux={data: 0.})
    result_0 = event.get_chi2_gradient(chi2_gradient_test_1.grad_params)
    result_1 = event.fits[1].chi2_gradient
    np.testing.assert_almost_equal(2. * reference / result_0, 1., decimal=4)
    np.testing.assert_almost_equal(2. * result_1 / result_0, 1.)
    np.testing.assert_almost_equal(reference / result_1, 1., decimal=4)

    # Change something and test fit.get_chi2_gradient
    event.model.parameters.t_0 += 0.1
    result_2 = event.fits[1].get_chi2_gradient(
        chi2_gradient_test_1.grad_params)
    assert result_2[0] != result_1[0]
    event.model.parameters.t_0 -= 0.1
    result_3 = event.fits[1].get_chi2_gradient(
        chi2_gradient_test_1.grad_params)
    np.testing.assert_almost_equal(result_3 / reference, 1., decimal=4)
Ejemplo n.º 19
0
def test_event_get_chi2_double_source_simple():
    """
    basic test on ob08092 OGLE-IV data
    """
    t_0 = 5379.57091
    u_0 = 0.52298
    t_E = 17.94002

    data = mm.MulensData(file_name=SAMPLE_FILE_01)

    ev = mm.Event()
    mod = mm.Model({'t_0': t_0, 'u_0': u_0, 't_E': t_E})
    mod.set_datasets([data, data])

    ev.model = mod
    ev.datasets = [data, data]

    chi2 = ev.get_chi2()

    assert isinstance(chi2, float), 'wrong type of chi2'
    message = 'problem in resulting chi2 for 2 exactly the same datasets'
    np.testing.assert_almost_equal(chi2,
                                   854.407644,
                                   decimal=4,
                                   err_msg=message)
Ejemplo n.º 20
0
def test_event_chi2_binary_source_2datasets():
    """
    simple test if chi2 calculation for binary source
    works fine for 2 datasets
    """
    model = mm.Model({
        't_0_1': 5000.,
        'u_0_1': 0.05,
        't_0_2': 5100.,
        'u_0_2': 0.15,
        't_E': 25.
    })
    model_1 = mm.Model(model.parameters.source_1_parameters)
    model_2 = mm.Model(model.parameters.source_2_parameters)

    # prepare fake data:
    time = np.linspace(4900., 5200., 600)
    mag_1 = model_1.magnification(time)
    mag_2 = model_2.magnification(time)
    flux = 100. * mag_1 + 300. * mag_2 + 50.
    data_1 = mm.MulensData(data_list=[time, flux, 1. + 0. * time],
                           phot_fmt='flux')
    flux = 20. * mag_1 + 30. * mag_2 + 50.
    data_2 = mm.MulensData(data_list=[time, flux, 1. + 0. * time],
                           phot_fmt='flux')

    # Calculate chi^2:
    event = mm.Event([data_1, data_2], model)
    np.testing.assert_almost_equal(event.get_chi2(), 0.)
    np.testing.assert_almost_equal(event.get_chi2_for_dataset(0), 0.)
    np.testing.assert_almost_equal(event.get_chi2_for_dataset(1), 0.)
    # Make sure that changing parameters changes chi2:
    model.parameters.t_E = 100.
    assert event.get_chi2() > 1., 'wrong chi2'
    model.parameters.t_E = 25.
    np.testing.assert_almost_equal(event.get_chi2(), 0.)
    model.parameters.t_0_1 = 5010.
    assert event.get_chi2() > 1., 'wrong chi2'
    model.parameters.t_0_1 = 5000.

    # Test combination of Model.set_source_flux_ratio_for_band() and
    # Event.get_chi2_for_dataset().
    data_1.bandpass = '******'
    event.model.set_source_flux_ratio_for_band('some', 3.)
    np.testing.assert_almost_equal(event.get_chi2_for_dataset(0), 0.)
Ejemplo n.º 21
0
def test_file_read():
    """read sample file and check if values match"""
    data = mm.MulensData(file_name=SAMPLE_FILE_01)

    np.testing.assert_almost_equal(data.time[0],
                                   5264.84100,
                                   err_msg="time of first line doesn't match")

    assert data.mag[-1] == 13.913, "magnitude of the last line doesn't match"
Ejemplo n.º 22
0
def test_data_list():
    """
    check if initialization by list works fine
    """
    t = np.array([7500., 7501.])
    m = np.array([21.0, 21.1])
    e = np.array([0.001, 1.000])
    data = mm.MulensData(data_list=[t, m, e])
    np.testing.assert_almost_equal(
        data.time, t, err_msg='problem with time vector in MulensData')
Ejemplo n.º 23
0
 def test_gradient_init_1(self):
     """
     Test that fit_fluxes/update must be run before the gradient is accessed
     """
     data = mm.MulensData(file_name=SAMPLE_FILE_02)
     event = mm.Event(datasets=[data],
                      model=mm.Model(chi2_gradient_test_1.parameters),
                      fix_blend_flux={data: 0.})
     with self.assertRaises(TypeError):
         event.chi2_gradient()
Ejemplo n.º 24
0
def generate_dataset(time, flux_1, flux_2, blend_flux, flux_err,
                     model_1, model_2):
    """Generate simulated dataset assuming binary source model."""
    A_1 = model_1.get_magnification(time)
    A_2 = model_2.get_magnification(time)
    flux = A_1 * flux_1 + A_2 * flux_2 + blend_flux
    err_flux = flux_err + 0. * time
    flux += flux_err * np.random.normal(size=len(time))
    my_dataset = mm.MulensData([time, flux, err_flux], phot_fmt='flux')
    return my_dataset
Ejemplo n.º 25
0
def test_event_get_chi2_2():
    """
    Basic unit test on ob08092 OGLE-IV data. Same as above but with
    the data input twice (to test behavior for multiple datasets);
    also test if Event.get_chi2_for_dataset() gives correct answers.
    """
    t_0 = 5379.57091
    u_0 = 0.52298
    t_E = 17.94002
    answer = 427.20382201

    data_1 = mm.MulensData(file_name=SAMPLE_FILE_01)
    data_2 = mm.MulensData(file_name=SAMPLE_FILE_01)

    ev = mm.Event()
    mod = mm.Model({'t_0': t_0, 'u_0': u_0, 't_E': t_E})
    ev.model = mod
    ev.datasets = [data_1, data_2]

    chi2 = ev.get_chi2()
    assert isinstance(chi2, float), 'wrong type of chi2'
    np.testing.assert_almost_equal(float(chi2),
                                   2. * answer,
                                   decimal=4,
                                   err_msg='problem in resulting chi2')

    chi2_2 = ev.get_chi2_for_dataset(0)
    np.testing.assert_almost_equal(chi2_2, answer)

    chi2_3 = ev.get_chi2_for_dataset(1)
    np.testing.assert_almost_equal(chi2_3, answer)

    # New method of fixing the blending
    # Both datasets have zero blending
    ev.fix_blend_flux = {data_1: 0., data_2: 0.}
    chi2_no_blend = ev.get_chi2()
    assert isinstance(chi2_no_blend, float), 'wrong type of chi2'
    np.testing.assert_almost_equal(
        float(chi2_no_blend),
        2. * 459.09826,
        decimal=4,
        err_msg='problem in resulting chi2 for fixed no blending')
    assert (ev.blend_fluxes == [0., 0.])
Ejemplo n.º 26
0
def generate_dataset(f_mod, t):
    """
    pass in f_mod and t, returns a MulensData
    """

    # error in measurement
    err = f_mod * 0.01

    my_dataset = mm.MulensData(data_list=[t, f_mod, err], phot_fmt='flux')

    return my_dataset
Ejemplo n.º 27
0
def test_event_coords_ra_dec_2():
    """checks setting coords of Event after initialization"""
    ra_1_str = '01:00:00'
    dec_1_str = '+44:15:00'
    ra_1 = 15.
    dec_1 = 44.25

    data = mm.MulensData(file_name=SAMPLE_FILE_01)
    model = mm.Model({'t_0': 2450000., 'u_0': 0.1, 't_E': 100.})
    event = mm.Event(datasets=data, model=model)
    event.coords = '{0} {1}'.format(ra_1_str, dec_1_str)
    check_event_coords(event, ra_1, dec_1)
Ejemplo n.º 28
0
def _test_event_chi2_gradient_rho():
    """
    test calculation of chi2 gradient including finite source effects
    MB08310 is used as an example
    """
    kwargs = {'comments': ["\\", "|"]}
    datasets = [
        mm.MulensData(file_name=SAMPLE_FILE_310_01, bandpass='******', **kwargs),
        mm.MulensData(file_name=SAMPLE_FILE_310_02, bandpass='******', **kwargs),
        mm.MulensData(file_name=SAMPLE_FILE_310_03, bandpass='******', **kwargs)
    ]

    (gamma_I, gamma_V) = (0.44, 0.72)
    t_0 = 2454656.39975
    u_0 = 0.00300
    t_E = 11.14
    rho = 0.00492549
    t_star = rho * t_E
    parameters = {'t_0': t_0, 'u_0': u_0, 't_E': t_E, 'rho': rho}
    model = mm.Model(parameters)
    method = 'finite_source_LD_Yoo04'
    model.set_magnification_methods(
        [t_0 - 2. * t_star, method, t_0 + 2. * t_star])
    model.set_limb_coeff_gamma('R', (gamma_V + gamma_I) / 2.)
    model.set_limb_coeff_gamma('U', (gamma_V + gamma_I) / 2.)
    model.set_limb_coeff_gamma('I', gamma_I)

    # Set expected values
    # JCY - see sandbox/rho_gradient on pink laptop
    params = parameters.keys()
    gradient = {
        't_0': 1283513.3068849628,
        'u_0': 20492801.742886964,
        't_E': -9573.3589902395161,
        'rho': -1503911.2409404013
    }
    reference = np.array([gradient[key] for key in params])

    # Create event and run test
    event = mm.Event(model=model, datasets=datasets)
Ejemplo n.º 29
0
def test_model_PSPL_1():
    """tests basic evaluation of Paczynski model"""
    t_0 = 5379.57091
    u_0 = 0.52298
    t_E = 17.94002
    times = np.array([t_0 - 2.5 * t_E, t_0, t_0 + t_E])
    data = mm.MulensData(data_list=[times, times * 0., times * 0.])
    model = mm.Model({'t_0': t_0, 'u_0': u_0, 't_E': t_E})
    model.parameters.u_0 = u_0
    model.parameters.t_E = t_E
    model.set_datasets([data])
    almost(model.data_magnification,
           [np.array([1.028720763, 2.10290259, 1.26317278])],
           err_msg="PSPL model returns wrong values")
Ejemplo n.º 30
0
def test_BLPS_01():
    """simple binary lens with point source"""
    params = mm.ModelParameters({
        't_0': t_0,
        'u_0': u_0,
        't_E': t_E,
        'alpha': alpha,
        's': s,
        'q': q
    })

    model = mm.Model(parameters=params)
    t = np.array([2456112.5])
    data = mm.MulensData(data_list=[t, t * 0. + 16., t * 0. + 0.01])
    magnification = model.get_magnification(data.time[0])
    almost(magnification, 4.691830781584699)