This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
py3diagnosticplot.py
161 lines (155 loc) · 5.98 KB
/
py3diagnosticplot.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
#!/usr/bin/python
import numpy as np
from scipy import stats
import pylab
pylab.ion()
import parsematlab
import loaddata
from iwafgui import Error, Info, SaveAs
def diagnosticPlot(name, values):
args = values['generation-args'][1]
errors = []
for key in ['responsewindow']:
label, value = args[key]
value = parsematlab.parse(value)
if isinstance(value, str):
errors.append(label + '\n ' + value.replace('\n', '\n '))
args[key] = value
if len(errors) > 0:
Error('\n\n'.join(errors))
return
response_window = args['responsewindow']
fnames = values['flist'][1]
removeanomalies = args['removeanomalies'][1]
weightfile = values['weightfile'][1]
data = []
type = []
samplingrate = None
try:
for fname in fnames:
result = loaddata.load_data(fname, response_window, None,
removeanomalies = removeanomalies)
if isinstance(result, str):
Error(result)
return
if samplingrate == None:
samplingrate = result[2]
if samplingrate != result[2]:
Error('Not all data files have the same sampling rate.')
return
data.append(result[0])
type.append(result[1])
if len(data) == 0 or len(type) == 0:
Error('You must select some data to plot.')
return
try:
data = np.concatenate(data)
except ValueError:
Error('Not all data files have the same number of channels.')
return
type = np.concatenate(type)
if weightfile:
weights = loaddata.load_weights(weightfile)
if isinstance(weights, str):
Error(weights)
return
classifier = np.zeros(data.shape[1:])
classifier[:weights.shape[0], :weights.shape[1]] = weights
classifier_max = max(abs(classifier.max()), abs(classifier.min()))
else:
classifier = None
if isinstance(classifier, str):
Error(classifier)
return
num_plots = 3 if classifier == None else 4
signed_r = np.zeros(data.shape[1:])
for row in range(signed_r.shape[0]):
for col in range(signed_r.shape[1]):
signed_r[row, col] = stats.linregress(
data[:, row, col], type
)[2]
signed_r_max = max(abs(signed_r.max()), abs(signed_r.min()))
x = np.arange(data.shape[1]) * 1000 / samplingrate
target = data[type.nonzero()[0]].mean(axis = 0)
nontarget = data[(~type).nonzero()[0]].mean(axis = 0)
vmin, vmax = ylim = [min(target.min(), nontarget.min()),
max(target.max(), nontarget.max())]
fig = pylab.figure()
fig.subplots_adjust(bottom = 0.06, top = 0.93, hspace = 0.45)
master_ax = ax = pylab.subplot(num_plots, 1, 1)
pylab.title('Target', fontsize = 'medium')
pylab.imshow(target.transpose(), interpolation = 'nearest',
cmap = 'PRGn', aspect = 'auto', vmin = vmin, vmax = vmax,
origin = 'lower', extent = (
0,
data.shape[1] * 1000 / samplingrate,
-0.5,
data.shape[2] - 0.5
)
)
pylab.xticks(fontsize = 'small')
pylab.yticks(range(data.shape[2]),
[str(i) for i in range(1, data.shape[2] + 1)],
fontsize = 'small')
pylab.axes(pylab.colorbar().ax)
pylab.yticks(fontsize = 'small')
ax = pylab.subplot(num_plots, 1, 2, sharex = master_ax,
sharey = master_ax)
pylab.title('Non-Target', fontsize = 'medium')
pylab.imshow(nontarget.transpose(), interpolation = 'nearest',
cmap = 'PRGn', aspect = 'auto', vmin = vmin, vmax = vmax,
origin = 'lower', extent = (
0,
data.shape[1] * 1000 / samplingrate,
-0.5,
data.shape[2] - 0.5
)
)
pylab.xticks(fontsize = 'small')
pylab.yticks(range(data.shape[2]),
[str(i) for i in range(1, data.shape[2] + 1)],
fontsize = 'small')
pylab.axes(pylab.colorbar().ax)
pylab.yticks(fontsize = 'small')
ax = pylab.subplot(num_plots, 1, 3, sharex = master_ax,
sharey = master_ax)
pylab.title('Correlation Coefficient', fontsize = 'medium')
pylab.imshow(signed_r.transpose(), interpolation = 'nearest',
cmap = 'PRGn', aspect = 'auto', vmin = -signed_r_max,
vmax = signed_r_max, origin = 'lower', extent = (
0,
data.shape[1] * 1000 / samplingrate,
-0.5,
data.shape[2] - 0.5
)
)
pylab.xticks(fontsize = 'small')
pylab.yticks(range(data.shape[2]),
[str(i) for i in range(1, data.shape[2] + 1)],
fontsize = 'small')
pylab.axes(pylab.colorbar().ax)
pylab.yticks(fontsize = 'small')
if classifier == None:
return
ax = pylab.subplot(num_plots, 1, 4, sharex = master_ax,
sharey = master_ax)
pylab.title('Classifier Weights', fontsize = 'medium')
pylab.imshow(classifier.transpose(), interpolation = 'nearest',
cmap = 'PRGn', aspect = 'auto', vmin = -classifier_max,
vmax = classifier_max, origin = 'lower', extent = (
0,
data.shape[1] * 1000 / samplingrate,
-0.5,
data.shape[2] - 0.5
)
)
pylab.xticks(fontsize = 'small')
pylab.yticks(range(data.shape[2]),
[str(i) for i in range(1, data.shape[2] + 1)],
fontsize = 'small')
pylab.axes(pylab.colorbar().ax)
pylab.yticks(fontsize = 'small')
except MemoryError:
Error('Could not fit all the selected data in memory.\n' + \
'Try loading fewer data files.')
return