import pytest
import numpy as np
import scirs2
class TestWilcoxonTest:
def test_wilcoxon_basic(self):
x = np.array([1.2, 2.3, 3.1, 4.5, 5.2, 6.1, 7.3, 8.2])
y = np.array([1.5, 2.1, 3.4, 4.2, 5.5, 6.3, 7.1, 8.5])
result = scirs2.wilcoxon_py(x, y)
assert "statistic" in result
assert "pvalue" in result
assert result["statistic"] >= 0
assert 0 <= result["pvalue"] <= 1
def test_wilcoxon_zero_differences(self):
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([1.5, 2.0, 3.5, 4.0, 5.5])
result = scirs2.wilcoxon_py(x, y, zero_method="wilcox")
assert "statistic" in result
assert "pvalue" in result
def test_wilcoxon_zero_method_pratt(self):
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([1.5, 2.0, 3.5, 4.0, 5.5])
result = scirs2.wilcoxon_py(x, y, zero_method="pratt")
assert "statistic" in result
assert "pvalue" in result
def test_wilcoxon_no_correction(self):
x = np.array([1.2, 2.3, 3.1, 4.5, 5.2])
y = np.array([1.5, 2.1, 3.4, 4.2, 5.5])
result = scirs2.wilcoxon_py(x, y, correction=False)
assert "statistic" in result
assert "pvalue" in result
def test_wilcoxon_identical_samples(self):
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
with pytest.raises(RuntimeError, match="All differences are zero"):
scirs2.wilcoxon_py(x, y)
def test_wilcoxon_large_sample(self):
np.random.seed(42)
x = np.random.normal(0, 1, 50)
y = x + np.random.normal(0.2, 0.5, 50)
result = scirs2.wilcoxon_py(x, y)
assert "statistic" in result
assert "pvalue" in result
assert result["statistic"] >= 0
class TestMannWhitneyUTest:
def test_mannwhitneyu_basic(self):
x = np.array([1.2, 2.3, 3.1, 4.5, 5.2])
y = np.array([2.1, 3.4, 4.2, 5.5, 6.3, 7.1])
result = scirs2.mannwhitneyu_py(x, y)
assert "statistic" in result
assert "pvalue" in result
assert result["statistic"] >= 0
assert 0 <= result["pvalue"] <= 1
def test_mannwhitneyu_alternative_less(self):
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([6.0, 7.0, 8.0, 9.0, 10.0])
result = scirs2.mannwhitneyu_py(x, y, alternative="less")
assert "statistic" in result
assert "pvalue" in result
assert result["pvalue"] < 0.1
def test_mannwhitneyu_alternative_greater(self):
x = np.array([6.0, 7.0, 8.0, 9.0, 10.0])
y = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
result = scirs2.mannwhitneyu_py(x, y, alternative="greater")
assert "statistic" in result
assert "pvalue" in result
assert 0 <= result["pvalue"] <= 1
def test_mannwhitneyu_alternative_two_sided(self):
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([6.0, 7.0, 8.0, 9.0, 10.0])
result = scirs2.mannwhitneyu_py(x, y, alternative="two-sided")
assert "statistic" in result
assert "pvalue" in result
def test_mannwhitneyu_no_continuity(self):
x = np.array([1.2, 2.3, 3.1, 4.5, 5.2])
y = np.array([2.1, 3.4, 4.2, 5.5, 6.3])
result = scirs2.mannwhitneyu_py(x, y, use_continuity=False)
assert "statistic" in result
assert "pvalue" in result
def test_mannwhitneyu_identical_distributions(self):
np.random.seed(42)
x = np.random.normal(0, 1, 30)
y = np.random.normal(0, 1, 30)
result = scirs2.mannwhitneyu_py(x, y)
assert "statistic" in result
assert "pvalue" in result
assert result["pvalue"] > 0.05
def test_mannwhitneyu_different_sizes(self):
x = np.array([1.0, 2.0, 3.0])
y = np.array([4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0])
result = scirs2.mannwhitneyu_py(x, y)
assert "statistic" in result
assert "pvalue" in result
class TestKruskalWallisTest:
def test_kruskal_basic_two_groups(self):
x = np.array([1.2, 2.3, 3.1, 4.5, 5.2])
y = np.array([2.1, 3.4, 4.2, 5.5, 6.3])
result = scirs2.kruskal_py(x, y)
assert "statistic" in result
assert "pvalue" in result
assert result["statistic"] >= 0
assert 0 <= result["pvalue"] <= 1
def test_kruskal_three_groups(self):
x = np.array([1.0, 2.0, 3.0, 4.0])
y = np.array([5.0, 6.0, 7.0, 8.0])
z = np.array([9.0, 10.0, 11.0, 12.0])
result = scirs2.kruskal_py(x, y, z)
assert "statistic" in result
assert "pvalue" in result
assert result["pvalue"] < 0.05
def test_kruskal_four_groups(self):
g1 = np.array([1.0, 2.0, 3.0])
g2 = np.array([4.0, 5.0, 6.0])
g3 = np.array([7.0, 8.0, 9.0])
g4 = np.array([10.0, 11.0, 12.0])
result = scirs2.kruskal_py(g1, g2, g3, g4)
assert "statistic" in result
assert "pvalue" in result
def test_kruskal_identical_distributions(self):
np.random.seed(42)
g1 = np.random.normal(0, 1, 20)
g2 = np.random.normal(0, 1, 20)
g3 = np.random.normal(0, 1, 20)
result = scirs2.kruskal_py(g1, g2, g3)
assert "statistic" in result
assert "pvalue" in result
assert result["pvalue"] > 0.05
def test_kruskal_different_sample_sizes(self):
g1 = np.array([1.0, 2.0, 3.0])
g2 = np.array([4.0, 5.0, 6.0, 7.0, 8.0])
g3 = np.array([9.0, 10.0])
result = scirs2.kruskal_py(g1, g2, g3)
assert "statistic" in result
assert "pvalue" in result
def test_kruskal_large_number_of_groups(self):
np.random.seed(42)
groups = [np.random.normal(i, 1, 10) for i in range(5)]
result = scirs2.kruskal_py(*groups)
assert "statistic" in result
assert "pvalue" in result
assert result["pvalue"] < 0.05
class TestNonparametricEdgeCases:
def test_kruskal_insufficient_groups(self):
x = np.array([1.0, 2.0, 3.0])
with pytest.raises(RuntimeError, match="Need at least 2 groups"):
scirs2.kruskal_py(x)
def test_wilcoxon_with_ties(self):
x = np.array([1.0, 2.0, 2.0, 3.0, 4.0])
y = np.array([1.5, 2.5, 2.5, 3.5, 4.5])
result = scirs2.wilcoxon_py(x, y)
assert "statistic" in result
assert "pvalue" in result
def test_mannwhitneyu_with_ties(self):
x = np.array([1.0, 2.0, 2.0, 3.0, 4.0])
y = np.array([2.0, 3.0, 3.0, 4.0, 5.0])
result = scirs2.mannwhitneyu_py(x, y)
assert "statistic" in result
assert "pvalue" in result
def test_kruskal_with_ties(self):
g1 = np.array([1.0, 2.0, 2.0, 3.0])
g2 = np.array([2.0, 3.0, 3.0, 4.0])
g3 = np.array([3.0, 4.0, 4.0, 5.0])
result = scirs2.kruskal_py(g1, g2, g3)
assert "statistic" in result
assert "pvalue" in result
class TestNonparametricConsistency:
def test_wilcoxon_symmetric(self):
x = np.array([1.2, 2.3, 3.1, 4.5, 5.2])
y = np.array([1.5, 2.1, 3.4, 4.2, 5.5])
result1 = scirs2.wilcoxon_py(x, y)
result2 = scirs2.wilcoxon_py(y, x)
assert abs(result1["pvalue"] - result2["pvalue"]) < 1e-6
def test_kruskal_order_invariant(self):
g1 = np.array([1.0, 2.0, 3.0])
g2 = np.array([4.0, 5.0, 6.0])
g3 = np.array([7.0, 8.0, 9.0])
result1 = scirs2.kruskal_py(g1, g2, g3)
result2 = scirs2.kruskal_py(g3, g1, g2)
result3 = scirs2.kruskal_py(g2, g3, g1)
assert abs(result1["statistic"] - result2["statistic"]) < 1e-6
assert abs(result1["statistic"] - result3["statistic"]) < 1e-6
assert abs(result1["pvalue"] - result2["pvalue"]) < 1e-6
assert abs(result1["pvalue"] - result3["pvalue"]) < 1e-6