API

Base PDFs

Time PDFs

flarestack.core.time_pdf.box_func(t, t0, t1)[source]

Box function that is equal to 1 between t0 and t1, and 0 otherwise. Equal to 0.5 at to and t1.

Parameters
  • t – Time to be evaluated

  • t0 – Start time of box

  • t1 – End time of box

Returns

Value of Box function at t

flarestack.core.time_pdf.decay_fct(t, t0, decay_time, truncation=inf)[source]

Decay function that is equal to 0 before t0, equal to 1 at t0 and then decays with a decay time :param t: time to be evaluated :param t0: start time of the function :param decay_time: decay time :param truncation: truncation time, function will give zero for t < truncation :return: value at t

flarestack.core.time_pdf.decay_fct_integral(tstart, tend, t0, decay_time, truncation=inf)[source]

The integral function of decay_function based on the analytical form :param tstart: float, integrating from :param tend: float, integrating to :param t0: float, parameter t0 in decay_function, start time of the decay function :param decay_time: float, decay time :param truncation: float, truncation time, decay function will be 0 for t < truncation :return: float

flarestack.core.time_pdf.read_t_pdf_dict(t_pdf_dict)[source]

Ensures backwards compatibility for t_pdf_dict objects

class flarestack.core.time_pdf.TimePDF(t_pdf_dict, livetime_pdf=None)[source]
subclasses = {'box': <class 'flarestack.core.time_pdf.Box'>, 'custom_source_box': <class 'flarestack.core.time_pdf.CustomSourceBox'>, 'decay': <class 'flarestack.core.time_pdf.DecayPDF'>, 'detector_on_off_list': <class 'flarestack.core.time_pdf.DetectorOnOffList'>, 'fixed_end_box': <class 'flarestack.core.time_pdf.FixedEndBox'>, 'fixed_ref_box': <class 'flarestack.core.time_pdf.FixedRefBox'>, 'steady': <class 'flarestack.core.time_pdf.Steady'>}
__init__(t_pdf_dict, livetime_pdf=None)[source]
classmethod register_subclass(time_pdf_name)[source]
classmethod create(t_pdf_dict, livetime_pdf=None)[source]
product_integral(t, source)[source]

Calculates the product of the given signal PDF with the season box function. Thus gives 0 everywhere outside the season, and otherwise the value of the normalised integral. The season function is offset by 1e-9, to ensure that f(t1) is equal to 1. (i.e the function is equal to 1 at the end of the box).

Parameters
  • t – Time

  • source – Source to be considered

Returns

Product of signal integral and season

inverse_interpolate(source)[source]

Calculates the values for the integral of the signal PDF within the season. Then rescales these values, such that the start of the season yields 0, and then end of the season yields 1. Creates a function to interpolate between these values. Then, for a number between 0 and 1, the interpolated function will return the MJD time at which that fraction of the cumulative distribution was reached.

Parameters

source – Source to be considered

Returns

Interpolated function

simulate_times(source, n_s)[source]

Randomly draws times for n_s events for a given source, all lying within the current season. The values are based on an interpolation of the integrated time PDF.

Parameters
  • source – Source being considered

  • n_s – Number of event times to be simulated

Returns

Array of times in MJD for a given source

f(t, source)[source]
sig_t0(source)[source]

Calculates the starting time for the time pdf.

Parameters

source – Source to be considered

Returns

Time of PDF start

sig_t1(source)[source]

Calculates the ending time for the time pdf.

Parameters

source – Source to be considered

Returns

Time of PDF end

integral_to_infinity(source)[source]
effective_injection_time(source=None)[source]
get_livetime()[source]
get_mjd_conversion()[source]
class flarestack.core.time_pdf.Steady(t_pdf_dict, livetime_pdf=None)[source]

The time-independent case for a Time PDF. Requires no additional arguments in the dictionary for __init__. Used for a steady source that is continuously emitting.

__init__(t_pdf_dict, livetime_pdf=None)[source]
f(t, source)[source]

In the case of a steady source, the signal PDF is a uniform PDF in time. It is thus simply equal to the season_f, normalised with the length of the season to give an integral of 1. It is thus equal to the background PDF.

Parameters
  • t – Time

  • source – Source to be considered

Returns

Value of normalised box function at t

signal_integral(t, source)[source]

In the case of a steady source, the signal PDF is a uniform PDF in time. Thus, the integral is simply a linear function increasing between t0 (box start) and t1 (box end). After t1, the integral is equal to 1, while it is equal to 0 for t < t0.

Parameters
  • t – Time

  • source – Source to be considered

Returns

Value of normalised box function at t

flare_time_mask(source)[source]

In this case, the interesting period for Flare Searches is the entire season. Thus returns the start and end times for the season.

Returns

Start time (MJD) and End Time (MJD) for flare search period

effective_injection_time(source=None)[source]

Calculates the effective injection time for the given PDF. The livetime is measured in days, but here is converted to seconds.

Parameters

source – Source to be considered

Returns

Effective Livetime in seconds

raw_injection_time(source)[source]

Calculates the ‘raw injection time’ which is the injection time assuming a detector with 100% uptime. Useful for calculating source emission times for source-frame energy estimation.

Parameters

source – Source to be considered

Returns

Time in seconds for 100% uptime

sig_t0(source)[source]

