Start here to begin with Stingray.
In [1]:
import numpy as np
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')
In [2]:
from stingray import Lightcurve
A Lightcurve
object can be created in two ways :
Create 1000 time stamps
In [3]:
times = np.arange(1000)
times[:10]
Out[3]:
Create 1000 random count values between 0 to 100
In [4]:
counts = np.random.rand(1000)*100
counts[:10]
Out[4]:
Create a Lightcurve object with the times and counts array.
In [5]:
lc = Lightcurve(times, counts)
The number of data points can be counted with the len
function.
In [6]:
len(lc)
Out[6]:
In [7]:
arrivals = np.loadtxt("photon_arrivals.txt")
arrivals[:10]
Out[7]:
In [8]:
lc_new = Lightcurve.make_lightcurve(arrivals, 1)
The time bins and respective counts can be seen with lc.counts
and lc.time
In [9]:
lc_new.counts
Out[9]:
In [10]:
lc_new.time
Out[10]:
A Lightcurve object has the following properties :
time
: numpy array of time valuescounts
: numpy array of counts per bin valuescountrate
: numpy array of counts per second valuesn
: Number of data points in the lightcurvedt
: Time resolution of the light curvetseg
: Total duration of the light curvetstart
: Start time of the light curve
In [11]:
lc.n == len(lc)
Out[11]:
Two light curves can be summed up or subtracted from each other if they have same time arrays.
In [12]:
lc_rand = Lightcurve(np.arange(1000), [500]*1000)
In [13]:
lc_sum = lc + lc_rand
In [14]:
# We can see the difference between the maximum and minimum values
print(max(lc_sum) - max(lc))
print(min(lc_sum) - min(lc))
A negation operation on the lightcurve object inverts the count array from positive to negative values.
In [15]:
lc_neg = -lc
In [16]:
lc_sum = lc + lc_neg
In [17]:
np.all(lc_sum.counts == 0) # All the points on lc and lc_neg cancel each other
Out[17]:
Count value at a particular time can be obtained using indexing.
In [18]:
lc[120]
Out[18]:
A Lightcurve can also be sliced to generate a new object.
In [19]:
lc_sliced = lc[100:200]
In [20]:
len(lc_sliced.counts)
Out[20]:
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.
In [21]:
lc_1 = lc
In [22]:
lc_2 = Lightcurve(np.arange(1000, 2000), np.random.rand(1000)*1000)
In [23]:
lc_long = lc_1.join(lc_2) # Or vice-versa
In [24]:
print(len(lc_long))
A light curve can also be truncated.
In [25]:
lc_cut = lc_long.truncate(start=0, stop=1000)
In [26]:
len(lc_cut)
Out[26]:
Note : The start
and stop
values can also be given as time values.
In [27]:
lc_cut = lc_long.truncate(start=500, stop=1500, method='time')
In [28]:
lc_cut.time[0], lc_cut.time[-1]
Out[28]:
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.
In [29]:
lc_rebinned = lc_long.rebin(2)
In [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))
A lightcurve can be sorted using the sort
method. This function sorts time
array and the counts
array is changed accordingly.
In [31]:
new_lc_long = lc_long[:] # Copying into a new object
In [32]:
new_lc_long = new_lc_long.sort(reverse=True)
In [33]:
new_lc_long.time[0] == max(lc_long.time)
Out[33]:
You can sort counts
array using sort_counts
method which changes time
array accordingly.
In [34]:
new_lc = lc_long[:]
new_lc = new_lc.sort_counts()
new_lc.counts[-1] == max(lc_long.counts)
Out[34]:
A curve can be plotted with the plot
method.
In [35]:
lc.plot()
A plot can also be customized using several keyword arguments.
In [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.
In [37]:
lc.plot(marker = 'k', save=True, filename="lightcurve.png")
Note : See utils.savefig
function for more options on saving a file.
Stingray also has a sample Lightcurve
data which can be imported from within the library.
In [38]:
from stingray import sampledata
In [39]:
lc = sampledata.sample_data()
In [40]:
lc.plot()