Stingray API

Library of Time Series Methods For Astronomical X-ray Data.

Base Class

Most stingray classes are subclasses of a single class, stingray.StingrayObject, which implements most of the I/O functionality and common behavior (e.g. strategies to combine data and make operations such as the sum, difference, or negation). This class is not intended to be instantiated directly, but rather to be used as a base class for other classes. Any class wanting to inherit from stingray.StingrayObject should define a main_array_attr attribute, which defines the name of the attribute that will be used to store the “independent variable” main data array. For example, for all time series-like objects, the main array is the time array, while for the periodograms the main array is the frequency array. All arrays sharing the length (not the shape: they might be multi-dimensional!) of the main array are called “array attributes” and are accessible through the array_attrs method. When applying a mask or any other selection to a stingray.StingrayObject, all array attributes are filtered in the same way. Some array-like attributes might have the same length by chance, in this case the user or the developer should add these to the not_array_attr attribute. For example, stingray.StingrayTimeseries has gti among the not_array_attrs, since it is an array but not related 1-to-1 to the main array, even if in some cases it might happen to have the same numbers of elements of the main array, which is time.

StingrayObject

class stingray.StingrayObject(*args, **kwargs)[source]

This base class defines some general-purpose utilities.

The main purpose is to have a consistent mechanism for:

  • round-tripping to and from Astropy Tables and other dataframes

  • round-tripping to files in different formats

The idea is that any object inheriting StingrayObject should, just by defining an attribute called main_array_attr, be able to perform the operations above, with no additional effort.

main_array_attr is, e.g. time for StingrayTimeseries and Lightcurve, freq for Crossspectrum, energy for VarEnergySpectrum, and so on. It is the array with which all other attributes are compared: if they are of the same shape, they get saved as columns of the table/dataframe, otherwise as metadata.

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)[source]

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)[source]

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str][source]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

data_attributes() → list[str][source]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

dict() → dict[source]

Return a dictionary representation of the object.

classmethod from_astropy_table(ts: Table) → Tso[source]

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

classmethod from_pandas(ts: DataFrame) → Tso[source]

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

classmethod from_xarray(ts: Dataset) → Tso[source]

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict[source]

Give a dictionary with all non-None meta attrs of the object.

internal_array_attrs() → list[str][source]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

meta_attrs() → list[str][source]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str[source]

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

classmethod read(filename: str, fmt: str = None) → Tso[source]

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)[source]

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

to_astropy_table(no_longdouble=False) → Table[source]

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_pandas() → DataFrame[source]

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset[source]

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

write(filename: str, fmt: str = None) → None[source]

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


Data Classes

These classes define basic functionality related to common data types and typical methods that apply to these data types, including basic read/write functionality. Currently implemented are stingray.StingrayTimeseries, stingray.Lightcurve and stingray.events.EventList.

All time series-like data classes inherit from stingray.StingrayTimeseries, which implements most of the common functionality. The main data array is stored in the time attribute. Good Time Intervals (GTIs) are stored in the gti attribute, which is a list of 2-tuples or 2-lists containing the start and stop times of each GTI. The gti attribute is not an array attribute, since it is not related 1-to-1 to the main array, even if in some cases it might happen to have the same number of elements of the main array. It is by default added to the not_array_attr attribute.

StingrayTimeseries

class stingray.StingrayTimeseries(time: TTime = None, array_attrs: dict = {}, mjdref: TTime = 0, notes: str = '', gti: npt.ArrayLike = None, high_precision: bool = False, ephem: str = None, timeref: str = None, timesys: str = None, skip_checks: bool = False, **other_kw)[source]

Basic class for time series data.

This can be events, binned light curves, unevenly sampled light curves, etc. The only requirement is that the data (which can be any quantity, related or not to an electromagnetic measurement) are associated with a time measurement. We make a distinction between the array attributes, which have the same length of the time array, and the meta attributes, which can be scalars or arrays of different size. The array attributes can be multidimensional (e.g. a spectrum for each time bin), but their first dimension (array.shape[0]) must have same length of the time array.

Array attributes are singled out automatically depending on their shape. All filtering operations (e.g. apply_gtis, rebin, etc.) are applied to array attributes only. For this reason, it is advisable to specify whether a given attribute should not be considered as an array attribute by adding it to the not_array_attr list.

Parameters:
time: iterable

A list or array of time stamps

Attributes:
time: numpy.ndarray

The array of time stamps, in seconds from the reference MJD defined in mjdref

not_array_attr: list

List of attributes that are never to be considered as array attributes. For example, GTIs are not array attributes.

dt: float

The time resolution of the measurements. Can be a scalar or an array attribute (useful for non-evenly sampled data or events from different instruments). It can also be 0, which means that the time series is not evenly sampled and the effects of the time resolution are considered negligible for the analysis. This is sometimes the case for events from high-energy telescopes.

mjdreffloat

The MJD used as a reference for the time array.

gtis: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Good Time Intervals

high_precisionbool

Change the precision of self.time to float128. Useful while dealing with fast pulsars.

Other Parameters:
array_attrsdict

Array attributes to be set (e.g. {"flux": flux_array, "flux_err": flux_err_array}). In principle, they could be specified as simple keyword arguments. But this way, we will run a check on the length of the arrays, and raise an error if they are not of a shape compatible with the time array.

dt: float

The time resolution of the time series. Can be a scalar or an array attribute (useful for non-evenly sampled data or events from different instruments)

mjdreffloat

The MJD used as a reference for the time array.

gtis: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Good Time Intervals

high_precisionbool

Change the precision of self.time to float128. Useful while dealing with fast pulsars.

timerefstr

The time reference, as recorded in the FITS file (e.g. SOLARSYSTEM)

timesysstr

The time system, as recorded in the FITS file (e.g. TDB)

ephemstr

The JPL ephemeris used to barycenter the data, if any (e.g. DE430)

skip_checksbool

Skip checks on the time array. Useful when the user is reasonably sure that the input data are valid.

**other_kw

Used internally. Any other keyword arguments will be set as attributes of the object.

analyze_by_gti(func, fraction_step=1, **kwargs)[source]

Analyze the light curve with any function, on a GTI-by-GTI base.

Parameters:
funcfunction

Function accepting a StingrayTimeseries object as single argument, plus possible additional keyword arguments, and returning a number or a tuple - e.g., (result, error) where both result and error are numbers.

Returns:
start_timesarray

Lower time boundaries of all time segments.

stop_timesarray

upper time boundaries of all segments.

resultarray of N elements

The result of func for each segment of the light curve

Other Parameters:
fraction_stepfloat

By default, segments do not overlap (fraction_step = 1). If fraction_step < 1, then the start points of consecutive segments are fraction_step * segment_size apart, and consecutive segments overlap. For example, for fraction_step = 0.5, the window shifts one half of segment_size)

kwargskeyword arguments

These additional keyword arguments, if present, they will be passed to func

analyze_segments(func, segment_size, fraction_step=1, **kwargs)[source]

Analyze segments of the light curve with any function.

Intervals with less than one data point are skipped.

Parameters:
funcfunction

Function accepting a StingrayTimeseries object as single argument, plus possible additional keyword arguments, and returning a number or a tuple - e.g., (result, error) where both result and error are numbers.

segment_sizefloat

Length in seconds of the light curve segments. If None, the full GTIs are considered instead as segments.

Returns:
start_timesarray

Lower time boundaries of all time segments.

stop_timesarray

upper time boundaries of all segments.

resultlist of N elements

The result of func for each segment of the light curve. If the function returns multiple outputs, they are returned as a list of arrays. If a given interval has not enough data for a calculation, None is returned.

Other Parameters:
fraction_stepfloat

If the step is not a full segment_size but less (e.g. a moving window), this indicates the ratio between step step and segment_size (e.g. 0.5 means that the window shifts of half segment_size)

kwargskeyword arguments

These additional keyword arguments, if present, they will be passed to func

Examples

>>> import numpy as np
>>> time = np.arange(0, 10, 0.1)
>>> counts = np.zeros_like(time) + 10
>>> ts = StingrayTimeseries(time, counts=counts, dt=0.1)
>>> # Define a function that calculates the mean
>>> mean_func = lambda ts: np.mean(ts.counts)
>>> # Calculate the mean in segments of 5 seconds
>>> start, stop, res = ts.analyze_segments(mean_func, 5)
>>> len(res) == 2
True
>>> np.allclose(res, 10)
True
apply_gti_lists(new_gti_lists)[source]

Split the event list into different files, each with a different GTI.

Parameters:
new_gti_listslist of lists

A list of lists of GTIs. Each sublist should contain a list of GTIs for a new file.

Returns:
output_fileslist of str

A list of the output file names.

apply_gtis(new_gti=None, inplace: bool = True)[source]

Apply Good Time Intervals (GTIs) to a time series. Filters all the array attributes, only keeping the bins that fall into GTIs.

Parameters:
inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

change_mjdref(new_mjdref: float, inplace=False) → StingrayTimeseries[source]

Change the MJD reference time (MJDREF) of the time series

The times of the time series will be shifted in order to be referred to this new MJDREF

Parameters:
new_mjdreffloat

New MJDREF

Returns:
new_tsStingrayTimeseries object

The new time series, shifted by MJDREF

Other Parameters:
inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

concatenate(other, check_gti=True)[source]

Concatenate two StingrayTimeseries objects.

This method concatenates two or more StingrayTimeseries objects along the time axis. GTIs are recalculated by merging all the GTIs together. GTIs should not overlap at any point.

Parameters:
otherStingrayTimeseries object or list of StingrayTimeseries objects

A second time series object, or a list of objects to be concatenated

Other Parameters:
check_gtibool

Check if the GTIs are overlapping or not. Default: True If this is True and GTIs overlap, an error is raised.

estimate_segment_size(min_counts=None, min_samples=None, even_sampling=None)[source]

Estimate a reasonable segment length for segment-by-segment analysis.

The user has to specify a criterion based on a minimum number of counts (if the time series has a counts attribute) or a minimum number of time samples. At least one between min_counts and min_samples must be specified. In the special case of a time series with dt=0 (event list-like, where each time stamp correspond to a single count), the two definitions are equivalent.

Returns:
segment_sizefloat

The length of the light curve chunks that satisfies the conditions

Other Parameters:
min_countsint

Minimum number of counts for each chunk. Optional (but needs min_samples if left unspecified). Only makes sense if the series has a counts attribute and it is evenly sampled.

min_samplesint

Minimum number of time bins. Optional (but needs min_counts if left unspecified).

even_samplingbool

Force the treatment of the data as evenly sampled or not. If None, the data are considered evenly sampled if self.dt is larger than zero and the median separation between subsequent times is within 1% of self.dt.

Examples

>>> import numpy as np
>>> time = np.arange(150)
>>> counts = np.zeros_like(time) + 3
>>> ts = StingrayTimeseries(time, counts=counts, dt=1)
>>> assert np.isclose(ts.estimate_segment_size(min_counts=10, min_samples=3), 4.0)
>>> assert np.isclose(ts.estimate_segment_size(min_counts=10, min_samples=5), 5.0)
>>> counts[2:4] = 1
>>> ts = StingrayTimeseries(time, counts=counts, dt=1)
>>> assert np.isclose(ts.estimate_segment_size(min_counts=3, min_samples=1), 3.0)
>>> # A slightly more complex example
>>> dt=0.2
>>> time = np.arange(0, 1000, dt)
>>> counts = np.random.poisson(100, size=len(time))
>>> ts = StingrayTimeseries(time, counts=counts, dt=dt)
>>> assert np.isclose(ts.estimate_segment_size(100, 2), 0.4)
>>> min_total_bins = 40
>>> assert np.isclose(ts.estimate_segment_size(100, 40), 8.0)
property exposure

Return the total exposure of the time series, i.e. the sum of the GTIs.

Returns:
total_exposurefloat

The total exposure of the time series, in seconds.

fill_bad_time_intervals(max_length=None, attrs_to_randomize=None, buffer_size=None, even_sampling=None, seed=None)[source]

Fill short bad time intervals with random data.

Warning

This method is only appropriate for very short bad time intervals. The simulated data are basically white noise, so they are able to alter the statistical properties of variable data. For very short gaps in the data, the effect of these small injections of white noise should be negligible. How short depends on the single case, the user is urged not to use the method as a black box and make simulations to measure its effect. If you have long bad time intervals, you should use more advanced techniques, not currently available in Stingray for this use case, such as Gaussian Processes. In particular, please verify that the values of max_length and buffer_size are adequate to your case.

To fill the gaps in all but the time points (i.e., flux measures, energies), we take the buffer_size (by default, the largest value between 100 and the estimated samples in a max_length-long gap) valid data points closest to the gap and repeat them randomly with the same empirical statistical distribution. So, if the my_fancy_attr attribute, in the 100 points of the buffer, has 30 times 10, 10 times 9, and 60 times 11, there will be on average 30% of 10, 60% of 11, and 10% of 9 in the simulated data.

Times are treated differently depending on the fact that the time series is evenly sampled or not. If it is not, the times are simulated from a uniform distribution with the same count rate found in the buffer. Otherwise, times just follow the same grid used inside GTIs. Using the evenly sampled or not is decided based on the even_sampling parameter. If left to None, the time series is considered evenly sampled if self.dt is greater than zero and the median separation between subsequent times is within 1% of the time resolution.

Other Parameters:
max_lengthfloat

Maximum length of a bad time interval to be filled. If None, the criterion is bad time intervals shorter than 1/100th of the longest good time interval.

attrs_to_randomizelist of str, default None

List of array_attrs to randomize. If None, all array_attrs are randomized. It should not include time and _mask, which are treated separately.

buffer_sizeint, default 100

Number of good data points to use to calculate the means and variance the random data on each side of the bad time interval

even_samplingbool, default None

Force the treatment of the data as evenly sampled or not. If None, the data are considered evenly sampled if self.dt is larger than zero and the median separation between subsequent times is within 1% of self.dt.

seedint, default None

Random seed to use for the simulation. If None, a random seed is generated.

filter_at_time_intervals(time_intervals, check_gtis=True)[source]

Filter the event list at the given time intervals.

Parameters:
time_intervals2-d float array

List of time intervals of the form [[time0_0, time0_1], [time1_0, time1_1], ...]

Returns:
output_fileslist of str

A list of the output file names.

classmethod from_astropy_timeseries(ts: TimeSeries) → StingrayTimeseries[source]

Create a StingrayTimeseries from data in an Astropy TimeSeries

The timeseries has to define at least a column called time, the rest of columns will form the array attributes of the new time series, while the attributes in table.meta will form the new meta attributes of the time series.

Parameters:
tsastropy.timeseries.TimeSeries

A TimeSeries object with the array attributes as columns, and the meta attributes in the meta dictionary

Returns:
tsStingrayTimeseries

Timeseries object

join(*args, **kwargs)[source]

Join other StingrayTimeseries objects with the current one.

If both are empty, an empty StingrayTimeseries is returned.

Standard attributes such as pi and energy remain None if they are None in both. Otherwise, np.nan is used as a default value for the missing values. Arbitrary array attributes are created and joined using the same convention.

Multiple checks are done on the joined time series. If the time array of the series being joined is empty, it is ignored. If the time resolution is different, the final time series will have the rougher time resolution. If the MJDREF is different, the time reference will be changed to the one of the first time series. An empty time series will be ignored.

Note: join is not equivalent to concatenate. concatenate is used to join multiple non-overlapping time series along the time axis, while join is more general, and can be used to join multiple time series with different strategies (see parameter strategy below).

Parameters:
otherStingrayTimeseries or class:list of StingrayTimeseries

The other StingrayTimeseries object which is supposed to be joined with. If other is a list, it is assumed to be a list of StingrayTimeseries and they are all joined, one by one.

Returns:
`ts_new`StingrayTimeseries object

The resulting StingrayTimeseries object.

Other Parameters:
strategy{“intersection”, “union”, “append”, “infer”, “none”}

Method to use to merge the GTIs. If “intersection”, the GTIs are merged using the intersection of the GTIs. If “union”, the GTIs are merged using the union of the GTIs. If “none”, a single GTI with the minimum and the maximum time stamps of all GTIs is returned. If “infer”, the strategy is decided based on the GTIs. If there are no overlaps, “union” is used, otherwise “intersection” is used. If “append”, the GTIs are simply appended but they must be mutually exclusive.

plot(attr, witherrors=False, labels=None, ax=None, title=None, marker='-', save=False, filename=None, plot_btis=True, axis_limits=None)[source]

Plot the time series using matplotlib.

Plot the time series object on a graph self.time on x-axis and self.counts on y-axis with self.counts_err optionally as error bars.

Parameters:
attr: str

Attribute to plot.

Other Parameters:
witherrors: boolean, default False

Whether to plot the StingrayTimeseries with errorbars or not

labelsiterable, default None

A list or tuple with xlabel and ylabel as strings. E.g. if the attribute is 'counts', the list of labels could be ['Time (s)', 'Counts (s^-1)']

axmatplotlib.pyplot.axis object

Axis to be used for plotting. Defaults to creating a new one.

axis_limitslist, tuple, string, default None

Parameter to set axis properties of the matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for the``matplotlib.pyplot.axis()`` method.

titlestr, default None

The title of the plot.

markerstr, default ‘-’

Line style and color of the plot. Line styles and colors are combined in a single format string, as in 'bo' for blue circles. See matplotlib.pyplot.plot for more options.

saveboolean, optional, default False

If True, save the figure with specified filename.

filenamestr

File name of the image to save. Depends on the boolean save.

plot_btisbool

Plot the bad time intervals as red areas on the plot

rebin(dt_new=None, f=None, method='sum')[source]

Rebin the time series to a new time resolution. While the new resolution need not be an integer multiple of the previous time resolution, be aware that if it is not, the last bin will be cut off by the fraction left over by the integer division.

Parameters:
dt_new: float

The new time resolution of the time series. Must be larger than the time resolution of the old time series!

method: {``sum`` | ``mean`` | ``average``}, optional, default ``sum``

This keyword argument sets whether the counts in the new bins should be summed or averaged.

Returns:
ts_new: StingrayTimeseries object

The StingrayTimeseries object with the new, binned time series.

Other Parameters:
f: float

the rebin factor. If specified, it substitutes dt_new with f*self.dt

shift(time_shift: float, inplace=False) → StingrayTimeseries[source]

Shift the time, GTIs and tstart by the same amount

Parameters:
time_shift: float

The time interval by which the time series will be shifted (in the same units as the time array in StingrayTimeseries

Returns:
tsStingrayTimeseries object

The new time series shifted by time_shift

Other Parameters:
inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

sort(reverse=False, inplace=False)[source]

Sort a StingrayTimeseries object by time.

A StingrayTimeseries can be sorted in either increasing or decreasing order using this method. The time array gets sorted and the counts array is changed accordingly.

Parameters:
reverseboolean, default False

If True then the object is sorted in reverse order.

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

Returns:
ts_new: StingrayTimeseries object

The StingrayTimeseries object with sorted time and counts arrays.

Examples

>>> time = [2, 1, 3]
>>> count = [200, 100, 300]
>>> ts = StingrayTimeseries(time, array_attrs={"counts": count}, dt=1, skip_checks=True)
>>> ts_new = ts.sort()
>>> ts_new.time
array([1, 2, 3])
>>> assert np.allclose(ts_new.counts, [100, 200, 300])
split_by_gti(gti=None, min_points=2)[source]

Split the current StingrayTimeseries object into a list of StingrayTimeseries objects, one for each continuous GTI segment as defined in the gti attribute.

Parameters:
min_pointsint, default 1

The minimum number of data points in each time series. Light curves with fewer data points will be ignored.

Returns:
list_of_tsslist

A list of StingrayTimeseries objects, one for each GTI segment

to_astropy_timeseries() → TimeSeries[source]

Save the StingrayTimeseries to an Astropy timeseries.

Array attributes (time, pi, energy, etc.) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Returns:
tsastropy.timeseries.TimeSeries

A TimeSeries object with the array attributes as columns, and the meta attributes in the meta dictionary

truncate(start=0, stop=None, method='index')[source]

Truncate a StingrayTimeseries object.

This method takes a start and a stop point (either as indices, or as times in the same unit as those in the time attribute, and truncates all bins before start and after stop, then returns a new StingrayTimeseries object with the truncated time series.

Parameters:
startint, default 0

Index (or time stamp) of the starting point of the truncation. If no value is set for the start point, then all points from the first element in the time array are taken into account.

stopint, default None

Index (or time stamp) of the ending point (exclusive) of the truncation. If no value of stop is set, then points including the last point in the counts array are taken in count.

method{index | time}, optional, default index

Type of the start and stop values. If set to index then the values are treated as indices of the counts array, or if set to time, the values are treated as actual time values.

Returns:
ts_new: StingrayTimeseries object

The StingrayTimeseries object with truncated time and arrays.

Examples

>>> time = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> count = [10, 20, 30, 40, 50, 60, 70, 80, 90]
>>> ts = StingrayTimeseries(time, array_attrs={"counts": count}, dt=1)
>>> ts_new = ts.truncate(start=2, stop=8)
>>> assert np.allclose(ts_new.counts, [30, 40, 50, 60, 70, 80])
>>> assert np.allclose(ts_new.time, [3, 4, 5, 6, 7, 8])
>>> # Truncation can also be done by time values
>>> ts_new = ts.truncate(start=6, method='time')
>>> assert np.allclose(ts_new.time, [6, 7, 8, 9])
>>> assert np.allclose(ts_new.counts, [60, 70, 80, 90])

Lightcurve

class stingray.Lightcurve(time=None, counts=None, err=None, input_counts=True, gti=None, err_dist=None, bg_counts=None, bg_ratio=None, frac_exp=None, mjdref=0, dt=None, skip_checks=False, low_memory=False, mission=None, instr=None, header=None, **other_kw)[source]

Make a light curve object from an array of time stamps and an array of counts.

Parameters:
time: Iterable, :class:`~astropy.time.Time`, or :class:`~astropy.units.Quantity` object

A list or array of time stamps for a light curve. Must be a type that can be cast to np.array or list of floats, or that has a value attribute that does (e.g., a Quantity or Time object).

counts: iterable, optional, default ``None``

A list or array of the counts in each bin corresponding to the bins defined in time (note: use input_counts=False to input the count range, i.e. counts/second, otherwise use counts/bin).

err: iterable, optional, default ``None``

A list or array of the uncertainties in each bin corresponding to the bins defined in time (note: use input_counts=False to input the count range, i.e. counts/second, otherwise, use counts/bin). If None, we assume the data is Poisson distributed and calculate the error from the average of the lower and upper 1-sigma confidence intervals for the Poissonian distribution with mean equal to counts.

input_counts: bool, optional, default True

If True, the code assumes that the input data in counts is in units of counts/bin. If False, it assumes the data in counts is in counts/second.

gti: 2-d float array, default ``None``

[[gti0_0, gti0_1], [gti1_0, gti1_1], ...] Good Time Intervals. They are not applied to the data by default. They will be used by other methods to have an indication of the “safe” time intervals to use during analysis.

err_dist: str, optional, default ``None``

Statistical distribution used to calculate the uncertainties and other statistical values appropriately. Default makes no assumptions and keeps errors equal to zero.

bg_counts: iterable,``numpy.array`` or ``list`` of floats, optional, default ``None``

A list or array of background counts detected in the background extraction region in each bin corresponding to the bins defined in time.

bg_ratio: iterable, ``numpy.array`` or ``list`` of floats, optional, default ``None``

A list or array of source region area to background region area ratio in each bin. These are factors by which the bg_counts should be scaled to estimate background counts within the source aperture.

frac_exp: iterable, ``numpy.array`` or ``list`` of floats, optional, default ``None``

A list or array of fractional exposers in each bin.

mjdref: float

MJD reference (useful in most high-energy mission data)

dt: float or array of floats. Default median(diff(time))

Time resolution of the light curve. Can be an array of the same dimension as time specifying the width of each bin.

skip_checks: bool

If True, the user specifies that the data are already sorted and contain no infinite or nan points. Use at your own risk

low_memory: bool

If True, all the lazily evaluated attributes (e.g., countrate and countrate_err if input_counts is True) will _not_ be stored in memory, but calculated every time they are requested.

missionstr

Mission that recorded the data (e.g., NICER)

instrstr

Instrument onboard the mission

headerstr

The full header of the original FITS file, if relevant

**other_kw

Used internally. Any other keyword arguments will be ignored

Attributes:
time: numpy.ndarray

The array of midpoints of time bins.

bin_lo: numpy.ndarray

The array of lower time stamps of time bins.

bin_hi: numpy.ndarray

The array of higher time stamps of time bins.

counts: numpy.ndarray

The counts per bin corresponding to the bins in time.

counts_err: numpy.ndarray

The uncertainties corresponding to counts

bg_counts: numpy.ndarray

The background counts corresponding to the bins in time.

bg_ratio: numpy.ndarray

The ratio of source region area to background region area corresponding to each bin.

frac_exp: numpy.ndarray

The fractional exposers in each bin.

countrate: numpy.ndarray

The counts per second in each of the bins defined in time.

countrate_err: numpy.ndarray

The uncertainties corresponding to countrate

meanrate: float

The mean count rate of the light curve.

meancounts: float

The mean counts of the light curve.

n: int

The number of data points in the light curve.

dt: float or array of floats

The time resolution of the light curve.

mjdref: float

MJD reference date (tstart / 86400 gives the date in MJD at the start of the observation)

tseg: float

The total duration of the light curve.

tstart: float

The start time of the light curve.

gti: 2-d float array

[[gti0_0, gti0_1], [gti1_0, gti1_1], ...] Good Time Intervals. They indicate the “safe” time intervals to be used during the analysis of the light curve.

err_dist: string

Statistics of the Lightcurve, it is used to calculate the uncertainties and other statistical values appropriately. It propagates to Spectrum classes.

missionstr

Mission that recorded the data (e.g., NICER)

instrstr

Instrument onboard the mission

detector_iditerable

The detector that recoded each photon, if relevant (e.g., XMM, Chandra)

headerstr

The full header of the original FITS file, if relevant

analyze_lc_chunks(segment_size, func, fraction_step=1, **kwargs)[source]

Analyze segments of the light curve with any function.

Deprecated since version 2.0: Use analyze_segments() instead.

Parameters:
segment_sizefloat

Length in seconds of the light curve segments

funcfunction

Function accepting a Lightcurve object as single argument, plus possible additional keyword arguments, and returning a number or a tuple - e.g., (result, error) where both result and error are numbers.

Returns:
start_timesarray

Lower time boundaries of all time segments.

stop_timesarray

upper time boundaries of all segments.

resultarray of N elements

The result of func for each segment of the light curve

Other Parameters:
fraction_stepfloat

By default, segments do not overlap (fraction_step = 1). If fraction_step < 1, then the start points of consecutive segments are fraction_step * segment_size apart, and consecutive segments overlap. For example, for fraction_step = 0.5, the window shifts one half of segment_size)

kwargskeyword arguments

These additional keyword arguments, if present, they will be passed to func

apply_gtis(inplace=True)[source]

Apply GTIs to a light curve. Filters the time, counts, countrate, counts_err and countrate_err arrays for all bins that fall into Good Time Intervals and recalculates mean countrate and the number of bins.

Parameters:
inplacebool

If True, overwrite the current light curve. Otherwise, return a new one.

baseline(lam, p, niter=10, offset_correction=False)[source]

Calculate the baseline of the light curve, accounting for GTIs.

Parameters:
lamfloat

“smoothness” parameter. Larger values make the baseline stiffer Typically 1e2 < lam < 1e9

pfloat

“asymmetry” parameter. Smaller values make the baseline more “horizontal”. Typically 0.001 < p < 0.1, but not necessary.

Returns:
baselinenumpy.ndarray

An array with the baseline of the light curve

Other Parameters:
offset_correctionbool, default False

by default, this method does not align to the running mean of the light curve, but it goes below the light curve. Setting align to True, an additional step is done to shift the baseline so that it is shifted to the middle of the light curve noise distribution.

bexvar()[source]

Finds posterior samples of Bayesian excess variance (bexvar) for the light curve. It requires source counts in counts and time intervals for each bin. If the dt is an array then uses its elements as time intervals for each bin. If dt is float, it calculates the time intervals by assuming all intervals to be equal to dt.

Returns:
lc_bexvariterable, numpy.array of floats

An array of posterior samples of Bayesian excess variance (bexvar).

check_lightcurve()[source]

Make various checks on the lightcurve.

It can be slow, use it if you are not sure about your input data.

estimate_chunk_length(*args, **kwargs)[source]

Deprecated alias of estimate_segment_size.

estimate_segment_size(min_counts=100, min_samples=100, even_sampling=None)[source]

Estimate a reasonable segment length for segment-by-segment analysis.

The user has to specify a criterion based on a minimum number of counts (if the time series has a counts attribute) or a minimum number of time samples. At least one between min_counts and min_samples must be specified.

Returns:
segment_sizefloat

The length of the light curve chunks that satisfies the conditions

Other Parameters:
min_countsint

Minimum number of counts for each chunk. Optional (but needs min_samples if left unspecified). Only makes sense if the series has a counts attribute and it is evenly sampled.

min_samplesint

Minimum number of time bins. Optional (but needs min_counts if left unspecified).

even_samplingbool

Force the treatment of the data as evenly sampled or not. If None, the data are considered evenly sampled if self.dt is larger than zero and the median separation between subsequent times is within 1% of self.dt.

Examples

>>> import numpy as np
>>> time = np.arange(150)
>>> count = np.zeros_like(time) + 3
>>> lc = Lightcurve(time, count, dt=1)
>>> assert np.isclose(
...     lc.estimate_segment_size(min_counts=10, min_samples=3), 4)
>>> assert np.isclose(lc.estimate_segment_size(min_counts=10, min_samples=5), 5)
>>> count[2:4] = 1
>>> lc = Lightcurve(time, count, dt=1)
>>> assert np.isclose(lc.estimate_segment_size(min_counts=3, min_samples=1), 3)
>>> # A slightly more complex example
>>> dt=0.2
>>> time = np.arange(0, 1000, dt)
>>> counts = np.random.poisson(100, size=len(time))
>>> lc = Lightcurve(time, counts, dt=dt)
>>> assert np.isclose(lc.estimate_segment_size(100, 2), 0.4)
>>> min_total_bins = 40
>>> assert np.isclose(lc.estimate_segment_size(100, 40), 8.0)
static from_astropy_table(ts, **kwargs)[source]

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

static from_astropy_timeseries(ts, **kwargs)[source]

Create a StingrayTimeseries from data in an Astropy TimeSeries

The timeseries has to define at least a column called time, the rest of columns will form the array attributes of the new time series, while the attributes in table.meta will form the new meta attributes of the time series.

Parameters:
tsastropy.timeseries.TimeSeries

A TimeSeries object with the array attributes as columns, and the meta attributes in the meta dictionary

Returns:
tsStingrayTimeseries

Timeseries object

static from_lightkurve(lk, skip_checks=True)[source]

Creates a new Lightcurve from a lightkurve.LightCurve.

Parameters:
lklightkurve.LightCurve

A lightkurve LightCurve object

skip_checks: bool

If True, the user specifies that data are already sorted and contain no infinite or nan points. Use at your own risk.

join(other, skip_checks=False)[source]

Join two lightcurves into a single object.

The new Lightcurve object will contain time stamps from both the objects. The counts and countrate attributes in the resulting object will contain the union of the non-overlapping parts of the two individual objects, or the average in case of overlapping time arrays of both Lightcurve objects.

Good Time Intervals are also joined.

Note : Ideally, the time array of both lightcurves should not overlap.

Parameters:
otherLightcurve object

The other Lightcurve object which is supposed to be joined with.

skip_checks: bool

If True, the user specifies that data are already sorted and contain no infinite or nan points. Use at your own risk.

Returns:
lc_newLightcurve object

The resulting Lightcurve object.

Examples

>>> time1 = [5, 10, 15]
>>> count1 = [300, 100, 400]
>>> time2 = [20, 25, 30]
>>> count2 = [600, 1200, 800]
>>> lc1 = Lightcurve(time1, count1, dt=5)
>>> lc2 = Lightcurve(time2, count2, dt=5)
>>> lc = lc1.join(lc2)
>>> lc.time
array([ 5, 10, 15, 20, 25, 30])
>>> assert np.allclose(lc.counts, [ 300,  100,  400,  600, 1200,  800])
static make_lightcurve(toa, dt, tseg=None, tstart=None, gti=None, mjdref=0, use_hist=False)[source]

Make a light curve out of photon arrival times, with a given time resolution dt. Note that dt should be larger than the native time resolution of the instrument that has taken the data, and possibly a multiple!

Parameters:
toa: iterable

list of photon arrival times

dt: float

time resolution of the light curve (the bin width)

tseg: float, optional, default ``None``

The total duration of the light curve. If this is None, then the total duration of the light curve will be the interval between the arrival between either the first and the last gti boundary or, if gti is not set, the first and the last photon in toa.

Note: If tseg is not divisible by dt (i.e. if tseg/dt is not an integer number), then the last fractional bin will be dropped!

tstart: float, optional, default ``None``

The start time of the light curve. If this is None, either the first gti boundary or, if not available, the arrival time of the first photon will be used as the start time of the light curve.

gti: 2-d float array

[[gti0_0, gti0_1], [gti1_0, gti1_1], ...] Good Time Intervals

use_histbool

Use np.histogram instead of np.bincounts. Might be advantageous for very short datasets.

Returns:
lc: Lightcurve object

A Lightcurve object with the binned light curve

plot(witherrors=False, labels=None, ax=None, title=None, marker='-', save=False, filename=None, axis_limits=None, axis=None, plot_btis=True, attr=None)[source]

Plot the light curve using matplotlib.

Plot the light curve object on a graph self.time on x-axis and self.counts on y-axis with self.counts_err optionally as error bars.

Parameters:
witherrors: boolean, default False

Whether to plot the Lightcurve with errorbars or not

labelsiterable, default None

A list of tuple with xlabel and ylabel as strings.

axis_limitslist, tuple, string, default None

Parameter to set axis properties of the matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for the``matplotlib.pyplot.axis()`` method.

axislist, tuple, string, default None

Deprecated in favor of axis_limits, same functionality.

titlestr, default None

The title of the plot.

markerstr, default ‘-’

Line style and color of the plot. Line styles and colors are combined in a single format string, as in 'bo' for blue circles. See matplotlib.pyplot.plot for more options.

saveboolean, optional, default False

If True, save the figure with specified filename.

filenamestr

File name of the image to save. Depends on the boolean save.

axmatplotlib.pyplot.axis object

Axis to be used for plotting. Defaults to creating a new one.

plot_btisbool

Plot the bad time intervals as red areas on the plot

attrstr

The attribute to plot on the y-axis. By default, it is counts if input_counts is True, and countrate otherwise. It can be set to any other array attribute of the light curve by passing its name as a string.

classmethod read(filename, fmt=None, format_=None, err_dist='gauss', skip_checks=False, **fits_kwargs)[source]

Read a Lightcurve object from file.

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • hea : FITS Light curves from HEASARC-supported missions.

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a time column and a counts or countrate column. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all lightcurve attributes such as dt, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
lcLightcurve object
Other Parameters:
err_dist: str, default=’gauss’

Default error distribution if not specified in the file (e.g., for ASCII files). The default is ‘gauss’ just because it is likely that people using ASCII light curves will want to specify Gaussian error bars, if any.

skip_checksbool

See Lightcurve documentation

**fits_kwargsadditional keyword arguments

Any other arguments to be passed to lcurve_from_fits() (only relevant for hea/ogip formats)

rebin(dt_new=None, f=None, method='sum')[source]

Rebin the light curve to a new time resolution. While the new resolution need not be an integer multiple of the previous time resolution, be aware that if it is not, the last bin will be cut off by the fraction left over by the integer division.

Parameters:
dt_new: float

The new time resolution of the light curve. Must be larger than the time resolution of the old light curve!

method: {``sum`` | ``mean`` | ``average``}, optional, default ``sum``

This keyword argument sets whether the counts in the new bins should be summed or averaged.

Returns:
lc_new: Lightcurve object

The Lightcurve object with the new, binned light curve.

Other Parameters:
f: float

the rebin factor. If specified, it substitutes dt_new with f*self.dt

sort(reverse=False, inplace=False)[source]

Sort a Lightcurve object by time.

A Lightcurve can be sorted in either increasing or decreasing order using this method. The time array gets sorted and the counts array is changed accordingly.

Parameters:
reverseboolean, default False

If True then the object is sorted in reverse order.

inplacebool

If True, overwrite the current light curve. Otherwise, return a new one.

Returns:
lc_new: Lightcurve object

The Lightcurve object with sorted time and counts arrays.

Examples

>>> time = [2, 1, 3]
>>> count = [200, 100, 300]
>>> lc = Lightcurve(time, count, dt=1, skip_checks=True)
>>> lc_new = lc.sort()
>>> lc_new.time
array([1, 2, 3])
>>> assert np.allclose(lc_new.counts, [100, 200, 300])
sort_counts(reverse=False, inplace=False)[source]

Sort a Lightcurve object in accordance with its counts array.

A Lightcurve can be sorted in either increasing or decreasing order using this method. The counts array gets sorted and the time array is changed accordingly.

Parameters:
reverseboolean, default False

If True then the object is sorted in reverse order.

inplacebool

If True, overwrite the current light curve. Otherwise, return a new one.

Returns:
lc_new: Lightcurve object

The Lightcurve object with sorted time and counts arrays.

Examples

>>> time = [1, 2, 3]
>>> count = [200, 100, 300]
>>> lc = Lightcurve(time, count, dt=1, skip_checks=True)
>>> lc_new = lc.sort_counts()
>>> lc_new.time
array([2, 1, 3])
>>> assert np.allclose(lc_new.counts, [100, 200, 300])
split(min_gap, min_points=1)[source]

For data with gaps, it can sometimes be useful to be able to split the light curve into separate, evenly sampled objects along those data gaps. This method allows to do this: it finds data gaps of a specified minimum size, and produces a list of new Lightcurve objects for each contiguous segment.

Parameters:
min_gapfloat

The length of a data gap, in the same units as the time attribute of the Lightcurve object. Any smaller gaps will be ignored, any larger gaps will be identified and used to split the light curve.

min_pointsint, default 1

The minimum number of data points in each light curve. Light curves with fewer data points will be ignored.

Returns:
lc_splititerable of Lightcurve objects

The list of all contiguous light curves

Examples

>>> time = np.array([1, 2, 3, 6, 7, 8, 11, 12, 13])
>>> counts = np.random.rand(time.shape[0])
>>> lc = Lightcurve(time, counts, dt=1, skip_checks=True)
>>> split_lc = lc.split(1.5)
to_astropy_table(**kwargs)[source]

Save the light curve to an astropy.table.Table object.

The time array and all the array attributes become columns. The meta attributes become metadata of the astropy.table.Table object.

Other Parameters:
no_longdoublebool, default False

If True, the data are converted to double precision before being saved. This is useful, e.g., for saving to FITS files, which do not support long double precision.

to_astropy_timeseries(**kwargs)[source]

Save the light curve to an astropy.timeseries.TimeSeries object.

The time array and all the array attributes become columns. The meta attributes become metadata of the astropy.timeseries.TimeSeries object. The time array is saved as a TimeDelta object.

Other Parameters:
no_longdoublebool, default False

If True, the data are converted to double precision before being saved. This is useful, e.g., for saving to FITS files, which do not support long double precision.

to_lightkurve()[source]

Returns a lightkurve.LightCurve object. This feature requires Lightkurve to be installed (e.g., pip install lightkurve). An ImportError will be raised if this package is not available.

Returns:
lightcurvelightkurve.LightCurve

A lightkurve LightCurve object.

truncate(start=0, stop=None, method='index')[source]

Truncate a Lightcurve object.

This method takes a start and a stop point (either as indices, or as times in the same unit as those in the time attribute, and truncates all bins before start and after stop, then returns a new Lightcurve object with the truncated light curve.

Parameters:
startint, default 0

Index (or time stamp) of the starting point of the truncation. If no value is set for the start point, then all points from the first element in the time array are taken into account.

stopint, default None

Index (or time stamp) of the ending point (exclusive) of the truncation. If no value of stop is set, then points including the last point in the counts array are taken in count.

method{index | time}, optional, default index

Type of the start and stop values. If set to index then the values are treated as indices of the counts array, or if set to time, the values are treated as actual time values.

Returns:
lc_new: Lightcurve object

The Lightcurve object with truncated time and counts arrays.

Examples

>>> time = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> count = [10, 20, 30, 40, 50, 60, 70, 80, 90]
>>> lc = Lightcurve(time, count, dt=1)
>>> lc_new = lc.truncate(start=2, stop=8)
>>> assert np.allclose(lc_new.counts, [30, 40, 50, 60, 70, 80])
>>> lc_new.time
array([3, 4, 5, 6, 7, 8])
>>> # Truncation can also be done by time values
>>> lc_new = lc.truncate(start=6, method='time')
>>> lc_new.time
array([6, 7, 8, 9])
>>> assert np.allclose(lc_new.counts, [60, 70, 80, 90])

EventList

class stingray.events.EventList(time=None, energy=None, ncounts=None, mjdref=0, dt=0, notes='', gti=None, pi=None, high_precision=False, mission=None, instr=None, header=None, detector_id=None, ephem=None, timeref=None, timesys=None, rmf_file=None, skip_checks=False, **other_kw)[source]

Basic class for event list data. Event lists generally correspond to individual events (e.g. photons) recorded by the detector, and their associated properties. For X-ray data where this type commonly occurs, events are time stamps of when a photon arrived in the detector, and (optionally) the photon energy associated with the event.

Parameters:
time: iterable

A list or array of time stamps

Attributes:
time: numpy.ndarray

The array of event arrival times, in seconds from the reference MJD defined in mjdref

energy: numpy.ndarray

The array of photon energy values

ncounts: int

The number of data points in the event list

dt: float

The time resolution of the events. Only relevant when using events to produce light curves with similar bin time.

mjdreffloat

The MJD used as a reference for the time array.

gtis: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Good Time Intervals

piinteger, numpy.ndarray

PI channels

high_precisionbool

Change the precision of self.time to float128. Useful while dealing with fast pulsars.

missionstr

Mission that recorded the data (e.g. NICER)

instrstr

Instrument onboard the mission

detector_iditerable

The detector that recoded each photon, if relevant (e.g. XMM, Chandra)

headerstr

The full header of the original FITS file, if relevant

Other Parameters:
dt: float

The time resolution of the events. Only relevant when using events to produce light curves with similar bin time.

energy: iterable

A list of array of photon energy values in keV

mjdreffloat

The MJD used as a reference for the time array.

ncounts: int

Number of desired data points in event list. Deprecated

gtis: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Good Time Intervals

piinteger, numpy.ndarray

PI channels

notesstr

Any useful annotations

high_precisionbool

Change the precision of self.time to float128. Useful while dealing with fast pulsars.

missionstr

Mission that recorded the data (e.g. NICER)

instrstr

Instrument onboard the mission

headerstr

The full header of the original FITS file, if relevant

detector_iditerable

The detector that recorded each photon (if the instrument has more than one, e.g. XMM/EPIC-pn)

timerefstr

The time reference, as recorded in the FITS file (e.g. SOLARSYSTEM)

timesysstr

The time system, as recorded in the FITS file (e.g. TDB)

ephemstr

The JPL ephemeris used to barycenter the data, if any (e.g. DE430)

rmf_filestr, default None

The file name of the RMF file to use for calibration.

skip_checksbool, default False

Skip checks for the validity of the event list. Use with caution.

**other_kw

Used internally. Any other keyword arguments will be ignored

apply_deadtime(deadtime, inplace=False, **kwargs)[source]

Apply deadtime filter to this event list.

Additional arguments in kwargs are passed to get_deadtime_mask()

Parameters:
deadtimefloat

Value of dead time to apply to data

inplacebool, default False

If True, apply the deadtime to the current event list. Otherwise, return a new event list.

Returns:
new_event_listEventList object

Filtered event list. if inplace is True, this is the input object filtered for deadtime, otherwise this is a new object.

additional_outputobject

Only returned if return_all is True. See get_deadtime_mask() for more details.

Examples

>>> events = np.array([1, 1.05, 1.07, 1.08, 1.1, 2, 2.2, 3, 3.1, 3.2])
>>> events = EventList(events, gti=[[0, 3.3]])
>>> events.pi=np.array([1, 2, 2, 2, 2, 1, 1, 1, 2, 1])
>>> events.energy=np.array([1, 2, 2, 2, 2, 1, 1, 1, 2, 1])
>>> events.mjdref = 10
>>> filt_events, retval = events.apply_deadtime(0.11, inplace=False,
...                                             verbose=False,
...                                             return_all=True)
>>> assert filt_events is not events
>>> expected = np.array([1, 2, 2.2, 3, 3.2])
>>> assert np.allclose(filt_events.time, expected)
>>> assert np.allclose(filt_events.pi, 1)
>>> assert np.allclose(filt_events.energy, 1)
>>> assert not np.allclose(events.pi, 1)
>>> filt_events = events.apply_deadtime(0.11, inplace=True,
...                                     verbose=False)
>>> assert filt_events is events
convert_pi_to_energy(rmf_file)[source]

Calibrate the energy column of the event list.

Defines the energy attribute of the event list by converting the PI channels to energy using the provided RMF file.

Parameters:
rmf_filestr

The file name of the RMF file to use for calibration.

filter_detector_id(detector_id, inplace=False)[source]

Filters the data selecting only a selected detector or a list of detectors.

Parameters:
detector_idint or list of int

The detector id, or list of detector ids, to select.

Other Parameters:
inplacebool, default False

Do the change in place (modify current event list). Otherwise, copy to a new event list.

filter_energy_range(energy_range, inplace=False, use_pi=False)[source]

Filter the event list from a given energy range.

Parameters:
energy_range: [float, float]

Energy range in keV, or in PI channel (if use_pi is True)

Other Parameters:
inplacebool, default False

Do the change in place (modify current event list). Otherwise, copy to a new event list.

use_pibool, default False

Use PI channel instead of energy in keV

Examples

>>> events = EventList(time=[0, 1, 2], energy=[0.3, 0.5, 2], pi=[3, 5, 20])
>>> e1 = events.filter_energy_range([0, 1])
>>> assert np.allclose(e1.time, [0, 1])
>>> assert np.allclose(events.time, [0, 1, 2])
>>> e2 = events.filter_energy_range([0, 10], use_pi=True, inplace=True)
>>> assert np.allclose(e2.time, [0, 1])
>>> assert np.allclose(events.time, [0, 1])
static from_lc(lc)[source]

Create an EventList from a stingray.Lightcurve object. Note that all events in a given time bin will have the same time stamp.

Bins with negative counts will be ignored.

Parameters:
lc: :class:`stingray.Lightcurve` object

Light curve to use for creation of the event list.

Returns:
ev: EventList object

The resulting list of photon arrival times generated from the light curve.

get_color_evolution(energy_ranges, segment_size=None, use_pi=False)[source]

Compute the color in equal-length segments of the event list.

Parameters:
energy_ranges2x2 list

List of energy ranges to compute the color: [[en1_min, en1_max], [en2_min, en2_max]]

segment_sizefloat

Segment size in seconds. If None, the full GTIs are considered instead as segments.

Returns:
colorarray-like

Array of colors, computed in each segment as the ratio of the counts in the second energy range to the counts in the first energy range.

Other Parameters:
use_pibool, default False

Use PI channel instead of energy in keV

get_energy_mask(energy_range, use_pi=False)[source]

Get a mask corresponding to events with a given energy range.

Parameters:
energy_range: [float, float]

Energy range in keV, or in PI channel (if use_pi is True)

Other Parameters:
use_pibool, default False

Use PI channel instead of energy in keV

get_intensity_evolution(energy_range, segment_size=None, use_pi=False)[source]

Compute the intensity in equal-length segments (or full GTIs) of the event list.

Parameters:
energy_range[en1_min, en1_max]

Energy range to compute the intensity

segment_sizefloat

Segment size in seconds. If None, the full GTIs are considered instead as segments.

Returns:
intensityarray-like

Array of intensities (in counts/s), computed in each segment.

Other Parameters:
use_pibool, default False

Use PI channel instead of energy in keV

get_mask_for_filter_by_detector_id(value)[source]

Supplies a mask to filter the data for a particular detector(s) for a event file. For example can be used to filter the data for a single unit of LAXPC or a selected NICER detector ID

Parameters:

value – The value for which the mask returns 1

join(other, strategy='infer')[source]

Join two EventList objects into one.

If both are empty, an empty EventList is returned.

GTIs are crossed if the event lists are over a common time interval, and appended otherwise.

Standard attributes such as pi and energy remain None if they are None in both. Otherwise, np.nan is used as a default value for the EventList where they were None. Arbitrary attributes (e.g., Stokes parameters in polarimetric data) are created and joined using the same convention.

Multiple checks are done on the joined event lists. If the time array of the event list being joined is empty, it is ignored. If the time resolution is different, the final event list will have the rougher time resolution. If the MJDREF is different, the time reference will be changed to the one of the first event list. An empty event list will be ignored.

Parameters:
otherEventList object or class:list of EventList objects

The other EventList object which is supposed to be joined with. If other is a list, it is assumed to be a list of EventList objects and they are all joined, one by one.

Returns:
`ev_new`EventList object

The resulting EventList object.

Other Parameters:
strategy{“intersection”, “union”, “append”, “infer”, “none”}

Method to use to merge the GTIs. If “intersection”, the GTIs are merged using the intersection of the GTIs. If “union”, the GTIs are merged using the union of the GTIs. If “none”, a single GTI with the minimum and the maximum time stamps of all GTIs is returned. If “infer”, the strategy is decided based on the GTIs. If there are no overlaps, “union” is used, otherwise “intersection” is used. If “append”, the GTIs are simply appended but they must be mutually exclusive.

property ncounts

Number of events in the event list.

classmethod read(filename, fmt=None, rmf_file=None, **kwargs)[source]

Read a EventList object from file.

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • hea or ogip : FITS Event files from (well, some) HEASARC-supported missions.

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a time column. Other recognized columns are energy and pi. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all eventlist attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
ev: EventList object

The EventList object reconstructed from file

Other Parameters:
rmf_filestr, default None

The file name of the RMF file to use for energy calibration. Defaults to None, which implies no channel->energy conversion at this stage (or a default calibration applied to selected missions).

kwargsdict

Any further keyword arguments to be passed to FITSTimeseriesReader for reading in event lists in OGIP/HEASOFT format

simulate_energies(spectrum, use_spline=False)[source]

Assign (simulate) energies to event list from a spectrum.

Parameters:
spectrum: 2-d array or list [energies, spectrum]

Energies versus corresponding fluxes. The 2-d array or list must have energies across the first dimension and fluxes across the second one. If the dimension of the energies is the same as spectrum, they are interpreted as bin centers. If it is longer by one, they are interpreted as proper bin edges (similarly to the bins of np.histogram). Note that for non-uniformly binned spectra, it is advisable to pass the exact edges.

simulate_times(lc, use_spline=False, bin_time=None)[source]

Simulate times from an input light curve.

Randomly simulate photon arrival times to an EventList from a stingray.Lightcurve object, using the inverse CDF method.

..note::

Preferably use model light curves containing no Poisson noise, as this method will intrinsically add Poisson noise to them.

Parameters:
lc: :class:`stingray.Lightcurve` object
Returns:
timesarray-like

Simulated photon arrival times

Other Parameters:
use_splinebool

Approximate the light curve with a spline to avoid binning effects

bin_timefloat default None

Ignored and deprecated, maintained for backwards compatibility.

sort(inplace=False)[source]

Sort the event list in time.

Returns:
eventlistEventList

The sorted event list. If inplace=True, it will be a shallow copy of self.

Other Parameters:
inplacebool, default False

Sort in place. If False, return a new event list.

Examples

>>> events = EventList(time=[0, 2, 1], energy=[0.3, 2, 0.5], pi=[3, 20, 5],
...                    skip_checks=True)
>>> e1 = events.sort()
>>> assert np.allclose(e1.time, [0, 1, 2])
>>> assert np.allclose(e1.energy, [0.3, 0.5, 2])
>>> assert np.allclose(e1.pi, [3, 5, 20])

But the original event list has not been altered (inplace=False by default): >>> assert np.allclose(events.time, [0, 2, 1])

Let’s do it in place instead >>> e2 = events.sort(inplace=True) >>> assert np.allclose(e2.time, [0, 1, 2])

In this case, the original event list has been altered. >>> assert np.allclose(events.time, [0, 1, 2])

suggest_compatible_dt(dt, warn=True)[source]

Suggest a compatible time resolution for the event list.

If the event list has a time resolution, it is better to avoid using a time bin smaller than that for sure, but also a time bin which is not a multiple of it, as it will create beats that appear in the power spectrum as a comb of peaks. This method suggests a compatible time resolution.

Parameters:
dtfloat

Desired time resolution

Other Parameters:
warnbool, default True

Issue a warning if the time resolution is changed

to_binned_timeseries(dt, array_attrs=None)[source]

Convert the event list to a binned stingray.StingrayTimeseries object.

The result will be something similar to a light curve, but with arbitrary attributes corresponding to a weighted sum of each specified attribute of the event list.

E.g. if the event list has a q attribute, the final time series will have a q attribute, which is the sum of all q values in each time bin.

Parameters:
dt: float

Binning time of the light curve

Returns:
lc: stingray.Lightcurve object
Other Parameters:
array_attrs: list of str

List of attributes to be converted to light curve arrays. If None, all array attributes will be converted.

to_lc(dt, tstart=None, tseg=None)[source]

Convert event list to a stingray.Lightcurve object.

Parameters:
dt: float

Binning time of the light curve

Returns:
lc: stingray.Lightcurve object
Other Parameters:
tstartfloat

Start time of the light curve

tseg: float

Total duration of light curve

to_lc_iter(dt, segment_size=None)[source]

Convert event list to a generator of Lightcurves.

Parameters:
dt: float

Binning time of the light curves

Returns:
lc_gen: generator

Generates one stingray.Lightcurve object for each GTI or segment

Other Parameters:
segment_sizefloat, default None

Optional segment size. If None, use the GTI boundaries

to_lc_list(dt, segment_size=None)[source]

Convert event list to a list of Lightcurves.

Parameters:
dt: float

Binning time of the light curves

Returns:
lc_list: List

List containing one stingray.Lightcurve object for each GTI or segment

Other Parameters:
segment_sizefloat, default None

Optional segment size. If None, use the GTI boundaries


Event list functions

stingray.events.simple_events_from_lc(lc)[source]

Create an EventList from a stingray.Lightcurve object. Note that all events in a given time bin will have the same time stamp.

Bins with negative counts will be ignored.

Parameters:
lc: :class:`stingray.Lightcurve` object

Light curve to use for creation of the event list.

Returns:
ev: EventList object

The resulting list of photon arrival times generated from the light curve.

Examples

>>> from stingray import Lightcurve
>>> lc = Lightcurve([0, 1, 2], [2, 3, -1], dt=1)
>>> ev = simple_events_from_lc(lc)
>>> assert np.allclose(ev.time, [0, 0, 1, 1, 1])

Fourier Products

These classes implement commonly used Fourier analysis products, most importantly Crossspectrum and Powerspectrum, along with the variants for averaged cross/power spectra.

Crossspectrum

class stingray.Crossspectrum(data1=None, data2=None, norm='frac', gti=None, lc1=None, lc2=None, power_type='all', dt=None, fullspec=False, skip_checks=False, save_all=False, channels_overlap=False)[source]

Make a cross spectrum from a (binned) light curve. You can also make an empty Crossspectrum object to populate with your own Fourier-transformed data (this can sometimes be useful when making binned power spectra). Stingray uses the scipy.fft standards for the sign of the Nyquist frequency.

Parameters:
data1: :class:`stingray.Lightcurve` or :class:`stingray.events.EventList`, optional, default ``None``

The dataset for the first channel/band of interest.

data2: :class:`stingray.Lightcurve` or :class:`stingray.events.EventList`, optional, default ``None``

The dataset for the second, or “reference”, band.

norm: {``frac``, ``abs``, ``leahy``, ``none``}, default ``none``

The normalization of the (real part of the) cross spectrum.

power_type: string, optional, default ``real``

Parameter to choose among complete, real part and magnitude of the cross spectrum.

fullspec: boolean, optional, default ``False``

If False, keep only the positive frequencies, or if True, keep all of them .

Attributes:
freq: numpy.ndarray

The array of mid-bin frequencies that the Fourier transform samples

power: numpy.ndarray

The array of cross spectra (complex numbers)

power_err: numpy.ndarray

The uncertainties of power. An approximation for each bin given by power_err= power/sqrt(m). Where m is the number of power averaged in each bin (by frequency binning, or averaging more than one spectra). Note that for a single realization (m=1) the error is equal to the power.

df: float

The frequency resolution

m: int

The number of averaged cross-spectra amplitudes in each bin.

n: int

The number of data points/time bins in one segment of the light curves.

k: array of int

The rebinning scheme if the object has been rebinned otherwise is set to 1.

nphots1: float

The total number of photons in light curve 1

nphots2: float

The total number of photons in light curve 2

Other Parameters:
gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input Lightcurve GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the Lightcurve objects before making the cross spectrum.

lc1: :class:`stingray.Lightcurve`object OR iterable of :class:`stingray.Lightcurve` objects

For backwards compatibility only. Like data1, but no stingray.events.EventList objects allowed

lc2: :class:`stingray.Lightcurve`object OR iterable of :class:`stingray.Lightcurve` objects

For backwards compatibility only. Like data2, but no stingray.events.EventList objects allowed

dt: float

The time resolution of the light curve. Only needed when constructing light curves in the case where data1, data2 are EventList objects

skip_checks: bool

Skip initial checks, for speed or other reasons (you need to trust your inputs!)

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

classical_significances(threshold=1, trial_correction=False)[source]

Compute the classical significances for the powers in the power spectrum, assuming an underlying noise distribution that follows a chi-square distributions with 2M degrees of freedom, where M is the number of powers averaged in each bin.

Note that this function will only produce correct results when the following underlying assumptions are fulfilled:

  1. The power spectrum is Leahy-normalized

  2. There is no source of variability in the data other than the periodic signal to be determined with this method. This is important! If there are other sources of (aperiodic) variability in the data, this method will not produce correct results, but instead produce a large number of spurious false positive detections!

  3. There are no significant instrumental effects changing the statistical distribution of the powers (e.g. pile-up or dead time)

By default, the method produces (index,p-values) for all powers in the power spectrum, where index is the numerical index of the power in question. If a threshold is set, then only powers with p-values below that threshold with their respective indices. If trial_correction is set to True, then the threshold will be corrected for the number of trials (frequencies) in the power spectrum before being used.

Parameters:
thresholdfloat, optional, default 1

The threshold to be used when reporting p-values of potentially significant powers. Must be between 0 and 1. Default is 1 (all p-values will be reported).

trial_correctionbool, optional, default False

A Boolean flag that sets whether the threshold will be corrected by the number of frequencies before being applied. This decreases the threshold (p-values need to be lower to count as significant). Default is False (report all powers) though for any application where threshold is set to something meaningful, this should also be applied!

Returns:
pvalsiterable

A list of (index, p-value) tuples for all powers that have p-values lower than the threshold specified in threshold.

coherence()[source]

Compute Raw Coherence function of the cross spectrum.

Deprecated. Please use the raw_coherence() method of AveragedCrossspectrum, with the correct parameters.

Returns:
cohnumpy.ndarray

Coherence function

deadtime_correct(dead_time, rate, background_rate=0, limit_k=200, n_approx=None, paralyzable=False)[source]

Correct the power spectrum for dead time effects.

This correction is based on the formula given in Zhang et al. 2015, assuming a constant dead time for all events. For more advanced dead time corrections, see the FAD method from stingray.deadtime.fad

Parameters:
dead_time: float

The dead time of the detector.

ratefloat

Detected source count rate

Returns:
spectrum: Crossspectrum or derivative.

The dead-time corrected spectrum.

Other Parameters:
background_ratefloat, default 0

Detected background count rate. This is important to estimate when deadtime is given by the combination of the source counts and background counts (e.g. in an imaging X-ray detector).

paralyzable: bool, default False

If True, the dead time correction is done assuming a paralyzable dead time. If False, the correction is done assuming a non-paralyzable (more common) dead time.

limit_kint, default 200

Limit to this value the number of terms in the inner loops of calculations. Check the plots returned by the check_B() and check_A() functions to test that this number is adequate.

n_approxint, default None

Number of bins to calculate the model power spectrum. If None, it will use the size of the input frequency array. Relatively simple models (e.g., low count rates compared to dead time) can use a smaller number of bins to speed up the calculation, and the final power values will be interpolated.

static from_events(events1, events2, dt, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)[source]

Calculate AveragedCrossspectrum from two event lists

Parameters:
events1EventList

Events from channel 1

events2EventList

Events from channel 2

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency)

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

static from_lc_iterable(iter_lc1, iter_lc2, dt, segment_size, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)[source]

Calculate AveragedCrossspectrum from two light curves

Parameters:
iter_lc1iterable of stingray.Lightcurve objects or np.array

Light curves from channel 1. If arrays, use them as counts

iter_lc1iterable of stingray.Lightcurve objects or np.array

Light curves from channel 2. If arrays, use them as counts

dtfloat

The time resolution of the light curves (sets the Nyquist frequency)

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

save_allbool, default False

If True, save the cross spectrum of each segment in the cs_all attribute of the output Crossspectrum object.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

static from_lightcurve(lc1, lc2, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)[source]

Calculate AveragedCrossspectrum from two light curves

Parameters:
lc1stingray.Lightcurve

Light curve from channel 1

lc2stingray.Lightcurve

Light curve from channel 2

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

static from_stingray_timeseries(ts1, ts2, flux_attr, error_flux_attr=None, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)[source]

Calculate AveragedCrossspectrum from two light curves

Parameters:
ts1StingrayTimeseries

Time series from channel 1

ts2StingrayTimeseries

Time series from channel 2

flux_attrstr

What attribute of the time series will be used.

Other Parameters:
error_flux_attrstr

What attribute of the time series will be used as error bar.

segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

static from_time_array(times1, times2, dt, segment_size=None, gti=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, channels_overlap=False)[source]

Calculate AveragedCrossspectrum from two arrays of event times.

Parameters:
times1np.array

Event arrival times of channel 1

times2np.array

Event arrival times of channel 2

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency)

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum.

gti[[gti0, gti1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

initial_checks(data1=None, data2=None, norm='frac', gti=None, lc1=None, lc2=None, segment_size=None, power_type='real', dt=None, fullspec=False)[source]

Run initial checks on the input.

Returns True if checks are passed, False if they are not.

Raises various errors for different bad inputs

Examples

>>> times = np.arange(0, 10)
>>> counts = np.random.poisson(100, 10)
>>> lc1 = Lightcurve(times, counts, skip_checks=True)
>>> lc2 = Lightcurve(times, counts, skip_checks=True)
>>> ev1 = EventList(times)
>>> ev2 = EventList(times)
>>> c = Crossspectrum()
>>> ac = AveragedCrossspectrum()

If norm is not a string, raise a TypeError >>> Crossspectrum.initial_checks(c, norm=1) Traceback (most recent call last): … TypeError: norm must be a string…

If norm is not one of the valid norms, raise a ValueError >>> Crossspectrum.initial_checks(c, norm=”blabla”) Traceback (most recent call last): … ValueError: norm must be ‘frac’…

If power_type is not one of the valid norms, raise a ValueError >>> Crossspectrum.initial_checks(c, power_type=”blabla”) Traceback (most recent call last): … ValueError: power_type not recognized!

If the user passes only one light curve, raise a ValueError

>>> Crossspectrum.initial_checks(c, data1=lc1, data2=None)
Traceback (most recent call last):
...
ValueError: You can't do a cross spectrum...

If the user passes an event list without dt, raise a ValueError

>>> Crossspectrum.initial_checks(c, data1=ev1, data2=ev2, dt=None)
Traceback (most recent call last):
...
ValueError: If using event lists, please specify...
phase_lag()[source]

Calculate the fourier phase lag of the cross spectrum.

This is defined as the argument of the complex cross spectrum, and gives the delay at all frequencies, in cycles, of one input light curve with respect to the other.

plot(labels=None, axis=None, title=None, marker='-', save=False, filename=None, ax=None)[source]

Plot the amplitude of the cross spectrum vs. the frequency using matplotlib.

Parameters:
labelsiterable, default None

A list of tuple with xlabel and ylabel as strings.

axislist, tuple, string, default None

Parameter to set axis properties of the matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for the``matplotlib.pyplot.axis()`` method.

titlestr, default None

The title of the plot.

markerstr, default ‘-’

Line style and color of the plot. Line styles and colors are combined in a single format string, as in 'bo' for blue circles. See matplotlib.pyplot.plot for more options.

saveboolean, optional, default False

If True, save the figure with specified filename.

filenamestr

File name of the image to save. Depends on the boolean save.

axmatplotlib.Axes object

An axes object to fill with the cross correlation plot.

rebin(df=None, f=None, method='mean')[source]

Rebin the cross spectrum to a new frequency resolution df.

Parameters:
df: float

The new frequency resolution

Returns:
bin_cs = Crossspectrum (or one of its subclasses) object

The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from AveragedPowerspectrum, it will return an object of class AveragedPowerspectrum, too.

Other Parameters:
f: float

the rebin factor. If specified, it substitutes df with f*self.df

rebin_log(f=0.01)[source]

Logarithmic rebin of the periodogram. The new frequency depends on the previous frequency modified by a factor f:

\[d\nu_j = d\nu_{j-1} (1+f)\]
Parameters:
f: float, optional, default ``0.01``

parameter that steers the frequency resolution

Returns:
new_specCrossspectrum (or one of its subclasses) object

The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from AveragedPowerspectrum, it will return an object of class

save_as_xspec(outroot, header_keywords=None)[source]

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().

time_lag()[source]

Calculate the fourier time lag of the cross spectrum. The time lag is calculated by taking the phase lag \(\phi\) and

..math:

\tau = \frac{\phi}{\two pi \nu}

where \(\nu\) is the center of the frequency bins.

to_norm(norm, inplace=False)[source]

Convert Cross spectrum to new normalization.

Parameters:
normstr

The new normalization of the spectrum

Returns:
new_specobject, same class as input

The new, normalized, spectrum.

Other Parameters:
inplace: bool, default False

If True, change the current instance. Otherwise, return a new one


Coherence

Convenience function to compute the coherence between two stingray.Lightcurve objects.

stingray.coherence(lc1, lc2)[source]

Estimate coherence function of two light curves.

Note

This function is deprecated because it does not make sense in the current formulation. Please use the raw_coherence() and intrinsic_coherence() methods of AveragedCrossspectrum, with the correct parameters.

Parameters:
lc1: :class:`stingray.Lightcurve` object

The first light curve data for the channel of interest.

lc2: :class:`stingray.Lightcurve` object

The light curve data for reference band

Returns:
cohnp.ndarray

The array of coherence versus frequency


Powerspectrum

class stingray.Powerspectrum(data=None, norm='frac', gti=None, dt=None, lc=None, skip_checks=False)[source]

Make a Powerspectrum (also called periodogram) from a (binned) light curve. Periodograms can be normalized by either Leahy normalization, fractional rms normalization, absolute rms normalization, or not at all.

You can also make an empty Powerspectrum object to populate with your own fourier-transformed data (this can sometimes be useful when making binned power spectra).

Parameters:
data: :class:`stingray.Lightcurve` object, optional, default ``None``

The light curve data to be Fourier-transformed.

norm: {“leahy” | “frac” | “abs” | “none” }, optional, default “frac”

The normaliation of the power spectrum to be used. Options are “leahy”, “frac”, “abs” and “none”, default is “frac”.

Attributes:
norm: {“leahy” | “frac” | “abs” | “none” }

The normalization of the power spectrum.

freq: numpy.ndarray

The array of mid-bin frequencies that the Fourier transform samples.

power: numpy.ndarray

The array of normalized squared absolute values of Fourier amplitudes.

power_err: numpy.ndarray

The uncertainties of power. An approximation for each bin given by power_err= power/sqrt(m). Where m is the number of power averaged in each bin (by frequency binning, or averaging power spectra of segments of a light curve). Note that for a single realization (m=1) the error is equal to the power.

unnorm_power: numpy.ndarray

The array of unnormalized powers

unnorm_power_err: numpy.ndarray

The uncertainties of unnorm_power.

df: float

The frequency resolution.

m: int

The number of averaged powers in each bin.

n: int

The number of data points in the light curve.

nphots: float

The total number of photons in the light curve.

Other Parameters:
gti: 2-d float array

[[gti0_0, gti0_1], [gti1_0, gti1_1], ...] – Good Time intervals. This choice overrides the GTIs in the single light curves. Use with care, especially if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input object before making the power spectrum.

skip_checks: bool

Skip initial checks, for speed or other reasons (you need to trust your inputs!).

_calculate_errors()

Calculate the errors on cross powers and lags.

Uses different formulas if the reference band contains the photons of the subject band. This might happen, for example, when calculating covariance spectra using a large reference band. See the details in the documentation of stingray.fourier.error_on_averaged_cross_spectrum().

Please note that we have dedicated methods for covariance spectra and other variability versus energy spectra in stingray.varenergyspectrum, even though they only work for input event lists at the moment.

_initialize_empty()[source]

Set all attributes to None.

_initialize_from_any_input(data, dt=None, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)[source]

Initialize the class, trying to understand the input types.

The input arguments are the same as __init__(). Based on the type of data, this method will call the appropriate powerspectrum_from_XXXX function, and initialize self with the correct attributes.

_normalize_crossspectrum(unnorm_power)

Normalize the real part of the cross spectrum to Leahy, absolute rms^2, fractional rms^2 normalization, or not at all.

Parameters:
unnorm_power: numpy.ndarray

The unnormalized cross spectrum.

Returns:
power: numpy.nd.array

The normalized co-spectrum (real part of the cross spectrum). For ‘none’ normalization, imaginary part is returned as well.

_operation_with_other_obj(other, operation, operated_attrs=None, error_attrs=None, error_operation=None, inplace=False)

Helper method to codify an operation of one time series with another (e.g. add, subtract). Takes into account the GTIs, and returns a new StingrayTimeseries object.

Parameters:
otherStingrayTimeseries object

A second time series object

operationfunction

An operation between the StingrayTimeseries object calling this method, and other, operating on all the specified array attributes.

Returns:
ts_newStingrayTimeseries object

The new time series calculated in operation

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

The function used for error propagation. Defaults to the sum of squares.

_rms_error(powers)[source]

Compute the error on the fractional rms amplitude using error propagation. Note: this uses the actual measured powers, which is not strictly correct. We should be using the underlying power spectrum, but in the absence of an estimate of that, this will have to do.

\[r = \sqrt{P}\]
\[\begin{split}\delta r = \\frac{1}{2 * \sqrt{P}} \delta P\end{split}\]
Parameters:
powers: iterable

The list of powers used to compute the fractional rms amplitude.

Returns:
delta_rms: float

The error on the fractional rms amplitude.

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

classical_significances(threshold=1, trial_correction=False)[source]

Compute the classical significances for the powers in the power spectrum, assuming an underlying noise distribution that follows a chi-square distributions with 2M degrees of freedom, where M is the number of powers averaged in each bin.

Note that this function will only produce correct results when the following underlying assumptions are fulfilled:

  1. The power spectrum is Leahy-normalized

  2. There is no source of variability in the data other than the periodic signal to be determined with this method. This is important! If there are other sources of (aperiodic) variability in the data, this method will not produce correct results, but instead produce a large number of spurious false positive detections!

  3. There are no significant instrumental effects changing the statistical distribution of the powers (e.g. pile-up or dead time)

By default, the method produces (index,p-values) for all powers in the power spectrum, where index is the numerical index of the power in question. If a threshold is set, then only powers with p-values below that threshold with their respective indices. If trial_correction is set to True, then the threshold will be corrected for the number of trials (frequencies) in the power spectrum before being used.

Parameters:
thresholdfloat, optional, default 1

The threshold to be used when reporting p-values of potentially significant powers. Must be between 0 and 1. Default is 1 (all p-values will be reported).

trial_correctionbool, optional, default False

A Boolean flag that sets whether the threshold will be corrected by the number of frequencies before being applied. This decreases the threshold (p-values need to be lower to count as significant). Default is False (report all powers) though for any application where threshold is set to something meaningful, this should also be applied!

Returns:
pvalsiterable

A list of (p-value, index) tuples for all powers that have p-values lower than the threshold specified in threshold.

coherence()

Compute Raw Coherence function of the cross spectrum.

Deprecated. Please use the raw_coherence() method of AveragedCrossspectrum, with the correct parameters.

Returns:
cohnumpy.ndarray

Coherence function

compute_rms(min_freq, max_freq, poisson_noise_level=None)[source]

Compute the fractional rms amplitude in the power spectrum between two frequencies.

Parameters:
min_freq: float

The lower frequency bound for the calculation.

max_freq: float

The upper frequency bound for the calculation.

Returns:
rms: float

The fractional rms amplitude contained between min_freq and max_freq.

rms_err: float

The error on the fractional rms amplitude.

Other Parameters:
poisson_noise_levelfloat, default is None

This is the Poisson noise level of the PDS with same normalization as the PDS. If poissoin_noise_level is None, the Poisson noise is calculated in the idealcase e.g. 2./<countrate> for fractional rms normalisation Dead time and other instrumental effects can alter it. The user can fit the Poisson noise level outside this function using the same normalisation of the PDS and it will get subtracted from powers here.

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

deadtime_correct(dead_time, rate, background_rate=0, limit_k=200, n_approx=None, paralyzable=False)

Correct the power spectrum for dead time effects.

This correction is based on the formula given in Zhang et al. 2015, assuming a constant dead time for all events. For more advanced dead time corrections, see the FAD method from stingray.deadtime.fad

Parameters:
dead_time: float

The dead time of the detector.

ratefloat

Detected source count rate

Returns:
spectrum: Crossspectrum or derivative.

The dead-time corrected spectrum.

Other Parameters:
background_ratefloat, default 0

Detected background count rate. This is important to estimate when deadtime is given by the combination of the source counts and background counts (e.g. in an imaging X-ray detector).

paralyzable: bool, default False

If True, the dead time correction is done assuming a paralyzable dead time. If False, the correction is done assuming a non-paralyzable (more common) dead time.

limit_kint, default 200

Limit to this value the number of terms in the inner loops of calculations. Check the plots returned by the check_B() and check_A() functions to test that this number is adequate.

n_approxint, default None

Number of bins to calculate the model power spectrum. If None, it will use the size of the input frequency array. Relatively simple models (e.g., low count rates compared to dead time) can use a smaller number of bins to speed up the calculation, and the final power values will be interpolated.

dict() → dict

Return a dictionary representation of the object.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

static from_events(events, dt, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)[source]

Calculate an average power spectrum from an event list.

Parameters:
eventsEventList

Event list to be analyzed.

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency).

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedPowerspectrum.

gti: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average.

static from_lc_iterable(iter_lc, dt, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)[source]

Calculate the average power spectrum of an iterable collection of light curves.

Parameters:
iter_lciterable of stingray.Lightcurve objects or np.array

Light curves. If arrays, use them as counts.

dtfloat

The time resolution of the light curves (sets the Nyquist frequency)

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedPowerspectrum.

gti: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average.

static from_lightcurve(lc, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)[source]

Calculate a power spectrum from a light curve.

Parameters:
eventsstingray.Lightcurve

Light curve to be analyzed.

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency).

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedPowerspectrum.

gti: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

static from_stingray_timeseries(ts, flux_attr, error_flux_attr=None, segment_size=None, norm='none', silent=False, use_common_mean=True, gti=None, save_all=False)[source]

Calculate AveragedPowerspectrum from a time series.

Parameters:
tsStingrayTimeseries

Input Time Series

flux_attrstr

What attribute of the time series will be used.

Other Parameters:
error_flux_attrstr

What attribute of the time series will be used as error bar.

segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

save_allbool, default False

Save all intermediate PDSs used for the final average.

static from_time_array(times, dt, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True)[source]

Calculate an average power spectrum from an array of event times.

Parameters:
timesnp.array

Event arrival times.

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency).

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedPowerspectrum.

gti: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

initial_checks(data1=None, data2=None, norm='frac', gti=None, lc1=None, lc2=None, segment_size=None, power_type='real', dt=None, fullspec=False)

Run initial checks on the input.

Returns True if checks are passed, False if they are not.

Raises various errors for different bad inputs

Examples

>>> times = np.arange(0, 10)
>>> counts = np.random.poisson(100, 10)
>>> lc1 = Lightcurve(times, counts, skip_checks=True)
>>> lc2 = Lightcurve(times, counts, skip_checks=True)
>>> ev1 = EventList(times)
>>> ev2 = EventList(times)
>>> c = Crossspectrum()
>>> ac = AveragedCrossspectrum()

If norm is not a string, raise a TypeError >>> Crossspectrum.initial_checks(c, norm=1) Traceback (most recent call last): … TypeError: norm must be a string…

If norm is not one of the valid norms, raise a ValueError >>> Crossspectrum.initial_checks(c, norm=”blabla”) Traceback (most recent call last): … ValueError: norm must be ‘frac’…

If power_type is not one of the valid norms, raise a ValueError >>> Crossspectrum.initial_checks(c, power_type=”blabla”) Traceback (most recent call last): … ValueError: power_type not recognized!

If the user passes only one light curve, raise a ValueError

>>> Crossspectrum.initial_checks(c, data1=lc1, data2=None)
Traceback (most recent call last):
...
ValueError: You can't do a cross spectrum...

If the user passes an event list without dt, raise a ValueError

>>> Crossspectrum.initial_checks(c, data1=ev1, data2=ev2, dt=None)
Traceback (most recent call last):
...
ValueError: If using event lists, please specify...
internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

modulation_upper_limit(fmin=None, fmax=None, c=0.95)[source]

Upper limit on a sinusoidal modulation.

To understand the meaning of this amplitude: if the modulation is described by:

..math:: p = overline{p} (1 + a * sin(x))

this function returns a.

If it is a sum of sinusoidal harmonics instead ..math:: p = overline{p} (1 + sum_l a_l * sin(lx)) a is equivalent to \(\sqrt(\sum_l a_l^2)\).

See stingray.stats.power_upper_limit, stingray.stats.amplitude_upper_limit for more information.

The formula used to calculate the upper limit assumes the Leahy normalization. If the periodogram is in another normalization, we will internally convert it to Leahy before calculating the upper limit.

Parameters:
fmin: float

The minimum frequency to search (defaults to the first nonzero bin)

fmax: float

The maximum frequency to search (defaults to the Nyquist frequency)

Returns:
a: float

The modulation amplitude that could produce P>pmeas with 1 - c probability.

Other Parameters:
c: float

The confidence value for the upper limit (e.g. 0.95 = 95%)

Examples

>>> pds = Powerspectrum()
>>> pds.norm = "leahy"
>>> pds.freq = np.arange(0., 5.)
>>> # Note: this pds has 40 as maximum value between 2 and 5 Hz
>>> pds.power = np.array([100000, 1, 1, 40, 1])
>>> pds.m = 1
>>> pds.nphots = 30000
>>> assert np.isclose(
...     pds.modulation_upper_limit(fmin=2, fmax=5, c=0.99),
...     0.10164,
...     atol=0.0001)
phase_lag()

Calculate the fourier phase lag of the cross spectrum.

This is defined as the argument of the complex cross spectrum, and gives the delay at all frequencies, in cycles, of one input light curve with respect to the other.

plot(labels=None, axis=None, title=None, marker='-', save=False, filename=None, ax=None)

Plot the amplitude of the cross spectrum vs. the frequency using matplotlib.

Parameters:
labelsiterable, default None

A list of tuple with xlabel and ylabel as strings.

axislist, tuple, string, default None

Parameter to set axis properties of the matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for the``matplotlib.pyplot.axis()`` method.

titlestr, default None

The title of the plot.

markerstr, default ‘-’

Line style and color of the plot. Line styles and colors are combined in a single format string, as in 'bo' for blue circles. See matplotlib.pyplot.plot for more options.

saveboolean, optional, default False

If True, save the figure with specified filename.

filenamestr

File name of the image to save. Depends on the boolean save.

axmatplotlib.Axes object

An axes object to fill with the cross correlation plot.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

rebin(df=None, f=None, method='mean')[source]

Rebin the power spectrum.

Parameters:
df: float

The new frequency resolution.

Returns:
bin_cs = Powerspectrum object

The newly binned power spectrum.

Other Parameters:
f: float

The rebin factor. If specified, it substitutes df with f*self.df, so f>1 is recommended.

rebin_log(f=0.01)

Logarithmic rebin of the periodogram. The new frequency depends on the previous frequency modified by a factor f:

\[d\nu_j = d\nu_{j-1} (1+f)\]
Parameters:
f: float, optional, default ``0.01``

parameter that steers the frequency resolution

Returns:
new_specCrossspectrum (or one of its subclasses) object

The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from AveragedPowerspectrum, it will return an object of class

save_as_xspec(outroot, header_keywords=None)

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

time_lag()

Calculate the fourier time lag of the cross spectrum. The time lag is calculated by taking the phase lag \(\phi\) and

..math:

\tau = \frac{\phi}{\two pi \nu}

where \(\nu\) is the center of the frequency bins.

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_norm(norm, inplace=False)

Convert Cross spectrum to new normalization.

Parameters:
normstr

The new normalization of the spectrum

Returns:
new_specobject, same class as input

The new, normalized, spectrum.

Other Parameters:
inplace: bool, default False

If True, change the current instance. Otherwise, return a new one

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


AveragedCrossspectrum

class stingray.AveragedCrossspectrum(data1=None, data2=None, segment_size=None, norm='frac', gti=None, power_type='all', silent=False, lc1=None, lc2=None, dt=None, fullspec=False, save_all=False, use_common_mean=True, skip_checks=False, channels_overlap=False)[source]

Make an averaged cross spectrum from a light curve by segmenting two light curves, Fourier-transforming each segment and then averaging the resulting cross spectra.

Parameters:
data1: :class:`stingray.Lightcurve`object OR iterable of :class:`stingray.Lightcurve` objects OR :class:`~stingray.events.EventList` object

A light curve from which to compute the cross spectrum. In some cases, this would be the light curve of the wavelength/energy/frequency band of interest.

data2: :class:`stingray.Lightcurve`object OR iterable of :class:`stingray.Lightcurve` objects OR :class:`~stingray.events.EventList` object

A second light curve to use in the cross spectrum. In some cases, this would be the wavelength/energy/frequency reference band to compare the band of interest with.

segment_size: float

The size of each segment to average. Note that if the total duration of each Lightcurve object in lc1 or lc2 is not an integer multiple of the segment_size, then any fraction left-over at the end of the time series will be lost. Otherwise you introduce artifacts.

norm: {``frac``, ``abs``, ``leahy``, ``none``}, default ``none``

The normalization of the (real part of the) cross spectrum.

Attributes:
freq: numpy.ndarray

The array of mid-bin frequencies that the Fourier transform samples.

power: numpy.ndarray

The array of cross spectra.

power_err: numpy.ndarray

The uncertainties of power. An approximation for each bin given by power_err= power/sqrt(m). Where m is the number of power averaged in each bin (by frequency binning, or averaging power spectra of segments of a light curve). Note that for a single realization (m=1) the error is equal to the power.

df: float

The frequency resolution.

m: int

The number of averaged cross spectra.

n: int

The number of time bins per segment of light curve.

nphots1: float

The total number of photons in the first (interest) light curve.

nphots2: float

The total number of photons in the second (reference) light curve.

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals.

Other Parameters:
gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

dtfloat

The time resolution of the light curve. Only needed when constructing light curves in the case where data1 or data2 are of EventList

power_type: string, optional, default ``all``

Parameter to choose among complete, real part and magnitude of the cross spectrum.

silentbool, default False

Do not show a progress bar when generating an averaged cross spectrum. Useful for the batch execution of many spectra

lc1: :class:`stingray.Lightcurve`object OR iterable of :class:`stingray.Lightcurve` objects

For backwards compatibility only. Like data1, but no stingray.events.EventList objects allowed

lc2: :class:`stingray.Lightcurve`object OR iterable of :class:`stingray.Lightcurve` objects

For backwards compatibility only. Like data2, but no stingray.events.EventList objects allowed

fullspec: boolean, optional, default ``False``

If True, return the full array of frequencies, otherwise return just the positive frequencies.

save_allbool, default False

Save all intermediate PDSs used for the final average. Use with care. This is likely to fill up your RAM on medium-sized datasets, and to slow down the computation when rebinning.

skip_checks: bool

Skip initial checks, for speed or other reasons (you need to trust your inputs!)

use_common_mean: bool

Averaged cross spectra are normalized in two possible ways: one is by normalizing each of the single spectra that get averaged, the other is by normalizing after the averaging. If use_common_mean is selected, the spectrum will be normalized after the average.

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

classical_significances(threshold=1, trial_correction=False)

Compute the classical significances for the powers in the power spectrum, assuming an underlying noise distribution that follows a chi-square distributions with 2M degrees of freedom, where M is the number of powers averaged in each bin.

Note that this function will only produce correct results when the following underlying assumptions are fulfilled:

  1. The power spectrum is Leahy-normalized

  2. There is no source of variability in the data other than the periodic signal to be determined with this method. This is important! If there are other sources of (aperiodic) variability in the data, this method will not produce correct results, but instead produce a large number of spurious false positive detections!

  3. There are no significant instrumental effects changing the statistical distribution of the powers (e.g. pile-up or dead time)

By default, the method produces (index,p-values) for all powers in the power spectrum, where index is the numerical index of the power in question. If a threshold is set, then only powers with p-values below that threshold with their respective indices. If trial_correction is set to True, then the threshold will be corrected for the number of trials (frequencies) in the power spectrum before being used.

Parameters:
thresholdfloat, optional, default 1

The threshold to be used when reporting p-values of potentially significant powers. Must be between 0 and 1. Default is 1 (all p-values will be reported).

trial_correctionbool, optional, default False

A Boolean flag that sets whether the threshold will be corrected by the number of frequencies before being applied. This decreases the threshold (p-values need to be lower to count as significant). Default is False (report all powers) though for any application where threshold is set to something meaningful, this should also be applied!

Returns:
pvalsiterable

A list of (index, p-value) tuples for all powers that have p-values lower than the threshold specified in threshold.

coherence()[source]

Averaged Raw Coherence function.

Coherence is a Fourier frequency dependent measure of the linear correlation between time series measured simultaneously in two energy channels.

Note

This function is deprecated. Please use raw_coherence() or intrinsic_coherence() instead, depending on your needs.

NoteThe uncertainty in coherence function is strictly valid for Gaussian

statistics only.

Returns:
(coh, uncertainty)tuple of np.ndarray

Tuple comprising the coherence function and uncertainty.

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

deadtime_correct(dead_time, rate, background_rate=0, limit_k=200, n_approx=None, paralyzable=False)

Correct the power spectrum for dead time effects.

This correction is based on the formula given in Zhang et al. 2015, assuming a constant dead time for all events. For more advanced dead time corrections, see the FAD method from stingray.deadtime.fad

Parameters:
dead_time: float

The dead time of the detector.

ratefloat

Detected source count rate

Returns:
spectrum: Crossspectrum or derivative.

The dead-time corrected spectrum.

Other Parameters:
background_ratefloat, default 0

Detected background count rate. This is important to estimate when deadtime is given by the combination of the source counts and background counts (e.g. in an imaging X-ray detector).

paralyzable: bool, default False

If True, the dead time correction is done assuming a paralyzable dead time. If False, the correction is done assuming a non-paralyzable (more common) dead time.

limit_kint, default 200

Limit to this value the number of terms in the inner loops of calculations. Check the plots returned by the check_B() and check_A() functions to test that this number is adequate.

n_approxint, default None

Number of bins to calculate the model power spectrum. If None, it will use the size of the input frequency array. Relatively simple models (e.g., low count rates compared to dead time) can use a smaller number of bins to speed up the calculation, and the final power values will be interpolated.

dict() → dict

Return a dictionary representation of the object.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

static from_events(events1, events2, dt, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)

Calculate AveragedCrossspectrum from two event lists

Parameters:
events1EventList

Events from channel 1

events2EventList

Events from channel 2

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency)

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

static from_lc_iterable(iter_lc1, iter_lc2, dt, segment_size, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)

Calculate AveragedCrossspectrum from two light curves

Parameters:
iter_lc1iterable of stingray.Lightcurve objects or np.array

Light curves from channel 1. If arrays, use them as counts

iter_lc1iterable of stingray.Lightcurve objects or np.array

Light curves from channel 2. If arrays, use them as counts

dtfloat

The time resolution of the light curves (sets the Nyquist frequency)

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

save_allbool, default False

If True, save the cross spectrum of each segment in the cs_all attribute of the output Crossspectrum object.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

static from_lightcurve(lc1, lc2, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)

Calculate AveragedCrossspectrum from two light curves

Parameters:
lc1stingray.Lightcurve

Light curve from channel 1

lc2stingray.Lightcurve

Light curve from channel 2

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

static from_stingray_timeseries(ts1, ts2, flux_attr, error_flux_attr=None, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)

Calculate AveragedCrossspectrum from two light curves

Parameters:
ts1StingrayTimeseries

Time series from channel 1

ts2StingrayTimeseries

Time series from channel 2

flux_attrstr

What attribute of the time series will be used.

Other Parameters:
error_flux_attrstr

What attribute of the time series will be used as error bar.

segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

static from_time_array(times1, times2, dt, segment_size=None, gti=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, channels_overlap=False)

Calculate AveragedCrossspectrum from two arrays of event times.

Parameters:
times1np.array

Event arrival times of channel 1

times2np.array

Event arrival times of channel 2

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency)

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum.

gti[[gti0, gti1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

initial_checks(data1, segment_size=None, **kwargs)[source]

Examples

>>> times = np.arange(0, 10)
>>> ev1 = EventList(times)
>>> ev2 = EventList(times)
>>> ac = AveragedCrossspectrum()

If AveragedCrossspectrum, you need segment_size >>> AveragedCrossspectrum.initial_checks(ac, data1=ev1, data2=ev2, dt=1) Traceback (most recent call last): … ValueError: segment_size must be specified…

And it needs to be finite! >>> AveragedCrossspectrum.initial_checks(ac, data1=ev1, data2=ev2, dt=1., segment_size=np.nan) Traceback (most recent call last): … ValueError: segment_size must be finite!

internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

intrinsic_coherence(adjust_bias=False)[source]

Averaged Coherence function.

Intrinsic Coherence is defined in Vaughan and Nowak, 1997 [1], as

\[\tilde{\gamma}^2(f) = \frac{|\langle \tilde{C}(f) \rangle|^2 - \tilde{b}^2} {(\langle \tilde{P}_1(f) \rangle - \tilde{P}_{1, \rm noise}) (\langle \tilde{P}_2(f) \rangle - \tilde{P}_{2, \rm noise})}\]

where \(\tilde{b}^2\) is the bias term that accounts for the contribution of Poisson noise to the cross spectrum (see stingray.fourier.bias_term()), and tilde generally indicates noisy measurements of the cross spectrum \(C\) and the power spectra \(P_n\). The terms \(\tilde{P}_{n, \rm noise}\) are the estimates of the contribution of Poisson noise to the power spectra.

Note

This definition is strictly valid for Gaussian statistics only, meaning that the cross spectrum and the power spectra estimates have been obtained by averaging over at least 50 intervals. A warning will be raised if this is not the case

Parameters:
adjust_bias: bool, default False

If True, the bias term is adjusted to account for the fact that the bias term depends on the intrinsic coherence itself. This is done iteratively. See Ingram 2019, cited, for details on the bias term and its dependence on the intrinsic coherence, and the documentation of intrinsic_coherence().

Returns:
(coh, uncertainty)tuple of np.ndarray

Tuple comprising the coherence function and uncertainty.

References

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

phase_lag()[source]

Return the fourier phase lag of the cross spectrum.

plot(labels=None, axis=None, title=None, marker='-', save=False, filename=None, ax=None)

Plot the amplitude of the cross spectrum vs. the frequency using matplotlib.

Parameters:
labelsiterable, default None

A list of tuple with xlabel and ylabel as strings.

axislist, tuple, string, default None

Parameter to set axis properties of the matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for the``matplotlib.pyplot.axis()`` method.

titlestr, default None

The title of the plot.

markerstr, default ‘-’

Line style and color of the plot. Line styles and colors are combined in a single format string, as in 'bo' for blue circles. See matplotlib.pyplot.plot for more options.

saveboolean, optional, default False

If True, save the figure with specified filename.

filenamestr

File name of the image to save. Depends on the boolean save.

axmatplotlib.Axes object

An axes object to fill with the cross correlation plot.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

raw_coherence()[source]

Averaged Raw Coherence function.

Coherence is a Fourier frequency dependent measure of the linear correlation between time series measured simultaneously in two energy channels.

The function is defined as (see Ingram 2019 [2] :

\[\tilde{g}^2(f) = \frac{|\langle \tilde{C}(f) \rangle|^2 - \tilde{b}^2} {\langle \tilde{P}_1(f) \rangle \langle \tilde{P}_2(f) \rangle}\]

where \(\tilde{b}^2\) is the bias term that accounts for the contribution of Poisson noise to the cross spectrum (see stingray.fourier.bias_term()), and tilde generally indicates noisy measurements of the cross spectrum \(C\) and the power spectra \(P_n\).

Note

This definition is strictly valid for Gaussian statistics only, meaning that the cross spectrum and the power spectra estimates have been obtained by averaging over at least 50 intervals. A warning will be raised if this is not the case

Returns:
(coh, uncertainty)tuple of np.ndarray

Tuple comprising the coherence function and uncertainty.

References

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

rebin(df=None, f=None, method='mean')

Rebin the cross spectrum to a new frequency resolution df.

Parameters:
df: float

The new frequency resolution

Returns:
bin_cs = Crossspectrum (or one of its subclasses) object

The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from AveragedPowerspectrum, it will return an object of class AveragedPowerspectrum, too.

Other Parameters:
f: float

the rebin factor. If specified, it substitutes df with f*self.df

rebin_log(f=0.01)

Logarithmic rebin of the periodogram. The new frequency depends on the previous frequency modified by a factor f:

\[d\nu_j = d\nu_{j-1} (1+f)\]
Parameters:
f: float, optional, default ``0.01``

parameter that steers the frequency resolution

Returns:
new_specCrossspectrum (or one of its subclasses) object

The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from AveragedPowerspectrum, it will return an object of class

save_as_xspec(outroot, header_keywords=None)

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

time_lag()[source]

Calculate time lag and uncertainty.

Equation from Bendat & Piersol, 2011 [bendat-2011]__.

Returns:
lagnp.ndarray

The time lag

lag_errnp.ndarray

The uncertainty in the time lag

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_norm(norm, inplace=False)

Convert Cross spectrum to new normalization.

Parameters:
normstr

The new normalization of the spectrum

Returns:
new_specobject, same class as input

The new, normalized, spectrum.

Other Parameters:
inplace: bool, default False

If True, change the current instance. Otherwise, return a new one

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


AveragedPowerspectrum

class stingray.AveragedPowerspectrum(data=None, segment_size=None, norm='frac', gti=None, silent=False, dt=None, lc=None, large_data=False, save_all=False, skip_checks=False, use_common_mean=True)[source]

Make an averaged periodogram from a light curve by segmenting the light curve, Fourier-transforming each segment and then averaging the resulting periodograms.

Parameters:
data: :class:`stingray.Lightcurve`object OR iterable of :class:`stingray.Lightcurve` objects OR :class:`~stingray.events.EventList` object

The light curve data to be Fourier-transformed.

segment_size: float

The size of each segment to average. Note that if the total duration of each Lightcurve object in lc is not an integer multiple of the segment_size, then any fraction left-over at the end of the time series will be lost.

norm: {“leahy” | “frac” | “abs” | “none” }, optional, default “frac”

The normalization of the periodogram to be used.

Attributes:
norm: {``leahy`` | ``frac`` | ``abs`` | ``none`` }

The normalization of the periodogram.

freq: numpy.ndarray

The array of mid-bin frequencies that the Fourier transform samples.

power: numpy.ndarray

The array of normalized powers

power_err: numpy.ndarray

The uncertainties of power. An approximation for each bin given by power_err= power/sqrt(m). Where m is the number of power averaged in each bin (by frequency binning, or averaging power spectra of segments of a light curve). Note that for a single realization (m=1) the error is equal to the power.

unnorm_power: numpy.ndarray

The array of unnormalized powers

unnorm_power_err: numpy.ndarray

The uncertainties of unnorm_power.

df: float

The frequency resolution.

m: int

The number of averaged periodograms.

n: int

The number of data points in the light curve.

nphots: float

The total number of photons in the light curve.

cs_all: list of :class:`~stingray.Powerspectrum` objects

The list of all periodograms used to calculate the average periodogram.

Other Parameters:
gti: 2-d float array

[[gti0_0, gti0_1], [gti1_0, gti1_1], ...] – Good Time intervals. This choice overrides the GTIs in the single light curves. Use with care, especially if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input object before making the power spectrum.

silentbool, default False

Do not show a progress bar when generating an averaged cross spectrum. Useful for the batch execution of many spectra.

dt: float

The time resolution of the light curve. Only needed when constructing light curves in the case where data is of EventList.

save_allbool, default False

Save all intermediate PDSs used for the final average. Use with care. This is likely to fill up your RAM on medium-sized datasets, and to slow down the computation when rebinning.

skip_checks: bool

Skip initial checks, for speed or other reasons (you need to trust your inputs!).

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

classical_significances(threshold=1, trial_correction=False)

Compute the classical significances for the powers in the power spectrum, assuming an underlying noise distribution that follows a chi-square distributions with 2M degrees of freedom, where M is the number of powers averaged in each bin.

Note that this function will only produce correct results when the following underlying assumptions are fulfilled:

  1. The power spectrum is Leahy-normalized

  2. There is no source of variability in the data other than the periodic signal to be determined with this method. This is important! If there are other sources of (aperiodic) variability in the data, this method will not produce correct results, but instead produce a large number of spurious false positive detections!

  3. There are no significant instrumental effects changing the statistical distribution of the powers (e.g. pile-up or dead time)

By default, the method produces (index,p-values) for all powers in the power spectrum, where index is the numerical index of the power in question. If a threshold is set, then only powers with p-values below that threshold with their respective indices. If trial_correction is set to True, then the threshold will be corrected for the number of trials (frequencies) in the power spectrum before being used.

Parameters:
thresholdfloat, optional, default 1

The threshold to be used when reporting p-values of potentially significant powers. Must be between 0 and 1. Default is 1 (all p-values will be reported).

trial_correctionbool, optional, default False

A Boolean flag that sets whether the threshold will be corrected by the number of frequencies before being applied. This decreases the threshold (p-values need to be lower to count as significant). Default is False (report all powers) though for any application where threshold is set to something meaningful, this should also be applied!

Returns:
pvalsiterable

A list of (p-value, index) tuples for all powers that have p-values lower than the threshold specified in threshold.

coherence()

Averaged Raw Coherence function.

Coherence is a Fourier frequency dependent measure of the linear correlation between time series measured simultaneously in two energy channels.

Note

This function is deprecated. Please use raw_coherence() or intrinsic_coherence() instead, depending on your needs.

NoteThe uncertainty in coherence function is strictly valid for Gaussian

statistics only.

Returns:
(coh, uncertainty)tuple of np.ndarray

Tuple comprising the coherence function and uncertainty.

compute_rms(min_freq, max_freq, poisson_noise_level=None)

Compute the fractional rms amplitude in the power spectrum between two frequencies.

Parameters:
min_freq: float

The lower frequency bound for the calculation.

max_freq: float

The upper frequency bound for the calculation.

Returns:
rms: float

The fractional rms amplitude contained between min_freq and max_freq.

rms_err: float

The error on the fractional rms amplitude.

Other Parameters:
poisson_noise_levelfloat, default is None

This is the Poisson noise level of the PDS with same normalization as the PDS. If poissoin_noise_level is None, the Poisson noise is calculated in the idealcase e.g. 2./<countrate> for fractional rms normalisation Dead time and other instrumental effects can alter it. The user can fit the Poisson noise level outside this function using the same normalisation of the PDS and it will get subtracted from powers here.

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

deadtime_correct(dead_time, rate, background_rate=0, limit_k=200, n_approx=None, paralyzable=False)

Correct the power spectrum for dead time effects.

This correction is based on the formula given in Zhang et al. 2015, assuming a constant dead time for all events. For more advanced dead time corrections, see the FAD method from stingray.deadtime.fad

Parameters:
dead_time: float

The dead time of the detector.

ratefloat

Detected source count rate

Returns:
spectrum: Crossspectrum or derivative.

The dead-time corrected spectrum.

Other Parameters:
background_ratefloat, default 0

Detected background count rate. This is important to estimate when deadtime is given by the combination of the source counts and background counts (e.g. in an imaging X-ray detector).

paralyzable: bool, default False

If True, the dead time correction is done assuming a paralyzable dead time. If False, the correction is done assuming a non-paralyzable (more common) dead time.

limit_kint, default 200

Limit to this value the number of terms in the inner loops of calculations. Check the plots returned by the check_B() and check_A() functions to test that this number is adequate.

n_approxint, default None

Number of bins to calculate the model power spectrum. If None, it will use the size of the input frequency array. Relatively simple models (e.g., low count rates compared to dead time) can use a smaller number of bins to speed up the calculation, and the final power values will be interpolated.

dict() → dict

Return a dictionary representation of the object.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

static from_events(events, dt, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)

Calculate an average power spectrum from an event list.

Parameters:
eventsEventList

Event list to be analyzed.

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency).

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedPowerspectrum.

gti: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average.

static from_lc_iterable(iter_lc, dt, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)

Calculate the average power spectrum of an iterable collection of light curves.

Parameters:
iter_lciterable of stingray.Lightcurve objects or np.array

Light curves. If arrays, use them as counts.

dtfloat

The time resolution of the light curves (sets the Nyquist frequency)

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedPowerspectrum.

gti: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average.

static from_lightcurve(lc, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)

Calculate a power spectrum from a light curve.

Parameters:
eventsstingray.Lightcurve

Light curve to be analyzed.

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency).

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedPowerspectrum.

gti: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

static from_stingray_timeseries(ts, flux_attr, error_flux_attr=None, segment_size=None, norm='none', silent=False, use_common_mean=True, gti=None, save_all=False)

Calculate AveragedPowerspectrum from a time series.

Parameters:
tsStingrayTimeseries

Input Time Series

flux_attrstr

What attribute of the time series will be used.

Other Parameters:
error_flux_attrstr

What attribute of the time series will be used as error bar.

segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

save_allbool, default False

Save all intermediate PDSs used for the final average.

static from_time_array(times, dt, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True)

Calculate an average power spectrum from an array of event times.

Parameters:
timesnp.array

Event arrival times.

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency).

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedPowerspectrum.

gti: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

initial_checks(*args, **kwargs)[source]

Examples

>>> times = np.arange(0, 10)
>>> ev1 = EventList(times)
>>> ev2 = EventList(times)
>>> ac = AveragedCrossspectrum()

If AveragedCrossspectrum, you need segment_size >>> AveragedCrossspectrum.initial_checks(ac, data1=ev1, data2=ev2, dt=1) Traceback (most recent call last): … ValueError: segment_size must be specified…

And it needs to be finite! >>> AveragedCrossspectrum.initial_checks(ac, data1=ev1, data2=ev2, dt=1., segment_size=np.nan) Traceback (most recent call last): … ValueError: segment_size must be finite!

internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

intrinsic_coherence(adjust_bias=False)

Averaged Coherence function.

Intrinsic Coherence is defined in Vaughan and Nowak, 1997 [3], as

\[\tilde{\gamma}^2(f) = \frac{|\langle \tilde{C}(f) \rangle|^2 - \tilde{b}^2} {(\langle \tilde{P}_1(f) \rangle - \tilde{P}_{1, \rm noise}) (\langle \tilde{P}_2(f) \rangle - \tilde{P}_{2, \rm noise})}\]

where \(\tilde{b}^2\) is the bias term that accounts for the contribution of Poisson noise to the cross spectrum (see stingray.fourier.bias_term()), and tilde generally indicates noisy measurements of the cross spectrum \(C\) and the power spectra \(P_n\). The terms \(\tilde{P}_{n, \rm noise}\) are the estimates of the contribution of Poisson noise to the power spectra.

Note

This definition is strictly valid for Gaussian statistics only, meaning that the cross spectrum and the power spectra estimates have been obtained by averaging over at least 50 intervals. A warning will be raised if this is not the case

Parameters:
adjust_bias: bool, default False

If True, the bias term is adjusted to account for the fact that the bias term depends on the intrinsic coherence itself. This is done iteratively. See Ingram 2019, cited, for details on the bias term and its dependence on the intrinsic coherence, and the documentation of intrinsic_coherence().

Returns:
(coh, uncertainty)tuple of np.ndarray

Tuple comprising the coherence function and uncertainty.

References

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

modulation_upper_limit(fmin=None, fmax=None, c=0.95)

Upper limit on a sinusoidal modulation.

To understand the meaning of this amplitude: if the modulation is described by:

..math:: p = overline{p} (1 + a * sin(x))

this function returns a.

If it is a sum of sinusoidal harmonics instead ..math:: p = overline{p} (1 + sum_l a_l * sin(lx)) a is equivalent to \(\sqrt(\sum_l a_l^2)\).

See stingray.stats.power_upper_limit, stingray.stats.amplitude_upper_limit for more information.

The formula used to calculate the upper limit assumes the Leahy normalization. If the periodogram is in another normalization, we will internally convert it to Leahy before calculating the upper limit.

Parameters:
fmin: float

The minimum frequency to search (defaults to the first nonzero bin)

fmax: float

The maximum frequency to search (defaults to the Nyquist frequency)

Returns:
a: float

The modulation amplitude that could produce P>pmeas with 1 - c probability.

Other Parameters:
c: float

The confidence value for the upper limit (e.g. 0.95 = 95%)

Examples

>>> pds = Powerspectrum()
>>> pds.norm = "leahy"
>>> pds.freq = np.arange(0., 5.)
>>> # Note: this pds has 40 as maximum value between 2 and 5 Hz
>>> pds.power = np.array([100000, 1, 1, 40, 1])
>>> pds.m = 1
>>> pds.nphots = 30000
>>> assert np.isclose(
...     pds.modulation_upper_limit(fmin=2, fmax=5, c=0.99),
...     0.10164,
...     atol=0.0001)
phase_lag()

Return the fourier phase lag of the cross spectrum.

plot(labels=None, axis=None, title=None, marker='-', save=False, filename=None, ax=None)

Plot the amplitude of the cross spectrum vs. the frequency using matplotlib.

Parameters:
labelsiterable, default None

A list of tuple with xlabel and ylabel as strings.

axislist, tuple, string, default None

Parameter to set axis properties of the matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for the``matplotlib.pyplot.axis()`` method.

titlestr, default None

The title of the plot.

markerstr, default ‘-’

Line style and color of the plot. Line styles and colors are combined in a single format string, as in 'bo' for blue circles. See matplotlib.pyplot.plot for more options.

saveboolean, optional, default False

If True, save the figure with specified filename.

filenamestr

File name of the image to save. Depends on the boolean save.

axmatplotlib.Axes object

An axes object to fill with the cross correlation plot.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

raw_coherence()

Averaged Raw Coherence function.

Coherence is a Fourier frequency dependent measure of the linear correlation between time series measured simultaneously in two energy channels.

The function is defined as (see Ingram 2019 [4] :

\[\tilde{g}^2(f) = \frac{|\langle \tilde{C}(f) \rangle|^2 - \tilde{b}^2} {\langle \tilde{P}_1(f) \rangle \langle \tilde{P}_2(f) \rangle}\]

where \(\tilde{b}^2\) is the bias term that accounts for the contribution of Poisson noise to the cross spectrum (see stingray.fourier.bias_term()), and tilde generally indicates noisy measurements of the cross spectrum \(C\) and the power spectra \(P_n\).

Note

This definition is strictly valid for Gaussian statistics only, meaning that the cross spectrum and the power spectra estimates have been obtained by averaging over at least 50 intervals. A warning will be raised if this is not the case

Returns:
(coh, uncertainty)tuple of np.ndarray

Tuple comprising the coherence function and uncertainty.

References

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

rebin(df=None, f=None, method='mean')

Rebin the power spectrum.

Parameters:
df: float

The new frequency resolution.

Returns:
bin_cs = Powerspectrum object

The newly binned power spectrum.

Other Parameters:
f: float

The rebin factor. If specified, it substitutes df with f*self.df, so f>1 is recommended.

rebin_log(f=0.01)

Logarithmic rebin of the periodogram. The new frequency depends on the previous frequency modified by a factor f:

\[d\nu_j = d\nu_{j-1} (1+f)\]
Parameters:
f: float, optional, default ``0.01``

parameter that steers the frequency resolution

Returns:
new_specCrossspectrum (or one of its subclasses) object

The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from AveragedPowerspectrum, it will return an object of class

save_as_xspec(outroot, header_keywords=None)

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

time_lag()

Calculate time lag and uncertainty.

Equation from Bendat & Piersol, 2011 [bendat-2011]__.

Returns:
lagnp.ndarray

The time lag

lag_errnp.ndarray

The uncertainty in the time lag

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_norm(norm, inplace=False)

Convert Cross spectrum to new normalization.

Parameters:
normstr

The new normalization of the spectrum

Returns:
new_specobject, same class as input

The new, normalized, spectrum.

Other Parameters:
inplace: bool, default False

If True, change the current instance. Otherwise, return a new one

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


Dynamical Crossspectrum

class stingray.DynamicalCrossspectrum(data1=None, data2=None, segment_size=None, norm='frac', gti=None, sample_time=None, use_common_mean=True)[source]

Create a dynamical cross spectrum, also often called a spectrogram.

This class will divide two Lightcurve objects into segments of length segment_size, create a cross spectrum for each segment and store all powers in a matrix as a function of both time (using the mid-point of each segment) and frequency.

This is often used to trace changes in period of a (quasi-)periodic signal over time.

Parameters:
data1stingray.Lightcurve or EventList object

The time series or event list from the subject band, or channel, for which the dynamical cross spectrum is to be calculated. If EventList, dt must be specified as well.

data2stingray.Lightcurve or EventList object

The time series or event list from the reference band, or channel, of the dynamical cross spectrum. If EventList, dt must be specified as well.

segment_sizefloat, default 1

Length of the segment of light curve, default value is 1 (in whatever units the time array in the Lightcurve object uses).

norm: {“leahy” | “frac” | “abs” | “none” }, optional, default “frac”

The normalization of the periodogram to be used. The mean count rate that is used in the fractional normalization is selected with use_common_mean.

Attributes:
segment_size: float

The size of each segment to average. Note that if the total duration of each input object in lc is not an integer multiple of the segment_size, then any fraction left-over at the end of the time series will be lost.

dyn_psnp.ndarray

The matrix of normalized squared absolute values of Fourier amplitudes. The axis are given by the freq and time attributes.

norm: {``leahy`` | ``frac`` | ``abs`` | ``none``}

The normalization of the periodogram.

freq: numpy.ndarray

The array of mid-bin frequencies that the Fourier transform samples.

time: numpy.ndarray

The array of mid-point times of each interval used for the dynamical power spectrum.

df: float

The frequency resolution.

dt: float

The time resolution of the dynamical spectrum. It is not the time resolution of the input light curve. It is the integration time of each line of the dynamical power spectrum (typically, an integer multiple of segment_size).

m: int

The number of averaged powers in each spectral bin (initially 1, it changes after rebinning).

use_common_mean: bool

Whether the normalization uses the mean count rate of the whole observation (True) or of each segment (False).

nphots_all: numpy.ndarray

The number of photons in each segment. For a cross spectrum, the geometric mean of the two light curves.

Other Parameters:
gti: 2-d float array

[[gti0_0, gti0_1], [gti1_0, gti1_1], ...] – Good Time intervals. This choice overrides the GTIs in the single light curves. Use with care, especially if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input object before making the cross spectrum.

sample_time: float

Compulsory for input EventList data. The time resolution of the lightcurve that is created internally from the input event lists. Drives the Nyquist frequency.

use_common_mean: bool, default True

Select the mean count rate used to normalize each segment. If True, every segment is normalized by the mean count rate of the whole observation, so a change of count rate between segments shows up in the normalized powers. If False, each segment is normalized by its own mean count rate. The latter is usually what one wants when comparing the columns of a dynamical spectrum with each other: with norm="frac" it removes the effect of a varying count rate at constant fractional rms, as produced by an rms-flux relation. Note that with use_common_mean set to False the Poisson noise level follows each segment’s own count rate, so a single noise level no longer applies to the whole matrix, and compute_rms returns the fractional rms of each segment relative to its own mean count rate.

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

classical_significances(threshold=1, trial_correction=False)

Compute the classical significances for the powers in the power spectrum, assuming an underlying noise distribution that follows a chi-square distributions with 2M degrees of freedom, where M is the number of powers averaged in each bin.

Note that this function will only produce correct results when the following underlying assumptions are fulfilled:

  1. The power spectrum is Leahy-normalized

  2. There is no source of variability in the data other than the periodic signal to be determined with this method. This is important! If there are other sources of (aperiodic) variability in the data, this method will not produce correct results, but instead produce a large number of spurious false positive detections!

  3. There are no significant instrumental effects changing the statistical distribution of the powers (e.g. pile-up or dead time)

By default, the method produces (index,p-values) for all powers in the power spectrum, where index is the numerical index of the power in question. If a threshold is set, then only powers with p-values below that threshold with their respective indices. If trial_correction is set to True, then the threshold will be corrected for the number of trials (frequencies) in the power spectrum before being used.

Parameters:
thresholdfloat, optional, default 1

The threshold to be used when reporting p-values of potentially significant powers. Must be between 0 and 1. Default is 1 (all p-values will be reported).

trial_correctionbool, optional, default False

A Boolean flag that sets whether the threshold will be corrected by the number of frequencies before being applied. This decreases the threshold (p-values need to be lower to count as significant). Default is False (report all powers) though for any application where threshold is set to something meaningful, this should also be applied!

Returns:
pvalsiterable

A list of (index, p-value) tuples for all powers that have p-values lower than the threshold specified in threshold.

coherence()

Averaged Raw Coherence function.

Coherence is a Fourier frequency dependent measure of the linear correlation between time series measured simultaneously in two energy channels.

Note

This function is deprecated. Please use raw_coherence() or intrinsic_coherence() instead, depending on your needs.

NoteThe uncertainty in coherence function is strictly valid for Gaussian

statistics only.

Returns:
(coh, uncertainty)tuple of np.ndarray

Tuple comprising the coherence function and uncertainty.

compute_rms(min_freq, max_freq, poisson_noise_level=0)[source]

Compute the fractional rms amplitude in the power spectrum between two frequencies.

Parameters:
min_freq: float

The lower frequency bound for the calculation.

max_freq: float

The upper frequency bound for the calculation.

Returns:
rms: float

The fractional rms amplitude contained between min_freq and max_freq. If use_common_mean is True, it is relative to the mean count rate of the whole observation, otherwise to the mean count rate of each segment.

rms_err: float

The error on the fractional rms amplitude.

Other Parameters:
poisson_noise_levelfloat or iterable, default 0

This is the Poisson noise level of the PDS with same normalization as the PDS. If iterable, it should contain one level per spectrum, e.g. poisson_power of a DynamicalPowerspectrum with use_common_mean=False.

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

deadtime_correct(dead_time, rate, background_rate=0, limit_k=200, n_approx=None, paralyzable=False)

Correct the power spectrum for dead time effects.

This correction is based on the formula given in Zhang et al. 2015, assuming a constant dead time for all events. For more advanced dead time corrections, see the FAD method from stingray.deadtime.fad

Parameters:
dead_time: float

The dead time of the detector.

ratefloat

Detected source count rate

Returns:
spectrum: Crossspectrum or derivative.

The dead-time corrected spectrum.

Other Parameters:
background_ratefloat, default 0

Detected background count rate. This is important to estimate when deadtime is given by the combination of the source counts and background counts (e.g. in an imaging X-ray detector).

paralyzable: bool, default False

If True, the dead time correction is done assuming a paralyzable dead time. If False, the correction is done assuming a non-paralyzable (more common) dead time.

limit_kint, default 200

Limit to this value the number of terms in the inner loops of calculations. Check the plots returned by the check_B() and check_A() functions to test that this number is adequate.

n_approxint, default None

Number of bins to calculate the model power spectrum. If None, it will use the size of the input frequency array. Relatively simple models (e.g., low count rates compared to dead time) can use a smaller number of bins to speed up the calculation, and the final power values will be interpolated.

dict() → dict

Return a dictionary representation of the object.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

static from_events(events1, events2, dt, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)

Calculate AveragedCrossspectrum from two event lists

Parameters:
events1EventList

Events from channel 1

events2EventList

Events from channel 2

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency)

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

static from_lc_iterable(iter_lc1, iter_lc2, dt, segment_size, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)

Calculate AveragedCrossspectrum from two light curves

Parameters:
iter_lc1iterable of stingray.Lightcurve objects or np.array

Light curves from channel 1. If arrays, use them as counts

iter_lc1iterable of stingray.Lightcurve objects or np.array

Light curves from channel 2. If arrays, use them as counts

dtfloat

The time resolution of the light curves (sets the Nyquist frequency)

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

save_allbool, default False

If True, save the cross spectrum of each segment in the cs_all attribute of the output Crossspectrum object.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

static from_lightcurve(lc1, lc2, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)

Calculate AveragedCrossspectrum from two light curves

Parameters:
lc1stingray.Lightcurve

Light curve from channel 1

lc2stingray.Lightcurve

Light curve from channel 2

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

static from_stingray_timeseries(ts1, ts2, flux_attr, error_flux_attr=None, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)

Calculate AveragedCrossspectrum from two light curves

Parameters:
ts1StingrayTimeseries

Time series from channel 1

ts2StingrayTimeseries

Time series from channel 2

flux_attrstr

What attribute of the time series will be used.

Other Parameters:
error_flux_attrstr

What attribute of the time series will be used as error bar.

segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

static from_time_array(times1, times2, dt, segment_size=None, gti=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, channels_overlap=False)

Calculate AveragedCrossspectrum from two arrays of event times.

Parameters:
times1np.array

Event arrival times of channel 1

times2np.array

Event arrival times of channel 2

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency)

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum.

gti[[gti0, gti1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

initial_checks(data1, segment_size=None, **kwargs)

Examples

>>> times = np.arange(0, 10)
>>> ev1 = EventList(times)
>>> ev2 = EventList(times)
>>> ac = AveragedCrossspectrum()

If AveragedCrossspectrum, you need segment_size >>> AveragedCrossspectrum.initial_checks(ac, data1=ev1, data2=ev2, dt=1) Traceback (most recent call last): … ValueError: segment_size must be specified…

And it needs to be finite! >>> AveragedCrossspectrum.initial_checks(ac, data1=ev1, data2=ev2, dt=1., segment_size=np.nan) Traceback (most recent call last): … ValueError: segment_size must be finite!

internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

intrinsic_coherence(adjust_bias=False)

Averaged Coherence function.

Intrinsic Coherence is defined in Vaughan and Nowak, 1997 [5], as

\[\tilde{\gamma}^2(f) = \frac{|\langle \tilde{C}(f) \rangle|^2 - \tilde{b}^2} {(\langle \tilde{P}_1(f) \rangle - \tilde{P}_{1, \rm noise}) (\langle \tilde{P}_2(f) \rangle - \tilde{P}_{2, \rm noise})}\]

where \(\tilde{b}^2\) is the bias term that accounts for the contribution of Poisson noise to the cross spectrum (see stingray.fourier.bias_term()), and tilde generally indicates noisy measurements of the cross spectrum \(C\) and the power spectra \(P_n\). The terms \(\tilde{P}_{n, \rm noise}\) are the estimates of the contribution of Poisson noise to the power spectra.

Note

This definition is strictly valid for Gaussian statistics only, meaning that the cross spectrum and the power spectra estimates have been obtained by averaging over at least 50 intervals. A warning will be raised if this is not the case

Parameters:
adjust_bias: bool, default False

If True, the bias term is adjusted to account for the fact that the bias term depends on the intrinsic coherence itself. This is done iteratively. See Ingram 2019, cited, for details on the bias term and its dependence on the intrinsic coherence, and the documentation of intrinsic_coherence().

Returns:
(coh, uncertainty)tuple of np.ndarray

Tuple comprising the coherence function and uncertainty.

References

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

phase_lag()

Return the fourier phase lag of the cross spectrum.

plot(labels=None, axis=None, title=None, marker='-', save=False, filename=None, ax=None)

Plot the amplitude of the cross spectrum vs. the frequency using matplotlib.

Parameters:
labelsiterable, default None

A list of tuple with xlabel and ylabel as strings.

axislist, tuple, string, default None

Parameter to set axis properties of the matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for the``matplotlib.pyplot.axis()`` method.

titlestr, default None

The title of the plot.

markerstr, default ‘-’

Line style and color of the plot. Line styles and colors are combined in a single format string, as in 'bo' for blue circles. See matplotlib.pyplot.plot for more options.

saveboolean, optional, default False

If True, save the figure with specified filename.

filenamestr

File name of the image to save. Depends on the boolean save.

axmatplotlib.Axes object

An axes object to fill with the cross correlation plot.

power_colors(freq_edges=[0.00390625, 0.03125, 0.25, 2.0, 16.0], freqs_to_exclude=None, poisson_power=0)[source]

Compute the power colors of the dynamical power spectrum.

See Heil et al. 2015, MNRAS, 448, 3348

Parameters:
freq_edges: iterable

The edges of the frequency bins to be used for the power colors.

freqs_to_exclude1-d or 2-d iterable, optional, default None

The ranges of frequencies to exclude from the calculation of the power color. For example, the frequencies containing strong QPOs. A 1-d iterable should contain two values for the edges of a single range. (E.g. [0.1, 0.2]). [[0.1, 0.2], [3, 4]] will exclude the ranges 0.1-0.2 Hz and 3-4 Hz.

poisson_powerfloat or iterable, optional, default 0

The Poisson noise level of the power spectrum. If iterable, it should have the same length as freq. (This might apply to the case of a power spectrum with a strong dead time distortion). If 2-D, it should have the same shape as dyn_ps, and each column is used as the Poisson level of the corresponding spectrum.

Returns:
pc0: np.ndarray
pc0_err: np.ndarray
pc1: np.ndarray
pc1_err: np.ndarray

The power colors for each spectrum and their respective errors

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

raw_coherence()

Averaged Raw Coherence function.

Coherence is a Fourier frequency dependent measure of the linear correlation between time series measured simultaneously in two energy channels.

The function is defined as (see Ingram 2019 [6] :

\[\tilde{g}^2(f) = \frac{|\langle \tilde{C}(f) \rangle|^2 - \tilde{b}^2} {\langle \tilde{P}_1(f) \rangle \langle \tilde{P}_2(f) \rangle}\]

where \(\tilde{b}^2\) is the bias term that accounts for the contribution of Poisson noise to the cross spectrum (see stingray.fourier.bias_term()), and tilde generally indicates noisy measurements of the cross spectrum \(C\) and the power spectra \(P_n\).

Note

This definition is strictly valid for Gaussian statistics only, meaning that the cross spectrum and the power spectra estimates have been obtained by averaging over at least 50 intervals. A warning will be raised if this is not the case

Returns:
(coh, uncertainty)tuple of np.ndarray

Tuple comprising the coherence function and uncertainty.

References

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

rebin(df=None, f=None, method='mean')

Rebin the cross spectrum to a new frequency resolution df.

Parameters:
df: float

The new frequency resolution

Returns:
bin_cs = Crossspectrum (or one of its subclasses) object

The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from AveragedPowerspectrum, it will return an object of class AveragedPowerspectrum, too.

Other Parameters:
f: float

the rebin factor. If specified, it substitutes df with f*self.df

rebin_by_n_intervals(n, method='average')[source]

Rebin the Dynamic Power Spectrum to a new time resolution, by summing contiguous intervals.

This is different from meth:DynamicalPowerspectrum.rebin_time in that it averages n consecutive intervals regardless of their distance in time. rebin_time will instead average intervals that are separated at most by a time dt_new.

Parameters:
n: int

The number of intervals to be combined into one.

method: {“sum” | “mean” | “average”}, optional, default “average”

This keyword argument sets whether the powers in the new bins should be summed or averaged.

Returns:
time_new: numpy.ndarray

Time axis with new rebinned time resolution.

dynspec_new: numpy.ndarray

New rebinned Dynamical Cross Spectrum.

rebin_frequency(df_new, method='average')[source]

Rebin the Dynamic Power Spectrum to a new frequency resolution. Rebinning is an in-place operation, i.e. will replace the existing dyn_ps attribute.

While the new resolution does not need to be an integer of the previous frequency resolution, be aware that if this is the case, the last frequency bin will be cut off by the fraction left over by the integer division

Parameters:
df_new: float

The new frequency resolution of the dynamical power spectrum. Must be larger than the frequency resolution of the old dynamical power spectrum!

method: {“sum” | “mean” | “average”}, optional, default “average”

This keyword argument sets whether the powers in the new bins should be summed or averaged.

rebin_log(f=0.01)

Logarithmic rebin of the periodogram. The new frequency depends on the previous frequency modified by a factor f:

\[d\nu_j = d\nu_{j-1} (1+f)\]
Parameters:
f: float, optional, default ``0.01``

parameter that steers the frequency resolution

Returns:
new_specCrossspectrum (or one of its subclasses) object

The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from AveragedPowerspectrum, it will return an object of class

rebin_time(dt_new, method='average')[source]

Rebin the Dynamic Power Spectrum to a new time resolution.

Note: this is not changing the time resolution of the input light curve! dt is the integration time of each line of the dynamical power spectrum (typically, an integer multiple of segment_size).

While the new resolution does not need to be an integer of the previous time resolution, be aware that if this is the case, the last time bin will be cut off by the fraction left over by the integer division

Parameters:
dt_new: float

The new time resolution of the dynamical power spectrum. Must be larger than the time resolution of the old dynamical power spectrum!

method: {“sum” | “mean” | “average”}, optional, default “average”

This keyword argument sets whether the powers in the new bins should be summed or averaged.

Returns:
time_new: numpy.ndarray

Time axis with new rebinned time resolution.

dynspec_new: numpy.ndarray

New rebinned Dynamical Cross Spectrum.

save_as_xspec(outroot, header_keywords=None)

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().

shift_and_add(f0_list, nbins=100, output_obj_type=<class 'stingray.crossspectrum.AveragedCrossspectrum'>, rebin=None)[source]

Shift and add the dynamical cross spectrum.

This is the basic operation for the shift-and-add operation used to track kHz QPOs in X-ray binaries (e.g. Méndez et al. 1998, ApJ, 494, 65).

Parameters:
freqsnp.array

Array of frequencies, the same for all powers. Must be sorted and on a uniform grid.

power_listlist of np.array

List of power spectra. Each power spectrum must have the same length as the frequency array.

f0_listlist of float

List of central frequencies

Returns:
output: AveragedPowerspectrum or AveragedCrossspectrum

The final averaged power spectrum.

Other Parameters:
nbinsint, default 100

Number of bins to extract

rebinint, default None

Rebin the final spectrum by this factor. At the moment, the rebinning is linear.

output_obj_typeclass, default AveragedCrossspectrum

The type of the output object. Can be, e.g. AveragedCrossspectrum or AveragedPowerspectrum.

Examples

>>> power_list = [[2, 5, 2, 2, 2], [1, 1, 5, 1, 1], [3, 3, 3, 5, 3]]
>>> power_list = np.array(power_list).T
>>> freqs = np.arange(5) * 0.1
>>> f0_list = [0.1, 0.2, 0.3, 0.4]
>>> dps = DynamicalCrossspectrum()
>>> dps.dyn_ps = power_list
>>> dps.freq = freqs
>>> dps.df = 0.1
>>> dps.m = 1
>>> output = dps.shift_and_add(f0_list, nbins=5)
>>> assert np.array_equal(output.m, [2, 3, 3, 3, 2])
>>> assert np.array_equal(output.power, [2. , 2. , 5. , 2. , 1.5])
>>> assert np.allclose(output.freq, [0.05, 0.15, 0.25, 0.35, 0.45])
sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

time_lag()

Calculate time lag and uncertainty.

Equation from Bendat & Piersol, 2011 [bendat-2011]__.

Returns:
lagnp.ndarray

The time lag

lag_errnp.ndarray

The uncertainty in the time lag

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_norm(norm, inplace=False)

Convert Cross spectrum to new normalization.

Parameters:
normstr

The new normalization of the spectrum

Returns:
new_specobject, same class as input

The new, normalized, spectrum.

Other Parameters:
inplace: bool, default False

If True, change the current instance. Otherwise, return a new one

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

trace_maximum(min_freq=None, max_freq=None)[source]

Return the indices of the maximum powers in each segment Powerspectrum between specified frequencies.

Parameters:
min_freq: float, default ``None``

The lower frequency bound.

max_freq: float, default ``None``

The upper frequency bound.

Returns:
max_positionsnp.array

The array of indices of the maximum power in each segment having frequency between min_freq and max_freq.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


Dynamical Powerspectrum

class stingray.DynamicalPowerspectrum(lc=None, segment_size=None, norm='frac', gti=None, sample_time=None, use_common_mean=True)[source]

Create a dynamical power spectrum, also often called a spectrogram.

This class will divide a Lightcurve object into segments of length segment_size, create a power spectrum for each segment and store all powers in a matrix as a function of both time (using the mid-point of each segment) and frequency.

This is often used to trace changes in period of a (quasi-)periodic signal over time.

Parameters:
lcstingray.Lightcurve or EventList object

The time series or event list of which the dynamical power spectrum is to be calculated. If EventList, dt must be specified as well.

segment_sizefloat, default 1

Length of the segment of light curve, default value is 1 (in whatever units the time array in the Lightcurve object uses).

norm: {“leahy” | “frac” | “abs” | “none” }, optional, default “frac”

The normalization of the periodogram to be used. The mean count rate that enters the normalization is selected with use_common_mean.

Attributes:
segment_size: float

The size of each segment to average. Note that if the total duration of each input object in lc is not an integer multiple of the segment_size, then any fraction left-over at the end of the time series will be lost.

dyn_psnp.ndarray

The matrix of normalized squared absolute values of Fourier amplitudes. The axis are given by the freq and time attributes.

norm: {``leahy`` | ``frac`` | ``abs`` | ``none``}

The normalization of the periodogram.

freq: numpy.ndarray

The array of mid-bin frequencies that the Fourier transform samples.

time: numpy.ndarray

The array of mid-point times of each interval used for the dynamical power spectrum.

df: float

The frequency resolution.

dt: float

The time resolution of the dynamical spectrum. It is not the time resolution of the input light curve. It is the integration time of each line of the dynamical power spectrum (typically, an integer multiple of segment_size).

m: int

The number of averaged cross spectra.

use_common_mean: bool

Whether the normalization uses the mean count rate of the whole observation (True) or of each segment (False).

poisson_power: numpy.ndarray or None

The Poisson noise level of each segment, in the normalization of dyn_ps, if use_common_mean is False. None otherwise.

nphots_all: numpy.ndarray

The number of photons in each segment.

Other Parameters:
gti: 2-d float array

[[gti0_0, gti0_1], [gti1_0, gti1_1], ...] – Good Time intervals. This choice overrides the GTIs in the single light curves. Use with care, especially if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input object before making the power spectrum.

sample_time: float

Compulsory for input EventList data. The time resolution of the lightcurve that is created internally from the input event lists. Drives the Nyquist frequency.

use_common_mean: bool, default True

Select the mean count rate used to normalize each segment. If True, every segment is normalized by the mean count rate of the whole observation, so a change of count rate between segments shows up in the normalized powers. If False, each segment is normalized by its own mean count rate. The latter is usually what one wants when comparing the columns of a dynamical spectrum with each other: with norm="frac" it removes the effect of a varying count rate at constant fractional rms, as produced by an rms-flux relation. Note that with use_common_mean set to False the Poisson noise level follows each segment’s own count rate, so a single noise level no longer applies to the whole matrix. The level of each segment is stored in poisson_power, while meanrate remains the mean over the whole observation. compute_rms returns the fractional rms of each segment relative to its own mean count rate.

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

classical_significances(threshold=1, trial_correction=False)

Compute the classical significances for the powers in the power spectrum, assuming an underlying noise distribution that follows a chi-square distributions with 2M degrees of freedom, where M is the number of powers averaged in each bin.

Note that this function will only produce correct results when the following underlying assumptions are fulfilled:

  1. The power spectrum is Leahy-normalized

  2. There is no source of variability in the data other than the periodic signal to be determined with this method. This is important! If there are other sources of (aperiodic) variability in the data, this method will not produce correct results, but instead produce a large number of spurious false positive detections!

  3. There are no significant instrumental effects changing the statistical distribution of the powers (e.g. pile-up or dead time)

By default, the method produces (index,p-values) for all powers in the power spectrum, where index is the numerical index of the power in question. If a threshold is set, then only powers with p-values below that threshold with their respective indices. If trial_correction is set to True, then the threshold will be corrected for the number of trials (frequencies) in the power spectrum before being used.

Parameters:
thresholdfloat, optional, default 1

The threshold to be used when reporting p-values of potentially significant powers. Must be between 0 and 1. Default is 1 (all p-values will be reported).

trial_correctionbool, optional, default False

A Boolean flag that sets whether the threshold will be corrected by the number of frequencies before being applied. This decreases the threshold (p-values need to be lower to count as significant). Default is False (report all powers) though for any application where threshold is set to something meaningful, this should also be applied!

Returns:
pvalsiterable

A list of (index, p-value) tuples for all powers that have p-values lower than the threshold specified in threshold.

coherence()

Averaged Raw Coherence function.

Coherence is a Fourier frequency dependent measure of the linear correlation between time series measured simultaneously in two energy channels.

Note

This function is deprecated. Please use raw_coherence() or intrinsic_coherence() instead, depending on your needs.

NoteThe uncertainty in coherence function is strictly valid for Gaussian

statistics only.

Returns:
(coh, uncertainty)tuple of np.ndarray

Tuple comprising the coherence function and uncertainty.

compute_rms(min_freq, max_freq, poisson_noise_level=0)

Compute the fractional rms amplitude in the power spectrum between two frequencies.

Parameters:
min_freq: float

The lower frequency bound for the calculation.

max_freq: float

The upper frequency bound for the calculation.

Returns:
rms: float

The fractional rms amplitude contained between min_freq and max_freq. If use_common_mean is True, it is relative to the mean count rate of the whole observation, otherwise to the mean count rate of each segment.

rms_err: float

The error on the fractional rms amplitude.

Other Parameters:
poisson_noise_levelfloat or iterable, default 0

This is the Poisson noise level of the PDS with same normalization as the PDS. If iterable, it should contain one level per spectrum, e.g. poisson_power of a DynamicalPowerspectrum with use_common_mean=False.

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

deadtime_correct(dead_time, rate, background_rate=0, limit_k=200, n_approx=None, paralyzable=False)

Correct the power spectrum for dead time effects.

This correction is based on the formula given in Zhang et al. 2015, assuming a constant dead time for all events. For more advanced dead time corrections, see the FAD method from stingray.deadtime.fad

Parameters:
dead_time: float

The dead time of the detector.

ratefloat

Detected source count rate

Returns:
spectrum: Crossspectrum or derivative.

The dead-time corrected spectrum.

Other Parameters:
background_ratefloat, default 0

Detected background count rate. This is important to estimate when deadtime is given by the combination of the source counts and background counts (e.g. in an imaging X-ray detector).

paralyzable: bool, default False

If True, the dead time correction is done assuming a paralyzable dead time. If False, the correction is done assuming a non-paralyzable (more common) dead time.

limit_kint, default 200

Limit to this value the number of terms in the inner loops of calculations. Check the plots returned by the check_B() and check_A() functions to test that this number is adequate.

n_approxint, default None

Number of bins to calculate the model power spectrum. If None, it will use the size of the input frequency array. Relatively simple models (e.g., low count rates compared to dead time) can use a smaller number of bins to speed up the calculation, and the final power values will be interpolated.

dict() → dict

Return a dictionary representation of the object.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

static from_events(events1, events2, dt, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)

Calculate AveragedCrossspectrum from two event lists

Parameters:
events1EventList

Events from channel 1

events2EventList

Events from channel 2

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency)

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

static from_lc_iterable(iter_lc1, iter_lc2, dt, segment_size, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)

Calculate AveragedCrossspectrum from two light curves

Parameters:
iter_lc1iterable of stingray.Lightcurve objects or np.array

Light curves from channel 1. If arrays, use them as counts

iter_lc1iterable of stingray.Lightcurve objects or np.array

Light curves from channel 2. If arrays, use them as counts

dtfloat

The time resolution of the light curves (sets the Nyquist frequency)

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

save_allbool, default False

If True, save the cross spectrum of each segment in the cs_all attribute of the output Crossspectrum object.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

static from_lightcurve(lc1, lc2, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)

Calculate AveragedCrossspectrum from two light curves

Parameters:
lc1stingray.Lightcurve

Light curve from channel 1

lc2stingray.Lightcurve

Light curve from channel 2

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

static from_stingray_timeseries(ts1, ts2, flux_attr, error_flux_attr=None, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)

Calculate AveragedCrossspectrum from two light curves

Parameters:
ts1StingrayTimeseries

Time series from channel 1

ts2StingrayTimeseries

Time series from channel 2

flux_attrstr

What attribute of the time series will be used.

Other Parameters:
error_flux_attrstr

What attribute of the time series will be used as error bar.

segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

static from_time_array(times1, times2, dt, segment_size=None, gti=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, channels_overlap=False)

Calculate AveragedCrossspectrum from two arrays of event times.

Parameters:
times1np.array

Event arrival times of channel 1

times2np.array

Event arrival times of channel 2

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency)

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum.

gti[[gti0, gti1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

initial_checks(data1, segment_size=None, **kwargs)

Examples

>>> times = np.arange(0, 10)
>>> ev1 = EventList(times)
>>> ev2 = EventList(times)
>>> ac = AveragedCrossspectrum()

If AveragedCrossspectrum, you need segment_size >>> AveragedCrossspectrum.initial_checks(ac, data1=ev1, data2=ev2, dt=1) Traceback (most recent call last): … ValueError: segment_size must be specified…

And it needs to be finite! >>> AveragedCrossspectrum.initial_checks(ac, data1=ev1, data2=ev2, dt=1., segment_size=np.nan) Traceback (most recent call last): … ValueError: segment_size must be finite!

internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

intrinsic_coherence(adjust_bias=False)

Averaged Coherence function.

Intrinsic Coherence is defined in Vaughan and Nowak, 1997 [7], as

\[\tilde{\gamma}^2(f) = \frac{|\langle \tilde{C}(f) \rangle|^2 - \tilde{b}^2} {(\langle \tilde{P}_1(f) \rangle - \tilde{P}_{1, \rm noise}) (\langle \tilde{P}_2(f) \rangle - \tilde{P}_{2, \rm noise})}\]

where \(\tilde{b}^2\) is the bias term that accounts for the contribution of Poisson noise to the cross spectrum (see stingray.fourier.bias_term()), and tilde generally indicates noisy measurements of the cross spectrum \(C\) and the power spectra \(P_n\). The terms \(\tilde{P}_{n, \rm noise}\) are the estimates of the contribution of Poisson noise to the power spectra.

Note

This definition is strictly valid for Gaussian statistics only, meaning that the cross spectrum and the power spectra estimates have been obtained by averaging over at least 50 intervals. A warning will be raised if this is not the case

Parameters:
adjust_bias: bool, default False

If True, the bias term is adjusted to account for the fact that the bias term depends on the intrinsic coherence itself. This is done iteratively. See Ingram 2019, cited, for details on the bias term and its dependence on the intrinsic coherence, and the documentation of intrinsic_coherence().

Returns:
(coh, uncertainty)tuple of np.ndarray

Tuple comprising the coherence function and uncertainty.

References

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

phase_lag()

Return the fourier phase lag of the cross spectrum.

plot(labels=None, axis=None, title=None, marker='-', save=False, filename=None, ax=None)

Plot the amplitude of the cross spectrum vs. the frequency using matplotlib.

Parameters:
labelsiterable, default None

A list of tuple with xlabel and ylabel as strings.

axislist, tuple, string, default None

Parameter to set axis properties of the matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for the``matplotlib.pyplot.axis()`` method.

titlestr, default None

The title of the plot.

markerstr, default ‘-’

Line style and color of the plot. Line styles and colors are combined in a single format string, as in 'bo' for blue circles. See matplotlib.pyplot.plot for more options.

saveboolean, optional, default False

If True, save the figure with specified filename.

filenamestr

File name of the image to save. Depends on the boolean save.

axmatplotlib.Axes object

An axes object to fill with the cross correlation plot.

power_colors(freq_edges=[0.00390625, 0.03125, 0.25, 2.0, 16.0], freqs_to_exclude=None, poisson_power=None)[source]

Return the power colors of the dynamical power spectrum.

Parameters:
freq_edges: iterable

The edges of the frequency bins to be used for the power colors.

freqs_to_exclude1-d or 2-d iterable, optional, default None

The ranges of frequencies to exclude from the calculation of the power color. For example, the frequencies containing strong QPOs. A 1-d iterable should contain two values for the edges of a single range. (E.g. [0.1, 0.2]). [[0.1, 0.2], [3, 4]] will exclude the ranges 0.1-0.2 Hz and 3-4 Hz.

poisson_powerfloat or iterable, optional

Defaults to the theoretical Poisson noise level (e.g. 2 for Leahy normalization), calculated for each segment if use_common_mean is False. The Poisson noise level of the power spectrum. If iterable, it should have the same length as frequency. (This might apply to the case of a power spectrum with a strong dead time distortion). If 2-D, it should have the same shape as dyn_ps.

Returns:
pc0: np.ndarray
pc0_err: np.ndarray
pc1: np.ndarray
pc1_err: np.ndarray

The power colors for each spectrum and their respective errors

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

raw_coherence()

Averaged Raw Coherence function.

Coherence is a Fourier frequency dependent measure of the linear correlation between time series measured simultaneously in two energy channels.

The function is defined as (see Ingram 2019 [8] :

\[\tilde{g}^2(f) = \frac{|\langle \tilde{C}(f) \rangle|^2 - \tilde{b}^2} {\langle \tilde{P}_1(f) \rangle \langle \tilde{P}_2(f) \rangle}\]

where \(\tilde{b}^2\) is the bias term that accounts for the contribution of Poisson noise to the cross spectrum (see stingray.fourier.bias_term()), and tilde generally indicates noisy measurements of the cross spectrum \(C\) and the power spectra \(P_n\).

Note

This definition is strictly valid for Gaussian statistics only, meaning that the cross spectrum and the power spectra estimates have been obtained by averaging over at least 50 intervals. A warning will be raised if this is not the case

Returns:
(coh, uncertainty)tuple of np.ndarray

Tuple comprising the coherence function and uncertainty.

References

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

rebin(df=None, f=None, method='mean')

Rebin the cross spectrum to a new frequency resolution df.

Parameters:
df: float

The new frequency resolution

Returns:
bin_cs = Crossspectrum (or one of its subclasses) object

The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from AveragedPowerspectrum, it will return an object of class AveragedPowerspectrum, too.

Other Parameters:
f: float

the rebin factor. If specified, it substitutes df with f*self.df

rebin_by_n_intervals(n, method='average')

Rebin the Dynamic Power Spectrum to a new time resolution, by summing contiguous intervals.

This is different from meth:DynamicalPowerspectrum.rebin_time in that it averages n consecutive intervals regardless of their distance in time. rebin_time will instead average intervals that are separated at most by a time dt_new.

Parameters:
n: int

The number of intervals to be combined into one.

method: {“sum” | “mean” | “average”}, optional, default “average”

This keyword argument sets whether the powers in the new bins should be summed or averaged.

Returns:
time_new: numpy.ndarray

Time axis with new rebinned time resolution.

dynspec_new: numpy.ndarray

New rebinned Dynamical Cross Spectrum.

rebin_frequency(df_new, method='average')

Rebin the Dynamic Power Spectrum to a new frequency resolution. Rebinning is an in-place operation, i.e. will replace the existing dyn_ps attribute.

While the new resolution does not need to be an integer of the previous frequency resolution, be aware that if this is the case, the last frequency bin will be cut off by the fraction left over by the integer division

Parameters:
df_new: float

The new frequency resolution of the dynamical power spectrum. Must be larger than the frequency resolution of the old dynamical power spectrum!

method: {“sum” | “mean” | “average”}, optional, default “average”

This keyword argument sets whether the powers in the new bins should be summed or averaged.

rebin_log(f=0.01)

Logarithmic rebin of the periodogram. The new frequency depends on the previous frequency modified by a factor f:

\[d\nu_j = d\nu_{j-1} (1+f)\]
Parameters:
f: float, optional, default ``0.01``

parameter that steers the frequency resolution

Returns:
new_specCrossspectrum (or one of its subclasses) object

The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from AveragedPowerspectrum, it will return an object of class

rebin_time(dt_new, method='average')

Rebin the Dynamic Power Spectrum to a new time resolution.

Note: this is not changing the time resolution of the input light curve! dt is the integration time of each line of the dynamical power spectrum (typically, an integer multiple of segment_size).

While the new resolution does not need to be an integer of the previous time resolution, be aware that if this is the case, the last time bin will be cut off by the fraction left over by the integer division

Parameters:
dt_new: float

The new time resolution of the dynamical power spectrum. Must be larger than the time resolution of the old dynamical power spectrum!

method: {“sum” | “mean” | “average”}, optional, default “average”

This keyword argument sets whether the powers in the new bins should be summed or averaged.

Returns:
time_new: numpy.ndarray

Time axis with new rebinned time resolution.

dynspec_new: numpy.ndarray

New rebinned Dynamical Cross Spectrum.

save_as_xspec(outroot, header_keywords=None)

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().

shift_and_add(f0_list, nbins=100, rebin=None)[source]

Shift-and-add the dynamical power spectrum.

This is the basic operation for the shift-and-add operation used to track kHz QPOs in X-ray binaries (e.g. Méndez et al. 1998, ApJ, 494, 65).

Parameters:
freqsnp.array

Array of frequencies, the same for all powers. Must be sorted and on a uniform grid.

power_listlist of np.array

List of power spectra. Each power spectrum must have the same length as the frequency array.

f0_listlist of float

List of central frequencies

Returns:
output: AveragedPowerspectrum

The final averaged power spectrum.

Other Parameters:
nbinsint, default 100

Number of bins to extract

rebinint, default None

Rebin the final spectrum by this factor. At the moment, the rebinning is linear.

Examples

>>> power_list = [[2, 5, 2, 2, 2], [1, 1, 5, 1, 1], [3, 3, 3, 5, 3]]
>>> power_list = np.array(power_list).T
>>> freqs = np.arange(5) * 0.1
>>> f0_list = [0.1, 0.2, 0.3, 0.4]
>>> dps = DynamicalPowerspectrum()
>>> dps.dyn_ps = power_list
>>> dps.freq = freqs
>>> dps.df = 0.1
>>> dps.m = 1
>>> output = dps.shift_and_add(f0_list, nbins=5)
>>> assert isinstance(output, AveragedPowerspectrum)
>>> assert np.array_equal(output.m, [2, 3, 3, 3, 2])
>>> assert np.array_equal(output.power, [2. , 2. , 5. , 2. , 1.5])
>>> assert np.allclose(output.freq, [0.05, 0.15, 0.25, 0.35, 0.45])
sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

time_lag()

Calculate time lag and uncertainty.

Equation from Bendat & Piersol, 2011 [bendat-2011]__.

Returns:
lagnp.ndarray

The time lag

lag_errnp.ndarray

The uncertainty in the time lag

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_norm(norm, inplace=False)

Convert Cross spectrum to new normalization.

Parameters:
normstr

The new normalization of the spectrum

Returns:
new_specobject, same class as input

The new, normalized, spectrum.

Other Parameters:
inplace: bool, default False

If True, change the current instance. Otherwise, return a new one

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

trace_maximum(min_freq=None, max_freq=None)

Return the indices of the maximum powers in each segment Powerspectrum between specified frequencies.

Parameters:
min_freq: float, default ``None``

The lower frequency bound.

max_freq: float, default ``None``

The upper frequency bound.

Returns:
max_positionsnp.array

The array of indices of the maximum power in each segment having frequency between min_freq and max_freq.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


GtiCorrPowerspectrum

class stingray.GtiCorrPowerspectrum(*args, fill_lc=True, sigma_threshold=3, **kwargs)[source]

Calculate the power spectrum of gappy light curves.

GtiCorrPowerspectrum computes the power spectrum of gappy light curves, cleaning up the frequencies that are more affected by gaps. Optionally, it fills bad time intervals (BTIs) with the mean count rate from good time intervals (GTIs), mitigating window-induced features in the periodogram. By analyzing the visibility light curve (synthetic light curve with constant mean counts in GTIs), the class identifies strong peaks in the periodogram that correspond to the missing data and applies notch filtering to the power spectrum to remove these frequencies. Additionally, it rescales the power spectrum to account for the number of bins in and out GTIs, ensuring accurate white noise and rms estimation.

The detailed explanation of the method is given in El Byad et al. 2025

Parameters:
*args:

Any arguments that can be passed to Powerspectrum

Attributes:
freq: numpy.ndarray

The array of mid-bin frequencies that the Fourier transform samples

power: numpy.ndarray

The array of power values

power_err: numpy.ndarray

The uncertainties of power. An approximation for each bin given by power_err= power/sqrt(m). Where m is the number of power averaged in each bin (by frequency binning, or averaging more than one spectra). Note that for a single realization (m=1) the error is equal to the power.

df: float

The frequency resolution

m: int

The number of averaged cross-spectra amplitudes in each bin.

n: int

The number of data points/time bins in one segment of the light curves.

k: array of int

The rebinning scheme if the object has been rebinned otherwise is set to 1.

nphots: float

The total number of photons in light curve

Other Parameters:
fill_lc: boolean, optional, default ``True``

If True, fill the BTIs of the light curve with the mean value of the counts in the GTIs. Recommended.

sigma_threshold: float, optional, default ``3``

The sigma threshold for the detection of features in the power spectrum of the observing window.

**kwargs:

Any other keyword arguments that can be passed to Powerspectrum

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

classical_significances(threshold=1, trial_correction=False)

Compute the classical significances for the powers in the power spectrum, assuming an underlying noise distribution that follows a chi-square distributions with 2M degrees of freedom, where M is the number of powers averaged in each bin.

Note that this function will only produce correct results when the following underlying assumptions are fulfilled:

  1. The power spectrum is Leahy-normalized

  2. There is no source of variability in the data other than the periodic signal to be determined with this method. This is important! If there are other sources of (aperiodic) variability in the data, this method will not produce correct results, but instead produce a large number of spurious false positive detections!

  3. There are no significant instrumental effects changing the statistical distribution of the powers (e.g. pile-up or dead time)

By default, the method produces (index,p-values) for all powers in the power spectrum, where index is the numerical index of the power in question. If a threshold is set, then only powers with p-values below that threshold with their respective indices. If trial_correction is set to True, then the threshold will be corrected for the number of trials (frequencies) in the power spectrum before being used.

Parameters:
thresholdfloat, optional, default 1

The threshold to be used when reporting p-values of potentially significant powers. Must be between 0 and 1. Default is 1 (all p-values will be reported).

trial_correctionbool, optional, default False

A Boolean flag that sets whether the threshold will be corrected by the number of frequencies before being applied. This decreases the threshold (p-values need to be lower to count as significant). Default is False (report all powers) though for any application where threshold is set to something meaningful, this should also be applied!

Returns:
pvalsiterable

A list of (p-value, index) tuples for all powers that have p-values lower than the threshold specified in threshold.

coherence()

Compute Raw Coherence function of the cross spectrum.

Deprecated. Please use the raw_coherence() method of AveragedCrossspectrum, with the correct parameters.

Returns:
cohnumpy.ndarray

Coherence function

compute_rms(min_freq, max_freq, poisson_noise_level=None)

Compute the fractional rms amplitude in the power spectrum between two frequencies.

Parameters:
min_freq: float

The lower frequency bound for the calculation.

max_freq: float

The upper frequency bound for the calculation.

Returns:
rms: float

The fractional rms amplitude contained between min_freq and max_freq.

rms_err: float

The error on the fractional rms amplitude.

Other Parameters:
poisson_noise_levelfloat, default is None

This is the Poisson noise level of the PDS with same normalization as the PDS. If poissoin_noise_level is None, the Poisson noise is calculated in the idealcase e.g. 2./<countrate> for fractional rms normalisation Dead time and other instrumental effects can alter it. The user can fit the Poisson noise level outside this function using the same normalisation of the PDS and it will get subtracted from powers here.

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

deadtime_correct(dead_time, rate, background_rate=0, limit_k=200, n_approx=None, paralyzable=False)

Correct the power spectrum for dead time effects.

This correction is based on the formula given in Zhang et al. 2015, assuming a constant dead time for all events. For more advanced dead time corrections, see the FAD method from stingray.deadtime.fad

Parameters:
dead_time: float

The dead time of the detector.

ratefloat

Detected source count rate

Returns:
spectrum: Crossspectrum or derivative.

The dead-time corrected spectrum.

Other Parameters:
background_ratefloat, default 0

Detected background count rate. This is important to estimate when deadtime is given by the combination of the source counts and background counts (e.g. in an imaging X-ray detector).

paralyzable: bool, default False

If True, the dead time correction is done assuming a paralyzable dead time. If False, the correction is done assuming a non-paralyzable (more common) dead time.

limit_kint, default 200

Limit to this value the number of terms in the inner loops of calculations. Check the plots returned by the check_B() and check_A() functions to test that this number is adequate.

n_approxint, default None

Number of bins to calculate the model power spectrum. If None, it will use the size of the input frequency array. Relatively simple models (e.g., low count rates compared to dead time) can use a smaller number of bins to speed up the calculation, and the final power values will be interpolated.

dict() → dict

Return a dictionary representation of the object.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

static from_events(events, dt, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)

Calculate an average power spectrum from an event list.

Parameters:
eventsEventList

Event list to be analyzed.

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency).

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedPowerspectrum.

gti: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average.

static from_lc_iterable(iter_lc, dt, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)

Calculate the average power spectrum of an iterable collection of light curves.

Parameters:
iter_lciterable of stingray.Lightcurve objects or np.array

Light curves. If arrays, use them as counts.

dtfloat

The time resolution of the light curves (sets the Nyquist frequency)

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedPowerspectrum.

gti: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average.

static from_lightcurve(lc, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)

Calculate a power spectrum from a light curve.

Parameters:
eventsstingray.Lightcurve

Light curve to be analyzed.

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency).

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedPowerspectrum.

gti: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

static from_stingray_timeseries(ts, flux_attr, error_flux_attr=None, segment_size=None, norm='none', silent=False, use_common_mean=True, gti=None, save_all=False)

Calculate AveragedPowerspectrum from a time series.

Parameters:
tsStingrayTimeseries

Input Time Series

flux_attrstr

What attribute of the time series will be used.

Other Parameters:
error_flux_attrstr

What attribute of the time series will be used as error bar.

segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

save_allbool, default False

Save all intermediate PDSs used for the final average.

static from_time_array(times, dt, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True)

Calculate an average power spectrum from an array of event times.

Parameters:
timesnp.array

Event arrival times.

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency).

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedPowerspectrum.

gti: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

initial_checks(*args, **kwargs)[source]

Run initial checks on the input.

Returns True if checks are passed, False if they are not.

Raises various errors for different bad inputs

Examples

>>> times = np.arange(0, 10)
>>> counts = np.random.poisson(100, 10)
>>> lc1 = Lightcurve(times, counts, skip_checks=True)
>>> lc2 = Lightcurve(times, counts, skip_checks=True)
>>> ev1 = EventList(times)
>>> ev2 = EventList(times)
>>> c = Crossspectrum()
>>> ac = AveragedCrossspectrum()

If norm is not a string, raise a TypeError >>> Crossspectrum.initial_checks(c, norm=1) Traceback (most recent call last): … TypeError: norm must be a string…

If norm is not one of the valid norms, raise a ValueError >>> Crossspectrum.initial_checks(c, norm=”blabla”) Traceback (most recent call last): … ValueError: norm must be ‘frac’…

If power_type is not one of the valid norms, raise a ValueError >>> Crossspectrum.initial_checks(c, power_type=”blabla”) Traceback (most recent call last): … ValueError: power_type not recognized!

If the user passes only one light curve, raise a ValueError

>>> Crossspectrum.initial_checks(c, data1=lc1, data2=None)
Traceback (most recent call last):
...
ValueError: You can't do a cross spectrum...

If the user passes an event list without dt, raise a ValueError

>>> Crossspectrum.initial_checks(c, data1=ev1, data2=ev2, dt=None)
Traceback (most recent call last):
...
ValueError: If using event lists, please specify...
internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

modulation_upper_limit(fmin=None, fmax=None, c=0.95)

Upper limit on a sinusoidal modulation.

To understand the meaning of this amplitude: if the modulation is described by:

..math:: p = overline{p} (1 + a * sin(x))

this function returns a.

If it is a sum of sinusoidal harmonics instead ..math:: p = overline{p} (1 + sum_l a_l * sin(lx)) a is equivalent to \(\sqrt(\sum_l a_l^2)\).

See stingray.stats.power_upper_limit, stingray.stats.amplitude_upper_limit for more information.

The formula used to calculate the upper limit assumes the Leahy normalization. If the periodogram is in another normalization, we will internally convert it to Leahy before calculating the upper limit.

Parameters:
fmin: float

The minimum frequency to search (defaults to the first nonzero bin)

fmax: float

The maximum frequency to search (defaults to the Nyquist frequency)

Returns:
a: float

The modulation amplitude that could produce P>pmeas with 1 - c probability.

Other Parameters:
c: float

The confidence value for the upper limit (e.g. 0.95 = 95%)

Examples

>>> pds = Powerspectrum()
>>> pds.norm = "leahy"
>>> pds.freq = np.arange(0., 5.)
>>> # Note: this pds has 40 as maximum value between 2 and 5 Hz
>>> pds.power = np.array([100000, 1, 1, 40, 1])
>>> pds.m = 1
>>> pds.nphots = 30000
>>> assert np.isclose(
...     pds.modulation_upper_limit(fmin=2, fmax=5, c=0.99),
...     0.10164,
...     atol=0.0001)
phase_lag()

Calculate the fourier phase lag of the cross spectrum.

This is defined as the argument of the complex cross spectrum, and gives the delay at all frequencies, in cycles, of one input light curve with respect to the other.

plot(labels=None, axis=None, title=None, marker='-', save=False, filename=None, ax=None)

Plot the amplitude of the cross spectrum vs. the frequency using matplotlib.

Parameters:
labelsiterable, default None

A list of tuple with xlabel and ylabel as strings.

axislist, tuple, string, default None

Parameter to set axis properties of the matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for the``matplotlib.pyplot.axis()`` method.

titlestr, default None

The title of the plot.

markerstr, default ‘-’

Line style and color of the plot. Line styles and colors are combined in a single format string, as in 'bo' for blue circles. See matplotlib.pyplot.plot for more options.

saveboolean, optional, default False

If True, save the figure with specified filename.

filenamestr

File name of the image to save. Depends on the boolean save.

axmatplotlib.Axes object

An axes object to fill with the cross correlation plot.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

rebin(df=None, f=None, method='mean')

Rebin the power spectrum.

Parameters:
df: float

The new frequency resolution.

Returns:
bin_cs = Powerspectrum object

The newly binned power spectrum.

Other Parameters:
f: float

The rebin factor. If specified, it substitutes df with f*self.df, so f>1 is recommended.

rebin_log(*args, **kwargs)[source]

Rebin the power spectrum logarithmically and filter out NaN values.

This method overrides the parent class’s rebin_log method by applying a mask to remove any bins where the rebinned power is NaN.

Parameters:
*argstuple

Positional arguments passed to the parent class’s rebin_log method.

**kwargsdict

Keyword arguments passed to the parent class’s rebin_log method.

Returns:
GtiCorrPowerspectrum

A new power spectrum object with rebinned frequencies and powers, with NaN values filtered out.

save_as_xspec(outroot, header_keywords=None)

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

time_lag()

Calculate the fourier time lag of the cross spectrum. The time lag is calculated by taking the phase lag \(\phi\) and

..math:

\tau = \frac{\phi}{\two pi \nu}

where \(\nu\) is the center of the frequency bins.

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_norm(norm, inplace=False)

Convert Cross spectrum to new normalization.

Parameters:
normstr

The new normalization of the spectrum

Returns:
new_specobject, same class as input

The new, normalized, spectrum.

Other Parameters:
inplace: bool, default False

If True, change the current instance. Otherwise, return a new one

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


Multitaper

class stingray.multitaper.Multitaper(data=None, norm='frac', gti=None, dt=None, lc=None, NW=4, adaptive=False, jackknife=True, low_bias=True, lombscargle=False)[source]

Class to calculate the multitaper periodogram from lightcurve data.

Parameters:
data: :class:`stingray.Lightcurve` object, optional, default ``None``

The light curve data to be Fourier-transformed.

norm: {``leahy`` | ``frac`` | ``abs`` | ``none`` }, optional, default ``frac``

The normaliation of the power spectrum to be used. Options are leahy, frac, abs and none, default is frac.

NW: float, optional, default ``4``

The normalized half-bandwidth of the data tapers, indicating a multiple of the fundamental frequency of the DFT (Fs/N). Common choices are n/2, for n >= 4.

adaptive: boolean, optional, default ``False``

Use an adaptive weighting routine to combine the PSD estimates of different tapers.

jackknife: boolean, optional, default ``True``

Use the jackknife method to make an estimate of the PSD variance at each point.

low_bias: boolean, optional, default ``True``

Rather than use 2NW tapers, only use the tapers that have better than 90% spectral concentration within the bandwidth (still using a maximum of 2NW tapers)

lombscargle: boolean, optional, default ``False``

Whether to use the Lomb (1976) Scargle (1982) periodogram when calculating the Multitaper spectral estimate. Highly recommended for unevenly sampled time-series. Adaptive weighting and jack-knife estimated variance are yet not supported.

Attributes:
norm: {``leahy`` | ``frac`` | ``abs`` | ``none`` }

the normalization of the power spectrun

freq: numpy.ndarray

The array of mid-bin frequencies that the Fourier transform samples

power: numpy.ndarray

The array of normalized squared absolute values of Fourier amplitudes

unnorm_power: numpy.ndarray

The array of unnormalized values of Fourier amplitudes

multitaper_norm_power: numpy.ndarray

The array of normalized values of Fourier amplitudes, normalized according to the scheme followed in nitime, that is, by the length and the sampling frequency.

power_err: numpy.ndarray

The uncertainties of power. An approximation for each bin given by power_err= power/sqrt(m). Where m is the number of power averaged in each bin (by frequency binning, or averaging power spectrum). Note that for a single realization (m=1) the error is equal to the power.

df: float

The frequency resolution

m: int

The number of averaged powers in each bin

n: int

The number of data points in the light curve

k: array of int

The rebinning scheme if the object has been rebinned otherwise is set to 1.

nphots: float

The total number of photons in the light curve

jk_var_deg_freedom: numpy.ndarray

Array differs depending on whether the jackknife was used. It is either

  • The jackknife estimated variance of the log-psd, OR

  • The degrees of freedom in a chi2 model of how the estimated PSD is distributed about the true log-PSD (this is either 2*floor(2*NW), or calculated from adaptive weights)

Other Parameters:
gti: 2-d float array

[[gti0_0, gti0_1], [gti1_0, gti1_1], ...] – Good Time intervals. This choice overrides the GTIs in the single light curves. Use with care!

Notes

The bandwidth of the windowing function will determine the number of tapers to use. This parameter (NW) represents trade-off between frequency resolution (lower main lobe BW for the taper) and variance reduction (higher BW and number of averaged estimates). Typically, the number of tapers is calculated as 2x the bandwidth-to-fundamental-frequency ratio (NW), as these eigenfunctions have the best energy concentration.

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

classical_significances(threshold=1, trial_correction=False)

Compute the classical significances for the powers in the power spectrum, assuming an underlying noise distribution that follows a chi-square distributions with 2M degrees of freedom, where M is the number of powers averaged in each bin.

Note that this function will only produce correct results when the following underlying assumptions are fulfilled:

  1. The power spectrum is Leahy-normalized

  2. There is no source of variability in the data other than the periodic signal to be determined with this method. This is important! If there are other sources of (aperiodic) variability in the data, this method will not produce correct results, but instead produce a large number of spurious false positive detections!

  3. There are no significant instrumental effects changing the statistical distribution of the powers (e.g. pile-up or dead time)

By default, the method produces (index,p-values) for all powers in the power spectrum, where index is the numerical index of the power in question. If a threshold is set, then only powers with p-values below that threshold with their respective indices. If trial_correction is set to True, then the threshold will be corrected for the number of trials (frequencies) in the power spectrum before being used.

Parameters:
thresholdfloat, optional, default 1

The threshold to be used when reporting p-values of potentially significant powers. Must be between 0 and 1. Default is 1 (all p-values will be reported).

trial_correctionbool, optional, default False

A Boolean flag that sets whether the threshold will be corrected by the number of frequencies before being applied. This decreases the threshold (p-values need to be lower to count as significant). Default is False (report all powers) though for any application where threshold is set to something meaningful, this should also be applied!

Returns:
pvalsiterable

A list of (p-value, index) tuples for all powers that have p-values lower than the threshold specified in threshold.

coherence()

Compute Raw Coherence function of the cross spectrum.

Deprecated. Please use the raw_coherence() method of AveragedCrossspectrum, with the correct parameters.

Returns:
cohnumpy.ndarray

Coherence function

compute_rms(min_freq, max_freq, poisson_noise_level=None)

Compute the fractional rms amplitude in the power spectrum between two frequencies.

Parameters:
min_freq: float

The lower frequency bound for the calculation.

max_freq: float

The upper frequency bound for the calculation.

Returns:
rms: float

The fractional rms amplitude contained between min_freq and max_freq.

rms_err: float

The error on the fractional rms amplitude.

Other Parameters:
poisson_noise_levelfloat, default is None

This is the Poisson noise level of the PDS with same normalization as the PDS. If poissoin_noise_level is None, the Poisson noise is calculated in the idealcase e.g. 2./<countrate> for fractional rms normalisation Dead time and other instrumental effects can alter it. The user can fit the Poisson noise level outside this function using the same normalisation of the PDS and it will get subtracted from powers here.

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

deadtime_correct(dead_time, rate, background_rate=0, limit_k=200, n_approx=None, paralyzable=False)

Correct the power spectrum for dead time effects.

This correction is based on the formula given in Zhang et al. 2015, assuming a constant dead time for all events. For more advanced dead time corrections, see the FAD method from stingray.deadtime.fad

Parameters:
dead_time: float

The dead time of the detector.

ratefloat

Detected source count rate

Returns:
spectrum: Crossspectrum or derivative.

The dead-time corrected spectrum.

Other Parameters:
background_ratefloat, default 0

Detected background count rate. This is important to estimate when deadtime is given by the combination of the source counts and background counts (e.g. in an imaging X-ray detector).

paralyzable: bool, default False

If True, the dead time correction is done assuming a paralyzable dead time. If False, the correction is done assuming a non-paralyzable (more common) dead time.

limit_kint, default 200

Limit to this value the number of terms in the inner loops of calculations. Check the plots returned by the check_B() and check_A() functions to test that this number is adequate.

n_approxint, default None

Number of bins to calculate the model power spectrum. If None, it will use the size of the input frequency array. Relatively simple models (e.g., low count rates compared to dead time) can use a smaller number of bins to speed up the calculation, and the final power values will be interpolated.

dict() → dict

Return a dictionary representation of the object.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

static from_events(events, dt, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)

Calculate an average power spectrum from an event list.

Parameters:
eventsEventList

Event list to be analyzed.

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency).

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedPowerspectrum.

gti: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average.

static from_lc_iterable(iter_lc, dt, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)

Calculate the average power spectrum of an iterable collection of light curves.

Parameters:
iter_lciterable of stingray.Lightcurve objects or np.array

Light curves. If arrays, use them as counts.

dtfloat

The time resolution of the light curves (sets the Nyquist frequency)

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedPowerspectrum.

gti: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average.

static from_lightcurve(lc, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)

Calculate a power spectrum from a light curve.

Parameters:
eventsstingray.Lightcurve

Light curve to be analyzed.

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency).

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedPowerspectrum.

gti: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

static from_stingray_timeseries(ts, flux_attr, error_flux_attr=None, segment_size=None, norm='none', silent=False, use_common_mean=True, gti=None, save_all=False)

Calculate AveragedPowerspectrum from a time series.

Parameters:
tsStingrayTimeseries

Input Time Series

flux_attrstr

What attribute of the time series will be used.

Other Parameters:
error_flux_attrstr

What attribute of the time series will be used as error bar.

segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

save_allbool, default False

Save all intermediate PDSs used for the final average.

static from_time_array(times, dt, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True)

Calculate an average power spectrum from an array of event times.

Parameters:
timesnp.array

Event arrival times.

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency).

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedPowerspectrum.

gti: ``[[gti0_0, gti0_1], [gti1_0, gti1_1], …]``

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

initial_checks(data1=None, data2=None, norm='frac', gti=None, lc1=None, lc2=None, segment_size=None, power_type='real', dt=None, fullspec=False)

Run initial checks on the input.

Returns True if checks are passed, False if they are not.

Raises various errors for different bad inputs

Examples

>>> times = np.arange(0, 10)
>>> counts = np.random.poisson(100, 10)
>>> lc1 = Lightcurve(times, counts, skip_checks=True)
>>> lc2 = Lightcurve(times, counts, skip_checks=True)
>>> ev1 = EventList(times)
>>> ev2 = EventList(times)
>>> c = Crossspectrum()
>>> ac = AveragedCrossspectrum()

If norm is not a string, raise a TypeError >>> Crossspectrum.initial_checks(c, norm=1) Traceback (most recent call last): … TypeError: norm must be a string…

If norm is not one of the valid norms, raise a ValueError >>> Crossspectrum.initial_checks(c, norm=”blabla”) Traceback (most recent call last): … ValueError: norm must be ‘frac’…

If power_type is not one of the valid norms, raise a ValueError >>> Crossspectrum.initial_checks(c, power_type=”blabla”) Traceback (most recent call last): … ValueError: power_type not recognized!

If the user passes only one light curve, raise a ValueError

>>> Crossspectrum.initial_checks(c, data1=lc1, data2=None)
Traceback (most recent call last):
...
ValueError: You can't do a cross spectrum...

If the user passes an event list without dt, raise a ValueError

>>> Crossspectrum.initial_checks(c, data1=ev1, data2=ev2, dt=None)
Traceback (most recent call last):
...
ValueError: If using event lists, please specify...
internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

jackknifed_sdf_variance(freq_response, eigvals, adaptive)[source]

Compute the variance of the spectrum through jack-knife.

Returns the variance of the log-sdf estimated through jack-knifing a group of independent sdf estimates.

Parameters:
freq_response: numpy.ndarray

The frequency responses or the Fourier transforms of the tapered data

eigvals: numpy.ndarray

The eigenvalues of the DPSS tapers

adaptive: boolean

Whether or not to use the adaptive weighting method to calculate the log-sdf through jack-knife method

Returns:
varThe estimate for log-sdf variance

Notes

The jackknifed mean estimate is distributed about the true mean as a Student’s t-distribution with (K-1) degrees of freedom, and standard error equal to sqrt(var). However, Thompson and Chave [1] point out that this variance better describes the sample mean. [1] Thomson D J, Chave A D (1991) Advances in Spectrum Analysis and Array Processing (Prentice-Hall, Englewood Cliffs, NJ), 1, pp 58-113.

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

modulation_upper_limit(fmin=None, fmax=None, c=0.95)

Upper limit on a sinusoidal modulation.

To understand the meaning of this amplitude: if the modulation is described by:

..math:: p = overline{p} (1 + a * sin(x))

this function returns a.

If it is a sum of sinusoidal harmonics instead ..math:: p = overline{p} (1 + sum_l a_l * sin(lx)) a is equivalent to \(\sqrt(\sum_l a_l^2)\).

See stingray.stats.power_upper_limit, stingray.stats.amplitude_upper_limit for more information.

The formula used to calculate the upper limit assumes the Leahy normalization. If the periodogram is in another normalization, we will internally convert it to Leahy before calculating the upper limit.

Parameters:
fmin: float

The minimum frequency to search (defaults to the first nonzero bin)

fmax: float

The maximum frequency to search (defaults to the Nyquist frequency)

Returns:
a: float

The modulation amplitude that could produce P>pmeas with 1 - c probability.

Other Parameters:
c: float

The confidence value for the upper limit (e.g. 0.95 = 95%)

Examples

>>> pds = Powerspectrum()
>>> pds.norm = "leahy"
>>> pds.freq = np.arange(0., 5.)
>>> # Note: this pds has 40 as maximum value between 2 and 5 Hz
>>> pds.power = np.array([100000, 1, 1, 40, 1])
>>> pds.m = 1
>>> pds.nphots = 30000
>>> assert np.isclose(
...     pds.modulation_upper_limit(fmin=2, fmax=5, c=0.99),
...     0.10164,
...     atol=0.0001)
phase_lag()

Calculate the fourier phase lag of the cross spectrum.

This is defined as the argument of the complex cross spectrum, and gives the delay at all frequencies, in cycles, of one input light curve with respect to the other.

plot(labels=None, axis=None, title=None, marker='-', save=False, filename=None, ax=None)

Plot the amplitude of the cross spectrum vs. the frequency using matplotlib.

Parameters:
labelsiterable, default None

A list of tuple with xlabel and ylabel as strings.

axislist, tuple, string, default None

Parameter to set axis properties of the matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for the``matplotlib.pyplot.axis()`` method.

titlestr, default None

The title of the plot.

markerstr, default ‘-’

Line style and color of the plot. Line styles and colors are combined in a single format string, as in 'bo' for blue circles. See matplotlib.pyplot.plot for more options.

saveboolean, optional, default False

If True, save the figure with specified filename.

filenamestr

File name of the image to save. Depends on the boolean save.

axmatplotlib.Axes object

An axes object to fill with the cross correlation plot.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

psd_from_freq_response(freq_response, weights)[source]

Calculate the weighted PSD from the Fourier transformed data.

Calculate the weighted PSD from the Fourier transformed data by combining the frequencies responses of these tapered data using the given weights.

Parameters:
freq_response: numpy.ndarray

The frequency responses or the Fourier transforms of the tapered data

weights: numpy.ndarray

The weights to combine the different Fourier transforms of the data tapered with different windows to produce the weighted PSD.

Returns:
psd: numpy.ndarray

The weighted power spectral density of the provided Fourier transformed data.

Notes

The shape of freq_response and weights must either be same or broadcastable.

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

rebin(df=None, f=None, method='mean')[source]

Rebin the multitaper spectrum to a new frequency resolution df.

Parameters:
df: float

The new frequency resolution

Returns:
bin_cs = Multitaper (or one of its subclasses) object

The newly binned multitaper spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from AveragedPowerspectrum, it will return an object of class AveragedPowerspectrum, too.

Other Parameters:
f: float

the rebin factor. If specified, it substitutes df with f*self.df

rebin_log(f=0.01)

Logarithmic rebin of the periodogram. The new frequency depends on the previous frequency modified by a factor f:

\[d\nu_j = d\nu_{j-1} (1+f)\]
Parameters:
f: float, optional, default ``0.01``

parameter that steers the frequency resolution

Returns:
new_specCrossspectrum (or one of its subclasses) object

The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from AveragedPowerspectrum, it will return an object of class

save_as_xspec(outroot, header_keywords=None)

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

time_lag()

Calculate the fourier time lag of the cross spectrum. The time lag is calculated by taking the phase lag \(\phi\) and

..math:

\tau = \frac{\phi}{\two pi \nu}

where \(\nu\) is the center of the frequency bins.

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_norm(norm, inplace=False)

Convert Cross spectrum to new normalization.

Parameters:
normstr

The new normalization of the spectrum

Returns:
new_specobject, same class as input

The new, normalized, spectrum.

Other Parameters:
inplace: bool, default False

If True, change the current instance. Otherwise, return a new one

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


Lomb-Scargle Crossspectrum

class stingray.LombScargleCrossspectrum(data1: EventList | Lightcurve | None = None, data2: EventList | Lightcurve | None = None, norm: str | None = 'none', power_type: str | None = 'all', dt: float | None = None, fullspec: bool | None = False, skip_checks: bool = False, min_freq: float = None, max_freq: float = None, df: float = None, method: str = 'fast', oversampling: int = 10)[source]

Make a cross spectrum from an unevenly sampled (binned) light curve. You can also make an empty LombScargleCrossspectrum object to populate with your own Fourier-transformed data (this can sometimes be useful when making binned power spectra).

Parameters:
data1: :class:`stingray.lightcurve.Lightcurve` or :class:`stingray.events.EventList`, optional, default ``None``

The dataset for the first channel/band of interest.

data2: :class:`stingray.lightcurve.Lightcurve` or :class:`stingray.events.EventList`, optional, default ``None``

The dataset for the second, or “reference”, band.

norm: {``frac``, ``abs``, ``leahy``, ``none``}, string, optional, default ``none``

The normalization of the cross spectrum.

power_type: {``real``, ``absolute``, ``all`}, string, optional, default ``all``

Parameter to choose among complete, real part and magnitude of the cross spectrum.

fullspec: boolean, optional, default ``False``

If False, keep only the positive frequencies, or if True, keep all of them .

Attributes:
freq: numpy.ndarray

The array of mid-bin frequencies that the Fourier transform samples

power: numpy.ndarray

The array of cross spectra (complex numbers)

power_err: numpy.ndarray

The uncertainties of power. An approximation for each bin given by power_err= power/sqrt(m). Where m is the number of power averaged in each bin (by frequency binning, or averaging more than one spectra). Note that for a single realization (m=1) the error is equal to the power.

df: float

The frequency resolution

m: int

The number of averaged cross-spectra amplitudes in each bin.

n: int

The number of data points/time bins in one segment of the light curves.

k: array of int

The rebinning scheme if the object has been rebinned otherwise is set to 1.

nphots1: float

The total number of photons in light curve 1

nphots2: float

The total number of photons in light curve 2

Other Parameters:
dt: float

The time resolution of the light curve. Only needed when constructing light curves in the case where data1, data2 are EventList objects

skip_checks: bool

Skip initial checks, for speed or other reasons (you need to trust your inputs!)

min_freqfloat

Minimum frequency to take the Lomb-Scargle Fourier Transform

max_freq: float

Maximum frequency to take the Lomb-Scargle Fourier Transform

dffloat

The frequency resolution of the final periodogram.

methodstr

The method to be used by the Lomb-Scargle Fourier Transformation function. fast and slow are the allowed values. Default is fast. fast uses the optimized Press and Rybicki O(n*log(n))

oversamplingfloat, optional, default: 10

Interpolation Oversampling Factor (for the fast algorithm)

References

[1]

Scargle, J. D. , “Studies in astronomical time series analysis. III - Fourier transforms, autocorrelation functions, and cross-correlation functions of unevenly spaced data”. ApJ 1:343, p874-887, 1989

[2]

Press W.H. and Rybicki, G.B, “Fast algorithm for spectral analysis of unevenly sampled data”. ApJ 1:338, p277, 1989

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

classical_significances()[source]

Not applicable for unevenly sampled data

coherence()

Compute Raw Coherence function of the cross spectrum.

Deprecated. Please use the raw_coherence() method of AveragedCrossspectrum, with the correct parameters.

Returns:
cohnumpy.ndarray

Coherence function

compute_rms(min_freq, max_freq, poisson_noise_level=None)[source]

Compute the fractional rms amplitude in the power spectrum between two frequencies.

Parameters:
min_freq: float

The lower frequency bound for the calculation.

max_freq: float

The upper frequency bound for the calculation.

Returns:
rms: float

The fractional rms amplitude contained between min_freq and max_freq.

rms_err: float

The error on the fractional rms amplitude.

Other Parameters:
poisson_noise_levelfloat, default is None

This is the Poisson noise level of the PDS with same normalization as the PDS. If poissoin_noise_level is None, the Poisson noise is calculated in the idealcase e.g. 2./<countrate> for fractional rms normalisation Dead time and other instrumental effects can alter it. The user can fit the Poisson noise level outside this function using the same normalisation of the PDS and it will get subtracted from powers here.

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

deadtime_correct(dead_time, rate, background_rate=0, limit_k=200, n_approx=None, paralyzable=False)

Correct the power spectrum for dead time effects.

This correction is based on the formula given in Zhang et al. 2015, assuming a constant dead time for all events. For more advanced dead time corrections, see the FAD method from stingray.deadtime.fad

Parameters:
dead_time: float

The dead time of the detector.

ratefloat

Detected source count rate

Returns:
spectrum: Crossspectrum or derivative.

The dead-time corrected spectrum.

Other Parameters:
background_ratefloat, default 0

Detected background count rate. This is important to estimate when deadtime is given by the combination of the source counts and background counts (e.g. in an imaging X-ray detector).

paralyzable: bool, default False

If True, the dead time correction is done assuming a paralyzable dead time. If False, the correction is done assuming a non-paralyzable (more common) dead time.

limit_kint, default 200

Limit to this value the number of terms in the inner loops of calculations. Check the plots returned by the check_B() and check_A() functions to test that this number is adequate.

n_approxint, default None

Number of bins to calculate the model power spectrum. If None, it will use the size of the input frequency array. Relatively simple models (e.g., low count rates compared to dead time) can use a smaller number of bins to speed up the calculation, and the final power values will be interpolated.

dict() → dict

Return a dictionary representation of the object.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

from_events()[source]

Not applicable for unevenly sampled data

from_lc_iterable()[source]

Not applicable for unevenly sampled data

from_lightcurve()[source]

Not applicable for unevenly sampled data

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

static from_stingray_timeseries(ts1, ts2, flux_attr, error_flux_attr=None, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)

Calculate AveragedCrossspectrum from two light curves

Parameters:
ts1StingrayTimeseries

Time series from channel 1

ts2StingrayTimeseries

Time series from channel 2

flux_attrstr

What attribute of the time series will be used.

Other Parameters:
error_flux_attrstr

What attribute of the time series will be used as error bar.

segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

from_time_array()[source]

Not applicable for unevenly sampled data

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

initial_checks(data1, data2, norm, power_type, dt, min_freq, max_freq, fullspec, df, method, oversampling)[source]

Run initial checks on the input.

Returns True if checks are passed, False if they are not.

Raises various errors for different bad inputs

Examples

>>> times = np.arange(0, 10)
>>> counts = np.random.poisson(100, 10)
>>> lc1 = Lightcurve(times, counts, skip_checks=True)
>>> lc2 = Lightcurve(times, counts, skip_checks=True)
>>> ev1 = EventList(times)
>>> ev2 = EventList(times)
>>> c = Crossspectrum()
>>> ac = AveragedCrossspectrum()

If norm is not a string, raise a TypeError >>> Crossspectrum.initial_checks(c, norm=1) Traceback (most recent call last): … TypeError: norm must be a string…

If norm is not one of the valid norms, raise a ValueError >>> Crossspectrum.initial_checks(c, norm=”blabla”) Traceback (most recent call last): … ValueError: norm must be ‘frac’…

If power_type is not one of the valid norms, raise a ValueError >>> Crossspectrum.initial_checks(c, power_type=”blabla”) Traceback (most recent call last): … ValueError: power_type not recognized!

If the user passes only one light curve, raise a ValueError

>>> Crossspectrum.initial_checks(c, data1=lc1, data2=None)
Traceback (most recent call last):
...
ValueError: You can't do a cross spectrum...

If the user passes an event list without dt, raise a ValueError

>>> Crossspectrum.initial_checks(c, data1=ev1, data2=ev2, dt=None)
Traceback (most recent call last):
...
ValueError: If using event lists, please specify...
internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

phase_lag()

Calculate the fourier phase lag of the cross spectrum.

This is defined as the argument of the complex cross spectrum, and gives the delay at all frequencies, in cycles, of one input light curve with respect to the other.

plot(labels=None, axis=None, title=None, marker='-', save=False, filename=None, ax=None)

Plot the amplitude of the cross spectrum vs. the frequency using matplotlib.

Parameters:
labelsiterable, default None

A list of tuple with xlabel and ylabel as strings.

axislist, tuple, string, default None

Parameter to set axis properties of the matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for the``matplotlib.pyplot.axis()`` method.

titlestr, default None

The title of the plot.

markerstr, default ‘-’

Line style and color of the plot. Line styles and colors are combined in a single format string, as in 'bo' for blue circles. See matplotlib.pyplot.plot for more options.

saveboolean, optional, default False

If True, save the figure with specified filename.

filenamestr

File name of the image to save. Depends on the boolean save.

axmatplotlib.Axes object

An axes object to fill with the cross correlation plot.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

rebin(df=None, f=None, method='mean')

Rebin the cross spectrum to a new frequency resolution df.

Parameters:
df: float

The new frequency resolution

Returns:
bin_cs = Crossspectrum (or one of its subclasses) object

The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from AveragedPowerspectrum, it will return an object of class AveragedPowerspectrum, too.

Other Parameters:
f: float

the rebin factor. If specified, it substitutes df with f*self.df

rebin_log(f=0.01)

Logarithmic rebin of the periodogram. The new frequency depends on the previous frequency modified by a factor f:

\[d\nu_j = d\nu_{j-1} (1+f)\]
Parameters:
f: float, optional, default ``0.01``

parameter that steers the frequency resolution

Returns:
new_specCrossspectrum (or one of its subclasses) object

The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from AveragedPowerspectrum, it will return an object of class

save_as_xspec(outroot, header_keywords=None)

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

time_lag()[source]

Calculate the fourier time lag of the cross spectrum. The time lag is calculated by taking the phase lag \(\phi\) and

..math:

\tau = \frac{\phi}{\two pi \nu}

where \(\nu\) is the center of the frequency bins.

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_norm(norm, inplace=False)

Convert Cross spectrum to new normalization.

Parameters:
normstr

The new normalization of the spectrum

Returns:
new_specobject, same class as input

The new, normalized, spectrum.

Other Parameters:
inplace: bool, default False

If True, change the current instance. Otherwise, return a new one

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


Lomb-Scargle Powerspectrum

class stingray.LombScarglePowerspectrum(data: EventList | Lightcurve | None = None, norm: str | None = 'frac', power_type: str | None = 'all', dt: float | None = None, fullspec: bool | None = False, skip_checks: bool | None = False, min_freq: float | None = None, max_freq: float | None = None, df: float | None = None, method: str | None = 'fast', oversampling: int | None = 10)[source]

Make a LombScarglePowerspectrum (also called periodogram) from a unevenly sampled (binned) light curve. Periodograms can be normalized by either Leahy normalization, fractional rms normalization, absolute rms normalization, or not at all.

You can also make an empty LombScarglePowerspectrum object to populate with your own fourier-transformed data (this can sometimes be useful when making binned power spectra).

Parameters:
data: :class:`stingray.lightcurve.Lightcurve` or :class:`stingray.events.EventList` object, optional, default ``None``

The light curve data to be Fourier-transformed.

norm: {``frac``, ``abs``, ``leahy``, ``none``}, string, optional, default ``none``

The normalization of the power spectrum.

power_type: {``real``, ``absolute``, ``all`}, string, optional, default ``all``

Parameter to choose among complete, real part and magnitude of the power spectrum.

fullspec: boolean, optional, default ``False``

If False, keep only the positive frequencies, or if True, keep all of them .

Attributes:
freq: numpy.ndarray

The array of mid-bin frequencies that the Fourier transform samples.

power: numpy.ndarray

The array of normalized squared absolute values of Fourier amplitudes.

power_err: numpy.ndarray

The uncertainties of power. An approximation for each bin given by power_err= power/sqrt(m). Where m is the number of power averaged in each bin (by frequency binning, or averaging power spectra of segments of a light curve). Note that for a single realization (m=1) the error is equal to the power.

df: float

The frequency resolution.

m: int

The number of averaged powers in each bin.

n: int

The number of data points in the light curve.

nphots: float

The total number of photons in the light curve.

Other Parameters:
dt: float

The time resolution of the light curve. Only needed when constructing light curves in the case where data is a EventList object

skip_checks: bool

Skip initial checks, for speed or other reasons (you need to trust your inputs!).

min_freqfloat

Minimum frequency to take the Lomb-Scargle Fourier Transform

max_freq: float

Maximum frequency to take the Lomb-Scargle Fourier Transform

dffloat

The time resolution of the light curve. Only needed where data is a :class`stingray.Eventlist` object

methodstr

The method to be used by the Lomb-Scargle Fourier Transformation function. fast and slow are the allowed values. Default is fast. fast uses the optimized Press and Rybicki O(n*log(n))

oversamplingfloat, optional, default: 10

Interpolation Oversampling Factor (for the fast algorithm)

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

classical_significances()

Not applicable for unevenly sampled data

coherence()

Compute Raw Coherence function of the cross spectrum.

Deprecated. Please use the raw_coherence() method of AveragedCrossspectrum, with the correct parameters.

Returns:
cohnumpy.ndarray

Coherence function

compute_rms(min_freq, max_freq, poisson_noise_level=None)

Compute the fractional rms amplitude in the power spectrum between two frequencies.

Parameters:
min_freq: float

The lower frequency bound for the calculation.

max_freq: float

The upper frequency bound for the calculation.

Returns:
rms: float

The fractional rms amplitude contained between min_freq and max_freq.

rms_err: float

The error on the fractional rms amplitude.

Other Parameters:
poisson_noise_levelfloat, default is None

This is the Poisson noise level of the PDS with same normalization as the PDS. If poissoin_noise_level is None, the Poisson noise is calculated in the idealcase e.g. 2./<countrate> for fractional rms normalisation Dead time and other instrumental effects can alter it. The user can fit the Poisson noise level outside this function using the same normalisation of the PDS and it will get subtracted from powers here.

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

deadtime_correct(dead_time, rate, background_rate=0, limit_k=200, n_approx=None, paralyzable=False)

Correct the power spectrum for dead time effects.

This correction is based on the formula given in Zhang et al. 2015, assuming a constant dead time for all events. For more advanced dead time corrections, see the FAD method from stingray.deadtime.fad

Parameters:
dead_time: float

The dead time of the detector.

ratefloat

Detected source count rate

Returns:
spectrum: Crossspectrum or derivative.

The dead-time corrected spectrum.

Other Parameters:
background_ratefloat, default 0

Detected background count rate. This is important to estimate when deadtime is given by the combination of the source counts and background counts (e.g. in an imaging X-ray detector).

paralyzable: bool, default False

If True, the dead time correction is done assuming a paralyzable dead time. If False, the correction is done assuming a non-paralyzable (more common) dead time.

limit_kint, default 200

Limit to this value the number of terms in the inner loops of calculations. Check the plots returned by the check_B() and check_A() functions to test that this number is adequate.

n_approxint, default None

Number of bins to calculate the model power spectrum. If None, it will use the size of the input frequency array. Relatively simple models (e.g., low count rates compared to dead time) can use a smaller number of bins to speed up the calculation, and the final power values will be interpolated.

dict() → dict

Return a dictionary representation of the object.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

from_events()

Not applicable for unevenly sampled data

from_lc_iterable()

Not applicable for unevenly sampled data

from_lightcurve()

Not applicable for unevenly sampled data

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

static from_stingray_timeseries(ts1, ts2, flux_attr, error_flux_attr=None, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, channels_overlap=False)

Calculate AveragedCrossspectrum from two light curves

Parameters:
ts1StingrayTimeseries

Time series from channel 1

ts2StingrayTimeseries

Time series from channel 2

flux_attrstr

What attribute of the time series will be used.

Other Parameters:
error_flux_attrstr

What attribute of the time series will be used as error bar.

segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only relevant (and required) for AveragedCrossspectrum

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects. Could throw errors if these GTIs have overlaps with the input object GTIs! If you’re getting errors regarding your GTIs, don’t use this and only give GTIs to the input objects before making the cross spectrum.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

from_time_array()

Not applicable for unevenly sampled data

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

initial_checks(data1, data2, norm, power_type, dt, min_freq, max_freq, fullspec, df, method, oversampling)

Run initial checks on the input.

Returns True if checks are passed, False if they are not.

Raises various errors for different bad inputs

Examples

>>> times = np.arange(0, 10)
>>> counts = np.random.poisson(100, 10)
>>> lc1 = Lightcurve(times, counts, skip_checks=True)
>>> lc2 = Lightcurve(times, counts, skip_checks=True)
>>> ev1 = EventList(times)
>>> ev2 = EventList(times)
>>> c = Crossspectrum()
>>> ac = AveragedCrossspectrum()

If norm is not a string, raise a TypeError >>> Crossspectrum.initial_checks(c, norm=1) Traceback (most recent call last): … TypeError: norm must be a string…

If norm is not one of the valid norms, raise a ValueError >>> Crossspectrum.initial_checks(c, norm=”blabla”) Traceback (most recent call last): … ValueError: norm must be ‘frac’…

If power_type is not one of the valid norms, raise a ValueError >>> Crossspectrum.initial_checks(c, power_type=”blabla”) Traceback (most recent call last): … ValueError: power_type not recognized!

If the user passes only one light curve, raise a ValueError

>>> Crossspectrum.initial_checks(c, data1=lc1, data2=None)
Traceback (most recent call last):
...
ValueError: You can't do a cross spectrum...

If the user passes an event list without dt, raise a ValueError

>>> Crossspectrum.initial_checks(c, data1=ev1, data2=ev2, dt=None)
Traceback (most recent call last):
...
ValueError: If using event lists, please specify...
internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

phase_lag()

Calculate the fourier phase lag of the cross spectrum.

This is defined as the argument of the complex cross spectrum, and gives the delay at all frequencies, in cycles, of one input light curve with respect to the other.

plot(labels=None, axis=None, title=None, marker='-', save=False, filename=None, ax=None)

Plot the amplitude of the cross spectrum vs. the frequency using matplotlib.

Parameters:
labelsiterable, default None

A list of tuple with xlabel and ylabel as strings.

axislist, tuple, string, default None

Parameter to set axis properties of the matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for the``matplotlib.pyplot.axis()`` method.

titlestr, default None

The title of the plot.

markerstr, default ‘-’

Line style and color of the plot. Line styles and colors are combined in a single format string, as in 'bo' for blue circles. See matplotlib.pyplot.plot for more options.

saveboolean, optional, default False

If True, save the figure with specified filename.

filenamestr

File name of the image to save. Depends on the boolean save.

axmatplotlib.Axes object

An axes object to fill with the cross correlation plot.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

rebin(df=None, f=None, method='mean')

Rebin the cross spectrum to a new frequency resolution df.

Parameters:
df: float

The new frequency resolution

Returns:
bin_cs = Crossspectrum (or one of its subclasses) object

The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from AveragedPowerspectrum, it will return an object of class AveragedPowerspectrum, too.

Other Parameters:
f: float

the rebin factor. If specified, it substitutes df with f*self.df

rebin_log(f=0.01)

Logarithmic rebin of the periodogram. The new frequency depends on the previous frequency modified by a factor f:

\[d\nu_j = d\nu_{j-1} (1+f)\]
Parameters:
f: float, optional, default ``0.01``

parameter that steers the frequency resolution

Returns:
new_specCrossspectrum (or one of its subclasses) object

The newly binned cross spectrum or power spectrum. Note: this object will be of the same type as the object that called this method. For example, if this method is called from AveragedPowerspectrum, it will return an object of class

save_as_xspec(outroot, header_keywords=None)

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

time_lag()

Calculate the fourier time lag of the cross spectrum. The time lag is calculated by taking the phase lag \(\phi\) and

..math:

\tau = \frac{\phi}{\two pi \nu}

where \(\nu\) is the center of the frequency bins.

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_norm(norm, inplace=False)

Convert Cross spectrum to new normalization.

Parameters:
normstr

The new normalization of the spectrum

Returns:
new_specobject, same class as input

The new, normalized, spectrum.

Other Parameters:
inplace: bool, default False

If True, change the current instance. Otherwise, return a new one

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


CrossCorrelation

class stingray.CrossCorrelation(lc1=None, lc2=None, cross=None, mode='same', norm='none')[source]

Make a cross-correlation from light curves or a cross spectrum.

You can also make an empty CrossCorrelation object to populate with your own cross-correlation data.

Parameters:
lc1: :class:`stingray.Lightcurve` object, optional, default ``None``

The first light curve data for correlation calculations.

lc2: :class:`stingray.Lightcurve` object, optional, default ``None``

The light curve data for the correlation calculations.

cross: :class: `stingray.Crossspectrum` object, default ``None``

The cross spectrum data for the correlation calculations.

mode: {``full``, ``valid``, ``same``}, optional, default ``same``

A string indicating the size of the correlation output. See the relevant scipy documentation [scipy-docs] for more details.

norm: {``none``, ``variance``}

if “variance”, the cross correlation is normalized so that perfect correlation gives 1, and perfect anticorrelation gives -1. See Gaskell & Peterson 1987, Gardner & Done 2017

Attributes:
lc1: :class:`stingray.Lightcurve`

The first light curve data for correlation calculations.

lc2: :class:`stingray.Lightcurve`

The light curve data for the correlation calculations.

cross: :class: `stingray.Crossspectrum`

The cross spectrum data for the correlation calculations.

corr: numpy.ndarray

An array of correlation data calculated from two light curves

time_lags: numpy.ndarray

An array of all possible time lags against which each point in corr is calculated

dt: float

The time resolution of each light curve (used in time_lag calculations)

time_shift: float

Time lag that gives maximum value of correlation between two light curves. There will be maximum correlation between light curves if one of the light curve is shifted by time_shift.

n: int

Number of points in self.corr (length of cross-correlation data)

auto: bool

An internal flag to indicate whether this is a cross-correlation or an auto-correlation.

norm: {``none``, ``variance``}

The normalization specified in input

References

cal_timeshift(dt=1.0)[source]

Calculate the cross correlation against all possible time lags, both positive and negative.

The method signal.correlation_lags() uses SciPy versions >= 1.6.1 ([scipy-docs-lag])

Parameters:
dt: float, optional, default ``1.0``

Time resolution of the light curve, should be passed when object is populated with correlation data and no information about light curve can be extracted. Used to calculate time_lags.

Returns:
self.time_shift: float

Value of the time lag that gives maximum value of correlation between two light curves.

self.time_lags: numpy.ndarray

An array of time_lags calculated from correlation data

References

plot(labels=None, axis=None, title=None, marker='-', save=False, filename=None, ax=None)[source]

Plot the CrossCorrelation as function using Matplotlib. Plot the Crosscorrelation object on a graph self.time_lags on x-axis and self.corr on y-axis

Parameters:
labelsiterable, default None

A list of tuple with xlabel and ylabel as strings.

axislist, tuple, string, default None

Parameter to set axis properties of matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for matplotlib.pyplot.axis() function.

titlestr, default None

The title of the plot.

markerstr, default -

Line style and color of the plot. Line styles and colors are combined in a single format string, as in 'bo' for blue circles. See matplotlib.pyplot.plot for more options.

saveboolean, optional (default=False)

If True, save the figure with specified filename.

filenamestr

File name of the image to save. Depends on the boolean save.

axmatplotlib.Axes object

An axes object to fill with the cross correlation plot.


AutoCorrelation

class stingray.AutoCorrelation(lc=None, mode='same')[source]

Make an auto-correlation from a light curve. You can also make an empty Autocorrelation object to populate with your own auto-correlation data.

Parameters:
lc: :class:`stingray.Lightcurve` object, optional, default ``None``

The light curve data for correlation calculations.

mode: {``full``, ``valid``, ``same``}, optional, default ``same``

A string indicating the size of the correlation output. See the relevant scipy documentation [scipy-docs] for more details.

Attributes:
lc1, lc2::class:`stingray.Lightcurve`

The light curve data for correlation calculations.

corr: numpy.ndarray

An array of correlation data calculated from lightcurve data

time_lags: numpy.ndarray

An array of all possible time lags against which each point in corr is calculated

dt: float

The time resolution of each lightcurve (used in time_lag calculations)

time_shift: float, zero

Max. Value of AutoCorrelation is always at zero lag.

n: int

Number of points in self.corr(Length of auto-correlation data)

cal_timeshift(dt=1.0)

Calculate the cross correlation against all possible time lags, both positive and negative.

The method signal.correlation_lags() uses SciPy versions >= 1.6.1 ([scipy-docs-lag])

Parameters:
dt: float, optional, default ``1.0``

Time resolution of the light curve, should be passed when object is populated with correlation data and no information about light curve can be extracted. Used to calculate time_lags.

Returns:
self.time_shift: float

Value of the time lag that gives maximum value of correlation between two light curves.

self.time_lags: numpy.ndarray

An array of time_lags calculated from correlation data

References

plot(labels=None, axis=None, title=None, marker='-', save=False, filename=None, ax=None)

Plot the CrossCorrelation as function using Matplotlib. Plot the Crosscorrelation object on a graph self.time_lags on x-axis and self.corr on y-axis

Parameters:
labelsiterable, default None

A list of tuple with xlabel and ylabel as strings.

axislist, tuple, string, default None

Parameter to set axis properties of matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for matplotlib.pyplot.axis() function.

titlestr, default None

The title of the plot.

markerstr, default -

Line style and color of the plot. Line styles and colors are combined in a single format string, as in 'bo' for blue circles. See matplotlib.pyplot.plot for more options.

saveboolean, optional (default=False)

If True, save the figure with specified filename.

filenamestr

File name of the image to save. Depends on the boolean save.

axmatplotlib.Axes object

An axes object to fill with the cross correlation plot.


Spectrum constructor functions

These build cross and power spectra directly from the various input containers, without going through the class constructors.

stingray.crossspectrum.crossspectrum_from_time_array(times1, times2, dt, segment_size=None, gti=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, save_all=False, channels_overlap=False)[source]

Calculate AveragedCrossspectrum from two arrays of event times.

Parameters:
times1np.array

Event arrival times of channel 1

times2np.array

Event arrival times of channel 2

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency)

Returns:
specAveragedCrossspectrum or Crossspectrum

The output cross spectrum.

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

stingray.crossspectrum.crossspectrum_from_events(events1, events2, dt, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, save_all=False, channels_overlap=False)[source]

Calculate AveragedCrossspectrum from two event lists

Parameters:
events1EventList

Events from channel 1

events2EventList

Events from channel 2

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency)

Returns:
specAveragedCrossspectrum or Crossspectrum

The output cross spectrum.

Other Parameters:
segment_sizefloat, default None

The length, in seconds, of the light curve segments that will be averaged

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

stingray.crossspectrum.crossspectrum_from_lightcurve(lc1, lc2, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, save_all=False, channels_overlap=False)[source]

Calculate AveragedCrossspectrum from two light curves

Parameters:
lc1stingray.Lightcurve

Light curve from channel 1

lc2stingray.Lightcurve

Light curve from channel 2

Returns:
specAveragedCrossspectrum or Crossspectrum

The output cross spectrum.

Other Parameters:
segment_sizefloat, default None

The length, in seconds, of the light curve segments that will be averaged

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects

save_allbool, default False

Save all intermediate spectra used for the final average. Use with care. This is likely to fill up your RAM on medium-sized datasets, and to slow down the computation when rebinning.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

stingray.crossspectrum.crossspectrum_from_timeseries(ts1, ts2, flux_attr, error_flux_attr=None, segment_size=None, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, save_all=False, channels_overlap=False)[source]

Calculate AveragedCrossspectrum from two time series

Parameters:
ts1stingray.StingrayTimeseries

Time series from channel 1

ts2stingray.StingrayTimeseries

Time series from channel 2

flux_attrstr

What attribute of the time series will be used.

Returns:
specAveragedCrossspectrum or Crossspectrum

The output cross spectrum.

Other Parameters:
error_flux_attrstr

What attribute of the time series will be used as error bar.

segment_sizefloat, default None

The length, in seconds, of the light curve segments that will be averaged

normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. Defaults to the common GTIs from the two input objects

save_allbool, default False

Save all intermediate spectra used for the final average. Use with care. This is likely to fill up your RAM on medium-sized datasets, and to slow down the computation when rebinning.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

stingray.crossspectrum.crossspectrum_from_lc_iterable(iter_lc1, iter_lc2, dt, segment_size, norm='none', power_type='all', silent=False, fullspec=False, use_common_mean=True, gti=None, save_all=False, channels_overlap=False)[source]

Calculate AveragedCrossspectrum from two light curves

Parameters:
iter_lc1iterable of stingray.Lightcurve objects or np.array

Light curves from channel 1. If arrays, use them as counts

iter_lc1iterable of stingray.Lightcurve objects or np.array

Light curves from channel 2. If arrays, use them as counts

dtfloat

The time resolution of the light curves (sets the Nyquist frequency)

segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged

Returns:
specAveragedCrossspectrum or Crossspectrum

The output cross spectrum.

Other Parameters:
normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good Time intervals. The GTIs of the input light curves are intersected with these.

channels_overlap: bool, default False

If True, the reference band contains all the photons of the subject band. This happens, for example, when calculating covariance spectra (see, e.g., the docs for CovarianceSpectrum). This will generally be false in a single cross spectrum.

stingray.powerspectrum.powerspectrum_from_time_array(times, dt, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)[source]

Calculate a power spectrum from an array of event times.

Parameters:
timesnp.array

Event arrival times.

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency).

Returns:
specAveragedPowerspectrum or Powerspectrum

The output periodogram.

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only required (and used) for AveragedPowerspectrum.

gti[[gti0_0, gti0_1], [gti1_0, gti1_1], ...]

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average. Use with care. This is likely to fill up your RAM on medium-sized datasets, and to slow down the computation when rebinning.

stingray.powerspectrum.powerspectrum_from_events(events, dt, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)[source]

Calculate a power spectrum from an event list.

Parameters:
eventsEventList

Event list to be analyzed.

dtfloat

The time resolution of the intermediate light curves (sets the Nyquist frequency)

Returns:
specAveragedPowerspectrum or Powerspectrum

The output periodogram.

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only required (and used) for AveragedPowerspectrum.

gti[[gti0_0, gti0_1], [gti1_0, gti1_1], ...]

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average. Use with care. This is likely to fill up your RAM on medium-sized datasets, and to slow down the computation when rebinning.

stingray.powerspectrum.powerspectrum_from_lightcurve(lc, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)[source]

Calculate a power spectrum from a light curve

Parameters:
lcstingray.Lightcurve

Light curve to be analyzed.

Returns:
specAveragedPowerspectrum or Powerspectrum

The output periodogram.

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only required (and used) for AveragedPowerspectrum.

gti[[gti0_0, gti0_1], [gti1_0, gti1_1], ...]

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average. Use with care. This is likely to fill up your RAM on medium-sized datasets, and to slow down the computation when rebinning.

stingray.powerspectrum.powerspectrum_from_timeseries(ts, flux_attr, error_flux_attr=None, segment_size=None, norm='none', silent=False, use_common_mean=True, gti=None, save_all=False)[source]

Calculate power spectrum from a time series

Parameters:
tsstingray.StingrayTimeseries

Input time series

flux_attrstr

What attribute of the time series will be used.

Returns:
specAveragedCrossspectrum or Crossspectrum

The output cross spectrum.

Other Parameters:
error_flux_attrstr

What attribute of the time series will be used as error bar.

segment_sizefloat

The length, in seconds, of the light curve segments that will be averaged. Only required (and used) for AveragedPowerspectrum.

gti[[gti0_0, gti0_1], [gti1_0, gti1_1], ...]

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average. Use with care. This is likely to fill up your RAM on medium-sized datasets, and to slow down the computation when rebinning.

stingray.powerspectrum.powerspectrum_from_lc_iterable(iter_lc, dt, segment_size=None, gti=None, norm='frac', silent=False, use_common_mean=True, save_all=False)[source]

Calculate an average power spectrum from an iterable collection of light curves.

Parameters:
iter_lciterable of stingray.Lightcurve objects or np.array

Light curves. If arrays, use them as counts.

dtfloat

The time resolution of the light curves (sets the Nyquist frequency).

Returns:
specAveragedPowerspectrum or Powerspectrum

The output periodogram.

Other Parameters:
segment_sizefloat, default None

The length, in seconds, of the light curve segments that will be averaged. If not None, it will be used to check the segment size of the output.

gti[[gti0_0, gti0_1], [gti1_0, gti1_1], ...]

Additional, optional Good Time intervals that get intersected with the GTIs of the input object. Can cause errors if there are overlaps between these GTIs and the input object GTIs. If that happens, assign the desired GTIs to the input object.

normstr, default “frac”

The normalization of the periodogram. abs is absolute rms, frac is fractional rms, leahy is Leahy+83 normalization, and none is the unnormalized periodogram.

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). By default, we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars.

save_allbool, default False

Save all intermediate PDSs used for the final average. Use with care.

stingray.lombscargle.lscrossspectrum_from_lightcurve(lc1, lc2, norm='frac', power_type='all', fullspec=False, min_freq=None, max_freq=None, df=None, method='fast', oversampling=10, nyquist_factor=1)[source]

Creates a Lomb Scargle Cross Spectrum from two light curves :param lc1: Light curve from channel 1. :type lc1: stingray.lightcurve.Lightcurve object :param lc2: Light curve from channel 2. :type lc2: stingray.lightcurve.Lightcurve object

Other Parameters:
normstr, default “none”

The normalization of the periodogram. “frac” is fractional rms,”abs” is absolute rms, “leahy” is Leahy normalization, and “none” is the unnormalized periodogram

power_typestr, default “all”

Parameter to choose among complete, real part and magnitude of the spectrum

fullspecbool, default False

If False, keep only the positive frequencies, or if True, keep all of them

min_freqfloat, default 0

Minimum frequency to take the Lomb-Scargle Fourier Transform

max_freqfloat, default None

Maximum frequency to take the Lomb-Scargle Fourier Transform

methodstr, default “fast”

The method to be used by the Lomb-Scargle Fourier Transformation function. fast and slow are the allowed values. Default is fast. fast uses the optimized Press and Rybicki O(n*log(n)) algorithm, while slow uses the original O(n^2) algorithm.

oversamplingint, default 10

Interpolation Oversampling Factor (for the fast algorithm)

nyquist_factorint, default 1

How many times the Nyquist frequency to use as the maximum frequency


Cross spectrum functions

The public functions of stingray.crossspectrum that are not constructors: the two normalizations, the single-trial p-value of a cospectrum power, the time lag between two light curves, and the flux generator that the averaged spectra are built from.

stingray.crossspectrum.normalize_crossspectrum(unnorm_power, tseg, nbins, nphots1, nphots2, norm='none', power_type='real')[source]

Normalize the real part of the cross spectrum to Leahy, absolute rms^2, fractional rms^2 normalization, or not at all.

Here for API compatibility purposes. Will be removed in the next major release.

Parameters:
unnorm_power: numpy.ndarray

The unnormalized cross spectrum.

tseg: int

The length of the Fourier segment, in seconds.

nbinsint

Number of bins in the light curve

nphots1int

Number of photons in the light curve no. 1

nphots2int

Number of photons in the light curve no. 2

Returns:
power: numpy.nd.array

The normalized co-spectrum (real part of the cross spectrum). For ‘none’ normalization, imaginary part is returned as well.

Other Parameters:
normstr

One of 'leahy' (Leahy+83), 'frac' (fractional rms), 'abs' (absolute rms)

power_typestr

One of 'real' (real part), 'all' (all complex powers), 'abs' (absolute value)

stingray.crossspectrum.normalize_crossspectrum_gauss(unnorm_power, mean_flux, var, dt, N, norm='none', power_type='real')[source]

Normalize the real part of the cross spectrum to Leahy, absolute rms^2, fractional rms^2 normalization, or not at all.

Here for API compatibility purposes. Will be removed in the next major release.

Parameters:
unnorm_power: numpy.ndarray

The unnormalized cross spectrum.

mean_flux: float

The mean flux of the light curve (if a cross spectrum, the geometrical mean of the flux in the two channels)

var: float

The variance of the light curve (if a cross spectrum, the geometrical mean of the variance in the two channels)

dt: float

The sampling time of the light curve

N: int

The number of bins in the light curve

Returns:
power: numpy.nd.array

The normalized co-spectrum (real part of the cross spectrum). For ‘none’ normalization, imaginary part is returned as well.

Other Parameters:
normstr

One of 'leahy' (Leahy+83), 'frac' (fractional rms), 'abs' (absolute rms)

power_typestr

One of 'real' (real part), 'all' (all complex powers), 'abs' (absolute value)

stingray.crossspectrum.cospectra_pvalue(power, nspec)[source]

This function computes the single-trial p-value that the power was observed under the null hypothesis that there is no signal in the data.

Important: the underlying assumption that make this calculation valid is that the powers in the cross spectrum follow a Laplace distribution, and this requires that:

  1. the co-spectrum is normalized according to [Leahy 1983]_

  2. there is only white noise in the light curve. That is, there is no aperiodic variability that would change the overall shape of the power spectrum.

Also note that the p-value is for a single trial, i.e. the power currently being tested. If more than one power or more than one power spectrum are being tested, the resulting p-value must be corrected for the number of trials (Bonferroni correction).

Mathematical formulation in [Huppenkothen 2017]_.

Parameters:
powerfloat

The squared Fourier amplitude of a spectrum to be evaluated

nspecint

The number of spectra or frequency bins averaged in power. This matters because averaging spectra or frequency bins increases the signal-to-noise ratio, i.e. makes the statistical distributions of the noise narrower, such that a smaller power might be very significant in averaged spectra even though it would not be in a single cross spectrum.

Returns:
pvalfloat

The classical p-value of the observed power being consistent with the null hypothesis of white noise

References

stingray.crossspectrum.time_lag(lc1, lc2)[source]

Estimate the time lag of two light curves. Calculate time lag and uncertainty. Equation from Bendat & Piersol, 2011 [bendat-2011].

Parameters:
lc1: :class:`stingray.Lightcurve` object

The first light curve data for the channel of interest.

lc2: :class:`stingray.Lightcurve` object

The light curve data for reference band

Returns:
lagnp.ndarray

The time lag

lag_errnp.ndarray

The uncertainty in the time lag

References

stingray.crossspectrum.get_flux_generator(data, segment_size, dt=None)[source]

Get a flux generator from different segments of a data object

It is just a wrapper around stingray.fourier.get_flux_iterable_from_segments, providing this method with the information it needs to create the iterables, starting from an event list or a light curve.

Only accepts Lightcurve and EventList objects.

Parameters:
dataLightcurve or EventList

Input data

segment_sizefloat

Segment size in seconds

Returns:
flux_iterablegenerator

Generator of flux arrays.

Other Parameters:
dtfloat, default None

Sampling time of the output flux iterables. Required if input data is an event list, otherwise the light curve sampling time is selected.

Examples

>>> mean = 256
>>> length = 128
>>> times = np.sort(np.random.uniform(0, length, int(mean * length)))
>>> events = EventList(time=times, gti=[[0, length]])
>>> dt = 0.125
>>> segment_size = 4

Create a light curve >>> lc = events.to_lc(dt=dt)

Create a light curve with a different error distribution >>> lc_renorm = copy.deepcopy(lc) >>> lc_renorm.counts = lc.counts / mean >>> lc_renorm.counts_err = lc.counts_err / mean >>> lc_renorm.err_dist = “gauss”

Create an iterable from events, forgetting dt. Should fail >>> get_flux_generator(events, segment_size, dt=None) Traceback (most recent call last): … ValueError: If data is an EventList, you need to specify…

Create an iterable from events >>> iter_ev = get_flux_generator(events, segment_size, dt=dt)

Create an iterable from the light curve >>> iter_lc = get_flux_generator(lc, segment_size, dt=dt)

Create an iterable from the non-poisson light curve >>> iter_lc_nonpois = get_flux_generator(lc_renorm, segment_size, dt=dt)

Verify that they are equivalent >>> for l1, l2 in zip(iter_ev, iter_lc): assert np.allclose(l1, l2)

Note that the iterable for non-Poissonian light curves also returns the uncertainty >>> for l1, (l2, l2e) in zip(iter_lc, iter_lc_nonpois): assert np.allclose(l1, l2 * mean)


Dead-Time Corrections

stingray.deadtime.fad.FAD(data1, data2, segment_size, dt=None, norm='frac', plot=False, ax=None, smoothing_alg='gauss', smoothing_length=None, verbose=False, tolerance=0.05, strict=False, output_file=None, return_objects=False)[source]

Calculate Frequency Amplitude Difference-corrected (cross)power spectra.

Reference: Bachetti & Huppenkothen, 2018, ApJ, 853L, 21

The two input light curve must be strictly simultaneous, and recorded by two independent detectors with similar responses, so that the count rates are similar and dead time is independent. The method does not apply to different energy channels of the same instrument, or to the signal observed by two instruments with very different responses. See the paper for caveats.

Parameters:
data1Lightcurve or EventList

Input data for channel 1

data2Lightcurve or EventList

Input data for channel 2. Must be strictly simultaneous to data1 and, if a light curve, have the same binning time. Also, it must be strictly independent, e.g. from a different detector. There must be no dead time cross-talk between the two time series.

segment_size: float

The final Fourier products are averaged over many segments of the input light curves. This is the length of each segment being averaged. Note that the light curve must be long enough to have at least 30 segments, as the result gets better as one averages more and more segments.

dtfloat

Time resolution of the light curves used to produce periodograms

norm: {``frac``, ``abs``, ``leahy``, ``none``}, default ``none``

The normalization of the (real part of the) cross spectrum.

Returns:
resultsclass:astropy.table.Table object or dict or str

The content of results depends on whether return_objects is True or False. If return_objects==False, results is a Table with the following columns:

  • pds1: the corrected PDS of lc1

  • pds2: the corrected PDS of lc2

  • cs: the corrected cospectrum

  • ptot: the corrected PDS of lc1 + lc2

If return_objects is True, results is a dict, with keys named like the columns listed above but with AveragedPowerspectrum or AveragedCrossspectrum objects instead of arrays.

Other Parameters:
plotbool, default False

Plot diagnostics: check if the smoothed Fourier difference scatter is a good approximation of the data scatter.

axAxes object
If not None and plot is True, use this axis object to produce

the diagnostic plot. Otherwise, create a new figure.

smoothing_alg{‘gauss’, …}

Smoothing algorithm. For now, the only smoothing algorithm allowed is gauss, which applies a Gaussian Filter from scipy.

smoothing_lengthint, default segment_size * 3

Number of bins to smooth in gaussian window smoothing

verbose: bool, default False

Print out information on the outcome of the algorithm (recommended)

tolerancefloat, default 0.05

Accepted relative error on the FAD-corrected Fourier amplitude, to be used as success diagnostics. Should be ` stdtheor = 2 / np.sqrt(n) std = (average_corrected_fourier_diff / n).std() np.abs((std - stdtheor) / stdtheor) < tolerance `

strictbool, default False

Decide what to do if the condition on tolerance is not met. If True, raise a RuntimeError. If False, just throw a warning.

output_filestr, default None

Name of an output file (any extension automatically recognized by Astropy is fine)

stingray.deadtime.fad.calculate_FAD_correction(lc1, lc2, segment_size, norm='frac', gti=None, plot=False, ax=None, smoothing_alg='gauss', smoothing_length=None, verbose=False, tolerance=0.05, strict=False, output_file=None, return_objects=False)[source]

Calculate Frequency Amplitude Difference-corrected (cross)power spectra.

Reference: Bachetti & Huppenkothen, 2018, ApJ, 853L, 21

The two input light curve must be strictly simultaneous, and recorded by two independent detectors with similar responses, so that the count rates are similar and dead time is independent. The method does not apply to different energy channels of the same instrument, or to the signal observed by two instruments with very different responses. See the paper for caveats.

Parameters:
lc1: class:`stingray.ligthtcurve.Lightcurve`

Light curve from channel 1

lc2: class:`stingray.ligthtcurve.Lightcurve`

Light curve from channel 2. Must be strictly simultaneous to lc1 and have the same binning time. Also, it must be strictly independent, e.g. from a different detector. There must be no dead time cross-talk between the two light curves.

segment_size: float

The final Fourier products are averaged over many segments of the input light curves. This is the length of each segment being averaged. Note that the light curve must be long enough to have at least 30 segments, as the result gets better as one averages more and more segments.

norm: {``frac``, ``abs``, ``leahy``, ``none``}, default ``none``

The normalization of the (real part of the) cross spectrum.

Returns:
resultsclass:astropy.table.Table object or dict or str

The content of results depends on whether return_objects is True or False. If return_objects==False, results is a Table with the following columns:

  • pds1: the corrected PDS of lc1

  • pds2: the corrected PDS of lc2

  • cs: the corrected cospectrum

  • ptot: the corrected PDS of lc1 + lc2

If return_objects is True, results is a dict, with keys named like the columns listed above but with AveragedPowerspectrum or AveragedCrossspectrum objects instead of arrays.

Other Parameters:
plotbool, default False

Plot diagnostics: check if the smoothed Fourier difference scatter is a good approximation of the data scatter.

axAxes object
If not None and plot is True, use this axis object to produce

the diagnostic plot. Otherwise, create a new figure.

smoothing_alg{‘gauss’, …}

Smoothing algorithm. For now, the only smoothing algorithm allowed is gauss, which applies a Gaussian Filter from scipy.

smoothing_lengthint, default segment_size * 3

Number of bins to smooth in gaussian window smoothing

verbose: bool, default False

Print out information on the outcome of the algorithm (recommended)

tolerancefloat, default 0.05

Accepted relative error on the FAD-corrected Fourier amplitude, to be used as success diagnostics. Should be ` stdtheor = 2 / np.sqrt(n) std = (average_corrected_fourier_diff / n).std() np.abs((std - stdtheor) / stdtheor) < tolerance `

strictbool, default False

Decide what to do if the condition on tolerance is not met. If True, raise a RuntimeError. If False, just throw a warning.

output_filestr, default None

Name of an output file (any extension automatically recognized by Astropy is fine)

stingray.deadtime.fad.get_periodograms_from_FAD_results(FAD_results, kind='ptot')[source]

Get Stingray periodograms from FAD results.

Parameters:
FAD_resultsastropy.table.Table object or str

Results from calculate_FAD_correction, either as a Table or an output file name

kindstr, one of [‘ptot’, ‘pds1’, ‘pds2’, ‘cs’]

Kind of periodogram to get (E.g., ‘ptot’ -> PDS from the sum of the two light curves, ‘cs’ -> cospectrum, etc.)

Returns:
resultsAveragedCrossspectrum or AveragedPowerspectrum object

The periodogram.

stingray.deadtime.model.A(k, r0, td, tb, tau)[source]

Term in Eq. 39 in Zhang+95.

Parameters:
kint or array of ints

Order of the term

r0float

Detected countrate

tdfloat

Dead time

tbfloat

Bin time of the light curve

taufloat

Inverse of the incident countrate

stingray.deadtime.model.A0(r0, td, tb, tau)[source]

Term in Eq. 38 in Zhang+95.

Parameters:
r0float

Detected countrate

tdfloat

Dead time

tbfloat

Bin time of the light curve

taufloat

Inverse of the incident countrate

stingray.deadtime.model.A_single_k(k, r0, td, tb, tau)[source]

Term in Eq. 39 in Zhang+95.

Parameters:
kint

Order of the term

r0float

Detected countrate

tdfloat

Dead time

tbfloat

Bin time of the light curve

taufloat

Inverse of the incident countrate

stingray.deadtime.model.B(k, r0, td, tb, tau, limit_k=60)[source]

Term in Eq. 39 in Zhang+95, with a cut in the maximum k.

The cut can be risky. Only use if B is really 0 for high k. Use check_B to test.

Parameters:
kint or array of ints

Order of the term

r0float

Detected countrate

tdfloat

Dead time

tbfloat

Bin time of the light curve

taufloat

Inverse of the incident countrate

Other Parameters:
limit_kint

Limit to this value the number of terms in the inner loops of calculations. Check the plots returned by the check_B and check_A functions to test that this number is adequate.

stingray.deadtime.model.check_A(rate, td, tb, max_k=100, save_to=None, linthresh=1e-06, rate_is_incident=True)[source]

Test that A is well-behaved.

This function produces a plot of \(A_k - r_0^2 t_b^2\) vs \(k\), to visually check that \(A_k \rightarrow r_0^2 t_b^2\) for \(k\rightarrow\infty\), as per Eq. 43 in Zhang+95.

With this function is possible to determine how many inner loops k (limit_k in function pds_model_zhang) are necessary for a correct approximation of the dead time model

Parameters:
ratefloat

Count rate, either incident or detected (use the rate_is_incident bool to specify)

tdfloat

Dead time

tbfloat

Bin time of the light curve

Other Parameters:
max_kint

Maximum k to plot

save_tostr, default None

If not None, save the plot to this file

linthreshfloat, default 0.000001

Linear threshold for the “symlog” scale of the plot

rate_is_incidentbool, default True

If True, the input rate is the incident count rate. If False, it is the detected one.

stingray.deadtime.model.check_B(rate, td, tb, max_k=100, save_to=None, linthresh=1e-06, rate_is_incident=True)[source]

Check that \(B\rightarrow 0\) for \(k\rightarrow \infty\).

This function produces a plot of \(B_k\) vs \(k\), to visually check that \(B_k \rightarrow 0\) for \(k\rightarrow\infty\), as per Eq. 43 in Zhang+95.

With this function is possible to determine how many inner loops k (limit_k in function pds_model_zhang) are necessary for a correct approximation of the dead time model

Parameters:
ratefloat

Count rate, either incident or detected (use the rate_is_incident bool to specify)

tdfloat

Dead time

tbfloat

Bin time of the light curve

Other Parameters:
max_kint

Maximum k to plot

save_tostr, default None

If not None, save the plot to this file

linthreshfloat, default 0.000001

Linear threshold for the “symlog” scale of the plot

rate_is_incidentbool, default True

If True, the input rate is the incident count rate. If False, it is the detected one.

stingray.deadtime.model.e_m_x_x_over_factorial(x: float, m: int)[source]

Approximate the large number ratio in Eq. 34.

The original formula for \(G_n\) (eq. 34 in Zhang+95) has factors of the kind \(e^{-x} x^l / l!\). This is the product of very large numbers, that mostly balance each other. In this function, we approximate this product for large \(l\) starting from Stirling’s approximation for the factorial:

\[l! \approx \sqrt{2\pi l} (\frac{l}{e})^l A(l)\]

where \(A(l)\) is a series expansion in 1/l of the form \(1 + 1 / 12l + 1 / 288l^2 - 139/51840/l^3 + ...\). This allows to transform the product above into

\[\frac{e^{-x} x^l}{l!} \approx \frac{1}{A(l)\sqrt{2\pi l}}\left(\frac{x e}{l}\right)^l e^{-x}\]

and then, bringing the exponential into the parenthesis

\[\frac{e^{-x} x^l}{l!}\approx\frac{1}{A(l)\sqrt{2\pi l}} \left(\frac{x e^{1-x/l}}{l}\right)^l\]

The function inside the brackets has a maximum around \(x \approx l\) and is well-behaved, allowing to approximate the product for large \(l\) without the need to calculate the factorial or the exponentials directly.

stingray.deadtime.model.limit_A(rate, td, tb)[source]

Limit of A for k->infty, as per Eq. 43 in Zhang+95.

Parameters:
ratefloat

Incident count rate

tdfloat

Dead time

tbfloat

Bin time of the light curve

stingray.deadtime.model.non_paralyzable_dead_time_model(freqs, dead_time, rate, bin_time=None, limit_k=200, background_rate=0.0, n_approx=None)[source]

Calculate the dead-time-modified power spectrum.

Parameters:
freqsarray of floats

Frequency array

dead_timefloat

Dead time

ratefloat

Detected source count rate

Returns:
powerarray of floats

Power spectrum

Other Parameters:
bin_timefloat

Bin time of the light curve

limit_kint, default 200

Limit to this value the number of terms in the inner loops of calculations. Check the plots returned by the check_B and check_A functions to test that this number is adequate.

background_ratefloat, default 0

Detected background count rate. This is important to estimate when deadtime is given by the combination of the source counts and background counts (e.g. in an imaging X-ray detector).

n_approxint, default None

Number of bins to calculate the model power spectrum. If None, it will use the size of the input frequency array. Relatively simple models (e.g., low count rates compared to dead time) can use a smaller number of bins to speed up the calculation, and the final power values will be interpolated.

stingray.deadtime.model.pds_model_zhang(N, rate, td, tb, limit_k=60, rate_is_incident=True)[source]

Calculate the dead-time-modified power spectrum.

Parameters:
Nint

The number of spectral bins

ratefloat

Incident count rate

tdfloat

Dead time

tbfloat

Bin time of the light curve

Returns:
freqsarray of floats

Frequency array

powerarray of floats

Power spectrum

Other Parameters:
limit_kint

Limit to this value the number of terms in the inner loops of calculations. Check the plots returned by the check_B and check_A functions to test that this number is adequate.

rate_is_incidentbool, default True

If True, the input rate is the incident count rate. If False, it is the detected count rate.

stingray.deadtime.model.r_det(td, r_i)[source]

Calculate detected countrate given dead time and incident countrate.

Parameters:
tdfloat

Dead time

r_ifloat

Incident countrate

stingray.deadtime.model.r_in(td, r_0)[source]

Calculate incident countrate given dead time and detected countrate.

Parameters:
tdfloat

Dead time

r_0float

Detected countrate


Higher-Order Fourier and Spectral Timing Products

These classes implement higher-order Fourier analysis products (e.g. Bispectrum) and Spectral Timing related methods taking advantage of both temporal and spectral information in modern data sets.

Bispectrum

class stingray.bispectrum.Bispectrum(data=None, dt=None, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, method='fourier', maxlag=None, window=None, scale='biased', skip_checks=False, lc=None)[source]

Make a Bispectrum (auto-bispectrum) from a (binned) light curve.

The auto-bispectrum is the special case of the CrossBispectrum in which the three inputs are the same light curve, exactly as stingray.Powerspectrum is the auto case of stingray.Crossspectrum. It measures quadratic phase coupling within a single time series. For m averaged segments,

\[B(f_1, f_2) = \frac{1}{m} \sum_{i=0}^{m-1} X_i(f_1)\, X_i(f_2)\, X_i^{*}(f_1 + f_2)\]

Because X(f1) X(f2) is symmetric under f1 <-> f2, the auto case is sampled only on the positive Nyquist triangle (unlike the full signed grid of the cross case).

A single Bispectrum uses the whole light curve as one segment; use AveragedBispectrum for a statistically meaningful bicoherence and biphase.

Parameters:
datastingray.Lightcurve or stingray.events.EventList, optional

The light curve or event list to be Fourier-transformed. If an EventList is given, dt must be specified.

Attributes:
See :class:`CrossBispectrum`. ``nphots1 = nphots2 = nphots3 = nphots``. With
``method=”cumulant”`` the extra attributes ``cum3`` (the 3rd-order cumulant),
``lags``, ``maxlag``, ``window`` and ``scale`` are set, and ``bicoherence``
is ``None``.
Other Parameters:
dtfloat

The time resolution of the light curve. Only needed for an EventList.

bicoherence_norm{“kim_powers”, “sigl_chamoun”, “hagihira”}, default “kim_powers”

Which normalization to use for the bicoherence attribute. All lie in [0, 1]. With \(T_i = X_i(f_1) X_i(f_2) X_i^{*}(f_1+f_2)\):

"kim_powers"

The squared bicoherence of Kim & Powers (1979): \(b^2 = |\sum_i T_i|^2 / (\sum_i |X_i(f_1)X_i(f_2)|^2 \sum_i |X_i(f_1+f_2)|^2)\).

"sigl_chamoun"

Sigl & Chamoun (1994); the unsquared square root of the above.

"hagihira"

Hagihira (2001) / Hayashi (2007); \(b = |\sum_i T_i| / \sum_i |T_i|\).

poisson_subtractbool, default False

Subtract the Poisson-noise bias per segment (Wirnitzer 1985): \(T_i - |X_i(f_1)|^2 - |X_i(f_2)|^2 - |X_i(f_1+f_2)|^2 + 2N_i\). Only appropriate for photon-counting light curves given in counts.

bias_subtractbool, default False

Subtract the ~1/M statistical bias of the squared bicoherence, where M is the number of averaged segments (Fackrell 1996; Elgar & Guza 1988). Only defined for the "kim_powers" and "sigl_chamoun" normalizations. Can also be toggled after the fact with recompute_bicoherence().

method{“fourier”, “cumulant”}, default “fourier”

Which estimator to use. "fourier" is the direct Fourier-decomposition estimator (Maccarone 2013) described above. "cumulant" is the legacy indirect estimator: the bispectrum as the 2-D Fourier transform of the 3rd-order cumulant of the light curve (Rao & Gabr 1984), retained for backward comparison. It uses maxlag, window and scale below, ignores bicoherence_norm / poisson_subtract / bias_subtract, and produces no bicoherence; the auto-bispectrum only.

maxlagint, optional

(method="cumulant" only.) Maximum lag of the 3rd-order cumulant. Defaults to half the light-curve length.

windowstr, optional

(method="cumulant" only.) Lag window to taper the cumulant ("parzen", "hamming", "hanning", …); None applies no window.

scale{“biased”, “unbiased”}, default “biased”

(method="cumulant" only.) Normalization of the 3rd-order cumulant.

skip_checksbool, default False

Skip initial checks.

lcstingray.Lightcurve, optional

For backwards compatibility only. Deprecated; use data.

References

Rao, T. S. & Gabr, M. M., An Introduction to Bispectral Analysis and Bilinear Time Series Models, Lecture Notes in Statistics 24 (1984).

static from_events(events, dt, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, silent=False)[source]

Calculate a Bispectrum from an event list.

static from_lightcurve(lc, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, silent=False)[source]

Calculate a Bispectrum from a light curve.

static from_stingray_timeseries(ts, flux_attr, error_flux_attr=None, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, silent=False)[source]

Calculate a Bispectrum from a time series.

static from_time_array(times, dt, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, silent=False)[source]

Calculate a Bispectrum from an array of event times.

initial_checks(data=None, dt=None, segment_size=None)[source]

Basic checks on a single (auto) input.

plot_cum3(ax=None, save=False, filename=None)[source]

Plot the 3rd-order cumulant as a function of lag (method="cumulant" only).

Parameters:
axmatplotlib.axes.Axes, optional

The axes on which to plot. If None, a new figure and axes are created.

savebool, default False

If True, save the figure to a file.

filenamestr, optional

The filename to save the figure to. If None, defaults to “bispec_cum3.png”.


AveragedBispectrum

class stingray.bispectrum.AveragedBispectrum(data=None, segment_size=None, gti=None, dt=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, method='fourier', maxlag=None, window=None, scale='biased', silent=False, save_all=False, save_diagonal=False, skip_checks=False, lc=None)[source]

Make an averaged (auto-)bispectrum from a light curve or event list.

The light curve is split into segments of length segment_size, an auto-bispectrum is computed per segment, and the results are averaged (see Bispectrum). Averaging is what makes the bicoherence and biphase statistically meaningful.

Parameters:
datastingray.Lightcurve, iterable of them, or stingray.events.EventList

The light curve data to be Fourier-transformed.

segment_sizefloat

The size, in seconds, of each averaged segment.

Other Parameters:
gti[[gti0_0, gti0_1], ...]

Good time intervals.

dtfloat

The time resolution of the light curve (needed for an EventList).

silentbool, default False

Do not show a progress bar.

save_allbool, default False

Save all intermediate 2D bispectra (under bispec_all, shape m x nf x nf). Large; use with care.

save_diagonalbool, default False

Save only the diagonal of each intermediate bispectrum (under bispec_diagonal, shape m x nf). Enough for plot_jellyfish(), at a fraction of the save_all memory.

skip_checksbool, default False

Skip initial checks.

lcstingray.Lightcurve, optional

For backwards compatibility only. Deprecated; use data.

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

dict() → dict

Return a dictionary representation of the object.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

static from_events(events, dt, segment_size, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate an AveragedBispectrum from an event list.

static from_lc_iterable(iter_lc, dt, segment_size, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate an AveragedBispectrum from an iterable of light curves.

static from_lightcurve(lc, segment_size, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate an AveragedBispectrum from a light curve.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

static from_stingray_timeseries(ts, flux_attr, segment_size, error_flux_attr=None, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate an AveragedBispectrum from a time series.

static from_time_array(times, dt, segment_size, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate an AveragedBispectrum from an array of event times.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

initial_checks(data=None, dt=None, segment_size=None)[source]

Basic checks on a single (auto) input.

internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

plot_bicoherence(ax=None, save=False, filename=None, log=False)

Plot the bicoherence as a function of frequency.

Set log=True to plot \(\log_{10}\) of the bicoherence, which stretches the low end and makes weak couplings easier to see. Since the bicoherence lies in [0, 1], the log is non-positive; bins that are exactly zero (and the unresolved region) are left blank.

plot_cum3(ax=None, save=False, filename=None)

Plot the 3rd-order cumulant as a function of lag (method="cumulant" only).

Parameters:
axmatplotlib.axes.Axes, optional

The axes on which to plot. If None, a new figure and axes are created.

savebool, default False

If True, save the figure to a file.

filenamestr, optional

The filename to save the figure to. If None, defaults to “bispec_cum3.png”.

plot_jellyfish(f0=None, freqs=None, bicoherence_levels=(0.01, 0.05), fundamental_color='tab:blue', subharmonic_color='tab:orange', other_color='0.6', ax=None, save=False, filename=None)

Draw a “jellyfish plot” of the (auto-)bispectrum (Nathan et al. 2022).

For each frequency \(\nu\) on the diagonal (\(f_1 = f_2 = \nu\), which couples \(\nu\) and its harmonic \(2\nu\)), the per-segment triple products are accumulated segment by segment and the running (cumulative) sum is traced as a path in the complex plane, normalized so the amplitude of its end point is the bicoherence (Sigl & Chamoun convention) and its angle is the biphase.

Phase-coupled frequencies walk outward into a “tentacle”; uncoupled ones random-walk near the origin, forming the “body”. Reference circles of constant bicoherence give the scale.

Requires an averaged (cross-)bispectrum built with save_diagonal=True (which keeps only the diagonal the plot needs) or save_all=True.

Parameters:
f0float, optional

A reference (e.g. QPO fundamental) frequency to highlight. The diagonal path closest to f0 is drawn in fundamental_color, and the path closest to f0 / 2 in subharmonic_color. If None, every path is drawn in other_color.

Returns:
axmatplotlib.axes.Axes

The axes with the plot.

Other Parameters:
freqsiterable of float, optional

The diagonal frequencies to draw. Defaults to every resolved diagonal frequency.

bicoherence_levelsiterable of float, default (0.01, 0.05)

Radii, in bicoherence units, of the reference circles.

fundamental_color, subharmonic_color, other_colorcolor

Colors for the fundamental, subharmonic and remaining paths.

axmatplotlib.axes.Axes, optional

Axes to draw onto. A new one is created if None.

savebool, default False

If True, save the figure to filename.

filenamestr, optional

File name to save to. Defaults to bispec_jellyfish.png.

plot_mag(ax=None, save=False, filename=None)

Plot the magnitude of the bispectrum as a function of frequency.

plot_phase(ax=None, save=False, filename=None)

Plot the biphase as a function of frequency.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

recompute_bicoherence(norm=None, bias_subtract=False, inplace=False)

Recompute the bicoherence under a different normalization or debiasing.

Uses the accumulated bispectrum sums stored on the object, so no FFTs are recomputed. See Bispectrum for the definitions.

Parameters:
norm{“kim_powers”, “sigl_chamoun”, “hagihira”}, optional

The bicoherence normalization. Defaults to the object’s current bicoherence_norm (useful when only toggling bias_subtract).

Returns:
bicoherencenumpy.ndarray

The recomputed bicoherence.

Other Parameters:
bias_subtractbool, default False

Subtract the ~1/M statistical bias of the squared bicoherence (M = number of averaged segments; Fackrell 1996, Elgar & Guza 1988). See stingray.fourier.bicoherence_from_sums(). Only defined for "kim_powers" and "sigl_chamoun".

inplacebool, default False

If True, also overwrite self.bicoherence, self.bicoherence_norm and self.bias_subtract.

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


CrossBispectrum

class stingray.bispectrum.CrossBispectrum(data1=None, data2=None, data3=None, dt=None, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, skip_checks=False)[source]

Make a CrossBispectrum from up to three (binned) light curves.

The cross-bispectrum is the higher-order analogue of the cross spectrum. It measures quadratic phase coupling between channels: for three (real) simultaneous time series with per-segment Fourier transforms \(X_i\), \(Y_i\), \(Z_i\),

\[B(f_1, f_2) = \frac{1}{m} \sum_{i=0}^{m-1} X_i(f_1)\, Y_i(f_2)\, Z_i^{*}(f_1 + f_2).\]

The Bispectrum (auto-bispectrum) is the special case where the three inputs are the same light curve, in the same way that stingray.Powerspectrum is the auto case of stingray.Crossspectrum. In X-ray timing the usual use is two energy bands: e.g. testing whether the variability at f1 + f2 in one band is quadratically coupled to f1, f2 in another.

Unlike the auto-bispectrum, the cross-bispectrum is not symmetric under f1 <-> f2 (because X(f1) Y(f2) is not), so it is sampled on the full signed frequency plane (f1, f2 positive and negative, with |f1 + f2| <= f_Nyq) rather than the Nyquist triangle.

You can also make an empty CrossBispectrum object to populate with your own data. A single object (no segment_size) uses the whole light curve as one segment; for a statistically meaningful cross-bicoherence and cross-biphase you normally want AveragedCrossBispectrum.

Parameters:
data1, data2, data3stingray.Lightcurve or EventList, optional

The three channels, mapped to the factors X(f1), Y(f2) and Z(f1+f2) respectively. data2 and data3 default to data1 (recovering the auto-bispectrum). All three must be simultaneous (same dt, same time bins, compatible GTIs). If EventList, dt must be specified.

Attributes:
freqnumpy.ndarray

The (signed) Fourier frequencies the transform samples.

bispecnumpy.ndarray

The complex cross-bispectrum, an nf x nf matrix. The unresolved region (|f1 + f2| > f_Nyq) is set to NaN.

bicoherencenumpy.ndarray

The cross-bicoherence in [0, 1], in the bicoherence_norm convention. Use recompute_bicoherence() for a different one.

bicoherence_normstr

The normalization used for bicoherence.

poisson_subtractedbool

Whether the Poisson-noise bias was subtracted.

bias_subtractbool

Whether the ~1/M statistical bias of the squared bicoherence was subtracted.

channels_overlapbool

Whether the input channels share the same photons.

biphasenumpy.ndarray

The phase of the cross-bispectrum, defined over the full \(2\pi\) interval. Also available as bispec_phase.

bispec_magnumpy.ndarray

Magnitude of the cross-bispectrum.

bispec_err, biphase_errnumpy.ndarray

Approximate 1-sigma uncertainties (as in Bispectrum).

dffloat

The frequency resolution.

mint

The number of averaged cross-bispectra.

nint

The number of data points in each segment.

nphots1, nphots2, nphots3float

The mean photon count per segment in each channel.

nphotsfloat

The geometric mean of nphots1, nphots2 and nphots3.

Other Parameters:
dtfloat

The time resolution of the light curves. Only needed for EventList inputs.

gti[[gti0_0, gti0_1], ...]

Good time intervals. Defaults to the intersection of the inputs’ GTIs.

bicoherence_norm{“kim_powers”, “sigl_chamoun”, “hagihira”}, default “kim_powers”

Which normalization to use for the bicoherence attribute. See Bispectrum for the definitions; the cross denominators are \(\sum_i |X_i(f_1) Y_i(f_2)|^2\) and \(\sum_i |Z_i(f_1+f_2)|^2\).

poisson_subtractbool, default False

Subtract the Poisson-noise bias. Only has an effect when channels_overlap is True; for independent channels the Poisson noise is uncorrelated between factors and the cross-bispectrum is unbiased.

bias_subtractbool, default False

Subtract the ~1/M statistical bias of the squared bicoherence, where M is the number of averaged segments (Fackrell 1996; Elgar & Guza 1988). Only defined for the "kim_powers" and "sigl_chamoun" normalizations. Can also be toggled after the fact with recompute_bicoherence().

channels_overlapbool, default False

Whether the three channels are the same photon stream. Independent energy bands (the usual cross case): leave False.

skip_checksbool, default False

Skip initial checks, for speed or other reasons.

References

      1. Maccarone, MNRAS 435, 3547 (2013).

      1. Kim and E. J. Powers, IEEE Trans. Plasma Sci. PS-7, 120 (1979).

static from_events(events1, events2=None, events3=None, dt=None, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False)[source]

Calculate a CrossBispectrum from event lists.

static from_lightcurve(lc1, lc2=None, lc3=None, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False)[source]

Calculate a CrossBispectrum from light curves.

static from_time_array(times1, times2, times3, dt, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False)[source]

Calculate a CrossBispectrum from arrays of event times.

initial_checks(data1=None, data2=None, data3=None, dt=None, segment_size=None)[source]

Run basic checks on the inputs.

Returns True if the inputs can be used to build a cross-bispectrum, raises otherwise. An empty (None) data1 returns False so that an empty object is created.

plot_bicoherence(ax=None, save=False, filename=None, log=False)[source]

Plot the bicoherence as a function of frequency.

Set log=True to plot \(\log_{10}\) of the bicoherence, which stretches the low end and makes weak couplings easier to see. Since the bicoherence lies in [0, 1], the log is non-positive; bins that are exactly zero (and the unresolved region) are left blank.

plot_jellyfish(f0=None, freqs=None, bicoherence_levels=(0.01, 0.05), fundamental_color='tab:blue', subharmonic_color='tab:orange', other_color='0.6', ax=None, save=False, filename=None)[source]

Draw a “jellyfish plot” of the (auto-)bispectrum (Nathan et al. 2022).

For each frequency \(\nu\) on the diagonal (\(f_1 = f_2 = \nu\), which couples \(\nu\) and its harmonic \(2\nu\)), the per-segment triple products are accumulated segment by segment and the running (cumulative) sum is traced as a path in the complex plane, normalized so the amplitude of its end point is the bicoherence (Sigl & Chamoun convention) and its angle is the biphase.

Phase-coupled frequencies walk outward into a “tentacle”; uncoupled ones random-walk near the origin, forming the “body”. Reference circles of constant bicoherence give the scale.

Requires an averaged (cross-)bispectrum built with save_diagonal=True (which keeps only the diagonal the plot needs) or save_all=True.

Parameters:
f0float, optional

A reference (e.g. QPO fundamental) frequency to highlight. The diagonal path closest to f0 is drawn in fundamental_color, and the path closest to f0 / 2 in subharmonic_color. If None, every path is drawn in other_color.

Returns:
axmatplotlib.axes.Axes

The axes with the plot.

Other Parameters:
freqsiterable of float, optional

The diagonal frequencies to draw. Defaults to every resolved diagonal frequency.

bicoherence_levelsiterable of float, default (0.01, 0.05)

Radii, in bicoherence units, of the reference circles.

fundamental_color, subharmonic_color, other_colorcolor

Colors for the fundamental, subharmonic and remaining paths.

axmatplotlib.axes.Axes, optional

Axes to draw onto. A new one is created if None.

savebool, default False

If True, save the figure to filename.

filenamestr, optional

File name to save to. Defaults to bispec_jellyfish.png.

plot_mag(ax=None, save=False, filename=None)[source]

Plot the magnitude of the bispectrum as a function of frequency.

plot_phase(ax=None, save=False, filename=None)[source]

Plot the biphase as a function of frequency.

recompute_bicoherence(norm=None, bias_subtract=False, inplace=False)[source]

Recompute the bicoherence under a different normalization or debiasing.

Uses the accumulated bispectrum sums stored on the object, so no FFTs are recomputed. See Bispectrum for the definitions.

Parameters:
norm{“kim_powers”, “sigl_chamoun”, “hagihira”}, optional

The bicoherence normalization. Defaults to the object’s current bicoherence_norm (useful when only toggling bias_subtract).

Returns:
bicoherencenumpy.ndarray

The recomputed bicoherence.

Other Parameters:
bias_subtractbool, default False

Subtract the ~1/M statistical bias of the squared bicoherence (M = number of averaged segments; Fackrell 1996, Elgar & Guza 1988). See stingray.fourier.bicoherence_from_sums(). Only defined for "kim_powers" and "sigl_chamoun".

inplacebool, default False

If True, also overwrite self.bicoherence, self.bicoherence_norm and self.bias_subtract.


AveragedCrossBispectrum

class stingray.bispectrum.AveragedCrossBispectrum(data1=None, data2=None, data3=None, segment_size=None, gti=None, dt=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False, save_all=False, save_diagonal=False, skip_checks=False)[source]

Make an averaged cross-bispectrum from three simultaneous light curves.

Each channel is split into segments of length segment_size on a common GTI, a cross-bispectrum is computed per segment, and the results are averaged. See CrossBispectrum for the estimator definition, and AveragedBispectrum for the (auto) memory-saving save_all / save_diagonal options, which apply here unchanged.

Parameters:
data1, data2, data3stingray.Lightcurve, iterable, or EventList

The three channels. data2/data3 default to data1.

segment_sizefloat

The size, in seconds, of each averaged segment.

Other Parameters:
See :class:`CrossBispectrum`, plus ``dt``, ``silent``, ``save_all`` and
``save_diagonal`` as in :class:`AveragedBispectrum`.
add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

dict() → dict

Return a dictionary representation of the object.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

static from_events(events1, events2, events3, dt, segment_size, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate an AveragedCrossBispectrum from event lists.

static from_lightcurve(lc1, lc2, lc3, segment_size, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate an AveragedCrossBispectrum from light curves.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

static from_time_array(times1, times2, times3, dt, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False)

Calculate a CrossBispectrum from arrays of event times.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

initial_checks(data1=None, data2=None, data3=None, dt=None, segment_size=None)[source]

Run basic checks on the inputs.

Returns True if the inputs can be used to build a cross-bispectrum, raises otherwise. An empty (None) data1 returns False so that an empty object is created.

internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

plot_bicoherence(ax=None, save=False, filename=None, log=False)

Plot the bicoherence as a function of frequency.

Set log=True to plot \(\log_{10}\) of the bicoherence, which stretches the low end and makes weak couplings easier to see. Since the bicoherence lies in [0, 1], the log is non-positive; bins that are exactly zero (and the unresolved region) are left blank.

plot_jellyfish(f0=None, freqs=None, bicoherence_levels=(0.01, 0.05), fundamental_color='tab:blue', subharmonic_color='tab:orange', other_color='0.6', ax=None, save=False, filename=None)

Draw a “jellyfish plot” of the (auto-)bispectrum (Nathan et al. 2022).

For each frequency \(\nu\) on the diagonal (\(f_1 = f_2 = \nu\), which couples \(\nu\) and its harmonic \(2\nu\)), the per-segment triple products are accumulated segment by segment and the running (cumulative) sum is traced as a path in the complex plane, normalized so the amplitude of its end point is the bicoherence (Sigl & Chamoun convention) and its angle is the biphase.

Phase-coupled frequencies walk outward into a “tentacle”; uncoupled ones random-walk near the origin, forming the “body”. Reference circles of constant bicoherence give the scale.

Requires an averaged (cross-)bispectrum built with save_diagonal=True (which keeps only the diagonal the plot needs) or save_all=True.

Parameters:
f0float, optional

A reference (e.g. QPO fundamental) frequency to highlight. The diagonal path closest to f0 is drawn in fundamental_color, and the path closest to f0 / 2 in subharmonic_color. If None, every path is drawn in other_color.

Returns:
axmatplotlib.axes.Axes

The axes with the plot.

Other Parameters:
freqsiterable of float, optional

The diagonal frequencies to draw. Defaults to every resolved diagonal frequency.

bicoherence_levelsiterable of float, default (0.01, 0.05)

Radii, in bicoherence units, of the reference circles.

fundamental_color, subharmonic_color, other_colorcolor

Colors for the fundamental, subharmonic and remaining paths.

axmatplotlib.axes.Axes, optional

Axes to draw onto. A new one is created if None.

savebool, default False

If True, save the figure to filename.

filenamestr, optional

File name to save to. Defaults to bispec_jellyfish.png.

plot_mag(ax=None, save=False, filename=None)

Plot the magnitude of the bispectrum as a function of frequency.

plot_phase(ax=None, save=False, filename=None)

Plot the biphase as a function of frequency.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

recompute_bicoherence(norm=None, bias_subtract=False, inplace=False)

Recompute the bicoherence under a different normalization or debiasing.

Uses the accumulated bispectrum sums stored on the object, so no FFTs are recomputed. See Bispectrum for the definitions.

Parameters:
norm{“kim_powers”, “sigl_chamoun”, “hagihira”}, optional

The bicoherence normalization. Defaults to the object’s current bicoherence_norm (useful when only toggling bias_subtract).

Returns:
bicoherencenumpy.ndarray

The recomputed bicoherence.

Other Parameters:
bias_subtractbool, default False

Subtract the ~1/M statistical bias of the squared bicoherence (M = number of averaged segments; Fackrell 1996, Elgar & Guza 1988). See stingray.fourier.bicoherence_from_sums(). Only defined for "kim_powers" and "sigl_chamoun".

inplacebool, default False

If True, also overwrite self.bicoherence, self.bicoherence_norm and self.bias_subtract.

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


Dynamical Bispectrum

class stingray.bispectrum.DynamicalBispectrum(data=None, segment_size=None, bin_size=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, store='diagonal', gti=None, sample_time=None, skip_checks=False)[source]

Make a time-resolved (dynamical) auto-bispectrum from a single input.

The auto special case of DynamicalCrossBispectrum (the three channels are the same light curve), analogous to stingray.DynamicalPowerspectrum. See DynamicalCrossBispectrum for the full description of the two time scales (segment_size/bin_size), the store option, and the plotting/rebinning/tracking methods.

Parameters:
datastingray.Lightcurve or stingray.events.EventList

The light curve or event list. For an event list, sample_time must be given.

segment_sizefloat

Length, in seconds, of the FFT segments averaged inside each time bin.

bin_sizefloat

Length, in seconds, of each dynamical time bin.

Other Parameters:
See :class:`DynamicalCrossBispectrum` (``channels_overlap`` is always True
here).
add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

dict() → dict

Return a dictionary representation of the object.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

static from_events(events1, events2, events3, dt, segment_size, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False, save_all=False, save_diagonal=False)

Calculate an AveragedCrossBispectrum from event lists.

static from_lightcurve(lc1, lc2, lc3, segment_size, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False, save_all=False, save_diagonal=False)

Calculate an AveragedCrossBispectrum from light curves.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

static from_time_array(times1, times2, times3, dt, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False)

Calculate a CrossBispectrum from arrays of event times.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

initial_checks(data1=None, data2=None, data3=None, dt=None, segment_size=None)

Run basic checks on the inputs.

Returns True if the inputs can be used to build a cross-bispectrum, raises otherwise. An empty (None) data1 returns False so that an empty object is created.

internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

plot_bicoherence(ax=None, save=False, filename=None, log=False)

Plot the bicoherence as a function of frequency.

Set log=True to plot \(\log_{10}\) of the bicoherence, which stretches the low end and makes weak couplings easier to see. Since the bicoherence lies in [0, 1], the log is non-positive; bins that are exactly zero (and the unresolved region) are left blank.

plot_diagonal(ax=None, cmap='viridis', vmin=0.0, vmax=1.0, colorbar=True, log=False)

Plot the diagonal dynamical bicoherence b(nu, nu, t).

This is the closest analogue of the dynamical power spectrum: a nu (with f1 = f2 = nu) versus time image, where nu couples to its harmonic 2 nu. Works from either store. Set log=True for a logarithmic (LogNorm) colour scale.

plot_frame(t, ax=None, cmap='viridis', vmin=0.0, vmax=1.0, colorbar=True, log=False)

Plot the full (f1, f2) bicoherence map at the time bin nearest t.

Requires store="full". Set log=True for a logarithmic (LogNorm) colour scale.

plot_jellyfish(f0=None, freqs=None, bicoherence_levels=(0.01, 0.05), fundamental_color='tab:blue', subharmonic_color='tab:orange', other_color='0.6', ax=None, save=False, filename=None)

Draw a “jellyfish plot” of the (auto-)bispectrum (Nathan et al. 2022).

For each frequency \(\nu\) on the diagonal (\(f_1 = f_2 = \nu\), which couples \(\nu\) and its harmonic \(2\nu\)), the per-segment triple products are accumulated segment by segment and the running (cumulative) sum is traced as a path in the complex plane, normalized so the amplitude of its end point is the bicoherence (Sigl & Chamoun convention) and its angle is the biphase.

Phase-coupled frequencies walk outward into a “tentacle”; uncoupled ones random-walk near the origin, forming the “body”. Reference circles of constant bicoherence give the scale.

Requires an averaged (cross-)bispectrum built with save_diagonal=True (which keeps only the diagonal the plot needs) or save_all=True.

Parameters:
f0float, optional

A reference (e.g. QPO fundamental) frequency to highlight. The diagonal path closest to f0 is drawn in fundamental_color, and the path closest to f0 / 2 in subharmonic_color. If None, every path is drawn in other_color.

Returns:
axmatplotlib.axes.Axes

The axes with the plot.

Other Parameters:
freqsiterable of float, optional

The diagonal frequencies to draw. Defaults to every resolved diagonal frequency.

bicoherence_levelsiterable of float, default (0.01, 0.05)

Radii, in bicoherence units, of the reference circles.

fundamental_color, subharmonic_color, other_colorcolor

Colors for the fundamental, subharmonic and remaining paths.

axmatplotlib.axes.Axes, optional

Axes to draw onto. A new one is created if None.

savebool, default False

If True, save the figure to filename.

filenamestr, optional

File name to save to. Defaults to bispec_jellyfish.png.

plot_mag(ax=None, save=False, filename=None)

Plot the magnitude of the bispectrum as a function of frequency.

plot_montage(times=None, ncols=5, cmap='viridis', vmin=0.0, vmax=1.0, log=False)

Plot a grid of full (f1, f2) maps over time. Requires store="full".

Set log=True for a logarithmic (LogNorm) colour scale.

plot_phase(ax=None, save=False, filename=None)

Plot the biphase as a function of frequency.

plot_slice(f1, ax=None, cmap='viridis', vmin=0.0, vmax=1.0, colorbar=True, log=False)

Plot the bicoherence at a fixed f1 as a function of (f2, time).

Requires store="full". Set log=True for a logarithmic (LogNorm) colour scale.

plot_trace(f1, f2, axes=None)

Plot the bicoherence and biphase at a fixed (f1, f2) versus time.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

rebin_by_n_intervals(n, method='sum')

Rebin in time by combining n consecutive intervals.

The additive bispectrum sums are combined (not the finished bicoherence, which is a ratio and cannot be averaged), then the bicoherence and biphase are recomputed. method is accepted for API parity with DynamicalCrossspectrum but the sums are always summed.

rebin_frequency(df_new, method='sum')

Rebin the (diagonal) frequency axis to a coarser df_new.

Only implemented for store="diagonal". As for rebin_time(), the additive sums are combined and the bicoherence recomputed.

rebin_time(dt_new, method='sum')

Rebin to a coarser time resolution dt_new (an integer multiple of dt).

Combines the additive sums over consecutive bins and recomputes the bicoherence, so the result is the correct averaged bicoherence over the wider bin – not an average of bicoherence values.

recompute_bicoherence(norm=None, bias_subtract=False, inplace=False)

Recompute the bicoherence under a different normalization or debiasing.

Uses the accumulated bispectrum sums stored on the object, so no FFTs are recomputed. See Bispectrum for the definitions.

Parameters:
norm{“kim_powers”, “sigl_chamoun”, “hagihira”}, optional

The bicoherence normalization. Defaults to the object’s current bicoherence_norm (useful when only toggling bias_subtract).

Returns:
bicoherencenumpy.ndarray

The recomputed bicoherence.

Other Parameters:
bias_subtractbool, default False

Subtract the ~1/M statistical bias of the squared bicoherence (M = number of averaged segments; Fackrell 1996, Elgar & Guza 1988). See stingray.fourier.bicoherence_from_sums(). Only defined for "kim_powers" and "sigl_chamoun".

inplacebool, default False

If True, also overwrite self.bicoherence, self.bicoherence_norm and self.bias_subtract.

shift_and_add(f0_list, nbins=None)

Shift-and-add the diagonal bicoherence, aligning each bin to its f0.

For each time bin i the diagonal sums are shifted so that f0_list[i] lands on a common reference bin, then co-added; the bicoherence is recomputed from the co-added sums. This tracks a drifting coupling (e.g. a QPO fundamental) the way the kHz-QPO shift-and-add tracks a drifting Lorentzian.

Parameters:
f0_listiterable of float

The reference frequency in each time bin (length n_time).

Returns:
rel_freqnumpy.ndarray

Frequency relative to f0 (0 at the reference).

bicoherencenumpy.ndarray

The shifted-and-added bicoherence.

biphasenumpy.ndarray

The corresponding biphase.

Other Parameters:
nbinsint, optional

Length of the output (relative-frequency) axis. Defaults to the number of frequency bins.

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

trace(f1, f2)

Return (time, bicoherence(t), biphase(t)) at a fixed (f1, f2).

For store="diagonal" only diagonal points (f1 = f2) are available.

trace_maximum(min_freq=None, max_freq=None)

Trace the peak-bicoherence diagonal frequency index in each time bin.

Returns the array of frequency indices (into freq) of the maximum diagonal bicoherence between min_freq and max_freq per bin – the bicoherence analogue of trace_maximum().

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


Dynamical CrossBispectrum

class stingray.bispectrum.DynamicalCrossBispectrum(data1=None, data2=None, data3=None, segment_size=None, bin_size=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, store='diagonal', gti=None, sample_time=None, skip_checks=False)[source]

Make a time-resolved (dynamical) cross-bispectrum.

This is the higher-order analogue of stingray.DynamicalCrossspectrum. The observation is divided into time bins of length bin_size; within each bin an AveragedCrossBispectrum is computed over segments of length segment_size, and the results are stacked as a function of both time and frequency. It traces how quadratic phase coupling between channels evolves.

Unlike the dynamical power spectrum, the time unit is a block of several segments, not a single segment: a bicoherence built from one segment is identically 1 and carries no information, so it only becomes meaningful once several segments are averaged. Hence the two time scales segment_size (the FFT length, which sets df and the Nyquist frequency) and bin_size (the length of each dynamical row); the time resolution is bin_size.

Because each time bin holds a full 2-D (f1, f2) map, the stored object is a 3-D cube. That is expensive, so by default only the diagonal f1 = f2 = nu is kept (store="diagonal"), giving a nu x time image directly analogous to dyn_ps; pass store="full" to keep the whole cube (needed for plot_slice(), plot_frame(), plot_montage()).

Parameters:
data1, data2, data3stingray.Lightcurve or stingray.events.EventList

The three channels, mapped to X(f1), Y(f2), Z(f1+f2). data2/data3 default to data1 (the auto case). For event lists, sample_time must be given.

segment_sizefloat

Length, in seconds, of the FFT segments averaged inside each time bin.

bin_sizefloat

Length, in seconds, of each dynamical time bin. Must be at least segment_size; bin_size / segment_size segments are averaged per bin, and a warning is issued if that is small (the bicoherence bias floor is ~1/sqrt(m)).

Attributes:
freqnumpy.ndarray

The frequency axis (signed for the cross case, positive for the auto case).

timenumpy.ndarray

Mid-point time of each dynamical bin.

dyn_bicoherencenumpy.ndarray

The bicoherence per time bin: (n_time, nf) if store="diagonal", else (n_time, nf, nf).

dyn_bispecnumpy.ndarray

The complex cross-bispectrum per time bin, same shape as dyn_bicoherence.

dyn_biphasenumpy.ndarray

The biphase per time bin, same shape.

validnumpy.ndarray

(nf, nf) boolean mask of the resolved region.

valid_diagnumpy.ndarray

(nf,) boolean mask of the resolved diagonal frequencies.

df, dtfloat

Frequency resolution, and time resolution (= bin_size).

mint

Number of segments averaged per time bin.

nsegnumpy.ndarray

Number of segments actually averaged in each bin.

Other Parameters:
bicoherence_norm{“kim_powers”, “sigl_chamoun”, “hagihira”}, default “kim_powers”

Bicoherence normalization (see Bispectrum).

poisson_subtractbool, default False

Subtract the per-bin Poisson-noise bias. For the cross case only has an effect when channels_overlap is True. Note the bias scales as ~1/sqrt(N_bin) and a bin holds fewer photons than the whole observation, so the correction matters more per row than for a static bispectrum.

channels_overlapbool, default False

Whether the three channels share the same photons.

store{“diagonal”, “full”}, default “diagonal”

Whether to keep only the diagonal of each time bin (light) or the whole (f1, f2) map (heavy).

gti[[gti0_0, gti0_1], ...]

Good time intervals. Defaults to the intersection of the inputs’ GTIs.

sample_timefloat

Time resolution of the light curves created from event lists. Required for EventList inputs.

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

dict() → dict

Return a dictionary representation of the object.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

static from_events(events1, events2, events3, dt, segment_size, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False, save_all=False, save_diagonal=False)

Calculate an AveragedCrossBispectrum from event lists.

static from_lightcurve(lc1, lc2, lc3, segment_size, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False, save_all=False, save_diagonal=False)

Calculate an AveragedCrossBispectrum from light curves.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

static from_time_array(times1, times2, times3, dt, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False)

Calculate a CrossBispectrum from arrays of event times.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

initial_checks(data1=None, data2=None, data3=None, dt=None, segment_size=None)

Run basic checks on the inputs.

Returns True if the inputs can be used to build a cross-bispectrum, raises otherwise. An empty (None) data1 returns False so that an empty object is created.

internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

plot_bicoherence(ax=None, save=False, filename=None, log=False)

Plot the bicoherence as a function of frequency.

Set log=True to plot \(\log_{10}\) of the bicoherence, which stretches the low end and makes weak couplings easier to see. Since the bicoherence lies in [0, 1], the log is non-positive; bins that are exactly zero (and the unresolved region) are left blank.

plot_diagonal(ax=None, cmap='viridis', vmin=0.0, vmax=1.0, colorbar=True, log=False)[source]

Plot the diagonal dynamical bicoherence b(nu, nu, t).

This is the closest analogue of the dynamical power spectrum: a nu (with f1 = f2 = nu) versus time image, where nu couples to its harmonic 2 nu. Works from either store. Set log=True for a logarithmic (LogNorm) colour scale.

plot_frame(t, ax=None, cmap='viridis', vmin=0.0, vmax=1.0, colorbar=True, log=False)[source]

Plot the full (f1, f2) bicoherence map at the time bin nearest t.

Requires store="full". Set log=True for a logarithmic (LogNorm) colour scale.

plot_jellyfish(f0=None, freqs=None, bicoherence_levels=(0.01, 0.05), fundamental_color='tab:blue', subharmonic_color='tab:orange', other_color='0.6', ax=None, save=False, filename=None)

Draw a “jellyfish plot” of the (auto-)bispectrum (Nathan et al. 2022).

For each frequency \(\nu\) on the diagonal (\(f_1 = f_2 = \nu\), which couples \(\nu\) and its harmonic \(2\nu\)), the per-segment triple products are accumulated segment by segment and the running (cumulative) sum is traced as a path in the complex plane, normalized so the amplitude of its end point is the bicoherence (Sigl & Chamoun convention) and its angle is the biphase.

Phase-coupled frequencies walk outward into a “tentacle”; uncoupled ones random-walk near the origin, forming the “body”. Reference circles of constant bicoherence give the scale.

Requires an averaged (cross-)bispectrum built with save_diagonal=True (which keeps only the diagonal the plot needs) or save_all=True.

Parameters:
f0float, optional

A reference (e.g. QPO fundamental) frequency to highlight. The diagonal path closest to f0 is drawn in fundamental_color, and the path closest to f0 / 2 in subharmonic_color. If None, every path is drawn in other_color.

Returns:
axmatplotlib.axes.Axes

The axes with the plot.

Other Parameters:
freqsiterable of float, optional

The diagonal frequencies to draw. Defaults to every resolved diagonal frequency.

bicoherence_levelsiterable of float, default (0.01, 0.05)

Radii, in bicoherence units, of the reference circles.

fundamental_color, subharmonic_color, other_colorcolor

Colors for the fundamental, subharmonic and remaining paths.

axmatplotlib.axes.Axes, optional

Axes to draw onto. A new one is created if None.

savebool, default False

If True, save the figure to filename.

filenamestr, optional

File name to save to. Defaults to bispec_jellyfish.png.

plot_mag(ax=None, save=False, filename=None)

Plot the magnitude of the bispectrum as a function of frequency.

plot_montage(times=None, ncols=5, cmap='viridis', vmin=0.0, vmax=1.0, log=False)[source]

Plot a grid of full (f1, f2) maps over time. Requires store="full".

Set log=True for a logarithmic (LogNorm) colour scale.

plot_phase(ax=None, save=False, filename=None)

Plot the biphase as a function of frequency.

plot_slice(f1, ax=None, cmap='viridis', vmin=0.0, vmax=1.0, colorbar=True, log=False)[source]

Plot the bicoherence at a fixed f1 as a function of (f2, time).

Requires store="full". Set log=True for a logarithmic (LogNorm) colour scale.

plot_trace(f1, f2, axes=None)[source]

Plot the bicoherence and biphase at a fixed (f1, f2) versus time.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

rebin_by_n_intervals(n, method='sum')[source]

Rebin in time by combining n consecutive intervals.

The additive bispectrum sums are combined (not the finished bicoherence, which is a ratio and cannot be averaged), then the bicoherence and biphase are recomputed. method is accepted for API parity with DynamicalCrossspectrum but the sums are always summed.

rebin_frequency(df_new, method='sum')[source]

Rebin the (diagonal) frequency axis to a coarser df_new.

Only implemented for store="diagonal". As for rebin_time(), the additive sums are combined and the bicoherence recomputed.

rebin_time(dt_new, method='sum')[source]

Rebin to a coarser time resolution dt_new (an integer multiple of dt).

Combines the additive sums over consecutive bins and recomputes the bicoherence, so the result is the correct averaged bicoherence over the wider bin – not an average of bicoherence values.

recompute_bicoherence(norm=None, bias_subtract=False, inplace=False)

Recompute the bicoherence under a different normalization or debiasing.

Uses the accumulated bispectrum sums stored on the object, so no FFTs are recomputed. See Bispectrum for the definitions.

Parameters:
norm{“kim_powers”, “sigl_chamoun”, “hagihira”}, optional

The bicoherence normalization. Defaults to the object’s current bicoherence_norm (useful when only toggling bias_subtract).

Returns:
bicoherencenumpy.ndarray

The recomputed bicoherence.

Other Parameters:
bias_subtractbool, default False

Subtract the ~1/M statistical bias of the squared bicoherence (M = number of averaged segments; Fackrell 1996, Elgar & Guza 1988). See stingray.fourier.bicoherence_from_sums(). Only defined for "kim_powers" and "sigl_chamoun".

inplacebool, default False

If True, also overwrite self.bicoherence, self.bicoherence_norm and self.bias_subtract.

shift_and_add(f0_list, nbins=None)[source]

Shift-and-add the diagonal bicoherence, aligning each bin to its f0.

For each time bin i the diagonal sums are shifted so that f0_list[i] lands on a common reference bin, then co-added; the bicoherence is recomputed from the co-added sums. This tracks a drifting coupling (e.g. a QPO fundamental) the way the kHz-QPO shift-and-add tracks a drifting Lorentzian.

Parameters:
f0_listiterable of float

The reference frequency in each time bin (length n_time).

Returns:
rel_freqnumpy.ndarray

Frequency relative to f0 (0 at the reference).

bicoherencenumpy.ndarray

The shifted-and-added bicoherence.

biphasenumpy.ndarray

The corresponding biphase.

Other Parameters:
nbinsint, optional

Length of the output (relative-frequency) axis. Defaults to the number of frequency bins.

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

trace(f1, f2)[source]

Return (time, bicoherence(t), biphase(t)) at a fixed (f1, f2).

For store="diagonal" only diagonal points (f1 = f2) are available.

trace_maximum(min_freq=None, max_freq=None)[source]

Trace the peak-bicoherence diagonal frequency index in each time bin.

Returns the array of frequency indices (into freq) of the maximum diagonal bicoherence between min_freq and max_freq per bin – the bicoherence analogue of trace_maximum().

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


Covariancespectrum

class stingray.Covariancespectrum(data, dt=None, band_interest=None, ref_band_interest=None, std=None)[source]

Compute a covariance spectrum for the data. The input data can be either in event data or pre-made light curves. Event data can either be in the form of a numpy.ndarray with (time stamp, energy) pairs or a stingray.events.EventList object. If light curves are formed ahead of time, then a list of stingray.Lightcurve objects should be passed to the object, ideally one light curve for each band of interest.

For the case where the data is input as a list of stingray.Lightcurve objects, the reference band(s) should either be

  1. a single stingray.Lightcurve object,

  2. a list of stingray.Lightcurve objects with the reference band for each band of interest pre-made, or

  3. None, in which case reference bands will formed by combining all light curves except for the band of interest.

In the case of event data, band_interest and ref_band_interest can be (multiple) pairs of energies, and the light curves for the bands of interest and reference bands will be produced dynamically.

Parameters:
data{numpy.ndarray | stingray.events.EventList object | list of stingray.Lightcurve objects}

data contains the time series data, either in the form of a 2-D array of (time stamp, energy) pairs for event data, or as a list of light curves. Note : The event list must be in sorted order with respect to the times of arrivals.

dtfloat

The time resolution of the stingray.Lightcurve formed from the energy bin. Only used if data is an event list.

band_interest{None, iterable of tuples}

If None, all possible energy values will be assumed to be of interest, and a covariance spectrum in the highest resolution will be produced. Note: if the input is a list of stingray.Lightcurve objects, then the user may supply their energy values here, for construction of a reference band.

ref_band_interest{None, tuple, stingray.Lightcurve, list of stingray.Lightcurve objects}

Defines the reference band to be used for comparison with the bands of interest. If None, all bands except the band of interest will be used for each band of interest, respectively. Alternatively, a tuple can be given for event list data, which will extract the reference band (always excluding the band of interest), or one may put in a single stingray.Lightcurve object to be used (the same for each band of interest) or a list of stingray.Lightcurve objects, one for each band of interest.

stdfloat or np.array or list of numbers

The term std is used to calculate the excess variance of a band. If std is set to None, default Poisson case is taken and the std is calculated as mean(lc)**0.5. In the case of a single float as input, the same is used as the standard deviation which is also used as the std. And if the std is an iterable of numbers, their mean is used for the same purpose.

Attributes:
unnorm_covarnp.ndarray

An array of arrays with mid point band_interest and their covariance. It is the array-form of the dictionary energy_covar. The covariance values are unnormalized.

covarnp.ndarray

Normalized covariance spectrum.

covar_errornp.ndarray

Errors of the normalized covariance spectrum.

References

[1] Wilkinson, T. and Uttley, P. (2009), Accretion disc variability in the hard state of black hole X-ray binaries. Monthly Notices of the Royal Astronomical Society, 397: 666–676. doi: 10.1111/j.1365-2966.2009.15008.x

Examples

See the notebooks repository for detailed notebooks on the code.


AveragedCovariancespectrum

class stingray.AveragedCovariancespectrum(data, segment_size, dt=None, band_interest=None, ref_band_interest=None, std=None)[source]

Compute a covariance spectrum for the data, defined in [covar spectrum]_ Equation 15.

Parameters:
data{numpy.ndarray | list of stingray.Lightcurve objects}

data contains the time series data, either in the form of a 2-D array of (time stamp, energy) pairs for event data, or as a list of stingray.Lightcurve objects. Note : The event list must be in sorted order with respect to the times of arrivals.

segment_sizefloat

The length of each segment in the averaged covariance spectrum. The number of segments will be calculated automatically using the total length of the data set and the segment_size defined here.

dtfloat

The time resolution of the stingray.Lightcurve formed from the energy bin. Only used if data is an event list.

band_interest{None, iterable of tuples}

If None, all possible energy values will be assumed to be of interest, and a covariance spectrum in the highest resolution will be produced. Note: if the input is a list of stingray.Lightcurve objects, then the user may supply their energy values here, for construction of a reference band.

ref_band_interest{None, tuple, stingray.Lightcurve, list of stingray.Lightcurve objects}

Defines the reference band to be used for comparison with the bands of interest. If None, all bands except the band of interest will be used for each band of interest, respectively. Alternatively, a tuple can be given for event list data, which will extract the reference band (always excluding the band of interest), or one may put in a single stingray.Lightcurve object to be used (the same for each band of interest) or a list of stingray.Lightcurve objects, one for each band of interest.

stdfloat or np.array or list of numbers

The term std is used to calculate the excess variance of a band. If std is set to None, default Poisson case is taken and the std is calculated as mean(lc)**0.5. In the case of a single float as input, the same is used as the standard deviation which is also used as the std. And if the std is an iterable of numbers, their mean is used for the same purpose.

Attributes:
unnorm_covarnp.ndarray

An array of arrays with mid point band_interest and their covariance. It is the array-form of the dictionary energy_covar. The covariance values are unnormalized.

covarnp.ndarray

Normalized covariance spectrum.

covar_errornp.ndarray

Errors of the normalized covariance spectrum.

References


VarEnergySpectrum

Abstract base class for spectral timing products including both variability and spectral information.

class stingray.varenergyspectrum.VarEnergySpectrum(events=None, freq_interval=None, energy_spec=None, ref_band=None, bin_time=1, use_pi=False, segment_size=None, events2=None, return_complex=False, min_phot_per_segment=10)[source]

Base class for variability-energy spectrum.

This class is only a base for the various variability spectra, and it’s not to be instantiated by itself.

Parameters:
eventsstingray.events.EventList object

event list

freq_interval[f0, f1], floats

the frequency range over which calculating the variability quantity

energy_speclist or tuple (emin, emax, N, type)

if a list is specified, this is interpreted as a list of bin edges; if a tuple is provided, this will encode the minimum and maximum energies, the number of intervals, and lin or log.

Attributes:
events1array-like

list of events used to produce the spectrum

events2array-like

if the spectrum requires it, second list of events

freq_intervalarray-like

interval of frequencies used to calculate the spectrum

energy_intervals[[e00, e01], [e10, e11], ...]

energy intervals used for the spectrum

spectrumarray-like

the spectral values, corresponding to each energy interval

spectrum_errorarray-like

the error bars corresponding to spectrum

energyarray-like

Give the centers of the energy intervals.

Other Parameters:
ref_band[emin, emax], floats; default None

minimum and maximum energy of the reference band. If None, the full band is used.

use_pibool, default False

Use channel instead of energy

events2stingray.events.EventList object

event list for the second channel, if not the same. Useful if the reference band has to be taken from another detector.

return_complex: bool, default False

In spectra that produce complex values, return the whole spectrum. Otherwise, the absolute value will be returned.

check_phot_per_segment(phot_per_segment, eint)[source]

Check if the number of photons per segment is enough.

Parameters:
phot_per_segment: float or int

Number of photons in the segment

eint: iterable

Energy interval

property energy

Give the centers of the energy intervals.

save_as_xspec(outroot, header_keywords=None)[source]

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().


RmsEnergySpectrum

stingray.varenergyspectrum.RmsEnergySpectrum

alias of RmsSpectrum


LagEnergySpectrum

stingray.varenergyspectrum.LagEnergySpectrum

alias of LagSpectrum


ExcessVarianceSpectrum

class stingray.varenergyspectrum.ExcessVarianceSpectrum(events, freq_interval, energy_spec, bin_time=1, use_pi=False, segment_size=None, normalization='fvar')[source]

Calculate the Excess Variance spectrum.

For each energy interval, calculate the excess variance in the specified frequency range.

Parameters:
eventsstingray.events.EventList object

event list

freq_interval[f0, f1], list of float

the frequency range over which calculating the variability quantity

energy_speclist or tuple (emin, emax, N, type)

if a list is specified, this is interpreted as a list of bin edges; if a tuple is provided, this will encode the minimum and maximum energies, the number of intervals, and lin or log.

Attributes:
events1array-like

list of events used to produce the spectrum

freq_intervalarray-like

interval of frequencies used to calculate the spectrum

energy_intervals[[e00, e01], [e10, e11], ...]

energy intervals used for the spectrum

spectrumarray-like

the spectral values, corresponding to each energy interval

spectrum_errorarray-like

the errorbars corresponding to spectrum

Other Parameters:
ref_band[emin, emax], floats; default None

minimum and maximum energy of the reference band. If None, the full band is used.

use_pibool, default False

Use channel instead of energy

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

check_phot_per_segment(phot_per_segment, eint)

Check if the number of photons per segment is enough.

Parameters:
phot_per_segment: float or int

Number of photons in the segment

eint: iterable

Energy interval

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

dict() → dict

Return a dictionary representation of the object.

property energy

Give the centers of the energy intervals.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

save_as_xspec(outroot, header_keywords=None)

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


CountSpectrum

class stingray.varenergyspectrum.CountSpectrum(events, energy_spec, use_pi=False)[source]

Calculate the energy spectrum.

For each energy interval, compute the counts.

Parameters:
eventsstingray.events.EventList object

event list

energy_speclist or tuple (emin, emax, N, type)

if a list is specified, this is interpreted as a list of bin edges; if a tuple is provided, this will encode the minimum and maximum energies, the number of intervals, and lin or log.

Attributes:
events1array-like

list of events used to produce the spectrum

energy_intervals[[e00, e01], [e10, e11], ...]

energy intervals used for the spectrum

spectrumarray-like

the spectral values, corresponding to each energy interval

spectrum_errorarray-like

the errorbars corresponding to spectrum

Other Parameters:
use_pibool, default False

Use channel instead of energy

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

check_phot_per_segment(phot_per_segment, eint)

Check if the number of photons per segment is enough.

Parameters:
phot_per_segment: float or int

Number of photons in the segment

eint: iterable

Energy interval

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

dict() → dict

Return a dictionary representation of the object.

property energy

Give the centers of the energy intervals.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

save_as_xspec(outroot, header_keywords=None)

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


RmsSpectrum

class stingray.varenergyspectrum.RmsSpectrum(events=None, energy_spec=None, ref_band=None, freq_interval=[0, 1], bin_time=1, use_pi=False, segment_size=None, events2=None, norm='frac')[source]

Calculate the rms-Energy spectrum.

For each energy interval, calculate the power density spectrum in absolute or fractional r.m.s. normalization, and integrate it in the given frequency range to obtain the rms. If events2 is specified, the cospectrum is used instead of the PDS.

We assume absolute r.m.s. normalization. To get the fractional r.m.s. we just divide by the mean count rate.

Parameters:
eventsstingray.events.EventList object

event list

freq_interval[f0, f1], list of float

the frequency range over which calculating the variability quantity

energy_speclist or tuple (emin, emax, N, type)

if a list is specified, this is interpreted as a list of bin edges; if a tuple is provided, this will encode the minimum and maximum energies, the number of intervals, and lin or log.

Attributes:
events1array-like

list of events used to produce the spectrum

events2array-like

if the spectrum requires it, second list of events

freq_intervalarray-like

interval of frequencies used to calculate the spectrum

energy_intervals[[e00, e01], [e10, e11], ...]

energy intervals used for the spectrum

spectrumarray-like

the spectral values, corresponding to each energy interval

spectrum_errorarray-like

the errorbars corresponding to spectrum

Other Parameters:
ref_band[emin, emax], float; default None

minimum and maximum energy of the reference band. If None, the full band is used.

use_pibool, default False

Use channel instead of energy

events2stingray.events.EventList object

event list for the second channel, if not the same. Useful if the reference band has to be taken from another detector.

normstr, one of [“abs”, “frac”]

The normalization of the rms, whether absolute or fractional.

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

check_phot_per_segment(phot_per_segment, eint)

Check if the number of photons per segment is enough.

Parameters:
phot_per_segment: float or int

Number of photons in the segment

eint: iterable

Energy interval

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

dict() → dict

Return a dictionary representation of the object.

property energy

Give the centers of the energy intervals.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

save_as_xspec(outroot, header_keywords=None)

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


LagSpectrum

class stingray.varenergyspectrum.LagSpectrum(events=None, freq_interval=None, energy_spec=None, ref_band=None, bin_time=1, use_pi=False, segment_size=None, events2=None)[source]

Calculate the lag-energy spectrum.

For each energy interval, calculate the lag between two bands. If events2 is specified, the energy bands are chosen from this second event list, while the reference band from events.

Parameters:
eventsstingray.events.EventList object

event list

freq_interval[f0, f1], list of float

the frequency range over which calculating the variability quantity

energy_speclist or tuple (emin, emax, N, type)

if a list is specified, this is interpreted as a list of bin edges; if a tuple is provided, this will encode the minimum and maximum energies, the number of intervals, and lin or log.

Attributes:
events1array-like

list of events used to produce the spectrum

events2array-like

if the spectrum requires it, second list of events

freq_intervalarray-like

interval of frequencies used to calculate the spectrum

energy_intervals[[e00, e01], [e10, e11], ...]

energy intervals used for the spectrum

spectrumarray-like

the lag values, corresponding to each energy interval

spectrum_errorarray-like

the errorbars corresponding to spectrum

Other Parameters:
ref_band[emin, emax], float; default None

minimum and maximum energy of the reference band. If None, the full band is used.

use_pibool, default False

Use channel instead of energy

events2stingray.events.EventList object

event list for the second channel, if not the same. Useful if the reference band has to be taken from another detector.

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

check_phot_per_segment(phot_per_segment, eint)

Check if the number of photons per segment is enough.

Parameters:
phot_per_segment: float or int

Number of photons in the segment

eint: iterable

Energy interval

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

dict() → dict

Return a dictionary representation of the object.

property energy

Give the centers of the energy intervals.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

save_as_xspec(outroot, header_keywords=None)

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


CovarianceSpectrum

class stingray.varenergyspectrum.CovarianceSpectrum(events=None, energy_spec=None, ref_band=None, freq_interval=[0, 1], bin_time=1, use_pi=False, segment_size=None, events2=None, norm='abs')[source]

Calculate the covariance spectrum.

This is just the absolute value of the complex covariance spectrum. Refer to that documentation for details.

For the original formulation of the covariance spectrum, see: Wilkinson & Uttley 2009, MNRAS, 397, 666

Parameters:
eventsstingray.events.EventList object

event list

freq_interval[f0, f1], list of float

the frequency range over which calculating the variability quantity

energy_speclist or tuple (emin, emax, N, type)

if a list is specified, this is interpreted as a list of bin edges; if a tuple is provided, this will encode the minimum and maximum energies, the number of intervals, and lin or log.

Attributes:
events1array-like

list of events used to produce the spectrum

events2array-like

if the spectrum requires it, second list of events

freq_intervalarray-like

interval of frequencies used to calculate the spectrum

energy_intervals[[e00, e01], [e10, e11], ...]

energy intervals used for the spectrum

spectrumarray-like

the spectral values, corresponding to each energy interval

spectrum_errorarray-like

the errorbars corresponding to spectrum

Other Parameters:
ref_band[emin, emax], float; default None

minimum and maximum energy of the reference band. If None, the full band is used.

use_pibool, default False

Use channel instead of energy

events2stingray.events.EventList object

event list for the second channel, if not the same. Useful if the reference band has to be taken from another detector.

normstr, one of [“abs”, “frac”]

The normalization of the covariance, whether absolute or fractional.

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

check_phot_per_segment(phot_per_segment, eint)

Check if the number of photons per segment is enough.

Parameters:
phot_per_segment: float or int

Number of photons in the segment

eint: iterable

Energy interval

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

dict() → dict

Return a dictionary representation of the object.

property energy

Give the centers of the energy intervals.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

save_as_xspec(outroot, header_keywords=None)

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


ComplexCovarianceSpectrum

class stingray.varenergyspectrum.ComplexCovarianceSpectrum(events=None, energy_spec=None, ref_band=None, freq_interval=[0, 1], bin_time=1, use_pi=False, segment_size=None, events2=None, norm='frac', return_complex=True, min_phot_per_segment=10)[source]

Calculate the complex covariance spectrum.

For each energy interval, calculate the covariance between two bands. If events2 is specified, the energy bands are chosen from this second event list, while the reference band from events.

Mastroserio et al. 2018, MNRAS, 475, 4027

We assume absolute r.m.s. normalization. To get the fractional r.m.s. we just divide by the mean count rate.

Parameters:
eventsstingray.events.EventList object

event list

freq_interval[f0, f1], list of float

the frequency range over which calculating the variability quantity

energy_speclist or tuple (emin, emax, N, type)

if a list is specified, this is interpreted as a list of bin edges; if a tuple is provided, this will encode the minimum and maximum energies, the number of intervals, and lin or log.

Attributes:
events1array-like

list of events used to produce the spectrum

events2array-like

if the spectrum requires it, second list of events

freq_intervalarray-like

interval of frequencies used to calculate the spectrum

energy_intervals[[e00, e01], [e10, e11], ...]

energy intervals used for the spectrum

spectrumarray-like

the spectral values, corresponding to each energy interval

spectrum_errorarray-like

the errorbars corresponding to spectrum

Other Parameters:
ref_band[emin, emax], float; default None

minimum and maximum energy of the reference band. If None, the full band is used.

use_pibool, default False

Use channel instead of energy

events2stingray.events.EventList object

event list for the second channel, if not the same. Useful if the reference band has to be taken from another detector.

normstr, one of [“abs”, “frac”]

The normalization of the covariance, whether absolute or fractional.

add(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Add two StingrayObject instances.

Add the array values of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

apply_mask(mask: npt.ArrayLike, inplace: bool = False, filtered_attrs: list = None)

Apply a mask to all array attributes of the object

Parameters:
maskarray of bool

The mask. Has to be of the same length as self.time

Returns:
ts_newStingrayObject object

The new object with the mask applied if inplace is False, otherwise the same object.

Other Parameters:
inplacebool

If True, overwrite the current object. Otherwise, return a new one.

filtered_attrslist of str or None

Array attributes to be filtered. Defaults to all array attributes if None. The other array attributes will be set to None. The main array attr is always included.

array_attrs() → list[str]

List the names of the array attributes of the Stingray Object.

By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of array attributes.

check_phot_per_segment(phot_per_segment, eint)

Check if the number of photons per segment is enough.

Parameters:
phot_per_segment: float or int

Number of photons in the segment

eint: iterable

Energy interval

data_attributes() → list[str]

Clean up the list of attributes, only giving out those pointing to data.

List all the attributes that point directly to valid data. This method goes through all the attributes of the class, eliminating methods, properties, and attributes that are complicated to serialize such as other StingrayObject, or arrays of objects.

This function does not make difference between array-like data and scalar data.

Returns:
data_attributeslist of str

List of attributes pointing to data that are not methods, properties, or other StingrayObject instances.

dict() → dict

Return a dictionary representation of the object.

property energy

Give the centers of the energy intervals.

classmethod from_astropy_table(ts: Table) → Tso

Create a Stingray Object object from data in an Astropy Table.

The table MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

classmethod from_pandas(ts: DataFrame) → Tso

Create an StingrayObject object from data in a pandas DataFrame.

The dataframe MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

Since pandas does not support n-D data, multi-dimensional arrays can be specified as <colname>_dimN_M_K etc.

See documentation of make_1d_arrays_into_nd() for details.

classmethod from_xarray(ts: Dataset) → Tso

Create a StingrayObject from data in an xarray Dataset.

The dataset MUST contain at least a column named like the main_array_attr. The rest of columns will form the array attributes of the new object, while the attributes in ds.attrs will form the new meta attributes of the object.

get_meta_dict() → dict

Give a dictionary with all non-None meta attrs of the object.

internal_array_attrs() → list[str]

List the names of the internal array attributes of the Stingray Object.

These are array attributes that can be set by properties, and are generally indicated by an underscore followed by the name of the property that links to it (E.g. _counts in Lightcurve). By array attributes, we mean the ones with the same size and shape as main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of internal array attributes.

meta_attrs() → list[str]

List the names of the meta attributes of the Stingray Object.

By array attributes, we mean the ones with a different size and shape than main_array_attr (e.g. time in EventList)

Returns:
attributeslist of str

List of meta attributes.

pretty_print(func_to_apply=None, attrs_to_apply=[], attrs_to_discard=[]) → str

Return a pretty-printed string representation of the object.

This is useful for debugging, and for interactive use.

Other Parameters:
func_to_applyfunction

A function that modifies the attributes listed in attrs_to_apply. It must return the modified attributes and a label to be printed. If None, no function is applied.

attrs_to_applylist of str

Attributes to be modified by func_to_apply.

attrs_to_discardlist of str

Attributes to be discarded from the output.

classmethod read(filename: str, fmt: str = None) → Tso

Generic reader for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

Files that need the astropy.table.Table interface MUST contain at least a column named like the main_array_attr. The default ascii format is enhanced CSV (ECSV). Data formats supporting the serialization of metadata (such as ECSV and HDF5) can contain all attributes such as mission, gti, etc with no significant loss of information. Other file formats might lose part of the metadata, so must be used with care.

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values should be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Path and file name for the file to be read.

fmt: str

Available options are ‘pickle’, ‘hea’, and any Table-supported format such as ‘hdf5’, ‘ascii.ecsv’, etc.

Returns:
obj: StingrayObject object

The object reconstructed from file

save_as_xspec(outroot, header_keywords=None)

Save the cross spectrum in a format that can be read by XSPEC.

For power spectra (self.type == "powerspectrum"), the method will produce three files using outroot as the base name:

  • outroot.txt: a plain-text file containing the frequency, power, and error columns used as input to XSPEC.

  • outroot.pha: the spectral file.

  • outroot.rsp: the corresponding response file.

For non-powerspectrum cross spectra, the method will produce six files, corresponding to the real and imaginary parts of the cross spectrum:

  • outroot_real.txt, outroot_real.pha, outroot_real.rsp for the real part.

  • outroot_imag.txt, outroot_imag.pha, outroot_imag.rsp for the imaginary part.

Parameters:
outrootstr

The root name of the output files.

header_keywordsdict, optional

A dictionary of header keys and values to be added to the output files.

Raises:
ValueError

If the object has no df attribute or power_err attribute, or if either of them is None.

RuntimeError

If the underlying XSPEC/HEASOFT tools needed to create the XSPEC files are not available. This is raised by stingray.io.save_as_xspec().

sub(other, operated_attrs=None, error_attrs=None, error_operation=<function sqsum>, inplace=False)

Subtract all the array attrs of two StingrayObject instances element by element, assuming the main array attributes of the instances match exactly.

All array attrs ending with _err are treated as error bars and propagated with the sum of squares.

GTIs are crossed, so that only common intervals are saved.

Parameters:
otherStingrayTimeseries object

A second time series object

Other Parameters:
operated_attrslist of str or None

Array attributes to be operated on. Defaults to all array attributes not ending in _err. The other array attributes will be discarded from the time series to avoid inconsistencies.

error_attrslist of str or None

Array attributes to be operated on with error_operation. Defaults to all array attributes ending with _err.

error_operationfunction

Function to be called to propagate the errors

inplacebool

If True, overwrite the current time series. Otherwise, return a new one.

to_astropy_table(no_longdouble=False) → Table

Create an Astropy Table from a StingrayObject

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the meta dictionary.

Other Parameters:
no_longdoublebool

If True, reduce the precision of longdouble arrays to double precision. This needs to be done in some cases, e.g. when the table is to be saved in an architecture not supporting extended precision (e.g. ARM), but can also be useful when an extended precision is not needed.

to_pandas() → DataFrame

Create a pandas DataFrame from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

Since pandas does not support n-D data, multi-dimensional arrays are converted into columns before the conversion, with names <colname>_dimN_M_K etc.

See documentation of make_nd_into_arrays() for details.

to_xarray() → Dataset

Create an xarray Dataset from a StingrayObject.

Array attributes (e.g. time, pi, energy, etc. for EventList) are converted into columns, while meta attributes (mjdref, gti, etc.) are saved into the ds.attrs dictionary.

write(filename: str, fmt: str = None) → None

Generic writer for :class`StingrayObject`

Currently supported formats are

  • pickle (not recommended for long-term storage)

  • any other formats compatible with the writers in astropy.table.Table (ascii.ecsv, hdf5, etc.)

..note:

Complex values can be dealt with out-of-the-box in some formats
like HDF5 or FITS, not in others (e.g. all ASCII formats).
With these formats, and in any case when fmt is ``None``, complex
values will be stored as two columns of real numbers, whose names
are of the format <variablename>.real and <variablename>.imag
Parameters:
filename: str

Name and path of the file to save the object list to.

fmt: str

The file format to store the data in. Available options are pickle, hdf5, ascii, fits


Bispectrum constructor functions

These build bispectra and cross bispectra directly from the various input containers, without going through the class constructors.

stingray.bispectrum.bispectrum_from_time_array(times, dt, segment_size=None, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate an auto-bispectrum from an array of event times.

Parameters:
timesnp.array

Event arrival times.

dtfloat

The time resolution of the intermediate light curves.

Returns:
specAveragedBispectrum or Bispectrum

The output bispectrum.

Other Parameters:
segment_sizefloat

The length, in seconds, of the light curve segments to average. Only relevant (and required) for an AveragedBispectrum.

gti[[gti0_0, gti0_1], ...]

Good time intervals.

bicoherence_norm{“kim_powers”, “sigl_chamoun”, “hagihira”}, default “kim_powers”

The bicoherence normalization (see Bispectrum).

poisson_subtractbool, default False

Subtract the Poisson-noise bias.

silentbool, default False

Silence the progress bars.

save_all, save_diagonalbool, default False

Store per-segment bispectra (full cube / diagonal only).

stingray.bispectrum.bispectrum_from_events(events, dt, segment_size=None, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate an auto-bispectrum from an event list. See bispectrum_from_time_array for the parameters.

stingray.bispectrum.bispectrum_from_lightcurve(lc, segment_size=None, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate an auto-bispectrum from a light curve. See bispectrum_from_time_array for the parameters.

stingray.bispectrum.bispectrum_from_stingray_timeseries(ts, flux_attr, error_flux_attr=None, segment_size=None, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate an auto-bispectrum from a time series. See bispectrum_from_time_array for the parameters.

stingray.bispectrum.bispectrum_from_lc_iterable(iter_lc, dt, segment_size=None, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate an average auto-bispectrum from an iterable of light curves.

See bispectrum_from_time_array for the parameters.

stingray.bispectrum.crossbispectrum_from_time_array(times1, times2, times3, dt, segment_size=None, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate a cross-bispectrum from three arrays of event times.

The three channels must be simultaneous. See crossbispectrum_from_lightcurve and CrossBispectrum for the parameters.

stingray.bispectrum.crossbispectrum_from_events(events1, events2, events3, dt, segment_size=None, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate a cross-bispectrum from three event lists. See crossbispectrum_from_lightcurve for the parameters.

stingray.bispectrum.crossbispectrum_from_lightcurve(lc1, lc2, lc3, segment_size=None, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate a cross-bispectrum from three simultaneous light curves.

Parameters:
lc1, lc2, lc3stingray.Lightcurve

The three channels, mapped to X(f1), Y(f2), Z(f1+f2). They must share the same time bins.

Returns:
specAveragedCrossBispectrum or CrossBispectrum

The output cross-bispectrum.

Other Parameters:
See :class:`CrossBispectrum` and `bispectrum_from_time_array`.
stingray.bispectrum.crossbispectrum_from_stingray_timeseries(ts1, ts2, ts3, flux_attr, error_flux_attr=None, segment_size=None, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate a cross-bispectrum from three time series. See crossbispectrum_from_lightcurve for the parameters.

stingray.bispectrum.crossbispectrum_from_lc_iterable(iter_lc1, iter_lc2, iter_lc3, dt, segment_size=None, gti=None, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False, save_all=False, save_diagonal=False)[source]

Calculate an average cross-bispectrum from three iterables of light curves.

See crossbispectrum_from_lightcurve for the parameters.


Variability-energy spectrum functions

stingray.varenergyspectrum.get_non_overlapping_ref_band(channel_band, ref_band)[source]

Ensures that the channel_band (i.e. the band of interest) is not contained within the ref_band (i.e. the reference band)

Parameters:
channel_banditerable of type [elow, ehigh]

The lower/upper limits of the energies to be contained in the band of interest

ref_banditerable

The lower/upper limits of the energies in the reference band

Returns:
ref_intervalsiterable

The channels that are both in the reference band in not in the bands of interest

Examples

>>> channel_band = [2, 3]
>>> ref_band = [[0, 10]]
>>> new_ref = get_non_overlapping_ref_band(channel_band, ref_band)
>>> assert np.allclose(new_ref, [[0, 2], [3, 10]])

Test this also works with a 1-D ref. band >>> new_ref = get_non_overlapping_ref_band(channel_band, [0, 10]) >>> assert np.allclose(new_ref, [[0, 2], [3, 10]]) >>> new_ref = get_non_overlapping_ref_band([0, 1], [[2, 3]]) >>> assert np.allclose(new_ref, [[2, 3]])


Utilities

Commonly used utility functionality, including Good Time Interval operations and input/output helper methods.

Base Fourier methods

stingray.fourier.BICOHERENCE_NORMS = ('kim_powers', 'sigl_chamoun', 'hagihira')

Recognised bicoherence normalizations (see bicoherence_from_sums).

stingray.fourier.avg_bispectrum_from_iterable(flux_iterable, dt, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, silent=False, return_subbs=False, save_diagonal=False)[source]

Bispectrum, bicoherence and biphase from an iterable of segments.

Implements the direct Fourier-decomposition estimator of the bispectrum (Maccarone 2013, MNRAS 435, 3547; see also Kim & Powers 1979). For K segments with Fourier transforms X_i(f), the bispectrum is

\[B(f_1, f_2) = \frac{1}{K} \sum_{i=0}^{K-1} X_i(f_1)\, X_i(f_2)\, X_i^{*}(f_1 + f_2)\]

the squared bicoherence (Kim & Powers 1979 normalization) is

\[b^2(f_1, f_2) = \frac{\left|\sum_i X_i(f_1) X_i(f_2) X_i^{*}(f_1+f_2)\right|^2} {\sum_i |X_i(f_1) X_i(f_2)|^2 \; \sum_i |X_i(f_1+f_2)|^2}\]

and the biphase is the phase of the (complex) bispectrum, defined over the full \(2\pi\) interval.

Parameters:
flux_iterableiterable of np.array

Iterable of equal-length flux (counts/bin) arrays, one per segment, as produced by get_flux_iterable_from_segments. Tuples of (flux, error) are accepted; the error is ignored.

dtfloat

Time resolution of the light curves.

Returns:
resultsastropy.table.Table or None

A table with a freq column (the positive frequency axis) and, in its metadata, the 2D arrays bispec, bicoherence, biphase, bispec_err and biphase_err (all nf x nf, with the redundant region set to NaN), plus diagnostics (m, n, dt, df, nphots, segment_size). None if no usable segment was found.

Other Parameters:
poisson_subtractbool, default False

If True, subtract the Poisson-noise bias from each per-segment bispectrum before averaging, using the correction of Wirnitzer (1985) (see Nathan et al. 2022; Maccarone 2013),

\[B_i(f_1, f_2) \rightarrow X_i(f_1) X_i(f_2) X_i^{*}(f_1+f_2) - |X_i(f_1)|^2 - |X_i(f_2)|^2 - |X_i(f_1+f_2)|^2 + 2 N_i,\]

where \(N_i\) is the number of photons in the segment. Only appropriate for photon-counting (Poisson) light curves in counts.

silentbool, default False

Silence the progress bar.

return_subbsbool, default False

If True, also return the full per-segment 2D bispectra in the metadata (under subbs). Uses a lot of memory (m x nf x nf).

save_diagonalbool, default False

If True, return only the diagonal (f1 = f2) of each per-segment bispectrum (under subbs_diagonal, shape m x nf). This is all a jellyfish plot needs, at a fraction of the memory of return_subbs.

Notes

The bicoherence only becomes meaningful once several segments are averaged. For a single segment (m = 1) it is identically 1 and the biphase error is 0, by construction.

stingray.fourier.avg_bispectrum_from_timeseries(times, gti, segment_size, dt, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, silent=False, fluxes=None, errors=None, return_subbs=False, save_diagonal=False)[source]

Average bispectrum from event times or a light curve.

Splits the input into segments of length segment_size and averages the Fourier-decomposition bispectrum over them. See avg_bispectrum_from_iterable for the estimator definition.

Parameters:
timesfloat np.array

Array of times (photon arrival times, or light-curve bin centers).

gti[[gti00, gti01], …]

Good time intervals.

segment_sizefloat

Length of segments. If None, the full light curve is used as a single segment (in which case the bicoherence is degenerate).

dtfloat

Time resolution of the light curves.

Returns:
resultsastropy.table.Table or None

See avg_bispectrum_from_iterable.

Other Parameters:
poisson_subtractbool, default False

Subtract the Poisson-noise bias (Wirnitzer 1985). See avg_bispectrum_from_iterable.

silentbool, default False

Silence the progress bar.

fluxesfloat np.array, default None

Array of counts per bin. If None, times is treated as event arrival times and binned internally.

errorsfloat np.array, default None

Array of errors on the fluxes (currently unused by the estimator).

return_subbsbool, default False

Return the full per-segment 2D bispectra in the metadata.

save_diagonalbool, default False

Return only the diagonal of each per-segment bispectrum. See avg_bispectrum_from_iterable.

stingray.fourier.avg_cross_bispectrum_from_iterables(flux_iterable1, flux_iterable2, flux_iterable3, dt, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False, return_subbs=False, save_diagonal=False)[source]

Cross-bispectrum, cross-bicoherence and cross-biphase from three iterables of simultaneous segments.

The cross-bispectrum of three (real) channels with per-segment Fourier transforms \(X_i\), \(Y_i\), \(Z_i\) is

\[B(f_1, f_2) = \frac{1}{m} \sum_{i=0}^{m-1} X_i(f_1)\, Y_i(f_2)\, Z_i^{*}(f_1 + f_2).\]

It reduces to the auto-bispectrum (avg_bispectrum_from_iterable) when the three iterables yield the same fluxes. Unlike the auto case it is not symmetric under f1 <-> f2, so it is sampled on the full signed frequency grid (see _bispectrum_frequency_grid with fullspec=True).

Parameters:
flux_iterable1, flux_iterable2, flux_iterable3iterable of np.array

Iterables of equal-length flux (counts/bin) arrays, one per segment, for the three channels. They must be simultaneous: segment i must cover the same time interval in all three. Tuples of (flux, error) are accepted; the error is ignored.

dtfloat

Time resolution of the light curves.

Returns:
resultsastropy.table.Table or None

As in avg_bispectrum_from_iterable, with nphots the geometric mean of the three channels’ photon counts. None if no usable segment triple was found.

Other Parameters:
bicoherence_norm{“kim_powers”, “sigl_chamoun”, “hagihira”}, default “kim_powers”

Bicoherence normalization. See bicoherence_from_sums.

poisson_subtractbool, default False

Subtract the Poisson-noise bias. Only has an effect when channels_overlap is True (the three channels share the same photons); for independent channels the Poisson noise is uncorrelated between factors and the cross-bispectrum is unbiased, so nothing is subtracted. See avg_bispectrum_from_iterable for the correction.

channels_overlapbool, default False

Whether the three channels are the same photon stream. Controls the Poisson-noise correction (above). Independent bands: leave False.

silentbool, default False

Silence the progress bar.

return_subbs, save_diagonalbool, default False

As in avg_bispectrum_from_iterable.

stingray.fourier.avg_cross_bispectrum_from_timeseries(times1, times2, times3, gti, segment_size, dt, bicoherence_norm='kim_powers', poisson_subtract=False, bias_subtract=False, channels_overlap=False, silent=False, fluxes1=None, fluxes2=None, fluxes3=None, return_subbs=False, save_diagonal=False)[source]

Average cross-bispectrum from three simultaneous event/light-curve series.

Splits each channel into segments of length segment_size on a common GTI and averages the cross-bispectrum over them. See avg_cross_bispectrum_from_iterables for the estimator definition.

Parameters are the three-channel analogues of avg_bispectrum_from_timeseries: times1/2/3 and fluxes1/2/3 are the per-channel arrays; all other parameters are as in avg_cross_bispectrum_from_iterables.

stingray.fourier.avg_cs_from_iterables(flux_iterable1, flux_iterable2, dt, norm='frac', use_common_mean=True, silent=False, fullspec=False, power_type='all', return_auxil=False, return_subcs=False)[source]

Calculate the average cross spectrum from an iterable of light curves

Parameters:
flux_iterable1iterable of np.array``s or of tuples (``np.array, np.array)

Iterable providing either equal-length series of count measurements, or of tuples (fluxes, errors). They must all be of the same length.

flux_iterable2iterable of np.array``s or of tuples (``np.array, np.array)

Same as flux_iterable1, for the reference channel

dtfloat

Time resolution of the light curves used to produce periodograms

Returns:
resultsastropy.table.Table

Table containing the following columns:

freqnp.array

The frequencies.

powernp.array

The normalized cross spectral powers.

unnorm_powernp.array

The unnormalized cross spectral power.

unnorm_pds1np.array

The unnormalized auxiliary PDS from channel 1. Only returned if return_auxil is True.

unnorm_pds2np.array

The unnormalized auxiliary PDS from channel 2. Only returned if return_auxil is True.

And a number of other useful diagnostics in the metadata, including all attributes needed to allocate Crossspectrum objects, such as all the input arguments of this function (dt, segment_size), and, e.g.

nint

The number of bins in the light curves used in each segment.

mint

The number of averaged periodograms.

meanfloat

The mean flux (geometrical average of the mean fluxes in the two channels).

Other Parameters:
normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

return_auxilbool, default False

Return the auxiliary unnormalized PDSs from the two separate channels

return_subcsbool, default False

Return all sub spectra from each light curve chunk, and the number of photons in each chunk (nphots_subcs in the metadata)

stingray.fourier.avg_cs_from_iterables_quick(flux_iterable1, flux_iterable2, dt, norm='frac')[source]

Like avg_cs_from_iterables, with default options that make it quick.

Assumes that:

  • the flux iterables return counts/bin, no other units

  • the mean is calculated over the whole light curve, and normalization is done at the end

  • no auxiliary PDSs are returned

  • only positive frequencies are returned

  • the spectrum is complex, no real parts or absolutes

  • no progress bars

Parameters:
flux_iterable1iterable of np.array``s or of tuples (``np.array, np.array)

Iterable providing either equal-length series of count measurements, or of tuples (fluxes, errors). They must all be of the same length.

flux_iterable2iterable of np.array``s or of tuples (``np.array, np.array)

Same as flux_iterable1, for the reference channel

dtfloat

Time resolution of the light curves used to produce periodograms

Returns:
resultsastropy.table.Table

Table containing the following columns:

freqnp.array

The periodogram frequencies

powernp.array

The normalized periodogram powers

unnorm_powernp.array

The unnormalized periodogram powers

And a number of other useful diagnostics in the metadata, including all attributes needed to allocate Crossspectrum objects, such as all the input arguments of this function (dt, segment_size), and, e.g.

nint

the number of bins in the light curves used in each segment

mint

the number of averaged periodograms

meanfloat

the mean flux (geometrical average of the mean fluxes in the two channels)

Other Parameters:
normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

stingray.fourier.avg_cs_from_timeseries(times1, times2, gti, segment_size, dt, norm='frac', use_common_mean=True, fullspec=False, silent=False, power_type='all', fluxes1=None, fluxes2=None, errors1=None, errors2=None, return_auxil=False, return_subcs=False)[source]

Calculate the average cross spectrum from a list of event times or a light curve.

If the input is a light curve, the time arrays need to be uniformly sampled inside GTIs (they can have gaps outside), and the fluxes need to be passed through the fluxes1 and fluxes2 arrays. Otherwise, times are interpreted as photon arrival times

Parameters:
times1float np.array

Array of times in the sub-band

times2float np.array

Array of times in the reference band

gti[[gti00, gti01], [gti10, gti11], …]

common good time intervals

segment_sizefloat

length of segments. If None, the full light curve is used.

dtfloat

Time resolution of the light curves used to produce periodograms

Returns:
freqnp.array

The periodogram frequencies

pdsnp.array

The normalized periodogram powers

n_binint

the number of bins in the light curves used in each segment

n_aveint or float

the number of averaged periodograms

Other Parameters:
normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

fullspecbool, default False

Return the full periodogram, including negative frequencies

silentbool, default False

Silence the progress bars

power_typestr, default ‘all’

If ‘all’, give complex powers. If ‘abs’, the absolute value; if ‘real’, the real part

fluxes1float np.array, default None

Array of fluxes or counts per bin for channel 1

fluxes2float np.array, default None

Array of fluxes or counts per bin for channel 2

errors1float np.array, default None

Array of errors on the fluxes on channel 1

errors2float np.array, default None

Array of errors on the fluxes on channel 2

return_subcsbool, default False

Return all sub spectra from each light curve chunk

stingray.fourier.avg_pds_from_iterable(flux_iterable, dt, norm='frac', use_common_mean=True, silent=False, return_subcs=False)[source]

Calculate the average periodogram from an iterable of light curves

Parameters:
flux_iterableiterable of np.array``s or of tuples (``np.array, np.array)

Iterable providing either equal-length series of count measurements, or of tuples (fluxes, errors). They must all be of the same length.

dtfloat

Time resolution of the light curves used to produce periodograms

Returns:
resultsastropy.table.Table

Table containing the following columns:

freqnp.array

The periodogram frequencies

powernp.array

The normalized periodogram powers

unnorm_powernp.array

The unnormalized periodogram powers

And a number of other useful diagnostics in the metadata, including all attributes needed to allocate Powerspectrum objects, such as all the input arguments of this function (dt, segment_size), and, e.g.

nint

the number of bins in the light curves used in each segment

mint

the number of averaged periodograms

meanfloat

the mean flux

Other Parameters:
normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars

return_subcsbool, default False

Return all sub spectra from each light curve chunk, and the number of photons in each chunk (nphots_subcs in the metadata)

stingray.fourier.avg_pds_from_timeseries(times, gti, segment_size, dt, norm='frac', use_common_mean=True, silent=False, fluxes=None, errors=None, return_subcs=False)[source]

Calculate the average periodogram from a list of event times or a light curve.

If the input is a light curve, the time array needs to be uniformly sampled inside GTIs (it can have gaps outside), and the fluxes need to be passed through the fluxes array. Otherwise, times are interpreted as photon arrival times.

Parameters:
timesfloat np.array

Array of times.

gti[[gti00, gti01], [gti10, gti11], …]

Good time intervals.

segment_sizefloat

Length of segments. If None, the full light curve is used.

dtfloat

Time resolution of the light curves used to produce periodograms.

Returns:
freqnp.array

The periodogram frequencies

powernp.array

The normalized periodogram powers

n_binint

the number of bins in the light curves used in each segment

n_aveint or float

the number of averaged periodograms

meanfloat

the mean flux

Other Parameters:
normstr, default “frac”

The normalization of the periodogram. “abs” is absolute rms, “frac” is fractional rms, “leahy” is Leahy+83 normalization, and “none” is the unnormalized periodogram

use_common_meanbool, default True

The mean of the light curve can be estimated in each interval, or on the full light curve. This gives different results (Alston+2013). Here we assume the mean is calculated on the full light curve, but the user can set use_common_mean to False to calculate it on a per-segment basis.

silentbool, default False

Silence the progress bars

fluxesfloat np.array, default None

Array of counts per bin or fluxes

errorsfloat np.array, default None

Array of errors on the fluxes above

return_subcsbool, default False

Return all sub spectra from each light curve chunk

stingray.fourier.bias_term(power1, power2, power1_noise, power2_noise, n_ave, intrinsic_coherence=1.0)[source]

Bias term needed to calculate the coherence.

Introduced by Vaughan & Nowak 1997, ApJ 474, L43

but implemented here according to the formulation in Ingram 2019, MNRAS 489, 392

As recommended in the latter paper, returns 0 if n_ave > 500

Parameters:
power1float np.array

sub-band periodogram

power2float np.array

reference-band periodogram

power1_noisefloat

Poisson noise level of the sub-band periodogram

power2_noisefloat

Poisson noise level of the reference-band periodogram

n_aveint or float

number of intervals that have been averaged to obtain the input spectra

Returns:
biasfloat np.array, same shape as power1 and power2

The bias term

Other Parameters:
intrinsic_coherencefloat, default 1

If known, the intrinsic coherence.

stingray.fourier.bicoherence_from_sums(norm, abs_bispec_sum, denom1, denom2, sum_abs_triple, valid=None, n_seg=None, bias_subtract=False)[source]

Compute a bicoherence from the accumulated bispectrum sums.

Given, for each frequency pair \((f_1, f_2)\), the summed triple product magnitude \(|\sum_i T_i|\) (with \(T_i = X_i(f_1) X_i(f_2) X_i^{*}(f_1+f_2)\)), the summed power terms \(\sum_i |X_i(f_1) X_i(f_2)|^2\) and \(\sum_i |X_i(f_1+f_2)|^2\), and the summed per-segment magnitude \(\sum_i |T_i|\), return the bicoherence under one of several normalizations. All variants lie in [0, 1] by construction (Cauchy-Schwarz for "kim_powers" / "sigl_chamoun", the triangle inequality for "hagihira").

Parameters:
normstr

One of:

"kim_powers"

Squared bicoherence of Kim & Powers (1979); the plasma-physics standard (Nagashima 2006) and the form used by Maccarone (2013):

\[b^2 = \frac{\left|\sum_i T_i\right|^2} {\sum_i |X_i(f_1) X_i(f_2)|^2 \; \sum_i |X_i(f_1+f_2)|^2}\]

Note this returns the squared bicoherence.

"sigl_chamoun"

Sigl & Chamoun (1994); the (unsquared) square root of the Kim & Powers value:

\[b = \frac{\left|\sum_i T_i\right|} {\sqrt{\sum_i |X_i(f_1) X_i(f_2)|^2 \; \sum_i |X_i(f_1+f_2)|^2}}\]
"hagihira"

Hagihira (2001) / Hayashi (2007); normalizes by the summed magnitude of the per-segment triple products (the value the numerator would reach with perfect phase coupling):

\[b = \frac{\left|\sum_i T_i\right|}{\sum_i |T_i|}\]
abs_bispec_sumnp.array

\(|\sum_i T_i|\).

denom1np.array

\(\sum_i |X_i(f_1) X_i(f_2)|^2\).

denom2np.array

\(\sum_i |X_i(f_1+f_2)|^2\).

sum_abs_triplenp.array

\(\sum_i |T_i|\).

validnp.array, optional

Boolean mask; entries where it is False are set to NaN.

n_segint, optional

Number of averaged segments, M. Required when bias_subtract is True.

bias_subtractbool, default False

Subtract the statistical bias of the squared bicoherence. For uncoupled signals the expected squared bicoherence is not zero but \(\approx 1/M\) with M = n_seg averaged segments (Fackrell 1996; Elgar & Guza 1988; Kim & Powers 1979). With this option the bias is removed in the squared domain, \(b^2 \rightarrow b^2 - 1/M\), and the result mapped back to the requested normalization. Only defined for "kim_powers" (the squared form) and "sigl_chamoun" (its signed root); a ValueError is raised for "hagihira".

Returns:
bicoherencenp.array

The bicoherence.

stingray.fourier.check_powers_for_intrinsic_coherence(*args)

Check if the powers are above the threshold for the intrinsic coherence to be well defined.

Parameters:
power1float np.array

sub-band periodogram

power2float np.array

reference-band periodogram

power1_noisefloat

Poisson noise level of the sub-band periodogram

power2_noisefloat

Poisson noise level of the reference-band periodogram

n_aveint or float

number of intervals that have been averaged to obtain the input spectra

thresholdfloat

The threshold in sigma above the noise level for the powers to be considered “high”.

Returns:
low_power1bool np.array

Whether the powers of the first band are below the threshold.

low_power2bool np.array

Whether the powers of the second band are below the threshold.

stingray.fourier.cross_to_covariance(cross_power, ref_power, ref_power_noise, delta_nu)[source]

Convert a cross spectrum into a covariance spectrum.

Covariance: Wilkinson & Uttley 2009, MNRAS, 397, 666

Complex covariance: Mastroserio et al. 2018, MNRAS, 475, 4027

Parameters:
cross_powercomplex np.array

cross spectrum

ref_powerfloat np.array

reference-band periodogram

ref_power_noisefloat

Poisson noise level of the reference-band periodogram

delta_nufloat or np.array

spectral resolution. Can be a float, or an array if the spectral resolution is not constant throughout the periodograms

Returns:
covariancecomplex np.array

The cross spectrum, normalized as a covariance.

stingray.fourier.error_on_averaged_cross_spectrum(cross_power, seg_power, ref_power, n_ave, seg_power_noise, ref_power_noise, common_ref=False)[source]

Error on cross spectral quantities.

Ingram 2019 details the derivation of the corrected formulas when the subject band is included in the reference band (and photons are presents in both bands, so this does _not_ involve overlapping bands from different detectors). The keyword argument common_ref indicates if the reference band also contains the photons of the subject band.

Note: this is only valid for a very large number of averaged powers. Beware if n_ave < 50 or so.

Parameters:
cross_powercomplex np.array

cross spectrum

seg_powerfloat np.array

sub-band periodogram

ref_powerfloat np.array

reference-band periodogram

seg_power_noisefloat

Poisson noise level of the sub-band periodogram

ref_power_noisefloat

Poisson noise level of the reference-band periodogram

n_aveint or float

number of intervals that have been averaged to obtain the input spectra

Returns:
dRefloat np.array

Error on the real part of the cross spectrum

dImfloat np.array

Error on the imaginary part of the cross spectrum

dphifloat np.array

Error on the angle (or phase lag)

dGfloat np.array

Error on the modulus of the cross spectrum

Other Parameters:
common_refbool, default False

Are data in the sub-band also included in the reference band?

stingray.fourier.estimate_intrinsic_coherence(cross_power, power1, power2, power1_noise, power2_noise, n_ave)[source]

Estimate intrinsic coherence

Use the iterative procedure from sec. 5 of

Ingram 2019, MNRAS 489, 392

Parameters:
cross_powercomplex np.array

cross spectrum

power1float np.array

sub-band periodogram

power2float np.array

reference-band periodogram

power1_noisefloat

Poisson noise level of the sub-band periodogram

power2_noisefloat

Poisson noise level of the reference-band periodogram

n_aveint or float

number of intervals that have been averaged to obtain the input spectra

Returns:
coherencefloat np.array

The estimated intrinsic coherence, at all frequencies.

stingray.fourier.extract_pds_slice_around_freq(freqs, powers, f0, nbins=100)[source]

Extract a slice of PDS around a given frequency.

This function extracts a slice of the power spectrum around a given frequency. The slice has a length of nbins. If the slice goes beyond the edges of the power spectrum, the missing values are filled with NaNs.

Parameters:
freqsnp.array

Array of frequencies, the same for all powers

powersnp.array

Array of powers

f0float

Central frequency

Other Parameters:
nbinsint, default 100

Number of bins to extract

Examples

>>> freqs = np.arange(1, 100) * 0.1
>>> powers = 10 / freqs
>>> f0 = 0.3
>>> p = extract_pds_slice_around_freq(freqs, powers, f0)
>>> assert np.isnan(p[0])
>>> assert not np.any(np.isnan(p[48:]))
stingray.fourier.get_average_ctrate(times, gti, segment_size, counts=None)[source]

Calculate the average count rate during the observation.

This function finds the same segments that the averaged periodogram functions (avg_cs_from_iterables, avg_pds_from_iterables etc) will use, and returns the mean count rate. If counts is None, the input times are interpreted as events. Otherwise, the number of events is taken from counts

Parameters:
timesfloat np.array

Array of times

gti[[gti00, gti01], [gti10, gti11], …]

good time intervals

segment_sizefloat

length of segments

Returns:
ctratefloat

The average count rate in the segments that are used for the analysis.

Other Parameters:
countsfloat np.array, default None

Array of counts per bin

Examples

>>> times = np.sort(np.random.uniform(0, 1000, 1000))
>>> gti = np.asanyarray([[0, 1000]])
>>> counts, _ = np.histogram(times, bins=np.linspace(0, 1000, 11))
>>> bin_times = np.arange(50, 1000, 100)
>>> assert get_average_ctrate(bin_times, gti, 1000, counts=counts) == 1.0
>>> assert get_average_ctrate(times, gti, 1000) == 1.0
stingray.fourier.get_flux_iterable_from_segments(times, gti, segment_size, n_bin=None, dt=None, fluxes=None, errors=None)[source]

Get fluxes from different segments of the observation.

If fluxes is None, the input times are interpreted as events, and they are split into many binned series of length segment_size with n_bin bins.

If fluxes is an array, the number of events corresponding to each time bin is taken from fluxes

Therefore, at least one of either n_bin and fluxes needs to be specified.

Parameters:
timesfloat np.array

Array of times

gti[[gti00, gti01], [gti10, gti11], …]

good time intervals

segment_sizefloat

length of segments. If None, the full light curve is used.

Yields:
fluxnp.array

Array of fluxes

errnp.array

(optional) if errors is None, an array of errors in the segment

Other Parameters:
n_binint, default None

Number of bins to divide the segment_size in

fluxesfloat np.array, default None

Array of fluxes.

errorsfloat np.array, default None

Array of error bars corresponding to the flux values above.

dtfloat, default None

Time resolution of the light curve used to produce periodograms

stingray.fourier.get_rms_from_rms_norm_periodogram(power_sqrms, poisson_noise_sqrms, df, M, low_M_buffer_size=4)[source]

Calculate integrated rms spectrum (frac or abs).

If M=1, it starts by rebinning the powers slightly in order to get a slightly better approximation for the error bars.

Parameters:
power_sqrmsarray-like

Powers, in units of fractional rms ($(rms/mean)^2 Hz{-1}$)

poisson_noise_sqrmsfloat

Poisson noise level, in units of fractional rms ($(rms/mean)^2 Hz{-1}$

dffloat or np.array, same dimension of power_sqrms

The frequency resolution of each power

Mint or np.array, same dimension of power_sqrms

The number of powers averaged to obtain each value of power.

Other Parameters:
low_M_buffer_sizeint, default 4

If M=1, the powers are rebinned to have a minimum of low_M_buffer_size powers in each bin. This is done to get a better estimate of the error bars.

stingray.fourier.get_rms_from_unnorm_periodogram(unnorm_powers, nphots_per_segment, df, M=1, poisson_noise_unnorm=None, segment_size=None, kind='frac')[source]

Calculate the fractional rms amplitude from unnormalized powers.

We assume the powers come from an unnormalized Bartlett periodogram. If so, the Poisson noise level is nphots_per_segment, but the user can specify otherwise (e.g. if the Poisson noise level is altered by dead time). The segment_size and nphots_per_segment parameters refer to the length and averaged counts of each segment of data used for the Bartlett periodogram.

Parameters:
unnorm_powersnp.ndarray

The unnormalized power spectrum

nphots_per_segmentfloat

The averaged number of photons per segment of the data used for the Bartlett periodogram

dffloat

The frequency resolution of the periodogram

Other Parameters:
poisson_noise_unnormfloat

The unnormalized Poisson noise level

segment_sizefloat

The size of the segment, in seconds

Mint

The number of segments averaged to obtain the periodogram

kindstr

One of “frac” or “abs”

stingray.fourier.impose_symmetry_lsft(ft_res: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str], sum_y: float, len_y: int, freqs: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]) → _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str][source]

Impose symmetry on the input fourier transform.

Parameters:
ft_resnp.array

The Fourier transform of the signal.

sum_yfloat

The sum of the values of the signal.

len_yint

The length of the signal.

freqsnp.array

An array of frequencies at which the transform is sampled.

Returns:
lsft_resnp.array

The Fourier transform of the signal with symmetry imposed.

freqs_newnp.array

The new frequencies

stingray.fourier.integrate_power_in_frequency_range(frequency, power, frequency_range, power_err=None, df=None, m=1, poisson_power=0)[source]

Integrate the power in a given frequency range.

Parameters:
frequencyiterable

The frequencies of the power spectrum

poweriterable

The power at each frequency

frequency_rangeiterable of length 2

The frequency range to integrate

power_erriterable, optional, default None

The power error bar at each frequency

dffloat or float iterable, optional, default None

The frequency resolution of the input data. If None, it is calculated from the median difference of input frequencies.

mint, optional, default 1

The number of segments and/or contiguous frequency bins averaged to obtain power. Only needed if power_err is None

poisson_powerfloat, optional, default 0

The Poisson noise level of the power spectrum.

Returns:
power_integratedfloat

The integrated power

power_integrated_errfloat

The error on the integrated power

stingray.fourier.intrinsic_coherence(cross_power, power1, power2, power1_noise, power2_noise, n_ave, return_uncertainty=False, adjust_bias=False, atol=0.01)[source]

Intrinsic coherence estimations from cross and power spectra.

Vaughan & Nowak 1997, ApJ 474, L43

\[\tilde{\gamma}^2(f) = \frac{|\langle \tilde{C}(f) \rangle|^2 - \tilde{b}^2} {(\langle \tilde{P}_1(f) \rangle - \tilde{P}_{1, \rm noise}) (\langle \tilde{P}_2(f) \rangle - \tilde{P}_{2, \rm noise})}\]

where \(\tilde{b}^2\) is the bias term that accounts for the contribution of Poisson noise to the cross spectrum (see stingray.fourier.bias_term()), and tilde generally indicates noisy measurements of the cross spectrum \(C\) and the power spectra \(P_n\). The terms \(\tilde{P}_{\rm n, \rm noise}\) are the estimates of the contribution of Poisson noise to the power spectra.

The bias term depends on the intrinsic coherence itself, so it can be calculated iteratively following the procedure from sec. 5 of Ingram 2019, MNRAS 489, 3927, which adjusts the bias term until convergence is reached. This is done if adjust_bias is set to True. It is typically a very small correction.

For errors, assumes high powers, high coherence. See eq. 8 of the paper. Powers below 3 sigma above the noise level (P_noise / sqrt(MW)) are considered too low, and the coherence will be set to NaN.

TODO: implement a more general treatment of the uncertainty.

Parameters:
cross_powercomplex np.array

Cross spectrum

power1float np.array

Sub-band periodogram. Must have the same shape as cross_power.

power2float np.array

Reference-band periodogram. Must have the same shape as cross_power.

power1_noisefloat or float np.array of the same shape as cross_power

Poisson noise level of the sub-band periodogram

power2_noisefloat or float np.array of the same shape as cross_power

Poisson noise level of the reference-band periodogram

n_aveint or float or int np.array of the same shape as cross_power

number of intervals that have been averaged to obtain the input spectra

Returns:
coherencefloat np.array or tuple of two float np.array

The intrinsic coherence values at all frequencies. It is 0 if the numerator of the coherence calculation is negative, and NaN if the powers are too low compared to the noise level.

uncertaintyfloat np.array, optional

The uncertainty on the intrinsic coherence, calculated according to Vaughan & Nowak 1997, ApJ 474, L43, eq. 8.

Other Parameters:
return_uncertaintybool, default False

Whether to return the uncertainty on the coherence, calculated according to Vaughan & Nowak 1997, ApJ 474, L43, eq. 8.

atolfloat, default 0.01

The absolute tolerance for the convergence of the iterative procedure to adjust the bias term.Only relevant if adjust_bias is True.

stingray.fourier.lsft_fast(y: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str], t: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str], freqs: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str], sign: int | None = 1, oversampling: int | None = 10) → ndarray[tuple[Any, ...], dtype[_ScalarT]][source]

Calculates the Lomb-Scargle Fourier transform of a light curve. Only considers non-negative frequencies. Subtracts mean from data as it is required for the working of the algorithm.

Adapted from original Matlab code by J. D. Scargle, using Astropy’s trig_sum for speed.

Parameters:
ya numpy.array of floats

Observations to be transformed.

tnumpy.array of floats

Times of the observations

freqsnumpy.array

An array of frequencies at which the transform is sampled.

signint, optional, default: 1

The sign of the fourier transform. 1 implies positive sign and -1 implies negative sign.

fullspecbool, optional, default: False

Return LSFT values for full frequency array (True) or just positive frequencies (False).

oversamplingfloat, optional, default: 10

Interpolation Oversampling Factor

Returns:
ft_resnumpy.ndarray

An array of Fourier transformed data.

stingray.fourier.lsft_slow(y: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str], t: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str], freqs: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str], sign: int | None = 1) → ndarray[tuple[Any, ...], dtype[_ScalarT]][source]

Calculates the Lomb-Scargle Fourier transform of a light curve. Only considers non-negative frequencies. Subtracts mean from data as it is required for the working of the algorithm.

Adapted from original Matlab code by J. D. Scargle.

Parameters:
ya numpy.array of floats

Observations to be transformed.

tnumpy.array of floats

Times of the observations

freqsnumpy.ndarray

An array of frequencies at which the transform is sampled.

signint, optional, default: 1

The sign of the fourier transform. 1 implies positive sign and -1 implies negative sign.

fullspecbool, optional, default: False

Return LSFT values for full frequency array (True) or just positive frequencies (False).

Returns:
ft_resnumpy.ndarray

An array of Fourier transformed data.

stingray.fourier.normalize_abs(unnorm_power, dt, n_bin)[source]

Absolute rms normalization.

\[P = P_{\rm frac} * \mu^2\]

where \(\mu\) is the mean count rate, or equivalently

\[P = \frac{2}{T}P_{\rm unnorm}\]

In this normalization, the periodogram is in units of \(rms^2 Hz^{-1}\), and the squared root of the integrated periodogram will give the absolute rms in the required frequency range.

e.g. Uttley & McHardy, MNRAS 323, L26

Parameters:
unnorm_powernp.array of float or complex

The unnormalized (cross-)spectral powers

dtfloat

The sampling time

n_binint

The number of bins in the light curve

Returns:
powernp.array of the same kind and shape as unnorm_power

The normalized powers.

Examples

>>> mean = var = 100000
>>> n_bin = 1000000
>>> dt = 0.2
>>> meanrate = mean / dt
>>> lc = np.random.poisson(mean, n_bin)
>>> pds = np.abs(fft(lc))**2
>>> pdsnorm = normalize_abs(pds, dt, lc.size)
>>> assert np.isclose(
...     pdsnorm[1:n_bin//2].mean(), poisson_level(norm="abs", meanrate=meanrate), rtol=0.01)
stingray.fourier.normalize_frac(unnorm_power, dt, n_bin, mean_flux, background_flux=0)[source]

Fractional rms normalization.

\[P = \frac{P_{\rm Leahy}}{\mu} = \frac{2T}{N_{\rm ph}^2}P_{\rm unnorm}\]

where \(\mu\) is the mean count rate, \(T\) is the length of the observation, and \(N_{\rm ph}\) the number of photons. Alternative formulas found in the literature substitute \(T=N\,dt\), \(\mu=N_{\rm ph}/T\), which give equivalent results.

If the background can be estimated, one can calculate the source rms normalized periodogram as

\[P = P_{\rm Leahy} \frac{\mu}{(\mu - \beta)^2}\]

or

\[P = \frac{2T}{(N_{\rm ph} - \beta T)^2}P_{\rm unnorm}\]

where \(\beta\) is the background count rate.

This is also called the Belloni or Miyamoto normalization. In this normalization, the periodogram is in units of \((rms/mean)^2 Hz^{-1}\), and the squared root of the integrated periodogram will give the fractional rms in the required frequency range.

Belloni & Hasinger (1990) A&A 230, 103

Miyamoto et al. (1991), ApJ 383, 784

Parameters:
unnorm_powernp.array of float or complex

The unnormalized (cross-)spectral powers

dtfloat

The sampling time

n_binint

The number of bins in the light curve

mean_fluxfloat

The mean of the light curve used to calculate the periodogram. If the light curve is in counts, it gives the mean counts per bin.

Returns:
powernp.array of the same kind and shape as unnorm_power

The normalized powers.

Other Parameters:
background_fluxfloat, default 0

The background flux, in the same units as mean_flux.

Examples

>>> mean = var = 1000000
>>> back = 100000
>>> n_bin = 1000000
>>> dt = 0.2
>>> meanrate = mean / dt
>>> backrate = back / dt
>>> lc = np.random.poisson(mean, n_bin)
>>> pds = np.abs(fft(lc))**2
>>> pdsnorm = normalize_frac(pds, dt, lc.size, mean)
>>> assert np.isclose(
...     pdsnorm[1:n_bin//2].mean(), poisson_level(meanrate=meanrate,norm="frac"), rtol=0.01)
>>> pdsnorm = normalize_frac(pds, dt, lc.size, mean, background_flux=back)
>>> assert np.isclose(pdsnorm[1:n_bin//2].mean(),
...                   poisson_level(meanrate=meanrate,norm="frac",backrate=backrate),
...                   rtol=0.01)
stingray.fourier.normalize_leahy_from_variance(unnorm_power, variance, n_bin)[source]

Leahy+83 normalization, from the variance of the lc.

\[P = \frac{P_{\rm unnorm}}{N <\delta{x}^2>}\]

In this normalization, the periodogram of a single light curve is distributed according to a chi squared distribution with two degrees of freedom.

In this version, the normalization is obtained by the variance of the light curve bins, instead of the more usual version with the number of photons. This allows to obtain this normalization also in the case of non-Poisson distributed data.

Parameters:
unnorm_powernp.array of float or complex

The unnormalized (cross-)spectral powers

variancefloat

The mean variance of the light curve bins

n_binint

The number of bins in the light curve

Returns:
powernp.array of the same kind and shape as unnorm_power

The normalized powers.

Examples

>>> mean = var = 100000.
>>> n_bin = 1000000
>>> lc = np.random.poisson(mean, n_bin).astype(float)
>>> pds = np.abs(fft(lc))**2
>>> pdsnorm = normalize_leahy_from_variance(pds, var, lc.size)
>>> assert np.isclose(pdsnorm[0], 2 * np.sum(lc), rtol=0.01)
>>> assert np.isclose(pdsnorm[1:n_bin//2].mean(), poisson_level(norm="leahy"), rtol=0.01)

If the variance is zero, it will fail: >>> pdsnorm = normalize_leahy_from_variance(pds, 0., lc.size) Traceback (most recent call last): … ValueError: The variance used to normalize the …

stingray.fourier.normalize_leahy_poisson(unnorm_power, n_ph)[source]

Leahy+83 normalization.

\[P = \frac{2}{N_{\rm ph}} P_{\rm unnorm}\]

In this normalization, the periodogram of a single light curve is distributed according to a chi squared distribution with two degrees of freedom.

Leahy et al. 1983, ApJ 266, 160

Parameters:
unnorm_powernp.array of float or complex

The unnormalized (cross-)spectral powers

variancefloat

The mean variance of the light curve bins

n_binint

The number of bins in the light curve

Returns:
powernp.array of the same kind and shape as unnorm_power

The normalized powers.

Examples

>>> mean = var = 100000.
>>> n_bin = 1000000
>>> lc = np.random.poisson(mean, n_bin).astype(float)
>>> pds = np.abs(fft(lc))**2
>>> pdsnorm = normalize_leahy_poisson(pds, np.sum(lc))
>>> assert np.isclose(pdsnorm[0], 2 * np.sum(lc), rtol=0.01)
>>> assert np.isclose(pdsnorm[1:n_bin//2].mean(), poisson_level(norm="leahy"), rtol=0.01)
stingray.fourier.normalize_periodograms(unnorm_power, dt, n_bin, mean_flux=None, n_ph=None, variance=None, background_flux=0.0, norm='frac', power_type='all')[source]

Wrapper around all the normalize_NORM methods.

Normalize the cross-spectrum or the power-spectrum to Leahy, absolute rms^2, fractional rms^2 normalization, or not at all.

Parameters:
unnorm_powernumpy.ndarray

The unnormalized cross spectrum.

dtfloat

The sampling time of the light curve

n_binint

The number of bins in the light curve

Returns:
powernumpy.nd.array

The normalized co-spectrum (real part of the cross spectrum). For ‘none’ normalization, imaginary part is returned as well.

Other Parameters:
mean_fluxfloat

The mean of the light curve used to calculate the powers (If a cross spectrum, the geometrical mean of the light curves in the two channels). Only relevant for “frac” normalization

n_phint or float

The number of counts in the light curve used to calculate the unnormalized periodogram. Only relevant for Leahy normalization.

variancefloat

The average variance of the measurements in light curve (if a cross spectrum, the geometrical mean of the variances in the two channels). NOT the variance of the light curve, but of each flux measurement (square of light curve error bar)! Only relevant for the Leahy normalization of non-Poissonian data.

normstr

One of leahy (Leahy+83), frac (fractional rms), abs (absolute rms),

power_typestr

One of real (real part), all (all complex powers), abs (absolute value)

background_fluxfloat, default 0

The background flux, in the same units as mean_flux.

stingray.fourier.poisson_level(norm='frac', meanrate=None, n_ph=None, backrate=0)[source]

Poisson (white)-noise level in a periodogram of pure counting noise.

For Leahy normalization, this is:

\[P = 2\]

For the fractional r.m.s. normalization, this is

\[P = \frac{2}{\mu}\]

where \(\mu\) is the average count rate

For the absolute r.m.s. normalization, this is

\[P = 2 \mu\]

Finally, for the unnormalized periodogram, this is

\[P = N_{\rm ph}\]
Parameters:
normstr, default “frac”

Normalization of the periodogram. One of [“abs”, “frac”, “leahy”, “none”].

Returns:
power_noisefloat

The Poisson noise level in the wanted normalization.

Other Parameters:
meanratefloat, default None

Mean count rate in counts/s. Needed for r.m.s. norms (“abs” and “frac”).

n_phfloat, default None

Total number of counts in the light curve. Needed if norm=="none".

backratefloat, default 0

Background count rate in counts/s. Optional for fractional r.m.s. norm.

Raises:
ValueError

If the inputs are incompatible with the required normalization.

Examples

>>> poisson_level(norm="leahy")
2.0
>>> poisson_level(norm="abs", meanrate=10.)
20.0
>>> poisson_level(norm="frac", meanrate=10.)
0.2
>>> poisson_level(norm="none", n_ph=10)
10.0
>>> poisson_level(norm="asdfwrqfasdh3r", meanrate=10.)
Traceback (most recent call last):
...
ValueError: Unknown value for norm: asdfwrqfasdh3r...
>>> poisson_level(norm="none", meanrate=10)
Traceback (most recent call last):
...
ValueError: Bad input parameters for norm none...
>>> poisson_level(norm="abs", n_ph=10)
Traceback (most recent call last):
...
ValueError: Bad input parameters for norm abs...
stingray.fourier.positive_fft_bins(n_bin, include_zero=False)[source]

Give the range of positive frequencies of a complex FFT.

This assumes we are using Numpy’s FFT, or something compatible with it, like pyfftw.interfaces.numpy_fft, where the positive frequencies come before the negative ones, the Nyquist frequency is included in the negative frequencies but only in even number of bins, and so on. This is mostly to avoid using the freq > 0 mask, which is memory-hungry and inefficient with large arrays. We use instead a slice object, giving the range of bins of the positive frequencies.

See https://numpy.org/doc/stable/reference/routines.fft.html#implementation-details

Parameters:
n_binint

The number of bins in the FFT, including all frequencies

Returns:
positive_binsslice

Slice object encoding the positive frequency bins. See examples.

Other Parameters:
include_zerobool, default False

Include the zero frequency in the output slice

Examples

Let us calculate the positive frequencies using the usual mask >>> freq = np.fft.fftfreq(10) >>> good = freq > 0

This works well, but it is highly inefficient in large arrays. This function will instead return a slice object, which will work as an equivalent mask for the positive bins. Below, a few tests that this works as expected. >>> goodbins = positive_fft_bins(10) >>> assert np.allclose(freq[good], freq[goodbins]) >>> freq = np.fft.fftfreq(11) >>> good = freq > 0 >>> goodbins = positive_fft_bins(11) >>> assert np.allclose(freq[good], freq[goodbins]) >>> freq = np.fft.fftfreq(10) >>> good = freq >= 0 >>> goodbins = positive_fft_bins(10, include_zero=True) >>> assert np.allclose(freq[good], freq[goodbins]) >>> freq = np.fft.fftfreq(11) >>> good = freq >= 0 >>> goodbins = positive_fft_bins(11, include_zero=True) >>> assert np.allclose(freq[good], freq[goodbins])

stingray.fourier.raw_coherence(cross_power, power1, power2, power1_noise, power2_noise, n_ave, intrinsic_coherence=1.0, return_uncertainty=False)[source]

Raw coherence estimations from cross and power spectra.

The function is defined as (see Ingram 2019 [9] :

\[\tilde{g}^2(f) = \frac{|\langle \tilde{C}(f) \rangle|^2 - \tilde{b}^2} {\langle \tilde{P}_1(f) \rangle \langle \tilde{P}_2(f) \rangle}\]

where \(\tilde{b}^2\) is the bias term that accounts for the contribution of Poisson noise to the cross spectrum (see stingray.fourier.bias_term()), and tilde generally indicates noisy measurements of the cross spectrum \(C\) and the power spectra \(P_n\).

Parameters:
cross_powercomplex np.array

cross spectrum

power1float np.array

sub-band periodogram

power2float np.array

reference-band periodogram

power1_noisefloat

Poisson noise level of the sub-band periodogram

power2_noisefloat

Poisson noise level of the reference-band periodogram

n_aveint or float

number of intervals that have been averaged to obtain the input spectra

Returns:
coherencefloat np.array

The raw coherence values at all frequencies.

Other Parameters:
intrinsic_coherencefloat, default 1

If known, the intrinsic coherence.

return_uncertaintybool, default False

Whether to return the uncertainty on the coherence, calculated according to VN97

References

stingray.fourier.rms_calculation(unnorm_powers, min_freq, max_freq, nphots, T, M_freqs, K_freqs, freq_bins, poisson_noise_unnorm, deadtime=0.0)[source]

Compute the fractional rms amplitude in the given power or cross spectrum

NOTE: all array quantities are already in the correct energy range

Parameters:
unnorm_powersarray of float

unnormalised power or cross spectrum, the array has already been filtered for the given frequency range

min_freqfloat

The lower frequency bound for the calculation (from the freq grid).

max_freqfloat

The upper frequency bound for the calculation (from the freq grid).

nphotsfloat

Number of photons for the full power or cross spectrum

Tfloat

Time length of the light curve

M_freqscalar or array of float

If scalar, it is the number of segments in the AveragedCrossspectrum If array, it is the number of segments times the rebinning sample in the given frequency range.

K_freqscalar or array of float

If scalar, the power or cross spectrum is not rebinned (K_freq = 1) If array, the power or cross spectrum is rebinned and it is the rebinned sample in the given frequency range.

freq_binsinteger

if the cross or power spectrum is rebinned freq_bins = 1, if it NOT rebinned freq_bins is the number of frequency bins in the given frequency range.

poisson_noise_unnormfloat

This is the Poisson noise level unnormalised.

Returns:
rmsfloat

The fractional rms amplitude contained between min_freq and max_freq.

rms_errfloat

The error on the fractional rms amplitude.

Other Parameters:
deadtimefloat

Deadtime of the instrument

stingray.fourier.shift_and_add(freqs, power_list, f0_list, nbins=100, rebin=None, df=None, M=None)[source]

Add a list of power spectra, centered on different frequencies.

This is the basic operation for the shift-and-add operation used to track kHz QPOs in X-ray binaries (e.g. Méndez et al. 1998, ApJ, 494, 65).

Parameters:
freqsnp.array

Array of frequencies, the same for all powers. Must be sorted and on a uniform grid.

power_listlist of np.array

List of power spectra. Each power spectrum must have the same length as the frequency array.

f0_listlist of float

List of central frequencies

Returns:
fnp.array

Array of output frequencies

pnp.array

Array of output powers

nnp.array

Number of contributing power spectra at each frequency

Other Parameters:
nbinsint, default 100

Number of bins to extract

rebinint, default None

Rebin the final spectrum by this factor. At the moment, the rebinning is linear.

dffloat, default None

Frequency resolution of the power spectra. If not given, it is calculated from the input frequencies.

Mint or list of int, default None

Number of segments used to calculate each power spectrum. If a list is given, it must have the same length as the power list.

Examples

>>> power_list = [[2, 5, 2, 2, 2], [1, 1, 5, 1, 1], [3, 3, 3, 5, 3]]
>>> freqs = np.arange(5) * 0.1
>>> f0_list = [0.1, 0.2, 0.3, 0.4]
>>> f, p, n = shift_and_add(freqs, power_list, f0_list, nbins=5)
>>> assert np.array_equal(n, [2, 3, 3, 3, 2])
>>> assert np.array_equal(p, [2. , 2. , 5. , 2. , 1.5])
>>> assert np.allclose(f, [0.05, 0.15, 0.25, 0.35, 0.45])
stingray.fourier.unnormalize_periodograms(norm_power, dt, n_bin, n_ph, variance=None, background_flux=0.0, norm=None, power_type='all')[source]

Wrapper around all the normalize_NORM methods.

Unnormalize the power of the cross-spectrum to Leahy, absolute rms^2, fractional rms^2 normalization, or not at all.

Parameters:
norm_powernumpy.ndarray

The normalized cross-spectrum or poisson noise

dtfloat

The sampling time of the light curve

n_binint

The number of bins in the light curve

Returns:
powernumpy.nd.array

The normalized co-spectrum (real part of the cross spectrum). For ‘none’ normalization, imaginary part is returned as well.

Other Parameters:
mean_fluxfloat

The mean of the light curve used to calculate the powers (If a cross spectrum, the geometrical mean of the light curves in the two channels). Only relevant for “frac” normalization

n_phint or float

The number of counts in the light curve used to calculate the unnormalized periodogram. Only relevant for Leahy normalization.

variancefloat

The average variance of the measurements in light curve (if a cross spectrum, the geometrical mean of the variances in the two channels). NOT the variance of the light curve, but of each flux measurement (square of light curve error bar)! Only relevant for the Leahy normalization of non-Poissonian data.

normstr

One of leahy (Leahy+83), frac (fractional rms), abs (absolute rms),

power_typestr

One of real (real part), all (all complex powers), abs (absolute value)

background_fluxfloat, default 0

The background flux, in the same units as mean_flux.

Statistical Functions

stingray.stats.a_from_pf(p)[source]

Fractional amplitude of modulation from pulsed fraction

If the pulsed profile is defined as p = mean * (1 + a * sin(phase)),

we define “pulsed fraction” as 2a/b, where b = mean + a is the maximum and a is the amplitude of the modulation.

Hence, a = pf / (2 - pf)

Examples

>>> a_from_pf(1)
1.0
>>> a_from_pf(0)
0.0
stingray.stats.a_from_ssig(ssig, ncounts)[source]

Amplitude of a sinusoid corresponding to a given Z/PDS value

From Leahy et al. 1983, given a pulse profile p = lambda * (1 + a * sin(phase)), The theoretical value of Z^2_n is Ncounts / 2 * a^2

Note that if there are multiple sinusoidal components, one can use a = sqrt(sum(a_l)) (Bachetti+2021b)

Examples

>>> assert np.isclose(a_from_ssig(150, 30000), 0.1)
stingray.stats.amplitude_upper_limit(pmeas, counts, n=1, c=0.95, fft_corr=False, nyq_ratio=0, summed_flag=True)[source]

Upper limit on a sinusoidal modulation, given a measured power in the PDS/Z search.

Eq. 10 in Vaughan+94 and a_from_ssig: they are equivalent but Vaughan+94 corrects further for the response inside an FFT bin and at frequencies close to Nyquist. These two corrections are added by using fft_corr=True and nyq_ratio to the correct \(f / f_{Nyq}\) of the FFT peak

To understand the meaning of this amplitude: if the modulation is described by:

..math:: p = overline{p} (1 + a * sin(x))

this function returns a.

If it is a sum of sinusoidal harmonics instead ..math:: p = overline{p} (1 + sum_l a_l * sin(lx)) a is equivalent to \(\sqrt(\sum_l a_l^2)\).

See power_upper_limit

Parameters:
pmeas: float

The measured value of power

counts: int

The number of counts in the light curve used to calculate the spectrum

Returns:
a: float

The modulation amplitude that could produce P>pmeas with 1 - c probability

Other Parameters:
n: int

The number of summed powers to obtain pmeas. It can be multiple harmonics of the PDS, adjacent bins in a PDS summed to collect all the power in a QPO, the n in Z^2_n or the number of averaged PDS

c: float

The confidence value for the probability (e.g. 0.95 = 95%)

fft_corr: bool

Apply a correction for the expected power concentrated in an FFT bin, which is about 0.773 on average (it’s 1 at the center of the bin, 2/pi at the bin edge.

nyq_ratio: float

Ratio of the frequency of this feature with respect to the Nyquist frequency. Important to know when dealing with FFTs, because the FFT response decays between 0 and f_Nyq similarly to the response inside a frequency bin: from 1 at 0 Hz to ~2/pi at f_Nyq

summed_flag: bool

If True, pmeas is the sum of n powers. If False, pmeas is the average of n powers. This is relevant when dealing with averaged PDS, where the powers are averaged rather than summed. For example, Z^2_n searches deal with summed powers (i.e. summed_flag=True), while if power spectrum is averaged to improve the statistics the summed_flag should be set to False.

Examples

>>> aup = amplitude_upper_limit(40, 30000, 1, 0.99)
>>> aup_nyq = amplitude_upper_limit(40, 30000, 1, 0.99, nyq_ratio=1)
>>> assert np.isclose(aup_nyq, aup / (2 / np.pi))
>>> aup_corr = amplitude_upper_limit(40, 30000, 1, 0.99, fft_corr=True)
>>> assert np.isclose(aup_corr, aup / np.sqrt(0.773))
stingray.stats.chi2_logp(chi2, dof)[source]

Log survival function of the chi-squared distribution.

Returns a scalar if both arguments are scalars, and otherwise an array of the shape they broadcast to.

Examples

>>> chi2 = 31
>>> # Test check on dof
>>> chi2_logp(chi2, 1)
Traceback (most recent call last):
    ...
ValueError: The number of degrees of freedom cannot be < 2
>>> # Test that approximate function works as expected. chi2 / dof > 15,
>>> # but small and safe number in order to compare to scipy.stats
>>> assert np.isclose(chi2_logp(chi2, 2), stats.chi2.logsf(chi2, 2), atol=0.1)
>>> chi2 = np.array([5, 32])
>>> assert np.allclose(chi2_logp(chi2, 2), stats.chi2.logsf(chi2, 2), atol=0.1)
stingray.stats.classical_pvalue(power, nspec)[source]

Note: This is stingray’s original implementation of the probability distribution for the power spectrum. It is superseded by the implementation in pds_probability for practical purposes, but remains here for backwards compatibility and for its educational value as a clear, explicit implementation of the correct probability distribution.

Compute the probability of detecting the current power under the assumption that there is no periodic oscillation in the data.

This computes the single-trial p-value that the power was observed under the null hypothesis that there is no signal in the data.

Important: the underlying assumptions that make this calculation valid are:

  1. the powers in the power spectrum follow a chi-square distribution

  2. the power spectrum is normalized according to [Leahy 1983]_, such that the powers have a mean of 2 and a variance of 4

  3. there is only white noise in the light curve. That is, there is no aperiodic variability that would change the overall shape of the power spectrum.

Also note that the p-value is for a single trial, i.e. the power currently being tested. If more than one power or more than one power spectrum are being tested, the resulting p-value must be corrected for the number of trials (Bonferroni correction).

Mathematical formulation in [Groth 1975]_. Original implementation in IDL by Anna L. Watts.

Parameters:
powerfloat

The squared Fourier amplitude of a spectrum to be evaluated

nspecint

The number of spectra or frequency bins averaged in power. This matters because averaging spectra or frequency bins increases the signal-to-noise ratio, i.e. makes the statistical distributions of the noise narrower, such that a smaller power might be very significant in averaged spectra even though it would not be in a single power spectrum.

Returns:
pvalfloat

The classical p-value of the observed power being consistent with the null hypothesis of white noise

References

stingray.stats.equivalent_gaussian_Nsigma(p)[source]

Number of Gaussian sigmas corresponding to tail probability.

This function computes the value of the characteristic function of a standard Gaussian distribution for the tail probability equivalent to the provided p-value, and turns this value into units of standard deviations away from the Gaussian mean. This allows the user to make a statement about the signal such as “I detected this pulsation at 4.1 sigma

The example values below are obtained by brute-force integrating the Gaussian probability density function using the mpmath library between Nsigma and +inf.

Examples

>>> assert np.isclose(equivalent_gaussian_Nsigma(0.15865525393145707), 1,
...                   atol=0.01)
>>> assert np.isclose(equivalent_gaussian_Nsigma(0.0013498980316301035), 3,
...                   atol=0.01)
>>> assert np.isclose(equivalent_gaussian_Nsigma(9.865877e-10), 6,
...                   atol=0.01)
>>> assert np.isclose(equivalent_gaussian_Nsigma(6.22096e-16), 8,
...                   atol=0.01)
>>> assert np.isclose(equivalent_gaussian_Nsigma(3.0567e-138), 25, atol=0.1)
stingray.stats.fold_detection_level(nbin, epsilon=0.01, ntrial=1)[source]

Return the detection level for a folded profile.

See Leahy et al. (1983).

Parameters:
nbinint

The number of bins in the profile

epsilonfloat, default 0.01

The fractional probability that the signal has been produced by noise

Returns:
detlevfloat

The epoch folding statistics corresponding to a probability epsilon * 100 % that the signal has been produced by noise

Other Parameters:
ntrialint

The number of trials executed to find this profile

stingray.stats.fold_profile_logprobability(stat, nbin, ntrial=1)[source]

Calculate the probability of a certain folded profile, due to noise.

Parameters:
statfloat

The epoch folding statistics

nbinint

The number of bins in the profile

Returns:
logpfloat

The log-probability that the profile has been produced by noise

Other Parameters:
ntrialint

The number of trials executed to find this profile

stingray.stats.fold_profile_probability(stat, nbin, ntrial=1)[source]

Calculate the probability of a certain folded profile, due to noise.

Parameters:
statfloat

The epoch folding statistics

nbinint

The number of bins in the profile

Returns:
pfloat

The probability that the profile has been produced by noise

Other Parameters:
ntrialint

The number of trials executed to find this profile

stingray.stats.p_multitrial_from_single_trial(p1, n)[source]

Calculate a multi-trial p-value from a single-trial one.

Calling p the probability of a single success, the Binomial distributions says that the probability at least one outcome in n trials is

\[P(k\geq 1) = \sum_{k\geq 1} \binom{n}{k} p^k (1-p)^{(n-k)}\]

or more simply, using P(k ≥ 0) = 1

\[P(k\geq 1) = 1 - \binom{n}{0} (1-p)^n = 1 - (1-p)^n\]
Parameters:
p1float

The significance at which we reject the null hypothesis on each single trial.

nint

The number of trials

Returns:
pnfloat

The significance at which we reject the null hypothesis after multiple trials

stingray.stats.p_single_trial_from_p_multitrial(pn, n)[source]

Calculate the single-trial p-value from a total p-value

Let us say that we want to reject a null hypothesis at the pn level, after executing n different measurements. This might be the case because, e.g., we want to have a 1% probability of detecting a signal in an entire power spectrum, and we need to correct the detection level accordingly.

The typical procedure is dividing the initial probability (often called _epsilon_) by the number of trials. This is called the Bonferroni correction and it is often a good approximation, when pn is low: p1 = pn / n.

However, if pn is close to 1, this approximation gives incorrect results.

Here we calculate this probability by inverting the Binomial problem. Given that (see p_multitrial_from_single_trial) the probability of getting more than one hit in n trials, given the single-trial probability p, is

\[P (k \geq 1) = 1 - (1 - p)^n,\]

we get the single trial probability from the multi-trial one from

\[p = 1 - (1 - P)^{(1/n)}\]

This is also known as Šidák correction.

Parameters:
pnfloat

The significance at which we want to reject the null hypothesis after multiple trials

nint

The number of trials

Returns:
p1float

The significance at which we reject the null hypothesis on each single trial.

stingray.stats.pds_detection_level(epsilon=0.01, ntrial=1, n_summed_spectra=1, n_rebin=1)[source]

Detection level for a PDS.

Return the detection level (with probability 1 - epsilon) for a Power Density Spectrum of nbins bins, normalized a la Leahy (1983), based on the 2-dof \({\chi}^2\) statistics, corrected for rebinning (n_rebin) and multiple PDS averaging (n_summed_spectra)

Parameters:
epsilonfloat

The single-trial probability value(s)

Other Parameters:
ntrialint

The number of independent trials (the independent bins of the PDS)

n_summed_spectraint

The number of power density spectra that have been averaged to obtain this power level

n_rebinint

The number of power density bins that have been averaged to obtain this power level

Examples

>>> assert np.isclose(pds_detection_level(0.1), 4.6, atol=0.1)
>>> assert np.allclose(pds_detection_level(0.1, n_rebin=[1]), [4.6], atol=0.1)
stingray.stats.pds_logprobability(level, ntrial=1, n_summed_spectra=1, n_rebin=1)[source]

Give the probability of a given power level in PDS.

Return the probability of a certain power level in a Power Density Spectrum of nbins bins, normalized a la Leahy (1983), based on the 2-dof \({\chi}^2\) statistics, corrected for rebinning (n_rebin) and multiple PDS averaging (n_summed_spectra)

Parameters:
levelfloat or array of floats

The power level for which we are calculating the probability

Returns:
epsilonfloat

The probability value(s)

Other Parameters:
ntrialint

The number of independent trials (the independent bins of the PDS)

n_summed_spectraint

The number of power density spectra that have been averaged to obtain this power level

n_rebinint

The number of power density bins that have been averaged to obtain this power level

Examples

Let us test that it is always consistent with pds_probability. We use relatively small power values, because for large values pds_probability underflows. >>> powers = np.random.uniform(2, 40, 10) >>> nrebin = np.random.randint(1, 10, 10) >>> nsummed = np.random.randint(1, 100, 10) >>> ntrial = np.random.randint(1, 10000, 10) >>> logp = pds_logprobability(powers, ntrial, nsummed, nrebin) >>> p = pds_probability(powers, ntrial, nsummed, nrebin) >>> assert np.allclose(p, np.exp(logp))

stingray.stats.pds_probability(level, ntrial=1, n_summed_spectra=1, n_rebin=1)[source]

Give the probability of a given power level in PDS.

Return the probability of a certain power level in a Power Density Spectrum of nbins bins, normalized a la Leahy (1983), based on the 2-dof \({\chi}^2\) statistics, corrected for rebinning (n_rebin) and multiple PDS averaging (n_summed_spectra)

Parameters:
levelfloat or array of floats

The power level for which we are calculating the probability

Returns:
epsilonfloat

The probability value(s)

Other Parameters:
ntrialint

The number of independent trials (the independent bins of the PDS)

n_summed_spectraint

The number of power density spectra that have been averaged to obtain this power level

n_rebinint

The number of power density bins that have been averaged to obtain this power level

stingray.stats.pf_from_a(a)[source]

Pulsed fraction from fractional amplitude of modulation.

If the pulsed profile is defined as p = mean * (1 + a * sin(phase)),

we define “pulsed fraction” as 2a/b, where b = mean + a is the maximum and a is the amplitude of the modulation.

Hence, pulsed fraction = 2a/(1+a)

Examples

>>> pf_from_a(1)
1.0
>>> pf_from_a(0)
0.0
stingray.stats.pf_from_ssig(ssig, ncounts)[source]

Estimate pulsed fraction for a sinusoid from a given Z or PDS power.

See a_from_ssig and pf_from_a for more details

Examples

>>> assert np.isclose(round(a_from_pf(pf_from_ssig(150, 30000)), 1), 0.1)
stingray.stats.pf_upper_limit(*args, **kwargs)[source]

Upper limit on pulsed fraction, given a measured power in the PDS/Z search.

See power_upper_limit and pf_from_ssig. All arguments are the same as amplitude_upper_limit

Parameters:
pmeas: float

The measured value of power

counts: int

The number of counts in the light curve used to calculate the spectrum

Returns:
pf: float

The pulsed fraction that could produce P>pmeas with 1 - c probability

Other Parameters:
n: int

The number of summed powers to obtain pmeas. It can be multiple harmonics of the PDS, adjacent bins in a PDS summed to collect all the power in a QPO, or the n in Z^2_n

c: float

The confidence value for the probability (e.g. 0.95 = 95%)

fft_corr: bool

Apply a correction for the expected power concentrated in an FFT bin, which is about 0.773 on average (it’s 1 at the center of the bin, 2/pi at the bin edge.

nyq_ratio: float

Ratio of the frequency of this feature with respect to the Nyquist frequency. Important to know when dealing with FFTs, because the FFT response decays between 0 and f_Nyq similarly to the response inside a frequency bin: from 1 at 0 Hz to ~2/pi at f_Nyq

Examples

>>> pfup = pf_upper_limit(40, 30000, 1, 0.99)
>>> assert np.isclose(pfup, 0.13, atol=0.01)
stingray.stats.phase_dispersion_detection_level(nsamples, nbin, epsilon=0.01, ntrial=1)[source]

Return the detection level for a phase dispersion minimization periodogram..

Parameters:
nsamplesint

The number of time bins in the light curve

nbinint

The number of bins in the profile

epsilonfloat, default 0.01

The fractional probability that the signal has been produced by noise

Returns:
detlevfloat

The epoch folding statistics corresponding to a probability epsilon * 100 % that the signal has been produced by noise

Other Parameters:
ntrialint

The number of trials executed to find this profile

stingray.stats.phase_dispersion_logprobability(stat, nsamples, nbin, ntrial=1)[source]

Calculate the log-probability of a peak in a phase dispersion minimization periodogram, due to noise.

Uses the beta-distribution from Czerny-Schwarzendorf (1997).

Parameters:
statfloat

The value of the PDM inverse peak

nsamplesint

The number of samples in the time series

nbinint

The number of bins in the profile

Returns:
logpfloat

The log-probability that the profile has been produced by noise

Other Parameters:
ntrialint

The number of trials executed to find this profile

stingray.stats.phase_dispersion_probability(stat, nsamples, nbin, ntrial=1)[source]

Calculate the probability of a peak in a phase dispersion minimization periodogram, due to noise.

Uses the beta-distribution from Czerny-Schwarzendorf (1997).

Parameters:
statfloat

The value of the PDM inverse peak

nsamplesint

The number of samples in the time series

nbinint

The number of bins in the profile

Returns:
pfloat

The probability that the profile has been produced by noise

Other Parameters:
ntrialint

The number of trials executed to find this profile

stingray.stats.power_confidence_limits(preal, n=1, c=0.95, summed_flag=True)[source]

Confidence limits on power, given a (theoretical) signal power.

This is to be used when we expect a given power (e.g. from the pulsed fraction measured in previous observations) and we want to know the range of values the measured power could take to a given confidence level. Adapted from Vaughan et al. 1994, noting that, after appropriate normalization of the spectral stats, the distribution of powers in the PDS and the Z^2_n searches is always described by a noncentral chi squared distribution.

Parameters:
preal: float

The theoretical signal-generated value of power

Returns:
pmeas: [float, float]

The lower and upper bounds of the symmetric c-level confidence interval on the measured power

Other Parameters:
n: int

The number of summed powers to obtain the result. It can be multiple harmonics of the PDS, adjacent bins in a PDS summed to collect all the power in a QPO, or the n in Z^2_n

c: float

The confidence level (e.g. 0.95=95%)

summed_flag: bool

Whether the power is (if True) summed or (if False) averaged. Only relevant if n > 1

Examples

>>> cl = power_confidence_limits(150, c=0.68, n=1, summed_flag=True)
>>> assert np.allclose(cl, [127, 176], atol=1)
>>> cl = power_confidence_limits(150, c=0.68, n=8, summed_flag=True)
>>> assert np.allclose(cl, [141, 190], atol=1)
>>> cl = power_confidence_limits(150, c=0.68, n=1, summed_flag=False)
>>> assert np.allclose(cl, [127, 176], atol=1)
>>> cl = power_confidence_limits(150, c=0.68, n=8, summed_flag=False)
>>> assert np.allclose(cl, [143, 160], atol=1)
stingray.stats.power_upper_limit(pmeas, n=1, c=0.95, summed_flag=True)[source]

Upper limit on signal power, given a measured power in the PDS/Z search.

Adapted from Vaughan et al. 1994, noting that, after appropriate normalization of the spectral stats, the distribution of powers in the PDS and the Z^2_n searches is always described by a noncentral chi squared distribution.

Note that Vaughan+94 gives p(pmeas | preal), while we are interested in p(real | pmeas), which is not described by the NCX2 stat. Rather than integrating the CDF of this probability distribution, we start from a reasonable approximation and fit to find the preal that gives pmeas as a (e.g.95%) confidence limit.

As Vaughan+94 shows, this power is always larger than the observed one. This is because we are looking for the maximum signal power that, combined with noise powers, would give the observed power. This involves the possibility that noise powers partially cancel out some signal power.

Parameters:
pmeas: float

The measured value of power

Returns:
psig: float

The signal power that could produce P>pmeas with 1 - c probability

Other Parameters:
n: int

The number of summed powers to obtain pmeas. It can be multiple harmonics of the PDS, adjacent bins in a PDS summed to collect all the power in a QPO, the n in Z^2_n or the number of averaged PDS

c: float

The confidence value for the probability (e.g. 0.95 = 95%)

summed_flag: bool

If True, pmeas is the sum of n powers. If False, pmeas is the average of n powers. This is relevant when dealing with averaged PDS, where the powers are averaged rather than summed. For example, Z^2_n searches deal with summed powers (i.e. summed_flag=True), while if power spectrum is averaged to improve the statistics the summed_flag should be set to False.

Examples

>>> pup = power_upper_limit(40, 1, 0.99)
>>> assert np.isclose(pup, 75, atol=2)
stingray.stats.ssig_from_a(a, ncounts)[source]

Theoretical power in the Z or PDS search for a sinusoid of amplitude a.

From Leahy et al. 1983, given a pulse profile p = lambda * (1 + a * sin(phase)), The theoretical value of Z^2_n is Ncounts / 2 * a^2

Note that if there are multiple sinusoidal components, one can use a = sqrt(sum(a_l)) (Bachetti+2021b)

Examples

>>> round(ssig_from_a(0.1, 30000), 1)
150.0
stingray.stats.ssig_from_pf(pf, ncounts)[source]

Theoretical power in the Z or PDS for a sinusoid of pulsed fraction pf.

See ssig_from_a and a_from_pf for more details

Examples

>>> assert round(ssig_from_pf(pf_from_a(0.1), 30000), 1) == 150.0
stingray.stats.z2_n_detection_level(n=2, epsilon=0.01, ntrial=1, n_summed_spectra=1)[source]

Return the detection level for the Z^2_n statistics.

See Buccheri et al. (1983), Bendat and Piersol (1971).

Parameters:
nint, default 2

The n in $Z^2_n$ (number of harmonics, including the fundamental)

epsilonfloat, default 0.01

The fractional probability that the signal has been produced by noise

Returns:
detlevfloat

The epoch folding statistics corresponding to a probability epsilon * 100 % that the signal has been produced by noise

Other Parameters:
ntrialint

The number of trials executed to find this profile

n_summed_spectraint

Number of Z_2^n periodograms that are being averaged

stingray.stats.z2_n_logprobability(z2, n, ntrial=1, n_summed_spectra=1)[source]

Calculate the probability of a certain folded profile, due to noise.

Parameters:
z2float

A Z^2_n statistics value

nint, default 2

The n in $Z^2_n$ (number of harmonics, including the fundamental)

Returns:
pfloat

The probability that the Z^2_n value has been produced by noise

Other Parameters:
ntrialint

The number of trials executed to find this profile

n_summed_spectraint

Number of Z_2^n periodograms that were averaged to obtain z2

stingray.stats.z2_n_probability(z2, n, ntrial=1, n_summed_spectra=1)[source]

Calculate the probability of a certain folded profile, due to noise.

Parameters:
z2float

A Z^2_n statistics value

nint, default 2

The n in $Z^2_n$ (number of harmonics, including the fundamental)

Returns:
pfloat

The probability that the Z^2_n value has been produced by noise

Other Parameters:
ntrialint

The number of trials executed to find this profile

n_summed_spectraint

Number of Z_2^n periodograms that were averaged to obtain z2

GTI Functionality

stingray.gti.append_gtis(gti0, gti1)[source]

Union of two non-overlapping GTIs.

If the two GTIs “touch”, this is tolerated and the touching GTIs are joined in a single one.

Parameters:
gti0: 2-d float array

List of GTIs of the form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...].

gti1: 2-d float array

List of GTIs of the form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...].

Returns:
gti: 2-d float array

The newly created GTI array.

Examples

>>> assert np.allclose(append_gtis([[0, 1]], [[2, 3]]), [[0, 1], [2, 3]])
>>> np.allclose(append_gtis([[0, 1], [4, 5]], [[2, 3]]),
...             [[0, 1], [2, 3], [4, 5]])
True
>>> assert np.allclose(append_gtis([[0, 1]], [[1, 3]]), [[0, 3]])
stingray.gti.bin_intervals_from_gtis(gtis, segment_size, time, dt=None, fraction_step=1, epsilon=0.001)[source]

Compute start/stop times of equal time intervals, compatible with GTIs, and map them to the indices of an array of time stamps.

Used to start each FFT/PDS/cospectrum from the start of a GTI, and stop before the next gap in data (end of GTI). In this case, it is necessary to specify the time array containing the times of the light curve bins. Returns start and stop bins of the intervals to use for the PDS.

Parameters:
gtis2-d float array

List of GTIs of the form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...].

segment_sizefloat

Length of each time segment.

timearray-like

Array of time stamps.

Returns:
spectrum_start_binsarray-like

List of starting bins in the original time array to use in spectral calculations.

spectrum_stop_binsarray-like

List of end bins to use in the spectral calculations.

Other Parameters:
dtfloat, default median(diff(time))

Time resolution of the light curve.

epsilonfloat, default 0.001

The tolerance, in fraction of dt, for the comparisons at the borders.

fraction_stepfloat

If the step is not a full segment_size but less (e.g. a moving window), this indicates the ratio between step step and segment_size (e.g. 0.5 means that the window shifts by half segment_size).

Examples

>>> time = np.arange(0.5, 13.5)
>>> gtis = [[0, 5], [6, 8], [9, 10]]
>>> segment_size = 2
>>> start_bins, stop_bins = bin_intervals_from_gtis(gtis,segment_size,time)
>>> assert np.allclose(start_bins, [0, 2, 6])
>>> assert np.allclose(stop_bins, [2, 4, 8])
>>> assert np.allclose(time[start_bins[0]:stop_bins[0]], [0.5, 1.5])
>>> assert np.allclose(time[start_bins[1]:stop_bins[1]], [2.5, 3.5])
stingray.gti.calculate_segment_bin_start(startbin, stopbin, nbin, fraction_step=1)[source]

Get the starting indices of intervals of equal length.

A bit like np.arange, but checks that the last number is at least nbin less than stopbin. Useful when getting starting intervals of equal chunks of a binned light curve.

It is possible to make these intervals sliding, through the fraction_step parameter.

Parameters:
startbinint

Starting bin of the interval.

stopbinint

Last bin of the interval.

nbinint

Number of bins in each interval.

Returns:
spectrum_start_binsarray-like

List of starting bins in the original time array to use in spectral calculations.

Other Parameters:
fraction_stepfloat

If the step is not a full nbin but less (e.g. a moving window), this indicates the ratio between the step and nbin (e.g. 0.5 means that the window shifts by half nbin).

Examples

>>> st = calculate_segment_bin_start(0, 10000, 10000)
>>> int(st[-1])
0
>>> st = calculate_segment_bin_start(0, 5, 2)
>>> int(st[-1])
2
>>> st = calculate_segment_bin_start(0, 6, 2)
>>> int(st[-1])
4
stingray.gti.check_gtis(gti)[source]

Check if GTIs are well-behaved.

Check that:

  1. the shape of the GTI array is correct;

  2. no start > end

  3. no overlaps.

Parameters:
gtilist

A list of GTI (start, stop) pairs extracted from the FITS file.

Raises:
TypeError

If GTIs are of the wrong shape

ValueError

If GTIs have overlapping or displaced values

stingray.gti.check_separate(gti0, gti1)[source]

Check if two GTIs do not overlap.

Parameters:
gti0: 2-d float array

List of GTIs of form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...].

gti1: 2-d float array

List of GTIs of form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...].

Returns:
separate: bool

True if GTIs are mutually exclusive, False if not.

Examples

>>> gti0 = [[0, 10]]
>>> gti1 = [[20, 30]]
>>> assert check_separate(gti0, gti1)
>>> gti0 = [[0, 10]]
>>> gti1 = [[0, 10]]
>>> check_separate(gti0, gti1)
False
>>> gti0 = [[0, 10]]
>>> gti1 = [[10, 20]]
>>> assert check_separate(gti0, gti1)
>>> gti0 = [[0, 11]]
>>> gti1 = [[10, 20]]
>>> check_separate(gti0, gti1)
False
>>> gti0 = [[0, 11]]
>>> gti1 = [[10, 20]]
>>> check_separate(gti1, gti0)
False
>>> gti0 = [[0, 10], [30, 40]]
>>> gti1 = [[11, 28]]
>>> assert check_separate(gti0, gti1)
stingray.gti.create_gti_from_condition(time, condition, safe_interval=0, dt=None)[source]

Create a GTI list from a time array and a boolean mask (condition).

Parameters:
timearray-like

Array containing time stamps.

conditionarray-like

An array of bools, of the same length of time. A possible condition can be, e.g., the result of lc > 0.

Returns:
gtis[[gti0_0, gti0_1], [gti1_0, gti1_1], ...]

The newly created GTIs.

Other Parameters:
safe_intervalfloat or [float, float]

A safe interval to exclude at both ends (if single float) or the start and the end (if pair of values) of GTIs.

dtfloat

The width (in sec) of each bin of the time array. Can be irregular.

stingray.gti.create_gti_mask(time, gtis, safe_interval=None, min_length=0, return_new_gtis=False, dt=None, epsilon=0.001)[source]

Create GTI mask.

Assumes that no overlaps are present between GTIs

Parameters:
timenumpy.ndarray

An array of time stamps

gtis[[g0_0, g0_1], [g1_0, g1_1], ...], float array-like

The list of GTIs

Returns:
maskbool array

A mask labelling all time stamps that are included in the GTIs versus those that are not.

new_gtisNx2 array

An array of new GTIs created by this function.

Other Parameters:
safe_intervalfloat or [float, float], default None

A safe interval to exclude at both ends (if single float) or the start and the end (if pair of values) of GTIs. If None, no safe interval is applied to data.

min_lengthfloat

An optional minimum length for the GTIs to be applied. Only GTIs longer than min_length will be considered when creating the mask.

return_new_gtisbool

If True`, return the list of new GTIs (if min_length > 0)

dtfloat

Time resolution of the data, i.e. the interval between time stamps.

epsilonfloat

Fraction of dt that is tolerated at the borders of a GTI.

stingray.gti.create_gti_mask_complete(time, gtis, safe_interval=0, min_length=0, return_new_gtis=False, dt=None, epsilon=0.001)[source]

Create GTI mask, allowing for non-constant dt.

Assumes that no overlaps are present between GTIs.

Parameters:
timenumpy.ndarray

An array of time stamps.

gtis[[g0_0, g0_1], [g1_0, g1_1], ...], float array-like

The list of GTIs.

Returns:
maskbool array

A mask labelling all time stamps that are included in the GTIs versus those that are not.

new_gtisNx2 array

An array of new GTIs created by this function.

Other Parameters:
safe_intervalfloat or [float, float]

A safe interval to exclude at both ends (if single float) or the start and the end (if pair of values) of GTIs.

min_lengthfloat

An optional minimum length for the GTIs to be applied. Only GTIs longer than min_length will be considered when creating the mask.

return_new_gtisbool

If True, return the list of new GTIs (if min_length > 0).

dtfloat

Time resolution of the data, i.e. the interval between time stamps.

epsilonfloat

Fraction of dt that is tolerated at the borders of a GTI.

stingray.gti.create_gti_mask_jit(time, gtis, mask, gti_mask, min_length=0)[source]

Compiled and fast function to create GTI mask.

Parameters:
timenumpy.ndarray

An array of time stamps

gtisiterable of (start, stop) pairs

The list of GTIs.

masknumpy.ndarray

A pre-assigned array of zeros of the same shape as time Records whether a time stamp is part of the GTIs.

gti_masknumpy.ndarray

A pre-assigned array zeros in the same shape as time; records start/stop of GTIs.

min_lengthfloat

An optional minimum length for the GTIs to be applied. Only GTIs longer than min_length will be considered when creating the mask.

stingray.gti.cross_gtis(gti_list)[source]

From multiple GTI lists, extract the common intervals EXACTLY.

Parameters:
gti_listarray-like

List of GTI arrays, each one in the usual format [[gti0_0, gti0_1], [gti1_0, gti1_1], ...].

Returns:
gti0: 2-d float array

[[gti0_0, gti0_1], [gti1_0, gti1_1], ...] The newly created GTIs.

See also

cross_two_gtis

Extract the common intervals from two GTI lists EXACTLY

Examples

>>> gti1 = np.array([[1, 2]])
>>> gti2 = np.array([[1, 2]])
>>> newgti = cross_gtis([gti1, gti2])
>>> assert np.allclose(newgti, [[1, 2]])
>>> gti1 = np.array([[1, 4]])
>>> gti2 = np.array([[1, 2], [2, 4]])
>>> newgti = cross_gtis([gti1, gti2])
>>> assert np.allclose(newgti, [[1, 4]])
stingray.gti.cross_two_gtis(gti0, gti1)[source]

Extract the common intervals from two GTI lists EXACTLY.

Parameters:
gti0iterable of the form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...]
gti1iterable of the form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...]

The two lists of GTIs to be crossed.

Returns:
gtis[[gti0_0, gti0_1], [gti1_0, gti1_1], ...]

The newly created GTIs.

See also

cross_gtis

From multiple GTI lists, extract common intervals EXACTLY

Examples

>>> gti1 = np.array([[1, 2]])
>>> gti2 = np.array([[1, 2]])
>>> newgti = cross_two_gtis(gti1, gti2)
>>> assert np.allclose(newgti, [[1, 2]])
>>> gti1 = np.array([[1, 4]])
>>> gti2 = np.array([[1, 2], [2, 4]])
>>> newgti = cross_two_gtis(gti1, gti2)
>>> assert np.allclose(newgti, [[1, 4]])
>>> gti1 = np.array([[1, 2]])
>>> gti2 = np.array([[2, 3]])
>>> newgti = cross_two_gtis(gti1, gti2)
>>> len(newgti)
0
stingray.gti.eliminate_short_gtis(gtis, min_length, only_repeated=False)[source]

Eliminate GTIs shorter than a given length.

Parameters:
gtis2-d float array

List of GTIs of the form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...]

min_lengthfloat

Minimum length of GTIs to keep, in seconds

Returns:
new_gtis2-d float array

List of GTIs of the form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...], with GTIs shorter than the given length removed

Other Parameters:
only_repeatedbool

If True, only eliminate GTIs that are shorter than the given length and are separated by less than the given length from another problematic GTI. If False, eliminate all GTIs shorter than the given length, regardless of their separation from other GTIs.

Examples

min_length is shorter than all GTIs, so no GTI is removed: >>> gtis = [[0, 30], [40, 41], [90, 120], [130, 160]] >>> new_gtis = eliminate_short_gtis(gtis, 0.5) >>> assert np.allclose(new_gtis, [[0, 30], [40, 41], [90, 120], [130, 160]])

min_length is longer than one GTI, so that GTI is removed: >>> new_gtis = eliminate_short_gtis(gtis, 2) >>> assert np.allclose(new_gtis, [[0, 30], [90, 120], [130, 160]])

min_length is longer than one GTI, but only_repeated=True only removes GTIs that are part of a repeated pattern of short GTIs: >>> new_gtis = eliminate_short_gtis(gtis, 2, only_repeated=True) >>> assert np.allclose(new_gtis, [[0, 30], [40, 41], [90, 120], [130, 160]])

Two short GTIs in a row, separated by less than min_length, are removed when only_repeated=True: >>> gtis = [[0, 30], [40, 41], [41.5, 42], [90, 120], [130, 160]] >>> new_gtis = eliminate_short_gtis(gtis, 2, only_repeated=True) >>> assert np.allclose(new_gtis, [[0, 30], [90, 120], [130, 160]])

Four short GTIs in a row and only_repeated=True, but only three of them are separated by less than min_length. Those three are removed, while the one that is separated by more than min_length from the others is kept: >>> gtis = [[0, 30], [31, 32], [40, 40.5], [41, 41.3], [41.5, 42], [90, 120], [130, 160]] >>> new_gtis = eliminate_short_gtis(gtis, 2, only_repeated=True) >>> assert np.allclose(new_gtis, [[0, 30], [31, 32], [90, 120], [130, 160]])

stingray.gti.find_large_bad_time_intervals(gtis, bti_length_limit=86400)[source]

Find large bad time intervals (BTIs) in a list of GTIs, and split the GTI list accordingly.

Parameters:
gtis2-d float array

List of GTIs of the form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...]

bti_length_limitfloat

Maximum length of a BTI. If a BTI is longer than this, an edge will be returned at the midpoint of the BTI.

Returns:
bad_interval_midpointslist of float

List of midpoints of large bad time intervals

Examples

>>> gtis = [[0, 30], [86450, 86460], [86480, 86490]]
>>> bad_interval_midpoints = find_large_bad_time_intervals(gtis)
>>> assert np.allclose(bad_interval_midpoints, [43240])
stingray.gti.generate_indices_of_boundaries(times, gti, segment_size=None, dt=0)[source]

Get index boundaries and times from different parts of the observation.

It wraps around generate_indices_of_gti_boundaries, generate_indices_of_segment_boundaries_binned, and generate_indices_of_segment_boundaries_unbinned depending on:

  • segment_size being None (give GTI boundaries, segment boundaries otherwise)

  • dt being 0 or nonzero (unevenly sampled, evenly sampled otherwise)

Examples

>>> times = [0.1, 0.2, 0.5, 0.8, 1.1]
>>> gtis = [[0, 0.55], [0.6, 2.1]]
>>> vals0 = generate_indices_of_boundaries(times, gtis, segment_size=None)
>>> vals1 = generate_indices_of_gti_boundaries(times, gtis)
>>> assert check_iterables_close(vals0, vals1)
>>> vals0 = generate_indices_of_boundaries(times, gtis, segment_size=0.5)
>>> vals1 = generate_indices_of_segment_boundaries_unbinned(times, gtis, segment_size=0.5)
>>> assert check_iterables_close(vals0, vals1)
>>> times = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
>>> gtis = [[0.05, 0.55]]
>>> vals0 = generate_indices_of_boundaries(times, gtis, segment_size=0.5, dt=0.1)
>>> vals1 = generate_indices_of_segment_boundaries_binned(times, gtis, 0.5, dt=0.1)
>>> assert check_iterables_close(vals0, vals1)
stingray.gti.generate_indices_of_gti_boundaries(times, gti, dt=0)[source]

Get the indices of events from different GTIs of the observation.

This is a generator, yielding the boundaries of each GTI and the corresponding indices in the time array.

Parameters:
timesfloat np.array

Array of times.

gti[[gti00, gti01], [gti10, gti11], …]

Good time intervals.

Yields:
g0: float

Start time of current GTI.

g1: float

End time of current GTI.

startidx: int

Start index of the current GTI in the time array.

stopidx: int

End index of the current GTI in the time array. Note that this is larger by one, so that time[startidx:stopidx] returns the correct time interval.

Other Parameters:
dtfloat

If times are uniformly binned, this is the binning time.

Examples

>>> times = [0.1, 0.2, 0.5, 0.8, 1.1]
>>> gtis = [[0, 0.55], [0.6, 2.1]]
>>> vals = generate_indices_of_gti_boundaries(times, gtis)
>>> v0 = next(vals)
>>> assert np.allclose(v0[:2], gtis[0])
>>> assert np.allclose(v0[2:], [0, 3])
stingray.gti.generate_indices_of_segment_boundaries_binned(times, gti, segment_size, dt=None)[source]

Get the indices of binned times from different segments of the observation.

This is a generator, yielding the boundaries of each segment and the corresponding indices in the time array

Parameters:
timesfloat np.array

Array of times, uniformly sampled

gti[[gti00, gti01], [gti10, gti11], …]

good time intervals

segment_sizefloat

length of segments

Yields:
t0: float

First time value, from the time array, in the current segment

t1: float

Last time value, from the time array, in the current segment

startidx: int

Start index of the current segment in the time array

stopidx: int

End index of the current segment in the time array. Note that this is larger by one, so that time[startidx:stopidx] returns the correct time interval.

Examples

>>> times = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
>>> gtis = [[0.05, 0.55]]
>>> vals = generate_indices_of_segment_boundaries_binned(times, gtis, 0.5, dt=0.1)
>>> v0 = next(vals)
>>> assert np.allclose(v0[:2], [0.05, 0.55])
>>> assert np.allclose(v0[2:], [0, 5])
stingray.gti.generate_indices_of_segment_boundaries_unbinned(times, gti, segment_size, check_sorted=True)[source]

Get the indices of events from different segments of the observation.

This is a generator, yielding the boundaries of each segment and the corresponding indices in the time array.

Parameters:
timesfloat np.array

Array of times.

gti[[gti00, gti01], [gti10, gti11], …]

Good time intervals.

segment_sizefloat

Length of segments.

Yields:
t0: float

Start time of current segment.

t1: float

End time of current segment.

startidx: int

Start index of the current segment in the time array.

stopidx: int

End index of the current segment in the time array. Note that this is larger by one, so that time[startidx:stopidx] returns the correct time interval.

Other Parameters:
check_sortedbool, default True

If True, checks that the time array is sorted.

Examples

>>> times = [0.1, 0.2, 0.5, 0.8, 1.1]
>>> gtis = [[0, 0.55], [0.6, 2.1]]
>>> vals = generate_indices_of_segment_boundaries_unbinned(
...    times, gtis, 0.5)
>>> v0 = next(vals)
>>> assert np.allclose(v0[:2], [0, 0.5])
>>> # Note: 0.5 is not included in the interval
>>> assert np.allclose(v0[2:], [0, 2])
>>> v1 = next(vals)
>>> assert np.allclose(v1[:2], [0.6, 1.1])
>>> # Again: 1.1 is not included in the interval
>>> assert np.allclose(v1[2:], [3, 4])
stingray.gti.get_btis(gtis, start_time=None, stop_time=None)[source]

From GTIs, obtain bad time intervals, i.e. the intervals not covered by the GTIs.

GTIs have to be well-behaved, in the sense that they have to pass check_gtis.

Parameters:
gtisiterable

A list of GTIs.

start_timefloat

Optional start time of the overall observation (e.g. can be earlier than the first time stamp in gtis).

stop_timefloat

Optional stop time of the overall observation (e.g. can be later than the last time stamp in``gtis``).

Returns:
btisnumpy.ndarray

A list of bad time intervals.

stingray.gti.get_gti_extensions_from_pattern(lchdulist, name_pattern='GTI')[source]

Gets the GTI extensions that match a given pattern.

Parameters:
lchdulist: :class:`~astropy.io.fits.HDUList` object

The full content of a FITS file.

name_pattern: str

Pattern indicating all the GTI extensions.

Returns:
ext_list: list

List of GTI extension numbers whose name matches the input pattern.

Examples

>>> from astropy.io import fits
>>> start = np.arange(0, 300, 100)
>>> stop = start + 50.
>>> s1 = fits.Column(name='START', array=start, format='D')
>>> s2 = fits.Column(name='STOP', array=stop, format='D')
>>> hdu1 = fits.TableHDU.from_columns([s1, s2], name='GTI005XX')
>>> hdu2 = fits.TableHDU.from_columns([s1, s2], name='GTI00501')
>>> lchdulist = fits.HDUList([hdu1])
>>> gtiextn = get_gti_extensions_from_pattern(
...     lchdulist, name_pattern='GTI005[0-9]+')
>>> assert np.allclose(gtiextn, [1])
stingray.gti.get_gti_from_all_extensions(lchdulist, accepted_gtistrings=['GTI'], det_numbers=None)[source]

Intersect the GTIs from the all accepted extensions.

Parameters:
lchdulist: :class:`~astropy.io.fits.HDUList` object

The full content of a FITS file.

accepted_gtistrings: list of str

Base strings of GTI extensions. For missions adding the detector number to GTI extensions like, e.g., XMM and Chandra, this function automatically adds the detector number and looks for all matching GTI extensions (e.g. “STDGTI” will also retrieve “STDGTI05”; “GTI0” will also retrieve “GTI00501”).

Returns:
gti_list: [[gti00, gti01], [gti10, gti11], …]

List of good time intervals, as the intersection of all matching GTIs. If there are two matching extensions, with GTIs [[0, 50], [100, 200]] and [[40, 70]] respectively, this function will return [[40, 50]].

Examples

>>> from astropy.io import fits
>>> s1 = fits.Column(name='START', array=[0, 100, 200], format='D')
>>> s2 = fits.Column(name='STOP', array=[50, 150, 250], format='D')
>>> hdu1 = fits.TableHDU.from_columns([s1, s2], name='GTI00501')
>>> s1 = fits.Column(name='START', array=[200, 300], format='D')
>>> s2 = fits.Column(name='STOP', array=[250, 350], format='D')
>>> hdu2 = fits.TableHDU.from_columns([s1, s2], name='STDGTI05')
>>> lchdulist = fits.HDUList([hdu1, hdu2])
>>> gti = get_gti_from_all_extensions(
...     lchdulist, accepted_gtistrings=['GTI0', 'STDGTI'],
...     det_numbers=[5])
>>> assert np.allclose(gti, [[200, 250]])
stingray.gti.get_gti_from_hdu(gtihdu)[source]

Get the GTIs from a given FITS extension.

Parameters:
gtihdu: :class:`~astropy.io.fits.TableHDU` object

The GTI HDU.

Returns:
gti_list: [[gti00, gti01], [gti10, gti11], …]

List of good time intervals.

Examples

>>> from astropy.io import fits
>>> start = np.arange(0, 300, 100)
>>> stop = start + 50.
>>> s1 = fits.Column(name='START', array=start, format='D')
>>> s2 = fits.Column(name='STOP', array=stop, format='D')
>>> hdu1 = fits.TableHDU.from_columns([s1, s2], name='GTI00501')
>>> gti = get_gti_from_hdu(hdu1)
>>> assert np.allclose(gti, [[0, 50], [100, 150], [200, 250]])
stingray.gti.get_gti_lengths(gti)[source]

Calculate the length of each Good Time Interval.

Parameters:
gti[[gti00, gti01], [gti10, gti11], …]

The list of good time intervals.

Returns:
lengthsnp.ndarray

List of GTI lengths.

Examples

>>> gti = [[0, 1000], [1000, 1001], [3000, 3020]]
>>> assert np.allclose(get_gti_lengths(gti), [1000, 1, 20])
stingray.gti.get_total_gti_length(gti, minlen=0)[source]

Calculate the total exposure during Good Time Intervals.

Parameters:
gti[[gti00, gti01], [gti10, gti11], …]

The list of good time intervals.

minlenfloat

Minimum GTI length to consider.

Returns:
lengthfloat

The total exposure during GTIs.

Examples

>>> gti = [[0, 1000], [1000, 1001], [3000, 3020]]
>>> assert np.isclose(get_total_gti_length(gti), 1021)
>>> assert np.isclose(get_total_gti_length(gti, minlen=5), 1020)
stingray.gti.gti_border_bins(gtis, time, dt=None, epsilon=0.001)[source]

Find the indices in a time array corresponding to the borders of GTIs.

GTIs shorter than the bin time are not returned.

Parameters:
gtis2-d float array

List of GTIs of the form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...].

timearray-like

Array of time stamps.

Returns:
spectrum_start_binsarray-like

List of starting bins of each GTI

spectrum_stop_binsarray-like

List of stop bins of each GTI. The elements corresponding to these bins should not be included.

Other Parameters:
dtfloat or array of floats. Default median(diff(time))

Time resolution of the light curve. Can be an array of the same dimension as time

epsilonfloat, default 0.001

The tolerance, in fraction of dt, for the comparisons at the borders.

fraction_stepfloat

If the step is not a full segment_size but less (e.g. a moving window), this indicates the ratio between step step and segment_size (e.g. 0.5 means that the window shifts by half segment_size).

Examples

>>> times = np.arange(0.5, 13.5)
>>> gti_border_bins([[16., 18.]], times)
Traceback (most recent call last):
    ...
ValueError: Invalid time interval for the given GTIs
>>> start_bins, stop_bins = gti_border_bins(
...    [[0, 5], [6, 8]], times)
>>> assert np.allclose(start_bins, [0, 6])
>>> assert np.allclose(stop_bins, [5, 8])
>>> assert np.allclose(times[start_bins[0]:stop_bins[0]], [0.5, 1.5, 2.5, 3.5, 4.5])
>>> assert np.allclose(times[start_bins[1]:stop_bins[1]], [6.5, 7.5])
>>> start_bins, stop_bins = gti_border_bins(
...    [[0, 5], [6, 13]], times, dt=np.ones_like(times))
>>> assert np.allclose(start_bins, [0, 6])
>>> assert np.allclose(stop_bins, [5, 13])
>>> assert np.allclose(times[start_bins[0]:stop_bins[0]], [0.5, 1.5, 2.5, 3.5, 4.5])
>>> np.allclose(times[start_bins[1]:stop_bins[1]], [6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5])
True
stingray.gti.gti_len(gti)[source]

Deprecated, will be removed in version 2.0. Use get_total_gti_length.

stingray.gti.hdu_contains_gti(hdu)[source]

Test if a given FITS HDU contains a list of GTIs.

Examples

>>> from astropy.io import fits
>>> start = np.arange(0, 300, 100)
>>> stop = start + 50.
>>> s1 = fits.Column(name='START', array=start, format='D')
>>> s2 = fits.Column(name='STOP', array=stop, format='D')
>>> hdu1 = fits.TableHDU.from_columns([s1, s2], name='BLABLA')
>>> assert hdu_contains_gti(hdu1)
>>> s2 = fits.Column(name='blabla', array=stop, format='D')
>>> hdu1 = fits.TableHDU.from_columns([s1, s2], name='BLABLA')
>>> hdu_contains_gti(hdu1)
False
stingray.gti.join_equal_gti_boundaries(gti, threshold=0.0)[source]

If the start of a GTI and the end of the previous one is within a certain time value, join them.

Parameters:
gti: 2-d float array

List of GTIs of the form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...].

threshold: float number (units sec)

Maximum time interval to join two adjacent GTIs

Returns:
gti: 2-d float array

The newly created GTI array.

stingray.gti.join_gtis(gti0, gti1)[source]

Union of two GTIs.

If GTIs are mutually exclusive, it calls append_gtis. Otherwise we put the extremes of partially overlapping GTIs on an ideal line and look at the number of opened and closed intervals. When the number of closed and opened intervals is the same, the full GTI is complete and we close it.

In practice, we assign to each opening time of a GTI the value -1, and the value 1 to each closing time; when the cumulative sum is zero, the GTI has ended. The timestamp after each closed GTI is the start of a new one.

(g_all)    0     1     2     3     4     5     6     7     8     9
(cumsum)   -1   -2    -1     0    -1    -2     -1   -2    -1     0
GTI A      |-----:-----|     :     |-----:-----|     |-----:-----|
FINAL GTI  |-----:-----------|     |-----:-----------------:-----|
GTI B            |-----------|           |-----------------|

In case one GTI ends exactly where another one starts, the cumulative sum is 0 but we do not want to close. In this case, we make a check that the next element of the sequence is not equal to the one where we would close.

(g_all)    0    1,1         3,3          5
(cumsum)   -1   0,-1       -1,-2         0
GTI A      |-----|           |-----------|
FINAL GTI  |-----------------------------|
GTI B            |-----------|
Parameters:
gti0: 2-d float array

List of GTIs of the form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...]

gti1: 2-d float array

List of GTIs of the form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...]

Returns:
gti: 2-d float array

The newly created GTI

stingray.gti.load_gtis(fits_file, gtistring=None)[source]

Load Good Time Intervals (GTIs) from HDU EVENTS of file fits_file. File is expected to be in FITS format.

Parameters:
fits_filestr

File name and path for the FITS file with the GTIs to be loaded.

gtistringstr

If the name of the FITS extension with the GTIs is not GTI, the alternative name can be set with this parameter.

Returns:
gti_listlist

A list of GTI (start, stop) pairs extracted from the FITS file.

stingray.gti.merge_gtis(gti_list, strategy)[source]

Merge a list of GTIs using the specified method.

Invalid GTI lists (None or empty) are ignored.

Parameters:
gti_listlist of 2-d float arrays

List of GTIs.

Other Parameters:
strategy{“intersection”, “union”, “append”, “infer”, “none”}

Method to use to merge the GTIs. If “intersection”, the GTIs are merged using the intersection of the GTIs. If “union”, the GTIs are merged using the union of the GTIs. If “none”, a single GTI with the minimum and the maximum time stamps of all GTIs is returned. If “infer”, the strategy is decided based on the GTIs. If there are no overlaps, “union” is used, otherwise “intersection” is used. If “append”, the GTIs are simply appended but they must be mutually exclusive.

Examples

>>> gti1 = np.array([[1, 2], [3, 4], [5, 6]])
>>> gti2 = np.array([[1, 2]])
>>> gti3 = np.array([[4, 5]])
>>> gti = merge_gtis([gti1, gti2], "intersection")
>>> assert np.array_equal(gti, [[1, 2]])
>>> assert merge_gtis([gti1, gti2, gti3], "intersection") is None
>>> assert merge_gtis([gti2, gti3], "intersection") is None
>>> gti = merge_gtis([gti1, gti2], "infer")
>>> assert np.array_equal(gti, [[1, 2]])
>>> gti = merge_gtis([gti2, gti3], "infer")
>>> assert np.array_equal(gti, [[1, 2], [4, 5]])
stingray.gti.split_gtis_at_indices(gtis, index_list)[source]

Split a GTI list at the given indices, creating multiple GTI lists.

Parameters:
gtis2-d float array

List of GTIs of the form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...]

index_listint or array-like

Index or list of indices at which to split the GTIs

Returns:
gti_listslist of 2-d float arrays

List of GTI lists, split at the given indices

Examples

>>> gtis = [[0, 30], [50, 60], [80, 90]]
>>> new_gtis = split_gtis_at_indices(gtis, 1)
>>> assert np.allclose(new_gtis[0], [[0, 30]])
>>> assert np.allclose(new_gtis[1], [[50, 60], [80, 90]])
stingray.gti.split_gtis_by_exposure(gtis, exposure_per_chunk, new_interval_if_gti_sep=None)[source]

Split a list of GTIs into smaller GTI lists of a given total (approximate) exposure.

Parameters:
gtis2-d float array

List of GTIs of the form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...]

exposure_per_chunkfloat

Total exposure of each chunk, in seconds

Returns:
gti_listlist of 2-d float arrays

List of GTI lists, split into chunks of the given exposure / separated by more than the given limit separation

Other Parameters:
new_interval_if_gti_sepfloat

If the GTIs are separated by more than this time, split the observation in two.

Examples

>>> gtis = [[0, 30], [86450, 86460]]
>>> new_gtis = split_gtis_by_exposure(gtis, 400, new_interval_if_gti_sep=86400)
>>> assert np.allclose(new_gtis[0], [[0, 30]])
>>> assert np.allclose(new_gtis[1], [[86450, 86460]])
>>> gtis = [[0, 30], [40, 70], [90, 120], [130, 160]]
>>> new_gtis = split_gtis_by_exposure(gtis, 60)
>>> assert np.allclose(new_gtis[0], [[0, 30], [40, 70]])
>>> assert np.allclose(new_gtis[1], [[90, 120], [130, 160]])
stingray.gti.time_intervals_from_gtis(gtis, segment_size, fraction_step=1, epsilon=1e-05)[source]

Compute start/stop times of equal time intervals, compatible with GTIs.

Used to start each FFT/PDS/cospectrum from the start of a GTI, and stop before the next gap in data (end of GTI).

Parameters:
gtis2-d float array

List of GTIs of the form [[gti0_0, gti0_1], [gti1_0, gti1_1], ...]

segment_sizefloat

Length of the time segments

fraction_stepfloat

If the step is not a full segment_size but less (e.g. a moving window), this indicates the ratio between step step and segment_size (e.g. 0.5 means that the window shifts by half segment_size).

Returns:
spectrum_start_timesarray-like

List of starting times to use in the spectral calculations.

spectrum_stop_timesarray-like

List of end times to use in the spectral calculations.

Filter Functionality

class stingray.filters.DeadtimeFilterOutput[source]

Container for the extra output of get_deadtime_mask().

All attributes are None until that function is called with return_all=True, which fills in the event list the dead time was applied to (uf_events), a flag separating signal from background events (is_event), the deadtime values and the background events (bkg).

When the dead time is positive, uf_events and bkg hold the filtered events rather than the unfiltered ones the uf_ prefix suggests. mask is left None: the mask of good events is the first value get_deadtime_mask() returns, beside this container.

class stingray.filters.Optimal1D(model)[source]

Make a optimal filter for power spectrum or cross spectrum. It assumes that the first model is the QPO component.

Parameters:
model: astropy.modeling.models class instance

The compound model fit to the spectrum.

Attributes:
model: astropy.modeling.models class instance

The compound model fit to the spectrum.

filter: astropy.modeling.models class instance

It s the ratio of QPO component to the model fit to the spectrum.

class stingray.filters.Window1D(model)[source]

Make a top hat filter (window function) for power spectrum or cross spectrum. It assumes that the first model is the QPO component Lorentzian model.

Parameters:
model: astropy.modeling.models class instance

The compound model fit to the spectrum.

Attributes:
model: astropy.modeling.models class instance

The compound model fit to the spectrum.

x_o: Parameter class instance

Centroid of Lorentzian model.

fwhm: Parameter class instance

Full width at half maximum of Lorentzian model.

stingray.filters.filter_for_deadtime(event_list, deadtime, **kwargs)[source]

Filter an event list for a given dead time.

This function accepts either a list of times or a stingray.events.EventList object.

For the complete optional parameter list, see get_deadtime_mask()

Parameters:
ev_listarray-like or class:stingray.events.EventList

The event list

deadtime: float

The (mean, if not constant) value of deadtime

Returns:
new_ev_listclass:stingray.events.EventList object

The filtered event list

additional_outputdict

See get_deadtime_mask()

stingray.filters.get_deadtime_mask(ev_list, deadtime, bkg_ev_list=None, dt_sigma=None, paralyzable=False, return_all=False, verbose=False)[source]

Filter an event list for a given dead time.

Parameters:
ev_listarray-like

The event list

deadtime: float

The (mean, if not constant) value of deadtime

Returns:
maskarray-like, optional

The mask that filters the input event list and produces the output event list.

additional_outputobject

Object with all the following attributes. Only returned if return_all is True

uf_eventsarray-like

Unfiltered event list (events + background)

is_eventarray-like

Boolean values; True if event, False if background

deadtimearray-like

Dead time values

bkgarray-like

The filtered background event list

Other Parameters:
bkg_ev_listarray-like

A background event list that affects dead time

dt_sigmafloat

If specified, dead time will not have a single value but it will have a normal distribution with mean deadtime and standard deviation dt_sigma.

I/O Functionality

class stingray.io.EventReadOutput[source]

Container for what load_events_and_gtis() reads out of a FITS file.

It starts out empty: that function assigns the attributes it finds, such as the photon arrival times, the energies, and a few header keywords.

class stingray.io.FITSTimeseriesReader(fname, output_class=None, force_hduname=None, gti_file=None, gtistring=None, additional_columns=None, data_kind='events')[source]

Read a FITS table of event times without loading the data into memory.

Only the header and the GTIs are read when the object is created; the data stay on disk until they are asked for. A file too large to fit in memory can then be handed out in pieces – by number of samples, by time interval, or by GTI list – each piece either written to a file of its own or returned as an output_class object.

apply_gti_lists(new_gti_lists, root_file_name=None, fmt='pickle')[source]

Split the event list into different files, each with a different GTI.

Parameters:
new_gti_listslist of lists

A list of lists of GTIs. Each sublist should contain a list of GTIs for a new file.

Returns:
output_fileslist of str

A list of the output file names.

Other Parameters:
root_file_namestr, default None

The root name of the output files. The file name will be appended with “_00”, “_01”, etc. If None, a generator is returned instead of writing the files.

fmtstr

The format of the output files. Default is ‘hdf5’.

property exposure

Return the total exposure of the time series, i.e. the sum of the GTIs.

Returns:
total_exposurefloat

The total exposure of the time series, in seconds.

filter_at_time_intervals(time_intervals, root_file_name=None, fmt='pickle', check_gtis=True)[source]

Filter the event list at the given time intervals.

Parameters:
time_intervals2-d float array

List of time intervals of the form [[time0_0, time0_1], [time1_0, time1_1], ...]

Returns:
output_fileslist of str

A list of the output file names.

Other Parameters:
root_file_namestr, default None

The root name of the output files. The file name will be appended with “_00”, “_01”, etc. If None, a generator is returned instead of writing the files.

fmtstr

The format of the output files. Default is ‘hdf5’.

split_by_number_of_samples(nsamples, root_file_name=None, fmt='pickle')[source]

Split the event list into different files, each with approx. the given no. of photons.

Parameters:
nsamplesint

The number of photons in each output file.

Returns:
output_fileslist of str

A list of the output file names.

Other Parameters:
root_file_namestr, default None

The root name of the output files. The file name will be appended with “_00”, “_01”, etc. If None, a generator is returned instead of writing the files.

fmtstr

The format of the output files. Default is ‘hdf5’.

stingray.io.common_name(str1, str2, default='common')[source]

Strip two strings of the letters not in common.

Filenames must be of same length and only differ by a few letters.

Parameters:
str1str
str2str
Returns:
common_strstr

A string containing the parts of the two names in common

Other Parameters:
defaultstr

The string to return if common_str is empty

stingray.io.get_file_extension(fname)[source]

Get the extension from the file name.

If g-zipped, add ‘.gz’ to extension.

Examples

>>> get_file_extension('ciao.tar')
'.tar'
>>> get_file_extension('ciao.tar.gz')
'.tar.gz'
>>> get_file_extension('ciao.evt.gz')
'.evt.gz'
>>> get_file_extension('ciao.a.tutti.evt.gz')
'.evt.gz'
stingray.io.get_key_from_mission_info(info, key, default, inst=None, mode=None)[source]

Get the name of a header key or table column from the mission database.

Many entries in the mission database have default values that can be altered for specific instruments or observing modes. Here, if there is a definition for a given instrument and mode, we take that, otherwise we use the default).

Parameters:
infodict

Nested dictionary containing all the information for a given mission. It can be nested, e.g. contain some info for a given instrument, and for each observing mode of that instrument.

keystr

The key to read from the info dictionary

defaultobject

The default value. It can be of any type, depending on the expected type for the entry.

Returns:
retvalobject

The wanted entry from the info dictionary

Other Parameters:
inststr

Instrument

modestr

Observing mode

Examples

>>> info = {'ecol': 'PI', "A": {"ecol": "BLA"}, "C": {"M1": {"ecol": "X"}}}
>>> get_key_from_mission_info(info, "ecol", "BU", inst="A", mode=None)
'BLA'
>>> get_key_from_mission_info(info, "ecol", "BU", inst="B", mode=None)
'PI'
>>> get_key_from_mission_info(info, "ecol", "BU", inst="A", mode="M1")
'BLA'
>>> get_key_from_mission_info(info, "ecol", "BU", inst="C", mode="M1")
'X'
>>> get_key_from_mission_info(info, "ghghg", "BU", inst="C", mode="M1")
'BU'
stingray.io.high_precision_keyword_read(hdr, keyword)[source]

Read FITS header keywords, also if split in two.

In the case where the keyword is split in two, like

MJDREF = MJDREFI + MJDREFF

in some missions, this function returns the summed value. Otherwise, the content of the single keyword

Parameters:
hdrdict_like

The FITS header structure, or a dictionary

keywordstr

The key to read in the header

Returns:
valuelong double

The value of the key, or None if something went wrong

stingray.io.lcurve_from_fits(fits_file, gtistring='GTI', timecolumn='TIME', ratecolumn=None, ratehdu=1, fracexp_limit=0.9, outfile=None, noclobber=False, outdir=None)[source]

Load a lightcurve from a fits file.

Note

FITS light curve handling is still under testing. Absolute times might be incorrect depending on the light curve format.

Parameters:
fits_filestr

File name of the input light curve in FITS format

Returns:
datadict

Dictionary containing all information needed to create a stingray.Lightcurve object

Other Parameters:
gtistringstr

Name of the GTI extension in the FITS file

timecolumnstr

Name of the column containing times in the FITS file

ratecolumnstr

Name of the column containing rates in the FITS file

ratehdustr or int

Name or index of the FITS extension containing the light curve

fracexp_limitfloat

Minimum exposure fraction allowed

noclobberbool

If True, do not overwrite existing files

stingray.io.load_events_and_gtis(fits_file, additional_columns=None, gtistring=None, gti_file=None, hduname=None, column=None)[source]

Load event lists and GTIs from one or more files.

Loads event list from HDU EVENTS of file fits_file, with Good Time intervals. Optionally, returns additional columns of data from the same HDU of the events.

Parameters:
fits_filestr
Returns:
retvalsObject with the following attributes:
ev_listarray-like

Event times in Mission Epoch Time

gti_list: [[gti0_0, gti0_1], [gti1_0, gti1_1], …]

GTIs in Mission Epoch Time

additional_data: dict

A dictionary, where each key is the one specified in additional_colums. The data are an array with the values of the specified column in the fits file.

t_startfloat

Start time in Mission Epoch Time

t_stopfloat

Stop time in Mission Epoch Time

pi_listarray-like

Raw Instrument energy channels

cal_pi_listarray-like

Calibrated PI channels (those that can be easily converted to energy values, regardless of the instrument setup.)

energy_listarray-like

Energy of each photon in keV (only for NuSTAR, NICER, XMM)

instrstr

Name of the instrument (e.g. EPIC-pn or FPMA)

missionstr

Name of the instrument (e.g. XMM or NuSTAR)

mjdreffloat

MJD reference time for the mission

headerstr

Full header of the FITS file, for debugging purposes

detector_idarray-like, int

Detector id for each photon (e.g. each of the CCDs composing XMM’s or Chandra’s instruments)

Other Parameters:
additional_columns: list of str, optional

A list of keys corresponding to the additional columns to extract from the event HDU (ex.: [‘PI’, ‘X’])

gtistringstr

Comma-separated list of accepted GTI extensions (default GTI,STDGTI), with or without appended integer number denoting the detector

gti_filestr, default None

External GTI file

hdunamestr or int, default 1

Name of the HDU containing the event list

columnstr, default None

The column containing the time values. If None, we use the name specified in the mission database, and if there is nothing there, “TIME”

return_limits: bool, optional

Return the TSTART and TSTOP keyword values

stingray.io.mkdir_p(path)[source]

Safe mkdir function

Parameters:
pathstr

The absolute path to the directory to be created

stingray.io.pi_to_energy(pis, rmf_file)[source]

Read the energy channels corresponding to the given PI channels.

Parameters:
pisarray-like

The channels to lookup in the rmf

Other Parameters:
rmf_filestr

The rmf file used to read the calibration.

stingray.io.read_header_key(fits_file, key, hdu=1)[source]

Read the header key key from HDU hdu of the file fits_file.

Parameters:
fits_file: str

The file name and absolute path to the event file.

key: str

The keyword to be read

Returns:
valueobject

The value stored under key in fits_file

Other Parameters:
hduint

Index of the HDU extension from which the header key to be read.

stingray.io.read_rmf(rmf_file)[source]

Load RMF info.

Note

Preliminary: only EBOUNDS are read.

Parameters:
rmf_filestr

The rmf file used to read the calibration.

Returns:
pisarray-like

the PI channels

e_minsarray-like

the lower energy bound of each PI channel

e_maxsarray-like

the upper energy bound of each PI channel

stingray.io.ref_mjd(fits_file, hdu=1)[source]

Read MJDREFF, MJDREFI or, if failed, MJDREF, from the FITS header.

Parameters:
fits_filestr

The file name and absolute path to the event file.

Returns:
mjdrefnumpy.longdouble

the reference MJD

Other Parameters:
hduint

Index of the HDU extension from which the header key to be read.

stingray.io.run_flx2xsp(infile, outroot)[source]

Wrapper around the HEASOFT tool ftflx2xsp.

Converts a spectrum in text format to Xspec format.

Parameters:
infile: str

The name of the input file, containing the spectrum in text format. The file should have four columns: the lower energy of the bin, the higher energy of the bin, the power in the bin and the error on the power in the bin.

outroot: str

The root name of the output files. The file name will be appended with “.pha” and “.rsp” for the different files that will be created. The output files will be in Xspec format.

Raises:
RuntimeError

If the ftflx2xsp tool is not found in the system PATH.

stingray.io.save_as_xspec(x, dx, y, yerr, outroot, header_keywords=None)[source]

Save generic spectra in a format readable to FTOOLS and Xspec.

Parameters:
x: float array

The energies/frequencies of the spectrum

dx: float or float array

The width of the energy/frequency bins. If a scalar is given, it is interpreted as a constant bin width for all values in x.

y: float array

The flux/power of the spectrum in each bin

yerr: float array

The error on the flux/power in each bin

outroot: str

The root name of the output files. The file name will be appended with “.txt”, “.pha” and “.rsp” for the different files that will be created

header_keywords: dict, optional

A dictionary of header keywords to be added to the PHA file.

Raises:
RuntimeError

If the ftflx2xsp tool is not found in the system PATH (HEASOFT not installed or not initialized).

Notes

Uses method described by Ingram and Done in Appendix A of this paper

stingray.io.savefig(filename, **kwargs)[source]

Save a figure plotted by matplotlib.

Note : This function is supposed to be used after the plot function. Otherwise it will save a blank image with no plot.

Parameters:
filenamestr

The name of the image file. Extension must be specified in the file name. For example filename with .png extension will give a rasterized image while .pdf extension will give a vectorized output.

kwargskeyword arguments

Keyword arguments to be passed to savefig function of matplotlib.pyplot. For example use bbox_inches='tight' to remove the undesirable whitespace around the image.

stingray.io.split_numbers(number, shift=0)[source]

Split high precision number(s) into doubles.

You can specify the number of shifts to move the decimal point.

Parameters:
number: long double

The input high precision number which is to be split

Returns:
number_I: double

First part of high precision number

number_F: double

Second part of high precision number

Other Parameters:
shift: integer

Move the cut by shift decimal points to the right (left if negative)

Examples

>>> n = 12.34
>>> i, f = split_numbers(n)
>>> assert i == 12
>>> assert np.isclose(f, 0.34)
>>> assert np.allclose(split_numbers(n, 2), (12.34, 0.0))
>>> assert np.allclose(split_numbers(n, -1), (10.0, 2.34))

Mission-specific I/O

This module contains functions to interpret data from different missions.

The key functions are:

Whenever a given mission needs complicate processing, its functions can be made available for specific missions in their own separate modules. For example, the RXTE mission has its own module, rxte.py, which contains functions to interpret RXTE data.

stingray.mission_support.missions.get_rough_conversion_function(mission, instrument=None, epoch=None)[source]

Get a rough PI-Energy conversion function for a mission.

The function should accept a PI channel and return the corresponding energy. Additional keyword arguments (e.g. epoch, detector) can be passed to the function.

Parameters:
missionstr

Mission name

instrumentstr

Instrument onboard the mission

epochfloat

Epoch of the observation in MJD (important for missions updating their calibration).

Returns:
function

Conversion function

stingray.mission_support.missions.mission_specific_event_interpretation(mission)[source]

Get the mission-specific FITS interpretation function.

This function will read a file name or a FITS astropy.io.fits.HDUList object and modify it (see, e.g., rxte_pca_event_file_interpretation() for an example)

stingray.mission_support.missions.read_mission_info(mission=None)[source]

Search the relevant information about a mission in xselect.mdb.

stingray.mission_support.missions.rough_calibration(pis, mission)[source]

Make a rough conversion between PI channel and energy.

Only works for NICER, NuSTAR, IXPE, and XMM.

Parameters:
pis: float or array of floats

PI channels in data

mission: str

Mission name

Returns:
energiesfloat or array of floats

Energy values

Examples

>>> rough_calibration(0, 'nustar')
1.62
>>> rough_calibration(0.0, 'ixpe')
0.0
>>> # It's case-insensitive
>>> rough_calibration(1200, 'XMm')
1.2
>>> rough_calibration(10, 'asDf')
Traceback (most recent call last):
    ...
ValueError: Mission asdf not recognized
>>> rough_calibration(100, 'nicer')
1.0
stingray.mission_support.rxte.pca_calibration_func(epoch)[source]

Return the appropriate calibration function for RXTE for a given observing epoch.

This function has signature func(pha, detector_id) and gives the energy corresponding to the PHA channel for the given detector (array values allowed).

Internally, this is done by pre-allocating some arrays with the energy values for each PHA channel and detector group (1-4 and 0, due to a damage that PCU 0 incurred in 2000), and then returning a function that looks up the energy for each channel.

This does not require any interpolation, as the calibration is tabulated for each channel, and it is pretty efficient given the very small number of channels supported by the PCA (255).

Parameters:
epochfloat

The epoch of the observation in MJD.

Returns:
conversion_functioncallable

A function that converts PHA channel to energy. This function accepts two arguments: the PHA channel and the PCU number.

Examples

>>> conversion_function = pca_calibration_func(50082)
>>> float(conversion_function(10, 0))
3.04
>>> conversion_function = pca_calibration_func(55930)
>>> float(conversion_function(10, 0))
4.53
>>> float(conversion_function(10, 3))
4.49
>>> assert np.array_equal(conversion_function(10, [0, 3]), [4.53, 4.49])
>>> assert np.array_equal(conversion_function([10, 11], [0, 3]), [4.53, 4.90])
stingray.mission_support.rxte.rxte_calibration_func(instrument, epoch)[source]

Return the calibration function for RXTE at a given epoch.

Examples

>>> calibration_func = rxte_calibration_func("PCa", 50082)
>>> assert calibration_func(10) == pca_calibration_func(50082)(10)
>>> rxte_calibration_func("HEXTE", 55930)
Traceback (most recent call last):
...
ValueError: Unknown XTE instrument: HEXTE
stingray.mission_support.rxte.rxte_pca_event_file_interpretation(input_data, header=None, hduname=None)[source]

Interpret the FITS header of an RXTE event file.

At the moment, only science event files are supported. In these files, the energy channels are stored in a column named PHA. However, this is not the PHA column that can be directly used to convert to energy. These are channels that get changed on a per-observation basis, and can be converted to the “absolute” PHA channels (the ones tabulated in pca_calibration_func) by using the TEVTB2 keyword. This function changes the content of the PHA column by putting in the mean “absolute” PHA channel corresponding to each local PHA channel.

Parameters:
input_datastr, fits.HDUList, fits.HDU, np.array

The name of the FITS file to, or the HDUList inside, or the HDU with the data, or the data.

Other Parameters:
headerfits.Header, optional

Compulsory if hdulist is not a class:fits._BaseHDU, a HDUList, or a file name. The header of the relevant extension.

hdunamestr, optional

Name of the HDU (only relevant if hdulist is a HDUList), ignored otherwise.

Other Utility Functions

stingray.utils.baseline_als(x, y, lam=None, p=None, niter=10, return_baseline=False, offset_correction=False)[source]

Baseline Correction with Asymmetric Least Squares Smoothing.

Parameters:
xarray-like

the sample time/number/position

yarray-like

the data series corresponding to x

lamfloat

the lambda parameter of the ALS method. This control how much the baseline can adapt to local changes. A higher value corresponds to a stiffer baseline

pfloat

the asymmetry parameter of the ALS method. This controls the overall slope tolerated for the baseline. A higher value correspond to a higher possible slope

Returns:
y_subtractedarray-like, same size as y

The initial time series, subtracted from the trend

baselinearray-like, same size as y

Fitted baseline. Only returned if return_baseline is True

Other Parameters:
niterint

The number of iterations to perform

return_baselinebool

return the baseline?

offset_correctionbool

also correct for an offset to align with the running mean of the scan

Examples

>>> x = np.arange(0, 10, 0.01)
>>> y = np.zeros_like(x) + 10
>>> ysub = baseline_als(x, y)
>>> assert np.all(ysub < 0.001)
stingray.utils.check_isallfinite(array)[source]

Check if all elements of an array are finite.

Calls _check_isallfinite_numba if numba is installed, otherwise it uses np.isfinite.

Examples

>>> assert check_isallfinite([1, 2, 3])
>>> check_isallfinite([1, np.inf, 3])
False
>>> check_isallfinite([1, np.nan, 3])
False
stingray.utils.contiguous_regions(condition)[source]

Find contiguous True regions of the boolean array condition.

Return a 2D array where the first column is the start index of the region and the second column is the end index, found on [so-contiguous].

Parameters:
conditionbool array
Returns:
idx[[i0_0, i0_1], [i1_0, i1_1], ...]

A list of integer couples, with the start and end of each True blocks in the original array

Notes

stingray.utils.create_window(N, window_type='uniform')[source]

A method to create window functions commonly used in signal processing.

Windows supported are: Hamming, Hanning, uniform (rectangular window), triangular window, blackmann window among others.

Parameters:
Nint

Total number of data points in window. If negative, abs is taken.

window_type{uniform, parzen, hamming, hanning, triangular, welch, blackmann, flat-top}, optional, default uniform

Type of window to create.

Returns:
window: numpy.ndarray

Window function of length N.

stingray.utils.excess_variance(lc, normalization='fvar')[source]

Calculate the excess variance.

Vaughan et al. 2003, MNRAS 345, 1271 give three measurements of source intrinsic variance: if a light curve has a total variance of \(S^2\), and each point has an error bar \(\sigma_{err}\), the excess variance is defined as

\[\sigma_{XS} = S^2 - \overline{\sigma_{err}}^2;\]

the normalized excess variance is the excess variance divided by the square of the mean intensity:

\[\sigma_{NXS} = \dfrac{\sigma_{XS}}{\overline{x}^2};\]

the fractional mean square variability amplitude, or \(F_{var}\), is finally defined as

\[F_{var} = \sqrt{\dfrac{\sigma_{XS}}{\overline{x}^2}}\]
Parameters:
lca Lightcurve object
normalizationstr

if fvar, return the fractional mean square variability \(F_{var}\). If none, return the unnormalized excess variance variance \(\sigma_{XS}\). If norm_xs, return the normalized excess variance \(\sigma_{XS}\)

Returns
——-
var_xsfloat
var_xs_errfloat
stingray.utils.find_nearest(array, value, side='left')[source]

Return the array value that is closest to the input value (Abigail Stevens: Thanks StackOverflow!)

Parameters:
arraynp.array of ints or floats

1-D array of numbers to search through. Should already be sorted from low values to high values.

valueint or float

The value you want to find the closest to in the array.

Returns:
array[idx]int or float

The array value that is closest to the input value.

idxint

The index of the array of the closest value.

Other Parameters:
sidestr

Look at the numpy.searchsorted documentation for more information.

stingray.utils.get_random_state(random_state=None)[source]

Return a Mersenne Twister pseudo-random number generator.

Parameters:
seedinteger or numpy.random.RandomState, optional, default None
Returns:
random_statemtrand.RandomState object
stingray.utils.heaviside(x)[source]

Heaviside function. Returns 1 if x>0, and 0 otherwise.

Examples

>>> heaviside(2)
1
>>> heaviside(-1)
0
stingray.utils.is_int(obj)[source]

Test if object is an integer.

stingray.utils.is_iterable(var)[source]

Test if a variable is an iterable.

Parameters:
varobject

The variable to be tested for iterably-ness

Returns:
is_iterbool

Returns True if var is an Iterable, False otherwise

stingray.utils.is_string(s)[source]

Portable function to answer whether a variable is a string.

Parameters:
sobject

An object that is potentially a string

Returns:
isstringbool

A boolean decision on whether s is a string or not

stingray.utils.look_for_array_in_array(array1, array2)[source]

Find a subset of values in an array.

Parameters:
array1iterable

An array with values to be searched

array2iterable

A second array which potentially contains a subset of values also contained in array1

Returns ——- array3iterable An array with the subset of values
contained in both ``array1`` and ``array2``
stingray.utils.make_1d_arrays_into_nd(data: dict, label: str) → ndarray[source]

Literally the opposite of make_nd_into_arrays.

Call additional dimensions, e.g. _dimN_M

Parameters:
datadict

Input data

labelstr

Label for the array

Returns:
arraynp.array

N-dimensional array that was stored in the data.

Examples

>>> a1, a2, a3 = np.arange(3), np.arange(3, 6), np.arange(6, 9)
>>> A = np.array([a1, a2, a3]).T
>>> data = make_nd_into_arrays(A, "test")
>>> A_ret = make_1d_arrays_into_nd(data, "test")
>>> assert np.array_equal(A, A_ret)
>>> A = np.array([[[1, 2, 12], [3, 4, 34]],
...               [[5, 6, 56], [7, 8, 78]],
...               [[9, 10, 910], [11, 12, 1112]],
...               [[13, 14, 1314], [15, 16, 1516]]])
>>> data = make_nd_into_arrays(A, "_test")
>>> A_ret = make_1d_arrays_into_nd(data, "_test")
>>> assert np.array_equal(A, A_ret)
>>> data = make_nd_into_arrays(a1, "_test")
>>> A_ret = make_1d_arrays_into_nd(data, "_test")
>>> assert np.array_equal(a1, A_ret)
stingray.utils.make_dictionary_lowercase(dictionary, recursive=False)[source]

Make all keys of a dictionary lowercase.

Optionally, if some values are dictionaries, they can be made lowercase too.

Parameters:
dictionarydict

The dictionary to be made lowercase

Other Parameters:
recursivebool

If True, make all keys of nested dictionaries lowercase too.

Examples

>>> d1 = {"A": 1, "B": 2, "C": {"D": 3, "E": {"F": 4}}}
>>> d2 = make_dictionary_lowercase(d1)
>>> assert d2 == {"a": 1, "b": 2, "c": {"D": 3, "E": {"F": 4}}}
>>> d3 = make_dictionary_lowercase(d1, recursive=True)
>>> assert d3 == {"a": 1, "b": 2, "c": {"d": 3, "e": {"f": 4}}}
stingray.utils.make_nd_into_arrays(array: ndarray, label: str) → dict[source]

If an array is n-dimensional, make it into many 1-dimensional arrays.

Call additional dimensions, e.g. _dimN_M. See examples below.

Parameters:
arraynp.ndarray

Input data

labelstr

Label for the array

Returns:
datadict

Dictionary of arrays. Defaults to {label: array} if array is 1-dimensional, otherwise, e.g.: {label_dim1_2_3: array[1, 2, 3], ... }

Examples

>>> a1, a2, a3 = np.arange(3), np.arange(3, 6), np.arange(6, 9)
>>> A = np.array([a1, a2, a3]).T
>>> data = make_nd_into_arrays(A, "test")
>>> assert np.array_equal(data["test_dim0"], a1)
>>> assert np.array_equal(data["test_dim1"], a2)
>>> assert np.array_equal(data["test_dim2"], a3)
>>> A3 = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
>>> data = make_nd_into_arrays(A3, "test")
>>> assert np.array_equal(data["test_dim0_0"], [1, 5])
stingray.utils.nearest_power_of_two(x)[source]

Return a number which is nearest to x and is the integral power of two.

Parameters:
xint, float
Returns:
x_nearestint

Number closest to x and is the integral power of two.

stingray.utils.optimal_bin_time(fftlen, tbin)[source]

Vary slightly the bin time to have a power of two number of bins.

Given an FFT length and a proposed bin time, return a bin time slightly shorter than the original, that will produce a power-of-two number of FFT bins.

Parameters:
fftlenint

Number of positive frequencies in a proposed Fourier spectrum

tbinfloat

The proposed time resolution of a light curve

Returns:
resfloat

A time resolution that will produce a Fourier spectrum with fftlen frequencies and a number of FFT bins that are a power of two

stingray.utils.order_list_of_arrays(data, order)[source]

Sort an array according to the specified order.

Parameters:
dataiterable
Returns:
datalist or dict
stingray.utils.poisson_symmetrical_errors(counts)[source]

Optimized version of frequentist symmetrical errors.

Uses a lookup table in order to limit the calls to poisson_conf_interval

Parameters:
countsiterable

An array of Poisson-distributed numbers

Returns:
errnumpy.ndarray

An array of uncertainties associated with the Poisson counts in counts

Examples

>>> from astropy.stats import poisson_conf_interval
>>> counts = np.random.randint(0, 1000, 100)
>>> # ---- Do it without the lookup table ----
>>> err_low, err_high = poisson_conf_interval(np.asanyarray(counts),
...                 interval='frequentist-confidence', sigma=1)
>>> err_low -= np.asanyarray(counts)
>>> err_high -= np.asanyarray(counts)
>>> err = (np.absolute(err_low) + np.absolute(err_high))/2.0
>>> # Do it with this function
>>> err_thisfun = poisson_symmetrical_errors(counts)
>>> # Test that results are always the same
>>> assert np.allclose(err_thisfun, err)
stingray.utils.rebin_data(x, y, dx_new, yerr=None, method='sum', dx=None)[source]

Rebin some data to an arbitrary new data resolution. Either sum the data points in the new bins or average them.

Parameters:
x: iterable

The dependent variable with some resolution, which can vary throughout the time series.

y: iterable

The independent variable to be binned

dx_new: float

The new resolution of the dependent variable x

Returns:
xbin: numpy.ndarray

The midpoints of the new bins in x

ybin: float numpy.ndarray

The binned quantity y

ybin_err: float numpy.ndarray

The uncertainties of the binned values of y.

step_size: float

The size of the binning step

Other Parameters:
yerr: iterable, optional

The uncertainties of y, to be propagated during binning.

method: {``sum`` | ``average`` | ``mean``}, optional, default ``sum``

The method to be used in binning. Either sum the samples y in each new bin of x, or take the arithmetic mean.

dx: float

The old resolution (otherwise, calculated from difference between time bins)

Examples

>>> x = np.arange(0, 100, 0.01)
>>> y = np.ones(x.size)
>>> yerr = np.ones(x.size) * 2  # Each point has σ=2
>>> xbin, ybin, ybinerr, step_size = rebin_data(
...     x, y, 4, yerr=yerr, method='sum', dx=0.01)
>>> assert np.allclose(ybin, 400)
>>> assert np.allclose(ybinerr, 40)  # sqrt(400 * 2**2) = 40
>>> xbin, ybin, ybinerr, step_size = rebin_data(
...     x, y, 4, yerr=yerr, method='mean')
>>> assert np.allclose(ybin, 1)
>>> assert np.allclose(ybinerr, 0.1)  # sqrt(400 * 2**2) / 400 = 0.1
stingray.utils.rebin_data_log(x, y, f, y_err=None, dx=None)[source]

Logarithmic re-bin of some data. Particularly useful for the power spectrum.

The new dependent variable depends on the previous dependent variable modified by a factor f:

\[d\nu_j = d\nu_{j-1} (1+f)\]
Parameters:
x: iterable

The dependent variable with some resolution dx_old = x[1]-x[0]

y: iterable

The independent variable to be binned

f: float

The factor of increase of each bin wrt the previous one.

Returns:
xbin: numpy.ndarray

The midpoints of the new bins in x

ybin: numpy.ndarray

The binned quantity y

ybin_err: numpy.ndarray

The uncertainties of the binned values of y

step_size: float

The size of the binning step

Other Parameters:
yerr: iterable, optional

The uncertainties of y to be propagated during binning.

method: {``sum`` | ``average`` | ``mean``}, optional, default ``sum``

The method to be used in binning. Either sum the samples y in each new bin of x or take the arithmetic mean.

dx: float, optional

The binning step of the initial x

stingray.utils.simon(message, **kwargs)[source]

The Statistical Interpretation MONitor.

A warning system designed to always remind the user that Simon is watching him/her.

Parameters:
messagestring

The message that is thrown

kwargsdict

The rest of the arguments that are passed to warnings.warn. category defaults to stingray.exceptions.StingrayWarning.

stingray.utils.standard_error(xs, mean)[source]

Return the standard error of the mean (SEM) of an array of arrays.

Parameters:
xs2-d float array

List of data point arrays.

mean1-d float array

Average of the data points.

Returns:
standard_error1-d float array

Standard error of the mean (SEM).

Modeling

This subpackage defines classes and functions related to parametric modelling of various types of data sets. Currently, most functionality is focused on modelling Fourier products (especially power spectra and averaged power spectra), but rudimentary functionality exists for modelling e.g. light curves.

Log-Likelihood Classes

These classes define basic log-likelihoods for modelling time series and power spectra. stingray.modeling.LogLikelihood is an abstract base class, i.e. a template for creating user-defined log-likelihoods and should not be instantiated itself. Based on this base class are several definitions for a stingray.modeling.GaussianLogLikelihood, appropriate for data with normally distributed uncertainties, a stingray.modeling.PoissonLogLikelihood appropriate for photon counting data, and a stingray.modeling.PSDLogLikelihood appropriate for (averaged) power spectra.

class stingray.modeling.LogLikelihood(x, y, model, **kwargs)[source]

Abstract Base Class defining the structure of a LogLikelihood object. This class cannot be called itself, since each statistical distribution has its own definition for the likelihood, which should occur in subclasses.

Parameters:
xiterable

x-coordinate of the data. Could be multi-dimensional.

yiterable

y-coordinate of the data. Could be multi-dimensional.

modelan astropy.modeling.FittableModel instance

Your model

kwargs

keyword arguments specific to the individual sub-classes. For details, see the respective docstrings for each subclass

abstractmethod evaluate(parameters)[source]

This is where you define your log-likelihood. Do this, but do it in a subclass!

class stingray.modeling.GaussianLogLikelihood(x, y, yerr, model)[source]

Likelihood for data with Gaussian uncertainties. Astronomers also call this likelihood Chi-Squared, but be aware that this has nothing to do with the likelihood based on the Chi-square distribution, which is also defined as in of PSDLogLikelihood in this module!

Use this class here whenever your data has Gaussian uncertainties.

Parameters:
xiterable

x-coordinate of the data

yiterable

y-coordinte of the data

yerriterable

the uncertainty on the data, as standard deviation

modelan astropy.modeling.FittableModel instance

The model to use in the likelihood.

Attributes:
xiterable

x-coordinate of the data

yiterable

y-coordinte of the data

yerriterable

the uncertainty on the data, as standard deviation

modelan Astropy Model instance

The model to use in the likelihood.

nparint

The number of free parameters in the model

evaluate(pars, neg=False)[source]

Evaluate the Gaussian log-likelihood for a given set of parameters.

Parameters:
parsnumpy.ndarray

An array of parameters at which to evaluate the model and subsequently the log-likelihood. Note that the length of this array must match the free parameters in model, i.e. npar

negbool, optional, default False

If True, return the negative log-likelihood, i.e. -loglike, rather than loglike. This is useful e.g. for optimization routines, which generally minimize functions.

Returns:
loglikefloat

The log(likelihood) value for the data and model.

class stingray.modeling.PoissonLogLikelihood(x, y, model)[source]

Likelihood for data with uncertainties following a Poisson distribution. This is useful e.g. for (binned) photon count data.

Parameters:
xiterable

x-coordinate of the data

yiterable

y-coordinte of the data

modelan astropy.modeling.FittableModel instance

The model to use in the likelihood.

Attributes:
xiterable

x-coordinate of the data

yiterable

y-coordinte of the data

yerriterable

the uncertainty on the data, as standard deviation

modelan astropy.modeling.FittableModel instance

The model to use in the likelihood.

nparint

The number of free parameters in the model

evaluate(pars, neg=False)[source]

Evaluate the log-likelihood for a given set of parameters.

Parameters:
parsnumpy.ndarray

An array of parameters at which to evaluate the model and subsequently the log-likelihood. Note that the length of this array must match the free parameters in model, i.e. npar

negbool, optional, default False

If True, return the negative log-likelihood, i.e. -loglike, rather than loglike. This is useful e.g. for optimization routines, which generally minimize functions.

Returns:
loglikefloat

The log(likelihood) value for the data and model.

class stingray.modeling.PSDLogLikelihood(freq, power, model, m=1)[source]

A likelihood based on the Chi-square distribution, appropriate for modelling (averaged) power spectra. Note that this is not the same as the statistic astronomers commonly call Chi-Square, which is a fit statistic derived from the Gaussian log-likelihood, defined elsewhere in this module.

Parameters:
freqiterable

Array with frequencies

poweriterable

Array with (averaged/singular) powers corresponding to the frequencies in freq

modelan astropy.modeling.FittableModel instance

The model to use in the likelihood.

mint

1/2 of the degrees of freedom

Attributes:
xiterable

x-coordinate of the data

yiterable

y-coordinte of the data

yerriterable

the uncertainty on the data, as standard deviation

modelan astropy.modeling.FittableModel instance

The model to use in the likelihood.

nparint

The number of free parameters in the model

evaluate(pars, neg=False)[source]

Evaluate the log-likelihood for a given set of parameters.

Parameters:
parsnumpy.ndarray

An array of parameters at which to evaluate the model and subsequently the log-likelihood. Note that the length of this array must match the free parameters in model, i.e. npar

negbool, optional, default False

If True, return the negative log-likelihood, i.e. -loglike, rather than loglike. This is useful e.g. for optimization routines, which generally minimize functions.

Returns:
loglikefloat

The log(likelihood) value for the data and model.

class stingray.modeling.LaplaceLogLikelihood(x, y, yerr, model)[source]

A Laplace likelihood for the cospectrum.

Parameters:
xiterable

Array with independent variable

yiterable

Array with dependent variable

modelan astropy.modeling.FittableModel instance

The model to use in the likelihood.

yerriterable

Array with the uncertainties on y, in standard deviation

Attributes:
xiterable

x-coordinate of the data

yiterable

y-coordinte of the data

yerriterable

the uncertainty on the data, as standard deviation

modelan astropy.modeling.FittableModel instance

The model to use in the likelihood.

nparint

The number of free parameters in the model

evaluate(pars, neg=False)[source]

Evaluate the log-likelihood for a given set of parameters.

Parameters:
parsnumpy.ndarray

An array of parameters at which to evaluate the model and subsequently the log-likelihood. Note that the length of this array must match the free parameters in model, i.e. npar

negbool, optional, default False

If True, return the negative log-likelihood, i.e. -loglike, rather than loglike. This is useful e.g. for optimization routines, which generally minimize functions.

Returns:
loglikefloat

The log(likelihood) value for the data and model.


Posterior Classes

These classes define basic posteriors for parametric modelling of time series and power spectra, based on the log-likelihood classes defined in Log-Likelihood Classes. stingray.modeling.Posterior is an abstract base class laying out a basic template for defining posteriors. As with the log-likelihood classes above, several posterior classes are defined for a variety of data types.

Note that priors are not pre-defined in these classes, since they are problem dependent and should be set by the user. The convenience function stingray.modeling.set_logprior() can be useful to help set priors for these posterior classes.

class stingray.modeling.Posterior(x, y, model, **kwargs)[source]

Define a Posterior object.

The Posterior describes the Bayesian probability distribution of a set of parameters \(\theta\) given some observed data \(D\) and some prior assumptions \(I\).

It is defined as

\[p(\theta | D, I) = p(D | \theta, I) p(\theta | I)/p(D| I)\]

where \(p(D | \theta, I)\) describes the likelihood, i.e. the sampling distribution of the data and the (parametric) model, and \(p(\theta | I)\) describes the prior distribution, i.e. our information about the parameters \(\theta\) before we gathered the data. The marginal likelihood \(p(D| I)\) describes the probability of observing the data given the model assumptions, integrated over the space of all parameters.

Parameters:
xiterable

The abscissa or independent variable of the data. This could in principle be a multi-dimensional array.

yiterable

The ordinate or dependent variable of the data.

modelastropy.modeling.models instance

The parametric model supposed to represent the data. For details see the astropy.modeling documentation

kwargs

keyword arguments related to the subclasses of Posterior. For details, see the documentation of the individual subclasses

References

  • Sivia, D. S., and J. Skilling. “Data Analysis: A Bayesian Tutorial. 2006.”

  • Gelman, Andrew, et al. Bayesian data analysis. Vol. 2. Boca Raton, FL, USA: Chapman & Hall/CRC, 2014.

  • von Toussaint, Udo. “Bayesian inference in physics.” Reviews of Modern Physics 83.3 (2011): 943.

  • Hogg, David W. “Probability Calculus for inference”. arxiv: 1205.4446

logposterior(t0, neg=False)[source]

Definition of the log-posterior. Requires methods loglikelihood and logprior to both be defined.

Note that loglikelihood is set in the subclass of Posterior appropriate for your problem at hand, as is logprior.

Parameters:
t0numpy.ndarray

An array of parameters at which to evaluate the model and subsequently the log-posterior. Note that the length of this array must match the free parameters in model, i.e. npar

negbool, optional, default False

If True, return the negative log-posterior, i.e. -lpost, rather than lpost. This is useful e.g. for optimization routines, which generally minimize functions.

Returns:
lpostfloat

The value of the log-posterior for the given parameters t0

class stingray.modeling.GaussianPosterior(x, y, yerr, model, priors=None)[source]

A general class for two-dimensional data following a Gaussian sampling distribution.

Parameters:
xnumpy.ndarray

independent variable

ynumpy.ndarray

dependent variable

yerrnumpy.ndarray

measurement uncertainties for y

modelinstance of any subclass of astropy.modeling.FittableModel

The model for the power spectrum.

priorsdict of form {"parameter name": function}, optional

A dictionary with the definitions for the prior probabilities. For each parameter in model, there must be a prior defined with a key of the exact same name as stored in model.param_names. The item for each key is a function definition defining the prior (e.g. a lambda function or a scipy.stats.distribution.pdf. If priors = None, then no prior is set. This means priors need to be added by hand using the set_logprior() function defined in this module. Note that it is impossible to call a Posterior object itself or the self.logposterior method without defining a prior.

logposterior(t0, neg=False)

Definition of the log-posterior. Requires methods loglikelihood and logprior to both be defined.

Note that loglikelihood is set in the subclass of Posterior appropriate for your problem at hand, as is logprior.

Parameters:
t0numpy.ndarray

An array of parameters at which to evaluate the model and subsequently the log-posterior. Note that the length of this array must match the free parameters in model, i.e. npar

negbool, optional, default False

If True, return the negative log-posterior, i.e. -lpost, rather than lpost. This is useful e.g. for optimization routines, which generally minimize functions.

Returns:
lpostfloat

The value of the log-posterior for the given parameters t0

class stingray.modeling.PoissonPosterior(x, y, model, priors=None)[source]

Posterior for Poisson light curve data. Primary intended use is for modelling X-ray light curves, but alternative uses are conceivable.

Parameters:
xnumpy.ndarray

The independent variable (e.g. time stamps of a light curve)

ynumpy.ndarray

The dependent variable (e.g. counts per bin of a light curve)

modelinstance of any subclass of astropy.modeling.FittableModel

The model for the power spectrum.

priorsdict of form {"parameter name": function}, optional

A dictionary with the definitions for the prior probabilities. For each parameter in model, there must be a prior defined with a key of the exact same name as stored in model.param_names. The item for each key is a function definition defining the prior (e.g. a lambda function or a scipy.stats.distribution.pdf. If priors = None, then no prior is set. This means priors need to be added by hand using the set_logprior() function defined in this module. Note that it is impossible to call a Posterior object itself or the self.logposterior method without defining a prior.

Attributes:
xnumpy.ndarray

The independent variable (list of frequencies) stored in ps.freq

ynumpy.ndarray

The dependent variable (list of powers) stored in ps.power

modelinstance of any subclass of astropy.modeling.FittableModel

The model for the power spectrum.

logposterior(t0, neg=False)

Definition of the log-posterior. Requires methods loglikelihood and logprior to both be defined.

Note that loglikelihood is set in the subclass of Posterior appropriate for your problem at hand, as is logprior.

Parameters:
t0numpy.ndarray

An array of parameters at which to evaluate the model and subsequently the log-posterior. Note that the length of this array must match the free parameters in model, i.e. npar

negbool, optional, default False

If True, return the negative log-posterior, i.e. -lpost, rather than lpost. This is useful e.g. for optimization routines, which generally minimize functions.

Returns:
lpostfloat

The value of the log-posterior for the given parameters t0

class stingray.modeling.PSDPosterior(freq, power, model, priors=None, m=1)[source]

Posterior distribution for power spectra. Uses an exponential distribution for the errors in the likelihood, or a \(\chi^2\) distribution with \(2M\) degrees of freedom, where \(M\) is the number of frequency bins or power spectra averaged in each bin.

Parameters:
ps{stingray.Powerspectrum | stingray.AveragedPowerspectrum} instance

the stingray.Powerspectrum object containing the data

modelinstance of any subclass of astropy.modeling.FittableModel

The model for the power spectrum.

priorsdict of form {"parameter name": function}, optional

A dictionary with the definitions for the prior probabilities. For each parameter in model, there must be a prior defined with a key of the exact same name as stored in model.param_names. The item for each key is a function definition defining the prior (e.g. a lambda function or a scipy.stats.distribution.pdf. If priors = None, then no prior is set. This means priors need to be added by hand using the set_logprior() function defined in this module. Note that it is impossible to call a Posterior object itself or the self.logposterior method without defining a prior.

mint, default 1

The number of averaged periodograms or frequency bins in ps. Useful for binned/averaged periodograms, since the value of m will change the likelihood function!

Attributes:
ps{stingray.Powerspectrum | stingray.AveragedPowerspectrum} instance

the stingray.Powerspectrum object containing the data

xnumpy.ndarray

The independent variable (list of frequencies) stored in ps.freq

ynumpy.ndarray

The dependent variable (list of powers) stored in ps.power

modelinstance of any subclass of astropy.modeling.FittableModel

The model for the power spectrum.

logposterior(t0, neg=False)

Definition of the log-posterior. Requires methods loglikelihood and logprior to both be defined.

Note that loglikelihood is set in the subclass of Posterior appropriate for your problem at hand, as is logprior.

Parameters:
t0numpy.ndarray

An array of parameters at which to evaluate the model and subsequently the log-posterior. Note that the length of this array must match the free parameters in model, i.e. npar

negbool, optional, default False

If True, return the negative log-posterior, i.e. -lpost, rather than lpost. This is useful e.g. for optimization routines, which generally minimize functions.

Returns:
lpostfloat

The value of the log-posterior for the given parameters t0

class stingray.modeling.LaplacePosterior(x, y, yerr, model, priors=None)[source]

A general class for two-dimensional data following a Gaussian sampling distribution.

Parameters:
xnumpy.ndarray

independent variable

ynumpy.ndarray

dependent variable

yerrnumpy.ndarray

measurement uncertainties for y, in standard deviation

modelinstance of any subclass of astropy.modeling.FittableModel

The model for the power spectrum.

priorsdict of form {"parameter name": function}, optional

A dictionary with the definitions for the prior probabilities. For each parameter in model, there must be a prior defined with a key of the exact same name as stored in model.param_names. The item for each key is a function definition defining the prior (e.g. a lambda function or a scipy.stats.distribution.pdf. If priors = None, then no prior is set. This means priors need to be added by hand using the set_logprior() function defined in this module. Note that it is impossible to call a Posterior object itself or the self.logposterior method without defining a prior.

logposterior(t0, neg=False)

Definition of the log-posterior. Requires methods loglikelihood and logprior to both be defined.

Note that loglikelihood is set in the subclass of Posterior appropriate for your problem at hand, as is logprior.

Parameters:
t0numpy.ndarray

An array of parameters at which to evaluate the model and subsequently the log-posterior. Note that the length of this array must match the free parameters in model, i.e. npar

negbool, optional, default False

If True, return the negative log-posterior, i.e. -lpost, rather than lpost. This is useful e.g. for optimization routines, which generally minimize functions.

Returns:
lpostfloat

The value of the log-posterior for the given parameters t0


Parameter Estimation Classes

These classes implement functionality related to parameter estimation. They define basic fit and sample methods using scipy.optimize and emcee, respectively, for optimization and Markov Chain Monte Carlo sampling. stingray.modeling.PSDParEst implements some more advanced functionality for modelling power spectra, including both frequentist and Bayesian searches for (quasi-)periodic signals.

class stingray.modeling.ParameterEstimation(fitmethod='BFGS', max_post=True)[source]

Parameter estimation of two-dimensional data, either via optimization or MCMC. Note: optimization with bounds is not supported. If something like this is required, define (uniform) priors in the ParametricModel instances to be used below.

Parameters:
fitmethodstring, optional, default L-BFGS-B

Any of the strings allowed in scipy.optimize.minimize in the method keyword. Sets the fit method to be used.

max_postbool, optional, default True

If True, then compute the Maximum-A-Posteriori estimate. If False, compute a Maximum Likelihood estimate.

calibrate_lrt(lpost1, t1, lpost2, t2, sample=None, neg=True, max_post=False, nsim=1000, niter=200, nwalkers=500, burnin=200, namestr='test', seed=None)[source]

Calibrate the outcome of a Likelihood Ratio Test via MCMC.

In order to compare models via likelihood ratio test, one generally aims to compute a p-value for the null hypothesis (generally the simpler model). There are two special cases where the theoretical distribution used to compute that p-value analytically given the observed likelihood ratio (a chi-square distribution) is not applicable:

  • the models are not nested (i.e. Model 1 is not a special, simpler case of Model 2),

  • the parameter values fixed in Model 2 to retrieve Model 1 are at the edges of parameter space (e.g. if one must set, say, an amplitude to zero in order to remove a component in the more complex model, and negative amplitudes are excluded a priori)

In these cases, the observed likelihood ratio must be calibrated via simulations of the simpler model (Model 1), using MCMC to take into account the uncertainty in the parameters. This function does exactly that: it computes the likelihood ratio for the observed data, and produces simulations to calibrate the likelihood ratio and compute a p-value for observing the data under the assumption that Model 1 istrue.

If max_post=True, the code will use MCMC to sample the posterior of the parameters and simulate fake data from there.

If max_post=False, the code will use the covariance matrix derived from the fit to simulate data sets for comparison.

Parameters:
lpost1object of a subclass of Posterior

The Posterior object for model 1

t1iterable

The starting parameters for model 1

lpost2object of a subclass of Posterior

The Posterior object for model 2

t2iterable

The starting parameters for model 2

negbool, optional, default True

Boolean flag to decide whether to use the negative log-likelihood or log-posterior

max_post: bool, optional, default ``False``

If True, set the internal state to do the optimization with the log-likelihood rather than the log-posterior.

Returns:
pvaluefloat [0,1]

p-value ‘n stuff

compute_lrt(lpost1, t1, lpost2, t2, neg=True, max_post=False)[source]

This function computes the Likelihood Ratio Test between two nested models.

Parameters:
lpost1object of a subclass of Posterior

The Posterior object for model 1

t1iterable

The starting parameters for model 1

lpost2object of a subclass of Posterior

The Posterior object for model 2

t2iterable

The starting parameters for model 2

negbool, optional, default True

Boolean flag to decide whether to use the negative log-likelihood or log-posterior

max_post: bool, optional, default ``False``

If True, set the internal state to do the optimization with the log-likelihood rather than the log-posterior.

Returns:
lrtfloat

The likelihood ratio for model 2 and model 1

res1OptimizationResults object

Contains the result of fitting lpost1

res2OptimizationResults object

Contains the results of fitting lpost2

fit(lpost, t0, neg=True, scipy_optimize_options=None)[source]

Do either a Maximum-A-Posteriori (MAP) or Maximum Likelihood (ML) fit to the data.

MAP fits include priors, ML fits do not.

Parameters:
lpostPosterior (or subclass) instance

and instance of class Posterior or one of its subclasses that defines the function to be minimized (either in loglikelihood or logposterior)

t0{list | numpy.ndarray}

List/array with set of initial parameters

negbool, optional, default True

Boolean to be passed to lpost, setting whether to use the negative posterior or the negative log-likelihood. Useful for optimization routines, which are generally defined as minimization routines.

scipy_optimize_optionsdict, optional, default None

A dictionary with options for scipy.optimize.minimize, directly passed on as keyword arguments.

Returns:
resOptimizationResults object

An object containing useful summaries of the fitting procedure. For details, see documentation of class:OptimizationResults.

sample(lpost, t0, cov=None, nwalkers=500, niter=100, burnin=100, threads=1, print_results=True, plot=False, namestr='test', pool=False)[source]

Sample the Posterior distribution defined in lpost using MCMC. Here we use the emcee package, but other implementations could in principle be used.

Parameters:
lpostinstance of a Posterior subclass

and instance of class Posterior or one of its subclasses that defines the function to be minimized (either in loglikelihood or logposterior)

t0iterable

list or array containing the starting parameters. Its length must match lpost.model.npar.

nwalkersint, optional, default 500

The number of walkers (chains) to use during the MCMC procedure. The more walkers are used, the slower the estimation will be, but the better the final distribution is likely to be.

niterint, optional, default 100

The number of iterations to run the MCMC chains for. The larger this number, the longer the estimation will take, but the higher the chance that the walkers have actually converged on the true posterior distribution.

burninint, optional, default 100

The number of iterations to run the walkers before convergence is assumed to have occurred. This part of the chain will be discarded before sampling from what is then assumed to be the posterior distribution desired.

threadsDEPRECATED int, optional, default 1

The number of threads for parallelization. Default is 1, i.e. no parallelization With the change to the new emcee version 3, threads is deprecated. Use the pool keyword argument instead. This will no longer have any effect.

print_resultsbool, optional, default True

Boolean flag setting whether the results of the MCMC run should be printed to standard output. Default: True

plotbool, optional, default False

Boolean flag setting whether summary plots of the MCMC chains should be produced. Default: False

namestrstr, optional, default test

Optional string for output file names for the plotting.

poolbool, default False

If True, use pooling to parallelize the operation.

Returns:
resclass:SamplingResults object

An object of class SamplingResults summarizing the results of the MCMC run.

simulate_lrts(s_all, lpost1, t1, lpost2, t2, max_post=True, seed=None)[source]

Simulate likelihood ratios. For details, see definitions in the subclasses that implement this task.

class stingray.modeling.PSDParEst(ps, fitmethod='BFGS', max_post=True)[source]

Parameter estimation for parametric modelling of power spectra.

This class contains functionality that allows parameter estimation and related tasks that involve fitting a parametric model to an (averaged) power spectrum.

Parameters:
psclass:stingray.Powerspectrum or class:stingray.AveragedPowerspectrum object

The power spectrum to be modelled

fitmethodstr, optional, default BFGS

A string allowed by scipy.optimize.minimize as a valid fitting method

max_postbool, optional, default True

If True, do a Maximum-A-Posteriori (MAP) fit, i.e. fit with priors, otherwise do a Maximum Likelihood fit instead

calibrate_highest_outlier(lpost, t0, sample=None, max_post=False, nsim=1000, niter=200, nwalkers=500, burnin=200, namestr='test', seed=None)[source]

Calibrate the highest outlier in a data set using MCMC-simulated power spectra.

In short, the procedure does a MAP fit to the data, computes the statistic

\[\max{(T_R = 2(\mathrm{data}/\mathrm{model}))}\]

and then does an MCMC run using the data and the model, or generates parameter samples from the likelihood distribution using the derived covariance in a Maximum Likelihood fit. From the (posterior) samples, it generates fake power spectra. Each fake spectrum is fit in the same way as the data, and the highest data/model outlier extracted as for the data. The observed value of \(T_R\) can then be directly compared to the simulated distribution of \(T_R\) values in order to derive a p-value of the null hypothesis that the observed \(T_R\) is compatible with being generated by noise.

Parameters:
lpoststingray.modeling.PSDPosterior object

An instance of class stingray.modeling.PSDPosterior that defines the function to be minimized (either in loglikelihood or logposterior)

t0{list | numpy.ndarray}

List/array with set of initial parameters

sampleSamplingResults instance, optional, default None

If a sampler has already been run, the SamplingResults instance can be fed into this method here, otherwise this method will run a sampler automatically

max_post: bool, optional, default ``False``

If True, do MAP fits on the power spectrum to find the highest data/model outlier Otherwise, do a Maximum Likelihood fit. If True, the simulated power spectra will be generated from an MCMC run, otherwise the method will employ the approximated covariance matrix for the parameters derived from the likelihood surface to generate samples from that likelihood function.

nsimint, optional, default 1000

Number of fake power spectra to simulate from the posterior sample. Note that this number sets the resolution of the resulting p-value. For nsim=1000, the highest resolution that can be achieved is \(10^{-3}\).

niterint, optional, default 200

If sample is None, this variable will be used to set the number of steps in the MCMC procedure after burn-in.

nwalkersint, optional, default 500

If sample is None, this variable will be used to set the number of MCMC chains run in parallel in the sampler.

burninint, optional, default 200

If sample is None, this variable will be used to set the number of burn-in steps to be discarded in the initial phase of the MCMC run

namestrstr, optional, default test

A string to be used for storing MCMC output and plots to disk

seedint, optional, default None

An optional number to seed the random number generator with, for reproducibility of the results obtained with this method.

Returns:
pvalfloat

The p-value that the highest data/model outlier is produced by random noise, calibrated using simulated power spectra from an MCMC run.

References

For more details on the procedure employed here, see

calibrate_lrt(lpost1, t1, lpost2, t2, sample=None, neg=True, max_post=False, nsim=1000, niter=200, nwalkers=500, burnin=200, namestr='test', seed=None)

Calibrate the outcome of a Likelihood Ratio Test via MCMC.

In order to compare models via likelihood ratio test, one generally aims to compute a p-value for the null hypothesis (generally the simpler model). There are two special cases where the theoretical distribution used to compute that p-value analytically given the observed likelihood ratio (a chi-square distribution) is not applicable:

  • the models are not nested (i.e. Model 1 is not a special, simpler case of Model 2),

  • the parameter values fixed in Model 2 to retrieve Model 1 are at the edges of parameter space (e.g. if one must set, say, an amplitude to zero in order to remove a component in the more complex model, and negative amplitudes are excluded a priori)

In these cases, the observed likelihood ratio must be calibrated via simulations of the simpler model (Model 1), using MCMC to take into account the uncertainty in the parameters. This function does exactly that: it computes the likelihood ratio for the observed data, and produces simulations to calibrate the likelihood ratio and compute a p-value for observing the data under the assumption that Model 1 istrue.

If max_post=True, the code will use MCMC to sample the posterior of the parameters and simulate fake data from there.

If max_post=False, the code will use the covariance matrix derived from the fit to simulate data sets for comparison.

Parameters:
lpost1object of a subclass of Posterior

The Posterior object for model 1

t1iterable

The starting parameters for model 1

lpost2object of a subclass of Posterior

The Posterior object for model 2

t2iterable

The starting parameters for model 2

negbool, optional, default True

Boolean flag to decide whether to use the negative log-likelihood or log-posterior

max_post: bool, optional, default ``False``

If True, set the internal state to do the optimization with the log-likelihood rather than the log-posterior.

Returns:
pvaluefloat [0,1]

p-value ‘n stuff

compute_lrt(lpost1, t1, lpost2, t2, neg=True, max_post=False)

This function computes the Likelihood Ratio Test between two nested models.

Parameters:
lpost1object of a subclass of Posterior

The Posterior object for model 1

t1iterable

The starting parameters for model 1

lpost2object of a subclass of Posterior

The Posterior object for model 2

t2iterable

The starting parameters for model 2

negbool, optional, default True

Boolean flag to decide whether to use the negative log-likelihood or log-posterior

max_post: bool, optional, default ``False``

If True, set the internal state to do the optimization with the log-likelihood rather than the log-posterior.

Returns:
lrtfloat

The likelihood ratio for model 2 and model 1

res1OptimizationResults object

Contains the result of fitting lpost1

res2OptimizationResults object

Contains the results of fitting lpost2

fit(lpost, t0, neg=True, scipy_optimize_options=None)[source]

Do either a Maximum-A-Posteriori (MAP) or Maximum Likelihood (ML) fit to the power spectrum.

MAP fits include priors, ML fits do not.

Parameters:
lpoststingray.modeling.PSDPosterior object

An instance of class stingray.modeling.PSDPosterior that defines the function to be minimized (either in loglikelihood or logposterior)

t0{list | numpy.ndarray}

List/array with set of initial parameters

negbool, optional, default True

Boolean to be passed to lpost, setting whether to use the negative posterior or the negative log-likelihood.

scipy_optimize_optionsdict, optional, default None

A dictionary with options for scipy.optimize.minimize, directly passed on as keyword arguments.

Returns:
resOptimizationResults object

An object containing useful summaries of the fitting procedure. For details, see documentation of OptimizationResults.

plotfits(res1, res2=None, save_plot=False, namestr='test', log=False)[source]

Plotting method that allows to plot either one or two best-fit models with the data.

Plots a power spectrum with the best-fit model, as well as the data/model residuals for each model.

Parameters:
res1OptimizationResults object

Output of a successful fitting procedure

res2OptimizationResults object, optional, default None

Optional output of a second successful fitting procedure, e.g. with a competing model

save_plotbool, optional, default False

If True, the resulting figure will be saved to a file

namestrstr, optional, default test

If save_plot is True, this string defines the path and file name for the output plot

logbool, optional, default False

If True, plot the axes logarithmically.

sample(lpost, t0, cov=None, nwalkers=500, niter=100, burnin=100, threads=1, print_results=True, plot=False, namestr='test')[source]

Sample the posterior distribution defined in lpost using MCMC. Here we use the emcee package, but other implementations could in principle be used.

Parameters:
lpostinstance of a Posterior subclass

and instance of class Posterior or one of its subclasses that defines the function to be minimized (either in loglikelihood or logposterior)

t0iterable

list or array containing the starting parameters. Its length must match lpost.model.npar.

nwalkersint, optional, default 500

The number of walkers (chains) to use during the MCMC procedure. The more walkers are used, the slower the estimation will be, but the better the final distribution is likely to be.

niterint, optional, default 100

The number of iterations to run the MCMC chains for. The larger this number, the longer the estimation will take, but the higher the chance that the walkers have actually converged on the true posterior distribution.

burninint, optional, default 100

The number of iterations to run the walkers before convergence is assumed to have occurred. This part of the chain will be discarded before sampling from what is then assumed to be the posterior distribution desired.

threadsint, optional, default 1

The number of threads for parallelization. Default is 1, i.e. no parallelization

print_resultsbool, optional, default True

Boolean flag setting whether the results of the MCMC run should be printed to standard output

plotbool, optional, default False

Boolean flag setting whether summary plots of the MCMC chains should be produced

namestrstr, optional, default test

Optional string for output file names for the plotting.

Returns:
resSamplingResults object

An object containing useful summaries of the sampling procedure. For details see documentation of SamplingResults.

simulate_highest_outlier(s_all, lpost, t0, max_post=True, seed=None)[source]

Simulate \(n\) power spectra from a model and then find the highest data/model outlier in each.

The data/model outlier is defined as

\[\max{(T_R = 2(\mathrm{data}/\mathrm{model}))} .\]
Parameters:
s_allnumpy.ndarray

A list of parameter values derived either from an approximation of the likelihood surface, or from an MCMC run. Has dimensions (n, ndim), where n is the number of simulated power spectra to generate, and ndim the number of model parameters.

lpostinstance of a Posterior subclass

an instance of class Posterior or one of its subclasses that defines the function to be minimized (either in loglikelihood or logposterior)

t0iterable

list or array containing the starting parameters. Its length must match lpost.model.npar.

max_post: bool, optional, default ``False``

If True, do MAP fits on the power spectrum to find the highest data/model outlier Otherwise, do a Maximum Likelihood fit. If True, the simulated power spectra will be generated from an MCMC run, otherwise the method will employ the approximated covariance matrix for the parameters derived from the likelihood surface to generate samples from that likelihood function.

seedint, optional, default None

An optional number to seed the random number generator with, for reproducibility of the results obtained with this method.

Returns:
max_y_allnumpy.ndarray

An array of maximum outliers for each simulated power spectrum

simulate_lrts(s_all, lpost1, t1, lpost2, t2, seed=None)[source]

Simulate likelihood ratios for two given models based on MCMC samples for the simpler model (i.e. the null hypothesis).

Parameters:
s_allnumpy.ndarray of shape (nsamples, lpost1.npar)

An array with MCMC samples derived from the null hypothesis model in lpost1. Its second dimension must match the number of free parameters in lpost1.model.

lpost1LogLikelihood or Posterior subclass object

Object containing the null hypothesis model

t1iterable of length lpost1.npar

A starting guess for fitting the model in lpost1

lpost2LogLikelihood or Posterior subclass object

Object containing the alternative hypothesis model

t2iterable of length lpost2.npar

A starting guess for fitting the model in lpost2

max_postbool, optional, default True

If True, then lpost1 and lpost2 should be Posterior subclass objects; if False, then lpost1 and lpost2 should be LogLikelihood subclass objects

seedint, optional default None

A seed to initialize the numpy.random.RandomState object to be passed on to _generate_data. Useful for producing exactly reproducible results

Returns:
lrt_simnumpy.ndarray

An array with the simulated likelihood ratios for the simulated data


Auxiliary Classes

These are helper classes instantiated by stingray.modeling.ParameterEstimation and its subclasses to organize the results of model fitting and sampling in a more meaningful, easily accessible way.

class stingray.modeling.OptimizationResults(lpost, res, neg=True, log=None)[source]

Helper class that will contain the results of the regression. Less fiddly than a dictionary.

Parameters:
lpost: instance of :class:`Posterior` or one of its subclasses

The object containing the function that is being optimized in the regression

res: instance of ``scipy.OptimizeResult``

The object containing the results from a optimization run

negbool, optional, default True

A flag that sets whether the log-likelihood or negative log-likelihood is being used

loga logging.getLogger() object, default None

You can pass a pre-defined object for logging, else a new logger will be instantiated

Attributes:
resultfloat

The result of the optimization, i.e. the function value at the minimum that the optimizer found

p_optiterable

The list of parameters at the minimum found by the optimizer

modelastropy.models.Model instance

The parametric model fit to the data

covnumpy.ndarray

The covariance matrix for the parameters, has shape (len(p_opt), len(p_opt))

errnumpy.ndarray

The standard deviation of the parameters, derived from the diagonal of cov. Has the same shape as p_opt

mfitnumpy.ndarray

The values of the model for all x

deviancefloat

The deviance, calculated as -2*log(likelihood)

aicfloat

The Akaike Information Criterion, derived from the log(likelihood) and often used in model comparison between non-nested models; For more details, see [10]

bicfloat

The Bayesian Information Criterion, derived from the log(likelihood) and often used in model comparison between non-nested models; For more details, see [11]

meritfloat

sum of squared differences between data and model, normalized by the model values

dofint

The number of degrees of freedom in the problem, defined as the number of data points - the number of parameters

sexpint

2*(number of parameters)*(number of data points)

ssdfloat

sqrt(2*(sexp)), expected sum of data-model residuals

sobsfloat

sum of data-model residuals

References

_compute_covariance(lpost, res)[source]

Compute the covariance of the parameters using inverse of the Hessian, i.e. the second-order derivative of the log-likelihood. Also calculates an estimate of the standard deviation in the parameters, using the square root of the diagonal of the covariance matrix.

The Hessian is either estimated directly by the chosen method of fitting, or approximated using the statsmodel approx_hess function.

Parameters:
lpost: instance of :class:`Posterior` or one of its subclasses

The object containing the function that is being optimized in the regression

res: instance of ``scipy``’s ``OptimizeResult`` class

The object containing the results from a optimization run

_compute_criteria(lpost)[source]

Compute various information criteria useful for model comparison in non-nested models.

Currently implemented are the Akaike Information Criterion [12] and the Bayesian Information Criterion [13].

Parameters:
lpost: instance of :class:`Posterior` or one of its subclasses

The object containing the function that is being optimized in the regression

References

_compute_model(lpost)[source]

Compute the values of the best-fit model for all x.

Parameters:
lpost: instance of :class:`Posterior` or one of its subclasses

The object containing the function that is being optimized in the regression

_compute_statistics(lpost)[source]

Compute some useful fit statistics, like the degrees of freedom and the figure of merit.

Parameters:
lpost: instance of :class:`Posterior` or one of its subclasses

The object containing the function that is being optimized in the regression

print_summary(lpost)[source]

Print a useful summary of the fitting procedure to screen or a log file.

Parameters:
lpostinstance of Posterior or one of its subclasses

The object containing the function that is being optimized in the regression

class stingray.modeling.SamplingResults(sampler, ci_min=5, ci_max=95, log=None)[source]

Helper class that will contain the results of the sampling in a handy format.

Less fiddly than a dictionary.

Parameters:
sampler: ``emcee.EnsembleSampler`` object

The object containing the sampler that’s done all the work.

ci_min: float out of [0,100]

The lower bound percentile for printing credible intervals on the parameters

ci_max: float out of [0,100]

The upper bound percentile for printing credible intervals on the parameters

loga logging.getLogger() object, default None

You can pass a pre-defined object for logging, else a new logger will be instantiated

Attributes:
samplesnumpy.ndarray

An array of samples from the MCMC run, including all chains flattened into one long (nwalkers*niter, ndim) array

nwalkersint

The number of chains used in the MCMC procedure

niterint

The number of MCMC iterations in each chain

ndimint

The dimensionality of the problem, i.e. the number of parameters in the model

acceptancefloat

The mean acceptance ratio, calculated over all chains

Lfloat

The product of acceptance ratio and number of samples

acorfloat

The autocorrelation length for the chains; should be shorter than the chains themselves for independent sampling

rhatfloat

weighted average of between-sequence variance and within-sequence variance; Gelman-Rubin convergence statistic [14]

meannumpy.ndarray

An array of size ndim, with the posterior means of the parameters derived from the MCMC chains

stdnumpy.ndarray

An array of size ndim with the posterior standard deviations of the parameters derived from the MCMC chains

cinumpy.ndarray

An array of shape (ndim, 2) containing the lower and upper bounds of the credible interval (the Bayesian equivalent of the confidence interval) for each parameter using the bounds set by ci_min and ci_max

References

_check_convergence(sampler)[source]

Compute common statistics for convergence of the MCMC chains. While you can never be completely sure that your chains converged, these present reasonable heuristics to give an indication whether convergence is very far off or reasonably close.

Currently implemented are the autocorrelation time [15] and the Gelman-Rubin convergence criterion [16].

Parameters:
sampleran emcee.EnsembleSampler object

References

_compute_rhat(sampler)[source]

Compute Gelman-Rubin convergence criterion [17].

Parameters:
sampleran emcee.EnsembleSampler object

References

_infer(ci_min=5, ci_max=95)[source]

Infer the Posterior means, standard deviations and credible intervals (i.e. the Bayesian equivalent to confidence intervals) from the Posterior samples for each parameter.

Parameters:
ci_minfloat

Lower bound to the credible interval, given as percentage between 0 and 100

ci_maxfloat

Upper bound to the credible interval, given as percentage between 0 and 100

plot_results(nsamples=1000, fig=None, save_plot=False, filename='test.pdf')[source]

Plot some results in a triangle plot. If installed, will use [corner] for the plotting, if not, uses its own code to make a triangle plot.

By default, this method returns a matplotlib.Figure object, but if save_plot=True the plot can be saved to file automatically,

Parameters:
nsamplesint, default 1000

The maximum number of samples used for plotting.

figmatplotlib.Figure instance, default None

If created externally, you can pass a Figure instance to this method. If none is passed, the method will create one internally.

save_plotbool, default False

If True save the plot to file with a file name specified by the keyword filename. If False just return the Figure object

filenamestr

Name of the output file with the figure

References

print_results()[source]

Print results of the MCMC run on screen or to a log-file.


Convenience Functions

These functions are designed to help the user perform common tasks related to modelling and parameter estimation. In particular, the function stingray.modeling.set_logprior() is designed to help users set priors in their stingray.modeling.Posterior subclass objects.

stingray.modeling.set_logprior(lpost, priors)[source]

This function constructs the logprior method required to successfully use a Posterior object.

All instances of class Posterior and its subclasses require to implement a logprior methods. However, priors are strongly problem-dependent and therefore usually user-defined.

This function allows for setting the logprior method on any instance of class Posterior efficiently by allowing the user to pass a dictionary of priors and an instance of class Posterior.

Parameters:
lpostPosterior object

An instance of class Posterior or any of its subclasses

priorsdict

A dictionary containing the prior definitions. Keys are parameter names as defined by the astropy.models.FittableModel instance supplied to the model parameter in Posterior. Items are functions that take a parameter as input and return the log-prior probability of that parameter.

Returns:
logpriorfunction

The function definition for the prior

Examples

Make a light curve and power spectrum

>>> photon_arrivals = np.sort(np.random.uniform(0,1000, size=10000))
>>> lc = Lightcurve.make_lightcurve(photon_arrivals, dt=1.0)
>>> ps = Powerspectrum(lc, norm="frac")

Define the model

>>> pl = models.PowerLaw1D()
>>> pl.x_0.fixed = True

Instantiate the posterior:

>>> lpost = PSDPosterior(ps.freq, ps.power, pl, m=ps.m)

Define the priors:

>>> p_alpha = lambda alpha: ((-1. <= alpha) & (alpha <= 5.))
>>> p_amplitude = lambda amplitude: ((-10 <= np.log(amplitude)) &
...                                 ((np.log(amplitude) <= 10.0)))
>>> priors = {"alpha":p_alpha, "amplitude":p_amplitude}

Set the logprior method in the lpost object:

>>> lpost.logprior = set_logprior(lpost, priors)
stingray.modeling.scripts.fit_crossspectrum(cs, model, starting_pars=None, max_post=False, priors=None, fitmethod='L-BFGS-B')[source]

Fit a number of Lorentzians to a cross spectrum, possibly including white noise. Each Lorentzian has three parameters (amplitude, centroid position, full-width at half maximum), plus one extra parameter if the white noise level should be fit as well. Priors for each parameter can be included in case max_post = True, in which case the function will attempt a Maximum-A-Posteriori fit. Priors must be specified as a dictionary with one entry for each parameter. The parameter names are (amplitude_i, x_0_i, fwhm_i) for each i out of a total of N Lorentzians. The white noise level has a parameter amplitude_(N+1). For example, a model with two Lorentzians and a white noise level would have parameters: [amplitude_0, x_0_0, fwhm_0, amplitude_1, x_0_1, fwhm_1, amplitude_2].

Parameters:
csCrossspectrum

A Crossspectrum object with the data to be fit

model: astropy.modeling.models class instance

The parametric model supposed to represent the data. For details see the astropy.modeling documentation

starting_parsiterable, optional, default None

The list of starting guesses for the optimizer. If it is not provided, then default parameters are taken from model. See explanation above for ordering of parameters in this list.

max_postbool, optional, default False

If True, perform a Maximum-A-Posteriori fit of the data rather than a Maximum Likelihood fit. Note that this requires priors to be specified, otherwise this will cause an exception!

priors{dict | None}, optional, default None

Dictionary with priors for the MAP fit. This should be of the form {“parameter name”: probability distribution, …}

fitmethodstring, optional, default “L-BFGS-B”

Specifies an optimization algorithm to use. Supply any valid option for scipy.optimize.minimize.

Returns:
parestPSDParEst object

A PSDParEst object for further analysis

resOptimizationResults object

The OptimizationResults object storing useful results and quantities relating to the fit

stingray.modeling.scripts.fit_lorentzians(ps, nlor, starting_pars, fit_whitenoise=True, max_post=False, priors=None, fitmethod='L-BFGS-B')[source]

Fit a number of Lorentzians to a power spectrum, possibly including white noise. Each Lorentzian has three parameters (amplitude, centroid position, full-width at half maximum), plus one extra parameter if the white noise level should be fit as well. Priors for each parameter can be included in case max_post = True, in which case the function will attempt a Maximum-A-Posteriori fit. Priors must be specified as a dictionary with one entry for each parameter. The parameter names are (amplitude_i, x_0_i, fwhm_i) for each i out of a total of N Lorentzians. The white noise level has a parameter amplitude_(N+1). For example, a model with two Lorentzians and a white noise level would have parameters: [amplitude_0, x_0_0, fwhm_0, amplitude_1, x_0_1, fwhm_1, amplitude_2].

Parameters:
psPowerspectrum

A Powerspectrum object with the data to be fit

nlorint

The number of Lorentzians to fit

starting_parsiterable

The list of starting guesses for the optimizer. If it is not provided, then default parameters are taken from model. See explanation above for ordering of parameters in this list.

fit_whitenoisebool, optional, default True

If True, the code will attempt to fit a white noise level along with the Lorentzians. Be sure to include a starting parameter for the optimizer in starting_pars!

max_postbool, optional, default False

If True, perform a Maximum-A-Posteriori fit of the data rather than a Maximum Likelihood fit. Note that this requires priors to be specified, otherwise this will cause an exception!

priors{dict | None}, optional, default None

Dictionary with priors for the MAP fit. This should be of the form {“parameter name”: probability distribution, …}

fitmethodstring, optional, default “L-BFGS-B”

Specifies an optimization algorithm to use. Supply any valid option for scipy.optimize.minimize.

Returns:
parestPSDParEst object

A PSDParEst object for further analysis

resOptimizationResults object

The OptimizationResults object storing useful results and quantities relating to the fit

Examples

We start by making an example power spectrum with three Lorentzians >>> np.random.seed(400) >>> nlor = 3

>>> x_0_0 = 0.5
>>> x_0_1 = 2.0
>>> x_0_2 = 7.5
>>> amplitude_0 = 150.0
>>> amplitude_1 = 50.0
>>> amplitude_2 = 15.0
>>> fwhm_0 = 0.1
>>> fwhm_1 = 1.0
>>> fwhm_2 = 0.5

We will also include a white noise level: >>> whitenoise = 2.0

>>> model = (models.Lorentz1D(amplitude_0, x_0_0, fwhm_0) +
...          models.Lorentz1D(amplitude_1, x_0_1, fwhm_1) +
...          models.Lorentz1D(amplitude_2, x_0_2, fwhm_2) +
...          models.Const1D(whitenoise))
>>> freq = np.linspace(0.01, 10.0, 1000)
>>> p = model(freq)
>>> noise = np.random.exponential(size=len(freq))
>>> power = p*noise
>>> ps = Powerspectrum()
>>> ps.freq = freq
>>> ps.power = power
>>> ps.df = ps.freq[1] - ps.freq[0]
>>> ps.m = 1

Now we have to guess starting parameters. For each Lorentzian, we have amplitude, centroid position and fwhm, and this pattern repeats for each Lorentzian in the fit. The white noise level is the last parameter. >>> t0 = [150, 0.4, 0.2, 50, 2.3, 0.6, 20, 8.0, 0.4, 2.1]

We’re ready for doing the fit: >>> parest, res = fit_lorentzians(ps, nlor, t0)

res contains a whole array of useful information about the fit, for example the parameters at the optimum: >>> p_opt = res.p_opt

stingray.modeling.scripts.fit_powerspectrum(ps, model, starting_pars=None, max_post=False, priors=None, fitmethod='L-BFGS-B')[source]

Fit a number of Lorentzians to a power spectrum, possibly including white noise. Each Lorentzian has three parameters (amplitude, centroid position, full-width at half maximum), plus one extra parameter if the white noise level should be fit as well. Priors for each parameter can be included in case max_post = True, in which case the function will attempt a Maximum-A-Posteriori fit. Priors must be specified as a dictionary with one entry for each parameter. The parameter names are (amplitude_i, x_0_i, fwhm_i) for each i out of a total of N Lorentzians. The white noise level has a parameter amplitude_(N+1). For example, a model with two Lorentzians and a white noise level would have parameters: [amplitude_0, x_0_0, fwhm_0, amplitude_1, x_0_1, fwhm_1, amplitude_2].

Parameters:
psPowerspectrum

A Powerspectrum object with the data to be fit

model: astropy.modeling.models class instance

The parametric model supposed to represent the data. For details see the astropy.modeling documentation

starting_parsiterable, optional, default None

The list of starting guesses for the optimizer. If it is not provided, then default parameters are taken from model. See explanation above for ordering of parameters in this list.

fit_whitenoisebool, optional, default True

If True, the code will attempt to fit a white noise level along with the Lorentzians. Be sure to include a starting parameter for the optimizer in starting_pars!

max_postbool, optional, default False

If True, perform a Maximum-A-Posteriori fit of the data rather than a Maximum Likelihood fit. Note that this requires priors to be specified, otherwise this will cause an exception!

priors{dict | None}, optional, default None

Dictionary with priors for the MAP fit. This should be of the form {“parameter name”: probability distribution, …}

fitmethodstring, optional, default “L-BFGS-B”

Specifies an optimization algorithm to use. Supply any valid option for scipy.optimize.minimize.

Returns:
parestPSDParEst object

A PSDParEst object for further analysis

resOptimizationResults object

The OptimizationResults object storing useful results and quantities relating to the fit

Examples

We start by making an example power spectrum with three Lorentzians >>> m = 1 >>> nfreq = 100000 >>> freq = np.linspace(1, 1000, nfreq)

>>> np.random.seed(100)  # set the seed for the random number generator
>>> noise = np.random.exponential(size=nfreq)
>>> model = models.PowerLaw1D() + models.Const1D()
>>> model.x_0_0.fixed = True
>>> alpha_0 = 2.0
>>> amplitude_0 = 100.0
>>> amplitude_1 = 2.0
>>> model.alpha_0 = alpha_0
>>> model.amplitude_0 = amplitude_0
>>> model.amplitude_1 = amplitude_1
>>> p = model(freq)
>>> power = noise * p
>>> ps = Powerspectrum()
>>> ps.freq = freq
>>> ps.power = power
>>> ps.m = m
>>> ps.df = freq[1] - freq[0]
>>> ps.norm = "leahy"

Now we have to guess starting parameters. For each Lorentzian, we have amplitude, centroid position and fwhm, and this pattern repeats for each Lorentzian in the fit. The white noise level is the last parameter. >>> t0 = [80, 1.5, 2.5]

Let’s also make a model to test: >>> model_to_test = models.PowerLaw1D() + models.Const1D() >>> model_to_test.amplitude_1.fixed = True

We’re ready for doing the fit: >>> parest, res = fit_powerspectrum(ps, model_to_test, t0)

res contains a whole array of useful information about the fit, for example the parameters at the optimum: >>> p_opt = res.p_opt


Gaussian Process Modeling

class stingray.modeling.gpmodeling.GPResult(lc: Lightcurve)[source]

Makes a GPResult object which takes in a Stingray.Lightcurve and samples parameters of a model (Gaussian Process) based on the given prior and log_likelihood function.

Parameters:
lc: Stingray.Lightcurve object

The lightcurve on which the bayesian inference is to be done

comparison_plot(param1: str, param2: str, n1=0, n2=0, rkey=None, axis=None, save=False, filename=None)[source]

Plots the comparison plot between two given parameters

Parameters:
param1str

Name of the first parameter. Should be from the names of the parameter list used or from the names of parameters used in the prior_function

param2str

Name of the second parameter. Should be from the names of the parameter list used or from the names of parameters used in the prior_function

n1int, default 0

The index of the first parameter to be plotted (for multi component parameters). For multivariate parameters, the index of the specific parameter to be plotted.

n2int, default 0

The index of the second parameter to be plotted (for multi component parameters). For multivariate parameters, the index of the specific parameter to be plotted.

key: jax.random.PRNGKey, default ``random.PRNGKey(1234)``

Random key for the shuffling the weights

axislist, tuple, string, default None

Parameter to set axis properties of matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for matplotlib.pyplot.axis() method.

savebool, optionalm, default False

If True, save the figure with specified filename.

filenamestr

File name and path of the image to save. Depends on the boolean save.

Returns:
pltmatplotlib.pyplot object

Reference to plot, call show() to display it

get_evidence()[source]

Returns the log evidence of the model

get_max_likelihood_parameters()[source]

Returns the maximum likelihood parameters

get_max_posterior_parameters()[source]

Returns the optimal parameters for the model based on the NUTS sampling

get_parameters_names()[source]

Returns the names of the parameters

plot_cornerplot()[source]

Plots the corner plot for the sampled hyperparameters

plot_diagnostics()[source]

Plots the diagnostic plots for the sampling process

posterior_plot(name: str, n=0, axis=None, save=False, filename=None)[source]

Plots the posterior histogram for the given parameter

Parameters:
namestr

Name of the parameter. Should be from the names of the parameter list used or from the names of parameters used in the prior_function

nint, default 0

The index of the parameter to be plotted (for multi component parameters). For multivariate parameters, the index of the specific parameter to be plotted.

axislist, tuple, string, default None

Parameter to set axis properties of matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for matplotlib.pyplot.axis() method.

savebool, optional, default False

If True, save the figure with specified filename.

filenamestr

File name and path of the image to save. Depends on the boolean save.

Returns:
pltmatplotlib.pyplot object

Reference to plot, call show() to display it

print_summary()[source]

Prints a summary table for the model parameters

sample(prior_model=None, likelihood_model=None, max_samples=10000.0, num_live_points=500)[source]

Makes a Jaxns nested sampler over the Gaussian Process, given the prior and likelihood model

Parameters:
prior_model: jaxns.prior.PriorModelType object

A prior generator object. Can be made using the get_prior function or can use your own jaxns compatible prior function.

likelihood_model: jaxns.types.LikelihoodType object

A likelihood function which takes in the arguments of the prior model and returns the loglikelihood of the model. Can be made using the get_log_likelihood function or can use your own log_likelihood function with same order of arguments as the prior_model.

max_samples: int, default 1e4

The maximum number of samples to be taken by the nested sampler

num_live_pointsint, default 500

The number of live points to use in the nested sampling

Returns:
results: jaxns.results.NestedSamplerResults object

The results of the nested sampling process

weighted_posterior_plot(name: str, n=0, rkey=None, axis=None, save=False, filename=None)[source]

Returns the weighted posterior histogram for the given parameter

Parameters:
namestr

Name of the parameter. Should be from the names of the parameter list used or from the names of parameters used in the prior_function

nint, default 0

The index of the parameter to be plotted (for multi component parameters). For multivariate parameters, the index of the specific parameter to be plotted.

key: jax.random.PRNGKey, default ``random.PRNGKey(1234)``

Random key for the weighted sampling

axislist, tuple, string, default None

Parameter to set axis properties of matplotlib figure. For example it can be a list like [xmin, xmax, ymin, ymax] or any other acceptable argument for matplotlib.pyplot.axis() method.

savebool, optionalm, default False

If True, save the figure with specified filename.

filenamestr

File name and path of the image to save. Depends on the boolean save.

Returns:
pltmatplotlib.pyplot object

Reference to plot, call show() to display it

stingray.modeling.gpmodeling.get_gp_params(kernel_type, mean_type)[source]

Generates a list of the parameters for the GP model based on the kernel and mean type. To be used to set the order of the parameters for get_prior and get_log_likelihood() functions.

Parameters:
kernel_type: string

The type of kernel to be used for the Gaussian Process model:

["RN", "QPO", "QPO_plus_RN"]
mean_type: string

The type of mean to be used for the Gaussian Process model:

["gaussian", "exponential", "constant", "skew_gaussian",
 "skew_exponential", "fred"]
Returns:
A list of the parameters for the GP model

Examples

get_gp_params(“QPO_plus_RN”, “gaussian”) [‘log_arn’, ‘log_crn’, ‘log_aqpo’, ‘log_cqpo’, ‘log_freq’, ‘log_A’, ‘t0’, ‘log_sig’]

stingray.modeling.gpmodeling.get_kernel(kernel_type, kernel_params)[source]

Function for producing the kernel for the Gaussian Process. Returns the selected Tinygp kernel for the given parameters.

Parameters:
kernel_type: string

The type of kernel to be used for the Gaussian Process To be selected from the kernels already implemented:

["RN", "QPO", "QPO_plus_RN"]
kernel_params: dict

Dictionary containing the parameters for the kernel Should contain the parameters for the selected kernel

stingray.modeling.gpmodeling.get_log_likelihood(params_list, kernel_type, mean_type, times, counts, **kwargs)[source]

A log likelihood generator function based on given values. Makes a jaxns specific log likelihood function which takes in the parameters in the order of the parameters list, and calculates the log likelihood of the data given the parameters, and the model (kernel, mean) of the GP model. Note Any parameters with a prefix of log_ are taken to be in the log scale.

Parameters:
params_list:

A list in order of the parameters to be used.

prior_dict:

A dictionary of the priors of parameters to be used.

kernel_type:

The type of kernel to be used in the model:

["RN", "QPO", "QPO_plus_RN"]
mean_type:

The type of mean to be used in the model:

["gaussian", "exponential", "constant", "skew_gaussian",
 "skew_exponential", "fred"]
times: np.array or jnp.array

The time array of the lightcurve

counts: np.array or jnp.array

The photon counts array of the lightcurve

Returns:
The Jaxns specific log likelihood function.
stingray.modeling.gpmodeling.get_mean(mean_type, mean_params)[source]

Function for producing the mean function for the Gaussian Process.

Parameters:
mean_type: string

The type of mean to be used for the Gaussian Process To be selected from the mean functions already implemented:

["gaussian", "exponential", "constant", "skew_gaussian",
 "skew_exponential", "fred"]
mean_params: dict

Dictionary containing the parameters for the mean Should contain the parameters for the selected mean

Returns:
A function which takes in the time coordinates and returns the mean values.

Examples

Unimodal Gaussian Mean Function:

mean_params = {"A": 3.0, "t0": 0.2,  "sig": 0.1}
mean = get_mean("gaussian", mean_params)

Multimodal Skew Gaussian Mean Function:

mean_params = {"A": jnp.array([3.0, 4.0]), "t0": jnp.array([0.2, 1]),
              "sig1": jnp.array([0.1, 0.4]), "sig2": jnp.array([0.4, 0.1])}
mean = get_mean("skew_gaussian", mean_params)
stingray.modeling.gpmodeling.get_prior(params_list, prior_dict)[source]

A prior generator function based on given values. Makes a jaxns specific prior function based on the given prior dictionary. Jaxns requires the parameters of the prior function and log_likelihood function to be in the same order. This order is made according to the params_list.

Parameters:
params_list:

A list in order of the parameters to be used.

prior_dict:

A dictionary of the priors of parameters to be used. These parameters should be from tensorflow_probability distributions / Priors from jaxns or special priors from jaxns. Note: If jaxns priors are used, then the name given to them should be the same as the corresponding name in the params_list. Also, if a parameter is to be used in the log scale, it should have a prefix of log_

Returns:
The Prior generator function.
The arguments of the prior function are in the order of
  • Kernel arguments (RN arguments, QPO arguments),
  • Mean arguments
  • Miscellaneous arguments

Examples

A prior function for a Red Noise kernel and a Gaussian mean function:

# Obtain the parameters list
params_list = get_gp_params("RN", "gaussian")

# Make a prior dictionary using tensorflow_probability distributions
prior_dict = {
   "log_A": tfpd.Uniform(low = jnp.log(1e-1), high = jnp.log(2e+2)),
   "t0": tfpd.Uniform(low = 0.0 - 0.1, high = 1 + 0.1),
   "log_sig": tfpd.Uniform(low = jnp.log(0.5 * 1 / 20), high = jnp.log(2) ),
   "log_arn": tfpd.Uniform(low = jnp.log(0.1) , high = jnp.log(2) ),
   "log_crn": tfpd.Uniform(low = jnp.log(1 /5), high = jnp.log(20)),
}

prior_model = get_prior(params_list, prior_dict)

Pulsar

This submodule broadly defines functionality related to (X-ray) pulsar data analysis, especially periodicity searches.

class stingray.pulse.SincSquareModel(amplitude=1.0, mean=0.0, width=1.0, **kwargs)[source]
stingray.pulse.ef_profile_stat(profile, err=None)[source]

Calculate the epoch folding statistics ‘a la Leahy et al. (1983).

Parameters:
profilearray

The pulse profile

Returns:
statfloat

The epoch folding statistics

Other Parameters:
errfloat or array

The uncertainties on the pulse profile

Performs epoch folding at trial frequencies in photon data.

If no exposure correction is needed and numba is installed, it uses a fast algorithm to perform the folding. Otherwise, it runs a much slower algorithm, which however yields a more precise result. The search can be done in segments and the results averaged. Use segment_size to control this

Parameters:
timesarray-like

the event arrival times

frequenciesarray-like

the trial values for the frequencies

Returns:
(fgrid, stats) or (fgrid, fdgrid, stats), as follows:
fgridarray-like

frequency grid of the epoch folding periodogram

fdgridarray-like

frequency derivative grid. Only returned if fdots is an array.

statsarray-like

the epoch folding statistics corresponding to each frequency bin.

Other Parameters:
nbinint

the number of bins of the folded profiles

segment_sizefloat

the length of the segments to be averaged in the periodogram

fdotsarray-like

trial values of the first frequency derivative (optional)

expocorrbool

correct for the exposure (Use it if the period is comparable to the length of the good time intervals). If True, GTIs have to be specified via the gti keyword

gti[[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good time intervals

weightsarray-like

weight for each time. This might be, for example, the number of counts if the times array contains the time bins of a light curve

stingray.pulse.fftfit(prof, template=None, quick=False, sigma=None, use_bootstrap=False, **fftfit_kwargs)[source]

Align a template to a pulse profile.

Parameters:
profarray

The pulse profile

templatearray, default None

The template of the pulse used to perform the TOA calculation. If None, a simple sinusoid is used

Returns:
mean_amp, std_ampfloats

Mean and standard deviation of the amplitude

mean_phase, std_phasefloats

Mean and standard deviation of the phase

Other Parameters:
sigmaarray

error on profile bins (currently has no effect)

use_bootstrapbool

Calculate errors using a bootstrap method, with fftfit_error

**fftfit_kwargsadditional arguments for fftfit_error
stingray.pulse.fit_gaussian(x, y, amplitude=1.5, mean=0.0, stddev=2.0, tied={}, fixed={}, bounds={})[source]

Fit a gaussian function to x,y values.

Parameters:
xarray-like
yarray-like
Returns:
gfunction

The best-fit function, accepting x as input and returning the best-fit model as output

Other Parameters:
amplitudefloat

The initial value for the amplitude

meanfloat

The initial value for the mean of the gaussian function

stddevfloat

The initial value for the standard deviation of the gaussian function

tieddict
fixeddict
boundsdict

Parameters to be passed to the [astropy models]_

stingray.pulse.fit_sinc(x, y, amp=1.5, mean=0.0, width=1.0, tied={}, fixed={}, bounds={}, obs_length=None)[source]

Fit a sinc function to x,y values.

Parameters:
xarray-like
yarray-like
Returns:
sincfitfunction

The best-fit function, accepting x as input and returning the best-fit model as output

Other Parameters:
ampfloat

The initial value for the amplitude

meanfloat

The initial value for the mean of the sinc

obs_lengthfloat

The length of the observation. Default None. If it’s defined, it fixes width to 1/(pi*obs_length), as expected from epoch folding periodograms

widthfloat

The initial value for the width of the sinc. Only valid if obs_length is 0

tieddict
fixeddict
boundsdict

Parameters to be passed to the [astropy models]_

References

stingray.pulse.fold_events(times, *frequency_derivatives, mode='ef', nbin=16, weights=1, gti=None, ref_time=0, expocorr=False, variances=None)[source]

Epoch folding with exposure correction.

By default, the keyword times accepts a list of unbinned photon arrival times. If the input data is a (binned) light curve, then times will contain the time stamps of the observation, and weights should be set to the corresponding fluxes or counts.

Parameters:
timesarray of floats

Photon arrival times, or, if weights is set, time stamps of a light curve.

f, fdot, fddot…float

The frequency and any number of derivatives.

Returns:
phase_binsarray of floats

The phases corresponding to the pulse profile

profilearray of floats

The pulse profile

profile_errarray of floats

The uncertainties on the pulse profile

Other Parameters:
nbinint, optional, default 16

The number of bins in the pulse profile

weightsfloat or array of floats, optional, default 1

The weights of the data. It can either be specified as a single value for all points, or an array with the same length as time

gti[[gti0_0, gti0_1], [gti1_0, gti1_1], …], optional, default None

Good time intervals

ref_timefloat, optional, default 0

Reference time for the timing solution

expocorrbool, optional, default False

Correct each bin for exposure (use when the period of the pulsar is comparable to that of GTIs)

modestr, [“ef”, “pdm”], optional, default “ef”

Whether to calculate the epoch folding or phase dispersion minimization folded profile. For “ef”, it calculates the (weighted) sum of the data points in each phase bin, for “pdm”, the variance in each phase bin

stingray.pulse.get_TOA(prof, period, tstart, template=None, additional_phase=0, quick=False, debug=False, use_bootstrap=False, **fftfit_kwargs)[source]

Calculate the Time-Of-Arrival of a pulse.

Parameters:
profarray

The pulse profile

templatearray, default None

The template of the pulse used to perform the TOA calculation, if any. Otherwise use the default of fftfit

tstartfloat

The time at the start of the pulse profile

Returns:
toa, toastdfloats

Mean and standard deviation of the TOA

Other Parameters:
nstepint, optional, default 100

Number of steps for the bootstrap method

stingray.pulse.get_orbital_correction_from_ephemeris_file(mjdstart, mjdstop, parfile, ntimes=1000, ephem='DE405', return_pint_model=False)[source]

Get a correction for orbital motion from pulsar parameter file.

Parameters:
mjdstart, mjdstopfloat

Start and end of the time interval where we want the orbital solution

parfilestr

Any parameter file understood by PINT (Tempo or Tempo2 format)

Returns:
correction_secfunction

Function that accepts in input an array of times in seconds and a floating-point MJDref value, and returns the deorbited times

correction_mjdfunction

Function that accepts times in MJDs and returns the deorbited times.

Other Parameters:
ntimesint

Number of time intervals to use for interpolation. Default 1000

stingray.pulse.htest(data, nmax=20, datatype='binned', err=None)[source]

htest-test statistic, a` la De Jager+89, A&A, 221, 180D, eq. 2.

If datatype is “binned” or “gauss”, uses the formulation from Bachetti+2021, ApJ, arxiv:2012.11397

Parameters:
dataarray of floats

Phase values or binned flux values

nmaxint, default 20

Maximum of harmonics for Z^2_n

Returns:
Mint

The best number of harmonics that describe the signal.

htestfloat

The htest statistics of the events.

Other Parameters:
datatypestr

The datatype of data: “events” if phase values between 0 and 1, “binned” if folded pulse profile from photons, “gauss” if folded pulse profile with normally-distributed fluxes

errfloat

The uncertainty on the pulse profile fluxes (required for datatype=”gauss”, ignored otherwise)

stingray.pulse.p_to_f(*period_derivatives)[source]

Convert periods into frequencies, and vice versa.

For now, limited to third derivative. Raises when a fourth derivative is passed.

Parameters:
p, pdot, pddot, …floats

period derivatives, starting from zeroth and in increasing order

Examples

>>> assert p_to_f() == []
>>> assert np.allclose(p_to_f(1), [1])
>>> assert np.allclose(p_to_f(1, 2), [1, -2])
>>> assert np.allclose(p_to_f(1, 2, 3), [1, -2, 5])
>>> assert np.allclose(p_to_f(1, 2, 3, 4), [1, -2, 5, -16])
stingray.pulse.pdm_profile_stat(profile, sum_dev2, nsample)[source]

Calculate the phase dispersion minimization statistic following Stellingwerf (1978)

Parameters:
profilearray

The PDM pulse profile (normalised sum-of-squared-deviations), which uses the within bin means

sum_dev2float

The sum-of-squared-deviations of the whole sample, which uses the grand (whole sample together) mean

nsampleint

The number of time bins in the initial time series.

Returns:
statfloat

The epoch folding statistics

Performs folding at trial frequencies in time series data (i.e.~a light curve of flux or photon counts) and computes the Phase Dispersion Minimization statistic.

If no exposure correction is needed and numba is installed, it uses a fast algorithm to perform the folding. Otherwise, it runs a much slower algorithm, which however yields a more precise result. The search can be done in segments and the results averaged. Use segment_size to control this

Parameters:
timesarray-like

the time stamps of the time series

fluxarray-like

the flux or photon count values of the time series

frequenciesarray-like

the trial values for the frequencies

Returns:
(fgrid, stats) or (fgrid, fdgrid, stats), as follows:
fgridarray-like

frequency grid of the epoch folding periodogram

fdgridarray-like

frequency derivative grid. Only returned if fdots is an array.

statsarray-like

the epoch folding statistics corresponding to each frequency bin.

Other Parameters:
nbinint

the number of bins of the folded profiles

segment_sizefloat

the length of the segments to be averaged in the periodogram

fdotsarray-like

trial values of the first frequency derivative (optional)

expocorrbool

correct for the exposure (Use it if the period is comparable to the length of the good time intervals). If True, GTIs have to be specified via the gti keyword

gti[[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good time intervals

stingray.pulse.phase_exposure(start_time, stop_time, period, nbin=16, gti=None)[source]

Calculate the exposure on each phase of a pulse profile.

Parameters:
start_time, stop_timefloat

Starting and stopping time (or phase if period==1)

periodfloat

The pulse period (if 1, equivalent to phases)

Returns:
expoarray of floats

The normalized exposure of each bin in the pulse profile (1 is the highest exposure, 0 the lowest)

Other Parameters:
nbinint, optional, default 16

The number of bins in the profile

gti[[gti00, gti01], [gti10, gti11], …], optional, default None

Good Time Intervals

stingray.pulse.phaseogram(times, f, nph=128, nt=32, ph0=0, mjdref=None, fdot=0, fddot=0, pepoch=None, plot=False, phaseogram_ax=None, weights=None, **plot_kwargs)[source]

Calculate and plot the phaseogram of a pulsar observation.

The phaseogram is a 2-D histogram where the x axis is the pulse phase and the y axis is the time. It shows how the pulse phase changes with time, and it is very useful to see if the pulse solution is correct and/or if there are additional frequency derivatives appearing in the data (due to spin up or down, or even orbital motion)

Parameters:
timesarray

Event arrival times

ffloat

Pulse frequency

Returns:
phaseogr2-D matrix

The phaseogram

phasesarray-like

The x axis of the phaseogram (the x bins of the histogram), corresponding to the pulse phase in each column

timesarray-like

The y axis of the phaseogram (the y bins of the histogram), corresponding to the time at each row

additional_infodict

Additional information, like the pulse profile and the axes to modify the plot (the latter, only if return_plot is True)

Other Parameters:
nphint

Number of phase bins

ntint

Number of time bins

ph0float

The starting phase of the pulse

mjdreffloat

MJD reference time. If given, the y axis of the plot will be in MJDs, otherwise it will be in seconds.

fdotfloat

First frequency derivative

fddotfloat

Second frequency derivative

pepochfloat

If the input pulse solution is referred to a given time, give it here. It has no effect (just a phase shift of the pulse) if fdot is zero. if mjdref is specified, pepoch MUST be in MJD

weightsarray

Weight for each time

plotbool

Return the axes in the additional_info, and don’t close the plot, so that the user can add information to it.

stingray.pulse.plot_phaseogram(phaseogram, phase_bins, time_bins, unit_str='s', ax=None, **plot_kwargs)[source]

Plot a phaseogram.

Parameters:
phaseogramNxM array

The phaseogram to be plotted

phase_binsarray of M + 1 elements

The bins on the x-axis

time_binsarray of N + 1 elements

The bins on the y-axis

Returns:
axmatplotlib.pyplot.axis instance

Axis where the phaseogram was plotted.

Other Parameters:
unit_strstr

String indicating the time unit (e.g. ‘s’, ‘MJD’, etc)

axmatplotlib.pyplot.axis instance

Axis to plot to. If None, create a new one.

plot_kwargsdict

Additional arguments to be passed to pcolormesh

stingray.pulse.plot_profile(phase, profile, err=None, ax=None)[source]

Plot a pulse profile showing some stats.

If err is None, the profile is assumed in counts and the Poisson confidence level is plotted. Otherwise, err is shown as error bars

Parameters:
phasearray-like

The bins on the x-axis

profilearray-like

The pulsed profile

Returns:
axmatplotlib.pyplot.axis instance

Axis where the profile was plotted.

Other Parameters:
axmatplotlib.pyplot.axis instance

Axis to plot to. If None, create a new one.

stingray.pulse.pulse_phase(times, *frequency_derivatives, **opts)[source]

Calculate pulse phase from the frequency and its derivatives.

Parameters:
timesarray of floats

The times at which the phase is calculated

*frequency_derivatives: floats

List of derivatives in increasing order, starting from zero.

Returns:
phasesarray of floats

The absolute pulse phase

Other Parameters:
ph0float

The starting phase

to_1bool, default True

Only return the fractional part of the phase, normalized from 0 to 1

stingray.pulse.search_best_peaks(x, stat, threshold)[source]

Search peaks above threshold in an epoch folding periodogram.

If more values of stat are above threshold and are contiguous, only the largest one is returned (see Examples).

Parameters:
xarray-like

The x axis of the periodogram (frequencies, periods, …)

statarray-like

The y axis. It must have the same shape as x

thresholdfloat

The threshold value over which we look for peaks in the stat array

Returns:
best_xarray-like

the array containing the x position of the peaks above threshold. If no peaks are above threshold, an empty list is returned. The array is sorted by inverse value of stat

best_statarray-like

for each best_x, give the corresponding stat value. Empty if no peaks above threshold.

Examples

>>> # Test multiple peaks
>>> x = np.arange(10)
>>> stat = [0, 0, 0.5, 0, 0, 1, 1, 2, 1, 0]
>>> best_x, best_stat = search_best_peaks(x, stat, 0.5)
>>> len(best_x)
2
>>> assert np.isclose(best_x[0], 7.0)
>>> assert np.isclose(best_x[1], 2.0)
>>> stat = [0, 0, 2.5, 0, 0, 1, 1, 2, 1, 0]
>>> best_x, best_stat = search_best_peaks(x, stat, 0.5)
>>> assert np.isclose(best_x[0], 2.0)
>>> # Test no peak above threshold
>>> x = np.arange(10)
>>> stat = [0, 0, 0.4, 0, 0, 0, 0, 0, 0, 0]
>>> best_x, best_stat = search_best_peaks(x, stat, 0.5)
>>> best_x
[]
>>> best_stat
[]
stingray.pulse.sinc(x)[source]

Calculate a sinc function.

sinc(x)=sin(x)/x

Parameters:
xarray-like
Returns:
valuesarray-like
stingray.pulse.sinc_square_deriv(x, amplitude=1.0, mean=0.0, width=1.0)[source]

Calculate partial derivatives of sinc-squared.

Parameters:
x: array-like
Returns:
d_amplitudearray-like

partial derivative of sinc-squared function with respect to the amplitude

d_meanarray-like

partial derivative of sinc-squared function with respect to the mean

d_widtharray-like

partial derivative of sinc-squared function with respect to the width

Other Parameters:
amplitudefloat

the value for x=mean

meanfloat

mean of the sinc function

widthfloat

width of the sinc function

Examples

>>> assert np.allclose(sinc_square_deriv(0, amplitude=2.), [1., 0., 0.])
stingray.pulse.sinc_square_model(x, amplitude=1.0, mean=0.0, width=1.0)[source]

Calculate a sinc-squared function.

(sin(x)/x)**2

Parameters:
x: array-like
Returns:
sqvaluesarray-like

Return square of sinc function

Other Parameters:
amplitudefloat

the value for x=mean

meanfloat

mean of the sinc function

widthfloat

width of the sinc function

Examples

>>> assert np.isclose(sinc_square_model(0, amplitude=2.), 2.0)
stingray.pulse.z_n(data, n, datatype='events', err=None, norm=None)[source]

Z^2_n statistics, a` la Buccheri+83, A&A, 128, 245, eq. 2.

If datatype is “binned” or “gauss”, uses the formulation from Bachetti+2021, ApJ, arxiv:2012.11397

Parameters:
dataarray of floats

Phase values or binned flux values

nint, default 2

Number of harmonics, including the fundamental

Returns:
z2_nfloat

The Z^2_n statistics of the events.

Other Parameters:
datatypestr

The data type: “events” if phase values between 0 and 1, “binned” if folded pulse profile from photons, “gauss” if folded pulse profile with normally-distributed fluxes

errfloat

The uncertainty on the pulse profile fluxes (required for datatype=”gauss”, ignored otherwise)

normfloat

For backwards compatibility; if norm is not None, it is substituted to data, and data is ignored. This raises a DeprecationWarning

stingray.pulse.z_n_binned_events(profile, n)[source]

Z^2_n statistic for pulse profiles from binned events

See Bachetti+2021, arXiv:2012.11397

Parameters:
profilearray of floats

The folded pulse profile (containing the number of photons falling in each pulse bin)

nint

Number of harmonics, including the fundamental

Returns:
z2_nfloat

The value of the statistic

stingray.pulse.z_n_binned_events_all(profile, nmax=20)[source]

Z^2_n statistic for multiple harmonics and binned events

See Bachetti+2021, arXiv:2012.11397

Parameters:
profilearray of floats

The folded pulse profile (containing the number of photons falling in each pulse bin)

nint

Number of harmonics, including the fundamental

Returns:
kslist of ints

Harmonic numbers, from 1 to nmax (included)

z2_nfloat

The value of the statistic for all ks

stingray.pulse.z_n_events(phase, n)[source]

Z^2_n statistics, a` la Buccheri+83, A&A, 128, 245, eq. 2.

Parameters:
phasearray of floats

The phases of the events

nint, default 2

Number of harmonics, including the fundamental

Returns:
z2_nfloat

The Z^2_n statistic

stingray.pulse.z_n_events_all(phase, nmax=20)[source]

Z^2_n statistics, a` la Buccheri+83, A&A, 128, 245, eq. 2.

Parameters:
phasearray of floats

The phases of the events

nint, default 2

Number of harmonics, including the fundamental

Returns:
kslist of ints

Harmonic numbers, from 1 to nmax (included)

z2_nfloat

The Z^2_n statistic for all ks

stingray.pulse.z_n_gauss(profile, err, n)[source]

Z^2_n statistic for normally-distributed profiles

See Bachetti+2021, arXiv:2012.11397

Parameters:
profilearray of floats

The folded pulse profile

errfloat

The (assumed constant) uncertainty on the flux in each bin.

nint

Number of harmonics, including the fundamental

Returns:
z2_nfloat

The value of the statistic

stingray.pulse.z_n_gauss_all(profile, err, nmax=20)[source]

Z^2_n statistic for n harmonics and normally-distributed profiles

See Bachetti+2021, arXiv:2012.11397

Parameters:
profilearray of floats

The folded pulse profile

errfloat

The (assumed constant) uncertainty on the flux in each bin.

nmaxint

Maximum number of harmonics, including the fundamental

Returns:
kslist of ints

Harmonic numbers, from 1 to nmax (included)

z2_nlist of floats

The value of the statistic for all ks

Calculates the Z^2_n statistics at trial frequencies in photon data.

The “real” Z^2_n statistics is very slow. Therefore, in this function data are folded first, and then the statistics is calculated using the value of the profile as an additional normalization term. The two methods are mostly equivalent. However, the number of bins has to be chosen wisely: if the number of bins is too small, the search for high harmonics is ineffective. If no exposure correction is needed and numba is installed, it uses a fast algorithm to perform the folding. Otherwise, it runs a much slower algorithm, which however yields a more precise result. The search can be done in segments and the results averaged. Use segment_size to control this

Parameters:
timesarray-like

the event arrival times

frequenciesarray-like

the trial values for the frequencies

Returns:
(fgrid, stats) or (fgrid, fdgrid, stats), as follows:
fgridarray-like

frequency grid of the epoch folding periodogram

fdgridarray-like

frequency derivative grid. Only returned if fdots is an array.

statsarray-like

the Z^2_n statistics corresponding to each frequency bin.

Other Parameters:
nbinint

the number of bins of the folded profiles

segment_sizefloat

the length of the segments to be averaged in the periodogram

fdotsarray-like

trial values of the first frequency derivative (optional)

expocorrbool

correct for the exposure (Use it if the period is comparable to the length of the good time intervals.)

gti[[gti0_0, gti0_1], [gti1_0, gti1_1], …]

Good time intervals

weightsarray-like

weight for each time. This might be, for example, the number of counts if the times array contains the time bins of a light curve

Simulator

This submodule defines extensive functionality related to simulating spectral-timing data sets, including transfer and window functions, simulating light curves from power spectra for a range of stochastic processes.

class stingray.simulator.simulator.Simulator(dt, N, mean, rms, err=0.0, red_noise=1, random_state=None, tstart=0.0, poisson=False)[source]

Framework to simulate light curves with given variability distributions. The simulator module provides tools to simulate time series and spectral data. This can be useful, e.g., to access the uncertainty of a previous analysis or to develop an intuition of the detectability of a given phenomenon

Stingray simulator supports multiple methods to carry out these simulations. Light curves can be simulated through power-law spectrum, through a user-defined or pre-defined model, or through impulse responses. The module is designed in a way such that all these methods can be accessed using a common interface.

Parameters:
dtfloat, default 1

Time resolution (sampling interval) of the simulated light curve in seconds.

Nint, default 1024

Number of time bins in the simulated light curve.

meanfloat, default 0

mean value of the simulated light curve.

rmsfloat, default 1

Fractional RMS amplitude of the light curve; actual RMS is mean * rms.

errfloat, default 0

the errorbars on the final light curves.

red_noiseint, default 1

Simulate a light curve red_noise times longer, and extract a random segment of length N. This includes variability slower than the light curve, which leaks into its power spectrum as in real observations (red noise leakage). With the default of 1, the light curve is periodic and has no leakage.

random_stateint or numpy.random.RandomState, optional

Seed or random state for reproducible random number generation.

tstartfloat, default 0

Start time of the light curve in seconds.

poissonbool, default False

If True, simulates Poisson-distributed counts; otherwise, assumes Gaussian noise.

count_channels()[source]

Return total number of energy channels.

delete_channel(channel)[source]

Delete an energy channel.

delete_channels(channels)[source]

Delete multiple energy channels.

get_all_channels()[source]

Get lightcurves belonging to all channels.

get_channel(channel)[source]

Get lightcurve belonging to the energy channel.

get_channels(channels)[source]

Get multiple light curves belonging to the energy channels.

powerspectrum(lc, seg_size=None)[source]

Make a powerspectrum of the simulated light curve.

Parameters:
lclightcurve.Lightcurve object OR

iterable of lightcurve.Lightcurve objects The light curve data to be Fourier-transformed.

Returns:
powernumpy.ndarray

The array of normalized squared absolute values of Fourier amplitudes

static read(filename, fmt='pickle')[source]

Reads transfer function from a ‘pickle’ file.

Parameters:
fmtstr

the format of the file to be retrieved - accepts ‘pickle’.

Returns:
dataclass instance

TransferFunction object

relativistic_ir(t1=3, t2=4, t3=10, p1=1, p2=1.4, rise=0.6, decay=0.1)[source]

Construct a realistic impulse response considering the relativistic effects.

Parameters:
t1int

primary peak time

t2int

secondary peak time

t3int

end time

p1float

value of primary peak

p2float

value of secondary peak

risefloat

slope of rising exponential from primary peak to secondary peak

decayfloat

slope of decaying exponential from secondary peak to end time

Returns:
hnumpy.ndarray

Constructed impulse response

simple_ir(start=0, width=1000, intensity=1)[source]

Construct a simple impulse response using start time, width and scaling intensity. To create a delta impulse response, set width to the time resolution dt.

Parameters:
startint

start time of impulse response

widthfloat

Width of the impulse response. Widths shorter than dt produce a one-bin response.

intensityfloat

scaling parameter to set the intensity of delayed emission corresponding to direct emission.

Returns:
hnumpy.ndarray

Constructed impulse response

simulate(*args)[source]

Simulate light curve generation using power spectrum or impulse response.

Parameters:
args

See examples below.

Returns:
lightCurveLightCurve object

Examples

  • x = simulate(beta):

    For generating a light curve using power law spectrum.

    Parameters:
    • beta : float Defines the shape of spectrum

  • x = simulate(s):
    For generating a light curve from user-provided spectrum.

    Note: The spectrum must be given at the frequencies of the extended light curve, np.fft.rfftfreq(N * red_noise, d=dt)[1:], so it includes the frequencies below 1 / (N * dt) needed when red_noise > 1.

    Parameters:
    • s : array-like power spectrum

  • x = simulate(model):

    For generating a light curve from pre-defined model

    Parameters:
    • model : astropy.modeling.Model the pre-defined model

  • x = simulate(‘model’, params):

    For generating a light curve from pre-defined model

    Parameters:
    • model : string the pre-defined model

    • params : list iterable or dict the parameters for the pre-defined model

  • x = simulate(s, h):

    For generating a light curve using impulse response.

    Parameters:
    • s : array-like Underlying variability signal

    • h : array-like Impulse response

  • x = simulate(s, h, ‘same’):

    For generating a light curve of same length as input signal, using impulse response.

    Parameters:
    • s : array-like Underlying variability signal

    • h : array-like Impulse response

    • mode : str mode can be ‘same’, ‘filtered, or ‘full’. ‘same’ indicates that the length of output light curve is same as that of input signal. ‘filtered’ means that length of output light curve is len(s) - lag_delay ‘full’ indicates that the length of output light curve is len(s) + len(h) -1

simulate_channel(channel, *args)[source]

Simulate a lightcurve and add it to corresponding energy channel.

Parameters:
channelstr

range of energy channel (e.g., 3.5-4.5)

*args

see description of simulate() for details

Returns:
lightCurveLightCurve object
write(filename, fmt='pickle')[source]

Writes a transfer function to ‘pickle’ file.

Parameters:
fmtstr

the format of the file to be saved - accepts ‘pickle’

class stingray.simulator.transfer.TransferFunction(data, dt=1, de=1, tstart=0, estart=0, time=None, energy=None)[source]

Create or retrieve a transfer function, and form time and energy averaged responses.

Parameters:
datanumpy 2-d array / 2-d list

inner/first dimension (or column counts) represents time and outer/second dimension (or rows counts) represents energy.

As an example, if you have you 2-d model defined by ‘arr’, then arr[1][5] defines a time of 5(s) and energy of 1(keV) [assuming ‘dt’ and ‘de’ are 1 and ‘tstart’ and ‘estart’ are 0.]

Note that each row is a different energy channel starting from the lowest to the highest.

dtfloat, default 1

time interval

defloat, default 1

energy interval

tstartfloat, default 0

initial time value across time axis

estartfloat, default 0

initial energy value across energy axis

Attributes:
timenumpy.ndarray

energy-averaged/time-resolved response of 2-d transfer function

energynumpy.ndarray

time-averaged/energy-resolved response of 2-d transfer function

energy_response()[source]

Form a time-averaged/energy-resolved response of 2-d transfer function.

Returns:
timenumpy.ndarray

time-averaged/energy-resolved response of 2-d transfer function

plot(response='2d', save=False, filename=None, show=False)[source]

Plot ‘time’, ‘energy’ or 2-d response using matplotlib.

In case of 1-d response, ‘time’ and ‘energy’ would appear along x-axis and corresponding flux across y-axis. In case of 2-d response, a spectrograph would be formed with ‘time’ along x-axis and ‘energy’ along y-axis.

Parameters:
responsestr

type of response - accepts ‘time’, ‘energy’, ‘2d’

filenamestr

the name of file to save plot to. If a default of ‘None’ is picked, plot is not saved.

static read(filename, fmt='pickle')[source]

Reads transfer function from a ‘pickle’ file.

Parameters:
fmtstr

the format of the file to be retrieved - accepts ‘pickle’.

Returns:
dataclass instance

TransferFunction object

time_response(e0=None, e1=None)[source]

Form an energy-averaged/time-resolved response of 2-d transfer function.

Returns:
energynumpy.ndarray

energy-averaged/time-resolved response of 2-d transfer function

e0: int

start value of energy interval to be averaged

e1: int

end value of energy interval to be averaged

write(filename, fmt='pickle')[source]

Writes a transfer function to ‘pickle’ file.

Parameters:
fmtstr

the format of the file to be saved - accepts ‘pickle’

stingray.simulator.transfer.relativistic_ir(dt=0.125, t1=3, t2=4, t3=10, p1=1, p2=1.4, rise=0.6, decay=0.1)[source]

Construct a realistic impulse response considering the relativistic effects.

Parameters:
dtfloat

Time resolution

t1int

primary peak time

t2int

secondary peak time

t3int

end time

p1float

value of primary peak

p2float

value of secondary peak

risefloat

slope of rising exponential from primary peak to secondary peak

decayfloat

slope of decaying exponential from secondary peak to end time

Returns:
h: numpy.ndarray

Constructed impulse response

stingray.simulator.transfer.simple_ir(dt=0.125, start=0, width=1000, intensity=1)[source]

Construct a simple impulse response using start time, width and scaling intensity. To create a delta impulse response, set width to 1.

Parameters:
dtfloat

Time resolution

startint

start time of impulse response

widthint

width of impulse response

intensityfloat

scaling parameter to set the intensity of delayed emission corresponding to direct emission.

Returns:
hnumpy.ndarray

Constructed impulse response

Exceptions

Some basic Stingray-related errors and exceptions.

class stingray.exceptions.StingrayError[source]
class stingray.exceptions.StingrayWarning[source]

Base class for the warnings Stingray raises itself.

It subclasses UserWarning, which is what those warnings were before they had a class of their own, so filters written against UserWarning keep working. Having a class of their own means they can be silenced on their own:

import warnings
from stingray.exceptions import StingrayWarning

warnings.filterwarnings("ignore", category=StingrayWarning)
class stingray.modeling.posterior.PriorUndefinedError[source]

Raised when a Posterior object has no logprior method.

class stingray.modeling.posterior.LikelihoodUndefinedError[source]

Raised when a Posterior object has no loglikelihood method.

class stingray.modeling.posterior.IncorrectParameterError[source]

Raised when the parameters passed in do not match those of the model.