Wrappers for SBI

X-PSI wrapper functionalities to be used by the sbi package for Simulation-Based Inference (SBI).

class xpsi.WrapperSBI.CustomLikelihoodSBI(*args, **kwargs)[source]

Bases: Likelihood

Likelihood that returns synthesised signals for use with SBI.

Overrides synthesise() so that, instead of writing synthetic data to disk, it returns the synthesised counts (model_flux) that constitute the SBI training data. The base-class _driver() is reused unchanged; the synthesised signal is collected from each signal after driving.

synthesise(p, reinitialise=False, force=False, **kwargs)[source]

Synthesise pulsation data.

Parameters:
  • p (list) – Parameter vector.

  • reinitialise (optional[bool]) – Call self.reinitialise()?

  • force (optional[bool]) – Force complete reevaluation even if some parameters are unchanged.

  • kwargs (dict) – Keyword arguments propagated to custom signal synthesis methods. Examples of such arguments include exposure times or required total count numbers (see example notebooks).

Returns ndarray model_flux:

The synthesised counts, or None if the prior check failed.

class xpsi.WrapperSBI.SynthesiseData(channels, phases, first, last)[source]

Custom data container to enable synthesis.

Parameters:
  • channels (ndarray) – Instrument channel numbers which must be equal in number to the first dimension of the count matrix.

  • phases (ndarray) – Phases of the phase bins which must be equal in number to the second dimension of the count matrix.

  • first (int) – First channel index number to include in the synthesised data.

  • last (int) – Last channel index number to include in the synthesised data.

xpsi.WrapperSBI.synthesise_for_sbi(self, exposure_time=None, expected_source_counts=None, nchannels=None, nphases=None, seed=0, bkg=None, **kwargs)[source]

Synthesise a data set.

Bound onto a Signal subclass (see example notebooks), this replaces the file-writing synthesise() with an in-memory version that returns the synthesised counts. The result is also stored on the signal as self.synthesised so the likelihood layer can retrieve it.

Parameters:
  • exposure_time (float) – Exposure time in seconds to scale the expected count rate.

  • expected_source_counts (float) – Total expected number of source counts.

  • nchannels (int) – Number of channels in the synthesised data.

  • nphases (int) – Number of phase bins in the synthesised data.

  • seed (optional[int]) – Seed for the Poisson noise random number generation.

  • bkg (optional[numpy.ndarray]) – Background for the synthesized pulse profile. Default set to zero if none is provided.

Returns ndarray synthetic:

The synthesised data set.

class xpsi.WrapperSBI.SBIWrapper(prior, likelihood, instr_kwargs=None, train_using_CNNs=True, device='cpu')[source]

Wrap the X-PSI likelihood and prior into an SBI-compatible interface.

Parameters:
  • prior (xpsi.Prior) – xpsi.Prior instance.

  • likelihood (xpsi.Likelihood) – xpsi.Likelihood instance.

  • instr_kwargs (optional[dict]) – Instrument keyword arguments for the likelihood synthesise method.

  • train_using_CNNs (optional[bool]) – Whether to use CNNs for training. Defaults to True.

  • device (optional[str]) – Torch device on which to place the tensors. Defaults to "cuda" when available, otherwise "cpu".

log_prob(parameter_vector)[source]

Compute the log probability of the parameter vector.

Parameters:

parameter_vector (torch.Tensor) – The parameter vector.

Returns torch.Tensor:

The log probability of each parameter vector.

sample(sample_shape=())[source]

Sample from the prior distribution.

Parameters:

sample_shape (torch.Size) – The shape of the sample. Defaults to torch.Size([]).

Returns torch.Tensor:

The sampled values, on self.device.

simulator(parameter_vector)[source]

Simulate the pulse profile for the parameter vector.

Parameters:

parameter_vector (torch.Tensor) – The parameter vector for which to simulate the pulse profile.

Returns torch.Tensor model_flux:

The pulse profile for the input parameter vector.