-
Notifications
You must be signed in to change notification settings - Fork 0
/
trimming_module.py
77 lines (57 loc) · 2.3 KB
/
trimming_module.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
from PyQt5 import QtCore
import time
from tqdm import tqdm
import logging
# Create the class
class trimmingClass(QtCore.QThread):
# Set up the signal data types
progressSignal = QtCore.pyqtSignal(int)
progressMinimumSignal = QtCore.pyqtSignal(int)
progressMaximumSignal = QtCore.pyqtSignal(int)
completeSignal = QtCore.pyqtSignal(int)
statusBarSignal = QtCore.pyqtSignal(str)
def __init__(self, timepoints, board, storage_directory):
# Get the arduino board, storage directory and time points
super(trimmingClass, self).__init__(None)
# Arduino board and number of cuts (timepoints)
self.board = board
self.timepoints = timepoints
# Set up the progress bar minimum and maximum
self.progressMinimumSignal.emit(0)
self.progressMaximumSignal.emit(timepoints)
# Thread active bool, checked in each iteration and exit if False
self.threadActive = True
def stop(self):
# If stop signal was sent, set the threadActive bool to false and wait for thread to finish
self.threadActive = False
self.wait()
# Called automatically with thread.start()
def run(self):
print('Trimming block-face')
for i in tqdm(range(self.timepoints)):
# Send a cutting signal, mimic pressing and releasing the footswitch
last_cutting_time = time.time()
self.board.digital[9].write(0)
time.sleep(3)
self.board.digital[9].write(1)
# Poll every second to see if cut complete signal is high
while (not self.board.digital[12].read()):
time.sleep(1)
# If you don't get cut complete signal from the microtome in 18 seconds, assume cut was complete
# Do not want to get stuck in an infinite loop
if time.time() - last_cutting_time > 25:
break
# Update status bar and progress bar
self.statusBarSignal.emit('Trimming: End of section ' + str(i + 1) + ' out of ' + str(self.timepoints))
self.progressSignal.emit(i + 1)
# Break out of the for loop if user clicks stop acquisition button
if not self.threadActive:
logging.info('Stopped trimming after %s cuts', (i+1))
break
if self.threadActive:
logging.info('Completed trimming for %s cuts', self.timepoints)
self.completeSignal.emit(2)
# Stop the thread - called after the completeSignal is sent back to main and we are done with this thread
def terminate_thread(self):
self.quit()
self.wait()