Calculates the starting time for the window, equal to the source reference time in MJD minus the length of the pre-reference-time window (in days).

Parameters

source – Source to be considered

Returns

Time of Window Start

sig_t1(source)[source]

Calculates the starting time for the window, equal to the source reference time in MJD plus the length of the post-reference-time window (in days).

Parameters

source – Source to be considered

Returns

Time of Window End

class flarestack.core.time_pdf.Box(t_pdf_dict, season)[source]

The simplest time-dependent case for a Time PDF. Used for a source that is uniformly emitting for a fixed period of time. Requires arguments of Pre-Window and Post_window, and gives a box from Pre-Window days before the reference time to Post-Window days after the reference time.

__init__(t_pdf_dict, season)[source]
sig_t0(source)[source]

Calculates the starting time for the window, equal to the source reference time in MJD minus the length of the pre-reference-time window (in days).

Parameters

source – Source to be considered

Returns

Time of Window Start

sig_t1(source)[source]

Calculates the starting time for the window, equal to the source reference time in MJD plus the length of the post-reference-time window (in days).

Parameters

source – Source to be considered

Returns

Time of Window End

f(t, source=None)[source]

In this case, the signal PDF is a uniform PDF for a fixed duration of time. It is normalised with the length of the box in LIVETIME rather than days, to give an integral of 1.

Parameters
  • t – Time

  • source – Source to be considered

Returns

Value of normalised box function at t

signal_integral(t, source)[source]

In this case, the signal PDF is a uniform PDF for a fixed duration of time. Thus, the integral is simply a linear function increasing between t0 (box start) and t1 (box end). After t1, the integral is equal to 1, while it is equal to 0 for t < t0.

Parameters
  • t – Time

  • source – Source to be considered

Returns

Value of normalised box function at t

flare_time_mask(source)[source]

In this case, the interesting period for Flare Searches is the period of overlap of the flare and the box. Thus, for a given season, return the source and data

Returns

Start time (MJD) and End Time (MJD) for flare search period

effective_injection_time(source=None)[source]

Calculates the effective injection time for the given PDF. The livetime is measured in days, but here is converted to seconds.

Parameters

source – Source to be considered

Returns

Effective Livetime in seconds

raw_injection_time(source)[source]

Calculates the ‘raw injection time’ which is the injection time assuming a detector with 100% uptime. Useful for calculating source emission times for source-frame energy estimation.

Parameters

source – Source to be considered

Returns

Time in seconds for 100% uptime

class flarestack.core.time_pdf.FixedRefBox(t_pdf_dict, season)[source]

The simplest time-dependent case for a Time PDF. Used for a source that is uniformly emitting for a fixed period of time. In this case, the start and end time for the box is unique for each source. The sources must have a field “Start Time (MJD)” and another “End Time (MJD)”, specifying the period of the Time PDF.

__init__(t_pdf_dict, season)[source]
sig_t0(source=None)[source]

Calculates the starting time for the window, equal to the source reference time in MJD minus the length of the pre-reference-time window (in days).

Parameters

source – Source to be considered

Returns

Time of Window Start

sig_t1(source=None)[source]

Calculates the starting time for the window, equal to the source reference time in MJD plus the length of the post-reference-time window (in days).

Parameters

source – Source to be considered

Returns

Time of Window End

class flarestack.core.time_pdf.FixedEndBox(t_pdf_dict, season)[source]

The simplest time-dependent case for a Time PDF. Used for a source that is uniformly emitting for a fixed period of time. In this case, the start and end time for the box is the same for all sources.

__init__(t_pdf_dict, season)[source]
sig_t0(source=None)[source]

Calculates the starting time for the window, equal to the source reference time in MJD minus the length of the pre-reference-time window (in days).

Parameters

source – Source to be considered

Returns

Time of Window Start

sig_t1(source=None)[source]

Calculates the starting time for the window, equal to the source reference time in MJD plus the length of the post-reference-time window (in days).

Parameters

source – Source to be considered

Returns

Time of Window End

get_livetime()[source]
get_mjd_conversion()[source]
class flarestack.core.time_pdf.CustomSourceBox(t_pdf_dict, season)[source]

The simplest time-dependent case for a Time PDF. Used for a source that is uniformly emitting for a fixed period of time. In this case, the start and end time for the box is unique for each source. The sources must have a field “Start Time (MJD)” and another “End Time (MJD)”, specifying the period of the Time PDF.

__init__(t_pdf_dict, season)[source]
sig_t0(source)[source]

Calculates the starting time for the window, equal to the source reference time in MJD minus the length of the pre-reference-time window (in days).

Parameters

source – Source to be considered

Returns

Time of Window Start

sig_t1(source)[source]

Calculates the starting time for the window, equal to the source reference time in MJD plus the length of the post-reference-time window (in days).

Parameters

source – Source to be considered

Returns

Time of Window End

class flarestack.core.time_pdf.DetectorOnOffList(t_pdf_dict, livetime_pdf=None)[source]

TimePDF with predefined on/off periods. Can be used for a livetime function, in which observations are divided into runs with gaps. Can also be used for e.g pre-defined interesting period for a variable source.

__init__(t_pdf_dict, livetime_pdf=None)[source]
f(t, source=None)[source]
livetime_f(t, source=None)[source]
sig_t0(source=None)[source]

Calculates the starting time for the time pdf.

Parameters

source – Source to be considered

