pgmuvi.lightcurve

class pgmuvi.lightcurve.Lightcurve(xdata, ydata, yerr=None, xtransform='minmax', ytransform=None, name=None, **kwargs)

Bases: object

A class for storing, manipulating and fitting light curves

This class is designed to be a convenient way to store and manipulate light curve data, and to fit Gaussian Processes to that data. It is designed to be used with the GPyTorch library, and to be compatible with the Pyro library for MCMC fitting.

Parameters:
  • xdata (Tensor of floats) – The independent variable data

  • ydata (Tensor of floats) – The dependent variable data

  • yerr (Tensor of floats, optional) – The uncertainties on the dependent variable data, by default None

  • xtransform (str, optional) – The transform to apply to the x data, by default ‘minmax’

  • ytransform (str, optional) – The transform to apply to the y data, by default None

Examples

Notes

append_data(new_values_x, new_values_y)
compute_psd(freq, means=None, scales=None, weights=None, raw=False, log=False, debug=False, **kwargs)

Compute the power spectral density for the model

Parameters:
  • freq (array_like) – The frequencies at which to compute the PSD

  • means (array_like, optional) – The means of the gaussians in the spectral mixture kernel, by default None. If None, the means from the model will be used.

  • scales (array_like, optional) – The scales of the gaussians in the spectral mixture kernel, by default None. If None, the scales from the model will be used.

  • weights (array_like, optional) – The weights of the gaussians in the spectral mixture kernel, by default None. If None, the weights from the model will be used.

  • raw (bool, optional) – If True, the PSD will be computed in the space that the model was trained in, by default False. If False, the PSD will be computed in the original space of the data.

  • **kwargs (dict, optional) – Any other keyword arguments to be passed.

Returns:

psd – The power spectral density of the model at the frequencies given by freq.

Return type:

array_like

cuda()
fit(model=None, likelihood=None, num_mixtures=4, guess=None, grid_size=2000, cuda=False, training_iter=300, max_cg_iterations=None, optim='AdamW', miniter=None, stop=1e-05, lr=0.1, stopavg=30, **kwargs)

Fit the lightcurve

Parameters:
  • model (string or instance of gpytorch.models.GP, optional) –

    The model to use for the GP, by default None. If None, an error will be raised. If a string, it must be one of the following:

    ’1D’: SpectralMixtureGPModel ‘2D’: TwoDSpectralMixtureGPModel ‘1DLinear’: SpectralMixtureLinearMeanGPModel ‘2DLinear’: TwoDSpectralMixtureLinearMeanGPModel ‘1DSKI’: SpectralMixtureKISSGPModel ‘2DSKI’: TwoDSpectralMixtureKISSGPModel ‘1DLinearSKI’: SpectralMixtureLinearMeanKISSGPModel ‘2DLinearSKI’: TwoDSpectralMixtureLinearMeanKISSGPModel

    If an instance of a GP class, that object will be used.

  • likelihood (string, None or instance of) –

    gpytorch.likelihoods.likelihood.Likelihood or Constraint,

    optional

    If likelihood is passed, it will be passed along to set_likelihood() and used to set the likelihood function for the model. For details, see the documentation for set_likelihood().

  • num_mixtures (int, optional) – The number of mixtures to use in the spectral mixture kernel, by default 4. If None, a default value will be used. This value is passed to the constructor for the model if a string is passed as the model argument.

  • guess (dict, optional) – A dictionary of the hyperparameters to use for the model and likelihood. The keys should be the names of the parameters, and the values should be Tensors containing the values of the parameters. If None, no hyperparameters will be set. If a hyperparameter is passed for a parameter that is not a model or likelihood parameter, it will be ignored.

  • grid_size (int, optional) – The number of points to use in the grid for the KISS-GP models, by default 2000.

  • cuda (bool, optional) – Whether to use CUDA, by default False.

  • training_iter (int, optional) – The number of iterations to use for training, by default 300.

  • max_cg_iterations (int, optional) – The maximum number of conjugate gradient iterations to use, by default None. If None, gpytorch.settings.max_cg_iterations will be used.

  • optim (str or torch.optim.Optimizer, optional) –

    The optimizer to use for training, by default “AdamW”. If a string, it must be one of the following:

    ’AdamW’: torch.optim.AdamW ‘Adam’: torch.optim.Adam ‘SGD’: torch.optim.SGD

    Otherwise, it must be an instance of torch.optim.Optimizer.

  • miniter (int, optional) – The minimum number of iterations to use for training, by default None. If None, training_iter will be used.

  • stop (float, optional) – The stopping criterion for the training, by default 1e-5.

  • lr (float, optional) – The learning rate to use for the optimizer, by default 0.1.

  • stopavg (int, optional) – The number of iterations to use for the stopping criterion, by default 30.

  • **kwargs (dict, optional) – Any other keyword arguments to be passed to the model constructor, likelihood constructor, or the optimizer.

