def save_all_clusters_trials(self, matching_criteria_dicts, save_path='/home/skeshav/all_trials.csv'): with temp_setattr(self, 'df_all_conditions', pd.DataFrame()): for i, c in enumerate(self.cells()): for matching_criteria_dict in matching_criteria_dicts: with temp_setattr(c.block, 'current_condition', matching_criteria_dict): self.df_all_conditions = self.df_all_conditions.append( c.block.to_df(), ignore_index=True) self.df_all_conditions.to_csv(save_path)
def plot_all_sub_stimuli_histograms(self, matching_dictionary, n_bins=30, save=False, format='png'): for c in tqdm(self.cells()): with temp_setattr(c.block, 'current_condition', matching_dictionary): fig = plt.figure(facecolor='w', figsize=(8, 3)) plt.title('Cluster {}_Channel {}'.format(c.id, c.depth)) c.block.plot_all_sub_stimuli(n_bins=n_bins, fig=fig) if save: save_string = self.matching_dictionary_to_title_string( matching_dictionary) save_dir = os.path.join(self.main_dir, 'histogram_plots', save_string) if not os.path.isdir(save_dir): os.makedirs(save_dir) save_path = os.path.join( save_dir, 'cluster_{}_channel_{}.{}'.format( c.id, c.depth, format)) fig.savefig(save_path, format=format) else: plt.show()
def plot_overlay_histogram(self, matching_dictionaries, n_bins=20, save=False, format='png'): # FIXME: for c in self.cells(): fig = plt.figure(facecolor='w', figsize=(8, 3)) for matching_dictionary in matching_dictionaries: with temp_setattr(c.block, 'current_condition', matching_dictionary): fig = c.block.plot_all_sub_stimuli(n_bins=n_bins, fig=fig) plt.suptitle('Cluster {}_ Channel {}'.format(c.id, c.depth)) histogram_axes = [ fig.axes[i] for i in range(len(fig.axes)) if i % 2 == 1 ] normalise_axes(histogram_axes, space=3) #plt.show() if save: save_string = self.get_save_string(matching_dictionaries) save_dir = os.path.join(self.main_dir, 'histogram_plots', save_string) if not os.path.isdir(save_dir): os.makedirs(save_dir) save_path = os.path.join( save_dir, 'cluster_{}_channel_{}.{}'.format(c.id, c.depth, format)) fig.savefig(save_path, format=format) else: plt.show()
def plot_psth(self, extension): for angle in self.angles: plt.figure() angle_bsl_psth_y = [] angle_spin_psth_y = [] angle_bsl2_psth_y = [] for cell in self: with temp_setattr(cell.block, 'current_condition', {'angle': angle}): bsl_psth, spin_psth, bsl2_psth = cell.block.get_psth() bsl_psth_x, bsl_psth_y = bsl_psth spin_psth_x, spin_psth_y = spin_psth bsl2_psth_x, bsl2_psth_y = bsl2_psth angle_bsl_psth_y.append(bsl_psth_y) angle_spin_psth_y.append(spin_psth_y) angle_bsl2_psth_y.append(bsl2_psth_y) angle_bsl_psth_y = np.array(angle_bsl_psth_y, dtype=np.float64).sum(0) angle_spin_psth_y = np.array(angle_spin_psth_y, dtype=np.float64).sum(0) angle_bsl2_psth_y = np.array(angle_bsl2_psth_y, dtype=np.float64).sum(0) plt.step(bsl_psth_x, angle_bsl_psth_y, where='post', color='blue') plt.step(spin_psth_x, angle_spin_psth_y, where='post', color='green') plt.step(bsl2_psth_x, angle_bsl2_psth_y, where='post', color='red') plt.savefig( os.path.join( self.main_dir, '{}_PSTH.{}'.format(self.get_str(angle), extension))) plt.close()
def plot_single_trial_detection(self, angle, trial_id): # FIXME: trial plotter with temp_setattr(self.block, 'current_condition', {'angle': angle}): trial = self.block.trials[trial_id] trace = trial.processed_trace params = self.block.detection_params[angle] delta_wave = diff(box_smooth(trace, params.n_pnts_peak), box_smooth(trace, params.n_pnts_bsl), params.n_pnts_rise_t) plt.plot(trial.stimulus.x[:len(delta_wave)], delta_wave, linewidth=0.3 ) # FIXME: find why different length # REFACTOR: extract trace = signal.medfilt(trace.copy(), params.median_kernel_size) events_params = trial.events.get_events_point_params( ) # WARNING: not analysed_events because for GUI events_pos, peaks_pos, half_rises, _ = events_params _plot_trace(trace, 'trial_{}'.format(trial_id), events_pos, peaks_pos, color='grey', x=trial.stimulus.x, y_shift=0)
def save_detection(self, processing_type): for angle in self.angles: with temp_setattr(self.block, 'current_condition', { 'keep': True, 'angle': angle }): self.block.save_detection(angle, processing_type)
def get_results_as_df(self, angle): with temp_setattr(self.block, 'current_condition', { 'keep': True, 'angle': angle }): return pd.concat( [self._get_base_df(angle), self.block.get_results_df()], axis=1)
def do_stats(self, matching_dictionary): for c in self.cells(): with temp_setattr(c.block, 'current_condition', matching_dictionary): c.set_main_dir(self.main_dir) c.block.save_stats() self.stats.append(c.block.stats_df) print(self.stats)
def population_data(self, matching_dictionary, labels, time_match_label): all_groups = {} for label in labels: spikes_in_label = [] for c in self.cells(): with temp_setattr(c.block, 'current_condition', matching_dictionary): spikes_in_label.extend( c.block.get_events_in_period(label, time_match_label)) all_groups.setdefault(label, spikes_in_label) return all_groups
def analyse(self, processed=True): """ Loop over all recordings for each angle self.noises with the high frequency noise of each recording and self.events_params with the tuple of lists: (events_pos, peaks_pos, half_rises, peak_ampls) """ for angle in self.angles: with temp_setattr(self.block, 'current_condition', { 'keep': True, 'angle': angle }): self.block.analyse(angle, processed=processed)
def get_detection_params_path(self, angle): with temp_setattr(self, 'current_condition', { 'keep': True, 'angle': angle }): expected_path = os.path.join( self.cell.dir, '{}_detection_params.csv'.format(self)) if os.path.exists(expected_path): return expected_path else: warnings.warn( 'File {} not found to reload detection parameters, creating new file' .format(expected_path)) return None
def analyse( self, angle, processed=True ): # WARNING: all trials # TODO: rename? + DOUBLE WARNING should be only one angle """ All trials but one angle :param processed: :return: """ with temp_setattr(self, 'current_condition', {'angle': angle}): self.__reset_detection() for trial in self.kept_trials: trial.detect(self.detection_params[angle], processed) # detect all whether kept or not
def get_all_cells_all_trials_spiking_tables(self, angle, levels_var_name='position'): spiking_tables = [] for i, cell in enumerate(tqdm(self)): block = cell.block with temp_setattr(block, 'current_condition', { 'keep': True, 'angle': angle }): FieldOfView.check_all_trials_have_same_n_repeats( block) # TODO: ideally check all cells have same ? spiking_table, levels, cmd = block.get_freqs_from_timetable( levels_var_name) spiking_tables.append(spiking_table) spiking_tables = np.dstack(spiking_tables) # TEST: check shape spiking_tables = np.transpose(spiking_tables, (2, 0, 1)) return levels, spiking_tables, cmd
def get_events_integral(self, angle, trial_id): """ All trials but one angle (because for detection) :param trial_id: :return: """ with temp_setattr(self, 'current_condition', {'angle': angle}): integrals = [] trial = self.trials[trial_id] trace = trial.processed_trace filtered_trace = signal.medfilt(trace.copy(), 3) for event in trial.events: integrals.append(event.get_integral(filtered_trace)) if integrals: if __debug__: print(integrals) return np.array(integrals).mean() else: return 0
def analyse_block(self, angle): with temp_setattr(self.block, 'current_condition', { 'keep': True, 'angle': angle }): self.block.save_stats()
def plot_all(self, angle): with temp_setattr(self.block, 'current_condition', { 'keep': True, 'angle': angle }): self.block.plot()