From a511e3d5bc77ab452db509851e45c05a06d0609c Mon Sep 17 00:00:00 2001 From: Pentabyteman <22663502+Pentabyteman@users.noreply.github.com> Date: Tue, 18 Mar 2025 22:45:38 +0100 Subject: [PATCH 1/2] Added option to capitalize 'p' in p-value format --- statannotations/PValueFormat.py | 13 ++++++++----- statannotations/format_annotations.py | 8 +++++--- tests/test_pvalue_format.py | 8 ++++++++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/statannotations/PValueFormat.py b/statannotations/PValueFormat.py index 1ae1083..72ee8f9 100644 --- a/statannotations/PValueFormat.py +++ b/statannotations/PValueFormat.py @@ -12,7 +12,8 @@ 'simple_format_string', 'text_format', 'pvalue_thresholds', - 'show_test_name' + 'show_test_name', + 'p_capitalized' ] @@ -38,6 +39,7 @@ def __init__(self): self._pvalue_thresholds = self._get_pvalue_thresholds(DEFAULT) self._correction_format = "{star} ({suffix})" self.show_test_name = True + self.p_capitalized = False def config(self, **parameters): @@ -176,9 +178,10 @@ def format_data(self, result): text = (f"{result.test_short_name} " if self.show_test_name else "") - return ("{}p = {}{}" - .format('{}', self.pvalue_format_string, '{}') - .format(text, result.pvalue, result.significance_suffix)) + p_letter = "P" if self.p_capitalized else "p" + + return ("{}{} = {}{}" + .format(text, p_letter, self.pvalue_format_string, result.pvalue, result.significance_suffix)) elif self.text_format == 'star': was_list = False @@ -199,7 +202,7 @@ def format_data(self, result): # elif self.text_format == 'simple': else: return simple_text(result, self.simple_format_string, - self.pvalue_thresholds, self.show_test_name) + self.pvalue_thresholds, self.show_test_name, self.p_capitalized) def get_configuration(self): return {key: getattr(self, key) for key in CONFIGURABLE_PARAMETERS} diff --git a/statannotations/format_annotations.py b/statannotations/format_annotations.py index 8f2a0e7..883a9a1 100644 --- a/statannotations/format_annotations.py +++ b/statannotations/format_annotations.py @@ -33,7 +33,7 @@ def pval_annotation_text(result: List[StatResult], def simple_text(result: StatResult, pvalue_format, pvalue_thresholds, - short_test_name=True) -> str: + short_test_name=True, p_capitalized=False) -> str: """ Generates simple text for test name and pvalue. @@ -51,11 +51,13 @@ def simple_text(result: StatResult, pvalue_format, pvalue_thresholds, if short_test_name and result.test_short_name else "") + p_letter = "P" if p_capitalized else "p" + for threshold in thresholds: if result.pvalue < threshold[0]: - pval_text = "p ≤ {}".format(threshold[1]) + pval_text = "{} ≤ {}".format(p_letter, threshold[1]) break else: - pval_text = "p = {}".format(pvalue_format).format(result.pvalue) + pval_text = "{} = {}".format(p_letter, pvalue_format).format(result.pvalue) return result.adjust(text + pval_text) diff --git a/tests/test_pvalue_format.py b/tests/test_pvalue_format.py index 7a255fd..9d4ee53 100644 --- a/tests/test_pvalue_format.py +++ b/tests/test_pvalue_format.py @@ -114,6 +114,7 @@ def test_get_configuration(self): self.assertDictEqual(pvalue_format.get_configuration(), {'correction_format': '{star} ({suffix})', 'fontsize': 'medium', + 'p_capitalized': False, 'pvalue_format_string': '{:.3e}', 'show_test_name': True, 'simple_format_string': '{:.2f}', @@ -137,3 +138,10 @@ def test_config_pvalue_thresholds(self): " ns: 5.00e-02 < p <= 1.00e+00\n" " <= 0.05: 1.00e-03 < p <= 5.00e-02\n" "<= 0.001: p <= 1.00e-03\n\n") + + def test_pvalue_capitalized(self): + self.annotator.configure(pvalue_format={"text_format": "simple", + "p_capitalized": True}) + annotations = self.annotator._get_results("auto", pvalues=self.pvalues) + self.assertEqual(["P ≤ 0.05", "P ≤ 0.05", "P = 0.90"], + [annotation.text for annotation in annotations]) From c8514baddceb92309afc2b1aced2b2c5b8da1ba7 Mon Sep 17 00:00:00 2001 From: Pentabyteman <22663502+Pentabyteman@users.noreply.github.com> Date: Tue, 18 Mar 2025 23:01:27 +0100 Subject: [PATCH 2/2] fixed bug in capital-p for full text format and updated tests --- statannotations/PValueFormat.py | 3 ++- tests/test_pvalue_format.py | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/statannotations/PValueFormat.py b/statannotations/PValueFormat.py index 72ee8f9..8403af8 100644 --- a/statannotations/PValueFormat.py +++ b/statannotations/PValueFormat.py @@ -181,7 +181,8 @@ def format_data(self, result): p_letter = "P" if self.p_capitalized else "p" return ("{}{} = {}{}" - .format(text, p_letter, self.pvalue_format_string, result.pvalue, result.significance_suffix)) + .format('{}', p_letter, self.pvalue_format_string, '{}') + .format(text, result.pvalue, result.significance_suffix)) elif self.text_format == 'star': was_list = False diff --git a/tests/test_pvalue_format.py b/tests/test_pvalue_format.py index 9d4ee53..844a729 100644 --- a/tests/test_pvalue_format.py +++ b/tests/test_pvalue_format.py @@ -139,9 +139,18 @@ def test_config_pvalue_thresholds(self): " <= 0.05: 1.00e-03 < p <= 5.00e-02\n" "<= 0.001: p <= 1.00e-03\n\n") - def test_pvalue_capitalized(self): + def test_pvalue_simple_capitalized(self): self.annotator.configure(pvalue_format={"text_format": "simple", "p_capitalized": True}) annotations = self.annotator._get_results("auto", pvalues=self.pvalues) self.assertEqual(["P ≤ 0.05", "P ≤ 0.05", "P = 0.90"], [annotation.text for annotation in annotations]) + + def test_pvalue_full_capitalized(self): + self.annotator.configure(pvalue_format={"text_format": "full", + "p_capitalized": True, + "show_test_name": False, + "pvalue_format_string": "{:.2f}"}) + annotations = self.annotator._get_results("auto", pvalues=self.pvalues) + self.assertEqual(["P = 0.04", "P = 0.03", "P = 0.90"], + [annotation.text for annotation in annotations]) \ No newline at end of file