Returns:

_description_

Return type:

_type_

Raises:

ValueError – _description_

get_parameters(raw=False, transform=True)

Returns a dictionary of the parameters of the model, with the keys being the names of the parameters and the values being the values of the parameters. This is useful for getting the values of the parameters after training, for example.

The routine is rather hacky, since there is no built-in way to get the unconstrained values of the parameters from the model without knowing exactly what they are ahead of time. This routine therefore gets the names of the raw parameters, and then uses those names with string manipulation and __getattr__ to get the values of the constrained parameters.

Parameters:

raw (bool, default False) – If True, returns the raw values of the parameters, otherwise returns the constrained values of the parameters.

Returns:

pars – A dictionary of the parameters of the model, with the keys being the names of the parameters and the values being the values of the parameters.

Return type:

dict

get_periods()

Returns a list of the periods, scales and weights of the model. This is useful for getting the periods after training, for example.

init_hypers_from_LombScargle(**kwargs)
property magnitudes
mcmc(sampler=None, num_samples=500, warmup_steps=100, num_chains=1, disable_progbar=False, max_cg_iterations=None, **kwargs)

Run an MCMC sampler on the model

This function runs an MCMC sampler on the model, using the sampler specified in the sampler attribute. The results are stored in the mcmc_results attribute.

Parameters:
  • sampler (str or MCMC, optional) –

    The name of the sampler to use. If None, pyro.infer.mcmc.NUTS will be used. If a string, it must be one of the following:

    ’NUTS’: pyro.infer.mcmc.NUTS ‘HMC’: pyro.infer.mcmc.HMC

    Otherwise, it must be an instance of pyro.infer.mcmc.MCMC.

  • num_samples (int, optional) – The number of samples to draw from the posterior, by default 500.

  • warmup_steps (int, optional) – The number of warmup steps to use, by default 100.

  • disable_progbar (bool, optional) – Whether to disable the progress bar, by default False.

  • **kwargs (dict, optional) –

Returns:

mcmc_results – A dictionary containing the results of the MCMC sampling. The keys are the names of the parameters, and the values are the samples of the parameters.

Return type:

dict

property ndim
plot(ylim=None, show=True, mcmc_samples=False, **kwargs)

Plot the model and data

Parameters:
  • ylim (list, optional) – The y-limits of the plot, by default None. If None, the y-limits will be set automatically.

  • show (bool, optional) – Whether to show the plot, by default True.

  • mcmc_samples (bool, optional) – Whether to plot the samples from the MCMC run, by default False. This will only work if the MCMC sampler has been run.

  • **kwargs (dict, optional) – Any other keyword arguments to be passed to the plotting routine.

Returns:

fig – The figure object of the plot.

Return type:

matplotlib.pyplot.Figure

plot_corner(kind='scatter', var_names=None, filter_vars='like', marginals=True, point_estimate='median', **kwargs)

Plot a corner plot of the results of the MCMC sampling

Parameters:
  • kind (str, optional) – The kind of plot to use, based on arviz.plot_pair. If None, the default behaviour of arviz.plot_pair will be used, by default ‘scatter’. Other options are ‘kde’ and ‘hexbin’.

  • var_names (list, optional) – A list of the names of the variables to include in the corner plot. If None, the variables for the mean function and the covariance function will be included, by default None.

  • filter_vars (str, optional) – A string specifying how to filter the variables, based on arviz.plot_pair. If None, the default behaviour of arviz.plot_pair will be used, by default ‘like’.

  • marginals (bool, optional) – Whether to include the marginal distributions, by default True.

  • point_estimate (str, optional) – The point estimate to plot, based on arviz.plot_pair. If None, no point estimate will be plotted, by default ‘median’.

