Repository: StingraySoftware/stingray
Pull request #199 (2017-06-20 12:07:11)

To avoid confusion, use energy, not pha


Patch:
diff --git a/stingray/events.py b/stingray/events.py
index 15df218e..07ac2b93 100644
--- a/stingray/events.py
+++ b/stingray/events.py
@@ -19,7 +19,7 @@ __all__ = ['EventList']
 
 
 class EventList(object):
-    def __init__(self, time=None, pha=None, ncounts=None, mjdref=0, dt=0, notes="", 
+    def __init__(self, time=None, energy=None, ncounts=None, mjdref=0, dt=0, notes="",
             gti=None, pi=None):
         """
         Make an event list object from an array of time stamps
@@ -35,7 +35,7 @@ class EventList(object):
             The time resolution of the events. Only relevant when using events
             to produce light curves with similar bin time.
 
-        pha: iterable
+        energy: iterable
             A list of array of photon energy values
 
         mjdref : float
@@ -56,7 +56,7 @@ class EventList(object):
             The array of event arrival times, in seconds from the reference
             MJD (self.mjdref)
 
-        pha: numpy.ndarray
+        energy: numpy.ndarray
             The array of photon energy values
 
         ncounts: int
@@ -77,7 +77,7 @@ class EventList(object):
 
         """
         
-        self.pha = None if pha is None else np.array(pha)
+        self.energy = None if energy is None else np.array(energy)
         self.notes = notes
         self.dt = dt
         self.mjdref = mjdref
@@ -91,9 +91,9 @@ class EventList(object):
         else:
             self.time = None
 
-        if (time is not None) and (pha is not None):
-            if len(time) != len(pha):
-                raise ValueError('Lengths of time and pha must be equal.')
+        if (time is not None) and (energy is not None):
+            if len(time) != len(energy):
+                raise ValueError('Lengths of time and energy must be equal.')
 
     def to_lc(self, dt, tstart=None, tseg=None):
         """
@@ -253,10 +253,10 @@ class EventList(object):
 
         if isinstance(spectrum, list) or isinstance(spectrum, np.ndarray):
             
-            pha = np.array(spectrum)[0]
+            energy = np.array(spectrum)[0]
             fluxes = np.array(spectrum)[1]
 
-            if not isinstance(pha, np.ndarray):
+            if not isinstance(energy, np.ndarray):
                 raise IndexError("Spectrum must be a 2-d array or list")
         
         else:
@@ -272,7 +272,7 @@ class EventList(object):
         R = ra.uniform(0, 1, self.ncounts)
 
         # Assign energies to events corresponding to the random numbers drawn
-        self.pha = np.array([pha[np.argwhere(cum_prob == 
+        self.energy = np.array([energy[np.argwhere(cum_prob ==
             min(cum_prob[(cum_prob - r) > 0]))] for r in R])
 
     def join(self, other):
@@ -331,16 +331,16 @@ class EventList(object):
             ev_new.pi = np.concatenate([self.pi, other.pi])
             ev_new.pi = ev_new.pi[order]
 
-        if (self.pha is None) and (other.pha is None):
-            ev_new.pha = None
-        elif (self.pha is None) or (other.pha is None):
-            self.pha = assign_value_if_none(self.pha, np.zeros_like(self.time))
-            other.pha = assign_value_if_none(other.pha,
+        if (self.energy is None) and (other.energy is None):
+            ev_new.energy = None
+        elif (self.energy is None) or (other.energy is None):
+            self.energy = assign_value_if_none(self.energy, np.zeros_like(self.time))
+            other.energy = assign_value_if_none(other.energy,
                                              np.zeros_like(other.time))
 
-        if (self.pha is not None) and (other.pha is not None):
-            ev_new.pha = np.concatenate([self.pha, other.pha])
-            ev_new.pha = ev_new.pha[order]
+        if (self.energy is not None) and (other.energy is not None):
+            ev_new.energy = np.concatenate([self.energy, other.energy])
+            ev_new.energy = ev_new.energy[order]
 
         if self.gti is None and other.gti is not None and len(self.time) > 0:
             self.gti = \
@@ -383,7 +383,7 @@ class EventList(object):
         -------
         ev: `EventList` object
         """
-        attributes = ['time', 'pha', 'ncounts', 'mjdref', 'dt', 
+        attributes = ['time', 'energy', 'ncounts', 'mjdref', 'dt',
                 'notes', 'gti', 'pi']
         data = read(filename, format_, cols=attributes)
 
@@ -405,7 +405,7 @@ class EventList(object):
                 else:
                     values.append(None)
                     
-            return EventList(time=values[0], pha=values[1], ncounts=values[2], 
+            return EventList(time=values[0], energy=values[1], ncounts=values[2],
                 mjdref=values[3], dt=values[4], notes=values[5], gti=values[6], pi=values[7])
 
         elif format_ == 'pickle':
diff --git a/stingray/varenergyspectrum.py b/stingray/varenergyspectrum.py
index ecf1fa6d..abef3c33 100644
--- a/stingray/varenergyspectrum.py
+++ b/stingray/varenergyspectrum.py
@@ -99,8 +99,8 @@ class VarEnergySpectrum(object):
             energies1 = self.events1.pi
             energies2 = self.events2.pi
         else:
-            energies2 = self.events2.pha
-            energies1 = self.events1.pha
+            energies2 = self.events2.energy
+            energies1 = self.events1.energy
 
         gti = cross_two_gtis(self.events1.gti, self.events2.gti)
 


Test patch:
diff --git a/stingray/tests/test_events.py b/stingray/tests/test_events.py
index 63823177..13915309 100644
--- a/stingray/tests/test_events.py
+++ b/stingray/tests/test_events.py
@@ -28,10 +28,10 @@ class TestEvents(object):
 
     def test_inequal_length(self):
         """Check that exception is raised in case of
-        disparity in length of 'time' and 'pha'
+        disparity in length of 'time' and 'energy'
         """
         with pytest.raises(ValueError):
-            EventList(time=[1, 2, 3], pha=[10, 12])    
+            EventList(time=[1, 2, 3], energy=[10, 12])
 
     def test_to_lc(self):
         """Create a light curve from event list."""
@@ -101,17 +101,17 @@ class TestEvents(object):
         ev = EventList()
         ev.simulate_energies(self.spectrum)
 
-    def test_compare_pha(self):
+    def test_compare_energy(self):
         """Compare the simulated energy distribution to actual distribution.
         """
         fluxes = np.array(self.spectrum[1])
         ev = EventList(ncounts=1000)
         ev.simulate_energies(self.spectrum)
-        pha = [int(p) for p in ev.pha]
+        energy = [int(p) for p in ev.energy]
 
-        # Histogram pha to get shape approximation
-        gen_pha = ((np.array(pha) - 1) / 1).astype(int)
-        lc = np.bincount(pha)
+        # Histogram energy to get shape approximation
+        gen_energy = ((np.array(energy) - 1) / 1).astype(int)
+        lc = np.bincount(energy)
 
         # Remove first entry as it contains occurences of '0' element
         lc = lc[1:len(lc)]
@@ -151,7 +151,7 @@ class TestEvents(object):
         assert ev_new.time == None
         assert ev_new.gti == None
         assert ev_new.pi == None
-        assert ev_new.pha == None
+        assert ev_new.energy == None
 
         ev = EventList(time=[1, 2, 3])
         ev_other = EventList([])
@@ -168,12 +168,12 @@ class TestEvents(object):
         ev_new = ev.join(ev_other)
         assert ev_new.dt == 3
 
-    def test_join_without_pha(self):
-        ev = EventList(time=[1, 2, 3], pha=[3, 3, 3])
+    def test_join_without_energy(self):
+        ev = EventList(time=[1, 2, 3], energy=[3, 3, 3])
         ev_other = EventList(time=[4, 5])
         ev_new = ev.join(ev_other)
 
-        assert np.all(ev_new.pha == [3, 3, 3, 0, 0])
+        assert np.all(ev_new.energy == [3, 3, 3, 0, 0])
 
     def test_join_without_pi(self):
         ev = EventList(time=[1, 2, 3], pi=[3, 3, 3])
@@ -205,14 +205,14 @@ class TestEvents(object):
         """Join two overlapping event lists.
         """
         ev = EventList(time=[1, 1, 2, 3, 4], 
-                        pha=[3, 4, 7, 4, 3], gti=[[1, 2],[3, 4]])
+                        energy=[3, 4, 7, 4, 3], gti=[[1, 2],[3, 4]])
         ev_other = EventList(time=[5, 6, 6, 7, 10], 
-                            pha=[4, 3, 8, 1, 2], gti=[[6, 7]])
+                            energy=[4, 3, 8, 1, 2], gti=[[6, 7]])
         ev_new = ev.join(ev_other)
 
         assert (ev_new.time ==
                 np.array([1, 1, 2, 3, 4, 5, 6, 6, 7, 10])).all()
-        assert (ev_new.pha ==
+        assert (ev_new.energy ==
                 np.array([3, 4, 7, 4, 3, 4, 3, 8, 1, 2])).all()
         assert (ev_new.gti == 
                 np.array([[1, 2],[3, 4],[6, 7]])).all()
@@ -221,14 +221,14 @@ class TestEvents(object):
         """Join two non-overlapping event lists.
         """
         ev = EventList(time=[1, 1, 10, 6, 5], 
-                        pha=[10, 6, 3, 11, 2], gti=[[1, 3],[5, 6]])
+                        energy=[10, 6, 3, 11, 2], gti=[[1, 3],[5, 6]])
         ev_other = EventList(time=[5, 7, 6, 6, 10], 
-                            pha=[2, 3, 8, 1, 2], gti=[[5, 7],[8, 10]])
+                            energy=[2, 3, 8, 1, 2], gti=[[5, 7],[8, 10]])
         ev_new = ev.join(ev_other)
 
         assert (ev_new.time ==
                 np.array([1, 1, 5, 5, 6, 6, 6, 7, 10, 10])).all()
