Пример #1
0
def LM_plot_model(fit, figure_axe):
    """Plot the microlensing model from the fit.

    :param object fit: a fit object. See the microlfits for more details.
    :param matplotlib_axes figure_axe: a matplotlib axes correpsonding to the figure.
    """
    pyLIMA_parameters = fit.model.compute_pyLIMA_parameters(fit.fit_results)
    min_time = min([min(i.lightcurve_magnitude[:, 0]) for i in fit.event.telescopes])
    max_time = max([max(i.lightcurve_magnitude[:, 0]) for i in fit.event.telescopes])

    time = np.linspace(min_time, max_time + 100, 3000)
    extra_time = np.linspace(pyLIMA_parameters.to - 2 * pyLIMA_parameters.tE,
                             pyLIMA_parameters.to + 2 * pyLIMA_parameters.tE, 30000)
    time = np.sort(np.append(time, extra_time))
    reference_telescope = copy.copy(fit.event.telescopes[0])
    reference_telescope.lightcurve_magnitude = np.array(
        [time, [0] * len(time), [0] * len(time)]).T
    reference_telescope.lightcurve_flux = reference_telescope.lightcurve_in_flux()

    if fit.model.parallax_model[0] != 'None':
        reference_telescope.compute_parallax(fit.event, fit.model.parallax_model)

    flux_model = fit.model.compute_the_microlensing_model(reference_telescope, pyLIMA_parameters)[0]
    magnitude = microltoolbox.flux_to_magnitude(flux_model)

    figure_axe.plot(time, magnitude, '--k', label=fit.model.model_type, lw=2)
    figure_axe.set_ylim(
        [min(magnitude) - plot_lightcurve_windows, max(magnitude) + plot_lightcurve_windows])
    figure_axe.set_xlim(
        [pyLIMA_parameters.to-3*pyLIMA_parameters.tE, pyLIMA_parameters.to+3*pyLIMA_parameters.tE+100])
    figure_axe.invert_yaxis()
    figure_axe.text(0.01, 0.96, 'provided by pyLIMA', style='italic', fontsize=10,
                    transform=figure_axe.transAxes)
Пример #2
0
def align_telescope_lightcurve(lightcurve_telescope_flux, model_ghost, model_telescope):
    """Align data to the survey telescope (i.e telescope 0).

    :param array_like lightcurve_telescope_mag: the survey telescope in magnitude
    :param float fs_reference: thce survey telescope reference source flux (i.e the fitted value)
    :param float g_reference: the survey telescope reference blending parameter (i.e the fitted
    value)
    :param float fs_telescope: the telescope source flux (i.e the fitted value)
    :param float g_reference: the telescope blending parameter (i.e the fitted value)

    :return: the aligned to survey lightcurve in magnitude
    :rtype: array_like
    """
    time = lightcurve_telescope_flux[:, 0]
    flux = lightcurve_telescope_flux[:, 1]
    error_flux = lightcurve_telescope_flux[:, 2]
    err_mag = microltoolbox.error_flux_to_error_magnitude(error_flux, flux)

    residuals = 2.5 * np.log10(model_telescope / flux)


    magnitude_normalised = microltoolbox.flux_to_magnitude(model_ghost)+residuals

    lightcurve_normalised = [time, magnitude_normalised, err_mag]

    lightcurve_mag_normalised = np.array(lightcurve_normalised).T

    return lightcurve_mag_normalised
Пример #3
0
def align_telescope_lightcurve(lightcurve_telescope_flux, model_ghost, model_telescope):
    """Align data to the survey telescope (i.e telescope 0).

    :param array_like lightcurve_telescope_mag: the survey telescope in magnitude
    :param float fs_reference: thce survey telescope reference source flux (i.e the fitted value)
    :param float g_reference: the survey telescope reference blending parameter (i.e the fitted
    value)
    :param float fs_telescope: the telescope source flux (i.e the fitted value)
    :param float g_reference: the telescope blending parameter (i.e the fitted value)

    :return: the aligned to survey lightcurve in magnitude
    :rtype: array_like
    """
    time = lightcurve_telescope_flux[:, 0]
    flux = lightcurve_telescope_flux[:, 1]
    error_flux = lightcurve_telescope_flux[:, 2]
    err_mag = microltoolbox.error_flux_to_error_magnitude(error_flux, flux)

    residuals = 2.5 * np.log10(model_telescope / flux)


    magnitude_normalised = microltoolbox.flux_to_magnitude(model_ghost)+residuals

    lightcurve_normalised = [time, magnitude_normalised, err_mag]

    lightcurve_mag_normalised = np.array(lightcurve_normalised).T

    return lightcurve_mag_normalised
