forked from harp-lab/brainPH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistical_calculation_positive_negative.py
157 lines (139 loc) · 5.9 KB
/
statistical_calculation_positive_negative.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import json
from scipy.stats import f_oneway, ttest_rel
import matplotlib.pyplot as plt
import numpy as np
from statistics import mean
def get_anova_p_value(wd_645_1400, wd_1400_2500, wd_2500_645):
anova_result = f_oneway(wd_645_1400, wd_1400_2500, wd_2500_645)
return anova_result[1]
def get_t_values(wd_645_1400, wd_1400_2500, wd_2500_645):
t_values = []
t_values.append(ttest_rel(wd_645_1400, wd_1400_2500)[1])
t_values.append(ttest_rel(wd_1400_2500, wd_2500_645)[1])
t_values.append(ttest_rel(wd_2500_645, wd_645_1400)[1])
return [round(i, 6) for i in t_values]
def get_p_values(wd_645_1400, wd_1400_2500, wd_2500_645):
p_values = []
p_values.append(f_oneway(wd_645_1400, wd_1400_2500)[1])
p_values.append(f_oneway(wd_1400_2500, wd_2500_645)[1])
p_values.append(f_oneway(wd_2500_645, wd_645_1400)[1])
return [round(i, 6) for i in p_values]
def draw_line_chart(x, y, y_limit_bottom=0.0, y_limit_top=60.0,
x_limit_left=0, x_limit_right=320,
x_axis_label=None,
y_axis_label=None, legend=None, title=None):
plt.figure(figsize=(6, 3.5))
if legend:
plt.plot(x, y, label=legend)
plt.legend()
if x_axis_label:
plt.xlabel(x_axis_label)
if y_axis_label:
plt.ylabel(y_axis_label)
if title:
plt.title(title)
plt.ylim([y_limit_bottom, y_limit_top])
plt.xlim([x_limit_left, x_limit_right])
plt.tight_layout()
plt.show()
def draw_boxplots(labels, data, colors, x_axis_label=None, y_axis_label=None):
plt.figure(figsize=(6, 3.5))
bp = plt.boxplot(data, notch=True, vert=True,
patch_artist=True,
labels=labels,
medianprops={"color": "black"})
if x_axis_label:
plt.xlabel(x_axis_label)
if y_axis_label:
plt.ylabel(y_axis_label)
for box, color in zip(bp['boxes'], colors):
box.set_facecolor(color)
plt.tight_layout()
plt.show()
def plot_mds(mds_matrix, title, color, index):
x = mds_matrix[:, 0]
y = mds_matrix[:, 1]
ax = plt.subplot(1, 3, index)
ax.scatter(x, y, label=title, c=color)
ax.legend()
ax.set_title(title)
plt.tight_layout()
def get_distribution_distance(data, distance, total_subjects, reverse=False):
if reverse:
total_count = len(list(filter(lambda score: score >= distance, data)))
else:
total_count = len(list(filter(lambda score: score <= distance, data)))
percentage = (total_count / total_subjects) * 100
return f"Distance: {distance:3d}, number of subjects: {total_count:3d}, percentage: {percentage:.2f}%"
if __name__ == "__main__":
# output_dir = "output_positive"
output_dir = "output_negative"
distances_between_cohorts_data_file = f"{output_dir}/distances_between_cohorts_ws.json"
mds_mx645_data_file = f"{output_dir}/mds_mx645_ws.json"
mds_mx1400_data_file = f"{output_dir}/mds_mx1400_ws.json"
mds_std2500_data_file = f"{output_dir}/mds_std2500_ws.json"
with open(distances_between_cohorts_data_file) as fp:
distance_between_cohorts = json.load(fp)
wd_645_1400 = [distance[0] for distance in distance_between_cohorts]
wd_1400_2500 = [distance[1] for distance in distance_between_cohorts]
wd_2500_645 = [distance[2] for distance in distance_between_cohorts]
t_values = get_t_values(wd_645_1400, wd_1400_2500, wd_2500_645)
print("T-values:")
for value in t_values:
print(f"{value:.6f}", end=" ")
print("")
p_values = get_p_values(wd_645_1400, wd_1400_2500, wd_2500_645)
print("P-values:")
for value in p_values:
print(f"{value:.6f}", end=" ")
print("")
p_value = get_anova_p_value(wd_645_1400, wd_1400_2500, wd_2500_645)
p_value = round(p_value, 6)
print(f"ANOVA test p-value: {p_value:.6f}")
#
# mean_wd_645_1400 = round(mean(wd_645_1400), 3)
# print(f"Mean WD_MX645_MX1400: {mean_wd_645_1400}")
# mean_wd_1400_2500 = round(mean(wd_1400_2500), 3)
# print(f"Mean WD_MX1400_STD2500: {mean_wd_1400_2500}")
# mean_wd_2500_645 = round(mean(wd_2500_645), 3)
# print(f"Mean WD_STD2500_MX645: {mean_wd_2500_645}")
# distance_2 = get_distribution_distance(wd_645_1400, 2, 316)
# distance_5 = get_distribution_distance(wd_645_1400, 5, 316)
# distance_10 = get_distribution_distance(wd_645_1400, 10, 316, True)
# print(f"WD_MX645_MX1400: {distance_2}")
# print(f"WD_MX645_MX1400: {distance_5}")
# print(f"WD_MX645_MX1400: {distance_10}")
#
# subject_numbers = [i for i in range(1, 317)]
# # WD_mx645_mx1400
# draw_line_chart(subject_numbers, wd_645_1400, 0, 60, 0, 320,
# "Subject ID", "Distance", "WD(mx645 - mx1400)")
# boxplot_data = [wd_645_1400, wd_1400_2500, wd_2500_645]
# boxplot_labels = ["WD(645ms-1400ms)", "WD(1400ms-2500ms)",
# "WD(2500ms-645ms)"]
# boxplot_colors = ['orangered', 'lightblue', 'lightgreen']
# draw_boxplots(boxplot_labels, boxplot_data, boxplot_colors,
# "Distributions",
# "Distance")
# # WD_mx1400_std2500
# draw_line_chart(subject_numbers, wd_1400_2500, 0, 60, 0, 320,
# "Subject ID", "Distance", "WD(mx1400 - std2500)")
# # WD_std2500_mx645
# draw_line_chart(subject_numbers, wd_2500_645, 0, 60, 0, 320,
# "Subject ID", "Distance", "WD(mx1400 - std2500)")
#
# with open(mds_mx645_data_file) as fp:
# mds_mx645 = np.array(json.load(fp))
# with open(mds_mx1400_data_file) as fp:
# mds_mx1400 = np.array(json.load(fp))
# with open(mds_std2500_data_file) as fp:
# mds_std2500 = np.array(json.load(fp))
#
# title = f'mx645'
# plot_mds(mds_mx645, title, "red", 1)
# title = f'mx1400'
# plot_mds(mds_mx1400, title, "green", 2)
# title = f'std2500'
# plot_mds(mds_std2500, title, "purple", 3)
# plt.tight_layout()
# plt.show()