-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRespiRate.py
More file actions
778 lines (698 loc) · 32.9 KB
/
RespiRate.py
File metadata and controls
778 lines (698 loc) · 32.9 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
# -*- coding: utf-8 -*-
# RespiRate.py
# Copyright (C) 2017 Ashlar Ruby
# Licensed under the MIT license. See COPYING.md for details.
import ctypes
import itertools
from os import getcwd, makedirs, path
from platform import platform, system
import sys
from traceback import print_exc
import cv2
import numpy as np
from PyQt5 import QtWidgets, QtCore, QtGui
import requests
from RespiRateUI import Ui_MainWindow
import MouseVideo as mv
import MouseFunctions as mf
import peakdetect as pd
import matplotlib.pyplot as plt
from matplotlib import rcParams
from notifiCat import errorNotif, askQuestion, infoNotif
class Gui(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.videoFrame.setAlignment(QtCore.Qt.AlignCenter)
self.ui.actionOpen_video.triggered.connect(self.openNew)
self.ui.actionOpen_spreadsheet.triggered.connect(self.openOutput)
self.ui.actionQuit.triggered.connect(self.closeAll)
self.ui.actionCheck_for_Update.triggered.connect(self.updateCheck)
self.ui.actionAbout.triggered.connect(self.About)
self.ui.actionAbout_Qt.triggered.connect(self.AboutQt)
self.ui.actionConvert_to_CSV.triggered.connect(self.csvConvert)
self.slide = self.ui.horizontalSlider
self.slide.valueChanged.connect(self.slider_value_change)
self.ui.pushButton_Play.clicked.connect(self.playPressed)
self.ui.pushButton_Pause.clicked.connect(self.pausePressed)
self.ui.pushButton_SelectST.clicked.connect(self.enterStartT)
self.ui.pushButton_Contour.clicked.connect(self.contourPressed)
self.ui.pushButton_Clear.clicked.connect(
self.ui.textBrowser_Output.clear)
self.ui.lineEdit_mouseID.textChanged.connect(self.enableButton)
self.ui.lineEdit_startT.textChanged.connect(self.enableButton2)
self.ui.lineEdit_lenMeasure.textChanged.connect(self.enableButton3)
self.ui.pushButton_Contour.setEnabled(False)
self.ui.pushButton_SelectST.setEnabled(False)
self.ui.textBrowser_Output.setReadOnly(True)
# Set up a config file
makedirs(path.join(path.expanduser('~'), '.RespiRate'), exist_ok=True)
self.dir = path.abspath(path.join(path.expanduser('~'), '.RespiRate'))
self.config = path.abspath(path.join(self.dir, 'RRconf.txt'))
rcParams['figure.figsize'] = 14, 2
self.capture = 0
self.cont = 0
self.currentFrame = np.array([])
self.displayTime = 0
self.enableCount = 0
self.enableCount2 = 0
self.enableCount3 = 0
self.endTimemsec = 0
self.TIME = 0
self.filename = 0
self.firstframe = 0
self.img = 0
self.lastframe = 0
self.length = 0
self.lenOfMeas = 0
self.numberOfMice = 0
self.sliderVal = 0
self.startTimemsec = 0
self.test = errorCheck
self.timeInSec = 0
self.totalCount = 0
self.vid_dir = getcwd()
self.version = '0.0.7'
self._process = QtCore.QProcess(self)
self._timer = QtCore.QTimer(self)
if self.cont == 0:
self._timer.timeout.connect(self.captureNextFrame)
else:
self._timer.timeout.connect(self.opticalFlowTrack)
self.load_config()
self.update()
def load_config(self):
"""Open and read the config file."""
# If the file does not exist (first run)
if path.isfile(self.config) is False:
with open(self.config, 'w+') as f:
f.close()
# The file exist - get the location of the last loaded video.
elif path.isfile(self.config) is True:
with open(self.config, 'r+') as f:
# We need try/except in case the config file is empty
try:
data = f.read().splitlines()
self.vid_dir = data[0]
except IndexError as excpt:
# Return exception to terminal if run as script, but
# otherwise don't bother the user.
print('Config load error: ', excpt.__class__.__name__)
print_exc()
def updateCheck(self):
"""Check for updates."""
resp = requests.get(
'https://github.com/MDA-Courtyard/RespiRate/releases/')
data = str(resp.text)
index = data.find('RespiRate/releases/tag/v')
upstream_ver = data[index + 32] + data[index + 33] + data[
index + 34] + data[index + 35] + data[index + 36]
if upstream_ver > self.version:
url = str(
"https://github.com/MDA-Courtyard/RespiRate/releases/tag/v" +
upstream_ver)
msg_up = (
'<p>Version ' + upstream_ver + 'has been released.'
'<br>Download it from <a href="%s">here</a>.</br></p>' % url)
infoNotif(self, 'RespiRate', msg_up)
else:
msg_up = 'You have the latest available version of RespiRate.'
infoNotif(self, 'RespiRate', msg_up)
def About(self):
"""Brief description of the program."""
issuesUrl = 'href="https://github.com/MDA-Courtyard/RespiRate/issues"'
licenseUrl = ('href="https://github.com/MDA-Courtyard/RespiRate/blob/'
'master/COPYING.md"')
title = 'About'
msg = (
'<p><br><b>RespiRate v' + self.version + '</b></br>'
'<br>Copyright (C) 2017 Ashlar Ruby</br>'
'<br>Licensed under the MIT license.</br></p>'
'<p>This program uses opencv_ffmpeg320 libraries, released'
'<br>and copyrighted 2001 under the LGPL by Fabrice Bellard,</br>'
'<br>and the PyQt5 collection of graphical toolkits.</br></p>'
'<p><a href="https://github.com/MDA-Courtyard/RespiRate">Home Page</a>'
'<br><a %s>Help</a></br>'
'<br><a %s>Contributors and License</a></br></p>' %
(issuesUrl, licenseUrl))
QtWidgets.QMessageBox.about(self, title, msg)
def AboutQt(self):
"""Show a message box about Qt (useful for debugging)."""
QtWidgets.QMessageBox.aboutQt(self)
def closeAll(self):
"""Shut down cleanly when Quit is clicked."""
if isinstance(self.capture, int) is False: # A video is loaded
self.capture.release()
cv2.destroyAllWindows()
self._timer.stop()
sys.exit(0)
def csvConvert(self):
"""Select the output .xlsx spreadsheet to convert to .csv file"""
location = path.join(path.expanduser('~'), 'RespiRate')
wbook = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File',
location)
wbook = str(wbook[0])
if wbook is not '': # Make sure the file exists
msg = (
'<br>Convert spreadsheet to csv file?</br>'
'<b><br>Warning: this will overwrite any existing csv files</br>'
'<br>with the same name in the RespiRate folder!</br></b>')
conv = askQuestion(self, 'RespiRate', msg)
if conv == 'yes':
mf.convertCSV(self, location, wbook)
def openNew(self):
"""
Select and open a video file from the system file manager for analysis.
"""
self._timer.stop()
self.filename = QtWidgets.QFileDialog.getOpenFileName(
self, 'Open Video', self.vid_dir)
self.filename = str(self.filename[0])
# Has the video been loaded properly?
# We could use fileCheck just as well here, but let's keep the two tests
# distinct for now.
if self.test.nameCheck(self) == 'error':
# Make sure the select start time button is disabled
self.ui.pushButton_SelectST.setEnabled(False)
return
print(self.filename + '\n')
self.vid_dir = path.dirname(path.realpath(self.filename))
self.capture = cv2.VideoCapture(self.filename)
self.capture.open(self.filename)
# Is the file a readable video file?
if self.test.fileCheck(self) == 'error':
# Make sure the select start time button is disabled
self.ui.pushButton_SelectST.setEnabled(False)
return
self.length = int(self.capture.get(7) / self.capture.get(5))
self.endTimemsec = self.length * 1000
self.slide.setMinimum(0)
self.slide.setMaximum(self.length)
self.slide.setTracking(0)
self.ui.lcdNumber.setNumDigits(8)
self.ui.lcdNumber.display('00:00:00')
self.slide.setSliderPosition(0)
self.cont = 0
title = 'RespiRate - ' + self.filename
self.setWindowTitle(title)
self.TIME = self.endTimemsec
self.ui.pushButton_SelectST.setEnabled(True)
self.writeConfig()
self.captureNextFrame()
def openOutput(self):
"""Open the spreadsheet to which mouse data is exported."""
location = path.join(path.expanduser('~'), 'RespiRate')
ssheet = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File',
location)
ssheet = str(ssheet[0])
if ssheet != '':
print('Opening %r' % ssheet)
if system() is 'Windows':
# We have to run the command through a shell in Windows
self._process.start('cmd.exe', ['/c', 'start', ssheet])
elif system() is 'Darwin':
self._process.start('open', [ssheet])
elif system() is 'Linux':
self._process.start('xdg-open', [ssheet])
def captureNextFrame(self):
"""Capture frame and reverse RGB BGR and return opencv image"""
if self.cont == 0:
_, readFrame = self.capture.read()
self.currentFrame = cv2.cvtColor(readFrame, cv2.COLOR_BGR2RGB)
height, width = self.currentFrame.shape[:2]
self.img = QtGui.QImage(self.currentFrame, width, height,
QtGui.QImage.Format_RGB888)
self.img = QtGui.QPixmap(self.img)
self.ui.videoFrame.setPixmap(
self.img.scaled(self.ui.videoFrame.size(),
QtCore.Qt.KeepAspectRatio,
QtCore.Qt.SmoothTransformation))
if self.capture.get(0) > self.endTimemsec:
self.capture.set(0, self.startTimemsec)
self._timer.timeout.connect(self.tick)
self._timer.start()
elif self.cont == 1:
_, readFrame = self.capture.read()
self.currentFrame = cv2.cvtColor(readFrame, cv2.COLOR_BGR2RGB)
height, width = self.currentFrame.shape[:2]
self.img = QtGui.QImage(self.currentFrame, width, height,
QtGui.QImage.Format_RGB888)
self.img = QtGui.QPixmap(self.img)
self.ui.videoFrame.setPixmap(
self.img.scaled(self.ui.videoFrame.size(),
QtCore.Qt.KeepAspectRatio,
QtCore.Qt.SmoothTransformation))
if self.capture.get(0) >= self.endTimemsec:
self.capture.set(0, self.startTimemsec)
self._timer.timeout.connect(self.tick)
self._timer.start()
elif self.cont == 2:
self._timer.stop()
def slider_value_change(self, value):
"""If the slide is moved manually, skip the video to the corresponding time."""
self.sliderVal = value * 1000
self.capture.set(0, self.sliderVal)
def tick(self):
"""Change time display and slider location in main window."""
timecv = self.capture.get(0)
self.timeInSec = timecv / 1000
self.displayTime = QtCore.QTime((timecv / 3600000) % 60,
(timecv / 60000) % 60,
(timecv / 1000) % 60)
self.ui.lcdNumber.display(self.displayTime.toString('hh:mm:ss'))
self.slide.setSliderPosition(self.timeInSec)
def pausePressed(self):
"""Pause the video in the main window."""
self._timer.stop()
def playPressed(self):
"""Play the video in the main window."""
if self.test.nameCheck(self) == 'error' or 'None':
return
elif self.test.fileCheck(self) == 'error':
return
self._timer.start()
def writeConfig(self):
"""Write data to config file for future runs."""
with open(self.config, 'w') as conf:
try:
conf.writelines(self.vid_dir)
except TypeError as excpt:
# Return exception to terminal if run as script, but otherwise
# don't bother user.
print('Config write error: ', excpt.__class__.__name__)
print_exc()
def enterStartT(self):
"""Select the current video time as start time of measurement."""
self.ui.lineEdit_startT.setText(self.displayTime.toString('hh:mm:ss'))
def enableButton(self):
"""Enables the Contour button when all three fields are filled."""
self.enableCount = 1
self.totalCount = self.enableCount + self.enableCount2 + self.enableCount3
if self.totalCount == 3:
self.ui.pushButton_Contour.setEnabled(True)
def enableButton2(self):
"""Enables the Contour button when all three fields are filled."""
self.enableCount2 = 1
self.totalCount = self.enableCount + self.enableCount2 + self.enableCount3
if self.totalCount == 3:
self.ui.pushButton_Contour.setEnabled(True)
def enableButton3(self):
"""Enables the Contour button when all three fields are filled."""
self.enableCount3 = 1
self.totalCount = self.enableCount + self.enableCount2 + self.enableCount3
if self.totalCount == 3:
self.ui.pushButton_Contour.setEnabled(True)
def contourPressed(self):
"""Contour button has been pressed - begin analysis."""
self.cont = 1
# Get the number of mice, start time, and length of measurement.
mice = self.ui.lineEdit_mouseID.text()
tsec = self.ui.lineEdit_startT.text()
self.lenOfMeas = self.ui.lineEdit_lenMeasure.text()
# Do a quick scan for errors and halt execution if necessary:
# Was a video loaded before 'Contour' was pressed?
if self.test.nameCheck(self) == 'error':
return
elif self.test.fileCheck(self) == 'error':
return
# Has the time been typed incorrectly?
elif self.test.timeCheck(self, tsec) == 'error':
return
# Is the given length of measurement an integer?
elif self.test.measLen(self) == 'error':
return
# Number of mice we are using.
self.numberOfMice = len(mice.split(','))
startTimehhmmss = [int(x) for x in tsec.split(':')]
# Time (in seconds) that we begin the measurement
startTimeSec = (3600 * startTimehhmmss[0]) + (
60 * startTimehhmmss[1]) + startTimehhmmss[2]
self.startTimemsec = startTimeSec * 1000 # start-time in miliseconds
lenOfMeasmsec = int(self.lenOfMeas) * 1000
self.endTimemsec = self.startTimemsec + lenOfMeasmsec # ending time
endTime = self.endTimemsec
self.capture.set(0, self.endTimemsec)
self.lastframe = self.capture.get(1)
self.capture.set(0, self.startTimemsec)
self.firstframe = self.capture.get(1)
self.length = int(self.lenOfMeas)
# Reset slide to show length of analysis time.
self.slide.setMinimum(startTimeSec)
self.slide.setMaximum(self.length + startTimeSec)
self.slide.setSliderPosition(startTimeSec)
self._timer.start()
incontours = []
mouseNumList = []
respRates = []
bRespRates = []
minstdevs = []
try:
# Get a unique mouse ID for each mouse.
for numba in range(0, self.numberOfMice):
vid = mv.frameReader(self.filename)
img = vid.getFrame(self.firstframe)
conty = mv.contour(img)
mouseNum, ok = QtWidgets.QInputDialog.getText(
self, 'Mouse ID', 'Please enter the Mouse #')
if ok and mouseNum:
print('Mouse ID: ', mouseNum)
mouseNumList.append(mouseNum)
inconty = mv.insideContour(conty, img)
incontours.append(inconty)
else:
# If the user presses Cancel, close the contour window
cv2.destroyWindow('Click the outline of your ROI:')
return
self.cont = 2
# Parameters for Shi-Tomasi corner detection
feature_params = dict(maxCorners=10,
qualityLevel=0.3,
minDistance=7,
blockSize=7)
# Parameters for Lucas-Kanade optical flow
lk_params = dict(winSize=(15, 15),
maxLevel=2,
criteria=(cv2.TERM_CRITERIA_EPS
| cv2.TERM_CRITERIA_COUNT, 10, 0.03))
# Create some random colors
color = np.random.randint(0, 255, (100, 3))
self.capture.set(1, self.firstframe)
# Take first frame and find corners in it
_, old_frame = self.capture.read()
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
p0s = mf.ListOfLists(self.numberOfMice)
for numba in range(0, self.numberOfMice):
p0s[numba] = cv2.goodFeaturesToTrack(old_gray,
mask=incontours[numba],
**feature_params)
# Create a mask image for drawing purposes
mask = np.zeros_like(old_frame)
xPoints = mf.ListOfLists(self.numberOfMice)
yPoints = mf.ListOfLists(self.numberOfMice)
p1s = mf.ListOfLists(self.numberOfMice)
sts = mf.ListOfLists(self.numberOfMice)
errs = mf.ListOfLists(self.numberOfMice)
good_news = mf.ListOfLists(self.numberOfMice)
good_olds = mf.ListOfLists(self.numberOfMice)
dists = mf.ListOfLists(self.numberOfMice)
pointPos = mf.ListOfLists(self.numberOfMice)
peakPos = mf.ListOfLists(self.numberOfMice)
for numba in range(0, self.numberOfMice):
for i in range(len(p0s[numba])):
xPoints[numba].append([])
yPoints[numba].append([])
l = 0
for num in range(np.int(self.firstframe),
np.int(self.lastframe + 1)):
_, frame = self.capture.read()
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
stzeros = []
for numba in range(0, self.numberOfMice):
stzeros.append(np.array([]))
for numba in range(0, self.numberOfMice):
# Calculate optical flow
p1s[numba], sts[numba], errs[
numba] = cv2.calcOpticalFlowPyrLK(
old_gray, frame_gray, p0s[numba], None,
**lk_params)
# Select good points
if np.any(sts[numba]) == 0:
break
good_news[numba] = p1s[numba][sts[numba] == 1]
good_olds[numba] = p0s[numba][sts[numba] == 1]
if len(p0s[numba]) != len(good_olds[numba]):
stzeros[numba] = np.where(sts[numba] == 0)[0]
for j in stzeros[numba][::-1]:
for i in range(len(p0s[numba])):
if i == j:
del xPoints[numba][i]
del yPoints[numba][i]
# Draw the tracks
for i, (new, old) in enumerate(
list(zip(good_news[numba], good_olds[numba]))):
a, b = new.ravel()
c, d = old.ravel()
xPoints[numba][i].append([])
yPoints[numba][i].append([])
xPoints[numba][i][l].append(c)
yPoints[numba][i][l].append(d)
mask = cv2.line(mask, (a, b), (c, d),
color[i].tolist(), 2)
frame = cv2.circle(frame, (a, b), 5, color[i].tolist(),
-1)
p0s[numba] = good_news[numba].reshape(
-1, 1, 2) # Update the previous points
if np.any(sts[numba]) == 0:
endTime = self.capture.get(0)
break
# Show the respiratory motion in a new window
img = cv2.add(frame, mask)
cv2.imshow('flow', img)
k = cv2.waitKey(30) & 0xff
if k == 27:
endTime = self.capture.get(0)
break
# Now update the previous frame
old_gray = frame_gray.copy()
l = l + 1
for numba in range(0, self.numberOfMice):
pointx = mf.ListOfLists(len(p0s[numba]))
pointy = mf.ListOfLists(len(p0s[numba]))
peaksx = mf.ListOfLists(len(p0s[numba]))
peaksy = mf.ListOfLists(len(p0s[numba]))
distBTpeaksxt = mf.ListOfLists(len(p0s[numba]))
distBTpeaksxb = mf.ListOfLists(len(p0s[numba]))
distBTpeaksyt = mf.ListOfLists(len(p0s[numba]))
distBTpeaksyb = mf.ListOfLists(len(p0s[numba]))
avgs = mf.ListOfLists(len(p0s[numba]))
stdevs = mf.ListOfLists(len(p0s[numba]))
for i in range(len(p0s[numba])):
pointx[i] = xPoints[numba][i]
peaksx[i] = pd.peakdetect(pointx[i], None, 4,
0) # 3 for mice, 20 for human
pointy[i] = yPoints[numba][i]
peaksy[i] = pd.peakdetect(pointy[i], None, 4, 0)
for ii in range(len(p0s[numba])):
for i in range(len(peaksx[ii][0]) - 1):
distBTpeaksxt[ii].append(peaksx[ii][0][i + 1][0] -
peaksx[ii][0][i][0])
for i in range(len(peaksx[ii][1]) - 1):
distBTpeaksxb[ii].append(peaksx[ii][1][i + 1][0] -
peaksx[ii][1][i][0])
for i in range(len(peaksy[ii][0]) - 1):
distBTpeaksyt[ii].append(peaksy[ii][0][i + 1][0] -
peaksy[ii][0][i][0])
for i in range(len(peaksy[ii][1]) - 1):
distBTpeaksyb[ii].append(peaksy[ii][1][i + 1][0] -
peaksy[ii][1][i][0])
for i in range(len(p0s[numba])):
avgs[i].append(
sum(distBTpeaksxt[i]) / len(distBTpeaksxt[i])) # xtop
stdevs[i].append(np.std(distBTpeaksxt[i]) /
avgs[i][0]) # xtop
avgs[i].append(
sum(distBTpeaksxb[i]) /
len(distBTpeaksxb[i])) # xbottom
stdevs[i].append(np.std(distBTpeaksxb[i]) /
avgs[i][1]) # xbottom
avgs[i].append(
sum(distBTpeaksyt[i]) / len(distBTpeaksyt[i])) # ytop
stdevs[i].append(np.std(distBTpeaksyt[i]) /
avgs[i][2]) # ytop
avgs[i].append(
sum(distBTpeaksyb[i]) /
len(distBTpeaksyb[i])) # ybottom
stdevs[i].append(np.std(distBTpeaksyb[i]) /
avgs[i][3]) # ybottom
merged = list(itertools.chain.from_iterable(stdevs))
minst = min(merged)
matches = [match for match in mf.find(stdevs, minst)]
bestPoints = list(zip(*matches))[0]
distInd = list(zip(*matches))[1]
best = mf.ListOfLists(len(p0s[numba]))
beststdev = mf.ListOfLists(len(p0s[numba]))
for ii in range(len(p0s[numba])):
minimummy = min(stdevs[ii])
mins = [
i for i, j in enumerate(stdevs[ii]) if j == minimummy
]
for i in range(len(mins)):
best[ii] = avgs[ii][mins[i]]
beststdev[ii] = stdevs[ii][mins[i]]
minstdev = min(beststdev)
indMinStdev = [
i for i, j in enumerate(beststdev) if j == minstdev
]
bavg = [best[i] for i in indMinStdev]
avgOfbest = sum(best) / len(best)
avgOfbavg = sum(bavg) / len(bavg)
font = {
'family': 'sans-serif',
'color': 'black',
'weight': 'normal',
'size': 12
}
# Plotting
print('distInd', distInd)
xaxis = range(0, len(pointx[numba]))
xaxisYtop = list(zip(*peaksy[numba][0]))[0]
xaxisYbottom = list(zip(*peaksy[numba][1]))[0]
xaxisT = [x / 30 for x in xaxis]
xaxisYtopT = [x / 30 for x in xaxisYtop]
xaxisYbottomT = [x / 30 for x in xaxisYbottom]
# Close open figures with same name to prevent bad formatting
if plt.fignum_exists('Mouse %s' % mouseNumList[numba]):
plt.close('Mouse %s' % mouseNumList[numba])
plt.ion() # cosmetic, QCoreApplication error without this
plt.figure('Mouse %s' % mouseNumList[numba]) # Window name
plt.plot(xaxisT, pointy[numba], color='#3399FF') # Curve
plt.plot(xaxisYtopT,
list(zip(*peaksy[numba][0]))[1],
'o',
color='red',
markersize=4) # Peak
plt.plot(xaxisYbottomT,
list(zip(*peaksy[numba][1]))[1],
'o',
color='#FF9966',
markersize=4) # Valley
plt.title('Mouse ' + str(mouseNumList[numba]), fontdict=font)
pointPos[numba].append(pointy[numba])
peakPos[numba].append(peaksy[numba])
plt.xlabel('Time (s)', fontdict=font)
frame1 = plt.gca()
frame1.axes.get_yaxis().set_ticks([])
plt.tight_layout() # Don't cut off the title and x-label
plt.show()
respRate = (60 * 30) / avgOfbest
bRespRate = (60 * 30) / avgOfbavg
# Output respiratory rate and stdev to console; don't show if
# running standalone
print('\nBest: ' + str([round(elem, 3) for elem in best]))
print('Best Stdev: ' +
str([round(elem, 4) for elem in beststdev]))
print('Best Respiratory Rate: ', bRespRate, '\n')
respRates.append(respRate)
bRespRates.append(bRespRate)
minstdevs.append(minstdev)
for i, (bp, ind) in enumerate(list(zip(bestPoints, distInd))):
if ind == 0:
dists[numba].append(distBTpeaksxt[bp])
if ind == 1:
dists[numba].append(distBTpeaksxb[bp])
if ind == 2:
dists[numba].append(distBTpeaksyt[bp])
if ind == 3:
dists[numba].append(distBTpeaksyb[bp])
# Show results in main window
for numba in range(0, self.numberOfMice):
toPrint1 = mouseNumList[numba]
toPrint2 = round(bRespRates[numba], 2)
toPrint3 = round(minstdevs[numba], 4)
out = ' {:<18} {:^21.2f} {:>21.4f} '.format(
toPrint1, toPrint2, toPrint3)
self.ui.textBrowser_Output.append(out)
# Measurement terminated before full run time
if endTime != self.endTimemsec:
errorNotif(
self,
'<br>Measurement did not run for entire set length.</br>')
# Ask if we want to export data to a spreadsheet
export = askQuestion(self, 'RespiRate',
'<br>Export data to spreadsheet?</br>')
if export == 'yes':
for numba in range(0, self.numberOfMice):
videoName = path.splitext(path.basename(self.filename))[0]
toPrintList = [
str(videoName),
str(mouseNumList[numba]),
str(startTimeSec),
str(endTime / 1000),
str((endTime / 1000) - (startTimeSec)),
str(round(bRespRates[numba], 2)),
str(round(minstdevs[numba], 4))
]
workBook = 'output1.xls'
sheetName = 'Sheet1'
mf.xOutput(self, toPrintList, workBook, sheetName)
# If the measurement field on the mouse is out of focus or the mouse
# moves too much and all trackers are dropped, we get various errors.
# See TODO for more info
except (TypeError, ZeroDivisionError, IndexError) as excpt:
print('type is: ', excpt.__class__.__name__)
print_exc()
msg = ('<br>The selected region on ' + str(mouseNumList[numba]) +
' is not suitable for respiration measurements.</br>')
errorNotif(self, msg)
# Close the separate video windows.
cv2.destroyAllWindows()
class errorCheck:
def __init__(self, parent=Gui):
pass
def nameCheck(self):
"""Check that the video has been assigned."""
if self.filename == 0 or self.filename == u'':
msg_video = '<br>You have not selected a video!</br>'
errorNotif(self, msg_video)
return 'error'
def fileCheck(self):
"""Make sure user-selected file is actually a video."""
msg_video = '<br>Not a recognized video format.</br>'
# Test if the file was encoded in a recognizable video codec type. If
# not, it's not a (useable) video.
codec = self.capture.get(6)
if codec == 0.0:
errorNotif(self, msg_video)
return 'error'
def timeCheck(self, tsec):
"""
Check the following:
- Start time input is only integers (no letters or special characters)
- Start time is in correct format (hh:mm:ss)
- Second and minute input don't go above 59
"""
tsec = str(tsec)
time_check = tsec.split(':')
msg_time = '<br>Time must be in hh:mm:ss format</br>.'
# Only integers
for num in time_check:
if num.isdigit() is False:
print('false')
errorNotif(self, msg_time)
return 'error'
# Correct format
if len(tsec) != 8:
errorNotif(self, msg_time)
return 'error'
elif tsec[2] != ':' or tsec[5] != ':':
errorNotif(self, msg_time)
return 'error'
# Second and minute are not too high
elif int(time_check[-1]) > 59 or int(time_check[-2]) > 59:
errorNotif(self, msg_time)
return 'error'
def measLen(self):
"""Make sure the measurement length makes sense."""
if str(self.lenOfMeas).isdigit() is False:
msg_len = '<br>The given length of measurement cannot be understood.</br>'
errorNotif(self, msg_len)
return 'error'
def main():
if system() == 'Windows': # Needed to display the icon in the taskbar
if 'Vista' not in platform(): # Vista doesn't recognize this option.
myappid = 'RespiRate'
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(
myappid)
app = QtWidgets.QApplication.instance()
if not app:
app = QtWidgets.QApplication(sys.argv)
ex1 = Gui()
ex1.setWindowTitle('RespiRate')
ex1.setWindowIcon(QtGui.QIcon('RespiRate.ico'))
ex1.show()
ex1.activateWindow()
ex1._timer.stop()
sys.exit(app.exec_())
if __name__ == '__main__':
main()