ebachelet, updated 🕥 2023-03-21 00:10:35

Build Status Coverage Status DOI


Authors : Etienne Bachelet, [email protected] Rachel Street, [email protected] Valerio Bozza, [email protected] Martin Norbury, [email protected] and friends!

pyLIMA is an open source for modeling microlensing events. It should be flexible enough to handle your data and fit it. You can also practice by simulating events.

Documentation and Installation


Required materials

You need pip or you can install manually the required libraries Documentation

pyLIMA should now run both on python3 (python2 is not supported anymore, time for update!).

Installation and use

Lot of efforts have been made to have pyLIMA (and VBBinaryLensing) install through pip:


pip install pyLIMA ```

This new procedure which should avoid the previous installations headaches! Successfully test on various UNIX, MAC and Windows! If you encounter any problems, please contact [email protected]

You should be able to load pyLIMA as general module : python from pyLIMA import microlmagnification


Examples can be found in your pyLIMA directory. Look on the documentation to learn how to run it. There is two version for each examples, one using Jupyter notebook (.ipynb) or classic Python file (.py).

Example_1 : HOW TO FIT MY DATA?





What can you do?

pyLIMA is now in beta!! Here is the status of implemented microlensing models:

| Model | Implemented | Examples | Fit Method Advice | | :--- | :---: |:---: | ---: | | Point-Source Point Lens (PSPL) | Alt text | Yes | Levenberg-Marquardt (LM) | | Finite-Source Point Lens (FSPL) | Alt text | Yes | Levenberg-Marquardt (LM) or Differential Evolution (DE) | | Double-Source Point Lens (DSPL) | Alt text | Yes | Differential Evolution (DE) | | Uniform-Source Binary Lens (USBL) | Alt text | No | |

pyLIMA can also treat Second Order effects :

| Second-Order Effects | Implemented | Examples |Fit Method Advice | | :--- | :---: | :---: | ---: | | Annual parallax | Alt text | No | Levenberg-Marquardt (LM) | | Terrestrial parallax | Alt text | No | Levenberg-Marquardt (LM) | | Space parallax | Alt text | No| Levenberg-Marquardt (LM) | | Orbital Motion | Alt text | No | | | Xallarap | Alt text | No | |

How to contribute?

Want to contribute? Bug detections? Comments? Please email us : [email protected], [email protected], [email protected]


New example 5: KeyError: 'photometry'

opened on 2023-02-15 21:08:50 by anibal-art

Hello, I got this error when I run the new_example 5 in the last part of the notebook:

pyLIMA_plots.list_of_fake_telescopes = [] pyLIMA_plots.plot_lightcurves(usbl, fit_2.fit_results['best_model']) pyLIMA_plots.plot_geometry(usbl, fit_2.fit_results['best_model']) plt.show()

KeyError Traceback (most recent call last) Cell In [31], line 2 1 pyLIMA_plots.list_of_fake_telescopes = [] ----> 2 pyLIMA_plots.plot_lightcurves(usbl, fit_2.fit_results['best_model']) 3 pyLIMA_plots.plot_geometry(usbl, fit_2.fit_results['best_model']) 4 plt.show()

File ~/.conda/envs/my_environment/lib/python3.10/site-packages/pyLIMA-0.8.3-py3.10.egg/pyLIMA/outputs/pyLIMA_plots.py:263, in plot_lightcurves(microlensing_model, model_parameters) 259 figure, figure_axes = initialize_light_curves_plot(event_name=microlensing_model.event.name) 261 if len(model_parameters) != len(microlensing_model.model_dictionnary): --> 263 telescopes_fluxes = microlensing_model.find_telescopes_fluxes(model_parameters) 265 model_parameters = np.r_[model_parameters,telescopes_fluxes] 267 plot_photometric_models(figure_axes[0], microlensing_model, model_parameters, plot_unit='Mag')

File ~/.conda/envs/my_environment/lib/python3.10/site-packages/pyLIMA-0.8.3-py3.10.egg/pyLIMA/models/ML_model.py:365, in MLmodel.find_telescopes_fluxes(self, fancy_parameters) 361 for telescope in self.event.telescopes: 363 if telescope.lightcurve_flux is not None: --> 365 model = self.compute_the_microlensing_model(telescope, pyLIMA_parameters) 367 fluxes.append(model['f_source']) 368 fluxes.append(model['f_blend'])

File ~/.conda/envs/my_environment/lib/python3.10/site-packages/pyLIMA-0.8.3-py3.10.egg/pyLIMA/models/ML_model.py:277, in MLmodel.compute_the_microlensing_model(self, telescope, pyLIMA_parameters) 267 def compute_the_microlensing_model(self, telescope, pyLIMA_parameters): 268 """ Compute the microlens model according the injected parameters. This is modified by child submodel sublclass, 269 if not the default microlensing model is returned. 270 (...) 274 :rtype: array_like 275 """ --> 277 return self._default_microlensing_model(telescope, pyLIMA_parameters)

File ~/.conda/envs/my_environment/lib/python3.10/site-packages/pyLIMA-0.8.3-py3.10.egg/pyLIMA/models/ML_model.py:300, in MLmodel._default_microlensing_model(self, telescope, pyLIMA_parameters) 296 f_blend = None 298 if telescope.lightcurve_flux is not None: --> 300 magnification = self.model_magnification(telescope, pyLIMA_parameters) 302 f_source, f_blend = self.derive_telescope_flux(telescope, pyLIMA_parameters, magnification) 303 photometric_model = f_source * magnification + f_blend

File ~/.conda/envs/my_environment/lib/python3.10/site-packages/pyLIMA-0.8.3-py3.10.egg/pyLIMA/models/USBL_model.py:57, in USBLmodel.model_magnification(self, telescope, pyLIMA_parameters, return_impact_parameter) 53 if telescope.lightcurve_flux is not None: 55 self.u0_t0_from_uc_tc(pyLIMA_parameters) ---> 57 source_trajectoire = self.source_trajectory(telescope, pyLIMA_parameters, data_type='photometry') 59 separation = source_trajectoire[2] + pyLIMA_parameters.separation 60 magnification_USBL = \ 61 magnification_VBB.magnification_USBL(separation, pyLIMA_parameters.mass_ratio, 62 source_trajectoire[0], source_trajectoire[1], 63 pyLIMA_parameters.rho)

File ~/.conda/envs/my_environment/lib/python3.10/site-packages/pyLIMA-0.8.3-py3.10.egg/pyLIMA/models/ML_model.py:462, in MLmodel.source_trajectory(self, telescope, pyLIMA_parameters, data_type) 458 time = lightcurve['time'].value 460 if 'piEN' in pyLIMA_parameters._fields: --> 462 delta_positions = telescope.deltas_positions['photometry'] 464 if data_type == 'astrometry': 466 astrometry = telescope.astrometry

KeyError: 'photometry'

Also when I set perform_long_fit = True when I try to fit as a binary event:

RemoteTraceback Traceback (most recent call last) RemoteTraceback: """ Traceback (most recent call last): File "/home/anibal-art/.conda/envs/my_environment/lib/python3.10/multiprocessing/pool.py", line 125, in worker result = (True, func(args, kwds)) File "/home/anibal-art/.conda/envs/my_environment/lib/python3.10/multiprocessing/pool.py", line 48, in mapstar return list(map(args)) File "/home/anibal-art/.conda/envs/my_environment/lib/python3.10/site-packages/scipy/_lib/_util.py", line 407, in call return self.f(x, *self.args) File "/home/anibal-art/.conda/envs/my_environment/lib/python3.10/site-packages/pyLIMA-0.8.3-py3.10.egg/pyLIMA/fits/DE_fit.py", line 55, in objective_function photometric_likelihood = pyLIMA.fits.objective_functions.all_telescope_photometric_likelihood( File "/home/anibal-art/.conda/envs/my_environment/lib/python3.10/site-packages/pyLIMA-0.8.3-py3.10.egg/pyLIMA/fits/objective_functions.py", line 151, in all_telescope_photometric_likelihood residus, errflux = all_telescope_photometric_residuals(model, pyLIMA_parameters, norm=True, File "/home/anibal-art/.conda/envs/my_environment/lib/python3.10/site-packages/pyLIMA-0.8.3-py3.10.egg/pyLIMA/fits/objective_functions.py", line 115, in all_telescope_photometric_residuals microlensing_model = model.compute_the_microlensing_model(telescope, pyLIMA_parameters) File "/home/anibal-art/.conda/envs/my_environment/lib/python3.10/site-packages/pyLIMA-0.8.3-py3.10.egg/pyLIMA/models/ML_model.py", line 277, in compute_the_microlensing_model return self._default_microlensing_model(telescope, pyLIMA_parameters) File "/home/anibal-art/.conda/envs/my_environment/lib/python3.10/site-packages/pyLIMA-0.8.3-py3.10.egg/pyLIMA/models/ML_model.py", line 300, in _default_microlensing_model magnification = self.model_magnification(telescope, pyLIMA_parameters) File "/home/anibal-art/.conda/envs/my_environment/lib/python3.10/site-packages/pyLIMA-0.8.3-py3.10.egg/pyLIMA/models/USBL_model.py", line 57, in model_magnification source_trajectoire = self.source_trajectory(telescope, pyLIMA_parameters, data_type='photometry') File "/home/anibal-art/.conda/envs/my_environment/lib/python3.10/site-packages/pyLIMA-0.8.3-py3.10.egg/pyLIMA/models/ML_model.py", line 462, in source_trajectory delta_positions = telescope.deltas_positions['photometry'] KeyError: 'photometry' """

The above exception was the direct cause of the following exception:

KeyError Traceback (most recent call last) Cell In [33], line 3 1 ### Fit the model: 2 if perform_long_fit == True: ----> 3 fit_2.fit(computational_pool = pool) 5 # Save it 6 np.save('results_USBL_DE_966.npy', fit_2.fit_results['DE_population'])

File ~/.conda/envs/my_environment/lib/python3.10/site-packages/pyLIMA-0.8.3-py3.10.egg/pyLIMA/fits/DE_fit.py:119, in DEfit.fit(self, initial_population, computational_pool) 115 init = initial_population 117 bounds = [self.fit_parameters[key][1] for key in self.fit_parameters.keys()] --> 119 differential_evolution_estimation = scipy.optimize.differential_evolution(self.objective_function, 120 bounds=bounds, 121 mutation=(0.5, 1.5), popsize=int(self.DE_population_size), 122 maxiter=self.max_iteration, tol=0.00, 123 atol=1.0, strategy='rand1bin', 124 recombination=0.5, polish=False, init=init, 125 disp=self.display_progress, workers=worker) 127 print('DE converge to objective function : f(x) = ', str(differential_evolution_estimation['fun'])) 128 print('DE converge to parameters : = ', differential_evolution_estimation['x'].astype(str))

File ~/.conda/envs/my_environment/lib/python3.10/site-packages/scipy/optimize/_differentialevolution.py:329, in differential_evolution(func, bounds, args, strategy, maxiter, popsize, tol, mutation, recombination, seed, callback, disp, polish, init, atol, updating, workers, constraints, x0) 314 # using a context manager means that any created Pool objects are 315 # cleared up. 316 with DifferentialEvolutionSolver(func, bounds, args=args, 317 strategy=strategy, 318 maxiter=maxiter, (...) 327 constraints=constraints, 328 x0=x0) as solver: --> 329 ret = solver.solve() 331 return ret

File ~/.conda/envs/my_environment/lib/python3.10/site-packages/scipy/optimize/_differentialevolution.py:843, in DifferentialEvolutionSolver.solve(self) 838 self.feasible, self.constraint_violation = ( 839 self._calculate_population_feasibilities(self.population)) 841 # only work out population energies for feasible solutions 842 self.population_energies[self.feasible] = ( --> 843 self._calculate_population_energies( 844 self.population[self.feasible])) 846 self._promote_lowest_energy() 848 # do the optimization.

File ~/.conda/envs/my_environment/lib/python3.10/site-packages/scipy/optimize/_differentialevolution.py:968, in DifferentialEvolutionSolver._calculate_population_energies(self, population) 966 parameters_pop = self._scale_parameters(population) 967 try: --> 968 calc_energies = list(self._mapwrapper(self.func, 969 parameters_pop[0:nfevs])) 970 calc_energies = np.squeeze(calc_energies) 971 except (TypeError, ValueError) as e: 972 # wrong number of arguments for _mapwrapper 973 # or wrong length returned from the mapper

File ~/.conda/envs/my_environment/lib/python3.10/site-packages/scipy/_lib/_util.py:477, in MapWrapper.call(self, func, iterable) 474 def call(self, func, iterable): 475 # only accept one iterable because that's all Pool.map accepts 476 try: --> 477 return self._mapfunc(func, iterable) 478 except TypeError as e: 479 # wrong number of arguments 480 raise TypeError("The map-like callable must be of the" 481 " form f(func, iterable)") from e

File ~/.conda/envs/my_environment/lib/python3.10/multiprocessing/pool.py:367, in Pool.map(self, func, iterable, chunksize) 362 def map(self, func, iterable, chunksize=None): 363 ''' 364 Apply func to each element in iterable, collecting the results 365 in a list that is returned. 366 ''' --> 367 return self._map_async(func, iterable, mapstar, chunksize).get()

File ~/.conda/envs/my_environment/lib/python3.10/multiprocessing/pool.py:774, in ApplyResult.get(self, timeout) 772 return self._value 773 else: --> 774 raise self._value

KeyError: 'photometry'

Thanks for the help!

Example 1 input data

opened on 2023-01-22 18:11:16 by eddiemorris135

thanks for placing this tool in the public domain.

As a hobbyist I am using it to learn more about microlensing. However, I am struggling to find suitable data would replace 'Survey_1.dat' for example.

I attempted to use the following database: https://exoplanetarchive.ipac.caltech.edu/cgi-bin/TblSearch/nph-tblSearchInit?app=ExoTbls&config=keplerstellar with kepler ID 11904151

but the resulting PDCSAP_FLUX_ERR array is not of the same length as the PDCSAP_FLUX array.

Perhaps there are more convenient database sources? Thanks Ed

Availability for python 3.11

opened on 2023-01-11 02:55:06 by jluastro

Any chance there will be a pylima pip or conda installer for python 3.11 soon?

Add easy-to-access version string to PyLIMA

opened on 2023-01-05 04:28:13 by rachel3834

It has become customary that after importing a package, typing ipython> pyLIMA.version ...should return the current version of the installed package, for user reference.
As far as I can tell, PyLIMA doesn't have this yet, so I request that this be added.

Saving locally the "fit" object (not just the output)

opened on 2022-06-28 14:12:17 by petro121

Users may need to save the resulting "fit" object (not just the output in html, pdf or other format) locally on their machine (for example, to perform their own visualizations later). For now it is possible only to locally save the MCMC chains. However, if you need the "fit" object after you finished your session, you need to rerun the whole fit, which may take quite a while to be calculated, if the fit is complex.

Maybe this will be of use: https://stackoverflow.com/questions/4529815/saving-an-object-data-persistence

Add support for different date types (JD, MJD, HJD, BJD)

opened on 2022-03-29 13:56:31 by petro121

Even though in pyLIMA there are no absolute definition for which date type to use, some researches might like to account for subtle differences in time for different date types (Julian Date, Modified Julian Date, Heliocentric Julian Date, Barycentric Julian Date). This need is produced by the fact that there are some subtle effects that are mildly impacted by this, such as the "Earth parallax".


pyLIMA first release 2017-09-25 18:03:15