Skip to content

Commit b5351af

Browse files
authored
add option to capitalise "p" in annotations
1 parent 4f6d459 commit b5351af

3 files changed

Lines changed: 30 additions & 7 deletions

File tree

statannotations/PValueFormat.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
'simple_format_string',
1313
'text_format',
1414
'pvalue_thresholds',
15-
'show_test_name'
15+
'show_test_name',
16+
'p_capitalized'
1617
]
1718

1819

@@ -38,6 +39,7 @@ def __init__(self):
3839
self._pvalue_thresholds = self._get_pvalue_thresholds(DEFAULT)
3940
self._correction_format = "{star} ({suffix})"
4041
self.show_test_name = True
42+
self.p_capitalized = False
4143

4244
def config(self, **parameters):
4345

@@ -176,8 +178,10 @@ def format_data(self, result):
176178
text = (f"{result.test_short_name} " if self.show_test_name
177179
else "")
178180

179-
return ("{}p = {}{}"
180-
.format('{}', self.pvalue_format_string, '{}')
181+
p_letter = "P" if self.p_capitalized else "p"
182+
183+
return ("{}{} = {}{}"
184+
.format('{}', p_letter, self.pvalue_format_string, '{}')
181185
.format(text, result.pvalue, result.significance_suffix))
182186

183187
elif self.text_format == 'star':
@@ -199,7 +203,7 @@ def format_data(self, result):
199203
# elif self.text_format == 'simple':
200204
else:
201205
return simple_text(result, self.simple_format_string,
202-
self.pvalue_thresholds, self.show_test_name)
206+
self.pvalue_thresholds, self.show_test_name, self.p_capitalized)
203207

204208
def get_configuration(self):
205209
return {key: getattr(self, key) for key in CONFIGURABLE_PARAMETERS}

statannotations/format_annotations.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def pval_annotation_text(result: List[StatResult],
3333

3434

3535
def simple_text(result: StatResult, pvalue_format, pvalue_thresholds,
36-
short_test_name=True) -> str:
36+
short_test_name=True, p_capitalized=False) -> str:
3737
"""
3838
Generates simple text for test name and pvalue.
3939
@@ -51,11 +51,13 @@ def simple_text(result: StatResult, pvalue_format, pvalue_thresholds,
5151
if short_test_name and result.test_short_name
5252
else "")
5353

54+
p_letter = "P" if p_capitalized else "p"
55+
5456
for threshold in thresholds:
5557
if result.pvalue < threshold[0]:
56-
pval_text = "p ≤ {}".format(threshold[1])
58+
pval_text = "{} ≤ {}".format(p_letter, threshold[1])
5759
break
5860
else:
59-
pval_text = "p = {}".format(pvalue_format).format(result.pvalue)
61+
pval_text = "{} = {}".format(p_letter, pvalue_format).format(result.pvalue)
6062

6163
return result.adjust(text + pval_text)

tests/test_pvalue_format.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def test_get_configuration(self):
114114
self.assertDictEqual(pvalue_format.get_configuration(),
115115
{'correction_format': '{star} ({suffix})',
116116
'fontsize': 'medium',
117+
'p_capitalized': False,
117118
'pvalue_format_string': '{:.3e}',
118119
'show_test_name': True,
119120
'simple_format_string': '{:.2f}',
@@ -137,3 +138,19 @@ def test_config_pvalue_thresholds(self):
137138
" ns: 5.00e-02 < p <= 1.00e+00\n"
138139
" <= 0.05: 1.00e-03 < p <= 5.00e-02\n"
139140
"<= 0.001: p <= 1.00e-03\n\n")
141+
142+
def test_pvalue_simple_capitalized(self):
143+
self.annotator.configure(pvalue_format={"text_format": "simple",
144+
"p_capitalized": True})
145+
annotations = self.annotator._get_results("auto", pvalues=self.pvalues)
146+
self.assertEqual(["P ≤ 0.05", "P ≤ 0.05", "P = 0.90"],
147+
[annotation.text for annotation in annotations])
148+
149+
def test_pvalue_full_capitalized(self):
150+
self.annotator.configure(pvalue_format={"text_format": "full",
151+
"p_capitalized": True,
152+
"show_test_name": False,
153+
"pvalue_format_string": "{:.2f}"})
154+
annotations = self.annotator._get_results("auto", pvalues=self.pvalues)
155+
self.assertEqual(["P = 0.04", "P = 0.03", "P = 0.90"],
156+
[annotation.text for annotation in annotations])

0 commit comments

Comments
 (0)