Skip to content

Commit

Permalink
Improved Coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
pupperemeritus committed Aug 31, 2023
1 parent 6a66f26 commit a9d66a3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 66 deletions.
64 changes: 21 additions & 43 deletions stingray/lombscargle.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,19 @@ def __init__(
oversampling: int = 5,
):
self._type = None
good_input = data1 is not None and data2 is not None

if data1 is None and data2 is None:
if not skip_checks:
good_input = self.initial_checks(
data1=data1,
data2=data2,
norm=norm,
power_type=power_type,
dt=dt,
fullspec=fullspec,
min_freq=min_freq,
max_freq=max_freq,
df=df,
method=method,
oversampling=oversampling,
)
self._initialize_empty()
return

if dt is None:
if isinstance(data1, Lightcurve) or isinstance(data2, EventList):
dt = data1.dt
elif isinstance(data2, Lightcurve) or isinstance(data2, EventList):
dt = data2.dt
if dt is None:
raise ValueError("dt must be provided for EventLists")

if not skip_checks:
good_input = self.initial_checks(
data1=data1,
Expand All @@ -159,23 +154,15 @@ def __init__(
oversampling=oversampling,
)

if not good_input:
self._initialize_empty()
return
if dt is None:
if isinstance(data1, Lightcurve) or isinstance(data2, EventList):
dt = data1.dt
elif isinstance(data2, Lightcurve) or isinstance(data2, EventList):
dt = data2.dt
if dt is None:
raise ValueError("dt must be provided for EventLists")
if not good_input:
self._initialize_empty()
return

self.dt = dt
norm = norm.lower()
self.norm = norm
self.k = 1
self.df = df
if not good_input:
return self._initialize_empty()

if isinstance(data1, EventList):
self.lc1 = data1.to_lc(self.dt)
Expand All @@ -199,7 +186,7 @@ def __init__(
self._make_crossspectrum(
self.lc1, self.lc2, fullspec, method=method, oversampling=oversampling
)
if self.power_type == "abs":
if self.power_type == "absolute":
self.power = np.abs(self.power)
self.power_err = np.abs(self.power_err)
self.unnorm_power = np.abs(self.unnorm_power)
Expand Down Expand Up @@ -307,15 +294,6 @@ def _make_crossspectrum(self, lc1, lc2, fullspec, method, oversampling):
and Rybicki O(n*log(n))
"""
if self.lc2.mjdref != self.lc1.mjdref:
raise ValueError("MJDref is different in the two light curves")

if lc1.n != lc2.n:
raise StingrayError("Lightcurves do not have the same number of bins per segment.")

if not np.isclose(lc1.dt, lc2.dt, rtol=0.1 * lc1.dt / lc1.tseg):
raise StingrayError("Lightcurves do not have the same time binning dt.")

self.meancounts1 = lc1.meancounts
self.meancounts2 = lc2.meancounts

Expand Down Expand Up @@ -510,37 +488,37 @@ def time_lag(self):
"Object has no attribute named 'time_lag' ! Not applicable for unevenly sampled data"
)

def classical_significances(self, threshold=1, trial_correction=False):
def classical_significances(self):
"""Not applicable for unevenly sampled data"""
raise AttributeError(
"Object has no attribute named 'classical_significances' ! Not applicable for unevenly sampled data"
)

def from_time_array():
def from_time_array(self):
"""Not applicable for unevenly sampled data"""
raise AttributeError(
"Object has no attribute named 'from_time_array' ! Not applicable for unevenly sampled data"
)

def from_events():
def from_events(self):
"""Not applicable for unevenly sampled data"""
raise AttributeError(
"Object has no attribute named 'from_events' ! Not applicable for unevenly sampled data"
)

def from_lightcurve():
def from_lightcurve(self):
"""Not applicable for unevenly sampled data"""
raise AttributeError(
"Object has no attribute named 'from_lightcurve' ! Not applicable for unevenly sampled data"
)

def from_lc_iterable():
def from_lc_iterable(self):
"""Not applicable for unevenly sampled data"""
raise AttributeError(
"Object has no attribute named 'from_lc_iterable' ! Not applicable for unevenly sampled data"
)

def _initialize_from_any_input():
def _initialize_from_any_input(self):
"""Not required for unevenly sampled data"""
raise AttributeError("Object has no attribute named '_initialize_from_any_input' !")

Expand Down
52 changes: 29 additions & 23 deletions stingray/tests/test_lombscargle.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def setup_class(self):
@pytest.mark.parametrize("skip_checks", [True, False])
def test_initialize_empty(self, skip_checks):
lscs = LombScargleCrossspectrum(skip_checks=skip_checks)
assert lscs.freq is None
assert lscs.power is None
lscs.freq is None
lscs.power is None

def test_make_empty_crossspectrum(self):
lscs = LombScargleCrossspectrum()
Expand All @@ -54,31 +54,35 @@ def test_make_empty_crossspectrum(self):
assert lscs.oversampling is None
assert lscs.method is None

def test_bad_input(self):
with pytest.raises(TypeError):
lscs = LombScargleCrossspectrum(1, self.lc1)

def test_init_with_one_lc_none(self):
with pytest.raises(ValueError):
lscs = LombScargleCrossspectrum(self.lc1)
lscs = LombScargleCrossspectrum(self.lc1, None)

def test_init_with_norm_not_str(self):
with pytest.raises(TypeError):
lscs = LombScargleCrossspectrum(norm=1)
lscs = LombScargleCrossspectrum(self.lc1, self.lc2, norm=1)

def test_init_with_invalid_norm(self):
with pytest.raises(ValueError):
lscs = LombScargleCrossspectrum(norm="frabs")
lscs = LombScargleCrossspectrum(self.lc1, self.lc2, norm="frabs")

def test_init_with_power_type_not_str(self):
with pytest.raises(TypeError):
lscs = LombScargleCrossspectrum(power_type=3)
lscs = LombScargleCrossspectrum(self.lc1, self.lc2, power_type=3)

def test_init_with_invalid_power_type(self):
with pytest.raises(ValueError):
lscs = LombScargleCrossspectrum(power_type="reel")
lscs = LombScargleCrossspectrum(self.lc1, self.lc2, power_type="reel")

def test_init_with_wrong_lc_instance(self):
lc1_ = {"a": 1, "b": 2}
lc2_ = {"a": 1, "b": 2}
with pytest.raises(TypeError):
lscs = LombScargleCrossspectrum(lc1_, lc2_)
lscs = LombScargleCrossspectrum(lc1_, lc2_, dt=1)

def test_init_with_wrong_lc2_instance(self):
lc_ = {"a": 1, "b": 2}
Expand Down Expand Up @@ -110,13 +114,6 @@ def test_make_crossspectrum_diff_lc_stat(self):
cs = LombScargleCrossspectrum(self.lc1, lc_)
assert np.any(["different statistics" in r.message.args[0] for r in record])

def test_make_crossspectrum_diff_dt(self):
lc_ = Simulator(0.0002, 100, 100, 1, random_state=42, tstart=0).simulate(0)
with pytest.raises(
StingrayError, match="Lightcurves do not have the same time binning dt."
):
lscs = LombScargleCrossspectrum(self.lc1, lc_)

@pytest.mark.parametrize("power_type", ["real", "absolute", "all"])
def test_power_type(self, power_type):
lscs = LombScargleCrossspectrum(self.lc1, self.lc2, power_type=power_type)
Expand All @@ -142,11 +139,12 @@ def test_invalid_mixed_data(self):
with pytest.raises(ValueError):
lscs = LombScargleCrossspectrum(data2, self.lc1)

def test_diff_mjdref(self):
lc3 = copy.deepcopy(self.lc1)
lc3.mjdref += 1
with pytest.raises(ValueError):
lscs = LombScargleCrossspectrum(self.lc1, lc3)
def test_valid_mixed_data(self):
data2 = EventList(self.lc2.time, np.ones_like(self.lc2.time))
lscs = LombScargleCrossspectrum(self.lc1, data2)
assert lscs.power is not None
lscs2 = LombScargleCrossspectrum(data2, self.lc1)
assert lscs2.power is not None

def test_fullspec(self):
lscs = LombScargleCrossspectrum(self.lc1, self.lc2, fullspec=True)
Expand All @@ -158,7 +156,7 @@ def test_valid_method(self, method):
assert lscs.method == method

@pytest.mark.parametrize(
"func",
"func_name",
[
"phase_lag",
"time_lag",
Expand All @@ -170,9 +168,17 @@ def test_valid_method(self, method):
"_initialize_from_any_input ",
],
)
def test_raise_on_invalid_function(self, func):
def test_raise_on_invalid_function(self, func_name):
with pytest.raises(AttributeError):
lscs = LombScargleCrossspectrum(self.lc1, self.lc2).func()
lscs = LombScargleCrossspectrum(self.lc1, self.lc2)
func = getattr(lscs, func_name)
func()

def test_no_dt(self):
el1 = EventList(self.lc1.counts, self.lc1.time, dt=None)
el2 = EventList(self.lc2.counts, self.lc2.time, dt=None)
with pytest.raises(ValueError):
lscs = LombScargleCrossspectrum(el1, el2)


class TestLombScarglePowerspectrum:
Expand Down

0 comments on commit a9d66a3

Please sign in to comment.