Skip to content

Commit

Permalink
Rename functions _from_events -> _from_timeseries
Browse files Browse the repository at this point in the history
  • Loading branch information
matteobachetti committed Feb 19, 2024
1 parent 812b8f9 commit 720b50f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 34 deletions.
6 changes: 3 additions & 3 deletions stingray/crossspectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .gti import cross_two_gtis, time_intervals_from_gtis
from .lightcurve import Lightcurve
from .fourier import avg_cs_from_iterables, error_on_averaged_cross_spectrum
from .fourier import avg_cs_from_events, poisson_level
from .fourier import avg_cs_from_timeseries, poisson_level
from .fourier import normalize_periodograms, raw_coherence
from .fourier import get_flux_iterable_from_segments, power_color
from .fourier import get_rms_from_unnorm_periodogram
Expand Down Expand Up @@ -2456,7 +2456,7 @@ def crossspectrum_from_time_array(
force_averaged = segment_size is not None
# Suppress progress bar for single periodogram
silent = silent or (segment_size is None)
results = avg_cs_from_events(
results = avg_cs_from_timeseries(
times1,
times2,
gti,
Expand Down Expand Up @@ -2696,7 +2696,7 @@ def crossspectrum_from_timeseries(
err1 = getattr(ts1, error_flux_attr)
err2 = getattr(ts2, error_flux_attr)

results = avg_cs_from_events(
results = avg_cs_from_timeseries(
ts1.time,
ts2.time,
gti,
Expand Down
19 changes: 17 additions & 2 deletions stingray/fourier.py
Original file line number Diff line number Diff line change
Expand Up @@ -2243,7 +2243,15 @@ def local_show_progress(a):
return results


def avg_pds_from_events(
def avg_pds_from_events(*args, **kwargs):
warnings.warn(
"avg_pds_from_events is deprecated, use avg_cs_from_timeseries instead", DeprecationWarning
)

return avg_pds_from_timeseries(*args, **kwargs)


def avg_pds_from_timeseries(
times,
gti,
segment_size,
Expand Down Expand Up @@ -2333,7 +2341,14 @@ def avg_pds_from_events(
return cross


def avg_cs_from_events(
def avg_cs_from_events(*args, **kwargs):
warnings.warn(
"avg_cs_from_events is deprecated, use avg_cs_from_timeseries instead", DeprecationWarning
)
return avg_cs_from_timeseries(*args, **kwargs)


def avg_cs_from_timeseries(
times1,
times2,
gti,
Expand Down
8 changes: 4 additions & 4 deletions stingray/powerspectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from .lightcurve import Lightcurve
from .fourier import avg_pds_from_iterable, unnormalize_periodograms
from .fourier import avg_pds_from_events
from .fourier import avg_pds_from_timeseries
from .fourier import get_flux_iterable_from_segments
from .fourier import poisson_level
from .fourier import get_rms_from_unnorm_periodogram
Expand Down Expand Up @@ -1134,7 +1134,7 @@ def powerspectrum_from_time_array(
force_averaged = segment_size is not None
# Suppress progress bar for single periodogram
silent = silent or (segment_size is None)
table = avg_pds_from_events(
table = avg_pds_from_timeseries(
times,
gti,
segment_size,
Expand Down Expand Up @@ -1267,7 +1267,7 @@ def powerspectrum_from_lightcurve(
if gti is None:
gti = lc.gti

table = avg_pds_from_events(
table = avg_pds_from_timeseries(
lc.time,
gti,
segment_size,
Expand Down Expand Up @@ -1347,7 +1347,7 @@ def powerspectrum_from_timeseries(
if error_flux_attr is not None:
err = getattr(ts, error_flux_attr)

results = avg_pds_from_events(
results = avg_pds_from_timeseries(
ts.time,
gti,
segment_size,
Expand Down
61 changes: 45 additions & 16 deletions stingray/tests/test_fourier.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from stingray.fourier import fft, fftfreq, normalize_abs, normalize_frac, poisson_level
from stingray.fourier import (
get_flux_iterable_from_segments,
avg_pds_from_timeseries,
avg_cs_from_timeseries,
avg_pds_from_events,
avg_cs_from_events,
)
Expand Down Expand Up @@ -91,7 +93,19 @@ def test_avg_pds_imperfect_lc_size():
gti = np.asarray([[-0.5, 99.5]])
segment_size = 5.99
dt = 1
res = avg_pds_from_events(times, gti, segment_size, dt, fluxes=fluxes)
res = avg_pds_from_timeseries(times, gti, segment_size, dt, fluxes=fluxes)
assert res.meta["segment_size"] == 6
assert res.meta["dt"] == 1


def test_avg_pds_from_events_warns():
times = np.arange(100)
fluxes = np.ones(100).astype(float)
gti = np.asarray([[-0.5, 99.5]])
segment_size = 5.99
dt = 1
with pytest.warns(DeprecationWarning, match="avg_pds_from_events is deprecated"):
res = avg_pds_from_events(times, gti, segment_size, dt, fluxes=fluxes)
assert res.meta["segment_size"] == 6
assert res.meta["dt"] == 1

Expand All @@ -103,13 +117,28 @@ def test_avg_cs_imperfect_lc_size():
gti = np.asarray([[-0.5, 99.5]])
segment_size = 5.99
dt = 1
res = avg_cs_from_events(
res = avg_cs_from_timeseries(
times1, times2, gti, segment_size, dt, fluxes1=fluxes1, fluxes2=fluxes2
)
assert res.meta["segment_size"] == 6
assert res.meta["dt"] == 1


def test_avg_cs_from_events_warns():
times1 = times2 = np.arange(100)
fluxes1 = np.ones(100).astype(float)
fluxes2 = np.ones(100).astype(float)
gti = np.asarray([[-0.5, 99.5]])
segment_size = 5.99
dt = 1
with pytest.warns(DeprecationWarning, match="avg_cs_from_events is deprecated"):
res = avg_cs_from_events(
times1, times2, gti, segment_size, dt, fluxes1=fluxes1, fluxes2=fluxes2
)
assert res.meta["segment_size"] == 6
assert res.meta["dt"] == 1


class TestCoherence(object):
@classmethod
def setup_class(cls):
Expand Down Expand Up @@ -244,15 +273,15 @@ def test_fts_from_segments_cts_and_events_are_equal(self):

def test_avg_pds_bad_input(self):
times = np.sort(rng.uniform(0, 1000, 1))
out_ev = avg_pds_from_events(times, self.gti, self.segment_size, self.dt)
out_ev = avg_pds_from_timeseries(times, self.gti, self.segment_size, self.dt)
assert out_ev is None

@pytest.mark.parametrize("return_subcs", [True, False])
@pytest.mark.parametrize("return_auxil", [True, False])
def test_avg_cs_bad_input(self, return_auxil, return_subcs):
times1 = np.sort(rng.uniform(0, 1000, 1))
times2 = np.sort(rng.uniform(0, 1000, 1))
out_ev = avg_cs_from_events(
out_ev = avg_cs_from_timeseries(
times1,
times2,
self.gti,
Expand All @@ -265,7 +294,7 @@ def test_avg_cs_bad_input(self, return_auxil, return_subcs):

@pytest.mark.parametrize("norm", ["frac", "abs", "none", "leahy"])
def test_avg_pds_use_common_mean_similar_stats(self, norm):
out_comm = avg_pds_from_events(
out_comm = avg_pds_from_timeseries(
self.times,
self.gti,
self.segment_size,
Expand All @@ -275,7 +304,7 @@ def test_avg_pds_use_common_mean_similar_stats(self, norm):
silent=True,
fluxes=None,
)
out = avg_pds_from_events(
out = avg_pds_from_timeseries(
self.times,
self.gti,
self.segment_size,
Expand All @@ -289,7 +318,7 @@ def test_avg_pds_use_common_mean_similar_stats(self, norm):

@pytest.mark.parametrize("norm", ["frac", "abs", "none", "leahy"])
def test_avg_cs_use_common_mean_similar_stats(self, norm):
out_comm = avg_cs_from_events(
out_comm = avg_cs_from_timeseries(
self.times,
self.times2,
self.gti,
Expand All @@ -300,7 +329,7 @@ def test_avg_cs_use_common_mean_similar_stats(self, norm):
silent=True,
return_subcs=True,
)
out = avg_cs_from_events(
out = avg_cs_from_timeseries(
self.times,
self.times2,
self.gti,
Expand Down Expand Up @@ -328,7 +357,7 @@ def test_avg_cs_use_common_mean_similar_stats(self, norm):
@pytest.mark.parametrize("use_common_mean", [True, False])
@pytest.mark.parametrize("norm", ["frac", "abs", "none", "leahy"])
def test_avg_pds_cts_and_events_are_equal(self, norm, use_common_mean):
out_ev = avg_pds_from_events(
out_ev = avg_pds_from_timeseries(
self.times,
self.gti,
self.segment_size,
Expand All @@ -339,7 +368,7 @@ def test_avg_pds_cts_and_events_are_equal(self, norm, use_common_mean):
fluxes=None,
return_subcs=True,
)
out_ct = avg_pds_from_events(
out_ct = avg_pds_from_timeseries(
self.bin_times,
self.gti,
self.segment_size,
Expand All @@ -355,7 +384,7 @@ def test_avg_pds_cts_and_events_are_equal(self, norm, use_common_mean):
@pytest.mark.parametrize("use_common_mean", [True, False])
@pytest.mark.parametrize("norm", ["frac", "abs", "none", "leahy"])
def test_avg_pds_cts_and_err_and_events_are_equal(self, norm, use_common_mean):
out_ev = avg_pds_from_events(
out_ev = avg_pds_from_timeseries(
self.times,
self.gti,
self.segment_size,
Expand All @@ -366,7 +395,7 @@ def test_avg_pds_cts_and_err_and_events_are_equal(self, norm, use_common_mean):
fluxes=None,
return_subcs=True,
)
out_ct = avg_pds_from_events(
out_ct = avg_pds_from_timeseries(
self.bin_times,
self.gti,
self.segment_size,
Expand All @@ -389,7 +418,7 @@ def test_avg_pds_cts_and_err_and_events_are_equal(self, norm, use_common_mean):
@pytest.mark.parametrize("use_common_mean", [True, False])
@pytest.mark.parametrize("norm", ["frac", "abs", "none", "leahy"])
def test_avg_cs_cts_and_events_are_equal(self, norm, use_common_mean):
out_ev = avg_cs_from_events(
out_ev = avg_cs_from_timeseries(
self.times,
self.times2,
self.gti,
Expand All @@ -399,7 +428,7 @@ def test_avg_cs_cts_and_events_are_equal(self, norm, use_common_mean):
use_common_mean=use_common_mean,
silent=False,
)
out_ct = avg_cs_from_events(
out_ct = avg_cs_from_timeseries(
self.bin_times,
self.bin_times,
self.gti,
Expand All @@ -419,7 +448,7 @@ def test_avg_cs_cts_and_events_are_equal(self, norm, use_common_mean):
@pytest.mark.parametrize("use_common_mean", [True, False])
@pytest.mark.parametrize("norm", ["frac", "abs", "none", "leahy"])
def test_avg_cs_cts_and_err_and_events_are_equal(self, norm, use_common_mean):
out_ev = avg_cs_from_events(
out_ev = avg_cs_from_timeseries(
self.times,
self.times2,
self.gti,
Expand All @@ -429,7 +458,7 @@ def test_avg_cs_cts_and_err_and_events_are_equal(self, norm, use_common_mean):
use_common_mean=use_common_mean,
silent=False,
)
out_ct = avg_cs_from_events(
out_ct = avg_cs_from_timeseries(
self.bin_times,
self.bin_times,
self.gti,
Expand Down
23 changes: 14 additions & 9 deletions stingray/varenergyspectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
from stingray.lightcurve import Lightcurve
from stingray.utils import assign_value_if_none, simon, excess_variance, show_progress

from stingray.fourier import avg_cs_from_events, avg_pds_from_events, fftfreq, get_average_ctrate
from stingray.fourier import (
avg_cs_from_timeseries,
avg_pds_from_timeseries,
fftfreq,
get_average_ctrate,
)
from stingray.fourier import poisson_level, error_on_averaged_cross_spectrum, cross_to_covariance
from abc import ABCMeta, abstractmethod

Expand Down Expand Up @@ -530,7 +535,7 @@ def _spectrum_function(self):
sub2_power_noise = poisson_level(norm="abs", meanrate=countrate_sub2)

# Calculate the cross spectrum
results = avg_cs_from_events(
results = avg_cs_from_timeseries(

Check warning on line 538 in stingray/varenergyspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/varenergyspectrum.py#L538

Added line #L538 was not covered by tests
sub_events,
sub_events2,
self.gti,
Expand All @@ -550,7 +555,7 @@ def _spectrum_function(self):
delta_nu_after_mean * np.sqrt(sub_power_noise * sub2_power_noise)
)
else:
results = avg_pds_from_events(
results = avg_pds_from_timeseries(

Check warning on line 558 in stingray/varenergyspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/varenergyspectrum.py#L558

Added line #L558 was not covered by tests
sub_events, self.gti, self.segment_size, self.bin_time, silent=True, norm="abs"
)
if results is None:
Expand Down Expand Up @@ -804,7 +809,7 @@ def _spectrum_function(self):
ref_events = self._get_times_from_energy_range(self.events2, self.ref_band[0])

# Calculate the PDS in the reference band. Needed to calculate errors.
results = avg_pds_from_events(
results = avg_pds_from_timeseries(

Check warning on line 812 in stingray/varenergyspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/varenergyspectrum.py#L812

Added line #L812 was not covered by tests
ref_events, self.gti, self.segment_size, self.bin_time, silent=True, norm="none"
)

Expand All @@ -827,7 +832,7 @@ def _spectrum_function(self):
# Extract the photon arrival times from the subject band
sub_events = self._get_times_from_energy_range(self.events1, eint)

results_cross = avg_cs_from_events(
results_cross = avg_cs_from_timeseries(

Check warning on line 835 in stingray/varenergyspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/varenergyspectrum.py#L835

Added line #L835 was not covered by tests
sub_events,
ref_events,
self.gti,
Expand All @@ -837,7 +842,7 @@ def _spectrum_function(self):
norm="none",
)

results_ps = avg_pds_from_events(
results_ps = avg_pds_from_timeseries(

Check warning on line 845 in stingray/varenergyspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/varenergyspectrum.py#L845

Added line #L845 was not covered by tests
sub_events, self.gti, self.segment_size, self.bin_time, silent=True, norm="none"
)

Expand Down Expand Up @@ -982,7 +987,7 @@ def _spectrum_function(self):
countrate_ref = get_average_ctrate(ref_events, self.gti, self.segment_size)
ref_power_noise = poisson_level(norm="abs", meanrate=countrate_ref)

results = avg_pds_from_events(
results = avg_pds_from_timeseries(

Check warning on line 990 in stingray/varenergyspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/varenergyspectrum.py#L990

Added line #L990 was not covered by tests
ref_events, self.gti, self.segment_size, self.bin_time, silent=True, norm="abs"
)
freq = results["freq"]
Expand All @@ -1004,7 +1009,7 @@ def _spectrum_function(self):
countrate_sub = get_average_ctrate(sub_events, self.gti, self.segment_size)
sub_power_noise = poisson_level(norm="abs", meanrate=countrate_sub)

results_cross = avg_cs_from_events(
results_cross = avg_cs_from_timeseries(

Check warning on line 1012 in stingray/varenergyspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/varenergyspectrum.py#L1012

Added line #L1012 was not covered by tests
sub_events,
ref_events,
self.gti,
Expand All @@ -1014,7 +1019,7 @@ def _spectrum_function(self):
norm="abs",
)

results_ps = avg_pds_from_events(
results_ps = avg_pds_from_timeseries(

Check warning on line 1022 in stingray/varenergyspectrum.py

View check run for this annotation

Codecov / codecov/patch

stingray/varenergyspectrum.py#L1022

Added line #L1022 was not covered by tests
sub_events, self.gti, self.segment_size, self.bin_time, silent=True, norm="abs"
)

Expand Down

0 comments on commit 720b50f

Please sign in to comment.