Coverage for ibllib/io/extractors/opto_trials.py: 59%
32 statements
« prev ^ index » next coverage.py v7.5.4, created at 2024-07-08 17:16 +0100
« prev ^ index » next coverage.py v7.5.4, created at 2024-07-08 17:16 +0100
1import logging
2import numpy as np
4from ibllib.io.extractors import biased_trials
5from ibllib.io.extractors.base import BaseBpodTrialsExtractor
7_logger = logging.getLogger(__name__)
10class LaserBool(BaseBpodTrialsExtractor):
11 """
12 Extracts the laser probabilities from the bpod jsonable
13 """
14 save_names = ('_ibl_trials.laserStimulation.npy', '_ibl_trials.laserProbability.npy')
15 var_names = ('laserStimulation', 'laserProbability')
17 def _extract(self, **kwargs):
18 _logger.info('Extracting laser datasets') 1cdb
19 # reference pybpod implementation
20 lstim = np.array([float(t.get('laser_stimulation', np.NaN)) for t in self.bpod_trials]) 1cdb
21 lprob = np.array([float(t.get('laser_probability', np.NaN)) for t in self.bpod_trials]) 1cdb
23 # Karolina's choice world legacy implementation - from Slack message:
24 # it is possible that some versions I have used:
25 # 1) opto_ON_time (NaN - no laser or some number-laser)
26 # opto_ON_time=~isnan(opto_ON_time)
27 # laserON_trials=(opto_ON_time==1);
28 # laserOFF_trials=(opto_ON_time==0);
29 # 2) optoOUT (0 - no laser or 255 - laser):
30 # laserON_trials=(optoOUT ==255);
31 # laserOFF_trials=(optoOUT ==0);
32 if 'PROBABILITY_OPTO' in self.settings.keys() and np.all(np.isnan(lstim)): 1cdb
33 lprob = np.zeros_like(lprob) + self.settings['PROBABILITY_OPTO']
34 lstim = np.array([float(t.get('opto_ON_time', np.NaN)) for t in self.bpod_trials])
35 if np.all(np.isnan(lstim)):
36 lstim = np.array([float(t.get('optoOUT', np.NaN)) for t in self.bpod_trials])
37 lstim[lstim == 255] = 1
38 else:
39 lstim[~np.isnan(lstim)] = 1
40 lstim[np.isnan(lstim)] = 0
42 if np.all(np.isnan(lprob)): 1cdb
43 # this prevents the file from being saved when no data
44 self.save_names = ('_ibl_trials.laserStimulation.npy', None) 1b
45 _logger.warning('No laser probability found in bpod data') 1b
46 if np.all(np.isnan(lstim)): 1cdb
47 # this prevents the file from being saved when no data
48 self.save_names = (None, '_ibl_trials.laserProbability.npy')
49 _logger.warning('No laser stimulation found in bpod data')
50 return lstim, lprob 1cdb
53def extract_all(*args, extra_classes=None, **kwargs):
54 """
55 Extracts the biased trials for a training session
56 """
57 if extra_classes is not None:
58 extra_classes.append(LaserBool)
59 else:
60 extra_classes = [LaserBool]
61 return biased_trials.extract_all(*args, **kwargs, extra_classes=extra_classes)