Skip to content

Commit f34587a

Browse files
committed
updated new baseexperiment
1 parent 6223174 commit f34587a

File tree

3 files changed

+46
-70
lines changed

3 files changed

+46
-70
lines changed

experiments/base/experiments.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ def __init__(self):
198198
self._parameter_dict = dict(TRIGGER = 'str',
199199
PROCESS = 'str',
200200
INTERTRIAL_TIME = 'int',
201+
TRIAL_NAME = 'str',
201202
TRIAL_TRIGGER = 'str',
202203
TRIAL_TIME = 'int',
203204
STIMULUS_TIME = 'int',
@@ -215,13 +216,12 @@ def __init__(self):
215216
self._intertrial_timer = Timer(self._settings_dict['INTERTRIAL_TIME'])
216217

217218

218-
219219
def check_skeleton(self, frame, skeleton):
220220
status, trial = self._process.get_status()
221221
if status:
222222
current_trial = self._trials[trial]
223223
condition, response = current_trial['trigger'].check_skeleton(skeleton)
224-
self._process.pass_condition(condition)
224+
self._process.put(condition)
225225
result = self._process.get_result()
226226
if result is not None:
227227
self.process_result(result, trial)
@@ -250,7 +250,7 @@ def check_skeleton(self, frame, skeleton):
250250
init_result, response_body = self._init_trigger.check_skeleton(skeleton)
251251
if init_result:
252252
# check trial start triggers
253-
self._process.set_trial(self._current_trial)
253+
self._process.put_trial(self._trials[self._current_trial], self._current_trial)
254254
self._print_check = False
255255
elif not self._print_check:
256256
print('Next trial: #' + str(len(self._result_list) + 1) + ' ' + self._current_trial)
@@ -291,7 +291,7 @@ def _trials(self):
291291
result_func = any
292292
else:
293293
raise ValueError(f'Result function can only be "all" or "any", not {self._settings_dict["RESULT_FUNC"]}.')
294-
trials = {'Trial': dict(stimulus_timer=Timer(self._settings_dict['STIMULUS_TIME']),
294+
trials = {self._settings_dict['TRIAL_NAME']: dict(stimulus_timer=Timer(self._settings_dict['STIMULUS_TIME']),
295295
success_timer=Timer(self._settings_dict['TRIAL_TIME']),
296296
trigger=trigger,
297297
result_func=result_func)}

experiments/base/stimulus_process.py

Lines changed: 27 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -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

189145
class 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
"""

experiments/configs/default_config.ini

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ EXP_LENGTH = 40
1919
EXP_TIME = 3600
2020
EXP_COMPLETION = 10
2121

22+
23+
[BaseTrialExperiment]
24+
TRIGGER = BaseRegionTrigger
25+
PROCESS = BaseProtocolProcess
26+
INTERTRIAL_TIME = 40
27+
TRIAL_NAME = Trial
28+
TRIAL_TRIGGER = BaseRegionTrigger
29+
TRIAL_TIME = 10
30+
STIMULUS_TIME = 10
31+
RESULT_FUNC = any
32+
EXP_LENGTH = 40
33+
EXP_COMPLETION = 20
34+
EXP_TIME = 6000
35+
36+
2237
[BaseOptogeneticExperiment]
2338
TRIGGER = BaseRegionTrigger
2439
INTERTRIAL_TIME = 40

0 commit comments

Comments
 (0)