-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsyscheck.py
More file actions
321 lines (266 loc) · 10.8 KB
/
Copy pathsyscheck.py
File metadata and controls
321 lines (266 loc) · 10.8 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# -*- coding: utf-8 -*-
'''
System Check
PyCorder ActiChamp Recorder
------------------------------------------------------------
Copyright (C) 2010, Brain Products GmbH, Gilching
This file is part of PyCorder
PyCorder is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PyCorder. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------
@author: Norbert Hauser
@date: $Date: 2011-03-24 16:03:45 +0100 (Do, 24 Mrz 2011) $
@version: 1.0
B{Revision:} $LastChangedRevision: 62 $
'''
import os, sys, traceback, platform
import datetime
from loadlibs import *
__version__ = "0.90.0"
'''Application Version'''
logentries = ""
def GetExceptionTraceBack():
''' Get last trace back info as tuple
@return: tuple(string representation, filename, line number, module)
'''
exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
tb = traceback.extract_tb(exceptionTraceback)[-1]
fn = os.path.split(tb[0])[1]
txt = "%s, line %d, %s"%(fn, tb[1], tb[2])
return tuple([txt, fn, tb[1], tb[2]])
def logIt(logentry):
''' Print and collect log entries
@param logentry: log text
'''
global logentries
print logentry
logentries += logentry + "\r\n"
def logHeader():
''' Create header for log entries
'''
logIt("PyCorder System Check")
logIt("=====================")
logIt(datetime.datetime.now().strftime("%A, %d. %B %Y %I:%M%p\r\n"))
def checkOS():
''' Get operating system infos
'''
logIt("Operating System: %s"%(platform.platform()))
logIt("Processor: %s"%(platform.processor()))
logIt("Python: %s"%(sys.version))
# required libs available?
if len(import_log) == 0:
# yes, print versions
logIt(" NumPy: %s"%(np.__version__))
logIt(" SciPy: %s"%(sc.__version__))
logIt(" PyQt: %s"%(Qt.QT_VERSION_STR))
logIt(" PyQwt: %s"%(Qwt.QWT_VERSION_STR))
logIt(" lxml: %s"%(etree.__version__))
else:
logIt("One of the following modules are missing or have the wrong version:")
logIt(import_log)
raise Exception, "Python Libraries Missmatch"
def checkAmplifierBase():
''' Check amplifier base functionality
'''
amp = None
logIt("ActiCHamp Base Functionality")
try:
log = " Load DLL: "
amp = ActiChamp()
logIt(log+"OK")
log = " Connect HW: "
amp.open()
logIt(log+"OK")
log = " Initialize HW: "
rate = CHAMP_RATE_10KHZ
amp.setup(CHAMP_MODE_NORMAL, rate, 1)
logIt(log+"OK")
log = " HW Configuration: "
log += "EEG=%d, AUX=%d, TRG_IN=%d, TRG_OUT=%d "%(amp.properties.CountEeg,
amp.properties.CountAux,
amp.properties.TriggersIn,
amp.properties.TriggersOut)
if amp.properties.CountEeg in range(32,161,32) and \
amp.properties.CountAux == 8 and \
amp.properties.TriggersIn == 8 and \
amp.properties.TriggersOut == 8:
logIt(log+"OK")
else:
raise Exception, "HW Configuration Missmatch"
log = " Start Acquisition: "
amp.start()
logIt(log+"OK")
# read 2s of data
print " ... collecting data, please wait (~5s)"
log = " Read Data: "
eeg, trg, sct, atime, ptime, errors = readAmplifierData(amp, 2.0,
amp.properties.CountEeg,
amp.properties.CountAux)
logIt(log+"OK")
log = " Stop Acquisition: "
amp.stop()
logIt(log+"OK")
totalSamples = sct.shape[1]
sampleTime = ptime / totalSamples
utilization = sampleTime * sample_rate[rate] * 100
missingSamples = (sct[0][-1] - sct[0][0] + 1) - totalSamples
# check sample counter
if missingSamples == 0:
logIt(" Samplecounter: OK")
else:
logIt(" Samplecounter: %d missing samples FAILED"%(missingSamples))
# check device errors
if errors == 0:
logIt(" Device Errors: 0 OK")
else:
logIt(" Device Errors: %d FAILED"%(errors))
# check processing time
log = " Processing Time: %.0f%% "%(utilization)
if utilization < 50.0:
logIt(log + "OK")
else:
logIt(log + "FAILED")
# TEST only
if False:
eeg[0] = 0.0
eeg[2] *= 3.0
eeg[5] = 0.0
eeg[16] *= 0.8
eeg[66] = 0.0
# remove DC
cut = 5.0 / sample_rate[rate] * 2.0
b,a = signal.filter_design.butter(2, cut, 'high')
eeg = signal.lfilter(b, a, eeg)
eeg = eeg[:,eeg.shape[1]/2:]
# check for channels shorted and abnormal values
rms = np.sqrt(np.mean(eeg*eeg, 1)) # calculate RMS
rms_eeg = rms[:amp.properties.CountEeg] # split eeg and aux channels
rms_aux = rms[amp.properties.CountEeg:]
#print rms_eeg
#print rms_aux
def checkRms(rms_values, rms_limit):
# channels shorted (rms < limit)?
mask = lambda x: (x < rms_limit)
shorted = np.array(map(mask, rms_values))
# search for abnormal values ( > +/-2*SD)
channels_ok = np.nonzero(rms_values > rms_limit)
num_outlier = 0
while True:
sd = np.std(rms_values[channels_ok])
mean = rms_values[channels_ok].mean()
mask = lambda x: ((x > mean + 3.0*sd) or (x < mean - 3.0*sd)) and x > rms_limit
outlier = np.array(map(mask, rms_values))
channels_ok = ~(shorted | outlier)
if num_outlier == len(outlier):
break
num_outlier = len(outlier)
return shorted, outlier
eeg_shorted, eeg_outlier = checkRms(rms_eeg, 0.1)
aux_shorted, aux_outlier = checkRms(rms_aux, 0.1)
# create channel display
def createDispString(shorted, outlier, groupsize):
channels = len(shorted)
out_string = []
for group in range(channels / groupsize):
disp = []
for n in range(group * groupsize, (group+1) * groupsize):
if n in np.nonzero(shorted)[0]:
disp.append("x")
else:
if n in np.nonzero(outlier)[0]:
disp.append("?")
else:
disp.append("-")
out_string.append("%d %s"%(group+1, "".join(disp)))
return out_string
log = " Channel Data: "
if np.any([eeg_shorted, eeg_outlier]) or np.any([aux_shorted, aux_outlier]):
eeg_disp = createDispString(eeg_shorted, eeg_outlier, 32)
aux_disp = createDispString(aux_shorted, aux_outlier, 8)
log += "x=shorted, ?=outlier FAILED"
log += "\n EEG channels\n 12345678901234567890123456789012\n"
for n in range(len(eeg_disp)):
log += " " + eeg_disp[n] + "\n"
log += " AUX channels\n 12345678\n"
log += " " + aux_disp[0]
else:
log += "OK"
logIt(log)
#print eeg_shorted, eeg_outlier, aux_shorted, aux_outlier
# close device
log = " Close Device: "
amp.close()
logIt(log+"OK")
except Exception as e:
logIt(log + "FAILED")
try:
if amp != None:
amp.close()
except:
pass
raise e
def readAmplifierData(amp, duration, eegChannels, auxChannels):
''' Read data stream from amplifier
@param amp: amplifier object
@param duration: data size in seconds
@param eegChannels: number of eeg channels to read
@param auxChannels: number of aux channels to read
@return: channel data, trigger, sample counter,
acquisition time, processing time, device errors
'''
dataChunks = int(duration / 0.05)
# discard first second
for n in range(0, 20):
time.sleep(0.05)
d, disconnected = amp.read(range(eegChannels + auxChannels),
eegChannels,
auxChannels)
if d == None:
raise Exception, "No data transfer from amplifier"
# get initial error counter
initialErrors = amp.getDeviceStatus()[1]
# read data
t = time.clock()
processingTime = 0
for n in range(0, dataChunks):
time.sleep(0.05)
tp = time.clock()
d, disconnected = amp.read(range(eegChannels + auxChannels),
eegChannels,
auxChannels)
processingTime += time.clock() - tp
if d == None:
raise Exception, "No data transfer from amplifier"
if n == 0:
eeg = d[0]
trg = d[1]
sct = d[2]
else:
eeg = np.append(eeg, d[0], 1)
trg = np.append(trg, d[1], 1)
sct = np.append(sct, d[2], 1)
acquisitionTime = (time.clock()-t)*1000.0
# get device error counter
deviceErrors = amp.getDeviceStatus()[1] - initialErrors
return eeg, trg, sct, acquisitionTime, processingTime, deviceErrors
if __name__ == '__main__':
try:
logHeader()
checkOS()
from actichamp_w import *
from scipy import signal
checkAmplifierBase()
except Exception as e:
tb = GetExceptionTraceBack()[0]
logIt("\nERROR: " + tb + " -> " + str(e))
logIt("Can't proceed system check")
raw_input("\nPress RETURN to close this window ..." )
sys.exit(1)