def test_mlfit_DSPL_LM_without_guess(): current_event = _create_event() model = _create_model('DSPL') model.model_dictionnary = { 'to1': 0, 'uo1': 1, 'delta_to': 2, 'uo2': 3, 'tE': 4, 'q_F_I': 5, 'fs_Test': 6, 'g_Test': 7 } fancy_namedtuple = collections.namedtuple('Parameters', model.model_dictionnary.keys()) model.pyLIMA_standard_parameters_to_fancy_parameters.return_value = fancy_namedtuple( 10.0, 0.1, 20, 0.05, 20, 0.1, 10, 5) model.parameters_boundaries = [[0, 100], [0, 1], [0, 100], [0, 1], [0, 300], [0, 1]] fit = microlfits.MLFits(current_event) fit.mlfit(model, 'LM') assert fit.fit_covariance.shape == (6 + 2 * len(current_event.telescopes), 6 + 2 * len(current_event.telescopes)) assert len(fit.fit_results) == 6 + 2 * len(current_event.telescopes) + 1
def test_LM_Jacobian(): current_event = _create_event() model = _create_model('FSPL') model.model_dictionnary = { 'to': 0, 'uo': 1, 'tE': 2, 'rho': 3, 'fs_Test': 4, 'g_Test': 5 } fit = microlfits.MLFits(current_event) fit.model = model to = 0.0 uo = 0.1 tE = 1.0 rho = 0.26 fs = 10 g = 1.0 parameters = [to, uo, tE, rho, fs, g] Jacobian = fit.LM_Jacobian(parameters) assert Jacobian.shape == (6, len(current_event.telescopes[0].lightcurve_flux))
def test_chichi_telescopes(): current_event = _create_event() model = _create_model('FSPL') model.model_dictionnary = { 'to': 0, 'uo': 1, 'tE': 2, 'rho': 3, 'fs_Test': 4, 'g_Test': 5 } fit = microlfits.MLFits(current_event) fit.model = model to = 0.0 uo = 0.1 tE = 1.0 rho = 0.26 fs = 10 g = 1.0 parameters = [to, uo, tE, rho, fs, g] chichi_telescopes = fit.chichi_telescopes(parameters) chichi = sum(fit.residuals_LM(parameters)**2) assert len(chichi_telescopes) == 1 assert np.allclose(chichi, chichi_telescopes[0])
def test_mlfit_FSPL_LM_with_guess(): current_event = _create_event() model = _create_model('FSPL') model.parameters_guess = [10, 0.1, 20, 0.02] model.model_dictionnary = { 'to': 0, 'uo': 1, 'tE': 2, 'rho': 3, 'fs_Test': 4, 'g_Test': 5 } fancy_namedtuple = collections.namedtuple('Parameters', model.model_dictionnary.keys()) model.pyLIMA_standard_parameters_to_fancy_parameters.return_value = fancy_namedtuple( 10.0, 0.1, 20, 0.05, 10, 5) model.model_parameters = collections.namedtuple('parameters', model.model_dictionnary) model.parameters_boundaries = [[0, 100], [0, 1], [0, 300], [0, 1]] fit = microlfits.MLFits(current_event) fit.mlfit(model, 'LM') print fit.fit_results assert fit.fit_covariance.shape == (4 + 2 * len(current_event.telescopes), 4 + 2 * len(current_event.telescopes)) assert len(fit.fit_results) == 4 + 2 * len(current_event.telescopes) + 1
def test_check_fit_bad_rho(): current_event = _create_event() model = _create_model('FSPL') model.model_dictionnary = { 'to': 0, 'uo': 1, 'tE': 2, 'rho': 3, 'fs_Test': 4, 'g_Test': 5 } fit = microlfits.MLFits(current_event) fit.model = model fit.fit_results = [0.0, 0.0, 0.0, -1.0, 1.0, 0.0] flag = fit.check_fit() assert flag == 'Bad Fit' fit.fit_results = [0.0, 0.0, 0.0, 0.8, 1.0, 0.0] flag = fit.check_fit() assert flag == 'Bad Fit' fit.fit_results = [0.0, 0.0, 0.0, 0.05, 1.0, 0.0] flag = fit.check_fit() assert flag == 'Good Fit'
def test_mlfit_PSPL_MCMC_with_guess(): current_event = _create_event() model = _create_model('PSPL') model.parameters_guess = [10, 0.1, 20] fit = microlfits.MLFits(current_event) fit.mlfit(model, 'MCMC') assert fit.MCMC_chains.shape == (200, 100, 5)
def test_mlfit_PSPL_LM_without_guess(): current_event = _create_event() model = _create_model('PSPL') fit = microlfits.MLFits(current_event) fit.mlfit(model, 'LM') assert fit.fit_covariance.shape == (3 + 2 * len(current_event.telescopes), 3 + 2 * len(current_event.telescopes)) assert len(fit.fit_results) == 3 + 2 * len(current_event.telescopes) + 1
def fit(self, model, method, DE_population_size=10, flux_estimation_MCMC='MCMC', fix_parameters_dictionnary=None, grid_resolution=10, computational_pool=None, binary_regime=None): """Function to fit the event with a model and a method. :param model: the model you want to fit. More details in the microlfits module :param method: the fitting method you want to use. Has to be a string in the available_methods parameter: 'LM' : Levenberg-Marquardt algorithm 'DE' : Differential Evolution algorithm 'MCMC' : Monte-Carlo Markov Chain algorithm More details in the microlfits module A microlfits object is added in the event.fits list. For example, if you request two fits, you will obtain : event.fits=[fit1,fit2] More details in the microlfits module. """ available_kind = ['Microlensing'] available_methods = ['LM', 'DE', 'MCMC', 'GRIDS', 'TRF'] if self.kind not in available_kind: print( 'ERROR : No possible fit yet for a non microlensing event, sorry :(' ) raise EventException('Can not fit this event kind') if method not in available_methods: print('ERROR : Wrong method request, has to be a string selected between ' + \ ' or '.join(available_methods) + '') raise EventException('Wrong fit method request') fit = microlfits.MLFits(self) fit.mlfit(model, method, DE_population_size=DE_population_size, flux_estimation_MCMC=flux_estimation_MCMC, fix_parameters_dictionnary=fix_parameters_dictionnary, grid_resolution=grid_resolution, computational_pool=computational_pool, binary_regime=binary_regime) self.fits.append(fit)
def test_check_fit_bad_covariance(): current_event = _create_event() model = _create_model('PSPL') fit = microlfits.MLFits(current_event) fit.fit_covariance = np.array([[-1.0, 0.0], [0.0, 0.0]]) flag = fit.check_fit() assert flag == 'Bad Fit'
def test_mlfit_FSPL_LM_with_guess(): current_event = _create_event() model = microlmodels.create_model('FSPL',current_event) model.parameters_boundaries = [[0, 100], [0, 1], [0, 300], [0, 1]] fit = microlfits.MLFits(current_event) fit.mlfit(model, 'LM') assert fit.fit_covariance.shape == (4 + 2 * len(current_event.telescopes), 4 + 2 * len(current_event.telescopes)) assert len(fit.fit_results) == 4 + 2 * len(current_event.telescopes) + 1
def test_mlfit_PSPL_LM_with_guess(): current_event = _create_event() model = microlmodels.create_model('PSPL',current_event) model.parameters_guess = [10, 0.1, 20] fit = microlfits.MLFits(current_event) fit.mlfit(model, 'LM') assert fit.fit_covariance.shape == (3 + 2 * len(current_event.telescopes), 3 + 2 * len(current_event.telescopes)) assert len(fit.fit_results) == 3 + 2 * len(current_event.telescopes) + 1
def test_check_fit_source_flux(): current_event = _create_event() model = microlmodels.create_model('FSPL', current_event) model.define_model_parameters() fit = microlfits.MLFits(current_event) fit.model = model fit.fit_results = [0.0, 0.0, 0.0, 0.0, 1.0, 0.0] flag = fit.check_fit() assert flag == 'Good Fit' fit.fit_results = [0.0, 0.0, 0.0, 0.8, -1.0, 0.0] flag = fit.check_fit() assert flag == 'Bad Fit'
def test_LM_Jacobian(): current_event = _create_event() model = microlmodels.create_model('FSPL',current_event) model.define_model_parameters() fit = microlfits.MLFits(current_event) fit.model = model to = 0.0 uo = 0.1 tE = 1.0 rho = 0.26 fs = 10 g = 1.0 parameters = [to, uo, tE, rho, fs, g] Jacobian = fit.LM_Jacobian(parameters) Jacobian = Jacobian.T assert Jacobian.shape == (6, len(current_event.telescopes[0].lightcurve_flux))
def test_chichi_telescopes(): current_event = _create_event() model = microlmodels.create_model('FSPL', current_event) model.define_model_parameters() fit = microlfits.MLFits(current_event) fit.model = model to = 0.0 uo = 0.1 tE = 1.0 rho = 0.26 fs = 10 g = 1.0 parameters = [to, uo, tE, rho, fs, g] chichi_telescopes = fit.chichi_telescopes(parameters) chichi = sum(fit.residuals_LM(parameters)**2) assert len(chichi_telescopes) == 1 assert np.allclose(chichi, chichi_telescopes[0])
def create_model(e, params, diagnostics=False): f = microlfits.MLFits(e) parallax_params = ['None', params['to']] orbital_params = ['None', params['to']] model_params = ['to', 'uo', 'tE'] if params['rho'] != None: model_params.append('rho') if params['logs'] != None and params['logq'] != None and params[ 'alpha'] != None: model_params.append('logs') model_params.append('logq') model_params.append('alpha') if params['piEN'] != None and params['piEE'] != None: parallax_params = ['Full', params['to']] model_params.append('piEN') model_params.append('piEE') if params['sdsdt'] != None and params['dalphadt'] != None and params[ 'sdszdt'] != None: orbital_params = ['2D', params['to']] model_params.append('sdsdt') model_params.append('dalphadt') model_params.append('sdszdt') params['model_params'] = model_params model = microlmodels.create_model(params['model_type'], e, parallax=parallax_params, orbital_motion=orbital_params, blend_flux_ratio=False) if 'binary origin' in params.keys(): model.binary_origin = params['binary_origin'] model.define_model_parameters() f.model = model results = [] for key in model_params: results.append(params[key]) params['fitted_model_params'] = results results = results + params['phot_params'] results.append(params['chisq']) f.fit_results = results e.fits.append(f) if diagnostics: fig = microloutputs.LM_plot_lightcurves(f) plt.show() plt.close() return e, params
def simulate_ffp(): spring = True fall = False u0 = 0.01 tE = 1.0 if spring: t0 = 2460394.400000 start_jd = 2460389.500000 # 2024 March 20 end_jd = 2460399.500000 if fall: t0 = 2460389.500000 start_jd = 2460573.500000 # 2024 Aug 20 end_jd = 2460583.500000 # 2024 Aug 30 ra = 270.0 dec = -27.0 piEN = 0.01 piEE = 0.01 blend_ratio = 0.2 source_flux = mag_to_flux(22.0,1.0,24.0) blend_flux = source_flux * blend_ratio model_params = [ t0, u0, tE, piEN, piEE ] lsst_aperture = 6.68 lsst_read_noise = 10.0 wfirst_aperture = 2.4 wfirst_read_noise = 10.0 if spring: horizons_file = 'wfirst_observer_table_spring.txt' if fall: horizons_file = 'wfirst_observer_table_fall.txt' ffp_lsst = event.Event() ffp_lsst.name = 'FFP' ffp_lsst.ra = ra ffp_lsst.dec = dec lsst_lc = simulate_lightcurve(start_jd, end_jd, 0.5/24.0, lsst_aperture, spring, fall, day_gaps=True ) lsst = telescopes.Telescope(name='LSST', camera_filter='i', location='Earth', light_curve_magnitude=lsst_lc) ffp_lsst.telescopes.append(lsst) model_lsst = microlmodels.create_model('PSPL', ffp_lsst, parallax=['Full', t0]) model_lsst.define_model_parameters() fit_lsst = microlfits.MLFits(ffp_lsst) fit_lsst.model = model_lsst fit_lsst.fit_results = model_params ffp_lsst.fits.append(fit_lsst) print('Generated event lightcurve from LSST') print('Model parameters:') print_fit_params(model_params) ffp_wfirst = event.Event() ffp_wfirst.name = 'FFP' ffp_wfirst.ra = ra ffp_wfirst.dec = dec horizons_table = jplhorizons_utils.parse_JPL_Horizons_table(horizons_file_path=horizons_file, table_type='OBSERVER') spacecraft_positions = jplhorizons_utils.extract_spacecraft_positions(horizons_table,t0) wfirst_lc = simulate_lightcurve(start_jd, end_jd, 0.25/24.0, wfirst_aperture, spring, fall) wfirst = telescopes.Telescope(name='WFIRST', camera_filter='W149', spacecraft_name = 'WFIRST', location='Space', light_curve_magnitude=wfirst_lc) wfirst.spacecraft_positions = spacecraft_positions ffp_wfirst.telescopes.append(wfirst) model_wfirst = microlmodels.create_model('PSPL', ffp_wfirst, parallax=['Full', t0]) model_wfirst.define_model_parameters() fit_wfirst = microlfits.MLFits(ffp_wfirst) fit_wfirst.model = model_wfirst fit_wfirst.fit_results = model_params ffp_wfirst.fits.append(fit_wfirst) print('Generated event lightcurve from WFIRST') print('Model parameters:') print_fit_params(fit_wfirst) parameters = [ t0, u0, tE ] lsst_pylima_params = extract_event_parameters(ffp_lsst, fit_lsst, parameters, source_flux, blend_ratio) wfirst_pylima_params = extract_event_parameters(ffp_wfirst, fit_wfirst, parameters, source_flux, blend_ratio) lsst_lc = add_lensing_event_to_lightcurve(lsst_pylima_params, ffp_lsst, fit_lsst, lsst_read_noise) wfirst_lc = add_lensing_event_to_lightcurve(wfirst_pylima_params, ffp_wfirst, fit_wfirst, wfirst_read_noise) plot_fitted_lightcurves(lsst_lc, wfirst_lc, ffp_lsst, ffp_wfirst, 'LSST', 'WFIRST', 'ffp_sim_lightcurve.png', t0=t0,tE=tE)