|
17 | 17 | from experiments.custom.stimulation import show_visual_stim_img,laser_switch |
18 | 18 |
|
19 | 19 |
|
| 20 | +class ExampleSocialExperiment: |
| 21 | + """ |
| 22 | + Simple class to contain all of the experiment properties |
| 23 | + Uses multiprocess to ensure the best possible performance and |
| 24 | + to showcase that it is possible to work with any type of equipment, even timer-dependent |
| 25 | + """ |
| 26 | + def __init__(self): |
| 27 | + self.experiment_finished = False |
| 28 | + self._process = ExampleProtocolProcess() |
| 29 | + self._green_point = (313, 552) |
| 30 | + self._radius = 80 |
| 31 | + self._event = None |
| 32 | + self._current_trial = None |
| 33 | + self._trial_count = {trial: 0 for trial in self._trials} |
| 34 | + self._trial_timers = {trial: Timer(10) for trial in self._trials} |
| 35 | + self._exp_timer = Timer(600) |
| 36 | + |
| 37 | + def check_skeleton(self, frame, skeletons): |
| 38 | + """ |
| 39 | + Checking each passed animal skeleton for a pre-defined set of conditions |
| 40 | + Outputting the visual representation, if exist |
| 41 | + Advancing trials according to inherent logic of an experiment |
| 42 | + :param frame: frame, on which animal skeleton was found |
| 43 | + :param skeletons: skeletons, consisting of multiple joints of an animal |
| 44 | + """ |
| 45 | + self.check_exp_timer() # checking if experiment is still on |
| 46 | + for trial in self._trial_count: |
| 47 | + # checking if any trial hit a predefined cap |
| 48 | + if self._trial_count[trial] >= 10: |
| 49 | + self.stop_experiment() |
| 50 | + |
| 51 | + if not self.experiment_finished: |
| 52 | + result, response = False, None |
| 53 | + for trial in self._trials: |
| 54 | + # check for all trials if condition is met |
| 55 | + result_list = [] |
| 56 | + for skeleton in skeletons: |
| 57 | + result, response = self._trials[trial]['trigger'](skeleton=skeleton) |
| 58 | + if result: |
| 59 | + break |
| 60 | + plot_triggers_response(frame, response) |
| 61 | + if result: |
| 62 | + if self._current_trial is None: |
| 63 | + if not self._trial_timers[trial].check_timer(): |
| 64 | + self._current_trial = trial |
| 65 | + self._trial_timers[trial].reset() |
| 66 | + self._trial_count[trial] += 1 |
| 67 | + print(trial, self._trial_count[trial]) |
| 68 | + else: |
| 69 | + if self._current_trial == trial: |
| 70 | + self._current_trial = None |
| 71 | + self._trial_timers[trial].start() |
| 72 | + |
| 73 | + self._process.set_trial(self._current_trial) |
| 74 | + return result, response |
| 75 | + |
| 76 | + @property |
| 77 | + def _trials(self): |
| 78 | + """ |
| 79 | + Defining the trials |
| 80 | + """ |
| 81 | + green_roi = RegionTrigger('circle', self._green_point, self._radius * 2 + 7.5, 'bp1') |
| 82 | + trials = {'Greenbar_whiteback': dict(trigger=green_roi.check_skeleton, |
| 83 | + count=0)} |
| 84 | + return trials |
| 85 | + |
| 86 | + def check_exp_timer(self): |
| 87 | + """ |
| 88 | + Checking the experiment timer |
| 89 | + """ |
| 90 | + if not self._exp_timer.check_timer(): |
| 91 | + print("Experiment is finished") |
| 92 | + print("Time ran out.") |
| 93 | + self.stop_experiment() |
| 94 | + |
| 95 | + def start_experiment(self): |
| 96 | + """ |
| 97 | + Start the experiment |
| 98 | + """ |
| 99 | + self._process.start() |
| 100 | + if not self.experiment_finished: |
| 101 | + self._exp_timer.start() |
| 102 | + |
| 103 | + def stop_experiment(self): |
| 104 | + """ |
| 105 | + Stop the experiment and reset the timer |
| 106 | + """ |
| 107 | + self.experiment_finished = True |
| 108 | + print('Experiment completed!') |
| 109 | + self._exp_timer.reset() |
| 110 | + # don't forget to end the process! |
| 111 | + self._process.end() |
| 112 | + |
| 113 | + def get_trial(self): |
| 114 | + """ |
| 115 | + Check which trial is going on right now |
| 116 | + """ |
| 117 | + return self._current_trial |
| 118 | + |
| 119 | + |
20 | 120 | class ExampleExperiment: |
21 | 121 | """ |
22 | 122 | Simple class to contain all of the experiment properties |
|
0 commit comments