plot_psd(freq=None, means=None, scales=None, weights=None, show=True, raw=False, log=(True, False), truncate_psd=True, logpsd=False, mcmc_samples=False, **kwargs)

Plot the power spectral density of the model

Parameters:
  • freq (array_like, optional) – The frequencies at which to compute the PSD, by default None. If None, the frequencies will be computed automatically.

  • means (array_like, optional) – The means of the gaussians in the spectral mixture kernel, by default None. If None, the means from the model will be used.

  • scales (array_like, optional) – The scales of the gaussians in the spectral mixture kernel, by default None. If None, the scales from the model will be used.

  • weights (array_like, optional) – The weights of the gaussians in the spectral mixture kernel, by default None. If None, the weights from the model will be used.

  • show (bool, optional) – Whether to show the plot, by default True.

  • raw (bool, optional) – If True, the PSD will be computed in the space that the model was trained in, by default False. If False, the PSD will be computed in the original space of the data.

  • log (tuple, optional) – A tuple of two booleans, indicating whether to plot the x-axis and y-axis on a log scale, respectively, by default (True, False).

  • truncate_psd (float or bool, optional) – If not False, the PSD will be truncated at this value, by default True. This is useful for speeding up plotting when the frequency range is large. If logpsd is True, this value should be given in (natural) log space. If truncate_psd is True, the PSD will be truncated at 1e-6 times the maximum PSD for logpsd=False, and 1e-15 of the maximum PSD (i.e. max(ln(psd)) - 34.5388) for logpsd=True.

  • logpsd (bool, optional) – If True, the PSD will be plotted on a log scale, by default False. If True, truncate_psd must be given in (natural) log space.

  • mcmc_samples (bool, optional) – If True, many sample PSDs will be plotted using the MCMC samples, by default False. This will only work if the model has been fitted using MCMC.

  • **kwargs (dict, optional) – Any other keyword arguments to be passed to the plotting routine.

Returns:

fig, ax – The figure and axes objects of the plot.

Return type:

matplotlib.pyplot.Figure, matplotlib.pyplot.Axes

plot_results()
plot_trace(var_names=None, filter_vars='like', figsize=None, **kwargs)

Plot a trace plot of the results of the MCMC sampling

Parameters:

var_names (list, optional) – A list of the names of the variables to include in the trace plot. If None, the variables for the mean function and the covariance function will be included, by default None.

print_parameters(raw=False)

Prints the parameters of the model, with the keys being the names of the parameters and the values being the values of the parameters. This is useful for getting the values of the parameters after training, for example.

Parameters:

raw (bool, default False) – If True, prints the raw values of the parameters, otherwise prints the constrained values of the parameters.

print_periods()
print_results()
set_constraint(constraint, debug=False, **kwargs)

Set the constraint for the model parameters

Parameters:
  • constraint (dict, optional) – A dictionary of the constraints to use for the model parameters. The keys should be the names of the parameters, and the values should be instances of gpytorch.constraints.Constraint. If None, no constraints will be used. If a constraint is passed for a parameter that is not a model parameter, it will be ignored.

  • **kwargs (dict, optional) – Any other keyword arguments to be passed to the Constraint constructors.

set_default_constraints(**kwargs)

Set the default constraints for the model and likelihood parameters

The default constraints are as follows:
  • All parameters are constrained to be positive, except the mean of

the GP, which is constrained to be in the range of the y-data (a correction will be needed if the data are censored!) - The noise is constrained to be less than the standard deviation of the y-data, and greater than either 1e-4 or 1/10 of the smallest uncertainty on the y-data, if uncertainties are given, or 1e-4 times the standard deviation of the y data. - The mixture means greater than the frequency corresponding to the separation between the earliest and latest points in the data and less than the frequency corresponding to the separation between the two closest data points (should be updated to account for the window function and whatever we’re really sensitive to) - The mixture scales and weights are left with their default constraints as defined in GPyTorch.

Parameters:

**kwargs (dict, optional) – Any keyword arguments to be passed to the Constraint constructors.

set_default_priors(**kwargs)

Set the default priors for the model and likelihood parameters

The default priors are as follows:
  • Parameters that must be positive are given LogNormal, HalfNormal

