@@ -93,10 +93,10 @@ def base_trial_protocol_run(trial_q: mp.Queue, condition_q: mp.Queue, success_q:
9393 The function to use in ProtocolProcess class
9494 Designed to be run continuously alongside the main loop
9595 Three parameters are three mp.Queue classes, each passes corresponding values
96- :param trial_q: the protocol name (inwards)
96+ :param trial_q: the protocol name (inwards); dict of trial from respective experiment
9797 :param success_q: the result of each protocol (outwards)
98- :param trials: dict of possible trials
99- :param stimulus_name: exact name of stimulus function in base_stimulus .py
98+ :param condition_q: collects trigger results from trial trigger
99+ :param stimulus_name: exact name of stimulus function in base.stimulation .py
100100 """
101101 current_trial = None
102102 stimulation = setup_stimulation (stimulation_name )
@@ -105,92 +105,48 @@ def base_trial_protocol_run(trial_q: mp.Queue, condition_q: mp.Queue, success_q:
105105 if trial_q .empty () and current_trial is None :
106106 pass
107107 elif trial_q .full ():
108+ current_trial = trial_q .get ()
108109 finished_trial = False
109110 # starting timers
110- stimulus_timer = trials [ current_trial ] ['stimulus_timer' ]
111- success_timer = trials [ current_trial ] ['success_timer' ]
111+ current_trial ['stimulus_timer' ]. start ()
112+ current_trial ['success_timer' ]. start ()
112113 print ('Starting protocol {}' .format (current_trial ))
113- stimulus_timer .start ()
114- success_timer .start ()
115114 condition_list = []
116115 # this branch is for already running protocol
117116 elif current_trial is not None :
118117 # checking for stimulus timer and outputting correct image
119- if stimulus_timer .check_timer ():
118+ if current_trial [ ' stimulus_timer' ] .check_timer ():
120119 # if stimulus timer is running, show stimulus
121- stimulation .stimulate ()
120+ stimulation .start ()
122121 else :
123122 # if the timer runs out, finish protocol and reset timer
124- trials [current_trial ]['stimulus_timer' ].reset ()
123+ stimulation .stop ()
124+ current_trial ['stimulus_timer' ].reset ()
125125 current_trial = None
126126
127127 # checking if any condition was passed
128128 if condition_q .full ():
129129 stimulus_condition = condition_q .get ()
130130 # checking if timer for condition is running and condition=True
131- if success_timer .check_timer ():
132- # print('That was a success!')
131+ if current_trial ['success_timer' ].check_timer ():
133132 condition_list .append (stimulus_condition )
134- # elif success_timer.check_timer() and not stimulus_condition:
135- # # print('That was not a success')
136- # condition_list.append(False)
137133
138134 # checking if the timer for condition has run out
139- if not success_timer .check_timer () and not finished_trial :
140- if CTRL :
141- # start a random time interval
142- # TODO: working ctrl timer that does not set new time each frame...
143- ctrl_time = random .randint (0 ,INTERTRIAL_TIME + 1 )
144- ctrl_timer = Timer (ctrl_time )
145- ctrl_timer .start ()
146- print ('Waiting for extra' + str (ctrl_time ) + ' sec' )
147- if not ctrl_timer .check_timer ():
148- # in ctrl just randomly decide between the two
149- print ('Random choice between both stimuli' )
150- if random .random () >= 0.5 :
151- # very fast random choice between TRUE and FALSE
152- deliver_liqreward ()
153- print ('Delivered Reward' )
154-
155- else :
156- deliver_tone_shock ()
157- print ('Delivered Aversive' )
158-
159- ctrl_timer .reset ()
160- finished_trial = True
161- # outputting the result, whatever it is
162- success = trials [current_trial ]['result_func' ](condition_list )
163- success_q .put (success )
164- trials [current_trial ]['success_timer' ].reset ()
165-
166- else :
167- if current_trial == 'Bluebar_whiteback' :
168- deliver_tone_shock ()
169- print ('Delivered Aversive' )
170- elif current_trial == 'Greenbar_whiteback' :
171- if trials [current_trial ]['random_reward' ]:
172- if random .random () >= 0.5 :
173- # very fast random choice between TRUE and FALSE
174- deliver_liqreward ()
175- print ('Delivered Reward' )
176- else :
177- print ('No Reward' )
178- else :
179- deliver_liqreward ()
135+ if not current_trial ['success_timer' ].check_timer () and not finished_trial :
180136 # resetting the timer
181137 print ('Timer for condition run out' )
182138 finished_trial = True
183139 # outputting the result, whatever it is
184- success = trials [ current_trial ] ['result_func' ](condition_list )
140+ success = current_trial ['result_func' ](condition_list )
185141 success_q .put (success )
186- trials [ current_trial ] ['success_timer' ].reset ()
142+ current_trial ['success_timer' ].reset ()
187143
188144
189145class BaseProtocolProcess :
190146 """
191147 Class to help work with protocol function in multiprocessing
192148 """
193- def __init__ (self , trials : dict = None ):
149+ def __init__ (self ):
194150 """
195151 Setting up the three queues and the process itself
196152 """
@@ -199,12 +155,12 @@ def __init__(self, trials: dict = None):
199155 STIMULATION = 'str' )
200156 self ._settings_dict = get_process_settings (self ._name , self ._parameter_dict )
201157
202- if self ._settings_dict ['TYPE' ] == 'trial' and trials is not None :
158+ if self ._settings_dict ['TYPE' ] == 'trial' :
203159 self ._trial_queue = mp .Queue (1 )
204160 self ._success_queue = mp .Queue (1 )
205161 self ._condition_queue = mp .Queue (1 )
206162 self ._protocol_process = mp .Process (target = base_trial_protocol_run ,
207- args = (self ._trial_queue , self ._trial_queue ,
163+ args = (self ._trial_queue , self ._condition_queue ,
208164 self ._success_queue , self ._settings_dict ['STIMULATION' ]))
209165 elif self ._settings_dict ['TYPE' ] == 'switch' :
210166 self ._condition_queue = mp .Queue (1 )
@@ -247,15 +203,20 @@ def put(self, input_p):
247203 """
248204 Passing the trial name to the process
249205 """
206+
207+ if self ._condition_queue .empty ():
208+ self ._condition_queue .put (input_p )
209+
210+ def put_trial (self , trial : dict , trial_name ):
211+ """
212+ Passing the condition to the process
213+ """
250214 if self ._settings_dict ['TYPE' ] == 'trial' :
251215 if self ._trial_queue .empty () and self ._success_queue .empty ():
252- self ._trial_queue .put (input_p )
216+ self ._trial_queue .put (trial )
253217 self ._running = True
254- self ._current_trial = input_p
218+ self ._current_trial = trial_name
255219
256- elif self ._settings_dict ['TYPE' ] == 'switch' or self ._settings_dict ['TYPE' ] == 'supply' :
257- if self ._condition_queue .empty ():
258- self ._condition_queue .put (input_p )
259220
260221 def get_result (self ) -> bool :
261222 """
0 commit comments