Skip to content

Commit b2fb048

Browse files
committed
reformetted code with black
added ArduinoDevice LED test to example code
1 parent 12bce03 commit b2fb048

File tree

1 file changed

+99
-68
lines changed

1 file changed

+99
-68
lines changed

experiments/custom/stimulus_process.py

Lines changed: 99 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@
99
import time
1010
import cv2
1111
import multiprocessing as mp
12-
from experiments.custom.stimulation import show_visual_stim_img, deliver_liqreward, deliver_tone_shock, withdraw_liqreward, DigitalModDevice
12+
from experiments.custom.stimulation import (
13+
show_visual_stim_img,
14+
deliver_liqreward,
15+
deliver_tone_shock,
16+
withdraw_liqreward,
17+
DigitalModDevice,
18+
)
19+
from experiments.utils.gpio_control import DigitalArduinoDevice
1320
import random
1421

1522

1623
class Timer:
1724
"""
1825
Very simple timer
1926
"""
27+
2028
def __init__(self, seconds):
2129
"""
2230
Setting the time the timer needs to run
@@ -68,26 +76,30 @@ def get_start_time(self):
6876

6977
def example_protocol_run(condition_q: mp.Queue):
7078
current_trial = None
71-
#dmod_device = DigitalModDevice('Dev1/PFI0')
79+
# dmod_device = DigitalModDevice('Dev1/PFI0')
80+
led_machine = DigitalArduinoDevice("COM5")
7281
while True:
7382
# if no protocol is selected, running default picture (background)
7483
if condition_q.full():
7584
current_trial = condition_q.get()
7685
if current_trial is not None:
77-
show_visual_stim_img(type=current_trial, name='DlStream')
78-
#dmod_device.toggle()
86+
show_visual_stim_img(type=current_trial, name="DlStream")
87+
# dmod_device.toggle()
88+
led_machine.turn_on()
7989
else:
80-
show_visual_stim_img(name='DlStream')
81-
#dmod_device.turn_off()
90+
show_visual_stim_img(name="DlStream")
91+
# dmod_device.turn_off()
92+
led_machine.turn_off()
8293

83-
if cv2.waitKey(1) & 0xFF == ord('q'):
94+
if cv2.waitKey(1) & 0xFF == ord("q"):
8495
break
8596

8697

8798
class ProtocolProcess:
8899
"""
89100
Class to help work with protocol function in multiprocessing
90101
"""
102+
91103
def __init__(self):
92104
"""
93105
Setting up the three queues and the process itself
@@ -149,13 +161,15 @@ class ExampleProtocolProcess(ProtocolProcess):
149161
"""
150162
Class to help work with protocol function in multiprocessing with simple stimulation
151163
"""
164+
152165
def __init__(self):
153166
"""
154167
Setting up the three queues and the process itself
155168
"""
156169
super().__init__()
157-
self._protocol_process = mp.Process(target=example_protocol_run, args=(self._trial_queue,))
158-
170+
self._protocol_process = mp.Process(
171+
target=example_protocol_run, args=(self._trial_queue,)
172+
)
159173

160174

