def plot_distribution(filename, source_folder_positions, method = 'fit_ellipse'): data, info = load_time_trace(filename, source_folder_positions=source_folder_positions, verbose=False) fig, axes = plt.subplots(1, 3, figsize=(12, 4)) for i, mode in enumerate('xy'): x = data[method.lower().split('_')[-1] + ' ' + mode] print(mode + ' standard dev: ', np.std(x)) if isinstance(min(x), float): bins = 100 else: bins = range(int(min(x) - 1), int(max(x) + 1)) axes[i].hist(x, bins=bins) axes[i].set_xlabel('position ' + mode) axes[i].set_title('std dev {:0.2f} px'.format(np.std(x))) x, y = data[method.lower().split('_')[-1] + ' x'], data[method.lower().split('_')[-1] + ' y'] if isinstance(min(x), float): bins_x, bins_y = 100, 100 else: bins_x = range(int(min(x) - 1), int(max(x) + 1)) bins_y = range(int(min(y) - 1), int(max(y) + 1)) ret = axes[2].hist2d(x, y, bins=[bins_x, bins_y]) axes[2].set_xlabel('position x (px)') axes[2].set_ylabel('position y (px)') return fig
# get all the files and sort them by the run number # position_file_names = get_position_file_names(source_folder_positions, method=method, runs=list(range(60, 180))) # position_file_names = get_position_file_names(source_folder_positions, method=method, runs=list(range(83, 85))) position_file_names = get_position_file_names(source_folder_positions, method=method, tag=tag, runs=list(range(50, 54))) ################################################################################ #### run the script ################################################################################ for i, filename in enumerate(position_file_names): print(filename) data, info = load_time_trace( filename, source_folder_positions=source_folder_positions, verbose=False) return_dict = get_calibration_factor(data, particle_diameter=particle_diameter, verbose=False) print(return_dict) update_info(filename, key='calibration factor: (um/px)', value=return_dict['calibration factor: (um/px)'], folder_positions=source_folder_positions, dataset='ellipse', verbose=True)
def spectra_2D_map(position_file_names,source_folder_positions=None, modes='xy', navrg=10, frequency_range=None, tag='_Sample_6_Bead_1_', nmax=None, y_axis = '', method = 'fit_ellipse', verbose=False): """ calculated the psds and plots them as a waterfall plot Args: position_file_names: modes: navrg: y_axis: determines what to use for the y-axis, ['run', 'time (h)', ''] if '' y is equilly spaced xlim: tag: nmax: verbose: Returns: """ fig, ax = plt.subplots(len(modes), 1, sharex=True, figsize=(18, 5 * len(modes))) if len(modes)==1: ax = [ax] psd_data = {'y_axis':[]} for i, filename in enumerate(position_file_names): run = int(filename.split(tag)[1].split('-')[0].strip('_')) if verbose: print(filename, run) data, info = load_time_trace(filename, source_folder_positions=source_folder_positions, verbose=False) dt = 1./info['info']['FrameRate'] if verbose: print('FrameRate', info['info']['FrameRate']) if y_axis == '': psd_data['y_axis'].append(i) elif y_axis == 'run': psd_data['y_axis'].append(run) elif y_axis == 'time (h)': time = info['info']['File_Modified_Date_Local'] time = datetime.strptime(time.split('.')[0], '%Y-%m-%d %H:%M:%S') if i==0: start = time time = time-start psd_data['y_axis'].append(time.seconds/(60 * 60) + time.days * 24) # calculate the psd for mode in modes: if method == 'fit_ellipse': if mode == 'r': x = data['ellipse angle'] elif mode == 'z': x = data['ellipse a'] * data['ellipse b'] else: x = data['ellipse ' + mode] elif method.lower() == 'bright px': x = data['bright px ' + mode] x -= np.mean(x) if nmax is not None: x = x[0:nmax] f, p = power_spectral_density(x, time_step=dt, frequency_range=frequency_range) p = avrg(p, navrg) if mode in psd_data: psd_data[mode] = np.vstack([psd_data[mode],p]) else: psd_data[mode] = p psd_data['f'] = avrg(f, navrg) for a, mode in enumerate(modes): z = np.log10(psd_data[mode]) # get data y = psd_data['y_axis'] x = psd_data['f'] ax[a].pcolormesh(x, y, z) # plot ax[a].set_title(mode + ' data') ax[a].set_xlabel('frequency (Hz)') ax[a].set_ylabel(y_axis) return fig
def waterfall(position_file_names,source_folder_positions=None, modes='xy', navrg=10, off_set_factor=-3, frequency_range=None, tag='_Sample_6_Bead_1_', nmax=None, method = 'fit_ellipse', verbose=False): """ calculated the psds and plots them as a waterfall plot Args: position_file_names: modes: navrg: off_set_factor: xlim: tag: nmax: verbose: Returns: """ fig, ax = plt.subplots(len(modes), 1, sharex=True, figsize=(18, 5 * len(modes))) if len(modes)==1: ax = [ax] for i, filename in enumerate(position_file_names): run = int(filename.split(tag)[1].split('-')[0].strip('_')) if verbose: print(filename, run) data, info = load_time_trace(filename, source_folder_positions=source_folder_positions, verbose=False) dt = 1./info['info']['FrameRate'] # calculate the psd psd_data = {} for mode in modes: if method == 'fit_ellipse': if mode == 'r': x = data['ellipse angle'] elif mode == 'z': x = data['ellipse a'] * data['ellipse b'] else: x = data['ellipse ' + mode] elif method.lower() == 'bright px': x = data['bright px ' + mode] x -= np.mean(x) if nmax is not None: x = x[0:nmax] f, p = power_spectral_density(x, time_step=dt, frequency_range=frequency_range) psd_data[mode] = p psd_data['f'] = f f = psd_data['f'][1:] # get rid of first point (DC) f = avrg(f, navrg) # refold (assume that signal is aliasied) # f = np.mean(df_modes['FrameRate'])-avrg(f, navrg) # refold (assume that signal is aliasied) for a, mode in enumerate(modes): d = psd_data[mode] # get data d = avrg(d[1:] * 10 ** (off_set_factor * i), navrg) # shift on a log scale to get the waterfall effect ax[a].semilogy(f, d, label=str(run), alpha=0.5) # plot # if xlim is not None: # assert len(xlim) == 2 # ax[0].set_xlim(xlim) for a, mode in enumerate(modes): ax[a].set_title(mode + ' data') ax[a].set_xlabel('frequency (Hz)') plt.legend(loc=(1, 0.0)) return fig