Returns

Time of PDF start

sig_t1(source=None)[source]

Calculates the ending time for the time pdf.

Parameters

source – Source to be considered

Returns

Time of PDF end

parse_list()[source]
get_livetime()[source]
get_mjd_conversion()[source]
class flarestack.core.time_pdf.DecayPDF(t_pdf_dict, season)[source]
__init__(t_pdf_dict, season)[source]
decay_function(t, t0)[source]
decay_integral(a, b, t0)[source]
sig_t0(source)[source]

Gives the start time of the signal from a source. If the start time lies within the season, the start time is the source’s “ref_time_mjd”. If not it”s the start of the season. :param source: source to be considered :return: time of signal start

sig_t1(source)[source]

Gives the end time of a signal. For an endless decay, that’s just the end of the season. If the decay length is not infinite, the signal might end before the season ends. :param source: source to be considered :return: end time of signal

signal_integral(t, source)[source]

Gives the integrated signal using decay_fct_integral() :param t: float or array like :param source: the sources to be considered :return: float

effective_injection_time(source=None)[source]

Calculates the effective injection time for the given PDF. The livetime is measured in days, but here is converted to seconds.

Parameters

source – Source to be considered

Returns

Effective Livetime in seconds

raw_injection_time(source)[source]

Calculates the ‘raw injection time’ which is the injection time assuming a detector with 100% uptime. Useful for calculating source emission times for source-frame energy estimation.

Parameters

source – Source to be considered

Returns

Time in seconds for 100% uptime

f(t, source=None)[source]

In this case the PDF is the decay function, normalized to the integral over livetime :param t: float or array_like, Time :param source: Source to be considered :return: Value of normalised box function at t

Energy PDFs

This script contains the EnergyPDF classes, that are used for weighting events based on a given energy PDF.

flarestack.core.energy_pdf.read_e_pdf_dict(e_pdf_dict)[source]

Ensures backwards compatibility of e_pdf_dict objects.

Parameters

e_pdf_dict – Energy PDF dictionary

Returns

Updated Energy PDF dictionary compatible with new format

class flarestack.core.energy_pdf.EnergyPDF(e_pdf_dict)[source]

A base class for Energy PDFs.

subclasses = {'power_law': <class 'flarestack.core.energy_pdf.PowerLaw'>, 'spline': <class 'flarestack.core.energy_pdf.Spline'>}
__init__(e_pdf_dict)[source]

constructor :param e_pdf_dict:

classmethod register_subclass(energy_pdf_name)[source]

Adds a new subclass of EnergyPDF, with class name equal to “energy_pdf_name”.

classmethod create(e_pdf_dict)[source]
static f(energy)[source]
integrate_over_E(f, lower=None, upper=None)[source]

Uses Newton’s method to integrate function f over the energy range. By default, uses 100 GeV to 10 PeV, unless otherwise specified. Uses 1000 logarithmically-spaced bins to calculate integral.

Parameters
  • f – Function to be integrated

  • lower – Lower bound for integration

  • upper – Upper bound for integration

Returns

Integral of function

static integrate(f, lower, upper)[source]
piecewise_integrate_over_energy(f, lower=None, upper=None)[source]

Uses Newton’s method to integrate function f over the energy range. By default, uses 100 GeV to 10 PeV, unless otherwise specified. Uses 1000 logarithmically-spaced bins to calculate integral.

Parameters
  • f – Function to be integrated

  • lower – Lower bound for integration

  • upper – Upper bound for integration

Returns

Integral of function bins

static piecewise_integrate(f, lower, upper)[source]
flux_integral(lower=None, upper=None)[source]

Integrates over energy PDF to give integrated flux (dN/dT)

fluence_integral(lower=None, upper=None)[source]

Performs an integral for fluence over a given energy range. This is gives the total energy per unit area per second that is radiated.

return_energy_parameters()[source]
simulate_true_energies(n_s)[source]
class flarestack.core.energy_pdf.PowerLaw(e_pdf_dict=None)[source]

A Power Law energy PDF. Takes an argument of gamma in the dictionary for the init function, where gamma is the spectral index of the Power Law.

__init__(e_pdf_dict=None)[source]

Creates a PowerLaw object, which is an energy PDF based on a power law. The power law is generated from e_pdf_dict, which can specify a spectral index (Gamma), as well as an optional minimum energy (E Min) and a maximum energy (E Max)

Parameters

e_pdf_dict – Dictionary containing parameters

weight_mc(mc, gamma=None)[source]

Returns an array containing the weights for each MC event, given that the spectral index gamma has been chosen. Weights each event as (E/GeV)^-gamma, and multiplies this by the pre-existing MC oneweight value, to give the overall oneweight.

Parameters
  • mc – Monte Carlo

  • gamma – Spectral Index (default is value in e_pdf_dict)

Returns

Weights Array

f(energy)[source]
flux_integral(e_min=None, e_max=None)[source]

Integrates over energy PDF to give integrated flux (dN/dT)

fluence_integral(e_min=None, e_max=None)[source]

Performs an integral for fluence over a given energy range. This is gives the total energy per unit area per second that is radiated.

return_energy_parameters()[source]
return_injected_parameters()[source]
class flarestack.core.energy_pdf.Spline(e_pdf_dict={})[source]

A Power Law energy PDF. Takes an argument of gamma in the dictionary for the init function, where gamma is the spectral index of the Power Law.