Пример #4
0
def LM_plot_model(fit, figure_axe):
    """Plot the microlensing model from the fit.

    :param object fit: a fit object. See the microlfits for more details.
    :param matplotlib_axes figure_axe: a matplotlib axes correpsonding to the figure.
    """
    pyLIMA_parameters = fit.model.compute_pyLIMA_parameters(fit.fit_results)
    min_time = min([min(i.lightcurve_magnitude[:, 0]) for i in fit.event.telescopes])
    max_time = max([max(i.lightcurve_magnitude[:, 0]) for i in fit.event.telescopes])

    time = np.linspace(min_time, max_time + 100, 30000)
    extra_time = np.linspace(pyLIMA_parameters.to - 2 * pyLIMA_parameters.tE,
                             pyLIMA_parameters.to + 2 * pyLIMA_parameters.tE, 30000)
    time = np.sort(np.append(time, extra_time))
    reference_telescope = copy.copy(fit.event.telescopes[0])
    reference_telescope.lightcurve_magnitude = np.array(
        [time, [0] * len(time), [0] * len(time)]).T
    reference_telescope.lightcurve_flux = reference_telescope.lightcurve_in_flux()

    if fit.model.parallax_model[0] != 'None':
        reference_telescope.compute_parallax(fit.event, fit.model.parallax_model)

    flux_model = fit.model.compute_the_microlensing_model(reference_telescope, pyLIMA_parameters)[0]
    magnitude = microltoolbox.flux_to_magnitude(flux_model)

    figure_axe.plot(time, magnitude, '--k', label=fit.model.model_type, lw=2)
    figure_axe.set_ylim(
        [min(magnitude) - plot_lightcurve_windows, max(magnitude) + plot_lightcurve_windows])
    figure_axe.set_xlim(
        [pyLIMA_parameters.to-3*pyLIMA_parameters.tE, pyLIMA_parameters.to+3*pyLIMA_parameters.tE+100])
    figure_axe.invert_yaxis()
    figure_axe.text(0.01, 0.96, 'provided by pyLIMA', style='italic', fontsize=10,
                    transform=figure_axe.transAxes)
Пример #5
0
def MCMC_plot_model(fit, reference_telescope, parameters, couleurs, figure_axes, scalar_couleur_map):
    """ Plot a  model to a given figure, with the color corresponding to the objective function
    of the model.

    :param fit: a fit object. See the microlfits for more details.
    :param parameters: the parameters [list] of the model you want to plot.
    :param couleurs: the values of the objective function for the model that match the color
    table scalar_couleur_map
    :param figure_axes: the axes where the plot are draw
    :param scalar_couleur_map: a matplotlib table that return a color given a scalar value (the
    objective function here)
    """

    pyLIMA_parameters = fit.model.compute_pyLIMA_parameters(parameters)
    flux_model = fit.model.compute_the_microlensing_model(reference_telescope, pyLIMA_parameters)[0]

    magnitude_model = microltoolbox.flux_to_magnitude(flux_model)

    figure_axes.plot(reference_telescope.lightcurve_magnitude[:, 0], magnitude_model,
                     color=scalar_couleur_map.to_rgba(couleurs),
                     alpha=0.5)
Пример #6
0
def MCMC_plot_model(fit, reference_telescope, parameters, couleurs, figure_axes, scalar_couleur_map):
    """ Plot a  model to a given figure, with the color corresponding to the objective function
    of the model.

    :param fit: a fit object. See the microlfits for more details.
    :param parameters: the parameters [list] of the model you want to plot.
    :param couleurs: the values of the objective function for the model that match the color
    table scalar_couleur_map
    :param figure_axes: the axes where the plot are draw
    :param scalar_couleur_map: a matplotlib table that return a color given a scalar value (the
    objective function here)
    """

    pyLIMA_parameters = fit.model.compute_pyLIMA_parameters(parameters)
    flux_model = fit.model.compute_the_microlensing_model(reference_telescope, pyLIMA_parameters)[0]

    magnitude_model = microltoolbox.flux_to_magnitude(flux_model)

    figure_axes.plot(reference_telescope.lightcurve_magnitude[:, 0], magnitude_model,
                     color=scalar_couleur_map.to_rgba(couleurs),
                     alpha=0.5)
