Skip to content

Commit

Permalink
Merge pull request #243 from monarch-initiative/ielis/issue240
Browse files Browse the repository at this point in the history
Add T test
  • Loading branch information
ielis authored Sep 4, 2024
2 parents 532e611 + cda263f commit 0c06174
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/user-guide/stats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,12 @@ We will use :ref:`mann-whitney-u-test` as described above.
>>> score_statistic = MannWhitneyStatistic()


.. tip::

See :mod:`gpsea.analysis.pscore.stats` module for more statistical tests available
for using with phenotype scores.


**Final analysis**

We will put the final analysis together into :class:`~gpsea.analysis.pscore.PhenotypeScoreAnalysis`.
Expand Down
4 changes: 2 additions & 2 deletions src/gpsea/analysis/pscore/stats/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ._stats import PhenotypeScoreStatistic
from ._stats import MannWhitneyStatistic
from ._stats import MannWhitneyStatistic, TTestStatistic

__all__ = [
'PhenotypeScoreStatistic',
'MannWhitneyStatistic',
'MannWhitneyStatistic', 'TTestStatistic',
]
24 changes: 23 additions & 1 deletion src/gpsea/analysis/pscore/stats/_stats.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import abc
import typing

from scipy.stats import mannwhitneyu
from scipy.stats import mannwhitneyu, ttest_ind


class PhenotypeScoreStatistic(metaclass=abc.ABCMeta):
Expand Down Expand Up @@ -42,3 +42,25 @@ def compute_pval(
)

return pval


class TTestStatistic(PhenotypeScoreStatistic):
"""
`TTestStatistic` is a wrapper around SciPy's
:func:`~scipy.stats.ttest_ind` function to apply
T test on 2 phenotype scores.
"""

def compute_pval(
self,
scores: typing.Collection[typing.Sequence[float]],
) -> float:
assert len(scores) == 2, 'T test only supports 2 categories at this time'

x, y = scores
res = ttest_ind(
a=x, b=y,
alternative='two-sided',
)

return res.pvalue
54 changes: 54 additions & 0 deletions tests/analysis/pscore/test_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import typing
import pytest

from gpsea.analysis.pscore.stats import MannWhitneyStatistic, TTestStatistic


class TestMannWhitneyStatistic:

@pytest.fixture(scope='class')
def statistic(self) -> MannWhitneyStatistic:
return MannWhitneyStatistic()

@pytest.mark.parametrize(
'x, y, expected',
[
((1., 2., 3., ), (1., 2., 3., ), 1.),
((11., 15, 8., 12.,), (4., 2., 3., 3.5, 4.,), 0.01945103333136247),
]
)
def test_compute_pval(
self,
statistic: MannWhitneyStatistic,
x: typing.Sequence[float],
y: typing.Sequence[float],
expected: float,
):
actual = statistic.compute_pval((x, y))

assert actual == pytest.approx(expected)


class TestTTestStatistic:

@pytest.fixture(scope='class')
def statistic(self) -> TTestStatistic:
return TTestStatistic()

@pytest.mark.parametrize(
'x, y, expected',
[
((1., 2., 3., ), (1., 2., 3., ), 1.),
((11., 15, 8., 12.,), (4., 2., 3., 3.5, 4.,), 0.0004749950471148506),
]
)
def test_compute_pval(
self,
statistic: TTestStatistic,
x: typing.Sequence[float],
y: typing.Sequence[float],
expected: float,
):
actual = statistic.compute_pval((x, y))

assert actual == pytest.approx(expected)

0 comments on commit 0c06174

Please sign in to comment.