__init__(e_pdf_dict={})[source]

Creates a PowerLaw object, which is an energy PDF based on a power law. The power law is generated from e_pdf_dict, which can specify a spectral index (Gamma), as well as an optional minimum energy (E Min) and a maximum energy (E Max)

Parameters

e_pdf_dict – Dictionary containing parameters

weight_mc(mc)[source]

Returns an array containing the weights for each MC event, given that the spectral index gamma has been chosen. Weights each event using the energy spline, and multiplies this by the pre-existing MC oneweight value, to give the overall oneweight.

Parameters

mc – Monte Carlo

Returns

Weights Array

Spatial PDFs

class flarestack.core.spatial_pdf.SpatialPDF(spatial_pdf_dict, season)[source]

General SpatialPDF holder class. Has separate signal and background spatial PDF objects.

__init__(spatial_pdf_dict, season)[source]
class flarestack.core.spatial_pdf.SignalSpatialPDF(spatial_pdf_dict)[source]

Base Signal Spatial PDF class.

subclasses = {'circular_gaussian': <class 'flarestack.core.spatial_pdf.CircularGaussian'>}
__init__(spatial_pdf_dict)[source]
static simulate_distribution(source, data)[source]
static signal_spatial(source, events)[source]
classmethod register_subclass(spatial_pdf_name)[source]

Adds a new subclass of SpatialPDF, with class name equal to “spatial_pdf_name”.

classmethod create(s_pdf_dict)[source]
static rotate(ra1, dec1, ra2, dec2, ra3, dec3)[source]

Rotate ra1 and dec1 in a way that ra2 and dec2 will exactly map onto ra3 and dec3, respectively. All angles are treated as radians. Essentially rotates the events, so that they behave as if they were originally incident on the source.

Parameters
  • ra1 – Event Right Ascension

  • dec1 – Event Declination

  • ra2 – True Event Right Ascension

  • dec2 – True Event Declination

  • ra3 – Source Right Ascension

  • dec3 – Source Declination

Returns

Returns new Right Ascensions and Declinations

rotate_to_position(ev, ra, dec)[source]

Modifies the events by reassigning the Right Ascension and Declination of the events. Rotates the events, so that they are distributed as if they originated from the source. Removes the additional Monte Carlo information from sampled events, so that they appear like regular data.

The fields removed are:

True Right Ascension, True Declination, True Energy, OneWeight

Parameters
  • ev – Events

  • ra – Source Right Ascension (radians)

  • dec – Source Declination (radians)

Returns

Events (modified)

class flarestack.core.spatial_pdf.CircularGaussian(spatial_pdf_dict)[source]
simulate_distribution(source, data)[source]
static signal_spatial(source, cut_data)[source]

Calculates the angular distance between the source and the coincident dataset. Uses a Gaussian PDF function, centered on the source. Returns the value of the Gaussian at the given distances.

Parameters
  • source – Single Source

  • cut_data – Subset of Dataset with coincident events

Returns

Array of Spatial PDF values

class flarestack.core.spatial_pdf.BackgroundSpatialPDF(spatial_pdf_dict, season)[source]
subclasses = {'uniform': <class 'flarestack.core.spatial_pdf.UniformPDF'>, 'uniform_solid_angle': <class 'flarestack.core.spatial_pdf.UniformSolidAngle'>, 'zenith_spline': <class 'flarestack.core.spatial_pdf.ZenithSpline'>}
__init__(spatial_pdf_dict, season)[source]
classmethod register_subclass(bkg_spatial_name)[source]

Adds a new subclass of BackgroundSpatialPDF, with class name equal to “spatial_pdf_name”.

classmethod create(s_pdf_dict, season)[source]
background_spatial(events)[source]
class flarestack.core.spatial_pdf.UniformPDF(spatial_pdf_dict, season)[source]

A highly-simplified spatial PDF in which events are distributed uniformly over the celestial sphere.

background_spatial(events)[source]
class flarestack.core.spatial_pdf.UniformSolidAngle(spatial_pdf_dict, season)[source]

Generic class for a background PDF that is uniform over some fixed area, and 0 otherwise. Requires an argument to be passed in the ‘spatial_pdf_dict’, with key ‘background_solid_angle’. In the limit of a solid angle of 4 pi, this becomes identical to the UniformPDF class.

__init__(spatial_pdf_dict, season)[source]
background_spatial(events)[source]
class flarestack.core.spatial_pdf.ZenithSpline(spatial_pdf_dict, season)[source]

A 1D background spatial PDF, in which the background is assumed to be uniform in azimuth, but varying as a function of zenith. A spline is used to parameterise this distribution.

__init__(spatial_pdf_dict, season)[source]
static create_background_function(season)[source]
background_spatial(events)[source]

Composite PDF Objects

Injector

flarestack.core.injector.read_injector_dict(inj_dict)[source]

Ensures that injection dictionaries remain backwards-compatible

Parameters

inj_dict – Injection Dictionary

Returns

Injection Dictionary compatible with new format

class flarestack.core.injector.BaseInjector(season, sources, **kwargs)[source]

Base Injector Class

