target.core#

The basic Target and Transient object are defined in this module. Pre-defined transients (like SNIa) inherit from Transient and new object you want to define shall.

class skysurvey.target.core.Target[source]#

Bases: object

Base class for targets.

This class provides a framework for representing astronomical targets, including their models, templates, and cosmological parameters.

Parameters:
  • _KIND (str, optional) – The kind of target, by default “unknow”

  • _TEMPLATE (object, optional) – The template for the target, by default None

  • _MODEL (dict, optional) – The model for the target, by default None

  • _MAGSYS (str) – The photometric magnitude system. Defaults to “ab”.

  • _PEAK_ABSMAG_BAND (str) – The bandpass in which the peak absolute magnitude is defined. Defaults to “bessellb”.

  • _AMPLITUDE_NAME (str) – The name of the parameter used to scale the model flux. Defaults to “amplitude”.

  • _COSMOLOGY (astropy.cosmology, optional) – The cosmology to use, by default cosmology.Planck18

See also

from_setting: loads an instance given model parameters (dict)

classmethod from_setting(setting, **kwargs)[source]#

Load the target from a setting dictionary.

Note

Not implemented yet.

Parameters:
  • setting (dict) – Dictionary containing the model parameters.

  • **kwargs – Additional keyword arguments.

Returns:

The loaded target.

Return type:

Target

classmethod from_data(data, template=None, model=None, **kwargs)[source]#

Load the instance given existing data.

This means that the model will be ignored as data will not be generated but input.

Parameters:
  • data (pandas.DataFrame) – DataFrame containing (at least) the template parameters.

  • template (str, sncosmo.Source, sncosmo.Model or skysurvey.Template, optional) – The template source. If a string is given, it is assumed to be a sncosmo model name. By default None.

  • model (dict, optional) – Defines how template parameters are drawn and how they are connected. The model will update the default cls._MODEL if any. If None, cls._MODEL is used as default. By default None.

Returns:

The loaded target.

Return type:

Target

See also

from_draw: loads the instance from a random draw of targets given the model

classmethod from_draw(size=None, model=None, template=None, zmax=None, tstart=None, tstop=None, zmin=0, nyears=None, skyarea=None, rate=None, effect=None, cosmology=None, verbose=False, set_amplitude=False, **kwargs)[source]#

Load the instance from a random draw of targets given the model.

kwargs may hold specific transient options like: - magabs for TSTransient(){self._additional_input} (see class.__init__).

Parameters:
  • size (int, optional) – Number of target you want to sample. If None, 1 is assumed. Ignored if nyears is given. By default None.

  • model (dict, optional) – Defines how template parameters are drawn and how they are connected. The model will update the default cls._MODEL if any. If None, cls._MODEL is used as default. By default None.

  • template (str, optional) – Name of the template (sncosmo.Model(source)). If None, cls._TEMPLATE is used as default. By default None.

  • zmax (float, optional) – Maximum redshift to be simulated. By default None.

  • tstart (float, str, optional) – Starting time of the simulation. If a string is given, it is converted to mjd. By default None.

  • tstop (float, str, optional) – Ending time of the simulation. If a string is given, it is converted to mjd. If tstart and nyears are both given, tstop will be overwritten by tstart + 365.25 * nyears. By default None.

  • zmin (float, optional) – Minimum redshift to be simulated. By default 0.

  • nyears (float, optional) –

    If given, nyears will set:

    • size: it will be the number of target expected up to zmax in the given number of years. This uses get_rate(zmax).

    • tstop: tstart + 365.25 * nyears

    By default None.

  • skyarea (None, str, geometry, optional) –

    Sky area to be considered.

    • str: ‘full’ (equivalent to None), [‘extra-galactic’, not implemented yet]

    • geometry: shapely.Geometry

    • None: full sky

    By default None.

  • rate (float, callable, optional) – The transient rate. If a float is given, it is assumed to be the number of targets per Gpc3. If a callable is given, it is supposed to be a function of z that returns the volumetric rate as a function of wavelength.

  • effect ([type], optional) – [description]. By default None.

  • cosmology (None, astropy.Cosmology, optional) – specify the cosmology to be used.

  • set_amplitude (bool) – should the amplitude of the template be set at this stage ?

  • **kwargs – Goes to self.update_model_parameter().

