Repository navigation
Expand file tree
/
Copy pathStat_Annotation.py
More file actions
127 lines (103 loc) · 5.87 KB
/
Copy pathStat_Annotation.py
File metadata and controls
127 lines (103 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
def stat_annotation(ax, x1, x2, y, p_val, effect_size = None, cles = None, cles_first = False, preamble = None, **kwargs):
"""
Annotate plot to have statistics.
effect_size is Cohen's D
cles is Common Language Effect Size
preamble is any text that goes before the statistics
Optional Arguments:
color = kwargs.get("color", "grey")
h = kwargs.get("h", 0.1 * y)
lw = kwargs.get("lw", .7)
fontsize = kwargs.get("fontsize", 6)
exact_p = kwargs.get("exact_p", False)
indicator_length = kwargs.get("indicator_length", 0)
stacked = kwargs.get("stacked", False)
fontweight = kwargs.get("fontweight", "bold")
"""
main_effect_prong = False
if type(x1) in [np.ndarray, list, tuple]:
main_effect_prong = True
if np.size(x1) == 1:
main_effect_prong = False
color = kwargs.get("color", "grey")
h = kwargs.get("h", 0.05)
lw = kwargs.get("lw", .7)
fontsize = kwargs.get("fontsize", 6)
fontweight = kwargs.get("fontweight", "bold")
exact_p = kwargs.get("exact_p", False)
indicator_length = kwargs.get("indicator_length", 0)
stacked = kwargs.get("stacked", False)
#Handle P-Value
if p_val == None:
p_text = ''
elif p_val < 0.001 and not exact_p:
p_text = f"p < 0.001"
elif p_val >= 0.001 or exact_p:
p_text = f"p = {p_val:.3f}"
# Defining our theta hat (for CLES) strings:
theta_hat_cles_first = r', $\mathbf{\hat{\theta}}$ = '
theta_hat = r'$\mathbf{\hat{\theta}}$ = '
if fontweight == "normal":
theta_hat_cles_first = r', $\hat{\theta}$ = '
theta_hat = r'$\hat{\theta}$ = '
#Handle Effect Size and Common Language Effect Size
if effect_size != None and cles != None: #if both
if cles_first:
p_text = p_text + theta_hat_cles_first + f"{cles:.2f}" + f", d = {effect_size:.2f}"
else:
p_text = p_text + f", d = {effect_size:.2f}, " + theta + f"{cles:.2f}"
elif effect_size != None and cles == None: #if only Cohen D
p_text = p_text + f", d = {effect_size:.2f}"
elif effect_size == None and cles != None: #if only CLES
p_text = p_text + theta_hat_cles_first + f"{cles:.2f}"
if preamble != "" and preamble != None: #If Preamble
if not stacked:
p_text = preamble + " " + p_text
else:
p_text = preamble + ", " + p_text
if main_effect_prong:
if np.shape(x1) != np.shape(x2):
raise ValueError('Wrong Axis Shape')
#plot the text
if not stacked:
ax.plot([np.nanmean(x1), np.nanmean(x1), np.nanmean(x2), np.nanmean(x2)], [y[1], y[0], y[0], y[2]], lw=lw, c=color)
ax.plot([x1[0],x1[0],x1[1],x1[1]],[y[1] - indicator_length, y[1], y[1], y[1] - indicator_length], lw=lw, c=color)
ax.plot([x2[0],x2[0],x2[1],x2[1]],[y[2] - indicator_length, y[2], y[2], y[2] - indicator_length], lw=lw, c=color)
ax.text((np.nanmean(x1)+np.nanmean(x2))*.5, y[0]+h , p_text, ha='center', va='bottom', color=color, fontsize = fontsize, weight = fontweight)
else:
ax.plot([np.nanmean(x1), np.nanmean(x1), np.nanmean(x2), np.nanmean(x2)], [y[1], y[0], y[0], y[2]], lw=lw, c=color)
ax.plot([x1[0],x1[0],x1[1],x1[1]],[y[1] - indicator_length, y[1], y[1], y[1] - indicator_length], lw=lw, c=color)
ax.plot([x2[0],x2[0],x2[1],x2[1]],[y[2] - indicator_length, y[2], y[2], y[2] - indicator_length], lw=lw, c=color)
split_text = p_text.split(", ")
stacked_text = "\n".join(split_text)
ax.text((np.nanmean(x1)+np.nanmean(x2))*.5, y[0]+h , stacked_text, ha='center', va='bottom', color=color, fontsize = fontsize, weight = fontweight)
else:
#plot the text
if not stacked:
line = ax.plot([x1, x1, x2, x2], [y - indicator_length, y, y, y - indicator_length], lw=lw, c=color, clip_on = False)
text = ax.text((x1+x2)*.5, y+h , p_text, ha='center', va='bottom', color=color, fontsize = fontsize, weight = fontweight)
else:
line = ax.plot([x1, x1, x2, x2], [y - indicator_length, y, y, y - indicator_length], lw=lw, c=color, clip_on = False)
split_text = p_text.split(", ")
stacked_text = "\n".join(split_text)
text = ax.text((x1+x2)*.5, y+h , stacked_text, ha='center', va='bottom', color=color, fontsize = fontsize, weight = fontweight)
return line, text
def Stat_Annotation(ax, x1, x2, y, p_val, effect_size = None, h = 0, color = "grey", lw = .7, fontsize = 6, exact_p = False):
"""Legacy code. Use stat_annotation for more functionality"""
if p_val < 0.001 and not exact_p:
if effect_size != None:
ax.plot([x1, x1, x2, x2], [y, y, y, y], lw=lw, c=color)
ax.text((x1+x2)*.5, y+h , f"p < 0.001, d = {abs(effect_size):.2f}", ha='center', va='bottom', color=color, fontsize = fontsize, weight = "bold")
else:
ax.plot([x1, x1, x2, x2], [y, y, y, y], lw=lw, c=color)
ax.text((x1+x2)*.5, y+h , "p < 0.001", ha='center', va='bottom', color=color, fontsize = fontsize, weight = "bold")
elif p_val > 0.001 or exact_p:
if effect_size != None:
ax.plot([x1, x1, x2, x2], [y, y, y, y], lw=lw, c=color)
ax.text((x1+x2)*.5, y+h , f"p = {p_val:.3f}, d = {abs(effect_size):.2f}", ha='center', va='bottom', color=color, fontsize = fontsize, weight = "bold")
else:
ax.plot([x1, x1, x2, x2], [y, y, y, y], lw=lw, c=color)
ax.text((x1+x2)*.5, y+h , f"p = {p_val:.3f}", ha='center', va='bottom', color=color, fontsize = fontsize, weight = "bold")