def setUp(self): self.result = StatResult("This long description", "This", "stat", 3, 0.3, alpha=0.05)
def test_missing_formatter(self): res = StatResult("Custom test", None, pval=0.05, stat=None, stat_str=None) with self.assertRaisesRegex(ValueError, "PValueFormat"): annotation = Annotation(("group1", "group2"), res) print(annotation.text)
def simple_text(result: StatResult, pvalue_format, pvalue_thresholds, short_test_name=True) -> str: """ Generates simple text for test name and pvalue. :param result: StatResult instance :param pvalue_format: format string for pvalue :param pvalue_thresholds: String to display per pvalue range :param short_test_name: whether to display the test (short) name :returns: simple annotation """ # Sort thresholds thresholds = sorted(pvalue_thresholds, key=lambda x: x[0]) text = (f"{result.test_short_name} " if short_test_name and result.test_short_name else "") for threshold in thresholds: if result.pvalue < threshold[0]: pval_text = "p ≤ {}".format(threshold[1]) break else: pval_text = "p = {}".format(pvalue_format).format(result.pvalue) return result.adjust(text + pval_text)
def __call__(self, group_data1, group_data2, alpha=0.05, **stat_params): stat, pval = self._func(group_data1, group_data2, *self.args, **{**self.kwargs, **stat_params})[:2] return StatResult(self._test_long_name, self._test_short_name, self._stat_name, stat, pval, alpha=alpha)
def setUp(self) -> None: self.benjamini_hochberg = ComparisonsCorrection("Benjamini-Hochberg") self.stat_result = StatResult("Test X", "X", "Stat", 1, 0.02, alpha=0.05) self.stat_result.correction_method = self.benjamini_hochberg.name
def _get_custom_results(self, group_struct1, pvalues) -> StatResult: pvalue = self._value_for_group_struct(pvalues, group_struct1) if self.has_type0_comparisons_correction(): pvalue = self.comparisons_correction(pvalue, len(pvalues)) result = StatResult('Custom statistical test', self.test_short_name, None, None, pval=pvalue, alpha=self.alpha) return result
def apply_test( group_data1, group_data2, test: Union[StatTest, str] = None, comparisons_correction: Union[ComparisonsCorrection, str] = None, num_comparisons: int = 1, alpha: float = 0.05, **stats_params ): """Get formatted result of two-sample statistical test. :param group_data1: data :param group_data2: data :param test: Union[StatTest, str]: Statistical test to run. Either a `StatTest` instance or one of: - `Levene` - `Mann-Whitney` - `Mann-Whitney-gt` - `Mann-Whitney-ls` - `t-test_ind` - `t-test_welch` - `t-test_paired` - `Wilcoxon` - `Kruskal` :param comparisons_correction: Union[ComparisonsCorrection, str]: (Default value = None) Method to use for multiple comparisons correction. Either a `ComparisonsCorrection` instance or one of (interfacing statsmodels): - Bonferroni - Holm-Bonferroni - Benjamini-Hochberg - Benjamini-Yekutieli :param num_comparisons: int: (Default value = 1) Number of comparisons to use for multiple comparisons correction. :param alpha: float: (Default value = 0.05) Used for pvalue interpretation in case of comparisons_correction. :param stats_params: Additional keyword arguments to pass to the test function """ # Check arguments. if (isinstance(comparisons_correction, ComparisonsCorrection) or comparisons_correction is None): pass else: comparisons_correction = \ get_validated_comparisons_correction(comparisons_correction) if test is None: result = StatResult(None, '', None, None, np.nan) else: if isinstance(test, StatTest): get_stat_result = test else: get_stat_result = StatTest.from_library(test) result = get_stat_result( group_data1, group_data2, alpha=alpha, **stats_params) # Optionally, run multiple comparisons correction that can independently be # applied to each pval if comparisons_correction is not None and comparisons_correction.type == 0: result.pvalue = comparisons_correction(result.pvalue, num_comparisons) result.correction_method = comparisons_correction.name return result