Returns:

The loaded target with data, model and template loaded.

Return type:

Target

See also

from_setting: loads an instance given model parameters (dict)

set_cosmology(cosmology)[source]#

Set the cosmology to be used across the target.

Parameters:

cosmology (astropy.Cosmology) – the cosmology to be used.

set_template(template, rate_update=False)[source]#

Set the template.

Note

It is unlikely you want to set this directly.

Parameters:
  • template (str, sncosmo.Source, sncosmo.Model or skysurvey.Template) – This will reset self.template to the new template source.

  • rate_update (bool, optional) – [description], by default False

See also

from_draw: load the instance by a random draw generation. from_setting: loads an instance given model parameters

get_template(index=None, as_model=False, data=None, set_magabs=False, **kwargs)[source]#

Get a template (sncosmo.Model).

Parameters:
  • index (int, optional) – Index of a target (see self.data.index) to set the template parameters to that of the target. If None, the default sncosmo.Model parameters will be used. By default None.

  • as_model (bool, optional) – should this return the sncosmo.Model (True) or the skysurvey.Template (for info sncosmo.Model => skysurvey.Template.sncosmo_model)

  • data (pandas.DataFrame, None, optional) – which data should be used to set the parameter of the template. Ignored if index is None.

  • set_magabs (bool, optional) – should the peal magnitude of the template be set to magabs ?

  • **kwargs – Goes to self.template.get() and passed to sncosmo.Model.

Returns:

An instance of the template (or its associated sncosmo.Model). (see as_model)

Return type:

skysurvey.Template or sncosmo.Model

See also

get_target_template: get a template set to the target parameters. get_template_parameters: get the template parameters for the given target

get_target_template(index, as_model=False, **kwargs)[source]#

Get a template set to the target parameters.

This is a shortcut to get_template(index=index, **kwargs).

Parameters:
  • index (int) – Index of a target (see self.data.index) to set the template parameters to that of the target.

  • as_model (bool, optional) – should this return the sncosmo.Model (True) or the skysurvey.Template (for info sncosmo.Model => skysurvey.Template.sncosmo_model)

  • **kwargs – Goes to self.template.get() and passed to sncosmo.Model.

Returns:

An instance of the template (or its associated sncosmo.Model). (see as_model)

Return type:

skysurvey.Template or sncosmo.Model

See also

get_template: get a template instance (sncosmo.Model) get_template_parameters: get the template parameters for the given target

get_target_flux(index, band, phase, zp=None, zpsys=None, restframe=True)[source]#

Flux through the given bandpass(es) at the given time(s).

Default return value is flux in photons / s / cm^2. If zp and zpsys are given, flux(es) are scaled to the requested zeropoints.

Parameters:
  • index (int) – Index of a target (see self.data.index) to set the template parameters to that of the target.

  • band (str or list_like) – Name(s) of Bandpass(es) in registry.

  • phase (float or list_like) – Phase in day.

  • zp (float or list_like, optional) – If given, zeropoint to scale flux to (must also supply zpsys). If not given, flux is not scaled. By default None.

  • zpsys (str or list_like, optional) – Name of a magnitude system in the registry, specifying the system that zp is in. By default None.

  • restframe (bool, optional) – Is phase given in restframe? By default True.

Returns:

Flux in photons / s /cm^2, unless zp and zpsys are given, in which case flux is scaled so that it corresponds to the requested zeropoint. Return value is float if all input parameters are scalars, numpy.ndarray otherwise.

Return type:

float or numpy.ndarray

