|
11 | 11 | from functools import partial |
12 | 12 | from collections import Counter |
13 | 13 | from experiments.custom.stimulus_process import ClassicProtocolProcess, SimpleProtocolProcess,Timer, ExampleProtocolProcess |
14 | | -from experiments.custom.triggers import ScreenTrigger, RegionTrigger, OutsideTrigger, DirectionTrigger, SpeedTrigger, SimbaThresholdBehaviorTriggerPool |
| 14 | +from experiments.custom.triggers import ScreenTrigger, RegionTrigger, OutsideTrigger, DirectionTrigger, SpeedTrigger,\ |
| 15 | + SimbaThresholdBehaviorTriggerPool, BsoidClassBehaviorTriggerPool |
15 | 16 | from utils.plotter import plot_triggers_response |
16 | 17 | from utils.analysis import angle_between_vectors |
17 | 18 | from experiments.custom.stimulation import show_visual_stim_img,laser_switch |
18 | | -from experiments.custom.classifier import SimbaClassifier_Process, SimbaProcessPool |
| 19 | +from experiments.custom.classifier import SimbaClassifier_Process, SimbaProcessPool, BsoidProcessPool |
19 | 20 |
|
20 | 21 |
|
21 | 22 | from utils.configloader import THRESHOLD, POOL_SIZE |
@@ -125,6 +126,115 @@ def get_info(self): |
125 | 126 | info = self._behaviortrigger.get_last_prob() |
126 | 127 | return info |
127 | 128 |
|
| 129 | + |
| 130 | + |
| 131 | +""" experimental classification experiment using BSOID trained classifiers in a pool""" |
| 132 | + |
| 133 | +class BsoidBehaviorExperimentPool: |
| 134 | + """ |
| 135 | + Test experiment for Simba classification |
| 136 | + Simple class to contain all of the experiment properties and includes classification |
| 137 | + Uses multiprocess to ensure the best possible performance and |
| 138 | + to showcase that it is possible to work with any type of equipment, even timer-dependant |
| 139 | + """ |
| 140 | + |
| 141 | + def __init__(self): |
| 142 | + """Classifier process and initiation of behavior trigger""" |
| 143 | + self.experiment_finished = False |
| 144 | + self._process_pool = BsoidProcessPool(POOL_SIZE) |
| 145 | + #pass classifier to trigger, so that check_skeleton is the only function that passes skeleton |
| 146 | + #initiate in experiment, so that process can be started with start_experiment |
| 147 | + self._behaviortrigger = BsoidClassBehaviorTriggerPool(target_class= THRESHOLD, |
| 148 | + class_process_pool = self._process_pool) |
| 149 | + self._event = None |
| 150 | + #is not fully utilized in this experiment but is usefull to keep for further adaptation |
| 151 | + self._current_trial = None |
| 152 | + self._trial_count = {trial: 0 for trial in self._trials} |
| 153 | + self._trial_timers = {trial: Timer(10) for trial in self._trials} |
| 154 | + self._exp_timer = Timer(600) |
| 155 | + |
| 156 | + def check_skeleton(self, frame, skeleton): |
| 157 | + """ |
| 158 | + Checking each passed animal skeleton for a pre-defined set of conditions |
| 159 | + Outputting the visual representation, if exist |
| 160 | + Advancing trials according to inherent logic of an experiment |
| 161 | + :param frame: frame, on which animal skeleton was found |
| 162 | + :param skeleton: skeleton, consisting of multiple joints of an animal |
| 163 | + """ |
| 164 | + self.check_exp_timer() # checking if experiment is still on |
| 165 | + for trial in self._trial_count: |
| 166 | + # checking if any trial hit a predefined cap |
| 167 | + if self._trial_count[trial] >= 10: |
| 168 | + self.stop_experiment() |
| 169 | + |
| 170 | + if not self.experiment_finished: |
| 171 | + for trial in self._trials: |
| 172 | + # check for all trials if condition is met |
| 173 | + #this passes the skeleton to the trigger, where the feature extraction is done and the extracted features |
| 174 | + #are passed to the classifier process |
| 175 | + result, response = self._trials[trial]['trigger'](skeleton, target_class = self._trials[trial]['target_class']) |
| 176 | + plot_triggers_response(frame, response) |
| 177 | + #if the trigger is reporting back that the behavior is found: do something |
| 178 | + #currently nothing is done, expect counting the occurances |
| 179 | + if result: |
| 180 | + if self._current_trial is None: |
| 181 | + if not self._trial_timers[trial].check_timer(): |
| 182 | + self._current_trial = trial |
| 183 | + self._trial_timers[trial].reset() |
| 184 | + self._trial_count[trial] += 1 |
| 185 | + print(trial, self._trial_count[trial]) |
| 186 | + else: |
| 187 | + if self._current_trial == trial: |
| 188 | + self._current_trial = None |
| 189 | + self._trial_timers[trial].start() |
| 190 | + @property |
| 191 | + def _trials(self): |
| 192 | + """ |
| 193 | + Defining the trials |
| 194 | + """ |
| 195 | + trials = {'BSOID1': dict(trigger=self._behaviortrigger.check_skeleton, |
| 196 | + target_class = None, |
| 197 | + count=0)} |
| 198 | + return trials |
| 199 | + |
| 200 | + def check_exp_timer(self): |
| 201 | + """ |
| 202 | + Checking the experiment timer |
| 203 | + """ |
| 204 | + if not self._exp_timer.check_timer(): |
| 205 | + print("Experiment is finished") |
| 206 | + print("Time ran out.") |
| 207 | + self.stop_experiment() |
| 208 | + |
| 209 | + def start_experiment(self): |
| 210 | + """ |
| 211 | + Start the experiment |
| 212 | + """ |
| 213 | + self._process_pool.start() |
| 214 | + if not self.experiment_finished: |
| 215 | + self._exp_timer.start() |
| 216 | + |
| 217 | + def stop_experiment(self): |
| 218 | + """ |
| 219 | + Stop the experiment and reset the timer |
| 220 | + """ |
| 221 | + self.experiment_finished = True |
| 222 | + self._process_pool.end() |
| 223 | + print('Experiment completed!') |
| 224 | + self._exp_timer.reset() |
| 225 | + |
| 226 | + def get_trial(self): |
| 227 | + """ |
| 228 | + Check which trial is going on right now |
| 229 | + """ |
| 230 | + return self._event |
| 231 | + |
| 232 | + def get_info(self): |
| 233 | + """ returns optional info""" |
| 234 | + info = self._behaviortrigger.get_last_prob() |
| 235 | + return info |
| 236 | + |
| 237 | + |
128 | 238 | class ExampleExperiment: |
129 | 239 | """ |
130 | 240 | Simple class to contain all of the experiment properties |
|
0 commit comments