Skip to content

Commit

Permalink
added fixtures to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixAja committed Mar 4, 2020
1 parent 42f921e commit 282719f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 30 deletions.
Empty file added tests/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest


@pytest.fixture
def anscombes_quartet():

anscombes_quartet = {
"x_1": [10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5],
"x_2": [10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5],
"x_3": [0, 0, 0, 0, 1, 1, 2, 3],
"x_4": [8, 8, 8, 8, 8, 8, 8, 19, 8, 8, 8],
"y_1": [
8.04, 6.95, 7.58, 8.81, 8.33,
9.96, 7.24, 4.26, 10.84, 4.82, 5.68
],
"y_2": [
9.14, 8.14, 8.74, 8.77, 9.26, 8.1,
6.13, 3.1, 9.13, 7.26, 4.74
],
"y_3": [0, 1, 2, 3, 4, 5, 6, 7],
"y_4": [
6.58, 5.76, 7.71, 8.84,
8.47, 7.04, 5.25, 12.5, 5.56, 7.91, 6.89
]
}

return anscombes_quartet
71 changes: 41 additions & 30 deletions tests/xi_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,53 @@
from pyxi.pyxi import xi, xiPValue


def test_xi_and_p_val():
@pytest.fixture
def anscombes_xis(anscombes_quartet):

xis = {
"xi_1": xi(anscombes_quartet["x_1"], anscombes_quartet["y_1"]),
"xi_2": xi(anscombes_quartet["x_2"], anscombes_quartet["y_2"]),
"xi_3": xi(anscombes_quartet["x_3"], anscombes_quartet["y_3"]),
"xi_4": xi(anscombes_quartet["x_4"], anscombes_quartet["y_4"]),
}

return xis


x_1 = [10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5]
y_1 = [8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68]
x_2 = [10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5]
y_2 = [9.14, 8.14, 8.74, 8.77, 9.26, 8.1, 6.13, 3.1, 9.13, 7.26, 4.74]
x_3 = [0, 0, 0, 0, 1, 1, 2, 3]
y_3 = [0, 1, 2, 3, 4, 5, 6, 7]
x_4 = [8, 8, 8, 8, 8, 8, 8, 19, 8, 8, 8]
y_4 = [6.58, 5.76, 7.71, 8.84, 8.47, 7.04, 5.25, 12.5 , 5.56, 7.91, 6.89]
@pytest.fixture
def anscombes_pvalues(anscombes_xis):

xi_1 = xi(x_1, y_1)
xip_1 = xiPValue(xi_1)
p_vals = {
"p_1": xiPValue(anscombes_xis["xi_1"]),
"p_2": xiPValue(anscombes_xis["xi_2"]),
"p_3": xiPValue(anscombes_xis["xi_3"]),
"p_4": xiPValue(anscombes_xis["xi_4"]),
}

xi_2 = xi(x_2, y_2)
xip_2 = xiPValue(xi_2)
return p_vals

xi_3 = xi(x_3, y_3)
xip_3 = xiPValue(xi_3)

xi_4 = xi(x_4, y_4)
xip_4 = xiPValue(xi_4)
def test_xi_correlations(anscombes_xis):

assert anscombes_xis["xi_1"].correlation == 0.2749999999999999
assert anscombes_xis["xi_2"].correlation == 0.6
assert anscombes_xis["xi_3"].correlation == 0.725
assert anscombes_xis["xi_4"].correlation == 0.125


# values from R code
assert xi_1.correlation == 0.2749999999999999
assert xip_1.asymptotic(ties=False, nperm=1000, factor=True) == 0.07841556446646347
assert xip_1.permutation_test(nperm=1000) == 0.048
def test_p_val_asynptotic(anscombes_pvalues):

assert xi_2.correlation == 0.6
assert xip_2.asymptotic(ties=False, nperm=1000, factor=True) == 0.001004022
assert xip_2.permutation_test(nperm=1000) == 0
# values taken from R code
assert anscombes_pvalues["p1"].asymptotic(ties=False, nperm=1000, factor=True) == 0.07841556446646347
assert anscombes_pvalues["p2"].asymptotic(ties=False, nperm=1000, factor=True) == 0.001004022
assert anscombes_pvalues["p3"].asymptotic(ties=False, nperm=1000, factor=True) == 9.476043e-05
assert anscombes_pvalues["p4"].asymptotic(ties=False, nperm=1000, factor=True) == 0.2599336

assert xi_3.correlation == 0.725
assert xip_3.asymptotic(ties=False, nperm=1000, factor=True) == 9.476043e-05
assert xip_3.permutation_test(nperm=1000) == 0

assert xi_4.correlation == 0.125
assert xip_4.asymptotic(ties=False, nperm=1000, factor=True) == 0.2599336
assert xip_4.permutation_test(nperm=1000) == 0.378
def test_p_val_permutations(anscombes_pvalues):

# values taken from R code
assert anscombes_pvalues["p1"].permutation_test(nperm=1000) == 0.048
assert anscombes_pvalues["p2"].permutation_test(nperm=1000) == 0
assert anscombes_pvalues["p3"].permutation_test(nperm=1000) == 0
assert anscombes_pvalues["p4"].permutation_test(nperm=1000) == 0.378

0 comments on commit 282719f

Please sign in to comment.