def get_candidate_keys(pd, key, framerange=[2, 10]): dataset = rhfp.Dataset(pd) last_frame = dataset.trajec(key).frames[-1] candidate_keys = data_slicing.get_keys_in_framerange( pd, [last_frame - framerange[0], last_frame + framerange[1]]).tolist() candidate_keys.remove(key) return candidate_keys
def check_distance_on_candidate_keys(pd, key, candidate_keys, max_distance=10): frame = np.max(pd[pd.objid == key].frames) pd_subset = data_slicing.get_data_in_framerange(pd, [frame - 50, frame + 50]) dataset = rhfp.Dataset(pd_subset) key_position = np.array([ dataset.trajec(key).position_x[-1], dataset.trajec(key).position_y[-1] ]) errors = [] for candidate in candidate_keys: if candidate not in pd_subset.objid.values: e = np.inf # don't accept candidates that fully contain all the frames of the key #elif dataset.trajec(candidate).frames[0] < dataset.trajec(key).frames[0] and dataset.trajec(candidate).frames[-1] > dataset.trajec(key).frames[-1]: # e = np.inf else: candidates_frames = dataset.trajec(candidate).frames index = np.argmin( np.abs(candidates_frames - dataset.trajec(key).frames[-1])) candidate_position = np.array([ dataset.trajec(candidate).position_x[index], dataset.trajec(candidate).position_y[index] ]) e = np.linalg.norm(key_position - candidate_position) errors.append(e) errors = np.array(errors) indices_ok = np.where(errors < max_distance) return np.array(candidate_keys)[indices_ok].tolist()
def __init__(self, path, keys=None): self.pd, self.dvbag, self.backgroundimg = load_data_and_delta_video( path) self.dataset = read_hdf5_file_to_pandas.Dataset(self.pd) self.backgroundimg_filename = get_filename(path, '_bgimg_') self.binsx = None self.binsy = None self.filename = os.path.expanduser( os.path.join(path, 'stitches.pickle')) f = open(self.filename, 'w') self.stitches = [] pickle.dump(self.stitches, f) f.close() if keys is None: keys = np.unique(self.pd.objid.values) self.keys = keys self.key_index = -1 self.key = None self.candidate = None self.candidates = None self.candidate_index = -1 self.new_key = True ## create GUI self.app = QtGui.QApplication([]) self.w = QtGui.QWidget() self.layout = QtGui.QGridLayout() self.w.setLayout(self.layout) next_btn = QtGui.QPushButton('next match') next_btn.pressed.connect(self.next_stitch) self.layout.addWidget(next_btn, 0, 0) save_btn = QtGui.QPushButton('save stitch') save_btn.pressed.connect(self.save_stitch) self.layout.addWidget(save_btn, 1, 0) reject_btn = QtGui.QPushButton('reject stitch') reject_btn.pressed.connect(self.reject_stitch) self.layout.addWidget(reject_btn, 2, 0) self.button = 'accepted' self.p1 = pg.PlotWidget(title="Basic array plotting") # for showing starting point self.scatter = pg.ScatterPlotItem() self.p1.addItem(self.scatter) self.scatter.setZValue(0) self.layout.addWidget(self.p1, 0, 1) self.w.show()
def interpolate_keys(pd, keys): dataset = rhfp.Dataset(pd) first_frame = dataset.trajec(keys[0]).frames[0] last_frame = dataset.trajec(keys[-1]).frames[-1] new_idx = np.arange(first_frame, last_frame + 1) pd_subset = data_slicing.get_pd_subset_from_keys(pd, keys) # now remove duplicate frames pd_subset = pd_subset.drop_duplicates('frames') # now interpolate missing frames d = np.diff(pd_subset.frames.values) if np.max(d) > 1: print 'reindexing' pd_subset.reindex(new_idx).interpolate() d = np.diff(pd_subset.frames.values) print np.max(d), ' if this value == 1, everything worked'
def load_data(self): if self.load_original: self.original_pd = mta.read_hdf5_file_to_pandas.load_data_as_pandas_dataframe_from_hdf5_file(self.data_filename) print 'loading data' self.pd, self.config = mta.read_hdf5_file_to_pandas.load_and_preprocess_data(self.data_filename) self.path = self.config.path self.dataset = read_hdf5_file_to_pandas.Dataset(self.pd) filename = os.path.join(self.path, 'delete_cut_join_instructions.pickle') if os.path.exists(filename): f = open(filename, 'r+') data = pickle.load(f) f.close() else: data = [] self.instructions = data self.calc_time_etc() print 'data loaded' print 'N Trajecs: ', len(self.pd.groupby('objid'))
def load_data(self): self.pd, self.config = mta.read_hdf5_file_to_pandas.load_and_preprocess_data( self.data_filename) self.path = self.config.path self.dataset = read_hdf5_file_to_pandas.Dataset(self.pd) filename = os.path.join(self.path, 'delete_cut_join_instructions.pickle') if os.path.exists(filename): f = open(filename, 'r+') data = pickle.load(f) f.close() else: data = [] self.instructions = data # extract time and speed self.time_epoch = self.pd.time_epoch.groupby( self.pd.index).mean().values self.speed = self.pd.speed.groupby(self.pd.index).mean().values self.nflies = data_slicing.get_nkeys_per_frame(self.pd) self.time_epoch_continuous = np.linspace(np.min(self.time_epoch), np.max(self.time_epoch), len(self.nflies))
def draw_trajectories(self): for plotted_trace in self.plotted_traces: self.ui.qtplot_trajectory.removeItem(plotted_trace) self.ui.qtplot_trajectory.clear() pd_subset = mta.data_slicing.get_data_in_epoch_timerange( self.pd, self.troi) self.dataset = read_hdf5_file_to_pandas.Dataset(self.pd) self.init_bg_image() # plot a heatmap of the trajectories, for error checking h = mta.plot.get_heatmap(pd_subset, self.binsy, self.binsx, position_x='position_y', position_y='position_x', position_z='position_z', position_z_slice=None) indices = np.where(h != 0) img = copy.copy(self.backgroundimg) img[indices] = 0 self.img = pg.ImageItem(img, autoLevels=False) self.ui.qtplot_trajectory.addItem(self.img) self.img.setZValue(-200) # make sure image is behind other data # cross hair mouse stuff self.ui.qtplot_trajectory.scene().sigMouseMoved.connect( self.mouse_moved) self.ui.qtplot_trajectory.scene().sigMouseClicked.connect( self.mouse_clicked) self.crosshair_vLine = pg.InfiniteLine(angle=90, movable=False) self.crosshair_hLine = pg.InfiniteLine(angle=0, movable=False) self.ui.qtplot_trajectory.addItem(self.crosshair_vLine, ignoreBounds=True) self.ui.qtplot_trajectory.addItem(self.crosshair_hLine, ignoreBounds=True) keys = np.unique(pd_subset.objid.values) self.plotted_traces_keys = [] self.plotted_traces = [] if len(keys) < 100: for key in keys: trajec = self.dataset.trajec(key) first_time = np.max([self.troi[0], trajec.time_epoch[0]]) first_time_index = np.argmin( np.abs(trajec.time_epoch - first_time)) last_time = np.min([self.troi[-1], trajec.time_epoch[-1]]) last_time_index = np.argmin( np.abs(trajec.time_epoch - last_time)) #if trajec.length > 5: if key not in self.trajec_to_color_dict.keys(): color = get_random_color() self.trajec_to_color_dict.setdefault(key, color) else: color = self.trajec_to_color_dict[key] if key in self.trajec_width_dict.keys(): width = self.trajec_width_dict[key] else: width = 2 if self.ui.annotated_color_checkbox.checkState(): if key in self.annotated_keys: color = (0, 0, 0) width = 6 if self.ui.annotated_hide_checkbox.checkState(): if key in self.annotated_keys: color = (0, 0, 0, 0) width = 1 pen = pg.mkPen(color, width=width) plotted_trace = self.ui.qtplot_trajectory.plot( trajec.position_y[first_time_index:last_time_index], trajec.position_x[first_time_index:last_time_index], pen=pen) self.plotted_traces.append(plotted_trace) self.plotted_traces_keys.append(key) for i, key in enumerate(self.plotted_traces_keys): self.plotted_traces[i].curve.setClickable( True, width=self.clickable_width) self.plotted_traces[i].curve.key = key self.plotted_traces[i].curve.sigClicked.connect( self.trace_clicked) self.draw_data_to_add() self.draw_vlines_for_selected_trajecs()