-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsig_freqs_summary.py
199 lines (148 loc) · 6.96 KB
/
sig_freqs_summary.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
"""Counts the number of features from each domain that were in significant comparisons above and below a threshold"""
from sys import argv
from pandas import DataFrame, Series, read_csv
from matplotlib.pyplot import subplots, savefig, title as set_title, xlabel, ylabel, legend
from os import mkdir
from os.path import join, isdir
from numpy import mean, std, min, max, isnan
from utils.utils import (
ADNIMERGE_KEY, EXPRESSION_KEY, MRI_KEY, ADNIMERGE_FREQ_KEY, EXPRESSION_FREQ_KEY, MRI_FREQ_KEY, TOTAL_FREQ_KEY,
DOMAIN_KEY
)
SUMMARY_DIR: str = 'data/sig-freqs-summary/'
AVG_KEY: str = 'Average'
STD_KEY: str = 'Standard Deviation'
MIN_KEY: str = 'Minimum'
MAX_KEY: str = 'Maximum'
IDX_COL: str = 'Domain'
def main():
"""Main method"""
analysis_name: str = argv[1]
n_histogram_bins: int = int(argv[2])
sig_freq_table_path: str = argv[3]
break_y: bool = argv[4] == 'true'
if not isdir(SUMMARY_DIR):
mkdir(SUMMARY_DIR)
significance_frequencies: DataFrame = read_csv(sig_freq_table_path)
print('Number Of Features That Show Up In At Least One Significance Comparison: {} | For Analysis: {}'.format(
len(significance_frequencies), analysis_name
))
save_histograms(
significance_frequencies=significance_frequencies, n_histogram_bins=n_histogram_bins,
analysis_name=analysis_name, break_y=break_y
)
for freq_key in [ADNIMERGE_FREQ_KEY, EXPRESSION_FREQ_KEY, MRI_FREQ_KEY, TOTAL_FREQ_KEY]:
save_tables(significance_frequencies=significance_frequencies, analysis_name=analysis_name, freq_key=freq_key)
def save_histograms(significance_frequencies: DataFrame, n_histogram_bins: int, analysis_name: str, break_y: bool):
"""Saves the histograms for the total set of frequencies and the set for each domain"""
save_histogram(
significance_frequencies=significance_frequencies, n_histogram_bins=n_histogram_bins,
analysis_name=analysis_name, title='Total', break_y=break_y, start_break_count=8000, end_break_count=70000,
max_count=78000
)
adnimerge_frequencies: DataFrame = significance_frequencies.loc[
significance_frequencies[DOMAIN_KEY] == ADNIMERGE_KEY
]
save_histogram(
significance_frequencies=adnimerge_frequencies, n_histogram_bins=n_histogram_bins, analysis_name=analysis_name,
title='ADNIMERGE', break_y=False
)
expression_frequencies: DataFrame = significance_frequencies.loc[
significance_frequencies[DOMAIN_KEY] == EXPRESSION_KEY
]
save_histogram(
significance_frequencies=expression_frequencies, n_histogram_bins=n_histogram_bins, analysis_name=analysis_name,
title='Gene Expression', break_y=False
)
mri_frequencies: DataFrame = significance_frequencies.loc[
significance_frequencies[DOMAIN_KEY] == MRI_KEY
]
save_histogram(
significance_frequencies=mri_frequencies, n_histogram_bins=n_histogram_bins, analysis_name=analysis_name,
title='MRI', break_y=break_y, start_break_count=7000, end_break_count=70000, max_count=77000
)
def save_histogram(
significance_frequencies: DataFrame, n_histogram_bins: int, analysis_name: str, title: str, break_y: bool,
start_break_count: int = None, end_break_count: int = None, max_count: int = None
):
"""Saves a histogram with a broken y-axis"""
adnimerge_frequencies: Series = significance_frequencies[ADNIMERGE_FREQ_KEY]
expression_frequencies: Series = significance_frequencies[EXPRESSION_FREQ_KEY]
mri_frequencies: Series = significance_frequencies[MRI_FREQ_KEY]
frequencies: list = [adnimerge_frequencies, expression_frequencies, mri_frequencies]
if break_y:
broken_y_histogram(
frequencies=frequencies, n_histogram_bins=n_histogram_bins, title=title, end_break_count=end_break_count,
max_count=max_count, start_break_count=start_break_count
)
else:
regular_histogram(frequencies=frequencies, n_histogram_bins=n_histogram_bins, title=title)
xlabel('Comparison Frequency')
ylabel('Number of Features')
save_path: str = join(
SUMMARY_DIR, 'significant-frequencies-{}-{}.png'.format(title.lower().replace(' ', ''), analysis_name)
)
savefig(save_path)
def broken_y_histogram(
frequencies: list, n_histogram_bins: int, title: str, end_break_count: int, max_count: int, start_break_count: int
):
"""Creates a histogram graph with a broken y-axis"""
fig, (ax1, ax2) = subplots(2, 1, sharex=True)
fig.subplots_adjust(hspace=0.08)
ax1.hist(frequencies, bins=n_histogram_bins, stacked=True, density=False)
ax2.hist(frequencies, bins=n_histogram_bins, stacked=True, density=False)
ax1.set_ylim(end_break_count, max_count)
ax1.set_title(title)
ax2.set_ylim(0, start_break_count)
def regular_histogram(frequencies: list, n_histogram_bins: int, title: str):
"""Creates a histogram without a broken y-axis"""
_, ax = subplots()
ax.hist(frequencies, bins=n_histogram_bins, stacked=True)
set_title(title)
legend({ADNIMERGE_KEY: "red", EXPRESSION_KEY: "blue", MRI_KEY: "violet"})
def save_tables(significance_frequencies: DataFrame, analysis_name: str, freq_key: str):
"""Creates the tables that complement the histograms"""
idx_col: list = [ADNIMERGE_KEY, EXPRESSION_KEY, MRI_KEY]
table: dict = {
IDX_COL: idx_col,
AVG_KEY: [0.0] * len(idx_col),
STD_KEY: [0.0] * len(idx_col),
MIN_KEY: [0.0] * len(idx_col),
MAX_KEY: [0.0] * len(idx_col)
}
table: DataFrame = DataFrame(table)
table: DataFrame = table.set_index(IDX_COL)
for domain in [ADNIMERGE_KEY, EXPRESSION_KEY, MRI_KEY]:
set_val(
table=table, domain=domain, op=mean, header=AVG_KEY, significance_frequencies=significance_frequencies,
freq_key=freq_key
)
set_val(
table=table, domain=domain, op=std, header=STD_KEY, significance_frequencies=significance_frequencies,
freq_key=freq_key
)
set_val(
table=table, domain=domain, op=min, header=MIN_KEY, significance_frequencies=significance_frequencies,
freq_key=freq_key
)
set_val(
table=table, domain=domain, op=max, header=MAX_KEY, significance_frequencies=significance_frequencies,
freq_key=freq_key
)
save_path: str = join(SUMMARY_DIR, 'basic-stats-{}-{}.csv'.format(freq_key.replace(' ', '').lower(), analysis_name))
table: DataFrame = table.round(2)
table.to_csv(save_path)
def set_val(
table: DataFrame, domain: str, op: callable, header: str, significance_frequencies: DataFrame, freq_key: str
):
"""Computes a value in the table with basic stats"""
frequencies: DataFrame = significance_frequencies.loc[
significance_frequencies[DOMAIN_KEY] == domain
]
frequencies: Series = frequencies[freq_key]
val: float = op(frequencies)
if isnan(val):
val: float = 0.0
table[header][domain] = val
if __name__ == '__main__':
main()