Skip to content

Commit

Permalink
Improve tests efficiency through the use of fixtures to load the data
Browse files Browse the repository at this point in the history
  • Loading branch information
iskandergaba committed Oct 10, 2024
1 parent c338616 commit 22fd308
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 168 deletions.
17 changes: 17 additions & 0 deletions tests/detectors/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest
from statsmodels.datasets import co2


@pytest.fixture(scope="module")
def co2_daily():
return co2.load().data.resample("D").mean().ffill()


@pytest.fixture(scope="module")
def co2_weekly():
return co2.load().data.resample("W").mean().ffill()


@pytest.fixture(scope="module")
def co2_monthly():
return co2.load().data.resample("ME").mean().ffill()
62 changes: 30 additions & 32 deletions tests/detectors/test_acf.py
Original file line number Diff line number Diff line change
@@ -1,119 +1,117 @@
from statsmodels.datasets import co2

from pyriodicity import ACFPeriodicityDetector


def test_co2_daily_acf_default():
data = co2.load().data.resample("D").mean().ffill()
def test_co2_daily_acf_default(co2_daily):
data = co2_daily
acf_detector = ACFPeriodicityDetector(data)
periods = acf_detector.fit()
assert len(periods) > 0


def test_co2_weekly_acf_default():
data = co2.load().data.resample("W").mean().ffill()
def test_co2_weekly_acf_default(co2_weekly):
data = co2_weekly
acf_detector = ACFPeriodicityDetector(data)
periods = acf_detector.fit()
assert len(periods) > 0


def test_co2_monthly_acf_default():
data = co2.load().data.resample("ME").mean().ffill()
def test_co2_monthly_acf_default(co2_monthly):
data = co2_monthly
acf_detector = ACFPeriodicityDetector(data)
periods = acf_detector.fit()
assert len(periods) > 0


def test_co2_daily_acf_max_period_count_one():
data = co2.load().data.resample("D").mean().ffill()
def test_co2_daily_acf_max_period_count_one(co2_daily):
data = co2_daily
acf_detector = ACFPeriodicityDetector(data)
periods = acf_detector.fit(max_period_count=1)
assert len(periods) == 1
assert 364 in periods


def test_co2_weekly_acf_max_period_count_one():
data = co2.load().data.resample("W").mean().ffill()
def test_co2_weekly_acf_max_period_count_one(co2_weekly):
data = co2_weekly
acf_detector = ACFPeriodicityDetector(data)
periods = acf_detector.fit(max_period_count=1)
assert len(periods) == 1
assert 52 in periods


def test_co2_monthly_acf_max_period_count_one():
data = co2.load().data.resample("ME").mean().ffill()
def test_co2_monthly_acf_max_period_count_one(co2_monthly):
data = co2_monthly
acf_detector = ACFPeriodicityDetector(data)
periods = acf_detector.fit(max_period_count=1)
assert len(periods) == 1
assert 12 in periods


def test_co2_daily_acf_max_period_count_one_correlation_func_spearman():
data = co2.load().data.resample("D").mean().ffill()
def test_co2_daily_acf_max_period_count_one_correlation_func_spearman(co2_daily):
data = co2_daily
acf_detector = ACFPeriodicityDetector(data)
periods = acf_detector.fit(max_period_count=1, correlation_func="spearman")
assert len(periods) == 1
assert 364 in periods


def test_co2_weekly_acf_max_period_count_one_correlation_func_spearman():
data = co2.load().data.resample("W").mean().ffill()
def test_co2_weekly_acf_max_period_count_one_correlation_func_spearman(co2_weekly):
data = co2_weekly
acf_detector = ACFPeriodicityDetector(data)
periods = acf_detector.fit(max_period_count=1, correlation_func="spearman")
assert len(periods) == 1
assert 52 in periods


def test_co2_monthly_acf_max_period_count_one_correlation_func_spearman():
data = co2.load().data.resample("ME").mean().ffill()
def test_co2_monthly_acf_max_period_count_one_correlation_func_spearman(co2_monthly):
data = co2_monthly
acf_detector = ACFPeriodicityDetector(data)
periods = acf_detector.fit(max_period_count=1, correlation_func="spearman")
assert len(periods) == 1
assert 12 in periods


def test_co2_daily_acf_max_period_count_one_correlation_func_kendall():
data = co2.load().data.resample("D").mean().ffill()
def test_co2_daily_acf_max_period_count_one_correlation_func_kendall(co2_daily):
data = co2_daily
acf_detector = ACFPeriodicityDetector(data)
periods = acf_detector.fit(max_period_count=1, correlation_func="kendall")
assert len(periods) == 1
assert 364 in periods


def test_co2_weekly_acf_max_period_count_one_correlation_func_kendall():
data = co2.load().data.resample("W").mean().ffill()
def test_co2_weekly_acf_max_period_count_one_correlation_func_kendall(co2_weekly):
data = co2_weekly
acf_detector = ACFPeriodicityDetector(data)
periods = acf_detector.fit(max_period_count=1, correlation_func="kendall")
assert len(periods) == 1
assert 52 in periods


def test_co2_monthly_acf_max_period_count_one_correlation_func_kendall():
data = co2.load().data.resample("ME").mean().ffill()
def test_co2_monthly_acf_max_period_count_one_correlation_func_kendall(co2_monthly):
data = co2_monthly
acf_detector = ACFPeriodicityDetector(data)
periods = acf_detector.fit(max_period_count=1, correlation_func="kendall")
assert len(periods) == 1
assert 12 in periods


