Repository: StingraySoftware/stingray

Issue:
DynamicalPowerspectrum does not accept event lists
This is inconsistent with the other cross/power spectra behavior.

Discussion:

Patch:
diff --git a/stingray/powerspectrum.py b/stingray/powerspectrum.py
index 18a72819..652a1c19 100755
--- a/stingray/powerspectrum.py
+++ b/stingray/powerspectrum.py
@@ -467,7 +467,7 @@ class Powerspectrum(Crossspectrum):
         silent : bool, default False
             Silence the progress bars
         gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], ...]
-            Additional, optional, Good Time intervals, that get interesected with the 
+            Additional, optional, Good Time intervals, that get interesected with the
             GTIs of the input object.
         """
 
@@ -506,7 +506,7 @@ class Powerspectrum(Crossspectrum):
         silent : bool, default False
             Silence the progress bars
         gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], ...]
-            Additional, optional, Good Time intervals, that get interesected with the 
+            Additional, optional, Good Time intervals, that get interesected with the
             GTIs of the input object.
         """
 
@@ -890,8 +890,8 @@ class DynamicalPowerspectrum(AveragedPowerspectrum):
 
     Parameters
     ----------
-    lc : :class:`stingray.Lightcurve` object
-        The time series of which the Dynamical powerspectrum is
+    lc : :class:`stingray.Lightcurve` or :class:`stingray.EventList` object
+        The time series or event list of which the Dynamical powerspectrum is
         to be calculated.
 
     segment_size : float, default 1
@@ -935,6 +935,12 @@ class DynamicalPowerspectrum(AveragedPowerspectrum):
     """
 
     def __init__(self, lc, segment_size, norm="frac", gti=None, dt=None):
+        if isinstance(lc, EventList) and dt is None:
+            raise ValueError("To pass an input event lists, please specify dt")
+
+        if isinstance(lc, EventList):
+            lc = lc.to_lc(dt)
+
         if segment_size < 2 * lc.dt:
             raise ValueError("Length of the segment is too short to form a "
                              "light curve!")
@@ -1176,7 +1182,7 @@ def powerspectrum_from_events(events, dt, segment_size=None, norm="frac",
     silent : bool, default False
         Silence the progress bars
     gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], ...]
-        Additional, optional, Good Time intervals, that get interesected with the 
+        Additional, optional, Good Time intervals, that get interesected with the
         GTIs of the input object.
 
     Returns
@@ -1221,7 +1227,7 @@ def powerspectrum_from_lightcurve(lc, segment_size=None, norm="frac",
     silent : bool, default False
         Silence the progress bars
     gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], ...]
-        Additional, optional, Good Time intervals, that get interesected with the 
+        Additional, optional, Good Time intervals, that get interesected with the
         GTIs of the input object.
 
     Returns
@@ -1277,7 +1283,7 @@ def powerspectrum_from_lc_iterable(iter_lc, dt, segment_size=None, norm="frac",
     silent : bool, default False
         Silence the progress bars
     gti: [[gti0_0, gti0_1], [gti1_0, gti1_1], ...]
-        Good Time intervals. 
+        Good Time intervals.
 
     Returns
     -------


Test patch:
diff --git a/stingray/tests/test_powerspectrum.py b/stingray/tests/test_powerspectrum.py
index c8c82248..6fb9586b 100644
--- a/stingray/tests/test_powerspectrum.py
+++ b/stingray/tests/test_powerspectrum.py
@@ -910,6 +910,18 @@ class TestDynamicalPowerspectrum(object):
         with pytest.raises(ValueError):
             dps = DynamicalPowerspectrum(self.lc, segment_size=0)
 
+    def test_works_with_events(self):
+        lc = copy.deepcopy(self.lc)
+        lc.counts = np.floor(lc.counts)
+        ev = EventList.from_lc(lc)
+        dps = DynamicalPowerspectrum(lc, segment_size=10)
+        with pytest.raises(ValueError):
+            # Without dt, it fails
+            _ = DynamicalPowerspectrum(ev, segment_size=10)
+
+        dps_ev = DynamicalPowerspectrum(ev, segment_size=10, dt=self.lc.dt)
+        assert np.allclose(dps.dyn_ps, dps_ev.dyn_ps)
+
     def test_with_long_seg_size(self):
         with pytest.raises(ValueError):
             dps = DynamicalPowerspectrum(self.lc, segment_size=1000)