Пример #7
0
    def lightcurve_in_magnitude(self):
        """
        Transform flux to magnitude using m = 27.4-2.5*log10(flux) convention. Transform error bar
        accordingly. More details in microltoolbox module.

        :return: the lightcurve in magnitude, lightcurve_magnitude.
        :rtype: array_like
        """
        lightcurve = self.lightcurve_flux

        time = lightcurve[:, 0]
        flux = lightcurve[:, 1]
        error_flux = lightcurve[:, 2]

        magnitude = microltoolbox.flux_to_magnitude(flux)
        error_magnitude = microltoolbox.error_flux_to_error_magnitude(error_flux, flux)

        ligthcurve_magnitude = np.array([time, magnitude, error_magnitude]).T



        return ligthcurve_magnitude
Пример #8
0
def initial_guess_PSPL(event):
    """Function to find initial PSPL guess for Levenberg-Marquardt solver (method=='LM').
       This assumes no blending.

       :param object event: the event object on which you perform the fit on. More details on the
       event module.

       :return: the PSPL guess for this event.A list with Paczynski parameters (to,uo,tE) and the
       source flux of the survey telescope.
       :rtype: list,float
    """

    # to estimation
    to_estimations = []
    maximum_flux_estimations = []
    errors_magnitude = []

    for telescope in event.telescopes:
        # Lot of process here, if one fails, just skip
        lightcurve_magnitude = telescope.lightcurve_magnitude
        mean_error_magnitude = np.mean(lightcurve_magnitude[:, 2])
        try:

            # only the best photometry
            good_photometry_indexes = np.where((lightcurve_magnitude[:, 2] <
                                                max(0.1, mean_error_magnitude)))[0]
            lightcurve_bis = lightcurve_magnitude[good_photometry_indexes]

            lightcurve_bis = lightcurve_bis[lightcurve_bis[:, 0].argsort(), :]

            mag = lightcurve_bis[:, 1]
            flux = microltoolbox.magnitude_to_flux(mag)

            # clean the lightcurve using Savitzky-Golay filter on 3 points, degree 1.
            mag_clean = ss.savgol_filter(mag, 3, 1)
            time = lightcurve_bis[:, 0]
            flux_clean = microltoolbox.flux_to_magnitude(mag_clean)
            errmag = lightcurve_bis[:, 2]

            flux_source = min(flux_clean)
            good_points = np.where(flux_clean > flux_source)[0]

            while (np.std(time[good_points]) > 5) | (len(good_points) > 100):

                indexes = \
                    np.where((flux_clean[good_points] > np.median(flux_clean[good_points])) & (
                        errmag[good_points] <= max(0.1, 2.0 * np.mean(errmag[good_points]))))[0]

                if len(indexes) < 1:

                    break

                else:

                    good_points = good_points[indexes]

                    # gravity = (
                    #   np.median(time[good_points]), np.median(flux_clean[good_points]),
                    #    np.mean(errmag[good_points]))

                    # distances = np.sqrt((time[good_points] - gravity[0]) ** 2 / gravity[0] ** 2)

            to = np.median(time[good_points])
            max_flux = max(flux[good_points])
            to_estimations.append(to)
            maximum_flux_estimations.append(max_flux)
            errors_magnitude.append(np.mean(lightcurve_bis[good_points, 2]))

        except:

            time = lightcurve_magnitude[:, 0]
            flux = microltoolbox.magnitude_to_flux(lightcurve_magnitude[:, 1])
            to = np.median(time)
            max_flux = max(flux)
            to_estimations.append(to)
            maximum_flux_estimations.append(max_flux)

            errors_magnitude.append(mean_error_magnitude)

    to_guess = sum(np.array(to_estimations) / np.array(errors_magnitude) ** 2) / sum(
        1 / np.array(errors_magnitude) ** 2)
    survey = event.telescopes[0]
    lightcurve = survey.lightcurve_magnitude
    lightcurve = lightcurve[lightcurve[:, 0].argsort(), :]

    ## fs, uo, tE estimations only one the survey telescope


    time = lightcurve[:, 0]
    flux = microltoolbox.magnitude_to_flux(lightcurve[:, 1])
    errflux = microltoolbox.error_magnitude_to_error_flux(lightcurve[:, 2], flux)

    # fs estimation, no blend

    baseline_flux_0 = np.min(flux)
    baseline_flux = np.median(flux)

    while np.abs(baseline_flux_0 - baseline_flux) > 0.01 * baseline_flux:

        baseline_flux_0 = baseline_flux
        indexes = np.where((flux < baseline_flux))[0].tolist() + np.where(
            np.abs(flux - baseline_flux) < np.abs(errflux))[0].tolist()
        baseline_flux = np.median(flux[indexes])

        if len(indexes) < 100:
            print 'low'
            baseline_flux = np.median(flux[flux.argsort()[:100]])
            break

    fs_guess = baseline_flux

    # uo estimation
    max_flux = maximum_flux_estimations[0]
    Amax = max_flux / fs_guess
    if (Amax<1.0) | np.isnan(Amax):
        Amax=1.1
    uo_guess = np.sqrt(-2 + 2 * np.sqrt(1 - 1 / (1 - Amax ** 2)))


    # tE estimations
    tE_guesses = []

    # Method 1 : flux(t_demi_amplification) = 0.5 * fs_guess * (Amax + 1)

    half_magnification = 0.5 * (Amax + 1)

    flux_demi_amplification = fs_guess * half_magnification

    index_plus = np.where((time > to_guess) & (flux < flux_demi_amplification))[0]
    index_moins = np.where((time < to_guess) & (flux < flux_demi_amplification))[0]

    if len(index_plus) != 0:

        if len(index_moins) != 0:
            t_demi_amplification = (time[index_plus[0]] - time[index_moins[-1]])
            tE_demi_amplification = t_demi_amplification / (
                2 * np.sqrt(-2 + 2 * np.sqrt(1 + 1 / (half_magnification ** 2 - 1)) - uo_guess ** 2))

            tE_guesses.append(tE_demi_amplification)

        else:
            t_demi_amplification = time[index_plus[0]] - to_guess
            tE_demi_amplification = t_demi_amplification / np.sqrt(
                -2 + 2 * np.sqrt(1 + 1 / (half_magnification ** 2 - 1)) - uo_guess ** 2)

            tE_guesses.append(tE_demi_amplification)
    else:

        if len(index_moins) != 0:
            t_demi_amplification = to_guess - time[index_moins[-1]]
            tE_demi_amplification = t_demi_amplification / np.sqrt(
                -2 + 2 * np.sqrt(1 + 1 / (half_magnification ** 2 - 1)) - uo_guess ** 2)

            tE_guesses.append(tE_demi_amplification)

    # Method 2 : flux(t_E) = fs_guess * (uo^+3)/[(uo^2+1)^0.5*(uo^2+5)^0.5]

    amplification_tE = (uo_guess ** 2 + 3) / ((uo_guess ** 2 + 1) ** 0.5 * np.sqrt(uo_guess ** 2 + 5))
    flux_tE = fs_guess * amplification_tE

    index_tE_plus = np.where((flux < flux_tE) & (time > to))[0]
    index_tE_moins = np.where((flux < flux_tE) & (time < to))[0]

    if len(index_tE_moins) != 0:
        index_tE_moins = index_tE_moins[-1]
        tE_moins = to_guess - time[index_tE_moins]

        tE_guesses.append(tE_moins)

    if len(index_tE_plus) != 0:
        index_tE_plus = index_tE_plus[0]
        tE_plus = time[index_tE_plus] - to_guess

        tE_guesses.append(tE_plus)

    # Method 3 : the first points before/after to_guess that reach the baseline. Very rough
    # approximation ot tE.

    index_tE_baseline_plus = np.where((time > to) & (np.abs(flux - fs_guess) < np.abs(errflux)))[0]
    index_tE_baseline_moins = np.where((time < to) & (np.abs(flux - fs_guess) < np.abs(errflux)))[0]

    if len(index_tE_baseline_plus) != 0:
        tEPlus = time[index_tE_baseline_plus[0]] - to_guess

        tE_guesses.append(tEPlus)

    if len(index_tE_baseline_moins) != 0:
        tEMoins = to_guess - time[index_tE_baseline_moins[-1]]

        tE_guesses.append(tEMoins)

    tE_guess = np.median(tE_guesses)

    # safety reason, unlikely
    if (tE_guess < 0.1) | np.isnan(tE_guess):
        tE_guess = 20.0

    # [to,uo,tE], fsource

    return [to_guess, uo_guess, tE_guess], fs_guess