|
10 | 10 | import time |
11 | 11 | from functools import partial |
12 | 12 | from collections import Counter |
13 | | -from experiments.custom.stimulus_process import ClassicProtocolProcess, SimpleProtocolProcess,Timer, ExampleProtocolProcess |
14 | | -from experiments.custom.triggers import ScreenTrigger, RegionTrigger, OutsideTrigger, DirectionTrigger, SpeedTrigger |
| 13 | +from experiments.custom.stimulus_process import ClassicProtocolProcess, SimpleProtocolProcess,Timer\ |
| 14 | + , ExampleProtocolProcess |
| 15 | +from experiments.custom.triggers import ScreenTrigger, RegionTrigger, OutsideTrigger, DirectionTrigger\ |
| 16 | + , SpeedTrigger, SocialInteractionTrigger |
15 | 17 | from utils.plotter import plot_triggers_response |
16 | 18 | from utils.analysis import angle_between_vectors |
17 | 19 | from experiments.custom.stimulation import show_visual_stim_img,laser_switch |
18 | 20 |
|
19 | 21 |
|
| 22 | +"""Social or multiple animal experiments in combination with SLEAP or non-flattened maDLC pose estimation""" |
| 23 | + |
| 24 | +class ExampleSocialInteractionExperiment: |
| 25 | + """ |
| 26 | + In this experiment the skeleton/instance of each animal will be considers for the trigger, |
| 27 | + any animal can trigger the stimulation (the first one to result in TRUE). |
| 28 | +
|
| 29 | + Simple class to contain all of the experiment properties |
| 30 | + Uses multiprocess to ensure the best possible performance and |
| 31 | + to showcase that it is possible to work with any type of equipment, even timer-dependent |
| 32 | + """ |
| 33 | + def __init__(self): |
| 34 | + self.experiment_finished = False |
| 35 | + self._process = ExampleProtocolProcess() |
| 36 | + self._proximity_threshold = 30 |
| 37 | + self._min_animals = 2 |
| 38 | + self._event = None |
| 39 | + self._current_trial = None |
| 40 | + self._max_reps = 999 |
| 41 | + self._trial_count = {trial: 0 for trial in self._trials} |
| 42 | + self._trial_timers = {trial: Timer(10) for trial in self._trials} |
| 43 | + self._exp_timer = Timer(600) |
| 44 | + |
| 45 | + def check_skeleton(self, frame, skeletons): |
| 46 | + """ |
| 47 | + Checking passed animal skeletons for a pre-defined set of conditions |
| 48 | + Outputting the visual representation, if exist |
| 49 | + Advancing trials according to inherent logic of an experiment |
| 50 | + :param frame: frame, on which animal skeleton was found |
| 51 | + :param skeletons: skeletons, consisting of multiple joints of an animal |
| 52 | + """ |
| 53 | + self.check_exp_timer() # checking if experiment is still on |
| 54 | + for trial in self._trial_count: |
| 55 | + # checking if any trial hit a predefined cap |
| 56 | + if self._trial_count[trial] >= self._max_reps: |
| 57 | + self.stop_experiment() |
| 58 | + |
| 59 | + if not self.experiment_finished: |
| 60 | + result, response = False, None |
| 61 | + #checking if enough animals were detected |
| 62 | + if len(skeletons) >= self._min_animals: |
| 63 | + for trial in self._trials: |
| 64 | + # check if social interaction trigger is true |
| 65 | + result, response = self._trials[trial]['trigger'](skeletons=skeletons) |
| 66 | + plot_triggers_response(frame, response) |
| 67 | + if result: |
| 68 | + if self._current_trial is None: |
| 69 | + if not self._trial_timers[trial].check_timer(): |
| 70 | + self._current_trial = trial |
| 71 | + self._trial_timers[trial].reset() |
| 72 | + self._trial_count[trial] += 1 |
| 73 | + print(trial, self._trial_count[trial]) |
| 74 | + else: |
| 75 | + if self._current_trial == trial: |
| 76 | + self._current_trial = None |
| 77 | + self._trial_timers[trial].start() |
| 78 | + |
| 79 | + self._process.set_trial(self._current_trial) |
| 80 | + else: |
| 81 | + pass |
| 82 | + return result, response |
| 83 | + |
| 84 | + @property |
| 85 | + def _trials(self): |
| 86 | + """ |
| 87 | + Defining the trials |
| 88 | + """ |
| 89 | + identification_dict = dict(active={'animal': 1 |
| 90 | + , 'bp': ['bp0'] |
| 91 | + } |
| 92 | + ,passive = {'animal': 0 |
| 93 | + , 'bp': ['bp2'] |
| 94 | + } |
| 95 | + ) |
| 96 | + |
| 97 | + interaction_trigger = SocialInteractionTrigger(threshold= self._proximity_threshold |
| 98 | + , identification_dict = identification_dict |
| 99 | + , interaction_type = 'proximity' |
| 100 | + , debug=True |
| 101 | + ) |
| 102 | + |
| 103 | + trials = {'DLStream_test': dict(trigger=interaction_trigger.check_skeleton, |
| 104 | + count=0)} |
| 105 | + return trials |
| 106 | + |
| 107 | + def check_exp_timer(self): |
| 108 | + """ |
| 109 | + Checking the experiment timer |
| 110 | + """ |
| 111 | + if not self._exp_timer.check_timer(): |
| 112 | + print("Experiment is finished") |
| 113 | + print("Time ran out.") |
| 114 | + self.stop_experiment() |
| 115 | + |
| 116 | + def start_experiment(self): |
| 117 | + """ |
| 118 | + Start the experiment |
| 119 | + """ |
| 120 | + self._process.start() |
| 121 | + if not self.experiment_finished: |
| 122 | + self._exp_timer.start() |
| 123 | + |
| 124 | + def stop_experiment(self): |
| 125 | + """ |
| 126 | + Stop the experiment and reset the timer |
| 127 | + """ |
| 128 | + self.experiment_finished = True |
| 129 | + print('Experiment completed!') |
| 130 | + self._exp_timer.reset() |
| 131 | + # don't forget to end the process! |
| 132 | + self._process.end() |
| 133 | + |
| 134 | + def get_trial(self): |
| 135 | + """ |
| 136 | + Check which trial is going on right now |
| 137 | + """ |
| 138 | + return self._current_trial |
| 139 | + |
| 140 | + |
| 141 | +class ExampleMultipleAnimalExperiment: |
| 142 | + """ |
| 143 | + In this experiment the skeleton/instance of each animal will be considers for the trigger, |
| 144 | + any animal can trigger the stimulation (the first one to result in TRUE). |
| 145 | +
|
| 146 | + Simple class to contain all of the experiment properties |
| 147 | + Uses multiprocess to ensure the best possible performance and |
| 148 | + to showcase that it is possible to work with any type of equipment, even timer-dependent |
| 149 | + """ |
| 150 | + |
| 151 | + def __init__(self): |
| 152 | + self.experiment_finished = False |
| 153 | + self._process = ExampleProtocolProcess() |
| 154 | + self._green_point = (550, 163) |
| 155 | + self._radius = 40 |
| 156 | + self._dist_threshold = 80 |
| 157 | + self._event = None |
| 158 | + self._current_trial = None |
| 159 | + self._max_reps = 10 |
| 160 | + self._trial_count = {trial: 0 for trial in self._trials} |
| 161 | + self._trial_timers = {trial: Timer(10) for trial in self._trials} |
| 162 | + self._exp_timer = Timer(600) |
| 163 | + |
| 164 | + def check_skeleton(self,frame,skeletons): |
| 165 | + """ |
| 166 | + Checking each passed animal skeleton for a pre-defined set of conditions |
| 167 | + Outputting the visual representation, if exist |
| 168 | + Advancing trials according to inherent logic of an experiment |
| 169 | + :param frame: frame, on which animal skeleton was found |
| 170 | + :param skeletons: skeletons, consisting of multiple joints of an animal |
| 171 | + """ |
| 172 | + self.check_exp_timer() # checking if experiment is still on |
| 173 | + for trial in self._trial_count: |
| 174 | + # checking if any trial hit a predefined cap |
| 175 | + if self._trial_count[trial] >= self._max_reps: |
| 176 | + self.stop_experiment() |
| 177 | + |
| 178 | + if not self.experiment_finished: |
| 179 | + result,response = False,None |
| 180 | + for trial in self._trials: |
| 181 | + # check for all trials if condition is met |
| 182 | + result_list = [] |
| 183 | + for skeleton in skeletons: |
| 184 | + # checking each skeleton for trigger success |
| 185 | + result,response = self._trials[trial]['trigger'](skeleton=skeleton) |
| 186 | + # if one of the triggers is true, break the loop and continue (the first True) |
| 187 | + if result: |
| 188 | + break |
| 189 | + plot_triggers_response(frame,response) |
| 190 | + if result: |
| 191 | + if self._current_trial is None: |
| 192 | + if not self._trial_timers[trial].check_timer(): |
| 193 | + self._current_trial = trial |
| 194 | + self._trial_timers[trial].reset() |
| 195 | + self._trial_count[trial] += 1 |
| 196 | + print(trial,self._trial_count[trial]) |
| 197 | + else: |
| 198 | + if self._current_trial == trial: |
| 199 | + self._current_trial = None |
| 200 | + self._trial_timers[trial].start() |
| 201 | + |
| 202 | + self._process.set_trial(self._current_trial) |
| 203 | + return result,response |
| 204 | + |
| 205 | + @property |
| 206 | + def _trials(self): |
| 207 | + """ |
| 208 | + Defining the trials |
| 209 | + """ |
| 210 | + green_roi = RegionTrigger('circle',self._green_point,self._radius * 2 + 7.5,'bp1') |
| 211 | + trials = {'Greenbar_whiteback': dict(trigger=green_roi.check_skeleton, |
| 212 | + count=0)} |
| 213 | + return trials |
| 214 | + |
| 215 | + def check_exp_timer(self): |
| 216 | + """ |
| 217 | + Checking the experiment timer |
| 218 | + """ |
| 219 | + if not self._exp_timer.check_timer(): |
| 220 | + print("Experiment is finished") |
| 221 | + print("Time ran out.") |
| 222 | + self.stop_experiment() |
| 223 | + |
| 224 | + def start_experiment(self): |
| 225 | + """ |
| 226 | + Start the experiment |
| 227 | + """ |
| 228 | + self._process.start() |
| 229 | + if not self.experiment_finished: |
| 230 | + self._exp_timer.start() |
| 231 | + |
| 232 | + def stop_experiment(self): |
| 233 | + """ |
| 234 | + Stop the experiment and reset the timer |
| 235 | + """ |
| 236 | + self.experiment_finished = True |
| 237 | + print('Experiment completed!') |
| 238 | + self._exp_timer.reset() |
| 239 | + # don't forget to end the process! |
| 240 | + self._process.end() |
| 241 | + |
| 242 | + def get_trial(self): |
| 243 | + """ |
| 244 | + Check which trial is going on right now |
| 245 | + """ |
| 246 | + return self._current_trial |
| 247 | + |
| 248 | + |
| 249 | +"""Single animal or flattened multi animal pose estimation experiments (e.g. different fur color) |
| 250 | +or by use of the FLATTEN_MA parameter in advanced settings""" |
| 251 | + |
| 252 | + |
20 | 253 | class ExampleExperiment: |
21 | 254 | """ |
22 | 255 | Simple class to contain all of the experiment properties |
|
0 commit comments