def up_and_down(file1, file2): data1 = dataframes.DataStore(file1) data2 = dataframes.DataStore(file2) duty1, order1 = duty_order(data1) duty2, order2 = duty_order(data2) plotter = plotting.Plotter() plotter.add_plot(duty1, order1, label='up') plotter.add_plot(duty2, order2, label='down') plotter.configure_legend() plotter.show_figure()
def __init__(self, filename=None, mpp=1, fps=30.0, rad=None, max_lag=100, temp=293): ''' :param filename: DataStore filename :param mpp: microns per pixel :param fps: frames per second of movie :param rad: radius of particles in microns :param max_lag: maximum lag time in seconds - NB this is different to trackpy which measures this in frames :param temp: temperature of experiment All output quantities such as emsd and creep are in microns and seconds. Creep in SI. ''' if filename is None: filename = load_filename(file_filter='*.hdf5') self.filename = filename self.data = dataframes.DataStore(filename) self.df = self.data.df self.df_subset = self.df.copy() self.mpp = mpp self.fps = fps self.rad = rad self.max_lag = int(max_lag * fps) self.temp = temp
def extra_steps(self): with dataframes.DataStore(self.data_filename) as data: calc = statistics.PropertyCalculator(data) calc.count() calc.duty_cycle() data.set_dtypes({'x': np.float32, 'y': np.float32, 'r': np.uint8}) data.df = filter_near_edge(data.df, data.metadata['boundary'], 12)
def plot_shape_factor_histogram(filename, frame): datastore = dataframes.DataStore(filename, load=True) shape_factors = datastore.get_info(frame, ['shape factor']) n, bins = np.histogram(shape_factors, bins=100) plt.figure() plt.plot(bins[:-1], n, 'x') plt.show()
def _track_process(self, group_number): """ Method called by track. If not using multiprocess call with group number 0 Parameters ---------- group_number: int Sets the group number for multiprocessing to split the input. """ # Create the DataStore instance data_name = (str(group_number) + '.hdf5' if self.multiprocess else self.data_filename) with dataframes.DataStore(data_name, load=False) as data: data.add_metadata('number_of_frames', self.num_frames) data.add_metadata('video_filename', self.input_filename) start = self.frame_div * group_number self.cap = video.ReadVideo(self.input_filename) self.cap.set_frame(start) if group_number == 3: missing = self.num_frames - 4 * (self.num_frames // 4) frame_div = self.frame_div + missing else: frame_div = self.frame_div # Iterate over frames for f in tqdm(range(frame_div), 'Tracking'): info, boundary, info_headings = self.analyse_frame() data.add_tracking_data(start + f, info, col_names=info_headings) if f == 0: data.add_metadata('boundary', boundary)
def __init__(self, file): self.data = dataframes.DataStore(file) self.calc = statistics.PropertyCalculator(self.data) self.duty = self.calc.duty() self.duty_unique = np.sort(np.unique(self.duty)) self.setup_figure() plt.show()
def order_histogram(file, frames): data = dataframes.DataStore(file) plotter = plotting.Plotter() for f in frames: order = data.get_info(f, ['real order']) n, bins = np.histogram(order, np.linspace(0, 1, 100)) bins = bins[:-1] + (bins[1] - bins[0]) / 2 plotter.add_plot(bins, n, label=str(f)) plotter.configure_xaxis(0, 'Order parameter') plotter.configure_yaxis(0, 'Frequency') plotter.configure_legend(0, title='Frame') plotter.show_figure()
def calculate_corr_data(file=None, rmin=1, rmax=20, dr=0.02): if file is None: file = filedialogs.load_filename() new_file = file[:-5] + '_corr.hdf5' if not os.path.exists(new_file): data = dataframes.DataStore(file) calc = statistics.PropertyCalculator(data) res = calc.correlations_all_duties(rmin, rmax, dr) res = res.reset_index() res.to_hdf(new_file, 'df') else: print('file already exists')
def frame_order(file): data = dataframes.DataStore(file) print(data.num_frames) plotter = plotting.Plotter() # duty_cycle = data.frame_data.Duty # frames = data.frame_data.Duty # order = data.frame_data['mean order'] # plotter.add_plot(frames, order, fmt='x') # plotter.show_figure() fdata = data.frame_data fdata = fdata.groupby('Duty').mean() plotter.add_plot(fdata.index.values, fdata['mean order'].values) plotter.show_figure()
def histogram(file, duty): data = dataframes.DataStore(file) print(data.frame_data.head()) df = data.frame_data.merge(data.df.drop('frame', 1).reset_index()).set_index('Duty') p = plotting.Plotter() for d in duty: orders = df.loc[d, 'real order'] n, bins = np.histogram(orders, bins=100, density=True) p.add_plot(bins[:-1]+(bins[1]-bins[0])/2, n*(bins[1]-bins[0]), label=d) p.configure_legend() p.configure_xaxis(xlabel='Order Parameter') p.configure_yaxis(ylabel='Relative Frequency')
def run(files): plotter = plotting.Plotter(subplot=(3, 2)) duties = list(range(400, 1050, 50)) for i, file in enumerate(files): print(i) with dataframes.DataStore(file) as data: df1, df2 = split_df(data.df) plotter = order_histograms(df1, duties, plotter, i) plotter.configure_legend(i) plotter.configure_xaxis(i, 'Order') plotter.configure_yaxis(i, 'Density') plotter.configure_subplot_title(i, file) plotter.show_figure()
def _link_trajectories(self): """Implements the trackpy functions link_df and filter_stubs""" # Reload DataStore with dataframes.DataStore(self.data_filename) as data: # Trackpy methods data.reset_index() data.df = trackpy.link_df( data.df, self.parameters['max frame displacement'], memory=self.parameters['memory']) data.df = trackpy.filter_stubs(data.df, self.parameters['min frame life']) data.set_frame_index()
def tracking(filename): core_name = os.path.splitext(filename)[0] vid_name = core_name + '.MP4' data_name = core_name + '.hdf5' out_name = core_name + '_check.png' data = dataframes.DataStore(data_name) crop = data.metadata['crop'] vid = video.ReadVideo(vid_name) print(vid_name) frames = np.arange(4)*vid.num_frames//4 ims = [images.crop_img(vid.find_frame(f), crop) for f in frames] circles = [data.get_info(f, ['x', 'y', 'r']) for f in frames] new_ims = [images.draw_circles(im, c) for im, c in zip(ims, circles)] out = images.vstack(images.hstack(new_ims[0], new_ims[1]), images.hstack(new_ims[2], new_ims[3])) images.save(out, out_name)
def order(filename): core_name = os.path.splitext(filename)[0] vid_name = filename data_name = core_name + '.hdf5' out_name = core_name + '_check_order.png' data = dataframes.DataStore(data_name) crop = data.metadata['crop'] vid = video.ReadVideo(vid_name) frame = 100 im = images.crop_img(vid.find_frame(frame), crop) circles = data.df.loc[frame, ['x', 'y', 'r', 'order_r', 'order_i']] circles['order_mag'] = np.abs(circles.order_r + 1j * circles.order_i) circles = circles[['x', 'y', 'r', 'order_mag']].values out = images.draw_circles(im, circles) out = images.add_colorbar(out) images.display(out) images.save(out, out_name)
import warnings from Generic import filedialogs from ParticleTracking import dataframes, statistics warnings.filterwarnings('ignore') directory = filedialogs.open_directory('Open Directory containing videos') # files = os.listdir(directory+'/*.hdf5') files = filedialogs.get_files_directory(directory + '/*.hdf5') for file in files: if 'corr' not in file: print(file) meta = dataframes.load_metadata(file) headings = meta['headings'] if 'edge_distance' not in headings: with dataframes.DataStore(file) as data: # annotation.CircleAnnotator(file, data, 'particle').annotate() calculator = statistics.PropertyCalculator(data) # calculator.order() # calculator.density() # calculator.count() calculator.distance()
def sort_polygon_vertices(points): cx = np.mean(points[:, 0]) cy = np.mean(points[:, 1]) angles = np.arctan2((points[:, 1] - cy), (points[:, 0] - cx)) sort_indices = np.argsort(angles) x = points[sort_indices, 0] y = points[sort_indices, 1] return x, y if __name__ == "__main__": from ParticleTracking import dataframes import matplotlib.pyplot as plt file = "/media/data/Data/July2019/RampsN29/15790009.hdf5" data = dataframes.DataStore(file) df = data.df.loc[:50] boundary = data.metadata['boundary'] r, g, g6 = corr_multiple_frames(df, boundary, 1, 20, 0.01) plt.figure() plt.subplot(1, 2, 1) plt.plot(r, g) plt.subplot(1, 2, 2) plt.plot(r, g6 / g) plt.show() # df = data.df.loc[0] # for i in range(1000): # dists_and_orders(df, 600)
def calculate_angles(vectors): angles = np.angle(vectors[:, 0] + 1j * vectors[:, 1]) return angles def calculate_orders(angles, list_indices, filtered): # calculate summand for every angle step = np.exp(6j * angles) # set summand to zero if bond length > threshold step *= filtered list_indices -= 1 # sum the angles and count neighbours for each particle stacked = np.cumsum((step, filtered), axis=1)[:, list_indices[1:]] stacked[:, 1:] = np.diff(stacked, axis=1) neighbors = stacked[1, :] indxs = neighbors != 0 orders = np.zeros_like(neighbors) orders[indxs] = stacked[0, indxs] / neighbors[indxs] return orders, neighbors if __name__ == "__main__": from Generic import filedialogs from ParticleTracking import dataframes, statistics file = filedialogs.load_filename() data = dataframes.DataStore(file, load=True) calc = statistics.PropertyCalculator(data) calc.order() print(data.df.head()) # print(data.df.dtypes)
def run(direc, lattice_spacing=5): files = filedialogs.get_files_directory(direc + '/*.png') savename = direc + '/data.hdf5' N = len(files) # Load images ims = [images.load(f, 0) for f in tqdm(files, 'Loading images')] # Find Circles circles = [images.find_circles(im, 27, 200, 7, 16, 16) for im in tqdm(ims, 'Finding Circles')] # Save data data = dataframes.DataStore(savename, load=False) for f, info in tqdm(enumerate(circles), 'Adding Circles'): data.add_tracking_data(f, info, ['x', 'y', 'r']) # Calculate order parameter calc = statistics.PropertyCalculator(data) calc.order() # Get the course graining width cgw = get_cgw(data.df.loc[0]) / 2 # Create the lattice points x = np.arange(0, max(data.df.x), lattice_spacing) y = np.arange(0, max(data.df.y), lattice_spacing) x, y = np.meshgrid(x, y) # Calculate the coarse order fields fields = [coarse_order_field(data.df.loc[f], cgw, x, y) for f in tqdm(range(N), 'Calculating Fields')] # Calculate the field threshold field_threshold = get_field_threshold(fields, lattice_spacing, ims[0]) # Find the contours representing the boundary in each frame contours = [find_contours(f, field_threshold) for f in tqdm(fields, 'Calculating contours')] # Multiply the contours by the lattice spacing contours = [c * lattice_spacing for c in contours] # Find the angle of the image to rotate the boundary to the x-axis a, c, p1, p2 = get_angle(ims[0]) # Rotate the selection points and the contours by the angle p1 = rotate_points(np.array(p1), c, a) p2 = rotate_points(np.array(p2), c, a) contours = [rotate_points(contour.squeeze(), c, a) for contour in contours] xmin = int(p1[0]) xmax = int(p2[0]) h = int(p1[1]) # Get the heights of the fluctuations from the straight boundary hs = [get_h(contour, ims[0].shape, xmin, xmax, h) for contour in tqdm(contours, 'Calculating heights')] # Calculate the fourier transforms for all the frames L = xmax - xmin pixels_to_mms = 195/L print('One pixel is {:.2f} mm'.format(pixels_to_mms)) #convert to mm hs = [h * pixels_to_mms for h in hs] L = L * pixels_to_mms k, yplot = get_fourier(hs, L) return k, yplot
def save_crop(self): with dataframes.DataStore(self.data_filename) as data: crop = self.ip.crop data.add_metadata('crop', crop)
video.Annotator.__init__(self, in_name, out_name, frames_as_surface=True) def process_frame(self, frame, f): info = self.data.get_info(f, ['x', 'y', 'r', self.parameter]) info = info[:, :3] if self.parameter == 'particle' else info frame = images.pygame_draw_circles(frame, info) return frame def check_crop(self, filename): if self.crop: crop_name = os.path.splitext(filename)[0] + '_crop.mp4' if not os.path.exists(crop_name): crop = self.data.metadata['crop'] print(crop) video.crop_video(filename, crop[0][0], crop[1][0], crop[0][1], crop[1][1]) return crop_name else: return filename if __name__ == "__main__": from ParticleTracking import dataframes dataframe = dataframes.DataStore("/home/ppxjd3/Videos/short.hdf5", load=True) input_video = "/home/ppxjd3/Videos/short.MP4" annotator = CircleAnnotator(input_video, dataframe, 'particle', crop=True) annotator.annotate()
def extra_steps(self): duty_cycle = read_audio_file(self.input_filename, self.num_frames) data_store = dataframes.DataStore(self.data_filename, load=True) data_store.add_frame_property('Duty', duty_cycle) data_store.save()
def frame_duty(file): data = dataframes.DataStore(file, load=True) print(data.frame_data.head())