def test_co2_daily_acf_max_period_count_one_window_func_blackman():
data = co2.load().data.resample("D").mean().ffill()
def test_co2_daily_acf_max_period_count_one_window_func_blackman(co2_daily):
data = co2_daily
acf_detector = ACFPeriodicityDetector(data)
periods = acf_detector.fit(max_period_count=1, window_func="blackman")
assert len(periods) == 1
assert 364 in periods


def test_co2_weekly_acf_max_period_count_one_window_func_blackman():
data = co2.load().data.resample("W").mean().ffill()
def test_co2_weekly_acf_max_period_count_one_window_func_blackman(co2_weekly):
data = co2_weekly
acf_detector = ACFPeriodicityDetector(data)
periods = acf_detector.fit(max_period_count=1, window_func="blackman")
assert len(periods) == 1
assert 52 in periods


def test_co2_monthly_acf_max_period_count_one_window_func_blackman():
data = co2.load().data.resample("ME").mean().ffill()
def test_co2_monthly_acf_max_period_count_one_window_func_blackman(co2_monthly):
data = co2_monthly
acf_detector = ACFPeriodicityDetector(data)
periods = acf_detector.fit(max_period_count=1, window_func="blackman")
assert len(periods) == 1
Expand Down
50 changes: 24 additions & 26 deletions tests/detectors/test_autoperiod.py
Original file line number Diff line number Diff line change
@@ -1,92 +1,90 @@
from statsmodels.datasets import co2

from pyriodicity import Autoperiod


def test_co2_daily_autoperiod_default():
data = co2.load().data.resample("D").mean().ffill()
def test_co2_daily_autoperiod_default(co2_daily):
data = co2_daily
autoperiod = Autoperiod(data)
periods = autoperiod.fit()
assert len(periods) > 0
assert 364 in periods


def test_co2_weekly_autoperiod_default():
data = co2.load().data.resample("W").mean().ffill()
def test_co2_weekly_autoperiod_default(co2_weekly):
data = co2_weekly
autoperiod = Autoperiod(data)
periods = autoperiod.fit()
assert len(periods) > 0
assert 52 in periods


def test_co2_monthly_autoperiod_default():
data = co2.load().data.resample("ME").mean().ffill()
def test_co2_monthly_autoperiod_default(co2_monthly):
data = co2_monthly
autoperiod = Autoperiod(data)
periods = autoperiod.fit()
assert len(periods) > 0
assert 12 in periods


def test_co2_daily_autoperiod_detrend_func_none():
data = co2.load().data.resample("D").mean().ffill()
def test_co2_daily_autoperiod_detrend_func_none(co2_daily):
data = co2_daily
autoperiod = Autoperiod(data)
periods = autoperiod.fit(detrend_func=None)
assert len(periods) == 0


def test_co2_weekly_autoperiod_detrend_func_none():
data = co2.load().data.resample("W").mean().ffill()
def test_co2_weekly_autoperiod_detrend_func_none(co2_weekly):
data = co2_weekly
autoperiod = Autoperiod(data)
periods = autoperiod.fit(detrend_func=None)
assert len(periods) == 0


def test_co2_monthly_autoperiod_detrend_func_none():
data = co2.load().data.resample("ME").mean().ffill()
def test_co2_monthly_autoperiod_detrend_func_none(co2_monthly):
data = co2_monthly
autoperiod = Autoperiod(data)
periods = autoperiod.fit(detrend_func=None)
assert len(periods) == 0


def test_co2_daily_autoperiod_detrend_func_constant():
data = co2.load().data.resample("D").mean().ffill()
def test_co2_daily_autoperiod_detrend_func_constant(co2_daily):
data = co2_daily
autoperiod = Autoperiod(data)
periods = autoperiod.fit(detrend_func="constant")
assert len(periods) == 0


def test_co2_weekly_autoperiod_detrend_func_constant():
data = co2.load().data.resample("W").mean().ffill()
def test_co2_weekly_autoperiod_detrend_func_constant(co2_weekly):
data = co2_weekly
autoperiod = Autoperiod(data)
periods = autoperiod.fit(detrend_func="constant")
assert len(periods) == 0


def test_co2_monthly_autoperiod_detrend_func_constant():
data = co2.load().data.resample("ME").mean().ffill()
def test_co2_monthly_autoperiod_detrend_func_constant(co2_monthly):
data = co2_monthly
autoperiod = Autoperiod(data)
periods = autoperiod.fit(detrend_func="constant")
assert len(periods) == 0


def test_co2_daily_autoperiod_detrend_func_constant_window_func_blackman():
data = co2.load().data.resample("D").mean().ffill()
def test_co2_daily_autoperiod_detrend_func_constant_window_func_blackman(co2_daily):
data = co2_daily
autoperiod = Autoperiod(data)
periods = autoperiod.fit(detrend_func="constant", window_func="blackman")
assert len(periods) > 0
assert 364 in periods


def test_co2_weekly_autoperiod_detrend_func_constant_window_func_blackman():
data = co2.load().data.resample("W").mean().ffill()
def test_co2_weekly_autoperiod_detrend_func_constant_window_func_blackman(co2_weekly):
data = co2_weekly
autoperiod = Autoperiod(data)
periods = autoperiod.fit(detrend_func="constant", window_func="blackman")
assert len(periods) > 0
assert 52 in periods


def test_co2_monthly_autoperiod_detrend_func_constant_window_func_blackman():
data = co2.load().data.resample("ME").mean().ffill()
def test_co2_monthly_autoperiod_detrend_func_constant_window_func_blackman(co2_monthly):
data = co2_monthly
autoperiod = Autoperiod(data)
periods = autoperiod.fit(detrend_func="constant", window_func="blackman")
assert len(periods) > 0
Expand Down
Loading

0 comments on commit 22fd308

Please sign in to comment.