subclasses = {'effective_area_injector': <class 'flarestack.core.injector.EffectiveAreaInjector'>, 'low_memory_injector': <class 'flarestack.core.injector.LowMemoryInjector'>, 'mc_injector': <class 'flarestack.core.injector.MCInjector'>}
__init__(season, sources, **kwargs)[source]
calculate_n_exp()[source]
calculate_n_exp_single(source)[source]
get_n_exp_single(source)[source]
get_expectation(source, scale)[source]
update_sources(sources)[source]

Reuses an injector with new sources

Parameters

sources – Sources to be added

create_dataset(scale, angular_error_modifier=None)[source]

Create a dataset based on scrambled data for background, and Monte Carlo simulation for signal. Returns the composite dataset. The source flux can be scaled by the scale parameter.

Parameters
  • scale – Ratio of Injected Flux to source flux

  • angular_error_modifier – AngularErrorModifier to change angular errors

Returns

Simulated dataset

inject_signal(scale)[source]
classmethod register_subclass(inj_name)[source]

Adds a new subclass of EnergyPDF, with class name equal to “energy_pdf_name”.

classmethod create(season, sources, **kwargs)[source]
static get_dec_and_omega(source, bandwidth)[source]
class flarestack.core.injector.MCInjector(season, sources, **kwargs)[source]

Core Injector Class, returns a dataset on which calculations can be performed. This base class is tailored for injection of MC into mock background. This can be either MC background, or scrambled real data.

subclasses = {}
__init__(season, sources, **kwargs)[source]
select_mc_band(source)[source]

For a given source, selects MC events within a declination band of width +/- 5 degrees that contains the source. Then returns the MC data subset containing only those MC events.

Parameters

source – Source to be simulated

Returns

mc (cut): Simulated events which lie within the band

Returns

omega: Solid Angle of the chosen band

Returns

band_mask: The mask which removes events outside band

get_band_mask(source, min_dec, max_dec)[source]
calculate_single_source(source, scale)[source]

Calculate the weighted MC for a single source, given a flux scale and a distance scale.

Parameters
  • source

  • scale

Returns

calculate_n_exp_single(source)[source]
calculate_fluence(source, scale, source_mc, band_mask, omega)[source]

Function to calculate the fluence for a given source, and multiply the oneweights by this. After this step, the oneweight sum is equal to the expected neutrino number.

Parameters
  • source – Source to be calculated

  • scale – Flux scale

  • source_mc – MC that is close to source

  • band_mask – Closeness mask for MC

  • omega – Solid angle covered by MC mask

Returns

Modified source MC

inject_signal(scale)[source]

Randomly select simulated events from the Monte Carlo dataset to simulate a signal for each source. The source flux can be scaled by the scale parameter.

Parameters

scale – Ratio of Injected Flux to source flux.

Returns

Set of signal events for the given IC Season.

class flarestack.core.injector.LowMemoryInjector(season, sources, **kwargs)[source]

For large numbers of sources O(~100), saving MC masks becomes increasingly burdensome. As a solution, the LowMemoryInjector should be used instead. It will be somewhat slower, but will have much more reasonable memory consumption.

__init__(season, sources, **kwargs)[source]
calculate_n_exp()[source]
make_injection_band_mask()[source]
load_band_mask(index)[source]
get_band_mask(source, min_dec, max_dec)[source]
class flarestack.core.injector.EffectiveAreaInjector(season, sources, **kwargs)[source]

Class for injecting signal events by relying on effective areas rather than pre-existing Monte Carlo simulation. This Injector should be used for analysing public data, as no MC is provided.

__init__(season, sources, **kwargs)[source]
inject_signal(scale)[source]
calculate_single_source(source, scale)[source]
calculate_n_exp_single(source)[source]
calculate_energy_proxy(source)[source]
class flarestack.core.injector.MockUnblindedInjector(season, sources=nan, **kwargs)[source]

If the data is not really to be unblinded, then MockUnblindedInjector should be called. In this case, the create_dataset function simply returns one background scramble.

__init__(season, sources=nan, **kwargs)[source]
create_dataset(scale, angular_error_modifier=None)[source]

Returns a background scramble

Returns

Scrambled data

class flarestack.core.injector.TrueUnblindedInjector(season, sources, **kwargs)[source]

If the data is unblinded, then UnblindedInjector should be called. In this case, the create_dataset function simply returns the unblinded dataset.

__init__(season, sources, **kwargs)[source]
create_dataset(scale, angular_error_modifier=None)[source]

Log Likelihood

flarestack.core.llh.read_llh_dict(llh_dict)[source]

Ensures that llh dictionaries remain backwards-compatible

Parameters

llh_dict – LLH Dictionary

Returns

LLH Dictionary compatible with new format

class flarestack.core.llh.LLH(season, sources, llh_dict)[source]

Base class LLH.

subclasses = {'fixed_energy': <class 'flarestack.core.llh.FixedEnergyLLH'>, 'spatial': <class 'flarestack.core.llh.SpatialLLH'>, 'standard': <class 'flarestack.core.llh.StandardLLH'>, 'standard_matrix': <class 'flarestack.core.llh.StandardMatrixLLH'>, 'standard_overlapping': <class 'flarestack.core.llh.StandardOverlappingLLH'>}
__init__(season, sources, llh_dict)[source]
classmethod register_subclass(llh_name)[source]

Adds a new subclass of EnergyPDF, with class name equal to “energy_pdf_name”.

