def load_pca(save_path): res = defaultdict(list) experiment_dirs = sorted( [os.path.join(save_path, d) for d in os.listdir(save_path)]) for d in experiment_dirs: temp_res = fio.load_pickle(d) reduce.chain_defaultdicts(res, temp_res) return res
def analyze_data(save_path, condition_config, m_threshold=None, excitatory=True): name = 'dict' if not excitatory: name += '_I' res = fio.load_pickle(os.path.join(save_path, name + '.pkl')) list_odor_on = res['DAQ_O_ON_F'] list_water_on = res['DAQ_W_ON_F'] res['ssig'] = [] res['msig'] = [] res['sig'] = [] for i in range(len(list_odor_on)): p_list = res['p'][i] dff_list = res['dff'][i] ssig_list = [] msig_list = [] sig_list = [] onset_list = [] amplitude_list = [] duration_list = [] odor = res['odor'][i] for p, dff in zip(p_list, dff_list): ssig = np.array(p) < condition_config.p_threshold reached_ssig = [ np.all(x) for x in _rolling_window(ssig, condition_config.p_window) ] if odor == 'water': s, e = list_water_on[i], list_water_on[i] + 10 else: s, e = list_odor_on[i], list_water_on[i] if np.any(reached_ssig[s:e]): statistical_significance = True else: statistical_significance = False if m_threshold == None: magnitude_threshold = condition_config.m_threshold else: magnitude_threshold = m_threshold if excitatory: msig = dff > magnitude_threshold else: msig = dff < magnitude_threshold reached_msig = np.array([ np.all(x) for x in _rolling_window(msig, condition_config.m_window) ]) if np.any(reached_msig[s:e]): mag_significance = True else: mag_significance = False if statistical_significance and mag_significance: reached_ssig = [ np.all(x) for x in _rolling_window(ssig, condition_config.p_window) ] onset = np.where(reached_ssig[s:e])[0][0] onset_list.append(onset) duration = np.sum(reached_msig[s:e]) duration_list.append(duration) if excitatory: amplitude = np.max(dff[s:e]) else: amplitude = np.min(dff[s:e]) amplitude_list.append(amplitude) else: onset_list.append(-1) duration_list.append(-1) amplitude_list.append(-1) ssig_list.append(statistical_significance) msig_list.append(mag_significance) sig_list.append(statistical_significance and mag_significance) res['ssig'].append(np.array(ssig_list).astype(int)) res['msig'].append(np.array(msig_list).astype(int)) res['sig'].append(np.array(sig_list).astype(int)) res['onset'].append(np.array(onset_list).astype(int)) res['duration'].append(np.array(duration_list).astype(int)) res['amplitude'].append(np.array(amplitude_list)) for key, val in res.items(): res[key] = np.array(val) return res
plot_bool=True, odor_end=odor_end) ixa = temp['odor_valence'] == 'CS+' ixb = temp['odor_valence'] == 'CS-' a = temp['corrcoef'][ixa] b = temp['corrcoef'][ixb] print(ranksums(a, b)) if condition.name == 'PIR': naive_config = statistics.analyze.PIR_NAIVE_Config() data_path_ = os.path.join(Config.LOCAL_DATA_PATH, Config.LOCAL_DATA_TIMEPOINT_FOLDER, naive_config.condition.name) save_path_ = os.path.join(Config.LOCAL_EXPERIMENT_PATH, 'COUNTING', naive_config.condition.name) res_naive = fio.load_pickle(os.path.join(save_path_, 'dict.pkl')) learned_day_per_mouse_, last_day_per_mouse_ = get_days_per_mouse( data_path_, naive_config.condition) # res = statistics.analyze.analyze_data(save_path, condition_config, m_threshold=.1) # res_naive = statistics.analyze.analyze_data(save_path_, naive_config, m_threshold= .1) # res_naive['odor_valence'] = np.array(['Naive'] * len(res_naive['day'])) excitatory = [True, False] thresholds = [0.1, -0.05] for i, sign in enumerate(excitatory): res = statistics.analyze.analyze_data(save_path, condition_config, m_threshold=thresholds[i],
import seaborn as sns conditions = [ conditions.PIR, conditions.OFC, conditions.OFC_LONGTERM, conditions.OFC_COMPOSITE, conditions.MPFC_COMPOSITE ] save_path = os.path.join(Config.LOCAL_FIGURE_PATH, 'MISC', 'SNR') out = defaultdict(list) psth = False for condition in conditions: data_path = os.path.join(Config.LOCAL_EXPERIMENT_PATH, 'COUNTING', condition.name) res = fio.load_pickle(os.path.join(data_path, 'dict.pkl')) mice = np.unique(res['mouse']) res = filter.filter_days_per_mouse(res, len(mice) * [0]) if psth: for i, d in enumerate(res['data']): res['data'][i] = np.mean(d, axis=1) for mouse in mice: temp = filter.filter(res, {'mouse': mouse}) data = np.concatenate(temp['data'], axis=1) out['condition'].append(condition.name) out['mouse'].append(mouse) out['data'].append(data)
from collections import defaultdict from tools import file_io import filter import reduce import plot data_directory = Config().LOCAL_DATA_PATH data_directory = os.path.join(data_directory, 'registration','motion_metrics') conditions = glob.glob(os.path.join(data_directory, '*/')) res = defaultdict(list) for c in conditions: mouse_pickles = glob.glob(os.path.join(c, '*.pkl')) for pickle in mouse_pickles: temp = file_io.load_pickle(pickle) reduce.chain_defaultdicts(res, temp) res['within_day_crisp_average'] = [] for v in res['within_day_crisp']: res['within_day_crisp_average'].append(np.mean(v)) for k, v in res.items(): res[k] = np.array(v) save_path = os.path.join(Config.LOCAL_FIGURE_PATH, 'MISC', 'MOTION_METRICS') mouse = 'M4_OFC' vmin, vmax = 0.6, 1
from scipy.stats import ranksums, wilcoxon, kruskal plt.style.use('default') mpl.rcParams['pdf.fonttype'] = 42 mpl.rcParams['ps.fonttype'] = 42 mpl.rcParams['font.size'] = 6 mpl.rcParams['font.family'] = 'arial' condition_config = statistics.analyze.OFC_Reversal_Config() condition = condition_config.condition data_path = os.path.join(Config.LOCAL_DATA_PATH, Config.LOCAL_DATA_TIMEPOINT_FOLDER, condition.name) save_path = os.path.join(Config.LOCAL_EXPERIMENT_PATH, 'COUNTING', condition.name) figure_path = os.path.join(Config.LOCAL_FIGURE_PATH, 'OTHER', 'COUNTING', condition.name) #analysis res = fio.load_pickle(os.path.join(save_path, 'dict.pkl')) # res.pop('data') # retrieving relevant days learned_day_per_mouse, last_day_per_mouse = get_days_per_mouse(data_path, condition) if condition_config.start_at_training and hasattr(condition, 'training_start_day'): start_days_per_mouse = condition.training_start_day else: start_days_per_mouse = [0] * len(np.unique(res['mouse'])) training_start_day_per_mouse = condition.training_start_day lick_res = behavior.behavior_analysis.get_licks_per_day(data_path, condition) analysis.add_odor_value(lick_res, condition) lick_res = filter.filter(lick_res, {'odor_valence': ['CS+', 'CS-', 'PT CS+']}) lick_res = reduce.new_filter_reduce(lick_res, ['odor_valence', 'day', 'mouse'], reduce_key='lick_boolean')