columns='subject_nickname', index=['signed_contrast']) # COMPUTE THE ACROSS-MOUSE VARIANCE FOR EACH CONTRAST # AVERAGE VARIANCE ACROSS CONTRASTS choice_var = 1 / choicedat.var(axis=1).mean() return choice_var # ================================= # # GET DATA FROM TRAINED ANIMALS # ================================= # # query sessions - BASIC TASK use_sessions, _ = query_sessions_around_criterion(criterion='trained', days_from_criterion=[0, 2], as_dataframe=False, force_cutoff=True) trial_fields = ('trial_stim_contrast_left', 'trial_stim_contrast_right', 'trial_response_time', 'trial_stim_prob_left', 'trial_feedback_type', 'trial_stim_on_time', 'trial_response_choice') # query trial data for sessions and subject name and lab info trials = use_sessions.proj('task_protocol') * behavior.TrialSet.Trial.proj(*trial_fields) subject_info = subject.Subject.proj('subject_nickname') * \ (subject.SubjectLab * reference.Lab).proj('institution_short') # Fetch, join and sort data as a pandas DataFrame behav = dj2pandas(trials.fetch(format='frame') .join(subject_info.fetch(format='frame')) .sort_values(by=['institution_short', 'subject_nickname',
# import wrappers etc from ibl_pipeline import reference, subject, behavior from dj_tools import plot_psychometric, dj2pandas # INITIALIZE A FEW THINGS seaborn_style() figpath = figpath() pal = group_colors() institution_map, col_names = institution_map() col_names = col_names[:-1] # ================================= # # GET DATA FROM TRAINED ANIMALS # ================================= # use_sessions, use_days = query_sessions_around_criterion( criterion='trained', days_from_criterion=[2, 0], as_dataframe=False) # restrict by list of dicts with uuids for these sessions b = use_sessions * subject.Subject * subject.SubjectLab * reference.Lab * \ behavior.TrialSet.Trial # reduce the size of the fetch b2 = b.proj('institution_short', 'subject_nickname', 'task_protocol', 'trial_stim_contrast_left', 'trial_stim_contrast_right', 'trial_response_choice', 'task_protocol', 'trial_stim_prob_left', 'trial_feedback_type') bdat = b2.fetch( order_by= 'institution_short, subject_nickname, session_start_time, trial_id', format='frame').reset_index() behav = dj2pandas(bdat) behav['institution_code'] = behav.institution_short.map(institution_map) assert (~behav.empty)
import numpy as np from scipy import stats from os.path import join import seaborn as sns from paper_behavior_functions import (query_sessions_around_criterion, seaborn_style, institution_map, group_colors, figpath) from dj_tools import dj2pandas, fit_psychfunc from ibl_pipeline import behavior, subject, reference import scikit_posthocs as sp # Settings fig_path = figpath() # Query sessions sessions = query_sessions_around_criterion(criterion='biased', days_from_criterion=[0, 15])[0] sessions = sessions * subject.Subject * subject.SubjectLab * reference.Lab # Create dataframe with behavioral metrics of all mice learned = pd.DataFrame(columns=[ 'mouse', 'lab', 'perf_easy', 'threshold', 'bias', 'reaction_time', 'lapse_low', 'lapse_high', 'n_trials', 'n_sessions' ]) for i, nickname in enumerate(np.unique(sessions.fetch('subject_nickname'))): if np.mod(i + 1, 10) == 0: print('Loading data of subject %d of %d' % (i + 1, len(np.unique(sessions.fetch('subject_nickname'))))) # Get only the trials of the 50/50 blocks trials = (sessions * behavior.TrialSet.Trial
kf = LeaveOneOut() y_pred = np.empty(len(labels), dtype='<U5') for train_index, test_index in kf.split(data): clf.fit(data[train_index], labels[train_index]) y_pred[test_index] = clf.predict(data[test_index]) f1 = f1_score(labels, y_pred, average='micro') cm = confusion_matrix(labels, y_pred) return f1, cm # %% Query sessions if QUERY is True: from paper_behavior_functions import query_sessions_around_criterion from ibl_pipeline import reference, subject, behavior use_sessions, _ = query_sessions_around_criterion(criterion='biased', days_from_criterion=[1, 3]) use_sessions = use_sessions & 'task_protocol LIKE "%biased%"' # only get biased sessions b = (use_sessions * subject.Subject * subject.SubjectLab * reference.Lab * behavior.TrialSet.Trial) b2 = b.proj('institution_short', 'subject_nickname', 'task_protocol', 'trial_stim_contrast_left', 'trial_stim_contrast_right', 'trial_response_choice', 'task_protocol', 'trial_stim_prob_left', 'trial_feedback_type', 'trial_response_time', 'trial_stim_on_time', 'time_zone') bdat = b2.fetch(order_by='institution_short, subject_nickname, session_start_time, trial_id', format='frame').reset_index() behav = dj2pandas(bdat) behav['institution_code'] = behav.institution_short.map(institution_map()[0]) else: behav = load_csv('Fig4.csv') biased_fits = pd.DataFrame()
# progress bar tqdm.pandas(desc="model fitting") # whether to query data from DataJoint (True), or load from disk (False) query = True institution_map, col_names = institution_map() # ========================================== # #%% 1. LOAD DATA # ========================================== # # Query sessions: before and after full task was first introduced if query is True: use_sessions, _ = query_sessions_around_criterion( criterion='biased', days_from_criterion=[2, 3], as_dataframe=False, force_cutoff=True) trial_fields = ('trial_stim_contrast_left', 'trial_stim_contrast_right', 'trial_response_time', 'trial_stim_prob_left', 'trial_feedback_type', 'trial_stim_on_time', 'trial_response_choice') # query trial data for sessions and subject name and lab info trials = use_sessions.proj('task_protocol') * behavior.TrialSet.Trial.proj( *trial_fields) subject_info = subject.Subject.proj('subject_nickname') * \ (subject.SubjectLab * reference.Lab).proj('institution_short') # Fetch, join and sort data as a pandas DataFrame
y_pred = np.array([]) y_true = np.array([]) for train_index, test_index in kf.split(resp): train_resp = resp[train_index] test_resp = resp[test_index] clf.fit(train_resp, [labels[j] for j in train_index]) y_pred = np.append(y_pred, clf.predict(test_resp)) y_true = np.append(y_true, [labels[j] for j in test_index]) f1 = f1_score(y_true, y_pred, labels=np.unique(labels), average='micro') unique_labels, label_counts = np.unique(labels, return_counts=True) cm = confusion_matrix(y_true, y_pred, labels=unique_labels) return f1, cm # %% query sessions use_sessions = query_sessions_around_criterion(criterion='ephys', days_from_criterion=[2, 0])[0] b = (use_sessions * subject.Subject * subject.SubjectLab * reference.Lab * behavior.TrialSet.Trial & 'task_protocol LIKE "%biased%"') # load data into pandas dataframe b2 = b.proj('institution_short', 'subject_nickname', 'task_protocol', 'trial_stim_contrast_left', 'trial_stim_contrast_right', 'trial_response_choice', 'task_protocol', 'trial_stim_prob_left', 'trial_feedback_type', 'trial_response_time', 'trial_stim_on_time', 'time_zone') bdat = b2.fetch( order_by= 'institution_short, subject_nickname, session_start_time, trial_id', format='frame').reset_index() behav = dj2pandas(bdat)
import numpy as np from scipy import stats from os.path import join import seaborn as sns from paper_behavior_functions import (query_sessions_around_criterion, seaborn_style, institution_map, group_colors, figpath) from dj_tools import dj2pandas, fit_psychfunc from ibl_pipeline import behavior, subject, reference import scikit_posthocs as sp # Settings fig_path = figpath() # Query sessions sessions = query_sessions_around_criterion(criterion='trained', days_from_criterion=[2, 0])[0] sessions = sessions * subject.Subject * subject.SubjectLab * reference.Lab # Create dataframe with behavioral metrics of all mice learned = pd.DataFrame(columns=[ 'mouse', 'lab', 'perf_easy', 'n_trials', 'threshold', 'bias', 'reaction_time', 'lapse_low', 'lapse_high' ]) for i, nickname in enumerate(np.unique(sessions.fetch('subject_nickname'))): if np.mod(i + 1, 10) == 0: print('Loading data of subject %d of %d' % (i + 1, len(np.unique(sessions.fetch('subject_nickname'))))) # Get the trials of the sessions around criterion trials = (sessions * behavior.TrialSet.Trial
pal = group_colors() institution_map, col_names = institution_map() col_names = col_names[:-1] # colors for biased blocks cmap = sns.color_palette([[0.8984375, 0.37890625, 0.00390625], [0.3, 0.3, 0.3], [0.3671875, 0.234375, 0.59765625]]) sns.set_palette(cmap) # ================================= # # GET DATA FROM TRAINED ANIMALS # ================================= # if QUERY is True: # query sessions use_sessions, _ = query_sessions_around_criterion( criterion='ephys', days_from_criterion=[2, 0], force_cutoff=True) use_sessions = use_sessions & 'task_protocol LIKE "%biased%"' # only get biased sessions # restrict by list of dicts with uuids for these sessions b = (use_sessions * subject.Subject * subject.SubjectLab * reference.Lab * behavior.TrialSet.Trial) # reduce the size of the fetch b2 = b.proj('institution_short', 'subject_nickname', 'task_protocol', 'session_uuid', 'trial_stim_contrast_left', 'trial_stim_contrast_right', 'trial_response_choice', 'task_protocol', 'trial_stim_prob_left', 'trial_feedback_type', 'trial_response_time', 'trial_stim_on_time') # construct pandas dataframe bdat = b2.fetch(
'subject_nickname')['institution_short'].apply(list).str[0] # Change lab name into lab number training_time['lab_number'] = training_time.lab.map(institution_map) training_time = training_time.sort_values('lab_number') # Save to csv training_time.to_csv(join('data', 'Fig2d.csv')) # %%=============================== # # FIGURE 3 # ================================= # print('Starting figure 3..') # query sessions use_sessions, _ = query_sessions_around_criterion(criterion='trained', days_from_criterion=[2, 0]) use_sessions = use_sessions & 'task_protocol LIKE "%training%"' # only get training sessions # query all trials for these sessions, it's split in two because otherwise the query would become # too big to handle in one go b = (use_sessions * subject.Subject * subject.SubjectLab * reference.Lab * behavior.TrialSet.Trial) # reduce the size of the fetch b2 = b.proj('institution_short', 'subject_nickname', 'task_protocol', 'session_uuid', 'trial_stim_contrast_left', 'trial_stim_contrast_right', 'trial_response_choice', 'task_protocol', 'trial_stim_prob_left', 'trial_feedback_type', 'trial_response_time', 'trial_stim_on_time') # construct pandas dataframe