or HalfCauchy priors, depending on the parameter. - The noise is given a HalfNormal prior with a scale of 1/10 of the smallest uncertainty on the y-data, if uncertainties are given, or 1/10 of the standard deviation of the y data. - The mean of the GP is given a Gaussian prior with a mean of the mean of the y-data and a standard deviation of 1/10 of the standard deviation of the y-data.

Parameters:

**kwargs (dict, optional) – Any keyword arguments to be passed to the Prior constructors.

set_hypers(hypers=None, debug=False, **kwargs)

Set the hyperparameters for the model and likelihood. This is a convenience function that calls the model.initialize() to set the hyperparameters. However, first it applies any transforms to the hyperparameters, so that the user can pass the hyperparameters in the original data space if they wish.

Parameters:
  • hypers (dict, optional) – A dictionary of the hyperparameters to use for the model and likelihood. The keys should be the names of the parameters, and the values should be Tensors containing the values of the parameters. If None, no hyperparameters will be set. If a hyperparameter is passed for a parameter that is not a model or likelihood parameter, it will be ignored.

  • **kwargs (dict, optional) – Any other keyword arguments to be passed to the initialize.

set_likelihood(likelihood=None, **kwargs)

Set the likelihood function for the model

Parameters:

likelihood (string, None or instance of) –

gpytorch.likelihoods.likelihood.Likelihood or Constraint,

optional

The likelihood function to use for the GP, by default None. If None, a Gaussian likelihood will be used. If a string, it must be ‘learn’, in which case a Gaussian likelihood with learnable noise will be used. If an instance of a Likelihood object, that object will be used. If a Constraint object, a GaussianLikelihood will be used, with the constraint being passed to the likelihood as the noise_constraint argument. If a Constraint object is passed, the noise_constraint argument will override any other arguments passed to the likelihood function. You can also provide a class as input, in which case the class will be instantiated with the kwargs provided under the assumption that it is a Likelihood object, and it will also be passed the uncertainties on the y data, if available.

set_model(model=None, likelihood=None, num_mixtures=None, **kwargs)

Set the model for the lightcurve

Parameters:
  • model (string or instance of gpytorch.models.GP, optional) –

    The model to use for the GP, by default None. If None, an error will be raised. If a string, it must be one of the following:

    ’1D’: SpectralMixtureGPModel ‘2D’: TwoDSpectralMixtureGPModel ‘1DLinear’: SpectralMixtureLinearMeanGPModel ‘2DLinear’: TwoDSpectralMixtureLinearMeanGPModel ‘1DSKI’: SpectralMixtureKISSGPModel ‘2DSKI’: TwoDSpectralMixtureKISSGPModel ‘1DLinearSKI’: SpectralMixtureLinearMeanKISSGPModel ‘2DLinearSKI’: TwoDSpectralMixtureLinearMeanKISSGPModel

    If an instance of a GP class, that object will be used. _description_, by default None

  • likelihood (string, None or instance of) –

    gpytorch.likelihoods.likelihood.Likelihood or Constraint,

    optional

    If likelihood is passed, it will be passed along to set_likelihood() and used to set the likelihood function for the model. For details, see the documentation for set_likelihood().

  • num_mixtures (int, optional) – The number of mixtures to use in the spectral mixture kernel, by default None. If None, a default value will be used. This value is passed to the constructor for the model if a string is passed as the model argument.

  • **kwargs (dict, optional) – Any other keyword arguments to be passed to the model constructor.

set_prior(prior=None, **kwargs)

Set the prior for the model parameters

Parameters:
  • prior (dict, optional) – A dictionary of the priors to use for the model parameters. The keys should be the names of the parameters, and the values should be instances of gpytorch.priors.Prior. If None, no priors will be used. If a prior is passed for a parameter that is not a model parameter, it will be ignored.

  • **kwargs (dict, optional) – Any other keyword arguments to be passed to the Prior constructors.

summary(prob=0.683, use_arviz=True, var_names=None, filter_vars='like', stat_focus='median', **kwargs)

Print a summary of the results of the MCMC sampling

Parameters:
  • prob (float, optional) – The probability to use for the credible intervals, by default 0.683.

  • use_arviz (bool, optional) – Whether to use arviz to print the summary, by default True.

  • var_names (list, optional) – A list of the names of the variables to include in the summary. If None, the variables for the mean function and the covariance function will be included, by default None.

  • filter_vars (str, optional) – A string specifying how to filter the variables, based on arviz.summary. If None, the default behaviour of arviz.summary will be used, by default ‘like’.

  • stat_focus (str, optional) – A string specifying which statistic to focus on, based on arviz.summary. If None, the default behaviour of arviz.summary will be used (‘mean’), by default ‘median’.

