def multi_files_tracking(start_frame=0, plot_particle=False, single_particle=False, set_frame_num=None): file_list = glob.glob("*.nd2") window_centers_caches = {} for file in file_list: center_file_name = file.split(".")[0] + "_window_centers.csv" if os.path.isfile(center_file_name): window_centers_caches[file] = pd.read_csv(center_file_name, index_col=0) else: pic = pims.ND2_Reader(file)[0] window_centers_caches[file] = show_and_select( pic, file.split(".")[0]) for file in file_list: window_centers_xy = window_centers_caches[file] single_file_tracking(file, window_centers_xy, start_frame=start_frame, set_frame_num=set_frame_num, plot_particle=plot_particle, single_particle=single_particle)
def _open_nd2(filename, nd2_m, nd2_c, debug=False): """ open nd2 file using pims-nd library """ if debug: print('... read %s with nd2_reader library' % filename) meta = Meta(filename) meta['openMethod'] = 'pims-nd' with pims.ND2_Reader(filename) as tif: if 't' in tif.sizes: tif.bundle_axes = 'tyx' if 'm' in tif.sizes: if tif.sizes['m'] > 1: tif.iter_axes = 'm' print('... multistack image %i of %i selected' % (nd2_m + 1, tif.sizes['m'])) if 'c' in tif.sizes: tif.default_coords['c'] = nd2_c print('... multichannel image %i of %i selected' % (nd2_c + 1, tif.sizes['c'])) imgarray = np.array(tif[nd2_m]) meta.update_dim(tif.sizes['t'], tif.sizes['x'], tif.sizes['y']) meta.update_exp(tif[0].metadata['t_ms']) meta._mpp = tif.metadata['calibration_um'] return meta, imgarray
def nd2frames(nd_fh): if nd_fh.endswith('nd2'): frames=pims.ND2_Reader(nd_fh) elif nd_fh.endswith('mp4'): frames=pims.Video(nd_fh) from pimsviewer.utils import wrap_frames_sequence frames=wrap_frames_sequence(frames) if nd_fh.endswith('nd2'): if len(np.shape(frames))==4: frames = average_z(frames) else: frames.default_coords['c'] = 1 frames.bundle_axes='yx' frames.iter_axes = 't' return frames
def single_file_tracking(file_name, window_centers_xy=None, half_window_size=HALF_WINDOW_SIZE, start_frame=0, length_per_pixel=SIZE_PER_PIXEL, set_frame_num=None, plot_particle=True, sigma=1.0, single_particle=False): """Tracking particles in a single file""" frames = pims.ND2_Reader(file_name) frame_shape = frames[0].shape if not set_frame_num: frame_num = frames.sizes['t'] - start_frame else: frame_num = min(set_frame_num, frames.sizes['t'] - start_frame) if window_centers_xy is None: if single_particle: window_centers_xy = pd.DataFrame([], columns=["x", "y", "color"]) window_centers_xy.loc[1] = { "x": frame_shape[1] // 2, "y": frame_shape[0] // 2, "color": "g" } else: window_centers_xy = show_and_select( frames[0], file_name=file_name, half_window_size=half_window_size) for i in window_centers_xy.index: color = window_centers_xy.loc[i, "color"] roi_origin_rc = (window_centers_xy.loc[i, "y"] - half_window_size, window_centers_xy.loc[i, "x"] - half_window_size) single_window_tracking(frames, frame_num, roi_origin_rc, i, file_name, start_frame, color=color, half_window_size=half_window_size, length_per_pixel=length_per_pixel, plot_particle=plot_particle, sigma=sigma) return
def nd2msd(nd_fh): # print nd_fh frames=pims.ND2_Reader(nd_fh) logging.info('number of frames = %d' % len(np.shape(frames))) if len(np.shape(frames))==4: frames = average_z(frames) threshold=np.percentile(frames,75) f_batch = tp.batch(frames,diameter=11,threshold=threshold) t = tp.link_df(f_batch, search_range=11, memory=3) t_flt = tp.filter_stubs(t, 3*int(len(frames)/4)) try: d = tp.compute_drift(t_flt) t_cor = tp.subtract_drift(t_flt, d) except: t_cor=t_flt logging.info("drift correction excepted") # plt.figure() # tp.plot_traj(t_flt) # plt.figure() # d.plot() imsd=tp.imsd(t_cor,0.1,0.2, max_lagtime=100, statistic='msd') emsd=tp.emsd(t_cor,0.1,0.2, max_lagtime=100) return imsd,emsd