import pytest
import numpy as np
import scirs2
class TestAndersonDarling:
def test_anderson_darling_basic(self):
np.random.seed(42)
data = np.random.normal(0, 1, 100)
result = scirs2.anderson_darling_py(data)
assert "statistic" in result
assert "pvalue" in result
assert result["statistic"] >= 0
assert 0 <= result["pvalue"] <= 1
def test_anderson_darling_normal_data(self):
np.random.seed(42)
data = np.random.normal(0, 1, 200)
result = scirs2.anderson_darling_py(data)
assert 0 <= result["pvalue"] <= 1
def test_anderson_darling_non_normal_data(self):
np.random.seed(42)
data = np.random.uniform(-3, 3, 100)
result = scirs2.anderson_darling_py(data)
assert result["pvalue"] < 0.05
def test_anderson_darling_small_sample(self):
data = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0])
result = scirs2.anderson_darling_py(data)
assert "statistic" in result
assert "pvalue" in result
def test_anderson_darling_insufficient_data(self):
data = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
with pytest.raises(RuntimeError, match="Sample size must be at least 8"):
scirs2.anderson_darling_py(data)
class TestDAgostinoK2:
def test_dagostino_k2_basic(self):
np.random.seed(42)
data = np.random.normal(0, 1, 100)
result = scirs2.dagostino_k2_py(data)
assert "statistic" in result
assert "pvalue" in result
assert "statistic" in result
assert "pvalue" in result
def test_dagostino_k2_normal_data(self):
np.random.seed(42)
data = np.random.normal(0, 1, 200)
result = scirs2.dagostino_k2_py(data)
assert "statistic" in result
assert "pvalue" in result
def test_dagostino_k2_skewed_data(self):
np.random.seed(42)
data = np.random.exponential(1.0, 100)
result = scirs2.dagostino_k2_py(data)
assert "statistic" in result
assert "pvalue" in result
def test_dagostino_k2_minimum_sample_size(self):
data = np.random.normal(0, 1, 20)
result = scirs2.dagostino_k2_py(data)
assert "statistic" in result
assert "pvalue" in result
def test_dagostino_k2_insufficient_data(self):
data = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0])
with pytest.raises(RuntimeError, match="Sample size must be at least 20"):
scirs2.dagostino_k2_py(data)
def test_dagostino_k2_large_sample(self):
np.random.seed(42)
data = np.random.normal(0, 1, 500)
result = scirs2.dagostino_k2_py(data)
assert "statistic" in result
assert "pvalue" in result
class TestKS2Samp:
def test_ks_2samp_basic(self):
np.random.seed(42)
x = np.random.normal(0, 1, 50)
y = np.random.normal(0, 1, 50)
result = scirs2.ks_2samp_py(x, y)
assert "statistic" in result
assert "pvalue" in result
assert result["statistic"] >= 0
assert 0 <= result["pvalue"] <= 1
def test_ks_2samp_same_distribution(self):
np.random.seed(42)
x = np.random.normal(0, 1, 100)
y = np.random.normal(0, 1, 100)
result = scirs2.ks_2samp_py(x, y)
assert 0 <= result["pvalue"] <= 1
def test_ks_2samp_different_distributions(self):
np.random.seed(42)
x = np.random.normal(0, 1, 100)
y = np.random.normal(2, 1, 100)
result = scirs2.ks_2samp_py(x, y)
assert result["pvalue"] < 0.05
def test_ks_2samp_alternative_less(self):
np.random.seed(42)
x = np.random.normal(0, 1, 50)
y = np.random.normal(1, 1, 50)
result = scirs2.ks_2samp_py(x, y, alternative="less")
assert "statistic" in result
assert "pvalue" in result
def test_ks_2samp_alternative_greater(self):
np.random.seed(42)
x = np.random.normal(1, 1, 50)
y = np.random.normal(0, 1, 50)
result = scirs2.ks_2samp_py(x, y, alternative="greater")
assert "statistic" in result
assert "pvalue" in result
def test_ks_2samp_different_sample_sizes(self):
np.random.seed(42)
x = np.random.normal(0, 1, 30)
y = np.random.normal(0, 1, 70)
result = scirs2.ks_2samp_py(x, y)
assert "statistic" in result
assert "pvalue" in result
def test_ks_2samp_identical_samples(self):
data = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
result = scirs2.ks_2samp_py(data, data)
assert result["statistic"] == 0.0
assert result["pvalue"] > 0.9
class TestFriedman:
def test_friedman_basic(self):
data = np.array([
[5.1, 4.9, 5.3],
[6.2, 5.8, 6.1],
[5.7, 5.5, 5.9],
[4.8, 4.6, 5.0],
[5.3, 5.1, 5.5]
])
result = scirs2.friedman_py(data)
assert "statistic" in result
assert "pvalue" in result
assert result["statistic"] >= 0
assert 0 <= result["pvalue"] <= 1
def test_friedman_no_difference(self):
np.random.seed(42)
data = np.random.normal(5, 1, (10, 3))
result = scirs2.friedman_py(data)
assert result["pvalue"] > 0.05
def test_friedman_with_difference(self):
n_subjects = 20
data = np.zeros((n_subjects, 3))
np.random.seed(42)
for i in range(n_subjects):
baseline = np.random.normal(5, 1)
data[i, 0] = baseline + np.random.normal(0, 0.1)
data[i, 1] = baseline + 2 + np.random.normal(0, 0.1) data[i, 2] = baseline + 4 + np.random.normal(0, 0.1)
result = scirs2.friedman_py(data)
assert result["pvalue"] < 0.05
def test_friedman_four_treatments(self):
data = np.array([
[5.1, 5.2, 5.0, 5.3],
[6.2, 6.1, 6.0, 6.3],
[5.7, 5.8, 5.6, 5.9],
[4.8, 4.9, 4.7, 5.0],
[5.3, 5.4, 5.2, 5.5],
[6.0, 6.1, 5.9, 6.2]
])
result = scirs2.friedman_py(data)
assert "statistic" in result
assert "pvalue" in result
def test_friedman_many_subjects(self):
np.random.seed(42)
data = np.random.normal(5, 1, (50, 3))
result = scirs2.friedman_py(data)
assert "statistic" in result
assert "pvalue" in result
def test_friedman_minimum_requirements(self):
data = np.array([
[5.1, 5.3],
[6.2, 6.1]
])
result = scirs2.friedman_py(data)
assert "statistic" in result
assert "pvalue" in result
def test_friedman_insufficient_subjects(self):
data = np.array([[5.1, 5.3, 5.5]])
with pytest.raises(RuntimeError, match="At least 2"):
scirs2.friedman_py(data)
def test_friedman_insufficient_treatments(self):
data = np.array([
[5.1],
[6.2],
[5.7]
])
with pytest.raises(RuntimeError, match="At least 2"):
scirs2.friedman_py(data)
class TestNormalityComparison:
def test_all_normality_tests_on_normal_data(self):
np.random.seed(42)
data = np.random.normal(0, 1, 200)
shapiro_result = scirs2.shapiro_py(data)
anderson_result = scirs2.anderson_darling_py(data)
dagostino_result = scirs2.dagostino_k2_py(data)
assert "pvalue" in shapiro_result
assert "pvalue" in anderson_result
assert "pvalue" in dagostino_result
def test_all_normality_tests_on_non_normal_data(self):
np.random.seed(42)
data = np.random.uniform(-3, 3, 200)
shapiro_result = scirs2.shapiro_py(data)
anderson_result = scirs2.anderson_darling_py(data)
dagostino_result = scirs2.dagostino_k2_py(data)
assert "pvalue" in shapiro_result
assert "pvalue" in anderson_result
assert "pvalue" in dagostino_result
class TestNormalityEdgeCases:
def test_anderson_darling_with_ties(self):
data = np.array([1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, 5.0, 5.0])
result = scirs2.anderson_darling_py(data)
assert "statistic" in result
assert "pvalue" in result
def test_dagostino_k2_with_constant_data(self):
data = np.full(30, 5.0)
with pytest.raises(RuntimeError):
scirs2.dagostino_k2_py(data)
def test_ks_2samp_with_overlapping_samples(self):
np.random.seed(42)
x = np.random.normal(0, 1, 100)
y = np.random.normal(0.1, 1, 100)
result = scirs2.ks_2samp_py(x, y)
assert 0 <= result["pvalue"] <= 1