Skip to main content

Time-dependent analysis of point sources in Fermi-LAT data

Project description

Making FermiLAT gamma-ray light curves with wtlike

Quickly create a light curve for any 4FGL source, on any time scale, with optional Bayesian Block analysis

github links:

this document, the repository

Introduction

wtlike(Perhaps pronounced "DUB-Tee-like"), is a library optimized for interactive exploration in a Jupyter notebook with access to all Fermi-LAT data, and to analyze the time dependence of any source in the 4FGL catalog on any time scale, with the option of performing a Bayesian Block partition to select optimal time intervals. The source can be identified by the 4FGL name, or any equivalent common name.

Here is a minimal demo:

from wtlike import *
config = Config()
if config.valid:
    wtl = WtLike('3C 273')
    wtl.plot(UTC=True);
SourceData:  3C 273: Restoring from cache with key "P88Y3157_data"
SourceData: Source 3C 273 with:
	 data:        96,186 photons from 2008-08-04 to 2021-08-01
	 exposure: 2,872,539 intervals,  average effective area 1989 cm^2 for 85.8 Ms
	 rates:  source 1.73e-07/s, background 3.91e-07/s, S/N ratio 4.42e-01
CellData: Bin photon data into 677 1-week bins from 54683.0 to 59422.0
LightCurve: select 667 cells for fitting with e>5 & n>2

png

This assumes that the name for the source, in this case the historically famous first quasar to be discovered, can be associated with a 4FGL catalog source. The plot shows, as a function of UTC (or MJD if desired) time, weekly measurements of deviations of the flux relative to the average of the 12-year interval used to define the 4FGL-DR3 catalog.

The first stage, extracting data for the source, takes ~10 min, but, using an included cache system, only has to be done once.

Overview

This package has code that was developed with the nbdev code/tests/documentation environment from the github package lat-timing to generate light curves of Fermi-LAT sources.
It is based on a paper by Matthew Kerr, which derives the weighted likelihood formalism used here, specifically with the Bayesian Block to detect and characterize variability of a gamma-ray source.

There are several innovative design features that significantly improve the speed and portability.

  • Condensed photon and spacecraft data.
  • Weight tables
  • A cache to improve interactivity
  • Likelihood functions fit to a modified Poisson function
  • Unbinned likelihood
  • A simple user interface

How it works

Historically, gamma-ray source measurements have used two methods:

  1. For a fixed time interval, where the energy and position are used, with a model including all potential sources and a model of the detector response to define the likelihood. This has been the only way to study weak sources. A light curve must apply this to each time interval.
  2. Or, for very bright flares, for example GRBs, one can simply count the number of photons within a circular region, that is, aperture photometry.

Matthew Kerr introduced a third method, basically counting photons but using information from a static likelihood analysis to assign a "weight" to each photon, the probability for being from the source in question, then optimizing this likelihood. This assumes that the only thing changing is the flux of the source. He calls it "retrospective", since the analysis for the full time is then applied back to the individual photons. Another way of looking at it is to make the assumption that the time dependence of a source's photon flux factorizes from the energy and spatitial dependence.

Likelihood evaluation

In a significant modification from Kerr's implemetation as described in that paper, we evaluate weights for each photon by a table lookup.

Assumptions:

  • Source photons are completely contained in the dataset boundaries (the "ROI").
  • The instrument response is constant with time.
  • The background pattern is constant. (Clearly violated if a neighboring source varies!)

The likelihood evaluation is implemented in the module loglike.

Photon Data

Fermi data are retrieved from the Fermi-LAT weekly data files extracted from the GSFC FTP server, with subfolders for the photon data, photon and spacecraft data, spacecraft. It is described here. The files are organized into individual weeks, starting UTC midnight each Thursday. Files for the most recent week are updated daily.

We convert each pair of photon and spacecraft files to two DataFrame tables with the minimum information needed to evaluate the likelihood, as compressed as possible. Particularly, the energy and position are binned. Details can be seen in the module data_man.

The entire data set (SOURCE class, energy>100 MeV) in this format occupies ~2 GB.

Select Data for a source

All further processing uses a subset of the photons within a cone, currently $4^\circ$, about the selected source, and evaluates the exposure during 30-s intervals for this direction. In the class SourceData, implemented in the module source_data we

  1. Extract photons
  2. Evaluate exposure, using the effective area tables and a spectral assumption.
  3. Determine the weight for each photon, using the table for the source. See the module weights for details.

The result is a photon DataFrame, containing for each photon, the time $t$ in MJD units, $w$.

