def get_move_bouts(self, MBTT=0.2): """ designation """ MBVT, MBDT, MBTT = [getattr(self.experiment, x) for x in ['MBVT', 'MBDT', 'MBTT']] # get txy, distance, velocity, angles self.get_move_events() # coordinates within in FW bouts and homebase mask = self.index_ingestion_bout_and_homebase_coordinates() varNames = ['CT', 'CX', 'CY', 'idx_at_HB', 'velocity', 'distance'] T, X, Y, idx_hb, vel, dist = [self.load(var) for var in varNames] MB = [] idx_new = np.zeros(T.shape, dtype=bool) # # velocity threshold: slower than 1cm/s, it's slow idx = (vel > MBVT) & mask b_chunks = my_utils.find_nonzero_runs(idx) for idx_on, idx_off in b_chunks: x0, y0 = X[idx_on], Y[idx_on] x1, y1 = X[idx_off], Y[idx_off] d01 = np.sqrt((x1 - x0)**2 + (y1 - y0)**2) if d01 > MBDT: idx_new[idx_on : idx_off] = True MB.append([T[idx_on], T[idx_off]]) else: arrx = X[idx_on : idx_off] arry = Y[idx_on : idx_off] squared = lambda arrx, arry, x, y: np.sqrt((arrx - x)**2 + (arry - y)**2) d1 = squared(arrx, arry, x0, y0) d2 = squared(arrx, arry, x1, y1) if (d1 > MBDT).any() or (d2 > MBDT).any(): idx_new[idx_on : idx_off] = True MB.append([T[idx_on], T[idx_off]]) # # has to leave cell condition # cells = np.array(cell_ids[idx_on : idx_off]) # changed_cell = np.count_nonzero(cells[1:] - cells[:-1]) > 0 # connect short breaks B = Intervals(MB).connect_gaps(eps=MBTT).intervals if len(B) < len(MB): print "connected %d/%d M bout pauses<%.1fs" %( len(MB) - len(B), len(MB), MBTT) # get MB index for T lchunk = np.nonzero(np.in1d(T, B[:,0]))[0] rchunk = np.nonzero(np.in1d(T, B[:,1]))[0] idx_new = np.zeros(T.shape, dtype=bool) for l, r in zip(lchunk, rchunk): idx_new[l:r] = True assert len(B) == len(my_utils.find_nonzero_runs(idx_new)) print "%d M bouts vs. %d M events" %(#, ratio: %1.2f" %( B.shape[0], T.shape[0], # 1.*B.shape[0] / T.shape[0] ) setattr(self, 'MB_timeSet', B) setattr(self, 'MB_idx', idx_new)
def get_bout_array(self, feat): """ feature values across 24hrs """ act = feat[0] ftype = feat[1:] AS = self.load('AS_timeSet') B = self.load('%sB_timeSet' %act) AS_time = np.diff(AS).sum() / 3600. # hr num_bouts = B.shape[0] if num_bouts > 0: if ftype == 'BN': arr = np.array([num_bouts]) elif ftype == 'BASR': arr = np.array([num_bouts / AS_time]) elif ftype == 'BD': arr = np.diff(B) # sec elif ftype in ['BS', 'BI']: if act in ['F', 'W']: arr = self.get_amounts(I=B, act=act) #mg if ftype == 'BI': arr = self.get_intensities(I=B, act=act) # mg/s elif act == 'M': # sanity check idx = self.get_move_idx(move_type='bout') b_chunks = my_utils.find_nonzero_runs(idx) assert num_bouts == b_chunks.shape[0], 'WTF' arr = self.get_move_distances(move_type='bout') # cm if ftype == 'BI': arr = self.get_move_speeds(move_type='bout') # cm/s return np.array(arr)
def get_move_distances_in_AS_or_Bout(self, tbin, move_type=None): """ """ dist, _ = self.get_move_events() # cm # move timestamps for AS/Bouts(I), for specified bin idx1 = self.get_move_idx(tbin, move_type) I_chunks = my_utils.find_nonzero_runs(idx1) distances = np.zeros(len(I_chunks)) c = 0 for start, end in I_chunks: distances[c] = dist[start:end].sum() c += 1 return distances
def get_at_device_Set(self, act='F'): """computes time intervals at each device cell in a (2, 4) cage discretization. accepts act='HB' for niche (0, 0) returns array w shape (num_events, 2), start/stop time """ varNames = ['idx_at_%s' %act, 'CT'] try: idx, CT = [getattr(self, var) for var in varNames] except AttributeError: idx, CT = [self.load(var) for var in varNames] if act == 'HB': idx[-1] = False # avoid problems idx_runs = find_nonzero_runs(idx) list_of_times = [[self.CT[start], self.CT[end]] for start, end in idx_runs] arr = np.asarray(list_of_times) setattr(self, 'at_%s_timeSet' %act, arr) return arr
def plot_position_density_panel1(experiment, cycle='24H', tbin_type=None, level='mouseday', xbins=12, ybins=24, err_type='sd', plot_type='position_density', ADD_SOURCE=True): E = experiment text = cycle if tbin_type is None else tbin_type print "%s, %s, %s, xbins:%d, ybins:%d, %s" % ( plot_position_density_panel1.__name__, text, level, xbins, ybins, E.use_days) dirname_out = E.figures_dir + '%s/panel1/xbins%d_ybins%d/%s/%s_days/' % ( plot_type, xbins, ybins, text, E.use_days) if not os.path.isdir(dirname_out): os.makedirs(dirname_out) # load data data, labels = E.generate_position_density(cycle=cycle, tbin_type=tbin_type, level=level, xbins=xbins, ybins=ybins, err_type=err_type) # nest rects_HB = np.array([None] * len(data)) obs_rects = np.array([None] * len(data)) condition = (xbins, ybins) == (2, 4) if condition: rects_HB, obs_rects = E.generate_homebase_data() for strain in range(E.num_strains): idx_m = labels[:, 0] == strain mouseNumbers = np.unique(labels[idx_m, 1]) for c in xrange(len(mouseNumbers)): idx_md = labels[:, 1] == mouseNumbers[c] md_data, md_labels = data[idx_md], labels[idx_md] fname = dirname_out + '%s_%s_group%d_M%d_xbins%d_ybins%d' % ( plot_type, text, strain, mouseNumbers[c], xbins, ybins) rect_HB, obs_rect = None, None if condition: idx_start, idx_end = my_utils.find_nonzero_runs(idx_md)[0] rect_HB = rects_HB[idx_start:idx_end] # obs_rect = np.array(obs_rects[idx_start:idx_end]).reshape(len(mouseNumbers), len(E.daysToUse)) if tbin_type is not None: draw_position_density_tbins(E, md_data, md_labels, tbin_type, level, err_type, xbins, ybins, fname, rect_HB=rect_HB, obs_rect=obs_rect, plot_type=plot_type, ADD_SOURCE=ADD_SOURCE)
def plot_position_density_panel2(experiment, cycle='24H', tbin_type=None, xbins=12, ybins=24, err_type='sd', plot_type='position_density', ADD_SOURCE=True): E = experiment text = cycle if tbin_type is None else tbin_type string = E.get_days_to_use_text() print "%s, %s, xbins:%d, ybins:%d, %s" % ( plot_position_density_panel2.__name__, text, xbins, ybins, E.use_days) dirname_out = E.figures_dir + '%s/panel2/xbins%d_ybins%d/%s/%s_days/' % ( plot_type, xbins, ybins, text, E.use_days) if not os.path.isdir(dirname_out): os.makedirs(dirname_out) # load data data, labels = E.generate_position_density(cycle=cycle, tbin_type=tbin_type, level='mouseday', xbins=xbins, ybins=ybins, err_type=err_type) new_data = data.swapaxes(0, 1) # new_data = data_.reshape(data_.shape[0], data_.shape[1], -1) # tbins, mousedays, xbins, ybins, (avg, err) # tbins arr = my_utils.get_CT_bins( bin_type=tbin_type) / 3600 - 7 # CT in hours. num_tbins=arr.shape[0] tbins = ['CT%d-%d' % (row[0], row[1]) for row in arr] # nest rects_HB = np.array([None] * len(data)) obs_rects = np.array([None] * len(data)) condition = (xbins, ybins) == (2, 4) if condition: rects_HB, obs_rects = E.generate_homebase_data() for strain in range(E.num_strains): idx = labels[:, 0] == strain mouseNumbers = np.unique(labels[idx, 1]) for b in xrange(new_data.shape[0]): bin_data = new_data[b].reshape(len(mouseNumbers), len(E.daysToUse), ybins, xbins, 2) fig_title = '%s Experiment\n%s, %s\n%s days: %s\ngroup%d: %s, bin %d: %s' % ( E.short_name, plot_type.replace('_', ' ').title(), text, E.use_days.replace('_', ' ').upper(), string, strain, E.strain_names[strain], b, tbins[b]) fname = dirname_out + '%s_%s_group%d_xbins%d_ybins%d_tbin%d' % ( plot_type, text, strain, xbins, ybins, b) rect_HB, obs_rect = None, None if condition: idx_start, idx_end = my_utils.find_nonzero_runs(idx)[0] rect_HB = np.array(rects_HB[idx_start:idx_end]).reshape( len(mouseNumbers), len(E.daysToUse)) # obs_rect = np.array(obs_rects[idx_start:idx_end]).reshape(len(mouseNumbers), len(E.daysToUse)) if tbin_type is not None: draw_position_density_tbins2(E, bin_data, mouseNumbers, tbin_type, err_type, xbins, ybins, fig_title, fname, rect_HB=rect_HB, obs_rect=obs_rect, plot_type=plot_type, ADD_SOURCE=ADD_SOURCE)