1717from utils .plotter import plot_triggers_response
1818from utils .analysis import angle_between_vectors
1919from experiments .custom .stimulation import show_visual_stim_img ,laser_switch
20- from experiments .custom .classifier import SimbaProcessPool , BsoidProcessPool
20+ from experiments .custom .classifier import SimbaProcessPool , BsoidProcessPool , PureSimbaProcessPool
2121
2222
2323from utils .configloader import THRESHOLD , POOL_SIZE
2424
2525
26+ """ experimental classification experiment using Simba trained classifiers in a pool which are converted using the pure-predict package"""
27+
28+ class PureSimbaBehaviorPoolExperiment :
29+ """
30+ Test experiment for Simba classification with pure-predict conversion
31+ Simple class to contain all of the experiment properties and includes classification
32+ Uses multiprocess to ensure the best possible performance and
33+ to showcase that it is possible to work with any type of equipment, even timer-dependant
34+ """
35+
36+ def __init__ (self ):
37+ """Classifier process and initiation of behavior trigger"""
38+ self .experiment_finished = False
39+ self ._process_experiment = ExampleProtocolProcess ()
40+ self ._process_pool = PureSimbaProcessPool (POOL_SIZE )
41+ #pass classifier to trigger, so that check_skeleton is the only function that passes skeleton
42+ #initiate in experiment, so that process can be started with start_experiment
43+ self ._behaviortrigger = SimbaThresholdBehaviorPoolTrigger (prob_threshold = THRESHOLD ,
44+ class_process_pool = self ._process_pool ,
45+ debug = False )
46+ self ._event = None
47+ #is not fully utilized in this experiment but is usefull to keep for further adaptation
48+ self ._current_trial = None
49+ self ._max_reps = 999
50+ self ._trial_count = {trial : 0 for trial in self ._trials }
51+ self ._trial_timers = {trial : Timer (0 ) for trial in self ._trials }
52+ self ._exp_timer = Timer (9999 )
53+
54+ def check_skeleton (self , frame , skeleton ):
55+ """
56+ Checking each passed animal skeleton for a pre-defined set of conditions
57+ Outputting the visual representation, if exist
58+ Advancing trials according to inherent logic of an experiment
59+ :param frame: frame, on which animal skeleton was found
60+ :param skeleton: skeleton, consisting of multiple joints of an animal
61+ """
62+ self .check_exp_timer () # checking if experiment is still on
63+ for trial in self ._trial_count :
64+ # checking if any trial hit a predefined cap
65+ if self ._trial_count [trial ] >= self ._max_reps :
66+ self .stop_experiment ()
67+
68+ if not self .experiment_finished :
69+ for trial in self ._trials :
70+ # check for all trials if condition is met
71+ #this passes the skeleton to the trigger, where the feature extraction is done and the extracted features
72+ #are passed to the classifier process
73+ result , response = self ._trials [trial ]['trigger' ](skeleton , target_prob = self ._trials [trial ]['target_prob' ])
74+ plot_triggers_response (frame , response )
75+ #if the trigger is reporting back that the behavior is found: do something
76+ #currently nothing is done, expect counting the occurances
77+ if result :
78+ if self ._current_trial is None :
79+ if not self ._trial_timers [trial ].check_timer ():
80+ self ._current_trial = trial
81+ self ._trial_timers [trial ].reset ()
82+ self ._trial_count [trial ] += 1
83+ print (trial , self ._trial_count [trial ])
84+ else :
85+ if self ._current_trial == trial :
86+ self ._current_trial = None
87+ self ._trial_timers [trial ].start ()
88+
89+ self ._process_experiment .set_trial (self ._current_trial )
90+
91+ @property
92+ def _trials (self ):
93+ """
94+ Defining the trials
95+ """
96+ trials = {'DLStream_test' : dict (trigger = self ._behaviortrigger .check_skeleton ,
97+ target_prob = None ,
98+ count = 0 )}
99+ return trials
100+
101+ def check_exp_timer (self ):
102+ """
103+ Checking the experiment timer
104+ """
105+ if not self ._exp_timer .check_timer ():
106+ print ("Experiment is finished" )
107+ print ("Time ran out." )
108+ self .stop_experiment ()
109+
110+ def start_experiment (self ):
111+ """
112+ Start the experiment
113+ """
114+ self ._process_experiment .start ()
115+ self ._process_pool .start ()
116+ if not self .experiment_finished :
117+ self ._exp_timer .start ()
118+
119+ def stop_experiment (self ):
120+ """
121+ Stop the experiment and reset the timer
122+ """
123+ self .experiment_finished = True
124+ self ._process_experiment .end ()
125+ self ._process_pool .end ()
126+ print ('Experiment completed!' )
127+ self ._exp_timer .reset ()
128+
129+ def get_trial (self ):
130+ """
131+ Check which trial is going on right now
132+ """
133+ return self ._event
134+
135+ def get_info (self ):
136+ """ returns optional info"""
137+ info = self ._behaviortrigger .get_last_prob ()
138+ return info
139+
140+
141+
26142""" experimental classification experiment using Simba trained classifiers in a pool"""
27143class SimbaBehaviorPoolExperiment :
28144 """
@@ -39,7 +155,8 @@ def __init__(self):
39155 #pass classifier to trigger, so that check_skeleton is the only function that passes skeleton
40156 #initiate in experiment, so that process can be started with start_experiment
41157 self ._behaviortrigger = SimbaThresholdBehaviorPoolTrigger (prob_threshold = THRESHOLD ,
42- class_process_pool = self ._process_pool )
158+ class_process_pool = self ._process_pool ,
159+ debug = False )
43160 self ._event = None
44161 #is not fully utilized in this experiment but is usefull to keep for further adaptation
45162 self ._current_trial = None
0 commit comments