get_target_peakmag(index, band, magsys='ab')[source]#

Peak magnitude through the given bandpass(es) at the given time(s).

Parameters:
  • index (int) – Index of a target (see self.data.index) to set the template parameters to that of the target.

  • band (str or list_like) – Name(s) of Bandpass(es) in registry.

  • magsys (str or list_like, optional) – Name(s) of sncosmo.MagSystem in registry. By default “ab”.

Returns:

Magnitude at peak for the given band.

Return type:

float

get_target_mag(index, band, phase, magsys='ab', restframe=True)[source]#

Magnitude through the given bandpass(es) at the given time(s).

Parameters:
  • index (int) – Index of a target (see self.data.index) to set the template parameters to that of the target.

  • band (str or list_like) – Name(s) of Bandpass(es) in registry.

  • phase (float or list_like) – Phase in day.

  • magsys (str or list_like, optional) – Name(s) of sncosmo.MagSystem in registry. By default “ab”.

  • restframe (bool, optional) – Is phase given in restframe? By default True.

Returns:

Magnitude for each item in time, band, magsys. The return value is a float if all parameters are not interables. The return value is an numpy.ndarray if any are interable.

Return type:

float or numpy.ndarray

clone_target_change_entry(index, name, values, as_dataframe=False)[source]#

Get a clone of the given target at the given redshifts.

This:

  1. copies the index entries,

  2. sets the name to the input values

  3. redraw the model starting from name (creating a new dataframe)

  4. (optional) sets a new instance with the updated dataframe

Parameters:
  • index (int) – Index of a target (see self.data.index).

  • name (str) – Name of the entry to change.

  • values (list, array) – New values for this entry.

  • as_dataframe (bool, optional) – Should this return the created new dataframe (True) or a new instance (False). By default False.

Returns:

The cloned target or the new dataframe.

Return type:

Target or DataFrame

get_template_parameters(index=None, data=None)[source]#

Get the template parameters for the given target.

This method selects from self.data the parameters that actually are parameters of the template (and disregards the rest).

Parameters:

index (int, optional) – Index of a target (see self.data.index) to get the template parameters from that target only. By default None.

Returns:

The template parameters.

Return type:

pandas.DataFrame or pandas.Series

See also

template_parameter: parameters of the template (sncosmo.Model) | argument get_template: get a template instance (sncosmo.Model)

get_template_columns(data=None)[source]#

Get the data columns that are template parameters.

Returns:

The template columns.

Return type:

pandas.Index

apply_gaussian_noise(errmodel, data=None)[source]#

Apply gaussian noise to current entries.

Parameters:
  • errmodel (dict) – Dict that will feed a ModelDAG. The format is {x: {func:, kwargs:{}}}. This will draw x_err following the this formula and will update x assuming x_true for the original x and x_err for the given x drawn here. You can refeer to the original x using ‘@x_true’ in the func kwargs.

  • data (None, optional) – Original dataframe to be noisified. If None self.data is used. By default None.

Returns:

  • self if data is None

  • DataFrame otherwise.

Return type:

Target or DataFrame

Examples

>>> import skysurvey
>>> from scipy import stats
>>> errmodel = {"x1": {"func": stats.lognorm.rvs, "kwargs":{"s":0.6, "loc":0.001, "scale":0.15}},
...             "c": {"func": stats.lognorm.rvs, "kwargs":{"s":0.7, "loc":0.03, "scale":0.01}},
...             "magobs": {"func": stats.lognorm.rvs, "kwargs":{"s":0.9, "loc":0.03, "scale":0.01}},
...             }
>>> snia = skysurvey.SNeIa.from_draw(1000)
>>> snia = snia.apply_gaussian_noise(errmodel, data=snia.data)
magabs_to_magobs(z, magabs, cosmology=None)[source]#

Convert absolute magnitude into observed magnitude.

This is done given the (cosmological) redshift and a cosmology.