161175
"""The following is the original protocols we used for our experiments! If you are interested in using this,
@@ -164,10 +178,12 @@ def __init__(self):
164178

165179

166180
def start_unconditional(protocol):
167-
print('Running some stuff, water or sound for {} protocol'.format(protocol))
181+
print("Running some stuff, water or sound for {} protocol".format(protocol))
168182

169183

170-
def classic_protocol_run_old(trial_q: mp.Queue, condition_q: mp.Queue, success_q: mp.Queue, trials: dict):
184+
def classic_protocol_run_old(
185+
trial_q: mp.Queue, condition_q: mp.Queue, success_q: mp.Queue, trials: dict
186+
):
171187
"""
172188
The function to use in ProtocolProcess class
173189
Designed to be run continuously alongside the main loop
@@ -184,15 +200,15 @@ def classic_protocol_run_old(trial_q: mp.Queue, condition_q: mp.Queue, success_q
184200
# if no protocol is selected, running default picture (background)
185201
if trial_q.empty() and current_trial is None:
186202
# print('No protocol running')
187-
show_visual_stim_img(name='inside')
203+
show_visual_stim_img(name="inside")
188204
# if some protocol is passed, set up protocol timers and variables
189205
elif trial_q.full():
190206
current_trial = trial_q.get()
191207
finished_trial = False
192208
# starting timers
193-
stimulus_timer = trials[current_trial]['stimulus_timer']
194-
success_timer = trials[current_trial]['success_timer']
195-
print('Starting protocol {}'.format(current_trial))
209+
stimulus_timer = trials[current_trial]["stimulus_timer"]
210+
success_timer = trials[current_trial]["success_timer"]
211+
print("Starting protocol {}".format(current_trial))
196212
stimulus_timer.start()
197213
success_timer.start()
198214
condition_list = []
@@ -201,10 +217,10 @@ def classic_protocol_run_old(trial_q: mp.Queue, condition_q: mp.Queue, success_q
201217
# checking for stimulus timer and outputting correct image
202218
if stimulus_timer.check_timer():
203219
# if stimulus timer is running, show stimulus
204-
show_visual_stim_img(current_trial, name='inside')
220+
show_visual_stim_img(current_trial, name="inside")
205221
else:
206222
# if the timer runs out, finish protocol and reset timer
207-
trials[current_trial]['stimulus_timer'].reset()
223+
trials[current_trial]["stimulus_timer"].reset()
208224
current_trial = None
209225

210226
# checking if any condition was passed
@@ -221,59 +237,61 @@ def classic_protocol_run_old(trial_q: mp.Queue, condition_q: mp.Queue, success_q
221237
# checking if the timer for condition has run out
222238
if not success_timer.check_timer() and not finished_trial:
223239
if CTRL:
224-
#start a random time interval
225-
#TODO: working ctrl timer that does not set new time each frame...
226-
ctrl_time = random.randint(0, INTERTRIAL_TIME+1)
240+
# start a random time interval
241+
# TODO: working ctrl timer that does not set new time each frame...
242+
ctrl_time = random.randint(0, INTERTRIAL_TIME + 1)
227243
ctrl_timer = Timer(ctrl_time)
228244
ctrl_timer.start()
229-
print('Waiting for extra' + str(ctrl_time) + ' sec')
245+
print("Waiting for extra" + str(ctrl_time) + " sec")
230246
if not ctrl_timer.check_timer():
231247
# in ctrl just randomly decide between the two
232-
print('Random choice between both stimuli')
248+
print("Random choice between both stimuli")
233249
if random.random() >= 0.5:
234250
# very fast random choice between TRUE and FALSE
235251
deliver_liqreward()
236-
print('Delivered Reward')
252+
print("Delivered Reward")
237253

238254
else:
239255
deliver_tone_shock()
240-
print('Delivered Aversive')
256+
print("Delivered Aversive")
241257

242258
ctrl_timer.reset()
243259
finished_trial = True
244260
# outputting the result, whatever it is
245-
success = trials[current_trial]['result_func'](condition_list)
261+
success = trials[current_trial]["result_func"](condition_list)
246262
success_q.put(success)
247-
trials[current_trial]['success_timer'].reset()
263+
trials[current_trial]["success_timer"].reset()
248264

249265
else:
250-
if current_trial == 'Bluebar_whiteback':
266+
if current_trial == "Bluebar_whiteback":
251267
deliver_tone_shock()
252-
print('Delivered Aversive')
253-
elif current_trial == 'Greenbar_whiteback':
254-
if trials[current_trial]['random_reward']:
268+
print("Delivered Aversive")
269+
elif current_trial == "Greenbar_whiteback":
270+
if trials[current_trial]["random_reward"]:
255271
if random.random() >= 0.5:
256-
#very fast random choice between TRUE and FALSE
272+
# very fast random choice between TRUE and FALSE
257273
deliver_liqreward()
258-
print('Delivered Reward')
274+
print("Delivered Reward")
259275
else:
260-
print('No Reward')
276+
print("No Reward")
261277
else:
262278
deliver_liqreward()
263279
# resetting the timer
264-
print('Timer for condition run out')
280+
print("Timer for condition run out")
265281
finished_trial = True
266282
# outputting the result, whatever it is
267-
success = trials[current_trial]['result_func'](condition_list)
283+
success = trials[current_trial]["result_func"](condition_list)
268284
success_q.put(success)
269-
trials[current_trial]['success_timer'].reset()
285+
trials[current_trial]["success_timer"].reset()
270286

271287
# don't delete that
272-
if cv2.waitKey(1) & 0xFF == ord('q'):
288+
if cv2.waitKey(1) & 0xFF == ord("q"):
273289
break
274290

275291

276-
def classic_protocol_run(trial_q: mp.Queue, condition_q: mp.Queue, success_q: mp.Queue, trials: dict):
292+
def classic_protocol_run(
293+
trial_q: mp.Queue, condition_q: mp.Queue, success_q: mp.Queue, trials: dict
294+
):
277295
"""
278296
The function to use in ProtocolProcess class
279297
Designed to be run continuously alongside the main loop
@@ -290,21 +308,21 @@ def classic_protocol_run(trial_q: mp.Queue, condition_q: mp.Queue, success_q: mp
290308
# if no protocol is selected, running default picture (background)
291309
if trial_q.empty() and current_trial is None:
292310
# print('No protocol running')
293-
show_visual_stim_img(name='inside')
311+
show_visual_stim_img(name="inside")
294312
# if some protocol is passed, set up protocol timers and variables
295313
elif trial_q.full():
296314
current_trial = trial_q.get()
297315
finished_trial = False
298316
delivery = False
299317
reward_del = False
300318
# starting timers
301-
stimulus_timer = trials[current_trial]['stimulus_timer']
302-
collection_timer = trials[current_trial]['collection_timer']
303-
success_timer = trials[current_trial]['success_timer']
319+
stimulus_timer = trials[current_trial]["stimulus_timer"]
320+
collection_timer = trials[current_trial]["collection_timer"]
321+
success_timer = trials[current_trial]["success_timer"]
304322
delivery_timer = Timer(3.5)
305323
shock_timer = Timer(3.5)
306324
# withdraw_timer = Timer(3.5)
307-
print('Starting protocol {}'.format(current_trial))
325+
print("Starting protocol {}".format(current_trial))
308326
stimulus_timer.start()
309327
success_timer.start()
310328
condition_list = []
@@ -314,11 +332,11 @@ def classic_protocol_run(trial_q: mp.Queue, condition_q: mp.Queue, success_q: mp
314332
# checking for stimulus timer and outputting correct image
315333
if stimulus_timer.check_timer():
316334
# if stimulus timer is running, show stimulus
317-
show_visual_stim_img(current_trial, name='inside')
335+
show_visual_stim_img(current_trial, name="inside")
318336
else:
319337
# if the timer runs out, finish protocol and reset timer
320-
trials[current_trial]['stimulus_timer'].reset()
321-
show_visual_stim_img(name='inside')
338+
trials[current_trial]["stimulus_timer"].reset()
339+
show_visual_stim_img(name="inside")
322340
# checking if any condition was passed
323341
if condition_q.full():
324342
stimulus_condition = condition_q.get()
@@ -333,23 +351,23 @@ def classic_protocol_run(trial_q: mp.Queue, condition_q: mp.Queue, success_q: mp
333351

334352
if not delivery:
335353
if current_trial is not None:
336-
print('Timer for condition ran out')
354+
print("Timer for condition ran out")
337355
print_check = True
338-
#check wether animal collected within success timer
339-
success = trials[current_trial]['result_func'](condition_list)
340-
trials[current_trial]['success_timer'].reset()
356+
# check wether animal collected within success timer
357+
success = trials[current_trial]["result_func"](condition_list)
358+
trials[current_trial]["success_timer"].reset()
341359

342-
print('Stimulation.')
360+
print("Stimulation.")
343361

344-
if current_trial == 'Bluebar_whiteback':
362+
if current_trial == "Bluebar_whiteback":
345363
deliver_tone_shock()
346-
print('Aversive')
364+
print("Aversive")
347365
shock_timer.start()
348-
elif current_trial == 'Greenbar_whiteback':
366+
elif current_trial == "Greenbar_whiteback":
349367
deliver_liqreward()
350368
delivery_timer.start()
351369
reward_del = True
352-
print('Reward')
370+
print("Reward")
353371
delivery = True
354372
collection_timer.start()
355373
elif delivery:
@@ -362,26 +380,31 @@ def classic_protocol_run(trial_q: mp.Queue, condition_q: mp.Queue, success_q: mp
362380
# if the animal didnt go to collect reward, withdraw reward again.
363381
withdraw_liqreward()
364382
# withdraw_timer.start()
365-
trials[current_trial]['collection_timer'].reset()
383+
trials[current_trial]["collection_timer"].reset()
366384
current_trial = None
367385
# put success in queue and finish trial
368386
success_q.put(success)
369387

370-
if not delivery_timer.check_timer() and delivery_timer.get_start_time() is not None:
388+
if (
389+
not delivery_timer.check_timer()
390+
and delivery_timer.get_start_time() is not None
391+
):
371392
deliver_liqreward()
372393
delivery_timer.reset()
373-
if not shock_timer.check_timer() and shock_timer.get_start_time()is not None:
394+
if (
395+
not shock_timer.check_timer()
396+
and shock_timer.get_start_time() is not None
397+
):
374398
deliver_tone_shock()
375399
shock_timer.reset()
376400

377-
378401
# if not withdraw_timer.check_timer() and withdraw_timer.get_start_time() is not None:
379402
# withdraw_liqreward(False)
380403
# withdraw_timer.reset()
381404
# delivery = False
382405

383406
# don't delete that
384-
if cv2.waitKey(1) & 0xFF == ord('q'):
407+
if cv2.waitKey(1) & 0xFF == ord("q"):
385408
break
386409

387410

@@ -404,7 +427,7 @@ def simple_protocol_run(trial_q: mp.Queue, success_q: mp.Queue, trials: dict):
404427
print(current_trial)
405428
# this branch is for already running protocol
406429
elif current_trial is not None:
407-
print('Stimulating...')
430+
print("Stimulating...")
408431
current_trial = None
409432
success_q.put(True)
410433
deliver_liqreward()
@@ -416,17 +439,23 @@ class ClassicProtocolProcess:
416439
"""
417440
Class to help work with protocol function in multiprocessing
418441
"""
442+
419443
def __init__(self, trials):
420444
"""
421445
Setting up the three queues and the process itself
422446
"""
423447
self._trial_queue = mp.Queue(1)
424448
self._success_queue = mp.Queue(1)
425449
self._condition_queue = mp.Queue(1)
426-
self._protocol_process = mp.Process(target=classic_protocol_run, args=(self._trial_queue,
427-
self._condition_queue,
428-
self._success_queue,
429-
trials))
450+
self._protocol_process = mp.Process(
451+
target=classic_protocol_run,
452+
args=(
453+
self._trial_queue,
454+
self._condition_queue,
455+
self._success_queue,
456+
trials,
457+
),
458+
)
430459
self._running = False
431460
self._current_trial = None
432461

@@ -480,11 +509,13 @@ class SimpleProtocolProcess(ClassicProtocolProcess):
480509
"""
481510
Class to help work with protocol function in multiprocessing with simple stimulation
482511
"""
512+
483513
def __init__(self, trials):
484514
"""
485515
Setting up the three queues and the process itself
486516
"""
487517
super().__init__(trials)
488-
self._protocol_process = mp.Process(target=simple_protocol_run, args=(self._trial_queue,
489-
self._success_queue,
490-
trials))
518+
self._protocol_process = mp.Process(
519+
target=simple_protocol_run,
520+
args=(self._trial_queue, self._success_queue, trials),
521+
)

0 commit comments

Comments
 (0)