MulensModel.event module

class MulensModel.event.Event(datasets=None, model=None, coords=None, fix_blend_flux=None, fix_source_flux=None, fix_source_flux_ratio=None, data_ref=0)

Bases: object

Combines a microlensing model with data. Allows calculating chi^2 and making a number of plots.

Arguments :

datasets : MulensData or list of MulensData objects,

Datasets that will be linked to the event. These datasets will be used for chi^2 calculation, plotting etc.

modelModel

Microlensing model that will be linked to the event. In order to get chi^2 for different sets of model parameters you should keep a single Model instance and change parameters for this model (i.e., do not provide separate Model instances).

coords : str, Coordinates, or astropy.SkyCoord

Coordinates of the event. If str, then needs format accepted by astropy.SkyCoord e.g., '18:00:00 -30:00:00'.

fix_blend_flux, fix_source_flux: dict

Used to fix the source flux(es) or blend flux for a particular dataset. The dataset is the key, and the value to be fixed is the value. For example, to fix the blending of some dataset my_data to zero set fix_blend_flux={my_data: 0.}. See also FitData .

fix_source_flux_ratio: dict

Used to fix the flux ratio for a given band or dataset. The keys should be either MulensData objects or str. If a MulensData object is specified, it will take precedence over a band.

fit: DEPRECATED

data_ref: int or MulensData

Reference dataset. If int then gives index of reference dataset in datasets. Default is the first dataset.

The datasets can be in magnitude or flux spaces. When we calculate chi^2 we do it in magnitude or flux space depending on value of chi2_fmt attribute. If dataset is in magnitude space and model results in negative flux, then we calculate chi^2 in flux space but only for the epochs with negative model flux.

You can print an instance of this class. Information on model and datasets will be provided.

plot(t_range=None, residuals=True, show_errorbars=None, show_bad=None, legend=True, trajectory=None, title=None, subtract_2450000=True, subtract_2460000=False, data_ref=None)

Basic plotting. Default is to plot the light curve with the residuals. If the model has 2 or more lenses, also plot the source trajectory with caustics. For more detailed control over the plotting, see plot_model(), plot_data(), and plot_trajectory().

Keywords:
t_range: list, tuple

Time range over which to show the light curve plot.

residuals: bool

Whether or not to plot the residuals with the light curve.

show_errorbars: bool

Whether or not to show the errorbars on the data. Default is None (see plot_data()).

show_bad: bool

Whether or not to show data points marked as “bad” (see bad). Default is None (see plot_data()).

legend: bool

Whether or not to show a legend for the datasets.

trajectory: bool

Whether or not to plot the source trajectory. Defaults to False for single lens events and True for binary lenses.

title: str

Title for the plot. Same title is used for trajectory plot, if applicable.

subtract_2450000, subtract_2460000: bool

see plot_data().

data_ref: int or MulensData

see plot_data().

plot_model(data_ref=None, **kwargs)

Plot the model light curve in magnitudes. See MulensModel.model.Model.plot_lc() for details.

Keywords :
data_ref: int or MulensData

If data_ref is not specified, uses data_ref.

**kwargs:

Keywords passed to MulensModel.model.Model.plot_lc(). You can use them to set time range plotted, fluxes, limb-darkening etc.

plot_data(phot_fmt='mag', data_ref=None, show_errorbars=None, show_bad=None, subtract_2450000=False, subtract_2460000=False, **kwargs)

Plot the data scaled to the model.

Keywords (all optional):
phot_fmt: string (‘mag’, ‘flux’)

Whether to plot the data in magnitudes or in flux. Default is ‘mag’.

data_ref: int or MulensData

If data_ref is not specified, uses data_ref.

show_errorbars: boolean or None

Do you want errorbars to be shown for all datasets? Default is None, which means the option is taken from each dataset plotting properties (for which default is True). If True, then data are plotted using matplotlib.errorbar(). If False, then data are plotted using matplotlib.scatter().

show_bad: boolean or None

Do you want data marked as bad to be shown? Default is None, which means the option is taken from each dataset plotting properties (for which default is False). If bad data are shown, then they are plotted with ‘x’ marker.

subtract_2450000, subtract_2460000: boolean

If True, subtracts 2450000 or 2460000 from the time axis to get more human-scale numbers. If using, make sure to also set the same settings for all other plotting calls (e.g. plot_lc()).

**kwargs:

Passed to matplotlib plotting functions. Contrary to previous behavior, **kwargs are no longer remembered.

plot_residuals(show_errorbars=None, data_ref=None, subtract_2450000=False, subtract_2460000=False, show_bad=None, **kwargs)

Plot the residuals (in magnitudes) to the model.

Keywords:

For explanation of keywords, see doctrings in plot_data(). Note different order of keywords.

plot_trajectory(**kwargs)

Plot the trajectory of the source. See MulensModel.model.Model.plot_trajectory() for details.

plot_source_for_datasets(**kwargs)

Plot source positions for all linked datasets. See MulensModel.model.Model.plot_source() for details.

Note: plots all points in datasets (including ones flagged as bad) using the same marker.