Parameters:
  • z (float, array-like) – Cosmological redshift.

  • magabs (float, array-like) – Absolute magnitude.

  • cosmology (astropy.Cosmology, None) – specify the cosmology to use to convert observed- to absolute-magnitude. If None, self.cosmology is used. Careful with specifying the cosmology, in a self consistant why.

Returns:

Array of observed magnitude (distmod(z) + magabs).

Return type:

array-like

set_model(model, rate_update=True)[source]#

Set the target model.

The model defines what template parameters to draw and how they are connected.

Note

It is unlikely you need to use that directly.

Parameters:
  • model (dict or ModelDAG) – Model that will be used to draw the Target parameter.

  • rate_update (bool, optional) – Should this check for rate options and feedin rate=self.rate? By default True.

Return type:

None

See also

from_setting: loads an instance given model parameters (dict) from_draw: loads and draw random data.

set_data(data, incl_template=True)[source]#

Attach data to this instance.

Parameters:
  • data (pandas.DataFrame) – DataFrame containing (at least) the template parameters.

  • incl_template (bool, optional) – If data does not contain the template column should this add it? By default True.

Return type:

None

get_model(**kwargs)[source]#

Get a copy of the model (dict).

You can change the model you get (not the current model) using the kwargs.

Parameters:

**kwargs – Can change the model entry parameters for istance, t0: {“low”:0, “high”:10} will update model[“t0”][“param”] = …

Returns:

A copy of the model (with param potentially updated).

Return type:

dict

See also

update_model: change the current model (not just the one you get) get_model_parameter: access the model parameters.

get_model_parameter(entry, key, default=None, model=None)[source]#

Access a parameter of the model.

Parameters:
  • entry (str) – Name of the variable as given by the model dict.

  • key (str) – Name of the parameters.

  • default (any, optional) – Value returned if the parameter is not found. By default None.

  • model (modelDAG, optional) – Get the parameter of this model instead of self.model. Use with caution. By default None.

Returns:

Value of the entry parameter.

Return type:

any

Examples

>>> self.get_model_parameter('redshift', 'zmax', None)
update_model_parameter(rate_update=True, **kwargs)[source]#

Change the kwargs entry of a model.

update_model(rate_update=True, **kwargs)[source]#

Change the given entries of the model.

Parameters:
  • rate_update (bool, optional) – [description], by default True

  • **kwargs – Will update any model entry (or create a new one at the end).

Examples

Changing the b entry function and make it depends on a >>> rng = np.random.default_rng() >>> self.update_model(b={“func”:rng.normal, “kwargs”:{“loc”:”@a”, “scale”:1}})

add_effect(effect, model=None, data=None, overwrite=False, **kwargs)[source]#

Add an effect to the target affecting how spectra or lightcurve are generated.

This changes the template, using self.template.add_effect(), and changes the target’s model if effect.model is set.

Parameters:
  • effect (dict, skysurvey.effect.Effect) –

    Effect that should be used to change the target. e.g. mw_ebv = skysurvey.effect.Effect.from_name('mw') These format are accepted:

    • dict: {effect: sncosmo.Effect, "name": str, "frame": str, (model: optionel)}

    • skysurvey.effect.Effect

  • model (dict, optional) – Defines how the data will be drawn. This updates self.model. By default None.

  • data (pandas.DataFrame, optional) – Value that will be added to the data to capture the effect (if any). If data and model are given, model is not used. By default None.

  • overwrite (bool, optional) – [description], by default False

  • **kwargs – goes to self.data.merge(data, **kwargs) if data is given. Ignored otherwise.

Return type:

None

show_scatter(xkey, ykey, ckey=None, ax=None, fig=None, index=None, data=None, colorbar=True, bins=None, bcolor='0.6', err_suffix='_err', **kwargs)[source]#

Show a scatter plot of the data.

