def align_ethoscan_data(exp_start, eth_start, eth_obs, times): '''Find the indices in the `times` vector at which the `eth_obs` occured. Parameters ---------- exp_start : datetime.datetime Start of the experiment; i.e. what datetime corresponds to the 0th index of `times`. eth_start : datetime.datetime Start of the Ethoscan report. Ethoscan reports appear to start behavioral classification 1 second after the report starts. Thus, this datetime is 1 second before the first classified behavior. eth_obs : np.array A single observation (row) of parsed Ethoscan data. times : np.array Times since `exp_start` for raw observations. Returns ------- np.array ''' eth_obs_start = add_seconds(eth_start, eth_obs[0]) eth_obs_end = add_seconds(eth_obs_start, eth_obs[2]) start = (eth_obs_start - exp_start).total_seconds() end = (eth_obs_end - exp_start).total_seconds() return np.searchsorted(times, [start, end], side='left')
def test_add_seconds(self): dt = datetime.datetime(2016, 10, 6, 3, 45, 12) seconds = 150 exp = datetime.datetime(2016, 10, 6, 3, 47, 42) obs = add_seconds(dt, seconds) self.assertEqual(obs, exp) seconds = 1350232 exp = datetime.datetime(2016, 10, 21, 18, 49, 4) obs = add_seconds(dt, seconds) self.assertEqual(obs, exp) dt = datetime.datetime(2016, 2, 24, 7, 45, 00) seconds = 638859 exp = datetime.datetime(2016, 3, 2, 17, 12, 39) obs = add_seconds(dt, seconds) self.assertEqual(obs, exp)
# each obs is a row of data = a behavior quantified by ethoscan for obs in eth_data: obs_start = obs[0] obs_duration = obs[2] # Data parsed from the raw promethion files by our scripts x1_data = x1[int(estart_idx + obs_start):int(estart_idx + obs_start + obs_duration)] y1_data = y1[int(estart_idx + obs_start):int(estart_idx + obs_start + obs_duration)] # Create the datetime at which the observation would be found in the raw # file to reparse the data from the Promethion output. Basically a check of # our parsing. raw_data = [] dt_start = add_seconds(eth_start, int(obs_start)) dt_start_str = '%s/%s/%s %s' % (dt_start.month, dt_start.day, dt_start.year, dt_start.strftime('%T')) dt_end = add_seconds(eth_start, int(obs_start + obs_duration)) dt_end_str = '%s/%s/%s %s' % (dt_end.month, dt_end.day, dt_end.year, dt_end.strftime('%T')) start_line = subprocess.check_output("grep -m 1 -n '%s' %s" % (dt_start_str, raw_data_fp), shell=True) end_line = subprocess.check_output("grep -m 1 -n '%s' %s" % (dt_end_str, raw_data_fp), shell=True) start_line = int(start_line.strip().split(':')[0]) end_line = int(end_line.strip().split(':')[0])
# each obs is a row of data = a behavior quantified by ethoscan for obs in eth_data: obs_start = obs[0] obs_duration = obs[2] # Data parsed from the raw promethion files by our scripts x1_data = x1[int(estart_idx + obs_start): int(estart_idx + obs_start + obs_duration)] y1_data = y1[int(estart_idx + obs_start): int(estart_idx + obs_start + obs_duration)] # Create the datetime at which the observation would be found in the raw # file to reparse the data from the Promethion output. Basically a check of # our parsing. raw_data = [] dt_start = add_seconds(eth_start, int(obs_start)) dt_start_str = '%s/%s/%s %s' % (dt_start.month, dt_start.day, dt_start.year, dt_start.strftime('%T')) dt_end = add_seconds(eth_start, int(obs_start+obs_duration)) dt_end_str = '%s/%s/%s %s' % (dt_end.month, dt_end.day, dt_end.year, dt_end.strftime('%T')) start_line = subprocess.check_output("grep -m 1 -n '%s' %s" % (dt_start_str, raw_data_fp), shell=True) end_line = subprocess.check_output("grep -m 1 -n '%s' %s" % (dt_end_str, raw_data_fp), shell=True) start_line = int(start_line.strip().split(':')[0]) end_line = int(end_line.strip().split(':')[0])