Start here to begin with Stingray.

[1]:
import numpy as np
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')

# Creating a light curve¶

[2]:
from stingray import Lightcurve

A Lightcurve object can be created in two ways :

1. From an array of time stamps and an array of counts.

2. From photon arrival times.

## 1. Array of time stamps and counts¶

Create 1000 time stamps

[3]:
times = np.arange(1000)
times[:10]
[3]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Create 1000 random count values between 0 to 100

[4]:
counts = np.random.rand(1000)*100
counts[:10]
[4]:
array([44.51278801, 49.12756457, 37.81375413, 17.26513442, 34.2754572 ,
27.86166326, 19.95572292, 69.42193826, 51.91623424, 81.91501055])

Create a Lightcurve object with the times and counts array.

[5]:
lc = Lightcurve(times, counts)

The number of data points can be counted with the len function.

[6]:
len(lc)
[6]:
1000

## 2. Photon Arrival Times¶

[7]:
arrivals[:10]
[7]:
array([1., 1., 2., 2., 2., 3., 3., 3., 3., 3.])
[8]:
lc_new = Lightcurve.make_lightcurve(arrivals, 1)

The time bins and respective counts can be seen with lc.counts and lc.time

[9]:
lc_new.counts
[9]:
array([2, 3, 5, 1, 4, 1, 3, 1, 1])
[10]:
lc_new.time
[10]:
array([1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5])

# Properties¶

A Lightcurve object has the following properties :

1. time : numpy array of time values

2. counts : numpy array of counts per bin values

3. countrate : numpy array of counts per second values

4. n : Number of data points in the lightcurve

5. dt : Time resolution of the light curve

6. tseg : Total duration of the light curve

7. tstart : Start time of the light curve

[11]:
lc.n == len(lc)
[11]:
True

# Operations¶

Two light curves can be summed up or subtracted from each other if they have same time arrays.

[12]:
lc_rand = Lightcurve(np.arange(1000), [500]*1000)
[13]:
lc_sum = lc + lc_rand
[14]:
# We can see the difference between the maximum and minimum values
print(max(lc_sum) - max(lc))
print(min(lc_sum) - min(lc))
500.00000000000006
500.0

## Negation¶

A negation operation on the lightcurve object inverts the count array from positive to negative values.

[15]:
lc_neg = -lc
[16]:
lc_sum = lc + lc_neg
[17]:
np.all(lc_sum.counts == 0)  # All the points on lc and lc_neg cancel each other
[17]:
True

## Indexing¶

Count value at a particular time can be obtained using indexing.

[18]:
lc[120]
[18]:
69.45098286882671

A Lightcurve can also be sliced to generate a new object.

[19]:
lc_sliced = lc[100:200]
[20]:
len(lc_sliced.counts)
[20]:
100

# Methods¶

## Concatenation¶

Two light curves can be combined into a single object using the join method. Note that both of them must not have overlapping time arrays.

[21]:
lc_1 = lc
[22]:
lc_2 = Lightcurve(np.arange(1000, 2000), np.random.rand(1000)*1000)
[23]:
lc_long = lc_1.join(lc_2)  # Or vice-versa
[24]:
print(len(lc_long))
2000

## Truncation¶

A light curve can also be truncated.

[25]:
lc_cut = lc_long.truncate(start=0, stop=1000)
[26]:
len(lc_cut)
[26]:
1000

Note : The start and stop values can also be given as time values.

[27]:
lc_cut = lc_long.truncate(start=500, stop=1500, method='time')
[28]:
lc_cut.time[0], lc_cut.time[-1]
[28]:
(500, 1499)

## Re-binning¶

The time resolution (dt) can also be changed to a larger value.

Note : 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.

[29]:
lc_rebinned = lc_long.rebin(2)
[30]:
print("Old time resolution = " + str(lc_long.dt))
print("Number of data points = " + str(lc_long.n))
print("New time resolution = " + str(lc_rebinned.dt))
print("Number of data points = " + str(lc_rebinned.n))
Old time resolution = 1.0
Number of data points = 2000
New time resolution = 2
Number of data points = 1000

## Sorting¶

A lightcurve can be sorted using the sort method. This function sorts time array and the counts array is changed accordingly.

[31]:
new_lc_long = lc_long[:]  # Copying into a new object
[32]:
new_lc_long = new_lc_long.sort(reverse=True)
[33]:
new_lc_long.time[0] == max(lc_long.time)
[33]:
True

You can sort counts array using sort_counts method which changes time array accordingly.

[34]:
new_lc = lc_long[:]
new_lc = new_lc.sort_counts()
new_lc.counts[-1] == max(lc_long.counts)
[34]:
True

## Plotting¶

A curve can be plotted with the plot method.

[35]:
lc.plot()

A plot can also be customized using several keyword arguments.

[36]:
lc.plot(labels=('Time', "Counts"),  # (xlabel, ylabel)
axis=(0, 1000, -50, 150),  # (xmin, xmax, ymin, ymax)
title="Random generated lightcurve",
marker='c:')  # c is for cyan and : is the marker style

The figure drawn can also be saved in a file using keywords arguments in the plot method itself.

[37]:
lc.plot(marker = 'k', save=True, filename="lightcurve.png")

Note : See utils.savefig function for more options on saving a file.

# Sample Data¶

Stingray also has a sample Lightcurve data which can be imported from within the library.

[38]:
from stingray import sampledata
[39]:
lc = sampledata.sample_data()
[40]:
lc.plot()