Parameters:
  • xkey (str) – The key for the x-axis data.

  • ykey (str) – The key for the y-axis data.

  • ckey (str, optional) – The key for the color-axis data. By default None.

  • ax (matplotlib.axes.Axes, optional) – The axes on which to plot. By default None.

  • fig (matplotlib.figure.Figure, optional) – The figure on which to plot. By default None.

  • index (int, optional) – The index of the data to plot. By default None.

  • data (pandas.DataFrame, optional) – The data to plot. By default None.

  • colorbar (bool, optional) – Whether to show a colorbar. By default True.

  • bins (int, optional) – The number of bins to use for the histogram. By default None.

  • bcolor (str, optional) – The color of the bins. By default “0.6”.

  • err_suffix (str, optional) – The suffix for the error columns. By default “_err”.

  • **kwargs – Additional keyword arguments to pass to ax.scatter.

Returns:

The figure containing the plot.

Return type:

matplotlib.figure.Figure

draw(size=None, zmax=None, zmin=0, tstart=None, tstop=None, nyears=None, skyarea=None, inplace=False, model=None, verbose=False, set_amplitude=False, **kwargs)[source]#

Draw the parameter model (using self.model.draw()).

Parameters:
  • size (int, optional) – Number of target you want to draw. Ignored is nyears is not None. By default None.

  • zmax (float, optional) – Maximum redshift to be simulated. By default None.

  • zmin (int, optional) – Minimum redshift to be simulated. By default 0.

  • tstart (float, optional) – Starting time of the simulation. By default None.

  • tstop (float, optional) – Ending time of the simulation. If tstart and nyears are both given, tstop will be overwritten by tstart + 365.25 * nyears. By default None.

  • nyears (float, optional) –

    If given, nyears will set:

    • size: it will be the number of target expected up to zmax in the given number of years. This uses get_rate(zmax).

    • tstop: tstart + 365.25 * nyears

    By default None.

  • skyarea (None, str, geometry, optional) –

    Sky area to be considered.

    • str: ‘full’ (equivalent to None), ‘extra-galactic’

    • geometry: shapely.Geometry

    • None: full sky

    By default None.

  • inplace (bool, optional) – Sets self.data to the newly drawn dataframe. By default False.

  • model ([type], optional) – [description]. By default None.

  • set_amplitude (bool) – should the template amplitude be computed.

Returns:

The simulated dataframe.

Return type:

DataFrame

amplitude_name = 'amplitude'#
peak_absmag_band = 'bessellb'#
magsys = 'ab'#
kind = 'unknow'#
property cosmology#

The cosmology to use.

property model#

The model of the target.

property data#

The data of the target.

property template#

The template of the target.

property template_source#

The source of the template.

property template_parameters#

The parameters of the template.

property template_effect_parameters#

The effect parameters of the template.

class skysurvey.target.core.Transient[source]#

Bases: Target

A transient target.

This class inherits from Target and adds a rate parameter.

Parameters:

_RATE (float, optional) – The rate of the transient, by default None

set_rate(float_or_func)[source]#

Set the transient rate.

Parameters:

float_or_func (float or callable) – If a float is given, it is assumed to be the number of targets per Gpc3. If a callable is given, it is supposed to be a function of z that returns the volumetric rate as a function of wavelength.

draw_redshift(zmax, zmin=0, zstep=0.0001, size=None, rate=None, **kwargs)[source]#

Draw redshift based on the rate (see get_rate()).

Parameters:
  • zmax (float) – Maximum redshift.

  • zmin (float, optional) – Minimum redshift. By default 0.

  • zstep (float, optional) – Redshift step. By default 1e-4.

  • size (int, optional) – Number of redshifts to draw. By default None.

  • rate (float, callable, optional) – The transient rate. If None, self.rate is used. By default None. If a float is given, it is assumed to be the number of targets per Gpc3. If a callable is given, it is supposed to be a function of z that returns the volumetric rate as a function of wavelength.

  • **kwargs – Additional keyword arguments to pass to draw_redshift.