-        assert (ev_new.pha ==
+        assert (ev_new.energy ==
                 np.array([10, 6, 2, 2, 11, 8, 1, 3, 3, 2])).all()
         assert (ev_new.gti == np.array([[5, 6]])).all()
 
diff --git a/stingray/tests/test_varenergyspectrum.py b/stingray/tests/test_varenergyspectrum.py
index d9c8fcc7..8c21d282 100644
--- a/stingray/tests/test_varenergyspectrum.py
+++ b/stingray/tests/test_varenergyspectrum.py
@@ -23,7 +23,7 @@ class TestVarEnergySpectrum(object):
         alltimes = np.random.uniform(tstart, tend, nphot)
         alltimes.sort()
         cls.events = EventList(alltimes,
-                               pha=np.random.uniform(0.3, 12, nphot),
+                               energy=np.random.uniform(0.3, 12, nphot),
                                gti = [[tstart, tend]])
         cls.vespec = DummyVarEnergy(cls.events, [0., 10000],
                                     (0.5, 5, 10, "lin"), [0.3, 10],
@@ -44,7 +44,7 @@ class TestVarEnergySpectrum(object):
 
     def test_ref_band_none(self):
         events = EventList([0.09, 0.21, 0.23, 0.32, 0.4, 0.54],
-                           pha=[0,0,0,0,1,1],
+                           energy=[0,0,0,0,1,1],
                            gti=[[0, 0.65]])
         vespec = DummyVarEnergy(events, [0., 10000],
                                 (0, 1, 2, "lin"),
@@ -52,7 +52,7 @@ class TestVarEnergySpectrum(object):
 
     def test_energy_spec_wrong_list_not_tuple(self):
         events = EventList([0.09, 0.21, 0.23, 0.32, 0.4, 0.54],
-                           pha=[0, 0, 0, 0, 1, 1],
+                           energy=[0, 0, 0, 0, 1, 1],
                            gti=[[0, 0.65]])
         # Test using a list instead of tuple
         # with pytest.raises(ValueError):
@@ -62,7 +62,7 @@ class TestVarEnergySpectrum(object):
 
     def test_energy_spec_wrong_str(self):
         events = EventList([0.09, 0.21, 0.23, 0.32, 0.4, 0.54],
-                           pha=[0, 0, 0, 0, 1, 1],
+                           energy=[0, 0, 0, 0, 1, 1],
                            gti=[[0, 0.65]])
         # Test using a list instead of tuple
         with pytest.raises(ValueError):
@@ -72,7 +72,7 @@ class TestVarEnergySpectrum(object):
 
     def test_construct_lightcurves(self):
         events = EventList([0.09, 0.21, 0.23, 0.32, 0.4, 0.54],
-                           pha=[0,0,0,0,1,1],
+                           energy=[0,0,0,0,1,1],
                            gti=[[0, 0.65]])
         vespec = DummyVarEnergy(events, [0., 10000],
                                 (0, 1, 2, "lin"), [0.5, 1.1],
@@ -85,7 +85,7 @@ class TestVarEnergySpectrum(object):
 
     def test_construct_lightcurves_no_exclude(self):
         events = EventList([0.09, 0.21, 0.23, 0.32, 0.4, 0.54],
-                           pha=[0,0,0,0,1,1],
+                           energy=[0,0,0,0,1,1],
                            gti=[[0, 0.65]])
 
         vespec = DummyVarEnergy(events, [0., 10000],
@@ -121,8 +121,8 @@ class TestRMSEnergySpectrum(object):
         test_ev1, test_ev2 = EventList(), EventList()
         test_ev1.simulate_times(test_lc)
         test_ev2.simulate_times(test_lc)
-        test_ev1.pha = np.random.uniform(0.3, 12, len(test_ev1.time))
-        test_ev2.pha = np.random.uniform(0.3, 12, len(test_ev2.time))
+        test_ev1.energy = np.random.uniform(0.3, 12, len(test_ev1.time))
+        test_ev2.energy = np.random.uniform(0.3, 12, len(test_ev2.time))
 
         cls.rms = RmsEnergySpectrum(test_ev1, [0., 100],
                                     (0.3, 12, 5, "lin"),
@@ -156,8 +156,8 @@ class TestLagEnergySpectrum(object):
         test_ev1, test_ev2 = EventList(), EventList()
         test_ev1.simulate_times(test_lc1)
         test_ev2.simulate_times(test_lc2)
-        test_ev1.pha = np.random.uniform(0.3, 9, len(test_ev1.time))
-        test_ev2.pha = np.random.uniform(9, 12, len(test_ev2.time))
+        test_ev1.energy = np.random.uniform(0.3, 9, len(test_ev1.time))
+        test_ev2.energy = np.random.uniform(9, 12, len(test_ev2.time))
 
         cls.lag = LagEnergySpectrum(test_ev1, [0., 0.5],
                                     (0.3, 9, 4, "lin"), [9, 12],