get_flux_for_dataset(dataset)

Get the source and blend flux for a given dataset.

Parameters :

dataset: MulensData or int If int should be the index (starting at 0) of the appropriate dataset in the datasets list.

Returns :
source_flux: np.ndarray

flux of sources. see source_fluxes

blend_flux: float

blending flux. see blend_flux

NOTE: This function does not recalculate fits or fluxes. If the data haven’t yet been fit to the model (i.e. self.fits = None), it will run fit_fluxes(). Otherwise, it just accesses the existing values. So if you change something in model or some fit parameter (e.g., fix_blend_flux), be sure to run fit_fluxes() first.

get_ref_fluxes(data_ref=None, fit_blending=None)

Get source and blending fluxes for the reference dataset. See get_flux_for_dataset(). If the reference dataset is not set, uses the first dataset as default. See data_ref.

get_chi2(fit_blending=None)

Calculates chi^2 of current model by fitting for source and blending fluxes.

Parameters :

fit_blending: DEPRECATED. use fix_blend_flux instead.

Returns :
chi2: float

Chi^2 value

get_chi2_for_dataset(index_dataset, fit_blending=None)

Calculates chi^2 for a single dataset

Parameters :
index_dataset: int

index that specifies for which dataset the chi^2 is requested

fit_blending: DEPRECATED. use fix_blending instead.

Returns :
chi2: float

chi2 for dataset[index_dataset].

get_chi2_per_point(fit_blending=None, bad=False)

Calculates chi^2 for each data point of the current model by fitting for source and blending fluxes.

Parameters :

fit_blending: DEPRECATED. use fix_blending instead.

bad: bool

Should chi2 be also caclulated for points marked as bad in MulensData? If False (default), then bad epochs have chi2 of np.nan.

Returns :
chi2: list of np.ndarray

Chi^2 contribution from each data point, e.g. chi2[data_num][k] returns the chi2 contribution from the k-th point of dataset data_num.

Example :

Assuming event is instance of Event class to get chi2 for 10-th point point of 0-th dataset.

chi2 = event.get_chi2_per_point()
print(chi2[0][10])
get_chi2_gradient(parameters, fit_blending=None)

Fit for fluxes and calculate chi^2 gradient (also called Jacobian), i.e., \(d chi^2/d parameter\).

Parameters :
parameters: str or list, required

Parameters with respect to which gradient is calculated. Currently accepted parameters are: t_0, u_0, t_eff, t_E, pi_E_N, and pi_E_E. The parameters for which you request gradient must be defined in py:attr:~model.

fit_blending: DEPRECATED. use fix_blending instead.

Returns :
gradient: float or np.ndarray

chi^2 gradient

calculate_chi2_gradient(parameters)

Calculate chi^2 gradient (also called Jacobian), i.e., \(d chi^2/d parameter\).

Parameters :
parameters: str or list, required

Parameters with respect to which gradient is calculated. Currently accepted parameters are: t_0, u_0, t_eff, t_E, pi_E_N, and pi_E_E. The parameters for which you request gradient must be defined in py:attr:~model.

Returns :
gradient: float or np.ndarray

chi^2 gradient

NOTE: Because this is not a ‘get’ function, it ASSUMES you have ALREADY fit for the fluxes, e.g. by calling get_chi2().

fit_fluxes(bad=False)

Fit for the optimal fluxes for each dataset (and its chi2)

property coords

see Coordinates

property model

an instance of Model

property datasets

a list of MulensData instances.

property data_ref

Reference data set for scaling the model fluxes to (for plotting). May be set as a MulensData object or an index (int). Default is the first data set.

Returns :

index (int) of the relevant dataset.

property chi2

float

Chi^2 value. Note this is a static property. It is only updated when fit_fluxes() or get_chi2() is run. So, if you change one of the settings be sure to run one of those functions to update the chi2.

property chi2_gradient

Return previously calculated chi^2 gradient (also called Jacobian), i.e., \(d chi^2/d parameter\). See get_chi2_gradient() and calculate_chi2_gradient().

Returns :
gradient: float or np.ndarray

chi^2 gradient. Will return None if the chi2 gradient was not previously calculated using one of the functions mentioned above.

property fits

list of FitData objects

There is one FitData object for each dataset containing the information for fitting the model to that dataset, e.g. fitted fluxes, chi2 (for that dataset).

property fluxes

list

An array giving the fitted source and blend flux(es) for each dataset.

property source_fluxes

list

An array giving the fitted source flux(es) for each dataset.

property blend_fluxes

list

An array giving the fitted blend flux for each dataset.

property sum_function

str

Function used for adding chi^2 contributions. Can be either ‘numpy.sum’ (default value) or ‘math.fsum’. The latter is slightly slower and more accurate, which may be important for large datasets.

reset_best_chi2()

DEPRECATED

Reset best_chi2 attribute and its parameters (best_chi2_parameters).

property best_chi2

DEPRECATED

float

The smallest value returned by get_chi2().

property best_chi2_parameters

DEPRECATED

dict

Parameters that gave the smallest chi2.