import numpy as np
import pytest
import scirs2
class TestDescriptiveStatistics:
def test_describe(self):
data = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
stats = scirs2.describe_py(data)
assert "mean" in stats
assert "std" in stats
assert "var" in stats
assert "min" in stats
assert "max" in stats
assert "median" in stats
assert "count" in stats
assert abs(stats["mean"] - 3.0) < 1e-10
assert stats["min"] == 1.0
assert stats["max"] == 5.0
assert stats["median"] == 3.0
assert stats["count"] == 5
def test_mean(self):
data = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
mean = scirs2.mean_py(data)
assert abs(mean - 3.0) < 1e-10
def test_std_population(self):
data = np.array([2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0])
std = scirs2.std_py(data, 0)
expected = np.std(data, ddof=0)
assert abs(std - expected) < 1e-10
def test_std_sample(self):
data = np.array([2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0])
std = scirs2.std_py(data, 1)
expected = np.std(data, ddof=1)
assert abs(std - expected) < 1e-10
def test_var(self):
data = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
var = scirs2.var_py(data, 0)
expected = np.var(data, ddof=0)
assert abs(var - expected) < 1e-10
class TestPercentiles:
def test_percentile_median(self):
data = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
p50 = scirs2.percentile_py(data, 50.0)
assert abs(p50 - 3.0) < 1e-10
def test_percentile_quartiles(self):
data = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0])
q1 = scirs2.percentile_py(data, 25.0)
q3 = scirs2.percentile_py(data, 75.0)
assert q1 < q3
assert 2.0 <= q1 <= 3.0
assert 6.0 <= q3 <= 7.0
def test_median(self):
data = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
median = scirs2.median_py(data)
assert abs(median - 3.0) < 1e-10
def test_iqr(self):
data = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0])
iqr = scirs2.iqr_py(data)
assert iqr > 0
class TestCorrelation:
def test_correlation_perfect_positive(self):
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([2.0, 4.0, 6.0, 8.0, 10.0])
corr = scirs2.correlation_py(x, y)
assert abs(corr - 1.0) < 1e-10
def test_correlation_perfect_negative(self):
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([10.0, 8.0, 6.0, 4.0, 2.0])
corr = scirs2.correlation_py(x, y)
assert abs(corr + 1.0) < 1e-10
def test_correlation_zero(self):
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([3.0, 3.0, 3.0, 3.0, 3.0])
corr = scirs2.correlation_py(x, y)
def test_covariance(self):
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
y = np.array([2.0, 4.0, 6.0, 8.0, 10.0])
cov = scirs2.covariance_py(x, y, 1)
expected = np.cov(x, y, ddof=1)[0, 1]
assert abs(cov - expected) < 1e-10
class TestEdgeCases:
def test_single_element(self):
data = np.array([5.0])
stats = scirs2.describe_py(data)
assert stats["mean"] == 5.0
assert stats["min"] == 5.0
assert stats["max"] == 5.0
def test_large_array(self):
data = np.random.randn(10000)
stats = scirs2.describe_py(data)
assert abs(stats["mean"]) < 0.1
assert abs(stats["std"] - 1.0) < 0.1
class TestTTests:
def test_ttest_1samp_basic(self):
data = np.array([4.5, 5.0, 5.5, 4.8, 5.2, 5.1, 4.9, 5.3])
result = scirs2.ttest_1samp_py(data, 5.0)
assert "statistic" in result
assert "pvalue" in result
assert "df" in result
assert result["pvalue"] > 0.05
def test_ttest_1samp_significant(self):
data = np.array([5.1, 5.2, 5.0, 5.3, 5.1, 5.2, 5.0, 5.1, 5.0, 5.2])
result = scirs2.ttest_1samp_py(data, 0.0)
assert result["pvalue"] < 0.05
def test_ttest_1samp_one_sided(self):
data = np.array([5.1, 5.2, 5.0, 5.3, 5.1, 5.2, 5.0, 5.1])
result = scirs2.ttest_1samp_py(data, 4.0, alternative="greater")
assert result["pvalue"] < 0.05
def test_ttest_ind_basic(self):
a = np.array([5.1, 5.2, 5.0, 5.3, 5.1])
b = np.array([5.0, 5.1, 4.9, 5.2, 5.0])
result = scirs2.ttest_ind_py(a, b)
assert "statistic" in result
assert "pvalue" in result
assert "df" in result
def test_ttest_ind_significant(self):
a = np.array([10.0, 10.1, 10.2, 10.0, 10.1, 10.0, 10.2])
b = np.array([0.0, 0.1, 0.2, 0.0, 0.1, 0.0, 0.2])
result = scirs2.ttest_ind_py(a, b)
assert result["pvalue"] < 0.05
class TestAdditionalStatistics:
def test_skew_symmetric(self):
data = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
skew = scirs2.skew_py(data)
assert abs(skew) < 0.1
def test_skew_positive(self):
data = np.array([1.0, 1.0, 1.0, 2.0, 5.0, 10.0])
skew = scirs2.skew_py(data)
assert skew > 0
def test_kurtosis_normal(self):
np.random.seed(42)
data = np.random.randn(10000)
kurt = scirs2.kurtosis_py(data)
assert abs(kurt) < 0.3
def test_kurtosis_uniform(self):
data = np.linspace(0, 1, 1000)
kurt = scirs2.kurtosis_py(data)
assert kurt < 0
def test_mode(self):
data = np.array([1.0, 2.0, 2.0, 2.0, 3.0, 3.0])
mode = scirs2.mode_py(data)
assert abs(mode - 2.0) < 0.01
def test_gmean(self):
data = np.array([1.0, 2.0, 4.0, 8.0])
gmean = scirs2.gmean_py(data)
assert abs(gmean - 2.828) < 0.01
def test_hmean(self):
data = np.array([1.0, 2.0, 4.0])
hmean = scirs2.hmean_py(data)
assert abs(hmean - 1.714) < 0.01
def test_zscore(self):
data = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
zscores = scirs2.zscore_py(data)
assert abs(np.mean(zscores)) < 1e-10
assert abs(np.std(zscores) - 1.0) < 0.1
if __name__ == "__main__":
pytest.main([__file__, "-v"])
class TestNormalityTests:
def test_shapiro_normal_data(self):
np.random.seed(42)
data = np.random.normal(0, 1, 20)
result = scirs2.shapiro_py(data)
assert "statistic" in result
assert "pvalue" in result
def test_shapiro_uniform_data(self):
np.random.seed(42)
data = np.random.uniform(0, 1, 20)
result = scirs2.shapiro_py(data)
assert "statistic" in result
assert "pvalue" in result
def test_shapiro_small_sample(self):
data = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
result = scirs2.shapiro_py(data)
assert "statistic" in result
assert "pvalue" in result
class TestChiSquare:
def test_chisquare_uniform(self):
observed = np.array([8.0, 9.0, 11.0, 12.0, 10.0, 10.0])
expected = np.array([10.0, 10.0, 10.0, 10.0, 10.0, 10.0])
result = scirs2.chisquare_py(observed, expected)
assert "statistic" in result
assert "pvalue" in result
assert "dof" in result
assert result["dof"] == 5
def test_chisquare_default_expected(self):
observed = np.array([10.0, 15.0, 12.0, 13.0])
result = scirs2.chisquare_py(observed)
assert "statistic" in result
assert "pvalue" in result
class TestANOVA:
def test_f_oneway_equal_groups(self):
group1 = np.array([85.0, 82.0, 78.0, 88.0, 91.0])
group2 = np.array([76.0, 80.0, 82.0, 84.0, 79.0])
group3 = np.array([91.0, 89.0, 93.0, 87.0, 90.0])
result = scirs2.f_oneway_py(group1, group2, group3)
assert "f_statistic" in result
assert "pvalue" in result
assert "df_between" in result
assert "df_within" in result
assert result["df_between"] == 2
assert result["df_within"] == 12
def test_f_oneway_unequal_groups(self):
group1 = np.array([1.0, 2.0, 3.0])
group2 = np.array([4.0, 5.0, 6.0, 7.0])
group3 = np.array([8.0, 9.0])
result = scirs2.f_oneway_py(group1, group2, group3)
assert "f_statistic" in result
assert "pvalue" in result
def test_f_oneway_two_groups(self):
group1 = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
group2 = np.array([2.0, 3.0, 4.0, 5.0, 6.0])
result = scirs2.f_oneway_py(group1, group2)
assert "f_statistic" in result
assert "pvalue" in result
assert result["df_between"] == 1
def test_f_oneway_identical_groups(self):
group1 = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
group2 = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
group3 = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
result = scirs2.f_oneway_py(group1, group2, group3)
assert result["f_statistic"] < 0.01 or result["pvalue"] > 0.9