classmethod create(season, sources, llh_dict)[source]
classmethod get_parameters(llh_dict)[source]
classmethod get_injected_parameters(mh_dict)[source]
signal_pdf(source, cut_data)[source]

Calculates the value of the signal spatial PDF for a given source for each event in the coincident data subsample. If there is a Time PDF given, also calculates the value of the signal Time PDF for each event. Returns either the signal spatial PDF values, or the product of the signal spatial and time PDFs.

Parameters
  • source – Source to be considered

  • cut_data – Subset of Dataset with coincident events

Returns

Array of Signal Spacetime PDF values

background_pdf(source, cut_data)[source]

Calculates the value of the background spatial PDF for a given source for each event in the coincident data subsample. Thus is done by calling the self.bkg_spline spline function, which was fitted to the Sin(Declination) distribution of the data.

If there is a signal Time PDF given, then the background time PDF is also calculated for each event. This is assumed to be a normalised uniform distribution for the season.

Returns either the background spatial PDF values, or the product of the background spatial and time PDFs.

Parameters
  • source – Source to be considered

  • cut_data – Subset of Dataset with coincident events

Returns

Array of Background Spacetime PDF values

create_energy_functions()[source]

Creates the acceptance function, which parameterises signal acceptance as a function of declination, and the energy weighting function, which gives the energy signal-over-background ratio

Returns

Acceptance function, energy_weighting_function

select_spatially_coincident_data(data, sources)[source]

Checks each source, and only identifies events in data which are both spatially and time-coincident with the source. Spatial coincidence is defined as a +/- 5 degree box centered on the given source. Time coincidence is determined by the parameters of the LLH Time PDF. Produces a mask for the dataset, which removes all events which are not coincident with at least one source.

Parameters
  • data – Dataset to be tested

  • sources – Sources to be tested

Returns

Mask to remove

static assume_background(n_s, n_coincident, n_all)[source]

To save time with likelihood calculation, it can be assumed that all events defined as “non-coincident”, because of distance in space and time to the source, are in fact background events. This is equivalent to setting S=0 for all non-coincident events. IN this case, the likelihood can be calculated as the product of the number of non-coincident events, and the likelihood of an event which has S=0.

Parameters
  • n_s – Array of expected number of events

  • n_coincident – Number of events that were not assumed to have S=0

  • n_all – The total number of events

Returns

Log Likelihood value for the given

create_kwargs(data, pull_corrector, weight_f=None)[source]
create_llh_function(data, pull_corrector, weight_f=None)[source]

Creates a likelihood function to minimise, based on the dataset.

Parameters

data – Dataset

Returns

LLH function that can be minimised

calculate_test_statistic(params, weights, **kwargs)[source]
static return_llh_parameters(llh_dict)[source]
static return_injected_parameters(mh_dict)[source]
class flarestack.core.llh.SpatialLLH(season, sources, llh_dict)[source]

Most basic LLH, in which only spatial, and optionally also temporal, information is included. No Energy PDF is used, and no energy weighting is applied.

fit_energy = False
__init__(season, sources, llh_dict)[source]
create_energy_function()[source]

In the most simple case of spatial-only weighting, you would neglect the energy weighting of events. Then, you can simply assume that the detector acceptance is roughly proportional to the data rate, i.e assuming that the incident background atmospheric neutrino flux is uniform. Thus the acceptance of the detector is simply the background spatial PDF (which is a spline fitted to data as a function of declination). This method does, admittedly neglect the fact that background in the southern hemisphere is mainly composed of muon bundles, rather than atmospheric neutrinos. Still, it’s slighty better than assuming a uniform detector acceptance

Returns

1D linear interpolation

create_llh_function(data, pull_corrector, weight_f=None)[source]

Creates a likelihood function to minimise, based on the dataset.

Parameters
  • data – Dataset

  • pull_corrector – pull_corrector

Returns

LLH function that can be minimised

calculate_test_statistic(params, weights, **kwargs)[source]

Calculates the test statistic, given the parameters. Uses numexpr for faster calculations.

Parameters
  • params – Parameters from Minimisation

  • weights – Normalised fraction of n_s allocated to each source

Returns

2 * llh value (Equal to Test Statistic)

class flarestack.core.llh.FixedEnergyLLH(season, sources, llh_dict)[source]
fit_energy = False
__init__(season, sources, llh_dict)[source]
create_energy_functions()[source]

Creates the acceptance function, which parameterises signal acceptance as a function of declination, and the energy weighting function, which gives the energy signal-over-background ratio

Returns

Acceptance function, energy_weighting_function

create_acceptance_function(acc_path)[source]
create_energy_weighting_function(SoB_path)[source]
create_kwargs(data, pull_corrector, weight_f=None)[source]

Creates a likelihood function to minimise, based on the dataset.

Parameters

data – Dataset

Returns

LLH function that can be minimised

calculate_test_statistic(params, weights, **kwargs)[source]

Calculates the test statistic, given the parameters. Uses numexpr for faster calculations.

Parameters
  • params – Parameters from Minimisation

  • weights – Normalised fraction of n_s allocated to each source

Returns

2 * llh value (Equal to Test Statistic)

class flarestack.core.llh.StandardLLH(season, sources, llh_dict)[source]
fit_energy = True
__init__(season, sources, llh_dict)[source]
create_energy_functions()[source]

Creates the acceptance function, which parameterises signal acceptance as a function of declination, and the energy weighting function, which gives the energy signal-over-background ratio