Returns:

The drawn redshifts.

Return type:

array

get_rate(z, rate=None, **kwargs)[source]#

Get the number of target (per year) up to the given redshift.

Parameters:
  • z (float) – Redshift.

  • rate (float, callable, optional) –

    If None, self.rate is used.

    If a float is given, it is assumed to be the number of targets per Gpc3. If a callable is given, it is supposed to be a function of z that returns the volumetric rate as a function of wavelength.

  • **kwargs – Goes to the rate function (if a function, not a number).

Returns:

The number of targets.

Return type:

int

See also

draw_redshift: draws redshifts from rate distribution.

get_lightcurve(band, times, sncosmo_model=None, index=None, in_mag=False, zp=25, zpsys='ab', **kwargs)[source]#

Get the transient lightcurve.

Parameters:
  • band (str, list) – Name of the band (should be known by sncosmo) or list of.

  • times (float, list) – Time of the observations.

  • sncosmo_model (sncosmo.Model, optional) – The sncosmo model to use. By default None.

  • index (int, optional) – The index of the target. By default None.

  • in_mag (bool, optional) – If True, the lightcurve is returned in magnitude. By default False.

  • zp (float, optional) – The zeropoint to use. By default 25.

  • zpsys (str, optional) – The zeropoint system to use. By default “ab”.

  • **kwargs – Additional keyword arguments to pass to self.template.get_lightcurve.

Returns:

1 lightcurve per band.

Return type:

ndarray

get_spectrum(time, lbdas, as_phase=True, sncosmo_model=None, index=None, **kwargs)[source]#

Get the transient spectrum at the given phase (time).

Parameters:
  • time (float or list_like) – Time(s) in days. If None (default), the times corresponding to the native phases of the model are used.

  • lbdas (float or list_like) – Wavelength(s) in Angstroms. If None (default), the native wavelengths of the model are used.

  • as_phase (bool, optional) – Is the given time a phase? (as_phase=True) or a actual time (False). By default True.

  • sncosmo_model ([type], optional) – [description]. By default None.

  • index ([type], optional) – [description]. By default None.

Returns:

flux – Spectral flux density values in ergs / s / cm^2 / Angstrom.

Return type:

float or numpy.ndarray

See also

get_lightcurve: get the transient lightcurve

show_lightcurve(band, index, params=None, ax=None, fig=None, colors=None, phase_range=None, npoints=500, zp=25, zpsys='ab', format_time=True, t0_format='mjd', in_mag=False, invert_mag=True, **kwargs)[source]#

Show the lightcurve.

Parameters:
  • band (str) – The band to show.

  • index (int) – The index of the target.

  • params (dict, optional) – Parameters to pass to get_target_template. By default None.

  • ax (matplotlib.axes.Axes, optional) – The axes to show the lightcurve on. By default None.

  • fig (matplotlib.figure.Figure, optional) – The figure to show the lightcurve on. By default None.

  • colors (list, optional) – The colors to use for the lightcurve. By default None.

  • phase_range (list, optional) – The phase range to show. By default None.

  • npoints (int, optional) – The number of points to show. By default 500.

  • zp (float, optional) – The zero point to use. By default 25.

  • zpsys (str, optional) – The zero point system to use. By default “ab”.

  • format_time (bool, optional) – Whether to format the time. By default True.

  • t0_format (str, optional) – The format of the time. By default “mjd”.

  • in_mag (bool, optional) – Whether to show the magnitude. By default False.

  • invert_mag (bool, optional) – Whether to invert the magnitude. By default True.

  • **kwargs – Additional keyword arguments to pass to template.show_lightcurve.

Returns:

The figure containing the plot.

Return type:

matplotlib.figure.Figure

property rate#

Rate of the transient.

If float, it is assumed to be the volumetric rate in Gpc-3 / yr-1. If a callable is given, it is supposed to be a function of z that returns the volumetric rate as a function of wavelength.