Wrappers for SBI

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

class xpsi.SBI_wrapper.Custom_SBI_Likelihood(star, signals, num_energies=128, fast_rel_num_energies=0.25, threads=1, llzero=-1e+90, externally_updated=False, prior=None, max_energy=None)[source]

Bases: Likelihood

Custom likelihood function for use with SBI.

Modifies the _driver and synthesise methods from the base class to return model_flux that is the synthesised signal.

_driver(fast_mode=False, synthesise=False, force_update=False, **kwargs)[source]

Main likelihood evaluation driver routine.

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:

model_flux (numpy.ndarray) synthesised counts.

class xpsi.SBI_wrapper.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.SBI_wrapper.synthesise(self, exposure_time=None, expected_source_counts=None, nchannels=None, nphases=None, seed=0, **kwargs)[source]

Synthesise data set.

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 random number generation for Poisson noise in synthesised data.

Returns:

synthetic (numpy.ndarray) The synthesised data set.

class xpsi.SBI_wrapper.xpsi_wrappers(prior, likelihood, instr_kwargs={}, train_using_CNNs=True)[source]

Class that wraps the xpsi likelihood and prior into a SBI compatible interface.

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

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

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

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

log_prob(parameter_vector)[source]

Compute the log probability of the parameter vector.

Parameters:

parameter_vector (torch.Tensor) – The parameter vector.

Returns:

log_probability (torch.Tensor) The log probability of the 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:

sample (torch.Tensor or torch.cuda.Tensor if CUDA is available.) The sampled values.

simulator(parameter_vector)[source]

Compute the likelihood of the parameter vector.

Parameters:

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

Returns:

model_flux (torch.Tensor) The pulse profile for the input parameter vector.