transform_x(values)
transform_y(values)
property xdata

The independent variable data

Getter:

Returns the independent variable data in its raw

(untransformed) state :setter: Takes the input data and transforms it as requested by the user :type: torch.Tensor

property ydata

The dependent variable data

Getter:

Returns the dependent variable data in its raw

(untransformed) state :setter: Takes the input data and transforms it as requested by the user :type: torch.Tensor

property yerr

The uncertainties on the dependent variable data

Getter:

Returns the uncertainties on the dependent variable data in

its raw (untransformed) state :setter: Takes the input data and transforms it as requested by the user :type: torch.Tensor

class pgmuvi.lightcurve.MinMax

Bases: Transformer

inverse(data, shift=True, **kwargs)

Invert a MinMax transformation based on saved state

Invert the transformation of the data from the [0,1] interval. It used the stored min and range of the data for the inverse transformation.

Parameters:

data (Tensor of floats) – The data to be reverse-transformed

transform(data, dim=0, apply_to=None, recalc=False, shift=True, **kwargs)

Perform a MinMax transformation

Transform the data such that each dimension is rescaled to the [0,1] interval. It stores the min and range of the data for the inverse transformation.

Parameters:
  • data (Tensor of floats) – The data to be transformed

  • apply_to (tensor of ints or slice objects, optional) – Which dimensions to apply the transform to. If None, apply to all

  • recalc (bool, default False) – Should the min and range of the transform be recalculated, or reused from previously?

  • shift (bool, default True) – Should the data be shifted such that the minimum value is 0? This is mainly included so that data or parameters can be transformed when they apply to a single period - in this case, only the range needs to be applied.

class pgmuvi.lightcurve.RobustZScore

Bases: Transformer

inverse(data, shift=True, **kwargs)

Invert a robust z-score transformation based on saved state

Invert the robust z-scoring of the data based on the saved median and median absolute deviation.

Parameters:

data (Tensor of floats) – The data to be reverse-transformed

transform(data, dim=0, apply_to=None, recalc=False, shift=True, **kwargs)

Perform a robust z-score transformation

Transform the data such that each dimension is rescaled such that its median is 0 and its median absolute deviation is 1.

Parameters:
  • data (Tensor of floats) – The data to be transformed

  • apply_to (int or tensor of ints, optional) – Which dimensions to apply the transform to. If None, apply to all

  • recalc (bool, default False) – Should the parameters of the transform be recalculated, or reused from previously?

  • shift (bool, default True) – Should the data be shifted such that the median value is 0? This is mainly included so that data or parameters can be transformed when they apply to a single period - in this case, only the median absolute deviation needs to be applied.

class pgmuvi.lightcurve.Transformer

Bases: object

inverse(data, shift=True, **kwargs)

Invert a transform based on saved parameters

This is a baseclass with no implementation, your subclass should implement the inverse transform itself

transform(data, **kwargs)

Transform some data and return it, storing the parameters required to repeat or reverse the transform

This is a baseclass with no implementations, your subclass should implement the transform itself

class pgmuvi.lightcurve.ZScore

Bases: Transformer

inverse(data, shift=True, **kwargs)

Invert a z-score transformation based on saved state

Invert the z-scoring of the data based on the saved mean and standard deviation

Parameters:

data (Tensor of floats) – The data to be reverse-transformed

transform(data, dim=0, apply_to=None, recalc=False, shift=True, **kwargs)

Perform a z-score transformation

Transform the data such that each dimension is rescaled such that its mean is 0 and its standard deviation is 1.

Parameters:
  • data (Tensor of floats) – The data to be transformed

  • apply_to (int or tensor of ints, optional) – Which dimensions to apply the transform to. If None, apply to all

  • recalc (bool, default False) – Should the parameters of the transform be recalculated, or reused from previously?

  • shift (bool, default True) – Should the data be shifted such that the mean value is 0? This is mainly included so that data or parameters can be transformed when they apply to a single period - in this case, only the standard deviation needs to be applied.

pgmuvi.lightcurve.minmax(data, dim=0)