Skip to content

Commit

Permalink
map scipy ttest functions with strings
Browse files Browse the repository at this point in the history
  • Loading branch information
JuliaS92 committed Dec 17, 2024
1 parent d28e36c commit e734a34
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
15 changes: 10 additions & 5 deletions alphastats/tl/differential_expression_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ def _extend_validation(self, input_data: pd.DataFrame, parameters: dict):
"""
super()._extend_validation(input_data, parameters)
if parameters["test_fun"] not in [
scipy.stats.ttest_ind,
scipy.stats.ttest_rel,
"independent",
"paired",
]:
raise ValueError(
"test_fun must be either scipy.stats.ttest_ind or scipy.stats.ttest_rel for t-test."
"test_fun must be either 'independent' for scipy.stats.ttest_ind or 'paired' for scipy.stats.ttest_rel."
)
if parameters["fdr_method"] not in ["bh", "by"]:
raise ValueError("fdr_method must be one of 'bh', 'by'.")
Expand Down Expand Up @@ -258,7 +258,7 @@ def _statistical_test_fun(
group1: list,
group2: list,
is_log2_transformed: bool,
test_fun: callable,
test_fun: str,
fdr_method: str,
) -> pd.DataFrame:
"""Runs the t-test analysis and returns the result.
Expand All @@ -268,14 +268,19 @@ def _statistical_test_fun(
group1 (list): The samples for group 1.
group2 (list): The samples for group 2.
is_log2_transformed (bool): Whether the data is log2 transformed.
test_fun (callable): The test function to use, scipy.stats.ttest_ind or scipy.stats.ttest_rel.
test_fun (str): The test function to use, independent for scipy.stats.ttest_ind or paired for scipy.stats.ttest_rel.
fdr_method (str): The FDR method to use, 'bh' or 'by'.
Returns:
pd.DataFrame: The result of the analysis.
"""
mat_transpose = input_data.loc[group1 + group2, :].transpose()

test_fun = {
"independent": scipy.stats.ttest_ind,
"paired": scipy.stats.ttest_rel,
}[test_fun]

if not is_log2_transformed:
mat_transpose = mat_transpose.transform(np.log2)
mat_transpose = mat_transpose.replace([np.inf, -np.inf], np.nan)
Expand Down
11 changes: 5 additions & 6 deletions tests/tl/test_differential_expression_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import numpy as np
import pandas as pd
import pytest
import scipy

from alphastats.dataset.keys import Cols
from alphastats.dataset.preprocessing import PreprocessingStateKeys
Expand Down Expand Up @@ -353,7 +352,7 @@ def test_dea_ttest_perform_runs():
dea.perform(
**TestableDifferentialExpressionAnalysisTwoGroups.valid_parameter_input,
**{
DeaParameters.TEST_FUN: scipy.stats.ttest_ind,
DeaParameters.TEST_FUN: "independent",
DeaParameters.FDR_METHOD: "bh",
PreprocessingStateKeys.LOG2_TRANSFORMED: True,
},
Expand All @@ -369,7 +368,7 @@ def test_dea_ttest_runs_log(mock_transform):
dea.perform(
**TestableDifferentialExpressionAnalysisTwoGroups.valid_parameter_input,
**{
DeaParameters.TEST_FUN: scipy.stats.ttest_ind,
DeaParameters.TEST_FUN: "independent",
DeaParameters.FDR_METHOD: "bh",
PreprocessingStateKeys.LOG2_TRANSFORMED: False,
},
Expand All @@ -383,12 +382,12 @@ def test_dea_ttest_validate_wrong_stats_method():
dea = DifferentialExpressionAnalysisTTest(input_data)
with pytest.raises(
ValueError,
match="test_fun must be either scipy.stats.ttest_ind or scipy.stats.ttest_rel for t-test.",
match="test_fun must be either 'independent' for scipy.stats.ttest_ind or 'paired' for scipy.stats.ttest_rel.",
):
dea.perform(
**TestableDifferentialExpressionAnalysisTwoGroups.valid_parameter_input,
**{
DeaParameters.TEST_FUN: float,
DeaParameters.TEST_FUN: "not defined",
DeaParameters.FDR_METHOD: "bh",
PreprocessingStateKeys.LOG2_TRANSFORMED: True,
},
Expand All @@ -402,7 +401,7 @@ def test_dea_ttest_validate_wrong_fdr_method():
dea.perform(
**TestableDifferentialExpressionAnalysisTwoGroups.valid_parameter_input,
**{
DeaParameters.TEST_FUN: scipy.stats.ttest_ind,
DeaParameters.TEST_FUN: "independent",
DeaParameters.FDR_METHOD: "unknown",
PreprocessingStateKeys.LOG2_TRANSFORMED: True,
},
Expand Down

0 comments on commit e734a34

Please sign in to comment.