Skip to content

Commit 2185fc2

Browse files
committed
- Cleaned up code in run_experiment_tab to make logic easier to follow.
- Moved variables button in run_experiment subjectboxes to ensure it is visible with standard window width. - Experiments no longer close automatically if there is an error when they are being setup to make it easier to see the problem and copy the error message.
1 parent ea68c84 commit 2185fc2

File tree

1 file changed

+65
-52
lines changed

1 file changed

+65
-52
lines changed

gui/run_experiment_tab.py

+65-52
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ def connect_to_board(self, i):
8989
return
9090
if not board.status['framework']:
9191
print_func('\nInstall pyControl framework on board before running experiment.')
92-
self.setup_failed[i] = True
92+
self.setup_failed[i] = True
93+
self.subjectboxes[i].error()
9394
board.subject = subject
9495
board.setup_ID = setup
9596
return board
@@ -104,6 +105,7 @@ def start_hardware_test(self, i):
104105
board.process_data()
105106
except PyboardError:
106107
self.setup_failed[i] = True
108+
self.subjectboxes[i].error()
107109

108110
def setup_task(self, i):
109111
'''Load the task state machine and set variables on i-th board.'''
@@ -115,6 +117,7 @@ def setup_task(self, i):
115117
board.setup_state_machine(self.experiment['task'])
116118
except PyboardError:
117119
self.setup_failed[i] = True
120+
self.subjectboxes[i].error()
118121
return
119122
# Set variables.
120123
board.subject_variables = [v for v in self.experiment['variables']
@@ -236,18 +239,34 @@ def setup_experiment(self, experiment):
236239
self.startstopclose_all_button.setEnabled(True)
237240
self.logs_button.setEnabled(True)
238241
self.plots_button.setEnabled(True)
242+
self.setups_started = 0
239243
self.setups_finished = 0
240-
self.setups_running = 0
241244

242245
def startstopclose_all(self):
243-
if self.setups_running == 0: # This logic in not very clear and may have some redundent terms.
246+
'''Called when startstopclose_all_button is clicked. Button is
247+
only active if all setups are in the same state.'''
248+
if self.startstopclose_all_button.text() == 'Close Exp.':
249+
self.close_experiment()
250+
elif self.startstopclose_all_button.text() == 'Start All':
244251
for i, board in enumerate(self.boards):
245-
self.subjectboxes[i].start_stop_rig()
246-
elif self.setups_finished < len(self.boards):
252+
self.subjectboxes[i].start_task()
253+
elif self.startstopclose_all_button.text() == 'Stop All':
247254
for i, board in enumerate(self.boards):
248-
self.subjectboxes[i].start_stop_rig()
255+
self.subjectboxes[i].stop_task()
256+
257+
def update_startstopclose_button(self):
258+
'''Called when a setup is started or stopped to update the
259+
startstopclose_all button.'''
260+
if self.setups_finished == len(self.boards):
261+
self.startstopclose_all_button.setText('Close Exp.')
262+
self.startstopclose_all_button.setIcon(QtGui.QIcon("gui/icons/close.svg"))
249263
else:
250-
self.close_experiment()
264+
self.startstopclose_all_button.setText('Stop All')
265+
self.startstopclose_all_button.setIcon(QtGui.QIcon("gui/icons/stop.svg"))
266+
if self.setups_started == len(self.boards) and self.setups_finished == 0:
267+
self.startstopclose_all_button.setEnabled(True)
268+
else:
269+
self.startstopclose_all_button.setEnabled(False)
251270

252271
def stop_experiment(self):
253272
self.update_timer.stop()
@@ -282,6 +301,7 @@ def stop_experiment(self):
282301
pv_file.write(json.dumps(persistent_variables, sort_keys=True, indent=4))
283302
if summary_variables:
284303
Summary_variables_dialog(self, sv_dict).show()
304+
self.startstopclose_all_button.setEnabled(True)
285305

286306
def abort_experiment(self):
287307
'''Called if an error occurs while the experiment is being set up.'''
@@ -293,13 +313,14 @@ def abort_experiment(self):
293313
board.stop_framework()
294314
time.sleep(0.05)
295315
board.process_data()
296-
self.subjectboxes[i].task_stopped()
316+
self.subjectboxes[i].stop_task()
297317
msg = QtGui.QMessageBox()
298318
msg.setWindowTitle('Error')
299-
msg.setText('There was an error. Closing Experiment')
319+
msg.setText('An error occured while setting up experiment')
300320
msg.setIcon(QtGui.QMessageBox.Warning)
301321
msg.exec()
302-
self.close_experiment()
322+
self.startstopclose_all_button.setText('Close Exp.')
323+
self.startstopclose_all_button.setEnabled(True)
303324

304325
def close_experiment(self):
305326
self.GUI_main.tab_widget.setTabEnabled(0, True) # Enable run task tab.
@@ -335,34 +356,10 @@ def show_hide_logs(self):
335356

336357
def update(self):
337358
'''Called regularly while experiment is running'''
338-
boards_running = False
339-
for i, board in enumerate(self.boards):
340-
if board.framework_running:
341-
boards_running = True
342-
try:
343-
board.process_data()
344-
if not board.framework_running:
345-
self.subjectboxes[i].task_stopped()
346-
self.subjectboxes[i].time_text.setText(str(datetime.now()-self.subjectboxes[i].start_time).split('.')[0])
347-
except PyboardError:
348-
self.subjectboxes[i].error()
359+
for subjectbox in self.subjectboxes:
360+
subjectbox.update()
349361
self.experiment_plot.update()
350-
if self.setups_running > 0: # This logic in not very clear and may have some redundent terms.
351-
if self.setups_running == len(self.boards) and self.setups_finished == 0:
352-
self.startstopclose_all_button.setEnabled(True)
353-
self.startstopclose_all_button.setText('Stop All')
354-
self.startstopclose_all_button.setIcon(QtGui.QIcon("gui/icons/stop.svg"))
355-
else:
356-
self.startstopclose_all_button.setEnabled(False)
357-
if self.setups_finished == 0:
358-
self.startstopclose_all_button.setText('Stop All')
359-
self.startstopclose_all_button.setIcon(QtGui.QIcon("gui/icons/stop.svg"))
360-
else:
361-
self.startstopclose_all_button.setText('Close Experiment')
362-
self.startstopclose_all_button.setIcon(QtGui.QIcon("gui/icons/close.svg"))
363-
364-
if not boards_running and self.setups_finished == len(self.boards):
365-
self.startstopclose_all_button.setEnabled(True)
362+
if self.setups_finished == len(self.boards):
366363
self.stop_experiment()
367364

368365
def print_to_logs(self, print_str):
@@ -419,6 +416,7 @@ def __init__(self, name, setup_number, parent=None):
419416
self.Vlayout = QtGui.QVBoxLayout(self)
420417
self.Hlayout = QtGui.QHBoxLayout()
421418
self.Hlayout.addWidget(self.start_stop_button)
419+
self.Hlayout.addWidget(self.variables_button)
422420
self.Hlayout.addWidget(self.status_label)
423421
self.Hlayout.addWidget(self.status_text)
424422
self.Hlayout.addWidget(self.time_label)
@@ -430,7 +428,6 @@ def __init__(self, name, setup_number, parent=None):
430428
self.Hlayout.addWidget(self.print_label)
431429
self.Hlayout.addWidget(self.print_text)
432430
self.Hlayout.setStretchFactor(self.print_text, 10)
433-
self.Hlayout.addWidget(self.variables_button)
434431
self.Vlayout.addLayout(self.Hlayout)
435432
self.Vlayout.addWidget(self.log_textbox)
436433

@@ -459,15 +456,18 @@ def assign_board(self, board):
459456
self.variables_dialog = Variables_dialog(self, board)
460457
self.variables_button.clicked.connect(self.variables_dialog.exec_)
461458
self.variables_button.setEnabled(True)
462-
self.start_stop_button.clicked.connect(self.start_stop_rig)
459+
self.start_stop_button.clicked.connect(self.start_stop_task)
463460

464-
def start_stop_rig(self):
461+
def start_stop_task(self):
462+
'''Called when start/stop button on Subjectbox pressed or
463+
startstopclose_all button is pressed.'''
465464
if self.state == 'pre_run':
466-
self.begin_rig()
465+
self.start_task()
467466
elif self.state == 'running':
468-
self.task_stopped()
467+
self.stop_task()
469468

470-
def begin_rig(self):
469+
def start_task(self):
470+
'''Start the task running on the Subjectbox's board.'''
471471
self.status_text.setText('Running')
472472
self.state = 'running'
473473
self.run_exp_tab.experiment_plot.start_experiment(self.setup_number)
@@ -484,28 +484,41 @@ def begin_rig(self):
484484

485485
self.start_stop_button.setText('Stop')
486486
self.start_stop_button.setIcon(QtGui.QIcon("gui/icons/stop.svg"))
487-
self.run_exp_tab.setups_running += 1
487+
self.run_exp_tab.setups_started += 1
488488

489489
self.run_exp_tab.GUI_main.refresh_timer.stop()
490490
self.run_exp_tab.update_timer.start(update_interval)
491+
self.run_exp_tab.update_startstopclose_button()
491492

492493
def error(self):
493494
'''Set state text to error in red.'''
494-
self.state_text.setText('Error')
495-
self.state_text.setStyleSheet('color: red;')
495+
self.status_text.setText('Error')
496+
self.status_text.setStyleSheet('color: red;')
496497

497-
def task_stopped(self):
498-
'''Called when task stops running.'''
498+
def stop_task(self):
499+
'''Called to stop task or if task stops automatically.'''
500+
if self.board.framework_running:
501+
self.board.stop_framework()
499502
self.state_text.setText('Stopped')
500503
self.state_text.setStyleSheet('color: grey;')
501504
self.status_text.setText('Stopped')
502-
self.start_stop_button.setVisible(False)
503-
# Stop running board
504-
if self.board.framework_running:
505-
self.board.stop_framework()
505+
self.start_stop_button.setEnabled(False)
506506
self.run_exp_tab.experiment_plot.active_plots.remove(self.setup_number)
507507
self.run_exp_tab.setups_finished += 1
508508
self.variables_button.setEnabled(False)
509+
self.run_exp_tab.update_startstopclose_button()
510+
511+
def update(self):
512+
'''Called regularly while experiment is running.'''
513+
if self.board.framework_running:
514+
try:
515+
self.board.process_data()
516+
if not self.board.framework_running:
517+
self.stop_task()
518+
self.time_text.setText(str(datetime.now()-self.start_time).split('.')[0])
519+
except PyboardError:
520+
self.stop_task()
521+
self.error()
509522

510523
def process_data(self, new_data):
511524
'''Update the state, event and print line info.'''

0 commit comments

Comments
 (0)