This class is a superclass of the user interface class WtLike introduced above.

Partition into cells

A "cell", the terminology used by Kerr, the set of photons in a specific time interval. The class CellData in the module cell_data, a subclass of SourceData manages this.

This class is instantiated with a tuple to define the binning in time. Denoted by (a, b, c), the elements are:

  • a--start time
  • b--stop time
  • c-- bin size in days, but 0 means orbit-based, intervals are contiguous eposure.

For the start and stop, values > 50000 are interpreted as MJD. Otherwise they are relative to start if positive or stop if negative for the full dataset, both rounded to a full day. Zero means actual start for a and stop for b. The default binning is simply (0, 0, 7) for weekly bins with the full dataset. Hourly for the most recent week would be (-7, 0, 1/24).

A DataFrame table of the cells is created as a data member cells, with content

  • t -- MJD time at cell center
  • tw -- cell width in days
  • e -- cell exposure, for reference
  • n -- the number of photons
  • w -- a list of n weights
  • S -- expected number source photons, the nominal source rate times the total exposure.
  • B -- expected number of background photons (unused)

Views

CellData implements a method view, which accepts a binning tuple as an argument, returns a copy of the current object, which can be a subclass, assigning to it the binning. Thus the view has all the attributes of its parent, but with a different set of cells.

So the following creates a new WtLike object that we generated above, rebins a copy with 25-day bins in the first 100 days, generates a list of the cells, then removes it since it wasn't assigned a reference variable.

if config.valid:
    wtl.view(0,100,25).cells
CellData: Bin photon data into 4 4-week bins from 54683.0 to 54783.0
LightCurve: select 4 cells for fitting with e>5 & n>2

Evaluate Likelihoods and make light curve plots

The class LightCurve, implemented in the module lightcurve is a subclass of SourceData. An instance invokes its superclass to generate the set of cells, then evaluates the likelihoods.

Poisson-like Likelihood

We fit the likelihood for each cell, using only a few evaluations, to a 3-parameter Poisson-like formula. Two advantages of this are:

  • efficiency -- for large numbers of photons, this is much faster
  • convenience -- the Poisson object implements functions that return the TS, 95% CL, maximum, and uncertainties, using the incomplete gamma function.

This results in a DataFrame fits containing

  • t, tw, n, from the cell, and
  • fit, the Poisson object

Light curve plots

The function Likelihood.plot actually invokes flux_plot

Apply Bayesian Blocks

The class WtLike, implememted in the module main, adds an implementation of the Bayesian Block (BB) algorithm, from the module bayesian using likelihood instead of counts. There we have two subclasses of astropy.stats.bayesian_blocks, CountFitness and the default LikelihoodFitness.

This code creates partitions between boundaries of a set of cells. Usage is via a special view, bb_view`

if config.valid:
    bb = wtl.bb_view()
    bb.plot();
Bayesian Blocks: partitioning 667 cells using LikelihoodFitness with penalty 5.0%
	found 93 / 667 blocks.
LightCurve: Loaded 93 / 93 cells for fitting

png

As you see, this made 94 blocks from the 656 weeks, fit each, and overplotted in on the weekly light curve.

Simulation

Finally, a simulation option is available. See the tutorial for an example

Installation

Note that this is in beta mode. It requires: matplotlib pandas scipy astropy healpy

To install from pyPI:

pip install wtlike

Data requirements: There are three sets of files:

  • photon data
    These are a set of weekly pickled python dict objects with compressed condensed photon and spacecraft data extracted from the GSFC FTP site. They contain every photon above 100 MeV, and less than $100^\circ$ from the zenith.

  • weight tables
    Each source to be analyzed needs a table defining the photon weight as a function of position, energy, and event type. These are currently generated by pointlike. (A fermipy-generated version would be preferable.)

  • effective area
    A standard Fermi instrument response file (IRF) defining the effective area as a function of detector angle and energy.

A set of these is available as a 1.6 GB zip file.

Input data

There are three data sources which wtlike needs to function:

  • The photon/spacecraft data
  • A table of weights for each source
  • An effective area IRF table

These must be found under a folder, which by default is ~/wtlike_data. In that folder there must be (perhaps links to) three folders named data_files, weight_files, aeff_files. A copy of what I'm using is at /afs/slac/g/glast/users/burnett/wtlike_data

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wtlike-0.3.8.tar.gz (74.0 kB view hashes)

Uploaded Source

Built Distribution

wtlike-0.3.8-py3-none-any.whl (84.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page