Returns

Acceptance function, energy_weighting_function

create_acceptance_function()[source]

Creates a 2D linear interpolation of the acceptance of the detector for the given season, as a function of declination and gamma. Returns this interpolation function.

Returns

2D linear interpolation

new_acceptance(source, params=None)[source]

Calculates the detector acceptance for a given source, using the 2D interpolation of the acceptance as a function of declination and gamma. If gamma IS NOT being fit, uses the default value of gamma for weighting (determined in __init__). If gamma IS being fit, it will be the last entry in the parameter array, and is the acceptance uses this value.

Parameters
  • source – Source to be considered

  • params – Parameter array

Returns

Value for the acceptance of the detector, in the given

season, for the source

create_kwargs(data, pull_corrector, weight_f=None)[source]

Creates a likelihood function to minimise, based on the dataset.

Parameters

data – Dataset

Returns

LLH function that can be minimised

calculate_test_statistic(params, weights, **kwargs)[source]

Calculates the test statistic, given the parameters. Uses numexpr for faster calculations.

Parameters
  • params – Parameters from Minimisation

  • weights – Normalised fraction of n_s allocated to each source

Returns

2 * llh value (Equal to Test Statistic)

create_SoB_energy_cache(cut_data)[source]

Evaluates the Log(Signal/Background) values for all coincident data. For each value of gamma in self.gamma_support_points, calculates the Log(Signal/Background) values for the coincident data. Then saves each weight array to a dictionary.

Parameters

cut_data – Subset of the data containing only coincident events

Returns

Dictionary containing SoB values for each event for each

gamma value.

estimate_energy_weights(gamma, energy_SoB_cache)[source]

Quickly estimates the value of Signal/Background for Gamma. Uses pre-calculated values for first and second derivatives. Uses a Taylor series to estimate S(gamma), unless SoB has already been calculated for a given gamma.

Parameters
  • gamma – Spectral Index

  • energy_SoB_cache – Weight cache

Returns

Estimated value for S(gamma)

static return_llh_parameters(llh_dict)[source]
static return_injected_parameters(mh_dict)[source]
class flarestack.core.llh.StandardOverlappingLLH(season, sources, llh_dict)[source]
create_kwargs(data, pull_corrector, weight_f=None)[source]

Creates a likelihood function to minimise, based on the dataset.

Parameters

data – Dataset

Returns

LLH function that can be minimised

calculate_test_statistic(params, weights, **kwargs)[source]

Calculates the test statistic, given the parameters. Uses numexpr for faster calculations.

Parameters
  • params – Parameters from Minimisation

  • weights – Normalised fraction of n_s allocated to each source

Returns

2 * llh value (Equal to Test Statistic)

class flarestack.core.llh.StandardMatrixLLH(season, sources, llh_dict)[source]
create_kwargs(data, pull_corrector, weight_f=None)[source]

Creates a likelihood function to minimise, based on the dataset.

Parameters

data – Dataset

Returns

LLH function that can be minimised

flarestack.core.llh.generate_dynamic_flare_class(season, sources, llh_dict)[source]

Utils

IceCube Utils

flarestack.icecube_utils.dataset_loader.data_loader(data_path, floor=True, cut_fields=True)[source]

Helper function to load data for a given season/set of season. Adds sinDec field if this is not available, and combines multiple years of data is appropriate (different sets of data from the same icecube configuration should be given as a list)

Parameters
  • data_path – Path to data or list of paths to data

  • cut_fields – Boolean to remove unused fields from datasets on loading

Returns

Loaded Dataset (experimental or MC)

flarestack.icecube_utils.dataset_loader.grl_loader(season)[source]
flarestack.icecube_utils.dataset_loader.verify_grl_with_data(seasons)[source]

Submitter

class flarestack.cluster.submitter.Submitter(mh_dict, use_cluster, n_cpu=None, do_sensitivity_scale_estimation=False, remove_old_results=False, **cluster_kwargs)[source]
submitter_dict = {'DESY': <class 'flarestack.cluster.submitter.DESYSubmitter'>, 'WIPAC': <class 'flarestack.cluster.submitter.WIPACSubmitter'>, 'local': <class 'flarestack.cluster.submitter.LocalSubmitter'>}
__init__(mh_dict, use_cluster, n_cpu=None, do_sensitivity_scale_estimation=False, remove_old_results=False, **cluster_kwargs)[source]

A class that takes care of submitting the trial calculations. Also can estimate the sensitivity scale before submitting. :param mh_dict: dict, MinimisationHandler dictionary :param use_cluster: bool, whether to run the trials locally or on the cluster :param n_cpu: int, number of cores to use :param do_sensitivity_scale_estimation: str, containing ‘asimov’, ‘quick_injections’ or both :param remove_old_results: bool, if True will delete directories containing injection values and pickled

results from previous trials

Parameters

cluster_kwargs – keyword arguments used by the cluster

submit_cluster(mh_dict)[source]

Splits the trials into jobs and submits them to be calculated on the cluster

submit_local(mh_dict)[source]

Uses the MultiprocessWrapper to split the trials into jobs and run them locally

submit(mh_dict)[source]
wait_for_job()[source]

Waits until the cluster is finished processing the job with the ID self.job_id

static get_pending_ids()[source]
static wait_for_cluster(job_ids=None)[source]

Waits until the cluster is done. Wait for all jobs if job_ids is None or give a list of IDs

Parameters

job_ids – list, optional, if given, specifies the IDs of the obs that will be waited on

run_quick_injections_to_estimate_sensitivity_scale()[source]

Roughly estimates the injection scale in order to find a better scale range. The quick injection trials are run locally. Note that a scale still has to be given in the mh_dict as a first estimate.

do_asimov_scale_estimation()[source]

estimate the injection scale using Asimov estimation

analyse(do_disc=False)[source]

Submits the minimisation handler dictionary (self.mh_dict) to be analysed. This happens locally if self.use_cluster == False. :param do_disc: bool, if True, use the estimated discovery potential as

the injection scale instead of the sensitivity.

classmethod register_submitter_class(server_name)[source]

Adds a new subclass of Submitter, with class name equal to “server_name”.

classmethod get_submitter(*args, **kwargs)[source]

Get an initialised instance of the Submitter class suited for the used server. :param args: arguments passed to teh submitter :param kwargs: keyword arguments passed to the submitter :return: instance of Submitter subclass

classmethod get_submitter_class()[source]

Get the Submitter class suited for the used server.

class flarestack.cluster.submitter.LocalSubmitter(mh_dict, use_cluster, n_cpu=None, do_sensitivity_scale_estimation=False, **cluster_kwargs)[source]
__init__(mh_dict, use_cluster, n_cpu=None, do_sensitivity_scale_estimation=False, **cluster_kwargs)[source]

A class that takes care of submitting the trial calculations. Also can estimate the sensitivity scale before submitting. :param mh_dict: dict, MinimisationHandler dictionary :param use_cluster: bool, whether to run the trials locally or on the cluster :param n_cpu: int, number of cores to use :param do_sensitivity_scale_estimation: str, containing ‘asimov’, ‘quick_injections’ or both :param remove_old_results: bool, if True will delete directories containing injection values and pickled

results from previous trials

Parameters

cluster_kwargs – keyword arguments used by the cluster

class flarestack.cluster.submitter.DESYSubmitter(mh_dict, use_cluster, n_cpu=None, **cluster_kwargs)[source]
submit_file = '/home/docs/flarestack__data/cluster/SubmitDESY.sh'
username = 'docs'
status_cmd = 'qstat -u docs'
submit_cmd = 'qsub'
root_dir = '/home/docs/checkouts/readthedocs.org/user_builds/flarestack/checkouts/v2.4.5'
__init__(mh_dict, use_cluster, n_cpu=None, **cluster_kwargs)[source]

Initialises a DESYSubmitter instance.

Parameters
  • mh_dict (dict) – the MinimisationHandler dict

  • use_cluster (bool) – wjether to use the cluster

  • n_cpu (int) – how many CPUs to use on the local machine

  • cluster_kwargs – keyword arguments for the cluster, available are: h_cpu in the form “hh:mm:ss”: how long the cluster jobs run trials_per_task: int, how many trials to run per cluster job cluster_cpu: int, how many CPUs to use on the cluster machines ram_per_core in the form “<number>G”: e.g. 6G to use 6GB RAM for each cluster job

static get_ids(qstat_command)[source]

Takes a command that queries the DESY cluster and returns a list of job IDs

property ntasks_total

Returns the total number of tasks

property ntasks_running

Returns the number of running tasks

wait_for_job()[source]

Waits until the cluster is finished processing the job with the ID self.job_id

make_cluster_submission_script()[source]

Produces the shell script used to run on the DESY cluster.

submit_cluster(mh_dict)[source]

Submits the job to the cluster

static get_pending_ids()[source]
static clear_log_dir()[source]
class flarestack.cluster.submitter.WIPACSubmitter(*args, **kwargs)[source]
wipac_cluster_dir = '/home/docs/flarestack__data/cluster/WIPAC'
home_dir = '/home/docs'
username = 'docs'
status_cmd = 'condor_q docs'
root_dir = '/home/docs/checkouts/readthedocs.org/user_builds/flarestack/checkouts/v2.4.5'
scratch_on_nodes = '/scratch/docs'
__init__(*args, **kwargs)[source]

A class that takes care of submitting the trial calculations. Also can estimate the sensitivity scale before submitting.

Parameters
  • args – arguments to be passed to Submitter class

  • kwargs – keyword arguments used by the cluster, available are: manual_submit: bool, if True only prints out the location of the submit file without actually submitting to the cluster trials_per_task: int, how many trials to run per cluster job cluster_cpu: int, how many CPUs to use per cluster job ram_per_core: float, how much RAM for each cluster jobs in MB

make_executable_file(path)[source]

Produces the executable that will be submitted to the NPX cluster. :param path: str, path to the file

make_submit_file(n_tasks)[source]

Produces the submit file that will be submitted to the NPX cluster. :param n_tasks: Number of jobs that will be created

submit_cluster(mh_dict)[source]

Submits the job to the cluster

static get_condor_status()[source]

Queries condor to get cluster status. :return: str, output of query command

collect_condor_status()[source]

Gets the condor status and saves it to private attribute

property condor_status

Get the status of jobs running on condor. :return: number of jobs that are done, running, waiting, total, held

wait_for_job()[source]

Waits until the cluster is finished processing the job with the ID self.job_id

static get_pending_ids()[source]