diff --git a/.DS_Store b/.DS_Store index 5d65514..73e6d3e 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Behavior/Generate_R37_pilot_sessions_plots/amos_behavior.R b/Behavior/Generate_R37_pilot_sessions_plots/amos_behavior.R deleted file mode 100644 index 4389acb..0000000 --- a/Behavior/Generate_R37_pilot_sessions_plots/amos_behavior.R +++ /dev/null @@ -1,113 +0,0 @@ -#Behavioral timeline during pilot recordings of Amos - -#Load libraries: -library(timeline) -library(ggplot2) - -#Load data: -setwd('C:/Users/Camille Testard/Desktop/Grants_Fellowships/R37_Project/pilot_data/session1') -behavioral_log = read.csv('Behavioral_log.csv') -names(behavioral_log)[1]='eventID' -behavioral_log$Start.Stop=as.character(behavioral_log$Start.Stop) - -# #For now exclude ambiguous start/stop -# log=behavioral_log[-which(str_length(as.character(behavioral_log$Start.Stop))>5),] -log=behavioral_log - -#Reorganize data for plotting -behavs = as.character(unique(log$Behavior)) -awakeness = behavs[c(2,3,5,11)] #sleep/wake states -behavs = behavs[-c(1,2,3,5,9,11,15,16)] #behaviors - -#For all behaviors outside of sleep/wake cycle: -new_log=data.frame(); b=1 -for (b in 1:length(behavs)){ - - data=log[log$Behavior==behavs[b],c("Time", "Behavior", "Start.Stop")] - if (length(which(is.na(data$Behavior)))!=0){data=data[-which(is.na(data$Behavior)),]} - - interim=split(data,data$Start.Stop) - names(interim[[1]])[1]="start.time";names(interim[[2]])[1]="stop.time" - - data = cbind(interim[[1]],interim[[2]]); data=data[,-c(3,5,6)] - data=data[,c("Behavior","start.time","stop.time")] - data[,c("start.time","stop.time")]=data[,c("start.time","stop.time")]/1000 - - new_log=rbind(new_log,data) -} - -#Add sleeping: -times = c(log$Time[which(log$Behavior=="wakingup")]/1000, - log$Time[which(log$Behavior=="awake")]/1000) -new_log[nrow(new_log)+1,"Behavior"]="Sleeping" -new_log[nrow(new_log),"start.time"]=1; new_log[nrow(new_log),"stop.time"]=times[1]; -new_log$duration.s = new_log$stop.time-new_log$start.time - -new_log$group.min=0; new_log$group.max=1 -new_log$Behavior=as.character(new_log$Behavior) - -#Add resting: -new_log = new_log[order(new_log$start.time),] -start.times=new_log$start.time[2:length(new_log$start.time)] -stop.times=new_log$stop.time[1:length(new_log$stop.time)-1] -intervals = as.numeric(start.times-stop.times) -free_intervals = which(intervals>0) - -resting=data.frame() -for (i in 1:length(free_intervals)){ - resting[i,'start.time']=stop.times[free_intervals[i]] - resting[i,'stop.time']=start.times[free_intervals[i]] -} -resting$duration.s = resting$stop.time-resting$start.time -resting$Behavior="Resting"; resting$group.min=0; resting$group.max=1; resting=resting[,names(new_log)] - -new_log=rbind(new_log,resting) -new_log$Behavior=factor(new_log$Behavior, - levels=c("Aggression","Proximity","Grooming", - "Feeding", "Submission", - "Resting","Walking","Drinking","Sleeping","Self-directed behavior")) -#Add wakefulness state -new_log = new_log[order(new_log$start.time),] -new_log$wake.state = 'awake' -new_log$wake.state[1] = 'sleeping' -new_log$wake.state[2:11] = 'wakingup' - -#Save to .csv -write.csv(new_log[,-c(5,6)],file='behav_log_restructured.csv',row.names = F) - -#Plot -behavior.log<-ggplot(new_log, aes(xmin=start.time, xmax= stop.time, ymin=group.min, ymax=group.max))+ - geom_rect(aes(fill=Behavior))+#, colour = "grey50")+ - theme_classic(base_size = 20)+ ylim(0,1)+xlim(0,max(new_log$stop.time))+ - xlab('Time since start of recording (in s)')+ - theme(axis.text.y= element_blank(), - axis.ticks.y = element_blank())+ - scale_x_continuous(breaks=c(0,600,2000,4000,6000)) - -ggsave(behavior.log,filename = "behavior_log_plot.eps") - -# #Add sleep/wake sates: -# new_log2=data.frame() -# times = c(log$Time[which(log$Behavior=="sleep")]/1000, -# log$Time[which(log$Behavior=="wakingup")]/1000, -# log$Time[which(log$Behavior=="awake")]/1000) -# new_log2[1,c("Behavior","start.time","stop.time")]=c("sleep",1,times[2]) -# new_log2[2,c("Behavior","start.time","stop.time")]=c("waking.up",times[2],times[3]) -# new_log2[3,c("Behavior","start.time","stop.time")]=c("awake",times[3],max(new_log$stop.time)) -# new_log2$start.time = as.numeric(new_log2$start.time); new_log2$stop.time = as.numeric(new_log2$stop.time) -# new_log2$duration.s = new_log2$stop.time-new_log2$start.time -# new_log2$group.min=1; new_log2$group.max=2 - -# #Combine the two -# final_log=rbind(new_log,new_log2) - -# ggplot(final_log, aes(xmin=start.time, xmax= stop.time, ymin=group.min, ymax=group.max))+ -# geom_rect(aes(fill=Behavior))+ -# theme_classic(base_size = 20)+ -# xlab('Time since start of recording (in s)')+ -# scale_y_discrete(limits=c(0.5, 1.5), labels=c("Behavior","Sleep/Wake State"))+ -# theme(axis.text.y= element_text(angle=45)) - -#other package: -# timeline(new_log, label.col='Behavior', start.col='start.time', end.col='stop.time', -# group.col='Group') diff --git a/Behavior/Generate_R37_pilot_sessions_plots/amos_behavior_session3.R b/Behavior/Generate_R37_pilot_sessions_plots/amos_behavior_session3.R deleted file mode 100644 index b460e22..0000000 --- a/Behavior/Generate_R37_pilot_sessions_plots/amos_behavior_session3.R +++ /dev/null @@ -1,83 +0,0 @@ -#Behavioral timeline during pilot recordings of Amos - -#Load libraries: -library(timeline) -library(ggplot2) - -#Load data: -setwd('C:/Users/Camille Testard/Desktop/Grants_Fellowships/R37_Project/pilot_data/session3') -behavioral_log = read.csv('behavioral_log_session3.csv') -behavioral_log$Start.Stop=as.character(behavioral_log$Start.Stop) -names(behavioral_log)[1]="Time" - -# #For now exclude ambiguous start/stop -# log=behavioral_log[-which(str_length(as.character(behavioral_log$Start.Stop))>5),] -log=behavioral_log - -#Reorganize data for plotting -behavs = as.character(unique(log$Behavior)) -awakeness = behavs[c(3,4)] #sleep/wake states. Seb forgot awake! -behavs = behavs[-c(1:4,9,10,15)] #behaviors -wakingup_time = log$Time[log$Behavior=="Waking up"]; - -#For all behaviors outside of sleep/wake cycle: -new_log=data.frame(); b=1 -for (b in 1:length(behavs)){ - - data=log[log$Behavior==behavs[b],c("Time", "Behavior", "Start.Stop")] - if (length(which(is.na(data$Behavior)))!=0){data=data[-which(is.na(data$Behavior)),]} - - interim=split(data,data$Start.Stop) - names(interim[[1]])[1]="start.time";names(interim[[2]])[1]="stop.time" - - data = cbind(interim[[1]],interim[[2]]); data=data[,-c(3,5,6)] - data=data[,c("Behavior","start.time","stop.time")] - data[,c("start.time","stop.time")]=data[,c("start.time","stop.time")] - - new_log=rbind(new_log,data) -} - -#Add sleeping: -times = c(log$Time[which(log$Behavior=="Waking up")]) -new_log[nrow(new_log)+1,"Behavior"]="Asleep" -new_log[nrow(new_log),"start.time"]=1; new_log[nrow(new_log),"stop.time"]=times[1]; -new_log$duration.s = new_log$stop.time-new_log$start.time - -if (length(which(new_log$duration.s<0)) !=0){print('ERROR NEGATIVE DURATION')} - -new_log$group.min=0; new_log$group.max=1 -new_log$Behavior=as.character(new_log$Behavior) - -#Add resting: -new_log = new_log[order(new_log$start.time),] -start.times=new_log$start.time[2:length(new_log$start.time)] -stop.times=new_log$stop.time[1:length(new_log$stop.time)-1] -intervals = as.numeric(start.times-stop.times) -free_intervals = which(intervals>0) - -resting=data.frame() -for (i in 1:length(free_intervals)){ - resting[i,'start.time']=stop.times[free_intervals[i]] - resting[i,'stop.time']=start.times[free_intervals[i]] -} -resting$duration.s = resting$stop.time-resting$start.time -resting$Behavior="Resting"; -resting$group.min=0; resting$group.max=1; resting=resting[,names(new_log)] - -new_log=rbind(new_log,resting) -new_log = new_log[order(new_log$start.time),] -new_log$Behavior=factor(new_log$Behavior, - levels=c("Aggression","Proximity","Grooming (giving)", "Grooming (receiving)", - "Feeding", "Submission","Yawning", - "Resting","Walking","Drinking","Asleep","Self-directed behavior")) -# #Add wakefulness state -# new_log$wake.state = 'awake' -# new_log$wake.state[1] = 'sleeping' -# wakingup_idx = max(which(new_log$start.time=stress_time) -# new_log$wake.state[stress_idx] = 'stressed' - -#Save to .csv -write.csv(new_log[,-c(5,6)],file='behav_log_restructured.csv',row.names = F) - diff --git a/Behavior/Generate_R37_pilot_sessions_plots/amos_behavior_session4.R b/Behavior/Generate_R37_pilot_sessions_plots/amos_behavior_session4.R deleted file mode 100644 index b69583f..0000000 --- a/Behavior/Generate_R37_pilot_sessions_plots/amos_behavior_session4.R +++ /dev/null @@ -1,125 +0,0 @@ -#Behavioral timeline during pilot recordings of Amos - -#Load libraries: -library(timeline) -library(ggplot2) - -#Load data: -setwd('C:/Users/Camille Testard/Desktop/Grants_Fellowships/R37_Project/pilot_data/session4') -behavioral_log = read.csv('Behavioral_log.csv') -behavioral_log$Start.Stop=as.character(behavioral_log$Start.Stop) - -# #For now exclude ambiguous start/stop -# log=behavioral_log[-which(str_length(as.character(behavioral_log$Start.Stop))>5),] -log=behavioral_log - -#Reorganize data for plotting -behavs = as.character(unique(log$Behavior)) -awakeness = behavs[c(2,4,10,16)] #sleep/wake states -behavs = behavs[-c(1,2,3,4,10,11,16,18)] #behaviors -stress_time = log$Time[log$Behavior=="Stressed"]; -fully_awake_time = log$Time[log$Behavior=="Fully awake"]; -wakingup_time = log$Time[log$Behavior=="Waking up"]; - -#For all behaviors outside of sleep/wake cycle: -new_log=data.frame(); b=1 -for (b in 1:length(behavs)){ - - data=log[log$Behavior==behavs[b],c("Time", "Behavior", "Start.Stop","stressed","simultaneous_behav")] - if (length(which(is.na(data$Behavior)))!=0){data=data[-which(is.na(data$Behavior)),]} - - interim=split(data,data$Start.Stop) - names(interim[[1]])[1]="start.time";names(interim[[2]])[1]="stop.time" - - data = cbind(interim[[1]],interim[[2]]); data=data[,-c(3,4,5,7,8)] - data=data[,c("Behavior","start.time","stop.time","stressed","simultaneous_behav")] - data[,c("start.time","stop.time")]=data[,c("start.time","stop.time")] - - new_log=rbind(new_log,data) -} - -#Add sleeping: -times = c(log$Time[which(log$Behavior=="Waking up")], - log$Time[which(log$Behavior=="Fully awake")]) -new_log[nrow(new_log)+1,"Behavior"]="Asleep" -new_log[nrow(new_log),"start.time"]=1; new_log[nrow(new_log),"stop.time"]=times[1]; -new_log[nrow(new_log),"stressed"]="no"; new_log[nrow(new_log),"simultaneous_behav"]="no"; -new_log$duration.s = new_log$stop.time-new_log$start.time - -if (length(which(new_log$duration.s<0)) !=0){print('ERROR NEGATIVE DURATION')} - -new_log$group.min=0; new_log$group.max=1 -new_log$Behavior=as.character(new_log$Behavior) - -#Add resting: -new_log = new_log[order(new_log$start.time),] -start.times=new_log$start.time[2:length(new_log$start.time)] -stop.times=new_log$stop.time[1:length(new_log$stop.time)-1] -intervals = as.numeric(start.times-stop.times) -free_intervals = which(intervals>0) - -resting=data.frame() -for (i in 1:length(free_intervals)){ - resting[i,'start.time']=stop.times[free_intervals[i]] - resting[i,'stop.time']=start.times[free_intervals[i]] -} -resting$duration.s = resting$stop.time-resting$start.time -resting$Behavior="Resting"; resting$stressed="no"; resting$stressed[resting$start.time>stress_time]="yes"; -resting$simultaneous_behav="no"; -resting$group.min=0; resting$group.max=1; resting=resting[,names(new_log)] - -new_log=rbind(new_log,resting) -new_log = new_log[order(new_log$start.time),] -new_log$Behavior=factor(new_log$Behavior, - levels=c("Aggression","Proximity","Grooming (giving)", "Grooming (receiving)", - "Feeding", "Submission","Yawning", - "Resting","Walking","Drinking","Asleep","Self-directed behavior")) -#Add wakefulness state -new_log$wake.state = 'awake' -new_log$wake.state[1] = 'sleeping' -wakingup_idx = max(which(new_log$start.time=stress_time) -new_log$wake.state[stress_idx] = 'stressed' - -#Save to .csv -write.csv(new_log[,-c(7,8)],file='behav_log_restructured.csv',row.names = F) - -#Plot -behavior.log<-ggplot(new_log, aes(xmin=start.time, xmax= stop.time, ymin=group.min, ymax=group.max))+ - geom_rect(aes(fill=Behavior))+#, colour = "grey50")+ - theme_classic(base_size = 20)+ ylim(0,1)+xlim(0,max(new_log$stop.time))+ - geom_vline(xintercept=wakingup_time, lwd=2)+ geom_vline(xintercept=fully_awake_time,lwd=2)+ - geom_vline(xintercept=stress_time,lwd=2)+ - xlab('Time since start of recording (in s)')+ - theme(axis.text.y= element_blank(), - axis.ticks.y = element_blank())+ - scale_x_continuous(breaks=c(0,600,2000,4000,6000, 8000)) - -ggsave(behavior.log,filename = "behavior_log_plot.eps") - -# #Add sleep/wake sates: -# new_log2=data.frame() -# times = c(log$Time[which(log$Behavior=="sleep")]/1000, -# log$Time[which(log$Behavior=="wakingup")]/1000, -# log$Time[which(log$Behavior=="awake")]/1000) -# new_log2[1,c("Behavior","start.time","stop.time")]=c("sleep",1,times[2]) -# new_log2[2,c("Behavior","start.time","stop.time")]=c("waking.up",times[2],times[3]) -# new_log2[3,c("Behavior","start.time","stop.time")]=c("awake",times[3],max(new_log$stop.time)) -# new_log2$start.time = as.numeric(new_log2$start.time); new_log2$stop.time = as.numeric(new_log2$stop.time) -# new_log2$duration.s = new_log2$stop.time-new_log2$start.time -# new_log2$group.min=1; new_log2$group.max=2 - -# #Combine the two -# final_log=rbind(new_log,new_log2) - -# ggplot(final_log, aes(xmin=start.time, xmax= stop.time, ymin=group.min, ymax=group.max))+ -# geom_rect(aes(fill=Behavior))+ -# theme_classic(base_size = 20)+ -# xlab('Time since start of recording (in s)')+ -# scale_y_discrete(limits=c(0.5, 1.5), labels=c("Behavior","Sleep/Wake State"))+ -# theme(axis.text.y= element_text(angle=45)) - -#other package: -# timeline(new_log, label.col='Behavior', start.col='start.time', end.col='stop.time', -# group.col='Group') diff --git a/Behavior/log_behavior_freq_partner.m b/Behavior/log_behavior_freq_partner.m deleted file mode 100644 index 5362bc4..0000000 --- a/Behavior/log_behavior_freq_partner.m +++ /dev/null @@ -1,144 +0,0 @@ -%% Log_behavior_freq_partner -%This script computes the duration and proportion of the partner's behaviors across -%sessions -%Testard C. August 2022 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =1; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) - - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - %Get behaviors - unq_behavior_labels = cell2mat({labels_partner{:,3}}'); - - %Adjust behaviors - unq_behavior_labels(unq_behavior_labels==find(matches(behav_categ,'Proximity')))=length(behav_categ); %set proximity to rest - unq_behavior_labels(unq_behavior_labels==find(matches(behav_categ,'Squeeze partner')))=find(matches(behav_categ,'Threat to partner')); %set proximity to rest - unq_behavior_labels(unq_behavior_labels==find(matches(behav_categ,'Squeeze Subject')))=find(matches(behav_categ,'Threat to subject')); %set proximity to rest - - behavior_labels = unq_behavior_labels;%(unq_behavior_labels~=length(behav_categ)); - behav = unique(behavior_labels); - - %Get block info - block_lbl = cell2mat({labels{:,12}}'); - block_lbl = block_lbl;%(unq_behavior_labels~=length(behav_categ)); - block_categ = string({labels{:,10}}'); - block_categ = block_categ;%(unq_behavior_labels~=length(behav_categ)); - - for beh = 1:length(behav_categ) - for bl = 1:3 - beh_stats(s, beh, bl)=length(find(behavior_labels == beh & block_lbl==bl)); - end - end - -% cats = reordercats(categorical(behav_categ),{'Rest','Getting groomed','Groom partner',... -% 'Self-groom','Scratch','Foraging','Threat to partner','Threat to subject',... -% 'Other monkeys vocalize','Drinking','Approach','Rowdy Room','Groom sollicitation',... -% 'Travel','Leave','Mounting','Aggression','Masturbating','Vocalization','Submission',... -% 'Yawning','Swinging','Head Bobbing','Object Manipulation','Lip smack',... -% 'Butt sniff','Squeeze partner','Squeeze Subject','Proximity'}); -% figure; hold on; set(gcf,'Position',[150 250 700 300]) -% bar(cats,squeeze(beh_stats(s,:,:)), 'stacked') -% legend('Paired, F neighbor', 'Paired, M neighbor','Alone') -% ylabel('seconds') -% set(gca,'FontSize',12); - - -end %end of session loop - -beh_dur_lovelace = squeeze(nansum(beh_stats(a_sessions,:,:),1)); -beh_dur_sallyride = squeeze(nansum(beh_stats(h_sessions,:,:),1)); - - -figure; hold on; set(gcf,'Position',[150 250 700 500]) -[~, idx_sorted_lovelace]=sort(sum(beh_dur_lovelace,2),'descend'); -cats = reordercats(categorical(behav_categ),string(behav_categ(idx_sorted_lovelace))); -subplot(2,1,1) -bar(cats,beh_dur_lovelace, 'stacked') -legend('Paired, F neighbor', 'Paired, M neighbor','Alone') -ylabel('seconds'); ylim([0 12000]); -set(gca,'FontSize',12); -title('Duration of behavior by block, Monkey L (Amos partner)') - -[~, idx_sorted_sallyride]=sort(sum(beh_dur_sallyride,2),'descend'); -cats = reordercats(categorical(behav_categ),string(behav_categ(idx_sorted_lovelace))); -subplot(2,1,2) -bar(cats,beh_dur_sallyride, 'stacked') -legend('Paired, F neighbor', 'Paired, M neighbor','Alone') -ylabel('seconds'); ylim([0 12000]); -set(gca,'FontSize',12); -title('Duration of behavior by block, Monkey S (Hooke partner)') - -%Plot proportion - -beh_prop_lovelace=beh_dur_lovelace./sum(sum(beh_dur_lovelace,2))*100; -beh_prop_sallyride=beh_dur_sallyride./sum(sum(beh_dur_sallyride,2))*100; - -figure; hold on; set(gcf,'Position',[150 250 700 500]) -cats = reordercats(categorical(behav_categ),string(behav_categ(idx_sorted_lovelace))); -subplot(2,1,1) -bar(cats,beh_prop_lovelace, 'stacked') -legend('Paired, F neighbor', 'Paired, M neighbor','Alone') -ylabel('% Time'); ylim([0 100]); -set(gca,'FontSize',12); -title('% of behavior by block, Monkey L, (Amos partner)') - -cats = reordercats(categorical(behav_categ),string(behav_categ(idx_sorted_sallyride))); -subplot(2,1,2) -bar(cats,beh_prop_sallyride, 'stacked') -legend('Paired, F neighbor', 'Paired, M neighbor','Alone') -ylabel('% Time'); ylim([0 100]); -set(gca,'FontSize',12); -title('% of behavior by block, Monkey S, (Hooke partner)') - diff --git a/Behavior/log_cooccurrence_behav.m b/Behavior/log_cooccurrence_behav.m deleted file mode 100644 index 99d2e52..0000000 --- a/Behavior/log_cooccurrence_behav.m +++ /dev/null @@ -1,89 +0,0 @@ -%% Log_transition_prob -%This script finds behavioral transtions, computes a transition matrix -%and plots a transition probability graph. -%Testard C. Feb 2022 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) - - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -behav_rest=nan(1,length(sessions)); % rest -behav_single=nan(1,length(sessions)); % single behavior recorded -behav_coWithProx=nan(1,length(sessions)); % behvior co-occurs with proximity or RR -behav_cooccur=nan(1,length(sessions)); % two key behaviors co-occurring -all_seconds=nan(1,length(sessions)); - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - %Get probability of co-occrrence - co_occurrence = cell2mat({labels{:,5}}'); - %Important notes: - %0: no behavior recorded (rest) - %1: only one behavior at a time - %2: Behavior co-occurs with proximity or RR - %3: Proximity and RR co-occur - %4: Co-occur with groom present - %5: Co-occur with other monkeys vocalize - %6: Two 'key' behaviors co-occur - - behav_rest(s) = length(find(co_occurrence==0)); % rest - behav_single(s) = length(find(co_occurrence==1)); % single behavior recorded - behav_coWithProx(s) = length(find(co_occurrence==1|co_occurrence==2)); % behvior co-occurs with proximity or RR - behav_cooccur(s) = length(find(co_occurrence>=4)); % two key behaviors co-occurring - all_seconds(s) = size(labels,1); - -end %end of session loop - -nansum(behav_rest)/nansum(all_seconds) -nansum(behav_cooccur)/nansum(all_seconds) - diff --git a/Behavior/old_code/Format_EventLog_hms.R b/Behavior/old_code/Format_EventLog_hms.R deleted file mode 100644 index 71ed166..0000000 --- a/Behavior/old_code/Format_EventLog_hms.R +++ /dev/null @@ -1,18 +0,0 @@ -# Format Event Log - -#Load libraries -library(stringr) -library(lubridate) - -#Load data -file = file.choose() -eventlog=read.csv(file) -date = '2021-08-15' - -#Format time column -eventlog$Time.stamp.no.ms=strtrim(eventlog$Time.stamp,8) - -#Save -dir <- dirname(file) -write.csv(eventlog, file=paste(dir, '/EVENTLOG_', date,'_formatted.csv',sep=""), row.names = F) - \ No newline at end of file diff --git a/Behavior/old_code/Format_behavior_log_old.R b/Behavior/old_code/Format_behavior_log_old.R deleted file mode 100644 index e115a3c..0000000 --- a/Behavior/old_code/Format_behavior_log_old.R +++ /dev/null @@ -1,143 +0,0 @@ -#Format_behavior_log.R -#This script re-formats the deuteron behavior log in a way that is easily interpretable for neural data analysis - -#Load libraries: -library(timeline) -library(ggplot2) -library(utils) - -setwd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/EventLogs/EventLogs_Done') - -#Load data: -file = file.choose() # chose the formatted behavior file -behavioral_log = read.csv(file) - -# #For now exclude ambiguous start/end -# log=behavioral_log[-which(str_length(as.character(behavioral_log$Start.end))>5),] -log=behavioral_log - -#Reorganize data for plotting -behavs = as.character(unique(log$Behavior)) -behavs = behavs[!(is.na(behavs) | behavs=="")] -#awakeness = behavs[c(2,3,5,11)] #sleep/wake states - -#Only keep behaviors. STOP!!!! need to check for each session because behavior ordering differs from one session to the next. -#behavs = behavs[-c(1,2)] #only keep behaviors -to_remove = cbind("Started recording", "Camera Sync", "Stopped recording") -if (any(is.na(match(to_remove, behavs)))) { - behavs - to_remove = to_remove[!is.na(match(to_remove, behavs))] -} -behavs = behavs[-match(to_remove, behavs)] #behavs[-c(1,2,length(behavs))] #only keep behaviors - -#For all behaviors outside of sleep/wake cycle: -new_log=data.frame(); b=1 -for (b in 11:length(behavs)){ - - data=log[log$Behavior==behavs[b],c("Time", "Behavior", "Start.end")] - if (length(which(is.na(data$Behavior)))!=0){data=data[-which(is.na(data$Behavior)),]}# remove rows with empty/NA behavior - - interim=split(data,data$Start.end) - names(interim[[2]])[1]="start.time";names(interim[[1]])[1]="end.time" - - data = cbind(interim[[1]],interim[[2]]); data=data[,-c(3,5,6)] - data=data[,c("Behavior","start.time","end.time")] - data[,c("start.time","end.time")]=data[,c("start.time","end.time")]/1000 - - new_log=rbind(new_log,data) -} - -# #Add sleeping: -# times = c(log$Time[which(log$Behavior=="wakingup")]/1000, -# log$Time[which(log$Behavior=="awake")]/1000) -# new_log[nrow(new_log)+1,"Behavior"]="Sleeping" -# new_log[nrow(new_log),"start.time"]=1; new_log[nrow(new_log),"end.time"]=times[1]; -# new_log$duration.s = new_log$end.time-new_log$start.time - -new_log$group.min=0; new_log$group.max=1 -new_log$Behavior=as.character(new_log$Behavior) - -# #Add resting (i.e. whenever no behavior was recorded): -# new_log = new_log[order(new_log$start.time),] -# start.times=new_log$start.time[2:length(new_log$start.time)] -# end.times=new_log$end.time[1:length(new_log$end.time)-1] -# intervals = as.numeric(start.times-end.times) -# free_intervals = which(intervals>0) -# -# resting=data.frame() -# for (i in 1:length(free_intervals)){ -# resting[i,'start.time']=end.times[free_intervals[i]] -# resting[i,'end.time']=start.times[free_intervals[i]] -# } -# resting$duration.s = resting$end.time-resting$start.time -# resting$Behavior="Resting"; resting$group.min=0; resting$group.max=1; resting=resting[,names(new_log)] -# -# new_log=rbind(new_log,resting) - -#Order new log chronologically -new_log = new_log[order(new_log$start.time),] -new_log$duration.s = new_log$end.time - new_log$start.time - -if (length(which(new_log$duration.s<0))>0){stop("NEGATIVE DURATION")} - -#Order behaviors for later plotting -new_log_final = new_log; unique(new_log$Behavior) -new_log_final$Behavior=factor(new_log_final$Behavior, - levels=c("Aggression","Proximity","Groom Give", "HIP","Foraging", "Vocalization","SS", "Masturbating", - "Submission", "Approach","Yawning","Self-groom","HIS","Other monkeys vocalize", - "Groom Receive","Leave","Drinking","SP","Pacing/Travel","Scratch","RR")) - -#Remove NAs (for behavior categories we do not consider here) -new_log_final = new_log_final[!is.na(new_log_final$Behavior),] - -# levels=c("Aggression","Proximity","Grooming", -# "Feeding", "Submission", -# "Resting","Walking","Drinking","Sleeping","Self-directed behavior")) - -# #Add wakefulness state -# new_log$wake.state = 'awake' -# new_log$wake.state[1] = 'sleeping' -# new_log$wake.state[2:11] = 'wakingup' - -#Save to .csv -# output_file = utils::choose.dir(default = "", caption = "Select folder") # choose output directory -# dir <- dirname(output_file) -setwd('~/Dropbox (Penn)/Deuteron_Backup/Deuteron_Data_Backup/Ready to analyze output/') -write.csv(new_log_final[,-c(4,5)],file=paste('EVENTLOG_restructured',as.character(substr(file, 95, 113)),'.csv',sep=""),row.names = F) - -#Plot -behavior.log<-ggplot(new_log_final, aes(xmin=start.time, xmax= end.time, ymin=group.min, ymax=group.max))+ - geom_rect(aes(fill=Behavior))+#, colour = "grey50")+ - theme_classic(base_size = 20)+ ylim(0,1)+xlim(0,max(new_log$end.time))+ - xlab('Time since start of recording (in s)')+ - theme(axis.text.y= element_blank(), - axis.ticks.y = element_blank())#+ - #scale_x_continuous(breaks=c(0,600,2000,4000,6000)) - -ggsave(behavior.log,filename = paste("behavior_log_plot",as.character(substr(file, 95, 113)),".png")) - -# #Add sleep/wake sates: -# new_log2=data.frame() -# times = c(log$Time[which(log$Behavior=="sleep")]/1000, -# log$Time[which(log$Behavior=="wakingup")]/1000, -# log$Time[which(log$Behavior=="awake")]/1000) -# new_log2[1,c("Behavior","start.time","end.time")]=c("sleep",1,times[2]) -# new_log2[2,c("Behavior","start.time","end.time")]=c("waking.up",times[2],times[3]) -# new_log2[3,c("Behavior","start.time","end.time")]=c("awake",times[3],max(new_log$end.time)) -# new_log2$start.time = as.numeric(new_log2$start.time); new_log2$end.time = as.numeric(new_log2$end.time) -# new_log2$duration.s = new_log2$end.time-new_log2$start.time -# new_log2$group.min=1; new_log2$group.max=2 - -# #Combine the two -# final_log=rbind(new_log,new_log2) - -# ggplot(final_log, aes(xmin=start.time, xmax= end.time, ymin=group.min, ymax=group.max))+ -# geom_rect(aes(fill=Behavior))+ -# theme_classic(base_size = 20)+ -# xlab('Time since start of recording (in s)')+ -# scale_y_discrete(limits=c(0.5, 1.5), labels=c("Behavior","Sleep/Wake State"))+ -# theme(axis.text.y= element_text(angle=45)) - -#other package: -# timeline(new_log, label.col='Behavior', start.col='start.time', end.col='end.time', -# group.col='Group') diff --git a/Behavior/old_code/Get_freq_prop_behav.R b/Behavior/old_code/Get_freq_prop_behav.R deleted file mode 100644 index 050b848..0000000 --- a/Behavior/old_code/Get_freq_prop_behav.R +++ /dev/null @@ -1,177 +0,0 @@ -#Get_freq_prop_behav.R - -#Load libraries: -library(ggplot2) -library(utils) -library(xlsx) -library(dplyr) -library(plotrix) - -#Load data: -sessions = c("Amos_2021-07-29","Hooke_2021-08-02","Amos_2021-08-03","Hooke_2021-08-05", - "Amos_2021-08-09","Hooke_2021-08-12","Amos_2021-08-13","Amos_2021-08-16", - "Hooke_2021-08-19","Amos_2021-08-20","Hooke_2021-08-21","Hooke_2021-08-29", - "Hooke_2021-09-03") -a_sessions = c(1,3,5,7,8,10) -h_sessions = c(2,4,6,9,11,12,13) - -all_logs = data.frame(); session_length = vector(); s=1 -for (s in 1:length(sessions)){ - setwd(paste('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/', sessions[s],sep="")) - log = read.csv(paste('EVENTLOG_restructured.csv',sep="")) - log$session_num = s - session_length[s] = log$end.time[nrow(log)]; #This should be correct within a few seconds. - all_logs= rbind(all_logs, log) -} - -#Rename certain behaviors: -all_logs$Behavior[all_logs$Behavior=="RR"]= "Rowdy room" -all_logs$Behavior[all_logs$Behavior=="Groom Give"]= "Groom partner" -all_logs$Behavior[all_logs$Behavior=="Groom Receive"]= "Getting groomed" -all_logs$Behavior[all_logs$Behavior=="Pacing/Travel"]= "Travel" -all_logs$Behavior[all_logs$Behavior=="HIS"]= "Threat to subject" -all_logs$Behavior[all_logs$Behavior=="HIP"]= "Threat to partner" -all_logs$Behavior[all_logs$Behavior=="SS"]= "Threat to subject" -all_logs$Behavior[all_logs$Behavior=="SP"]= "Threat to partner" -all_logs$Behavior[all_logs$Behavior=="Grm prsnt"]= "Groom solicitation" - -#Remove blocks -all_logs = all_logs[-grep("block",all_logs$Behavior),] - -#Get frequency table per behavior -behavs = unique(all_logs$Behavior); -behav_categ_social = c("social","social","asocial","social","asocial", "social", - "social","social","social","social","asocial","asocial", - "social","social","asocial","asocial","social","social","asocial", - "social","social","asocial", "social") -behav_categ_valence = c("affiliative", "affiliative","neutral","neutral","neutral", - "agonistic","neutral","affiliative","affiliative", "affiliative", - "neutral","neutral","affiliative","neutral","neutral","neutral", - "agonistic","agonistic","neutral","affiliative", - "agonistic","neutral","neutral") -cbind(behavs, behav_categ_social,behav_categ_valence) -behav_freq_table = data.frame(); b=1; s=1 -behav_freq_table_full = data.frame() - -for (s in 1:length(sessions)){ - - for (b in 1:length(behavs)){ - behav_freq_table[b,'Behavior'] = behavs[b] - behav_freq_table[b,'social_categ'] = behav_categ_social[b] - behav_freq_table[b,'valence_categ'] = behav_categ_valence[b] - behav_freq_table[b,'session'] = s - behav_freq_table[b,'obs.time'] = session_length[s] - behav_freq_table[b,'total.events'] = nrow(all_logs) - idx = which(all_logs$Behavior == behavs[b] & all_logs$session_num == s) - behav_freq_table[b,'duration'] = sum(all_logs$duration.s[idx]) - behav_freq_table[b,'proportion'] = behav_freq_table[b,'duration']/behav_freq_table[b,'obs.time'] - behav_freq_table[b,'num.events'] = length(idx) - } - - setwd(paste('~/Dropbox (Penn)/Datalogger/Results/', sessions[s],"/Behavior_results",sep="")) - - #Pie chart based on duration - png(file=paste("Behavior_duration_", sessions[s],".png",sep="")) - data = behav_freq_table[intersect(which(behav_freq_table$duration>0), which(behav_freq_table$Behavior!= "Proximity")),] - slices <- data$duration - lbls <- data$Behavior - pie(slices, labels = lbls, main="Behaviors observed (duration)", cex=0.6) - # pie3D(slices, labels = lbls, explode=0.1, main="Behaviors observed", labelcex=0.3) - dev.off() - - #Pie chart based on number of events: - png(file=paste("Behavior_events_", sessions[s],".png",sep="")) - slices <- data$num.events - lbls <- data$Behavior - pie(slices, labels = lbls, main="Behaviors observed (events)", cex=0.6) - dev.off() - -p.dur<- ggplot(data=behav_freq_table, aes(x=reorder(Behavior, duration), y=duration, fill=categ)) + - geom_bar(stat="identity")+ coord_polar("y", start=0)+ - xlab('Behaviors')+ ylab('Duration (in s)')+ - labs(fill='Category')+ - coord_flip()+ theme_classic(base_size = 14) + ggtitle(sessions[s]) - #ggsave(paste("Behavior_duration_", sessions[s],".png",sep="")) - - p.frq<- ggplot(data=behav_freq_table, aes(x=reorder(Behavior, num.events), y=num.events, fill=categ)) + - geom_bar(stat="identity")+ xlab('Behaviors')+ ylab('# Occurences')+ - coord_flip()+ theme_classic(base_size = 18)+ ggtitle(sessions[s]) - #ggsave(paste("Behavior_frequency_", sessions[s],".png",sep="")) - - behav_freq_table_full = rbind(behav_freq_table_full, behav_freq_table) -} - -setwd('~/Dropbox (Penn)/Datalogger/Results/All_sessions/') - - -for (b in 1:length(behavs)){ - behav_freq_table[b,'Behavior'] = behavs[b] - behav_freq_table[b,'social_categ'] = behav_categ_social[b] - behav_freq_table[b,'valence_categ'] = behav_categ_valence[b] - behav_freq_table[b,'session'] = s - behav_freq_table[b,'obs.time'] = session_length[s] - behav_freq_table[b,'total.events'] = nrow(all_logs) - idx = which(all_logs$Behavior == behavs[b]) - behav_freq_table[b,'duration'] = sum(all_logs$duration.s[idx]) - behav_freq_table[b,'proportion'] = behav_freq_table[b,'duration']/behav_freq_table[b,'obs.time'] - behav_freq_table[b,'num.events'] = length(idx) -} - -#eps(file=paste("Behavior_duration_allSessions.png",sep="")) -data = behav_freq_table[intersect(which(behav_freq_table$duration>100), which(behav_freq_table$Behavior!= "Proximity")),] -slices <- data$duration -lbls <- data$Behavior -pie(slices, labels = lbls, main="Behaviors observed (duration)", cex=1, col=rainbow(nrow(data))) -# pie3D(slices, labels = lbls, explode=0.1, main="Behaviors observed", labelcex=0.3) -#dev.off() - -data2 = data.frame() -data2[1,"social_categ"] = "social" -data2[1,"duration"] = sum(data$duration[data$social_categ=="social"]) -data2[2,"social_categ"] = "asocial" -data2[2,"duration"] = sum(data$duration[data$social_categ=="asocial"]) -slices <- data2$duration -lbls <- data2$social_categ -pie(slices, labels = lbls, main="Behaviors observed (duration)", cex=2, col=cm.colors(2)) -data2$duration[1]/sum(data2$duration) - -data3 = data.frame() -data3[1,"valence"] = "affiliative" -data3[1,"duration"] = sum(data$duration[data$valence_categ=="affiliative"]) -data3[2,"valence"] = "agonistic" -data3[2,"duration"] = sum(data$duration[data$valence_categ=="agonistic"]) -slices <- data3$duration -lbls <- data3$valence -pie(slices, labels = lbls, main="Behaviors observed (duration)", cex=2, col=heat.colors(2)) -data3$duration[1]/sum(data3$duration) - -#Format behavior freq table -behav_freq_table_full<-behav_freq_table_full[behav_freq_table_full$Behavior!="Proximity",] -behav_freq_table_full$duration[is.na(behav_freq_table_full$duration)]=0 - -#plot frequency -ggplot(data=behav_freq_table_full, aes(x=reorder(Behavior, num.events), y=num.events, fill=social_categ)) + - geom_bar(stat="identity")+ xlab('Behaviors')+ ylab('# Occurences')+ - coord_flip()+ theme_classic(base_size = 18)+ ggtitle("All session cumulated") -ggsave(paste("Behavior_frequency_allSessions.png",sep="")) - -#plot duration -ggplot(data=behav_freq_table_full, aes(x=reorder(Behavior, duration), y=duration)) + - geom_bar(stat="identity")+ xlab('Behaviors')+ ylab('Duration (s)')+ - coord_flip()+ theme_classic(base_size = 16)+ ggtitle("All session cumulated") -ggsave(paste("Behavior_duration_allSessions.png",sep="")) - -ggplot(data=behav_freq_table_full[behav_freq_table_full$session %in% a_sessions,], aes(x=reorder(Behavior, duration), y=duration)) + - geom_bar(stat="identity")+ xlab('Behaviors')+ ylab('Duration (s)')+ - coord_flip()+ theme_classic(base_size = 16)+ ggtitle("Amos") -ggsave(paste("Behavior_duration_AmosSessions.png",sep="")) - -ggplot(data=behav_freq_table_full[behav_freq_table_full$session %in% h_sessions,], aes(x=reorder(Behavior, duration), y=duration)) + - geom_bar(stat="identity")+ xlab('Behaviors')+ ylab('Duration (s)')+ - coord_flip()+ theme_classic(base_size = 16)+ ggtitle("Hooke") -ggsave(paste("Behavior_duration_HookeSessions.png",sep="")) - -ggplot(data=behav_freq_table_full, aes(x=reorder(Behavior, proportion), y=proportion/4, fill=categ)) + - geom_bar(stat="identity")+ xlab('Behaviors')+ ylab('Proportion')+ - coord_flip()+ theme_classic(base_size = 18)+ ggtitle("All session cumulated") -ggsave(paste("Behavior_proportion_allSessions.png",sep="")) diff --git a/Behavior/old_code/Get_freq_prop_behav_partner.R b/Behavior/old_code/Get_freq_prop_behav_partner.R deleted file mode 100644 index 8798066..0000000 --- a/Behavior/old_code/Get_freq_prop_behav_partner.R +++ /dev/null @@ -1,172 +0,0 @@ -#Get_freq_prop_behav.R - -#Load libraries: -library(ggplot2) -library(utils) -library(xlsx) -library(dplyr) -library(plotrix) - -#Load data: -sessions = c("Amos_2021-07-29","Hooke_2021-08-02","Amos_2021-08-03", - "Hooke_2021-08-05","Amos_2021-08-09","Hooke_2021-08-12") -a_sessions = c(1,3,5) -h_sessions = c(2,4,6) - -setwd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -behav_categs = read.csv(file="behav_categs.csv") - -all_logs_subject = data.frame(); all_logs_partner = data.frame(); session_length = vector(); s=4 -for (s in 1:length(sessions)){ - setwd(paste('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/', sessions[s],sep="")) - log_subject = read.csv(paste('EVENTLOG_restructured.csv',sep="")) - log_subject$session_num = s - - log_partner = read.csv(paste('EVENTLOG_restructured_partner.csv',sep="")) - log_partner$session_num = s - - session_length[s] = log_subject$end.time[nrow(log_subject)]; #This should be correct within a few seconds. - - all_logs_subject= rbind(all_logs_subject, log_subject) - all_logs_partner= rbind(all_logs_partner, log_partner) -} - -#Rename certain behaviors: -all_logs_subject$Behavior[all_logs_subject$Behavior=="RR"]= "Rowdy room" -all_logs_subject$Behavior[all_logs_subject$Behavior=="Groom Give"]= "Groom partner" -all_logs_subject$Behavior[all_logs_subject$Behavior=="Groom Receive"]= "Getting groomed" -all_logs_subject$Behavior[all_logs_subject$Behavior=="Pacing/Travel"]= "Travel" -all_logs_subject$Behavior[all_logs_subject$Behavior=="HIS"]= "Threat to subject" -all_logs_subject$Behavior[all_logs_subject$Behavior=="HIP"]= "Threat to partner" -all_logs_subject$Behavior[all_logs_subject$Behavior=="SS"]= "Threat to subject" -all_logs_subject$Behavior[all_logs_subject$Behavior=="SP"]= "Threat to partner" -all_logs_subject$Behavior[all_logs_subject$Behavior=="Grm prsnt"]= "Limb presentation" - -all_logs_partner$Behavior[all_logs_partner$Behavior=="RR"]= "Rowdy room" -all_logs_partner$Behavior[all_logs_partner$Behavior=="Groom Give"]= "Groom partner" -all_logs_partner$Behavior[all_logs_partner$Behavior=="Groom Receive"]= "Getting groomed" -all_logs_partner$Behavior[all_logs_partner$Behavior=="Pacing/Travel"]= "Travel" -all_logs_partner$Behavior[all_logs_partner$Behavior=="HIS"]= "Threat to subject" -all_logs_partner$Behavior[all_logs_partner$Behavior=="HIP"]= "Threat to partner" -all_logs_partner$Behavior[all_logs_partner$Behavior=="SS"]= "Threat to subject" -all_logs_partner$Behavior[all_logs_partner$Behavior=="SP"]= "Threat to partner" -all_logs_partner$Behavior[all_logs_partner$Behavior=="Grm prsnt"]= "Limb presentation" - -#Remove blocks -all_logs_subject = all_logs_subject[-grep("block",all_logs_subject$Behavior),] -all_logs_partner = all_logs_partner[-grep("block",all_logs_partner$Behavior),] - -#Remove NAs -which(is.na(all_logs_subject), arr.ind = T) -which(is.na(all_logs_partner), arr.ind = T) -all_logs_subject=all_logs_subject[!is.na(all_logs_subject$Behavior),] -all_logs_partner=all_logs_partner[!is.na(all_logs_partner$Behavior),] - -#Get behavior category labels -behavs =behav_categs$behavs -# behavs = unique(c(all_logs_partner$Behavior, all_logs_subject$Behavior)); -# behav_categ_social = c("social","social","asocial","asocial","social","asocial", "social", -# "social","asocial","social","asocial","asocial","social", -# "asocial","asocial","social","social","social","asocial","asocial", -# "asocial","social","asocial", "social", "social", "social") -# behav_categ_valence = c("affiliative", "neutral","neutral","neutral","affiliative", -# "neutral","agonistic","affiliative","neutral", "affiliative", -# "neutral","neutral","affiliative","neutral","neutral","agonistic", -# "agonistic","affiliative","neutral", -# "neutral","neutral","affiliative","neutral","affiliative", -# "agonistic","neutral") -# behav_categ_recip = c("reciprocal","Not", "Not","Not","Not","reciprocal","Not","reciprocal", -# "Not","reciprocal","Not","Not", "reciprocal", -# "Not","Not","reciprocal","reciprocal","Not","reciprocal","Not","Not", -# "Not","Not","Not","Not","Not") -# behav_categs = cbind(behavs, behav_categ_social,behav_categ_valence, behav_categ_recip) -# write.csv(file="behav_categs.csv",behav_categs, row.names = F) - - -######################################################### -## Get behavior partitioning for all sessions - -setwd('~/Dropbox (Penn)/Datalogger/Results/All_sessions/') - -behav_freq_table = data.frame(); behav_freq_table_all = data.frame(); b=11; -for (s in 1:length(sessions)){ - for (b in 1:length(behavs)){ - - behav_freq_table[b,'Behavior'] = behavs[b] - behav_freq_table[b,'social_categ'] = behav_categs$behav_categ_social[b] - behav_freq_table[b,'valence_categ'] = behav_categs$behav_categ_valence[b] - behav_freq_table[b,'recip_categ'] = behav_categs$behav_categ_recip[b] - behav_freq_table[b,'session']=s - - idx_partner = which(all_logs_partner$Behavior == behavs[b] & all_logs_partner$session_num==s) - sec_partner = vector() - if (length(idx_partner)>0) { - for (inter in 1:length(idx_partner)){ - sec_partner = c(sec_partner, round(c(all_logs_partner$start.time[idx_partner[inter]]: - (all_logs_partner$end.time[idx_partner[inter]]-1)))) - } - } - behav_freq_table[b,'duration.partner'] = sum(all_logs_partner$duration.s[idx_partner]) - behav_freq_table[b,'proportion.partner'] = behav_freq_table[b,'duration.partner']/session_length[s] - behav_freq_table[b,'num.events.partner'] = length(idx_partner) - - idx_subject = which(all_logs_subject$Behavior == behavs[b] & all_logs_subject$session_num==s) - sec_subject = vector() - if(length(idx_subject)>0){ - for (inter in 1:length(idx_subject)){ - - sec_subject = c(sec_subject, round(c(all_logs_subject$start.time[idx_subject[inter]]: - (all_logs_subject$end.time[idx_subject[inter]]-1)))) - } - } - behav_freq_table[b,'duration.subject'] = sum(all_logs_subject$duration.s[idx_subject]) - behav_freq_table[b,'proportion.subject'] = behav_freq_table[b,'duration.subject']/session_length[s] - behav_freq_table[b,'num.events.subject'] = length(idx_subject) - - behav_freq_table[b,'duration.overlap'] = length(intersect(sec_partner, sec_subject)) - } - - behav_freq_table_all =rbind(behav_freq_table_all, behav_freq_table) -} - -data = behav_freq_table_all[which(behav_freq_table_all$Behavior!= "Proximity"),] - -data2 = data.frame() -data2[1,"social_categ"] = "social" -data2[1,"duration"] = sum(data$duration.subject[data$social_categ=="social"]) -data2[2,"social_categ"] = "asocial" -data2[2,"duration"] = sum(data$duration.subject[data$social_categ=="asocial"]) -slices <- data2$duration -lbls <- data2$social_categ -pie(slices, labels = lbls, main="Behaviors observed (duration)", cex=2, col=cm.colors(2)) -data2$duration[1]/sum(data2$duration) - -data3 = data.frame() -data3[1,"valence"] = "affiliative" -data3[1,"duration"] = sum(data$duration.subject[data$valence_categ=="affiliative"]) -data3[2,"valence"] = "agonistic" -data3[2,"duration"] = sum(data$duration.subject[data$valence_categ=="agonistic"]) -slices <- data3$duration -lbls <- data3$valence -pie(slices, labels = lbls, main="Behaviors observed (duration)", cex=2, col=heat.colors(2)) -data3$duration[1]/sum(data3$duration) - -data4 = data.frame() -data4[1,"recip"] = "reciprocal" -data4[1,"duration"] = sum(data$duration.subject[data$recip_categ=="reciprocal"]) -data4[2,"recip"] = "Not" -data4[2,"duration"] = sum(data$duration.subject[data$recip_categ=="Not"]) -slices <- data4$duration -lbls <- data4$recip -pie(slices, labels = lbls, main="Behaviors observed (duration)", cex=2, col=terrain.colors(2)) -data4$duration[1]/sum(data4$duration) - -data5 = data.frame() -data5[1,"overlap"] = "Subject-partner overlap" -data5[1,"duration"] = sum(behav_freq_table_all$duration.overlap) -data5[2,"overlap"] = "Different" -data5[2,"duration"] = sum(session_length) - sum(behav_freq_table_all$duration.overlap) -slices <- data5$duration -lbls <- data5$overlap -pie(slices, labels = lbls, main="Behaviors observed (duration)", cex=2, col=topo.colors(2)) -data5$duration[1]/sum(data5$duration) diff --git a/Behavior/old_code/NullBehavior_develop.m b/Behavior/old_code/NullBehavior_develop.m deleted file mode 100644 index e942735..0000000 --- a/Behavior/old_code/NullBehavior_develop.m +++ /dev/null @@ -1,77 +0,0 @@ -%% Running Notes -%Script for testing out function to generate simulated behavior for any -%particular session based on the statistics of that session. - -%Currently thinking to "fit" p value for bernoulli for each behavior and -%some term for how long each behavior tends to last. All "coins" -%for each behavior are flipped and winning behavior begins (set some random -%way to pick between ties). Then draw a sample for the duration of that -%bout of the behavior. Once bout is over, flip coins again to select new -%behavior and repeat until the session is over. - -%Update 2022-08-24: Fleshing out current plan, for now just get -%distribution of the probability of behaviors occuring and distribution of -%durations for each behavior. Then pull samples randomly according to the -%above plan until the entire session has behaviors. - -%If want to get fancier looks like we can use the matlab HMM toolbox to -%"fit" a markov model (I imagine it will just take the empirical transition -%probabilities) and use hmmgenerate to make a fake data set...on further -%thought maybe should just use hmmgenerate after calculating the -%information from above...maybe try both and see which data set looks -%better. - -%Decide to lead with getting fancier right away. Use hmmgenerate to make -%the fake data set. Looks good for now, can add a different method if -%curious later. - -%% Load in data - -%Parameters for setting Path -is_mac = 0; %For loading the data -is_ron = 1; %For setting path - - - -%Parameters for setting sessions to use and brain regions to look at -S_list = [1]; %List of session to pull; %For now focus on first Amost sessions -BRs = ["TEO", "vlPFC", "all"]; %Channels considered (sets brain region TEO vlPFC or all) - -%Parameters for neural data to be used across sessions -with_NC =1; %0: Noise cluster (NC) is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well-isolated units -temp_resolution = 1; %Temporal resolution of firing rate. 1sec %%Note this will change if we introduce smoothing -smooth_fr = 0; %Toggle to smooth the spike rasters (currently not setting up this code as may require working with ms neural data and then downsampling to get back) - -s = 1; -br = 3; - -if is_ron - - sessions = dir('C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_data'); sessions = sessions(3:end,:); - filePath = ['C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_data\' sessions(s).name]; - savePath = ['C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_results']; - -else - - - %Set session list -home = '~'; % set home directory -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) -savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/partner_vs_subject']; -end - - -channel_flag = BRs(br); - - -[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_Ron(filePath, temp_resolution, channel_flag, is_mac,is_ron, with_NC, isolatedOnly); - -%% Figure out any manipulation that need to be done before/in the function given the data that needs to be passed. - -the_labels = labels; %toggle this to whatever you want generated as fake labels -the_names = behav_categ; %pass in the names for each numerical label - -[sim_behav] = GenSimBehavior(the_labels,the_names, temp_resolution); \ No newline at end of file diff --git a/Behavior/old_code/get_behav_freq_old.R b/Behavior/old_code/get_behav_freq_old.R deleted file mode 100644 index f898750..0000000 --- a/Behavior/old_code/get_behav_freq_old.R +++ /dev/null @@ -1,39 +0,0 @@ -#Get frequency of behaviors for pilot data - -duration_session1 = 7606; -duration_session3 = 7466; -duration_session4 = 9119; -total.dur.s = duration_session1+duration_session3+duration_session4 -total.dur.h = total.dur.s/3600 - -#Load data: -setwd('C:/Users/Camille Testard/Desktop/Grants_Fellowships/R37_Project/pilot_data/all_behavior_sessions') -behavioral_log1 = read.csv('behav_log_restructured_session1.csv') -behavioral_log3 = read.csv('behav_log_restructured_session3.csv') -behavioral_log4 = read.csv('behav_log_restructured_session4.csv') - -#Get columns of interest -behavioral_log1 = behavioral_log1[,c("Behavior","duration.s")] -behavioral_log3 = behavioral_log3[,c("Behavior","duration.s")] -behavioral_log4 = behavioral_log4[,c("Behavior","duration.s")] - -#Combine data -all_logs = rbind(behavioral_log1,behavioral_log3,behavioral_log4) - -#Combine grooming to facilitate -idx = all_logs$Behavior=="Grooming (receiving)" | all_logs$Behavior=="Grooming (giving)" -all_logs$Behavior[idx] = "Grooming" -all_logs$Behavior[all_logs$Behavior=="Asleep"] = "Sleeping" - -#Get frequency table per behavior -behavs = unique(all_logs$Behavior); -behav_freq_table = data.frame(); b=1 -for (b in 1:length(behavs)){ - behav_freq_table[b,'Behavior'] = behavs[b] - idx = which(all_logs$Behavior == behavs[b]) - behav_freq_table[b,'duration/hrs observed'] = sum(all_logs$duration.s[idx])/total.dur.h - behav_freq_table[b,'#occurrence/hrs observed'] = length(idx)/total.dur.h -} - -#Save to .csv -write.csv( behav_freq_table,file=' behav_freq_table.csv',row.names = F) \ No newline at end of file diff --git a/Behavior/old_code/log_GenerateLabelsPerSecond.asv b/Behavior/old_code/log_GenerateLabelsPerSecond.asv deleted file mode 100644 index c1bcd6a..0000000 --- a/Behavior/old_code/log_GenerateLabelsPerSecond.asv +++ /dev/null @@ -1,71 +0,0 @@ -%% Log GenerateLabelsPerSecond -% Format the raw data to have two elements: -% 1. Neural data matrix size [Time (in sec) x #neurons] -% 2. Label vector which describes the behavior at time t [Time (in sec) x 1] -% Camille Testard - Sept. 2021 - -%% Load data -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) -cd(filePath) - -session = filePath(end-9:end); -monkey = filePath(end-14:end-10); - -behavior_log = readtable(['EVENTLOG_restructured_',monkey,session,'.csv']);% Behavioral data -load('Neural_data.mat') % Neural data; array1 is in TEO and array2 is in vlPFC -session_length = size(Unit_rasters,2); -Spike_count_raster = Unit_rasters'; - -%Preprocessing: round times in behavioral log -behavior_log{:,'start_time_round'}=round(behavior_log{:,'start_time'}); -behavior_log{:,'end_time_round'}=round(behavior_log{:,'end_time'}); -behavior_log{:,'duration_round'}=behavior_log{:,'end_time_round'}-behavior_log{:,'start_time_round'}; - -%% Get behavior label vector for each second - -%Create event intervals: -start_times = behavior_log{:,'start_time_round'}; -end_times = behavior_log{:,'end_time_round'}; -Intervals = [start_times end_times]; - -%Create behavior key -behav_categ = unique(behavior_log{:,'Behavior'}); %Get all the unique behaviors -double_behav_set = [find(matches(behav_categ,'Proximity')), find(matches(behav_categ,"RR"))];%, find(matches(behav_categ,"HIS")), find(matches(behav_categ,"HIP"))]; %For behaviors that often co-occur with other behaviors -omv = find(matches(behav_categ,'Other monkeys vocalize')); - -%Create behavior label vector (label every second of the session) -% This cell matrix will have three columns. The first column is the full -% name of the behavior label -labels = cell(session_length,3); %initialize dataframe -for s = 1:session_length %for all secs in a session - % this finds the index of he rows(2) that have x in between - idx = find(s > Intervals(:,1) & s < Intervals(:,2)); %find if this second belong to any interval - %IMPORTANT note: interval exclude boundaries as is. - if ~isempty(idx) %if yes - labels{s,1} = behavior_log{idx,'Behavior'}; %add behavior id (in plain english) - labels{s,2} = find(matches(behav_categ,labels{s,1})); %add behavior number - if length(labels{s,2})>1 %If one behavior co-occurs with proximity or RR - labels{s,4} = 'co-occur'; - labels{s,3} = setdiff(labels{s,2}, double_behav_set); % only consider the other behavior (it that takes precedence over proximity and RR) - else %If only one behavior happens in that sec - labels{s,3} = labels{s,2}; - labels{s,4} = 'single'; - end - if length(labels{s,3})~=1 %If two behaviors are co-occurring which do not include proximity or RR - if any(labels{s,3}==omv) % if one of the behavior includes other monkey vocalize - labels{s,3}=omv; %Keep OMV - else %Otherwise choose the first behavior -% error('More than one behavior simultansouly') -% return - labels{s,3}= labels{s,3}(2); %2nd behavior (HIP/HIS take precedence over aggression) - end - end - else %if not - labels{s,1} = NaN; labels{s,2} = 0; labels{s,3} = 20; %Set behavior category to "NaN" and - end -end - -%length(find(strfind(labels{:,4},'co-occur'))) - - % %% Save variables - save([filePath '/Labels_per_sec.mat'],'labels','behav_categ'); \ No newline at end of file diff --git a/Behavior/old_code/log_GenerateLabelsPerSecond.m b/Behavior/old_code/log_GenerateLabelsPerSecond.m deleted file mode 100644 index d5225e8..0000000 --- a/Behavior/old_code/log_GenerateLabelsPerSecond.m +++ /dev/null @@ -1,72 +0,0 @@ -%% Log GenerateLabelsPerSecond -% Format the raw data to have two elements: -% 1. Neural data matrix size [Time (in sec) x #neurons] -% 2. Label vector which describes the behavior at time t [Time (in sec) x 1] -% Camille Testard - Sept. 2021 - -%% Load data -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) -cd(filePath) - -session = filePath(end-9:end); -monkey = filePath(end-14:end-10); - -behavior_log = readtable(['EVENTLOG_restructured_',monkey,session,'.csv']);% Behavioral data -load('Neural_data.mat') % Neural data; array1 is in TEO and array2 is in vlPFC -session_length = size(Unit_rasters,2); -Spike_count_raster = Unit_rasters'; - -%Preprocessing: round times in behavioral log -behavior_log{:,'start_time_round'}=round(behavior_log{:,'start_time'}); -behavior_log{:,'end_time_round'}=round(behavior_log{:,'end_time'}); -behavior_log{:,'duration_round'}=behavior_log{:,'end_time_round'}-behavior_log{:,'start_time_round'}; - -%% Get behavior label vector for each second - -%Create event intervals: -start_times = behavior_log{:,'start_time_round'}; -end_times = behavior_log{:,'end_time_round'}; -Intervals = [start_times end_times]; - -%Create behavior key -behav_categ = unique(behavior_log{:,'Behavior'}); %Get all the unique behaviors -behav_categ{20}='Rest'; %Add rest as a behavior (no defined behavior ongoing) -double_behav_set = [find(matches(behav_categ,'Proximity')), find(matches(behav_categ,"RR"))];%, find(matches(behav_categ,"HIS")), find(matches(behav_categ,"HIP"))]; %For behaviors that often co-occur with other behaviors -omv = find(matches(behav_categ,'Other monkeys vocalize')); - -%Create behavior label vector (label every second of the session) -% This cell matrix will have three columns. The first column is the full -% name of the behavior label -labels = cell(session_length,3); %initialize dataframe -for s = 1:session_length %for all secs in a session - % this finds the index of he rows(2) that have x in between - idx = find(s > Intervals(:,1) & s < Intervals(:,2)); %find if this second belong to any interval - %IMPORTANT note: interval exclude boundaries as is. - if ~isempty(idx) %if yes - labels{s,1} = behavior_log{idx,'Behavior'}; %add behavior id (in plain english) - labels{s,2} = find(matches(behav_categ,labels{s,1})); %add behavior number - if length(labels{s,2})>1 %If one behavior co-occurs with proximity or RR - labels{s,4} = 'co-occur'; - labels{s,3} = setdiff(labels{s,2}, double_behav_set); % only consider the other behavior (it that takes precedence over proximity and RR) - else %If only one behavior happens in that sec - labels{s,3} = labels{s,2}; - labels{s,4} = 'single'; - end - if length(labels{s,3})~=1 %If two behaviors are co-occurring which do not include proximity or RR - if any(labels{s,3}==omv) % if one of the behavior includes other monkey vocalize - labels{s,3}=omv; %Keep OMV - else %Otherwise choose the first behavior -% error('More than one behavior simultansouly') -% return - labels{s,3}= labels{s,3}(2); %2nd behavior (HIP/HIS take precedence over aggression) - end - end - else %if not - labels{s,1} = NaN; labels{s,2} = 20; labels{s,3} = 20; %Set behavior category to "NaN" and - end -end - -%length(find(strfind(labels{:,4},'co-occur'))) - - % %% Save variables - save([filePath '/Labels_per_sec.mat'],'labels','behav_categ'); \ No newline at end of file diff --git a/Movement analyses/old_code/.DS_Store b/Fig1_Behavioral_analyses/.DS_Store similarity index 75% rename from Movement analyses/old_code/.DS_Store rename to Fig1_Behavioral_analyses/.DS_Store index 5008ddf..71bcebc 100644 Binary files a/Movement analyses/old_code/.DS_Store and b/Fig1_Behavioral_analyses/.DS_Store differ diff --git a/Behavior/.Rhistory b/Fig1_Behavioral_analyses/.Rhistory similarity index 100% rename from Behavior/.Rhistory rename to Fig1_Behavioral_analyses/.Rhistory diff --git a/BehaviorTransition_Sum_allSessions.m b/Fig1_Behavioral_analyses/BehaviorTransition_Sum_allSessions.m similarity index 95% rename from BehaviorTransition_Sum_allSessions.m rename to Fig1_Behavioral_analyses/BehaviorTransition_Sum_allSessions.m index 6041a14..fd1a036 100644 --- a/BehaviorTransition_Sum_allSessions.m +++ b/Fig1_Behavioral_analyses/BehaviorTransition_Sum_allSessions.m @@ -1,6 +1,7 @@ -%% Log_PETH_onset.m -% Produce peri-event time hitograms, aligned to onset of events. +%% BehaviorTransition_Sum_allSessions.m +% Get the number of transitions from one behavior to another. % C. Testard, July 2023 +% Revised by C. Testard Jan 2024. %Set session list is_mac = 1; @@ -20,7 +21,7 @@ channel_flag = "all"; randomsample=0; %subsample neurons to match between brain areas unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units smooth= 1; %smooth the data sigma = 1*temp_resolution;%set the smoothing window size (sigma) @@ -74,7 +75,7 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') diff --git a/Behavior/GenSimBehavior.m b/Fig1_Behavioral_analyses/GenSimBehavior.m similarity index 99% rename from Behavior/GenSimBehavior.m rename to Fig1_Behavioral_analyses/GenSimBehavior.m index 252a84f..78404df 100644 --- a/Behavior/GenSimBehavior.m +++ b/Fig1_Behavioral_analyses/GenSimBehavior.m @@ -9,6 +9,7 @@ % behavior and a zero for all of the rest of the behaviors. Think I'm % just going to do the hmmgenerate first and then implement my other idea % later if need be. +% Created by Ron W. DiTullio %% Decide on method to use Collect needed data use_markov = 1; %Leaving this as a toggle in here since it isn't really something we need constant access to change. diff --git a/Behavior/generate_BehaviorTimeline.R b/Fig1_Behavioral_analyses/generate_BehaviorTimeline.R similarity index 100% rename from Behavior/generate_BehaviorTimeline.R rename to Fig1_Behavioral_analyses/generate_BehaviorTimeline.R diff --git a/Behavior/log_GenerateDataToRes_Behavior_function.m b/Fig1_Behavioral_analyses/log_GenerateDataToRes_Behavior_function.m similarity index 100% rename from Behavior/log_GenerateDataToRes_Behavior_function.m rename to Fig1_Behavioral_analyses/log_GenerateDataToRes_Behavior_function.m diff --git a/Behavior/log_behavior_freq_subject.m b/Fig1_Behavioral_analyses/log_behavior_freq_subject.m similarity index 85% rename from Behavior/log_behavior_freq_subject.m rename to Fig1_Behavioral_analyses/log_behavior_freq_subject.m index 98e173a..55c975c 100644 --- a/Behavior/log_behavior_freq_subject.m +++ b/Fig1_Behavioral_analyses/log_behavior_freq_subject.m @@ -2,6 +2,7 @@ %This script computes the duration and proportion of behaviors across %blocks over all sessions, separated by monkeys. %Testard C. August 2022 +%Revised by C. Testard Jan 2024 %Set session list is_mac = 1; @@ -19,7 +20,7 @@ with_partner =0; %with parameters temp_resolution = 1; %temporal resolution channel_flag = "all"; %Units considered (vlPFC, TEO or all) -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units smooth= 1; % 1: smooth the data; 0: do not smooth sigma = 1;%set the smoothing window size (sigma) @@ -43,19 +44,12 @@ %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end + [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... + unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... + log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - disp('Data Loaded') + disp('Data Loaded') %Get behaviors unq_behavior_labels = cell2mat({labels{:,3}}'); diff --git a/Behavior/log_transtion_prob.m b/Fig1_Behavioral_analyses/log_transtion_prob.m similarity index 88% rename from Behavior/log_transtion_prob.m rename to Fig1_Behavioral_analyses/log_transtion_prob.m index fc36c36..9ec6290 100644 --- a/Behavior/log_transtion_prob.m +++ b/Fig1_Behavioral_analyses/log_transtion_prob.m @@ -1,7 +1,8 @@ %% Log_transition_prob %This script finds behavioral transtions, computes a transition matrix %and plots a transition probability graph. -%Testard C. Feb 2022 +%Created by Testard C. Feb 2022 +%Revised by C. Testard Jan 2024 is_mac = 1; if is_mac @@ -9,7 +10,7 @@ else home ='C:/Users/GENERAL'; end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) +cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) % go to data directory sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); %Set parameters @@ -17,14 +18,14 @@ channel_flag = "all"; randomsample=0; %subsample neurons to match between brain areas unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster smooth= 1; %smooth the data sigma = 1;%set the smoothing window size (sigma) only_beh_states =0; %1:only consider behavioral states (with extended duration). % I.e. exclude short point behaviors such as yawning, % vocalization and scratch. 0: consider short behaviors -threat_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence +threat_precedence=0; % 0: aggression takes precedence; 1: Threat to partner and subject states take precedence exclude_sq=1; %Select session range: @@ -42,25 +43,18 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') %Format data Spike_count_raster = Spike_rasters'; - %cooccurrence = cell2mat({labels{:,5}}'); {labels{find(cooccurrence>4),2}}'; length(find(cooccurrence>4))/size(Spike_count_raster,1) behavior_labels_subject = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - %behavior_labels_partner = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner block_labels = cell2mat({labels{:,11}}'); %Extract block info % labels_per_sec = table(behavior_labels_subject, behavior_labels_partner, block_labels); % writetable(labels_per_sec, 'Labels_per_sec.csv') % writematrix(behav_categ,'behav_categ.csv') - - %Consider squeeze partner to be threat to partner -% behavior_labels_subject(behavior_labels_subject==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); -% behavior_labels_subject(behavior_labels_subject==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - %Proximity & behaviors from other individuals set to rest behavior_labels_subject_select = behavior_labels_subject;%(behavior_labels_subject~=find(behav_categ=="Rest")); %remove "rest" behavior_labels_subject_select(behavior_labels_subject_select==find(behav_categ=="Proximity"))= find(behav_categ=="Rest"); %remove "proximity" @@ -80,7 +74,6 @@ %Get transitions x=behavior_labels_subject_select(1:end-1); y=behavior_labels_subject_select(2:end); - %shift_labels= [sscanf(sprintf('%d%d,',[x.';y.']),'%d,')]; %This line concatenates the numeric label from the PRECEEDING state with the FOLLOWING state as a string then reformats them as numbers for i =1:length(x) % shift_labels(i,:) = string(strcat(num2str(x(i)),'.',num2str(y(i)))); end diff --git a/Single neuron/.DS_Store b/Fig2_Single_Neuron_analyses/.DS_Store similarity index 100% rename from Single neuron/.DS_Store rename to Fig2_Single_Neuron_analyses/.DS_Store diff --git a/Single neuron/log_ForagingBehavior.m b/Fig2_Single_Neuron_analyses/log_CheckAreaDiff.m similarity index 93% rename from Single neuron/log_ForagingBehavior.m rename to Fig2_Single_Neuron_analyses/log_CheckAreaDiff.m index 91bb329..02fdb59 100644 --- a/Single neuron/log_ForagingBehavior.m +++ b/Fig2_Single_Neuron_analyses/log_CheckAreaDiff.m @@ -1,7 +1,10 @@ -%% Log_SingleNeuronTuning_zscore_batch +%% Log_CheckAreaDiff.m % This script computes the mean z-scored firing rate of individual neuron under different -% behavioral conditions. +% behavioral conditions and measures the inter-area difference. +% This script was primarily written to measure how consistent was the observed inter-area +% during foraging behavior. % C. Testard July 2022 +% Revised by C. Testard August 2024 %Set session list is_mac = 1; @@ -21,7 +24,7 @@ with_partner = 0; temp_resolution = 1; %Temporal resolution of firing rate. 1: 1sec; 10:100msec; 0.1: 10sec channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1; %0: MU is excluded; 1:MU is included; 2:ONLY multi-unit cluster isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units min_occurrence =30*temp_resolution; cohend_cutoff=0.3; p_cutoff=0.01;%Set thresholds @@ -63,7 +66,7 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); session_length = size(Spike_rasters,2); % get session length diff --git a/Single neuron/log_LifetimeSparseness_batch.m b/Fig2_Single_Neuron_analyses/log_LifetimeSparseness_batch.m similarity index 95% rename from Single neuron/log_LifetimeSparseness_batch.m rename to Fig2_Single_Neuron_analyses/log_LifetimeSparseness_batch.m index be42262..0ebdebc 100644 --- a/Single neuron/log_LifetimeSparseness_batch.m +++ b/Fig2_Single_Neuron_analyses/log_LifetimeSparseness_batch.m @@ -14,8 +14,11 @@ end cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); + +%Select session range: session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; +session_range = session_range_no_partner; +a_sessions = 1:6; h_sessions = [11:13,15:16,18]; %Set parameters plot_toggle = 0; %0: suppress plotting; 1:plot @@ -23,7 +26,7 @@ with_partner = 0; %need partner info? 0: No; 1:yes temp_resolution = 10; %Temporal resolution of firing rate. 1: 1sec; 10:100msec; 0.1: 10sec channel_flag = "all"; %Channels considered. vlPFC, TEO or all -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units min_occurrence =30*temp_resolution; %Minimum number of occurrences in the session needed to be considered for this analysis. cohend_cutoff=0.3; p_cutoff=0.001;%Set "significance" thresholds @@ -44,14 +47,7 @@ % num_selective_behav_per_neuron=cell(1,max(session_range_no_partner)); % n_per_behav = nan(max(session_range_no_partner),n_behav); % -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end + s=1; for s =session_range %1:length(sessions) @@ -62,17 +58,12 @@ %% Load data - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma,threat_precedence, exclude_sq); - end + + % Get data with specified temporal resolution and channels + [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... + unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... + log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') diff --git a/Single neuron/log_LifetimeSparseness_subsample_batch.m b/Fig2_Single_Neuron_analyses/log_LifetimeSparseness_subsample_ExtendedDataFig2c.m similarity index 97% rename from Single neuron/log_LifetimeSparseness_subsample_batch.m rename to Fig2_Single_Neuron_analyses/log_LifetimeSparseness_subsample_ExtendedDataFig2c.m index 6d7fc73..a2db8d7 100644 --- a/Single neuron/log_LifetimeSparseness_subsample_batch.m +++ b/Fig2_Single_Neuron_analyses/log_LifetimeSparseness_subsample_ExtendedDataFig2c.m @@ -1,8 +1,11 @@ -%% Log_LifetimeSparseness_batch +%% Log_LifetimeSparseness_subsample_ExtendedDataFig2c % This script computes firing rate of individual neuron under different % behavioral conditions. Then, it computes a cohen's d (or effect size) % difference between the distribution of firing rates during behavior X -% with a baseline firing rate (during rest). +% with a baseline firing rate (during rest). To account for the effect of +% amount of data (number of sample points) differing across behaviors, +% here we sub-sample the number of sample points to be equal across all +% behaviors. THis script was used to generate Extended Data Fig 2c. % Jan 2023, C. Testard %Set session list @@ -14,8 +17,10 @@ end cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; + +%Select session range: +session_range=[1:6,11:13,15:16,18]; +a_sessions = 1:6; h_sessions = [11:13,15:16,18]; %Set parameters plot_toggle = 0; %0: suppress plotting; 1:plot @@ -23,7 +28,7 @@ with_partner = 0; %need partner info? 0: No; 1:yes temp_resolution = 10; %Temporal resolution of firing rate. 1: 1sec; 10:100msec; 0.1: 10sec channel_flag = "all"; %Channels considered. vlPFC, TEO or all -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1; isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units min_occurrence =50*temp_resolution; %Minimum number of occurrences in the session needed to be considered for this analysis. cohend_cutoff=0.3; p_cutoff=0.01;%Set "significance" thresholds @@ -44,14 +49,6 @@ % num_selective_behav_per_neuron=cell(1,max(session_range_no_partner)); % n_per_behav = nan(max(session_range_no_partner),n_behav); % -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end s=1; for s =session_range %1:length(sessions) @@ -65,7 +62,7 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') diff --git a/Single neuron/log_PSTH_offset.m b/Fig2_Single_Neuron_analyses/log_PSTH_offset_Fig2c.m similarity index 100% rename from Single neuron/log_PSTH_offset.m rename to Fig2_Single_Neuron_analyses/log_PSTH_offset_Fig2c.m diff --git a/Single neuron/log_PSTH_onset.m b/Fig2_Single_Neuron_analyses/log_PSTH_onset_Fig2c.m similarity index 100% rename from Single neuron/log_PSTH_onset.m rename to Fig2_Single_Neuron_analyses/log_PSTH_onset_Fig2c.m diff --git a/Single neuron/log_SelectivityAlignedToEvent.m b/Fig2_Single_Neuron_analyses/log_SelectivityAlignedToEventOffset.m similarity index 96% rename from Single neuron/log_SelectivityAlignedToEvent.m rename to Fig2_Single_Neuron_analyses/log_SelectivityAlignedToEventOffset.m index f5a2d0c..2615469 100644 --- a/Single neuron/log_SelectivityAlignedToEvent.m +++ b/Fig2_Single_Neuron_analyses/log_SelectivityAlignedToEventOffset.m @@ -1,5 +1,5 @@ -%% Log_PETH.m -% Produce peri-event time hitograms. +%% Log_SelectivityAlignedToEventOffset.m +% Produce peri-event time hitograms, aligned to event offsets. % C. Testard, July 2023 %Set session list @@ -11,8 +11,8 @@ end cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range=[1:6,11:13,15:16]; -a_sessions = 1:6; h_sessions = [11:13,15:16]; +session_range=[1:6,11:13,15:16,18]; +a_sessions = 1:6; h_sessions = [11:13,15:16,18]; %Set parameters with_partner =0; @@ -20,7 +20,7 @@ channel_flag = "all"; randomsample=0; %subsample neurons to match between brain areas unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units smooth= 1; %smooth the data sigma = 1*temp_resolution;%set the smoothing window size (sigma) @@ -74,7 +74,7 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') diff --git a/Single neuron/log_SelectivityAlignedToEventOnset.m b/Fig2_Single_Neuron_analyses/log_SelectivityAlignedToEventOnset_Fig2d.m similarity index 94% rename from Single neuron/log_SelectivityAlignedToEventOnset.m rename to Fig2_Single_Neuron_analyses/log_SelectivityAlignedToEventOnset_Fig2d.m index 7ef5c38..2d7d987 100644 --- a/Single neuron/log_SelectivityAlignedToEventOnset.m +++ b/Fig2_Single_Neuron_analyses/log_SelectivityAlignedToEventOnset_Fig2d.m @@ -1,5 +1,6 @@ -%% Log_PETH.m -% Produce peri-event time hitograms. +%% Log_SelectivityAlignedToEventOnset_Fig2D.m +% Measure the behavior transition index (neural response index) for each unit +% aligned to event ONSET (Fig 2D). % C. Testard, July 2023 %Set session list @@ -20,7 +21,7 @@ channel_flag = "all"; randomsample=0; %subsample neurons to match between brain areas unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units smooth= 1; %smooth the data sigma = 1*temp_resolution;%set the smoothing window size (sigma) @@ -74,7 +75,7 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') @@ -155,9 +156,9 @@ end for i=1:length(beh_idx) - offset = behavior_log_adj{beh_idx(i),'start_time_round'}; - firing_rate(i,:) = Spike_rasters([offset-time_pre:offset, offset+1:offset+time_post],n); - behavior(i,:)=behavior_labels([offset-time_pre:offset, offset+1:offset+time_post]); + onset = behavior_log_adj{beh_idx(i),'start_time_round'}; + firing_rate(i,:) = Spike_rasters([onset-time_pre:onset, onset+1:onset+time_post],n); + behavior(i,:)=behavior_labels([onset-time_pre:onset, onset+1:onset+time_post]); end diff --git a/Single neuron/log_SingleNeuronTuning_Cohensd_batch.m b/Fig2_Single_Neuron_analyses/log_SingleNeuronTuning_Cohensd_Fig2f_Fig2h.m similarity index 96% rename from Single neuron/log_SingleNeuronTuning_Cohensd_batch.m rename to Fig2_Single_Neuron_analyses/log_SingleNeuronTuning_Cohensd_Fig2f_Fig2h.m index f1cb064..3c41372 100644 --- a/Single neuron/log_SingleNeuronTuning_Cohensd_batch.m +++ b/Fig2_Single_Neuron_analyses/log_SingleNeuronTuning_Cohensd_Fig2f_Fig2h.m @@ -1,9 +1,14 @@ -%% Log_SingleNeuronTuning_Cohensd_batch +%% Log_SingleNeuronTuning_Cohensd_Fig2F_Fig2H % This script computes firing rate of individual neuron under different % behavioral conditions. Then, it computes a cohen's d (or effect size) % difference between the distribution of firing rates during behavior X -% with a baseline firing rate (during rest). +% with a baseline firing rate (during rest). This results in a +% "neuro-ethogram", Fig 2F. This script also output a number of other +% descriptive plots on the selectivity of our population of single +% neurons, including the number of behaviors a unit is modulated be +% relative to rest (Fig 2H). % May 2022, C. Testard +% Revised by C. Testard Jan 2024 %Set session list is_mac = 1; @@ -14,17 +19,16 @@ end cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; +session_range=[1:6,11:13,15:16,18]; +a_sessions = 1:6; h_sessions = [11:13,15:16,18]; %Set parameters plot_toggle = 0; %0: suppress plotting; 1:plot select_behav=0; %If only plot heatmap for desired behavior -with_partner = 0; %need partner info? 0: No; 1:yes temp_resolution = 1; %Temporal resolution of firing rate. 1: 1sec; 10:100msec; 0.1: 10sec channel_flag = "all"; %Channels considered. vlPFC, TEO or all -with_NC =0; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=1; %Only consider isolated units. 0=all units; 1=only well isolated units +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster +isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units min_occurrence =30; %Minimum number of occurrences in the session needed to be considered for this analysis. cohend_cutoff=0.3; p_cutoff=0.01;%Set "significance" thresholds smooth= 1; % 1: smooth the data; 0: do not smooth @@ -43,14 +47,6 @@ num_selective_behav_per_neuron=cell(1,length(sessions)); n_per_behav = nan(length(sessions),n_behav); -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end s=1; for s =session_range %1:length(sessions) @@ -64,7 +60,7 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') @@ -360,6 +356,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Plot median effect size per behavior across all sessions, separated by monkey + figure; set(gcf,'Position',[150 250 1000 800]); subplot(2,1,1);hold on; [~, idx_sort]=sort(nanmean(median_cohend_per_behav)); @@ -400,7 +397,7 @@ %saveas(gcf, [savePath '/Median_effect_size_per_behavior.png']); pause(2); close all %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot proportion of selective units per behavior across all sessions, separated by monkey +%Plot proportion of modulated units per behavior across all sessions, separated by monkey figure; set(gcf,'Position',[150 250 1000 800]); subplot(2,1,1);hold on; [~, idx_sort]=sort(nanmean(prop_selective_per_behav)); @@ -434,22 +431,25 @@ %saveas(gcf, [savePath '/Proportion_selective_units_per_behavior.png']); pause(2); close all %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot number of behaviors a single neuron is selective for across all sessions, separated by monkey +%Plot number of behaviors a single neuron is modulated by (relative to rest) +% across all sessions +%Across both brain areas figure histogram(vertcat(num_selective_behav_per_neuron{:})) length(find(vertcat(num_selective_behav_per_neuron{:})~=0))/sum(n_neurons) length(find(vertcat(num_selective_behav_per_neuron{:})>1))/sum(n_neurons) -xlabel('Number of behaviors a given unit is selective for') +xlabel('Number of behaviors a given unit is modulated by relative to rest') ylabel('Neuron count') set(gca,'FontSize',15); saveas(gcf, [savePath '/Number_selective_behavior_per_unit.pdf']); +%Separated by brain area figure; hold on histogram(vertcat(num_selective_behav_per_neuron_TEO{:})) histogram(vertcat(num_selective_behav_per_neuron_vlPFC{:})) legend({'TEO','vlPFC'}) -xlabel('Number of behaviors a given unit is selective for') +xlabel('Number of behaviors a given unit is modulated by relative to rest') ylabel('Neuron count') set(gca,'FontSize',15); % saveas(gcf, [savePath '/Number_selective_behavior_per_unit_byArea.pdf']); diff --git a/Single neuron/log_SingleNeuronTuning_zscore_batch.m b/Fig2_Single_Neuron_analyses/log_SingleNeuronTuning_zscore_Fig2g.m similarity index 89% rename from Single neuron/log_SingleNeuronTuning_zscore_batch.m rename to Fig2_Single_Neuron_analyses/log_SingleNeuronTuning_zscore_Fig2g.m index a1bd90e..19811cf 100644 --- a/Single neuron/log_SingleNeuronTuning_zscore_batch.m +++ b/Fig2_Single_Neuron_analyses/log_SingleNeuronTuning_zscore_Fig2g.m @@ -1,7 +1,9 @@ -%% Log_SingleNeuronTuning_zscore_batch +%% Log_SingleNeuronTuning_zscore_Fig2G % This script computes the mean z-scored firing rate of individual neuron under different -% behavioral conditions. +% behavioral conditions. The final output is the mean Z-score firing rate +% across all behaviors and all neurons recorded. % C. Testard July 2022 +% Revised by C. Testard Jan 2024 %Set session list is_mac = 1; @@ -20,7 +22,7 @@ with_partner = 0; temp_resolution = 1; %Temporal resolution of firing rate. 1: 1sec; 10:100msec; 0.1: 10sec channel_flag = "vlPFC"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units min_occurrence =30*temp_resolution; cohend_cutoff=0.3; p_cutoff=0.01;%Set thresholds @@ -57,8 +59,9 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + disp('Data Loaded') session_length = size(Spike_rasters,2); % get session length @@ -129,14 +132,14 @@ caxis_upper = 2;%max(max(mean_beh)); caxis_lower = -2;%min(min(mean_beh)); cmap=flipud(cbrewer('div','RdBu', length(caxis_lower:0.01:caxis_upper))); -% % % figure; %set(gcf,'Position',[150 250 1000 500]); -% % % [nanrow nancol_a]=find(~isnan(meanBehav_sorted)); nancol_a = unique(nancol_a); -% % % order_units = [find(strcmp(brain_label,"TEO")), find(strcmp(brain_label,"vlPFC"))]; -% % % %unit_lim = length(find(strcmp(brain_label,"TEO")))+1; yline(unit_lim); %plot the -% % % %brain area limit -% % % hp=heatmap(meanBehav_sorted(order_units,nancol_a), 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',jet); hp.XDisplayLabels = AxesLabels_sorted_a(nancol_a); caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Zscore mean firing per behavior']) -% % % ax = gca; -% % % ax.FontSize = 14; +% figure; %set(gcf,'Position',[150 250 1000 500]); +% [nanrow nancol_a]=find(~isnan(meanBehav_sorted)); nancol_a = unique(nancol_a); +% order_units = [find(strcmp(brain_label,"TEO")), find(strcmp(brain_label,"vlPFC"))]; +% %unit_lim = length(find(strcmp(brain_label,"TEO")))+1; yline(unit_lim); %plot the +% %brain area limit +% hp=heatmap(meanBehav_sorted(order_units,nancol_a), 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',jet); hp.XDisplayLabels = AxesLabels_sorted_a(nancol_a); caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Zscore mean firing per behavior']) +% ax = gca; +% ax.FontSize = 14; % % % saveas(gcf, [savePath '/Zscore_heatmap_sorted.pdf']); close all @@ -160,7 +163,9 @@ % cd(['~/Dropbox (Penn)/Datalogger/Results/All_sessions/SingleUnit_results/']); % load('Zscored_responses_allSessions.mat') -%Plot massive heatmap +%Plot heatmap of z-score firing rate across behaviors across all sessions. +%Note: blanks are caused by not enough sample points for that behavior at +%that session. figure; set(gcf,'Position',[150 250 1500 400]); subplot(1,2,1) [~, sortIdx]= sort(nanmean(all_sessions_data_a,1)); @@ -171,7 +176,6 @@ ax.FontSize = 14; subplot(1,2,2) -all_sessions_data_h = cell2mat(save_meanBehav(h_sessions)'); [~, sortIdx]= sort(nanmean(all_sessions_data_h,1)); all_sessions_data_sorted_h = all_sessions_data_h(:,sortIdx); AxesLabels_sorted_h = AxesLabels(sortIdx); [nanrow nancol_h]=find(~isnan(all_sessions_data_sorted_h)); nancol_h = unique(nancol_h); @@ -196,7 +200,7 @@ ax.FontSize = 14; %saveas(gcf, [savePath '/Zscore_heatmap_binarized.pdf']); close all -%Plot violins +%Plot violins representing the distribution across neurons (Fig 2G) figure; set(gcf,'Position',[150 250 1500 400]); subplot(1,2,1) violin(all_sessions_data_sorted_a(:,nancol_a)) diff --git a/Single neuron/log_Visualize_SingleUnit_Selectivity.m b/Fig2_Single_Neuron_analyses/log_Visualize_SingleUnit_Selectivity_Fig2b_Fig2e.m similarity index 93% rename from Single neuron/log_Visualize_SingleUnit_Selectivity.m rename to Fig2_Single_Neuron_analyses/log_Visualize_SingleUnit_Selectivity_Fig2b_Fig2e.m index 20c25a9..4c5db2f 100644 --- a/Single neuron/log_Visualize_SingleUnit_Selectivity.m +++ b/Fig2_Single_Neuron_analyses/log_Visualize_SingleUnit_Selectivity_Fig2b_Fig2e.m @@ -1,10 +1,11 @@ -%% Log_Visualize_SingleUnit_Selectivity -% This script produces two plots for a given neuron to illustrate its -% response profile. First, it computes firing rate of individual neuron -% under different behavioral conditions. -% 1st plot: a proportional histogram of firing rates for a given behavior, compared to rest. -% 2nd plot: smooth spike trace with labeled time points according to a set of behaviors. +%% Log_Visualize_SingleUnit_Selectivity_Fig2b-e +% For each session, this script produces a heatmap to visualize the activity of the entire population +% of single neurons in a sesstion (ordered using rastermap, Fig 2B) as well as +% two plots for a given neuron to illustrate its response profile. +% 1st plot: a proportional histogram of firing rates for a given behavior, compared to rest (Fig 2e). +% 2nd plot: smooth spike trace with labeled time points according to a set of behaviors (Fig 2b). % C. Testard, July 2022 +% Revised by C. Testard Jan. 2024 %Set session list is_mac = 1; @@ -15,16 +16,15 @@ end cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; +session_range=[1:6,11:13,15:16,18]; +a_sessions = 1:6; h_sessions = [11:13,15:16,18]; %Set parameters -with_partner =0; temp = 1; temp_resolution = 1; channel_flag = "all"; randomsample=0; %subsample neurons to match between brain areas unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units smooth= 1; %smooth the data sigma = 10*temp_resolution;%set the smoothing window size (sigma) @@ -32,16 +32,6 @@ exclude_sq=1; plot_toggle=1; -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - - s=1; for s =session_range %1:length(sessions) @@ -55,7 +45,7 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') @@ -124,7 +114,7 @@ sorted_activity_all = activity_all(isort1_all,:); sorted_activity_byArea = [activity_vlpfc(isort1_vlpfc,:); activity_teo(isort1_teo,:)]; - if plot_toggle %plot rastermap + if plot_toggle %plot heatmap for activity of neural population in a given session % figure; imagesc(amap_all); xline(block_times.start_time(2)*temp_resolution,'LineWidth',2); xline(block_times.start_time(3)*temp_resolution,'LineWidth',2); colorbar; caxis([-2 2]) % figure; imagesc(sorted_activity_all); xline(block_times.start_time(2)*temp_resolution,'LineWidth',2); xline(block_times.start_time(3)*temp_resolution,'LineWidth',2); colorbar; caxis([-2 2]) % figure; imagesc(activity_all); xline(block_times.start_time(2)*temp_resolution,'LineWidth',2); xline(block_times.start_time(3)*temp_resolution,'LineWidth',2); colorbar; caxis([-2 2]) @@ -164,10 +154,10 @@ - %% Plot firing rate profile across macaques behavioral repertoire + %% Plot firing rate profile of single neurons across macaques behavioral repertoire %Set parameters - unqLabels = [7,8];%1:max(behavior_labels)-1; %Get unique behavior labels (exclude rest) + unqLabels = 1:max(behavior_labels)-1; %Get unique behavior labels (exclude rest) n_neurons(s) = size(Spike_rasters,1); %Get number of neurons n_behav = length(unqLabels); %Get number of unique behavior labels diff --git a/Grooming analyses/.DS_Store b/Fig3_Linear_decoding_analyses/.DS_Store similarity index 96% rename from Grooming analyses/.DS_Store rename to Fig3_Linear_decoding_analyses/.DS_Store index 379300d..96d5d11 100644 Binary files a/Grooming analyses/.DS_Store and b/Fig3_Linear_decoding_analyses/.DS_Store differ diff --git a/Linear decoding/log_SVM_SocialContext_NeighborID.m b/Fig3_Linear_decoding_analyses/log_SVM_SocialContext_NeighborID_Fig3f.m similarity index 87% rename from Linear decoding/log_SVM_SocialContext_NeighborID.m rename to Fig3_Linear_decoding_analyses/log_SVM_SocialContext_NeighborID_Fig3f.m index 4f5cc6d..a255d93 100644 --- a/Linear decoding/log_SVM_SocialContext_NeighborID.m +++ b/Fig3_Linear_decoding_analyses/log_SVM_SocialContext_NeighborID_Fig3f.m @@ -1,9 +1,8 @@ -%% Log_SVM_SocialContext_batch +%% Log_SVM_SocialContext_NeighborID_Fig3f % Run a linear decoder on a the neural activity to decode the identity of -% neighboring monkey. -% Batch version +% neighboring monkey during a given behavior. % March 2022 C. Testard - +% Revised by C. Testard Jan 2024. %Set session list is_mac = 1; @@ -14,17 +13,15 @@ end cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:3,11:13]; - +session_range=[1:6,11:13,15:16,18]; +a_sessions = 1:6; h_sessions = [11:13,15:16,18]; %Set parameters -with_partner =0; temp_resolution = 1; channel_flag = "all"; randomsample=0; %subsample neurons to match between brain areas unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units num_iter = 100;%Number of SVM iterations smooth= 1; % 1: smooth the data; 0: do not smooth @@ -34,15 +31,6 @@ min_occurrence = 15; agg_precedence = 1; -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - s=1; for s =session_range %1:length(sessions) @@ -54,24 +42,17 @@ for channel_flag = ["vlPFC", "TEO", "all"] %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end + [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... + unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... + log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') Spike_count_raster = Spike_rasters'; behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - block_labels = cell2mat({labels{:,12}}'); - paired_labels = cell2mat({labels{:,13}}'); + block_labels = cell2mat({labels{:,12}}'); %Extract block label + paired_labels = cell2mat({labels{:,13}}'); %Extract whether paired or alone info Spike_count_raster = Spike_count_raster(paired_labels==1,:); behavior_labels = behavior_labels(paired_labels==1); diff --git a/Linear decoding/log_SVM_SocialContext_PairedAlone.m b/Fig3_Linear_decoding_analyses/log_SVM_SocialContext_PairedAlone_Fig3f.m similarity index 86% rename from Linear decoding/log_SVM_SocialContext_PairedAlone.m rename to Fig3_Linear_decoding_analyses/log_SVM_SocialContext_PairedAlone_Fig3f.m index a310284..25445b6 100644 --- a/Linear decoding/log_SVM_SocialContext_PairedAlone.m +++ b/Fig3_Linear_decoding_analyses/log_SVM_SocialContext_PairedAlone_Fig3f.m @@ -1,7 +1,7 @@ -%% Log_SVM_SocialContext_batch -% Run a linear decoder on a the neural activity to decode whether the animal is paired or alone. -% Batch version +%% Log_SVM_SocialContext_PairedAlone_Fig3f +% Run a linear decoder on a the neural activity to decode whether the animal is paired or alone during a given behavior. % March 2022 C. Testard +% Revised by C. Testard Jan 2024. %Set session list @@ -13,17 +13,15 @@ end cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:6,11:13,15:16]; - +session_range=[1:6,11:13,15:16,18]; +a_sessions = 1:6; h_sessions = [11:13,15:16,18]; %Set parameters -with_partner =0; temp_resolution = 1; channel_flag = "all"; randomsample=0; %subsample neurons to match between brain areas unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units num_iter = 100;%Number of SVM iterations smooth= 1; % 1: smooth the data; 0: do not smooth @@ -33,15 +31,6 @@ min_occurrence = 15; threat_precedence = 1; -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - s=1; for s =session_range %1:length(sessions) @@ -53,17 +42,10 @@ for channel_flag = ["vlPFC", "TEO", "all"] %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence); - end + [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... + unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... + log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') diff --git a/Linear decoding/log_SVM_subject_batch.m b/Fig3_Linear_decoding_analyses/log_SVM_SubjectBehavior_Fig3e.m similarity index 95% rename from Linear decoding/log_SVM_subject_batch.m rename to Fig3_Linear_decoding_analyses/log_SVM_SubjectBehavior_Fig3e.m index e7f3711..b85540e 100644 --- a/Linear decoding/log_SVM_subject_batch.m +++ b/Fig3_Linear_decoding_analyses/log_SVM_SubjectBehavior_Fig3e.m @@ -1,8 +1,9 @@ -%% Log_SVM_subject_batch +%% Log_SVM_SubjectBehavior_Fig3e % Run a linear decoder on the neural activity for the subject's behavior -% (only including behaviors with a minimum # occurrence) -% Batch version of script +% (only including behaviors with a minimum # occurrence). THis will result +% in Fig 3e. % March 2022 - Camille Testard +% Revised by C. Testard Jan. 2024 %Set session list is_mac = 1; @@ -13,19 +14,17 @@ end cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - +session_range=[1:6,11:13,15:16,18]; +a_sessions = 1:6; h_sessions = [11:13,15:16,18]; %Split sessions by monkey %Set parameters -with_partner =0; temp = 1; temp_resolution = 1; channel_flag = "all"; randomsample=0; %subsample neurons to match between brain areas unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 10;%Number of SVM iterations +num_iter = 100;%Number of SVM iterations smooth= 1; % 1: smooth the data; 0: do not smooth sigma = 1*temp_resolution;%set the smoothing window size (sigma) null=0;%Set whether we want the null @@ -33,15 +32,6 @@ threat_precedence =0; exclude_sq=1; -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - s=1; for s =session_range %1:length(sessions) @@ -57,7 +47,7 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') @@ -279,7 +269,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy +% Bar plot decoding accuracy (Fig 3e) figure; hold on data = cell2mat(mean_hitrate'); diff --git a/Linear decoding/log_SVM_basic_function.m b/Fig3_Linear_decoding_analyses/log_SVM_basic_function.m similarity index 100% rename from Linear decoding/log_SVM_basic_function.m rename to Fig3_Linear_decoding_analyses/log_SVM_basic_function.m diff --git a/Linear decoding/log_plot_decoding_context.m b/Fig3_Linear_decoding_analyses/log_plot_decoding_context_Fig3f.m similarity index 97% rename from Linear decoding/log_plot_decoding_context.m rename to Fig3_Linear_decoding_analyses/log_plot_decoding_context_Fig3f.m index 823f806..55b994f 100644 --- a/Linear decoding/log_plot_decoding_context.m +++ b/Fig3_Linear_decoding_analyses/log_plot_decoding_context_Fig3f.m @@ -1,5 +1,5 @@ -%% log_plot_decoding_context -%Script which plots Fig 3 Panel H. +%% log_plot_decoding_context_Fig3f +%Script which plots Fig 3f. % poolig results from decoding paired vs. alone and neighbor ID % C. Testard Nov. 2022 diff --git a/Linear decoding/.DS_Store b/Fig3_Movement_analyses/.DS_Store similarity index 91% rename from Linear decoding/.DS_Store rename to Fig3_Movement_analyses/.DS_Store index 92c81a5..afe7744 100644 Binary files a/Linear decoding/.DS_Store and b/Fig3_Movement_analyses/.DS_Store differ diff --git a/Ridge regression/.DS_Store b/Fig3_Movement_analyses/Ridge regression/.DS_Store similarity index 100% rename from Ridge regression/.DS_Store rename to Fig3_Movement_analyses/Ridge regression/.DS_Store diff --git a/Ridge regression/log_RidgeRegression_batch.m b/Fig3_Movement_analyses/Ridge regression/log_RidgeRegression_Fig3i.m similarity index 94% rename from Ridge regression/log_RidgeRegression_batch.m rename to Fig3_Movement_analyses/Ridge regression/log_RidgeRegression_Fig3i.m index 9914b55..d687b6a 100644 --- a/Ridge regression/log_RidgeRegression_batch.m +++ b/Fig3_Movement_analyses/Ridge regression/log_RidgeRegression_Fig3i.m @@ -1,5 +1,5 @@ -%% Log_RidgeRegression -% Run a ridge regression (code from Musall et. al) +%% Log_RidgeRegression_Fig3i +% Run a ridge regression to find how much variance can be explained by movement vs. behavior (code adapted from Musall et. al 2019) % Testard C. Feb 2022 %Set session list @@ -19,7 +19,7 @@ channel_flag = 'TEO'; randomsample=0; %subsample neurons to match between brain areas unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units smooth= 1; %smooth the data sigma = 1*temp_resolution; %set the smoothing window size (sigma) @@ -56,26 +56,9 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); cd(filePath) - % - % %2022-12-13: trying totally random data instead. That runs. Now - % %trying non-random behavior data. This works as well. So it is - % %something with Vc that is causing the problem/the neural data in - % %general. - % Spike_rasters = poissrnd(.0001,size(Spike_rasters)); - % Spike_rasters(Spike_rasters<0) = 0; - - % %Did some simulation work, now setting threshold that cells must be active - % %for at least 0.1% (i.e. 0.001 * session length) of session to be included - % %and seeing if this fixes the issue. - % criterion = 0.001; - % low_firing = ((sum(Spike_rasters'>0)/length(Spike_rasters))0,:) = []; - %This doesn't seem to fix the issue. Might be dropping below activity - %after removing nans, so going to check Vc (neural activity variable) - %before using model %Load DLC dlc = readtable(['mvmt_data_c' num2str(c_cutoff) '.csv']);% Load DLC key point data @@ -181,13 +164,6 @@ subject_behav_reg(idx,b) = 1; end - % %Create partner behavior regressors: - % partner_behav_reg = zeros(size(Vc{s,chan},1), length(behav_categ)-1); %initialize - % for b = 1:length(behav_categ)-1 - % idx = find(behavior_labels_partner == b); - % partner_behav_reg(idx,b) = 1; - % end - %Create block regressor: block_reg = zeros(size(Vc,1), length(unique(block_labels))); %initialize for b = 1:length(unique(block_labels)) diff --git a/Ridge regression/log_analogToDesign.m b/Fig3_Movement_analyses/Ridge regression/log_analogToDesign.m similarity index 100% rename from Ridge regression/log_analogToDesign.m rename to Fig3_Movement_analyses/Ridge regression/log_analogToDesign.m diff --git a/Ridge regression/log_crossValModel.m b/Fig3_Movement_analyses/Ridge regression/log_crossValModel.m similarity index 100% rename from Ridge regression/log_crossValModel.m rename to Fig3_Movement_analyses/Ridge regression/log_crossValModel.m diff --git a/Ridge regression/log_makeDesignMatrix.m b/Fig3_Movement_analyses/Ridge regression/log_makeDesignMatrix.m similarity index 100% rename from Ridge regression/log_makeDesignMatrix.m rename to Fig3_Movement_analyses/Ridge regression/log_makeDesignMatrix.m diff --git a/Ridge regression/log_run_QR.m b/Fig3_Movement_analyses/Ridge regression/log_run_QR.m similarity index 100% rename from Ridge regression/log_run_QR.m rename to Fig3_Movement_analyses/Ridge regression/log_run_QR.m diff --git a/Ridge regression/log_run_QR_decomp.m b/Fig3_Movement_analyses/Ridge regression/log_run_QR_decomp.m similarity index 100% rename from Ridge regression/log_run_QR_decomp.m rename to Fig3_Movement_analyses/Ridge regression/log_run_QR_decomp.m diff --git a/Ridge regression/old_code/log_RidgeRegression.m b/Fig3_Movement_analyses/Ridge regression/old_code/log_RidgeRegression.m similarity index 100% rename from Ridge regression/old_code/log_RidgeRegression.m rename to Fig3_Movement_analyses/Ridge regression/old_code/log_RidgeRegression.m diff --git a/Ridge regression/old_code/log_RidgeRegression_alone.m b/Fig3_Movement_analyses/Ridge regression/old_code/log_RidgeRegression_alone.m similarity index 100% rename from Ridge regression/old_code/log_RidgeRegression_alone.m rename to Fig3_Movement_analyses/Ridge regression/old_code/log_RidgeRegression_alone.m diff --git a/Ridge regression/ridgeMML.m b/Fig3_Movement_analyses/Ridge regression/ridgeMML.m similarity index 100% rename from Ridge regression/ridgeMML.m rename to Fig3_Movement_analyses/Ridge regression/ridgeMML.m diff --git a/Movement analyses/log_SVM_subject_without_movement_batch.m b/Fig3_Movement_analyses/log_SVM_subject_without_movement_Fig3j.m similarity index 99% rename from Movement analyses/log_SVM_subject_without_movement_batch.m rename to Fig3_Movement_analyses/log_SVM_subject_without_movement_Fig3j.m index 1997590..47aff20 100644 --- a/Movement analyses/log_SVM_subject_without_movement_batch.m +++ b/Fig3_Movement_analyses/log_SVM_subject_without_movement_Fig3j.m @@ -1,7 +1,6 @@ -%% Log_SVM_subject_without_movement_batch +%% Log_SVM_subject_without_movement_Fig3j % Run a linear decoder on the neural activity after regressing out movement for the subject's behavior % (only including behaviors with a minimum # occurrence) -% Batch version of script % October 2022 - Camille Testard %Set session list @@ -105,8 +104,7 @@ lbls_final_notCateg = dummyvar(lbls_final); mvmt_final = mvmt(idx_to_keep,:); - %% Run regression - % Use adjusted Rsquared + %% Regress out movement by extracting residuals for unit = 1:size(Y_final,2) %for now one unit at a time. diff --git a/Movement analyses/log_decoding_behavior_from_mvmt_batch.m b/Fig3_Movement_analyses/log_decoding_behavior_from_mvmt.m similarity index 98% rename from Movement analyses/log_decoding_behavior_from_mvmt_batch.m rename to Fig3_Movement_analyses/log_decoding_behavior_from_mvmt.m index eab6c1a..f76b091 100644 --- a/Movement analyses/log_decoding_behavior_from_mvmt_batch.m +++ b/Fig3_Movement_analyses/log_decoding_behavior_from_mvmt.m @@ -1,7 +1,7 @@ -%% Log_SVM_subject_without_movement_batch -% Run a linear decoder on the neural activity after regressing out movement for the subject's behavior +%% Log_decoding_behavior_from_mvmt.m +% Run umap and a linear decoder on movement data alone for the subject's behavior +% Compare to decoder on movement data. % (only including behaviors with a minimum # occurrence) -% Batch version of script % October 2022 - Camille Testard %Set session list diff --git a/Movement analyses/log_plot5F.m b/Fig3_Movement_analyses/log_plot3j.m similarity index 92% rename from Movement analyses/log_plot5F.m rename to Fig3_Movement_analyses/log_plot3j.m index b75a945..04337cb 100644 --- a/Movement analyses/log_plot5F.m +++ b/Fig3_Movement_analyses/log_plot3j.m @@ -1,4 +1,6 @@ -%% log_plot5F.m +%% log_plot3j.m +% This plot combines output from other scripts into a figure. +% C. Testard Oct 2022 cd(['~/Dropbox (Penn)/Datalogger/Results/All_sessions/Mvmt_results/']); load(['SVM_results_mvmtControlled_rawRes_c0.2.mat']) diff --git a/Movement analyses/log_preprocess_video_allTracking.m b/Fig3_Movement_analyses/log_preprocess_video_allTracking.m similarity index 98% rename from Movement analyses/log_preprocess_video_allTracking.m rename to Fig3_Movement_analyses/log_preprocess_video_allTracking.m index c480887..731db17 100644 --- a/Movement analyses/log_preprocess_video_allTracking.m +++ b/Fig3_Movement_analyses/log_preprocess_video_allTracking.m @@ -1,6 +1,5 @@ -%% log_preprocess_video_dlc -%Script which combines movement data from multiple video angles and -%computes movement derivatives. +%% log_preprocess_video_allTracking +%Script which preprocesses and combines movement data from multiple video angles. %C. Testard Jan 2023 %Set session list @@ -14,7 +13,7 @@ sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); %Select session range: -session_range = [3:6,11:13,15:16,18]; +session_range = [3:6,11:13,15:16,18]; %sessions with valid video data prop_missing_data_body = nan(1,max(session_range)); prop_missing_data_logger = nan(1,max(session_range)); prop_missing_by_bp = nan(max(session_range),17); diff --git a/Movement analyses/log_preprocess_video_dlc.m b/Fig3_Movement_analyses/log_preprocess_video_dlc.m similarity index 98% rename from Movement analyses/log_preprocess_video_dlc.m rename to Fig3_Movement_analyses/log_preprocess_video_dlc.m index 7a5478d..60c1ffb 100644 --- a/Movement analyses/log_preprocess_video_dlc.m +++ b/Fig3_Movement_analyses/log_preprocess_video_dlc.m @@ -1,6 +1,5 @@ %% log_preprocess_video_dlc -%Script which combines movement data from multiple video angles and -%computes movement derivatives. +%Script which preprocesses DLC output of top view cameras to extract head location and direction. %C. Testard Oct 2022 %Set session list diff --git a/Neural states/.DS_Store b/Fig3_UMAP_analyses/.DS_Store similarity index 100% rename from Neural states/.DS_Store rename to Fig3_UMAP_analyses/.DS_Store diff --git a/Neural states/log_neural_state_similarity_batch.m b/Fig3_UMAP_analyses/log_neural_state_similarity_Fig5f.m similarity index 93% rename from Neural states/log_neural_state_similarity_batch.m rename to Fig3_UMAP_analyses/log_neural_state_similarity_Fig5f.m index fd5508a..d41d19b 100644 --- a/Neural states/log_neural_state_similarity_batch.m +++ b/Fig3_UMAP_analyses/log_neural_state_similarity_Fig5f.m @@ -11,16 +11,15 @@ end cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; +session_range=[1:6,11:13,15:16,18]; +a_sessions = 1:6; h_sessions = [11:13,15:16,18]; %Set parameters -with_partner =0; -temp_resolution = 1; +temp = 1; temp_resolution = 1; channel_flag = "all"; randomsample=0; %subsample neurons to match between brain areas unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units smooth= 0; % 1: smooth the data; 0: do not smooth sigma = 1*temp_resolution;%set the smoothing window size (sigma) @@ -28,15 +27,6 @@ simplify=1; %lump similar behavioral categories together agg_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - s=1; chan=1; for s =session_range %1:length(sessions) @@ -49,17 +39,10 @@ %channel_flag = "TEO"; %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end + [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... + unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... + log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') @@ -279,7 +262,7 @@ % "Z_behav_umap_lowRes", "Z_behav_pca_lowRes","Z_umap_block","Z_pca_block",... % "Z_umap_block_lowRes","Z_pca_block_lowRes","Z_umap_blockBehav","Z_pca_blockBehav",... % "Z_umap_blockBehav_lowRes","Z_pca_blockBehav_lowRes") -load('State_similarity.mat') +load('State_distances.mat') %Plot distance to threat to subject behav=[5,7,8,18,9]; diff --git a/Neural states/log_umap_state_distances_batch.m b/Fig3_UMAP_analyses/log_umap_state_distances_ExtendedDataFig3c-d.m similarity index 96% rename from Neural states/log_umap_state_distances_batch.m rename to Fig3_UMAP_analyses/log_umap_state_distances_ExtendedDataFig3c-d.m index 1e7af6a..2c4d220 100644 --- a/Neural states/log_umap_state_distances_batch.m +++ b/Fig3_UMAP_analyses/log_umap_state_distances_ExtendedDataFig3c-d.m @@ -1,5 +1,8 @@ -%% log_umap_visualization_batch -% This script applied unsupervised umap on smoothed firing +%% log_umap_state_distances_ExtendedDataFig3c-d +% This script applies unsupervised umap on smoothed firing to obtain neural +% states in a dimensionally reduced space. On that data, we calculated the +% pairwise Euclidean distance between neural states between distinct behaviors and +% social contexts (Extended Data Fig 3c-d) % Ref: Connor Meehan, Jonathan Ebrahimian, Wayne Moore, and Stephen Meehan (2022). Uniform Manifold Approximation and Projection (UMAP) (https://www.mathworks.com/matlabcentral/fileexchange/71902), MATLAB Central File Exchange. %Set session list @@ -11,8 +14,8 @@ end cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:6,11:13,15:16]; +session_range=[1:6,11:13,15:16,18]; +a_sessions = 1:6; h_sessions = [11:13,15:16,18]; %Set parameters with_partner =0; @@ -20,7 +23,7 @@ channel_flag = "all"; randomsample=0; %subsample neurons to match between brain areas unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units smooth= 1; % 1: smooth the data; 0: do not smooth sigma = 1*temp_resolution;%set the smoothing window size (sigma) @@ -50,7 +53,7 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') diff --git a/Neural states/log_umap_visualization_batch.m b/Fig3_UMAP_analyses/log_umap_visualization_Fig3a-d.m similarity index 97% rename from Neural states/log_umap_visualization_batch.m rename to Fig3_UMAP_analyses/log_umap_visualization_Fig3a-d.m index c4dd5ea..ef59f45 100644 --- a/Neural states/log_umap_visualization_batch.m +++ b/Fig3_UMAP_analyses/log_umap_visualization_Fig3a-d.m @@ -1,5 +1,6 @@ -%% log_umap_visualization_batch -% This script applied unsupervised umap on smoothed firing +%% log_umap_visualization_Fig3a-d +% This script applies unsupervised umap on smoothed firing rate +% to visualize neural states in a dimensionally reduced space. % Ref: Connor Meehan, Jonathan Ebrahimian, Wayne Moore, and Stephen Meehan (2022). Uniform Manifold Approximation and Projection (UMAP) (https://www.mathworks.com/matlabcentral/fileexchange/71902), MATLAB Central File Exchange. %Set session list @@ -11,8 +12,7 @@ end cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range = session_range_no_partner; +session_range=[1:6,11:13,15:16,18]; a_sessions = 1:6; h_sessions = [11:13,15:16,18]; %Set parameters @@ -21,7 +21,7 @@ channel_flag = "all"; randomsample=0; %subsample neurons to match between brain areas unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units smooth= 1; % 1: smooth the data; 0: do not smooth sigma = 1*temp_resolution;%set the smoothing window size (sigma) @@ -30,7 +30,6 @@ threat_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence exclude_sq=1; - s=1; for s =session_range %1:length(sessions) @@ -48,7 +47,7 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') diff --git a/Varia/GLM/.DS_Store b/Fig4_Grooming_analyses/.DS_Store similarity index 89% rename from Varia/GLM/.DS_Store rename to Fig4_Grooming_analyses/.DS_Store index 5869c68..1efc613 100644 Binary files a/Varia/GLM/.DS_Store and b/Fig4_Grooming_analyses/.DS_Store differ diff --git a/Single neuron/log_Visualize_SingleUnit_GroomingResponse.m b/Fig4_Grooming_analyses/log_Visualize_SingleUnit_GroomingResponse_Fig4F.m similarity index 83% rename from Single neuron/log_Visualize_SingleUnit_GroomingResponse.m rename to Fig4_Grooming_analyses/log_Visualize_SingleUnit_GroomingResponse_Fig4F.m index 55a924c..bc633f9 100644 --- a/Single neuron/log_Visualize_SingleUnit_GroomingResponse.m +++ b/Fig4_Grooming_analyses/log_Visualize_SingleUnit_GroomingResponse_Fig4F.m @@ -1,10 +1,10 @@ -%% Log_Visualize_SingleUnit_GroomingResponse +%% Log_Visualize_SingleUnit_GroomingResponse_Fig4F % This script produces two plots for a given neuron to illustrate its -% response profile. First, it computes firing rate of individual neuron -% under different behavioral conditions. -% 1st plot: a proportional histogram of firing rates for a given behavior, compared to rest. -% 2nd plot: smooth spike trace with labeled time points according to a set of behaviors. +% response profile to grooming (Fig 4F). +% 1st plot: a proportional histogram of firing rates for groom give vs. groom receive. +% 2nd plot: smooth spike trace with labeled time points according to groom give & groom receive. % C. Testard, July 2022 +% Revised by C. Testard Jan 2024 %Set session list is_mac = 1; @@ -210,32 +210,3 @@ % histogram(mean_gg_all-mean_gr_all, 'BinWidth',0.1) % length(find(mean_gg_all-mean_gr_all>0))/length(mean_gg_all) - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Old code -% %Plot example spike trains -% %figure; hold on -% spiketrains = 50; -% length_bout = 10; -% %xlim([0 length_bout]); ylim([0 spiketrains]) -% spike_times_behav = []; spike_times_rest = []; trials_behav = []; trials_rest = []; -% for st=1:spiketrains -% Hz_behav =Spike_rasters(n,randsample(idx,length_bout)); -% Hz_rest =Spike_rasters(n,randsample(idx_rest,length_bout)); -% -% total_spikes_behav = 0; total_spikes_rest = 0; -% for lb = 1:length_bout -% ticks_behav = rand(Hz_behav(lb),1); -% spike_times_behav = [spike_times_behav; lb-1+ticks_behav]; -% total_spikes_behav = total_spikes_behav + Hz_behav(lb); -% -% ticks_rest = rand(Hz_rest(lb),1); -% spike_times_rest=[spike_times_rest; lb-1+ticks_rest]; -% total_spikes_rest = total_spikes_rest+Hz_rest(lb); -% end -% -% trials_rest = [trials_rest, ones(1,total_spikes_rest)*st]; -% trials_behav = [trials_behav, ones(1,total_spikes_behav)*st]; -% end -% figure; subplot(2,1,1); spikeRasterPlot(seconds(spike_times_behav), trials_behav,'ColorOrder',[0 0 1]) -% subplot(2,1,2); spikeRasterPlot(seconds(spike_times_rest), trials_rest,'ColorOrder',[1 0 0]) \ No newline at end of file diff --git a/Grooming analyses/log_crossValModel_groom.m b/Fig4_Grooming_analyses/log_crossValModel_groom.m similarity index 100% rename from Grooming analyses/log_crossValModel_groom.m rename to Fig4_Grooming_analyses/log_crossValModel_groom.m diff --git a/Toolboxes/log_crossValReg_groom.m b/Fig4_Grooming_analyses/log_crossValReg_groom.m similarity index 100% rename from Toolboxes/log_crossValReg_groom.m rename to Fig4_Grooming_analyses/log_crossValReg_groom.m diff --git a/Grooming analyses/log_crossValRegression_groom.m b/Fig4_Grooming_analyses/log_crossValRegression_groom_Fig4h-j.m similarity index 96% rename from Grooming analyses/log_crossValRegression_groom.m rename to Fig4_Grooming_analyses/log_crossValRegression_groom_Fig4h-j.m index eea7a57..4d7b900 100644 --- a/Grooming analyses/log_crossValRegression_groom.m +++ b/Fig4_Grooming_analyses/log_crossValRegression_groom_Fig4h-j.m @@ -1,5 +1,7 @@ -%% log_crossValReg_groom -% Run cross-validated regression to predict reciprocity from neural data +%% log_crossValRegression_groom_Fig4h-j +% Run cross-validated regression to predict grooming reciprocity from neural data +% Output will result in Fig 4h-j and Extended Data Fig 4 f,g,i. +% C. Testard Jan. 2024 %Set session list is_mac = 1; @@ -10,14 +12,15 @@ end cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; - +session_range=[1:6,11:13,15:16,18]; +a_sessions = 1:6; h_sessions = [11:13,15:16,18]; %Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = 'all'; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +temp = 1; temp_resolution = 1; +channel_flag = "all"; +randomsample=0; %subsample neurons to match between brain areas +unq_behav=0; %If only consider epochs where only 1 behavior happens +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster randomsample=0; isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units num_iter = 100;%Number of SVM iterations @@ -28,15 +31,6 @@ threat_precedence =1; exclude_sq=1; -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - % a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - s=1; for s =session_range %1:length(sessions) @@ -52,7 +46,7 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') @@ -475,6 +469,7 @@ [~,idx_sorted]=sort(nanmedian(results)); results_sorted = results(:,idx_sorted); +%Fit per session figure; x=[ones(1,size(results_sorted,1)); 2*ones(1,size(results_sorted,1)); 3*ones(1,size(results_sorted,1))]; boxchart(results_sorted) @@ -483,6 +478,7 @@ ylabel('cvRsq') ylim([0 1]) +%Fit per neuron figure; data = cell2mat(FullRsq_per_neuron(session_range))'; x = [ones(1,size(data,1)); 2*ones(1,size(data,1)); 3*ones(1,size(data,1))]; diff --git a/Grooming analyses/log_grooming_behavior.m b/Fig4_Grooming_analyses/log_grooming_behavior_fig4b-e.m similarity index 98% rename from Grooming analyses/log_grooming_behavior.m rename to Fig4_Grooming_analyses/log_grooming_behavior_fig4b-e.m index c1ff7bb..ff04bd2 100644 --- a/Grooming analyses/log_grooming_behavior.m +++ b/Fig4_Grooming_analyses/log_grooming_behavior_fig4b-e.m @@ -1,6 +1,10 @@ -%% Log_grooming_behavior.m -% Characterize grooming reciprocity behaviorally. Run simulations to -% identify if reciprocity occurred more frequently than chance. +%% Log_grooming_behavior_fig4b-e.m +% Characterize grooming reciprocity behaviorally by extracting a series of statistics about +% grooming bouts received and given, their duration, +% interbout interval, turn-taking etc (Fig 4b-e, Extended Data Fig 4b-d, Extended Data Fig 5b). +% This script also runs simulations to identify if reciprocity occurred more frequently +% than chance (Extended Data Fig 4a). +% C. Testard July 2023 %Set session list is_mac = 1; @@ -11,20 +15,12 @@ end cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,8,11:13,15:18]; - +session_range=[1:6,11:13,15:16,18]; +a_sessions = 1:6; h_sessions = [11:13,15:16,18]; %Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +randomsample=0; %subsample neurons to match between brain areas randomsample=0; -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 0; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null simplify=0;%lump similar behavioral categories together to increase sample size. threat_precedence =1; exclude_sq=1; @@ -130,7 +126,6 @@ % figure; imagesc(behavior_labels_plot'); colormap(Cmap_groom) - %% Get groom behavior excluding intervening pause in grooming groom_labels = behavior_labels(ismember(behavior_labels, [7,8])); groomGive_only = zeros(size(groom_labels)); @@ -275,7 +270,8 @@ block_labels_plotSave{s}=block_labels_plot'; %figure; imagesc(behavior_labels_plot'); colormap(Cmap); colorbar - %% Get probablity of grooming after threat + + %% Get probablity of grooming after threat (Extended Fig 5b) alone_block = find(strcmp(block_times.Behavior,"Alone.block")); alone_start = block_times.start_time(alone_block); alone_end = block_times.end_time(alone_block); diff --git a/Grooming analyses/log_GroomReciprocity_decoding.m b/Fig4_Grooming_analyses/log_grooming_decoding_ExtendedDataFig4h.m similarity index 99% rename from Grooming analyses/log_GroomReciprocity_decoding.m rename to Fig4_Grooming_analyses/log_grooming_decoding_ExtendedDataFig4h.m index ba8ab7f..19047fe 100644 --- a/Grooming analyses/log_GroomReciprocity_decoding.m +++ b/Fig4_Grooming_analyses/log_grooming_decoding_ExtendedDataFig4h.m @@ -1,4 +1,4 @@ -%% log_SVM_GroomReciprocity.m +%% log_grooming_decoding_ExtendedDataFig4h.m %Decode grooming reciprocity from neural population activity. %Set session list diff --git a/Grooming analyses/log_grooming_umap.m b/Fig4_Grooming_analyses/log_grooming_umap_fig4k.m similarity index 100% rename from Grooming analyses/log_grooming_umap.m rename to Fig4_Grooming_analyses/log_grooming_umap_fig4k.m diff --git a/Grooming analyses/log_rest_decoding.m b/Fig4_Grooming_analyses/log_rest_decoding_ExtendedDataFig4h.m similarity index 73% rename from Grooming analyses/log_rest_decoding.m rename to Fig4_Grooming_analyses/log_rest_decoding_ExtendedDataFig4h.m index 4d2d003..63fa1be 100644 --- a/Grooming analyses/log_rest_decoding.m +++ b/Fig4_Grooming_analyses/log_rest_decoding_ExtendedDataFig4h.m @@ -1,4 +1,7 @@ -%% Log_grooming_behavior.m +%% Log_rest_decoding_ExtendedDatFig4h.m +% This script runs a linear decoder on resting bouts. The goal is to +% compare neural tracking of time between grooming and resting. +% C. Testard July 2023 %Set session list is_mac = 1; @@ -9,63 +12,25 @@ end cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - +session_range=[1:6,11:13,15:16,18]; +a_sessions = 1:6; h_sessions = [11:13,15:16,18]; %Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +temp = 1; temp_resolution = 1; +channel_flag = "all"; +randomsample=0; %subsample neurons to match between brain areas +unq_behav=0; %If only consider epochs where only 1 behavior happens +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster randomsample=0; isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units num_iter = 50;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth +smooth= 0; % 1: smooth the data; 0: do not smooth sigma = 1*temp_resolution;%set the smoothing window size (sigma) null=0;%Set whether we want the null simplify=0;%lump similar behavioral categories together to increase sample size. threat_precedence =1; exclude_sq=1; -%Set colormap -Cmap = [[1 0 0];...%Aggression; red - [1 0.4 0.1];...%Approach; dark orange - [0 0 0];...%But sniff; NA - [0.3 0.7 1];...%Drinking; light blue - [0 0.7 0];...%Foraging; dark green - [1 0 1];...%Groom sollicitation; magenta - [0 1 1];...%Groom partner; cyan - [0 0 1];...%Getting groomed; dark blue - [0.8 0 0];...%Threat to partner; dark red - [1 0 0];...%Threat to subject; red - [0.9 0.9 0];...%leave; dark yellow - [0 0 0];...%Lipsmack - [0.2 0.9 0.76];...%Masturbating; turquoise - [0.7 0 1];...%Mounting; light purple - [0.9 0.5 0];...%Other monkeys vocalize; orange - [1 0.8 0.1];...%Travel; yellow orange - [0 0 0];...%Proximity; NA - [0 0 0];...%Rowdy room; NA - [1 0 0];...%SP; NA - [1 0 0];...%SS; NA - [0.6314 0.5059 0.0118];...%Scratch; maroon - [0.5 0.2 0.5];...%Self-groom; dark purple - [ 1 0.07 0.65];...%Submission; dark pink - [0 0.4 0.5];...%Vocalzation; blue green - [0 0 0];...%Yawning; NA - [0.8 0.8 0.8]];%Rest; grey -Cmap_block = [[0.9 0.7 0.12];[0 0.6 0.8];[0.5 0 0];[0.8 0.8 0.8]]; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - s=1; for s =session_range %1:length(sessions) @@ -78,7 +43,7 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') @@ -110,14 +75,12 @@ time = 1:length(behavior_labels); bouts_to_consider = 1:size(rest_bout_final,1); - rest_idx=[]; timescale_all=[]; boutid_all=[]; + rest_idx=[]; boutid_all=[]; for b=1:length(bouts_to_consider) idx = rest_bout_final(bouts_to_consider(b),1):rest_bout_final(bouts_to_consider(b),2); - timescale = rescale(idx); bout_id = ones(size(idx))*b; rest_idx = [rest_idx, idx]; - timescale_all = [timescale_all, timescale]; boutid_all = [boutid_all, bout_id]; end @@ -180,6 +143,5 @@ data = mean_hitrate(session_range)'; data_shuffle = mean_hitrate_shuffled(session_range)'; bp = bar([mean(data(:,:)); mean(data_shuffle(:,:))]','FaceAlpha',0.2); - sp1 = scatter(ones(size(data,1))*1,data(:,1), 'filled','b'); sp1 = scatter(ones(size(data_shuffle,1))*2,data_shuffle(:,1), 'filled','r'); diff --git a/Grooming analyses/log_rest_umap.m b/Fig4_Grooming_analyses/log_rest_umap_fig4k.m similarity index 52% rename from Grooming analyses/log_rest_umap.m rename to Fig4_Grooming_analyses/log_rest_umap_fig4k.m index f107d13..cc6475c 100644 --- a/Grooming analyses/log_rest_umap.m +++ b/Fig4_Grooming_analyses/log_rest_umap_fig4k.m @@ -1,4 +1,6 @@ -%% Log_grooming_behavior.m +%% Log_rest_umap_Fig4k.m +% This script plots neural states during resting, ordered by bout number. +% By C. Testard Jan 2024 %Set session list is_mac = 1; @@ -9,16 +11,15 @@ end cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - +session_range=[1:6,11:13,15:16,18]; +a_sessions = 1:6; h_sessions = [11:13,15:16,18]; %Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -randomsample=0; +temp = 1; temp_resolution = 1; +channel_flag = "all"; +randomsample=0; %subsample neurons to match between brain areas +unq_behav=0; %If only consider epochs where only 1 behavior happens +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units num_iter = 50;%Number of SVM iterations smooth= 0; % 1: smooth the data; 0: do not smooth @@ -57,15 +58,6 @@ [0.8 0.8 0.8]];%Rest; grey Cmap_block = [[0.9 0.7 0.12];[0 0.6 0.8];[0.5 0 0];[0.8 0.8 0.8]]; -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - s=1; for s =session_range %1:length(sessions) @@ -78,7 +70,7 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') @@ -134,13 +126,6 @@ set(gca,'FontSize',12); title('Behavior') -% figure; -% scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),10,Cmap_block(block_labels(rest_idx),:),'filled') -% xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -% %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -% set(gca,'FontSize',12); -% title('Neighbor ID') - ax2= subplot(1,2,2); scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),10,boutid_all,'filled')%Cmap_recip(recip,:),'filled') xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') @@ -150,87 +135,9 @@ set(gca,'FontSize',12); title('Bout Number') -% figure -% scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),10,time(rest_idx),'filled')%Cmap_recip(recip,:),'filled') -% xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -% colormap(hot) -% %colorbar -% %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -% set(gca,'FontSize',12); -% title('Time in session') hlink = linkprop([ax1,ax2],{'CameraPosition','CameraUpVector'}); rotate3d on - %% Decoding - - neural_data = zscore(Spike_count_raster{s}); - behav=length(behav_categ); - rest_idx = find(ismember(behavior_labels,behav)); - - for categ = 1:2 - for subsample_iter = 1:10 - bouts_to_consider = randsample(size(rest_bout_final,1),16); - idx_all=[]; timescale_all=[]; boutid_all=[]; - - for b=1:length(bouts_to_consider) - idx = rest_bout_final(bouts_to_consider(b),1):rest_bout_final(bouts_to_consider(b),2); - timescale = round(rescale(idx)*10); - bout_id = ones(size(idx)).*b; - - idx_all = [idx_all, idx]; - timescale_all = [timescale_all, timescale]; - boutid_all = [boutid_all, bout_id]; - end - rest_categ = [timescale_all;boutid_all]; - neural_data_final=neural_data(idx_all,:); - behavior_labels_final = rest_categ(categ,:); - - disp('Start running SVM...') - for iter = 1:num_iter - - %subsample to match number of neurons across brain areas - Labels = behavior_labels_final; - Input_matrix = neural_data_final; - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %use the minimum # of instances - - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials, idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials); - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels', 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled', 5, 0, 0); - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %end of SVM iterations - - mean_hitrate(categ, subsample_iter) = mean(hitrate); - mean_hitrate_shuffled(categ, subsample_iter) = mean(hitrate_shuffled); - end - - end end diff --git a/Threat analyses/.DS_Store b/Fig5_Threat_analyses/.DS_Store similarity index 94% rename from Threat analyses/.DS_Store rename to Fig5_Threat_analyses/.DS_Store index 2a8d58d..512f82b 100644 Binary files a/Threat analyses/.DS_Store and b/Fig5_Threat_analyses/.DS_Store differ diff --git a/Threat analyses/log_Movements_Threat_Self_vs_Partner.m b/Fig5_Threat_analyses/log_Movements_Threat_Self_vs_Partner.m similarity index 95% rename from Threat analyses/log_Movements_Threat_Self_vs_Partner.m rename to Fig5_Threat_analyses/log_Movements_Threat_Self_vs_Partner.m index 6bd4811..4f4b2a0 100644 --- a/Threat analyses/log_Movements_Threat_Self_vs_Partner.m +++ b/Fig5_Threat_analyses/log_Movements_Threat_Self_vs_Partner.m @@ -1,5 +1,8 @@ %% Log_Movements_Threat_Self_vs_Partner +% This script compares the head orientation during threats to self vs. +% partner. % August 2023 - Camille Testard +% Revised by C. Testard Jan 2024 %Set session list is_mac = 1; @@ -18,7 +21,7 @@ channel_flag = "all"; randomsample=0; %subsample neurons to match between brain areas unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MU =1;%0: MU cluster is excluded; 1:MU cluster is included; 2:ONLY multi-unit cluster isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units num_iter = 50;%Number of SVM iterations smooth= 1; % 1: smooth the data; 0: do not smooth @@ -49,7 +52,7 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MU, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); %Load mvmt data mvmt = readtable(['mvmt_data_c' num2str(c_cutoff) '.csv']);% Load DLC key point data diff --git a/Threat analyses/log_ThreatToSelf_paired_vs_alone.m b/Fig5_Threat_analyses/log_ThreatToSelf_paired_vs_alone_Fig5c.m similarity index 62% rename from Threat analyses/log_ThreatToSelf_paired_vs_alone.m rename to Fig5_Threat_analyses/log_ThreatToSelf_paired_vs_alone_Fig5c.m index 01c6222..8313b24 100644 --- a/Threat analyses/log_ThreatToSelf_paired_vs_alone.m +++ b/Fig5_Threat_analyses/log_ThreatToSelf_paired_vs_alone_Fig5c.m @@ -1,9 +1,8 @@ -%% log_self_Vs_other_whenPaired -% This script extracts and compares the mean firing rate across different threat -% instances (self vs. other; alone vs. paired). -%Log_ThreatToSelf_paired_vs_alone -% This script computed the average firing rate of individual units -% during threats twoards the subject when paired vs alone +%% Log_ThreatToSelf_paired_vs_alone_Fig5c +% This script computes the average firing rate of individual units +% during threats towards the subject when paired vs alone. It outputs +% panels in Fig 5c +% Created by C. Testard. Last update Jan 2024 %Set session list is_mac = 1; @@ -23,7 +22,7 @@ channel_flag = "all"; randomsample=0; %subsample neurons to match between brain areas unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY multi-unit cluster +with_MUC =1;%0: MUC is excluded; 1:MUC is included; 2:ONLY multi-unit cluster isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units smooth= 1; % 1: smooth the data; 0: do not smooth sigma = 1*temp_resolution;%set the smoothing window size (sigma) @@ -46,7 +45,7 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label{s}, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MUC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') @@ -75,47 +74,50 @@ Threat_paired = mean(cell2mat(Spike_count_raster_subject(1,1,block_selfthreat==1)),3); Threat_alone = mean(cell2mat(Spike_count_raster_subject(1,1,block_selfthreat==0)),3); - %% Plot threat to self response in paired vs. alone conditions - -% % % % %Example neuron -% % % % paired = cell2mat(Spike_count_raster_subject(1,1,block_selfthreat==1)); -% % % % alone = cell2mat(Spike_count_raster_subject(1,1,block_selfthreat==0)); -% % % % n=1; -% % % % figure; hold on -% % % % plot(smoothdata(alone(n,:,1),'movmean',3), 'Color','b') -% % % % plot(smoothdata(alone(n,:,2),'movmean',3), 'Color','b') -% % % % plot(smoothdata(paired(n,:,1),'movmean',3), 'Color','r') -% % % % plot(smoothdata(paired(n,:,2),'movmean',3), 'Color','r') -% % % % xline(10,'LineStyle','--','LineWidth',2,'Label','Threat onset') -% % % % xlim([0,40]) -% % % % legend({'Alone','Paired'}) -% % % % -% % % % %Mean across all neurons -% % % % figure; hold on -% % % % mean_alone=smoothdata(mean(Threat_alone),'movmean',3); -% % % % sem_alone=std(Threat_alone)%./sqrt(size(Threat_alone,1)); -% % % % mean_paired=smoothdata(mean(Threat_paired),'movmean',3); -% % % % sem_paired=std(Threat_paired)%./sqrt(size(Threat_paired,1)); -% % % % %plot mean response across all neurons when ALONE -% % % % x = 1:numel(mean_alone); -% % % % curve1 = mean_alone+sem_alone; -% % % % curve2 = mean_alone-sem_alone; -% % % % x2 = [x, fliplr(x)]; -% % % % inBetween = [curve1, fliplr(curve2)]; -% % % % fill(x2, inBetween, [0.9 0.9 0.9], 'FaceAlpha',0.2,'FaceColor','r'); -% % % % plot(x,mean_alone,'LineWidth',2) -% % % % %when PAIRED -% % % % x = 1:numel(mean_paired); -% % % % curve1 = mean_paired+sem_paired; -% % % % curve2 = mean_paired-sem_paired; -% % % % x2 = [x, fliplr(x)]; -% % % % inBetween = [curve1, fliplr(curve2)]; -% % % % fill(x2, inBetween, [0.9 0.9 0.9], 'FaceAlpha',0.2,'FaceColor','m'); -% % % % plot(x,mean_paired,'LineWidth',2) -% % % % %plot event onset -% % % % xline(10,'LineStyle','--','LineWidth',2,'Label','Threat onset') -% % % % legend({'','Alone','','Paired'}) -% % % % xlim([0 40]) + if s==2 + %% Plot threat to self response in paired vs. alone conditions + %Line plots + + %Example neuron + paired = cell2mat(Spike_count_raster_subject(1,1,block_selfthreat==1)); + alone = cell2mat(Spike_count_raster_subject(1,1,block_selfthreat==0)); + n=1; + figure; hold on + plot(smoothdata(alone(n,:,1),'movmean',3), 'Color','b') + plot(smoothdata(alone(n,:,2),'movmean',3), 'Color','b') + plot(smoothdata(paired(n,:,1),'movmean',3), 'Color','r') + plot(smoothdata(paired(n,:,2),'movmean',3), 'Color','r') + xline(10,'LineStyle','--','LineWidth',2,'Label','Threat onset') + xlim([0,40]) + legend({'Alone','Paired'}) + + %Mean across all neurons + figure; hold on + mean_alone=smoothdata(mean(Threat_alone),'movmean',3); + sem_alone=std(Threat_alone)./sqrt(size(Threat_alone,1)); + mean_paired=smoothdata(mean(Threat_paired),'movmean',3); + sem_paired=std(Threat_paired)./sqrt(size(Threat_paired,1)); + %plot mean response across all neurons when ALONE + x = 1:numel(mean_alone); + curve1 = mean_alone+sem_alone; + curve2 = mean_alone-sem_alone; + x2 = [x, fliplr(x)]; + inBetween = [curve1, fliplr(curve2)]; + fill(x2, inBetween, [0.9 0.9 0.9], 'FaceAlpha',0.2,'FaceColor','r'); + plot(x,mean_alone,'LineWidth',2) + %when PAIRED + x = 1:numel(mean_paired); + curve1 = mean_paired+sem_paired; + curve2 = mean_paired-sem_paired; + x2 = [x, fliplr(x)]; + inBetween = [curve1, fliplr(curve2)]; + fill(x2, inBetween, [0.9 0.9 0.9], 'FaceAlpha',0.2,'FaceColor','m'); + plot(x,mean_paired,'LineWidth',2) + %plot event onset + xline(10,'LineStyle','--','LineWidth',2,'Label','Threat onset') + legend({'','Alone','','Paired'}) + xlim([0 40]) + end %% Plot average firing rate in both conditions for the length of the threat mean_activity_alone{s,1}=mean(Threat_alone(:,11:end),2); @@ -136,12 +138,16 @@ end %end of session for loop +%% Plot result across all units + brain_label_all = cat(2,brain_label{:}); mean_activity_alone_pooled = cell2mat(mean_activity_alone); mean_activity_paired_pooled = cell2mat(mean_activity_paired); length(find((mean_activity_alone_pooled- mean_activity_paired_pooled)>0))./size(mean_activity_paired_pooled,1) +%Figure 5c right panel +%Mean activity threat to self when paired vs. alone figure; hold on scatter(mean_activity_paired_pooled(strcmp(brain_label_all, "TEO")), mean_activity_alone_pooled(strcmp(brain_label_all, "TEO")), 'MarkerEdgeColor','red') scatter(mean_activity_paired_pooled(strcmp(brain_label_all, "vlPFC")), mean_activity_alone_pooled(strcmp(brain_label_all, "vlPFC")), 'MarkerEdgeColor','blue') diff --git a/Threat analyses/log_self_vs_other_whenPaired.m b/Fig5_Threat_analyses/log_representational_similarity_neural_activity_self_Vs_other_whenPaired_Fig5d.m similarity index 92% rename from Threat analyses/log_self_vs_other_whenPaired.m rename to Fig5_Threat_analyses/log_representational_similarity_neural_activity_self_Vs_other_whenPaired_Fig5d.m index 0fa2666..2cc2c68 100644 --- a/Threat analyses/log_self_vs_other_whenPaired.m +++ b/Fig5_Threat_analyses/log_representational_similarity_neural_activity_self_Vs_other_whenPaired_Fig5d.m @@ -1,6 +1,9 @@ -%% log_self_Vs_other_whenPaired -% This script computes the coefficient of correlation between the average -% responses +%% log_representational_similarity_neural_activity_self_Vs_other_whenPaired_Fig5d +% This script computes the representational similarity (or coefficient of correlation) +% between the average responses of neurons when threats are directed +% towards the subject or his partner, when paired (Fig 5d right panels). + +% Created by C. Testard. Last update Jan 2024 %Set session list is_mac = 1; @@ -55,7 +58,7 @@ block_labels = cell2mat({labels{:,13}}'); - %% Find threat to subject onset times + %% Find behavioral onset times threat_to_subject_onset = behavior_log.start_time_round(find(strcmp(behavior_log.Behavior, "HIS"))); threat_to_partner_onset = behavior_log.start_time_round(find(strcmp(behavior_log.Behavior, "HIP"))); @@ -150,7 +153,8 @@ disp(s) disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - % %Plot + % Plot matrices that are being correlated in the representational + % similarity analysis (Fig 4d, top right panel) % figure; % subplot(1,2,1) % imagesc(mean_response_ThreatSubject); colorbar; colormap parula; caxis([-4 4]) @@ -167,7 +171,8 @@ rep_similarity_threats(rep_similarity_threats==0)=nan; rep_similarity_selfGroom(rep_similarity_selfGroom ==0)=nan; - +%Bar plot of the RS scores by conditions and monkeys (Fig 5d bottom right +% panel) figure; hold on bar([nanmean(rep_similarity_threats), ... nanmean(rep_similarity_selfGroom),... diff --git a/Threat analyses/log_self_vs_other_whenPaired_LinePlot.m b/Fig5_Threat_analyses/log_self_vs_other_whenPaired_LinePlot_Fig5d.m similarity index 95% rename from Threat analyses/log_self_vs_other_whenPaired_LinePlot.m rename to Fig5_Threat_analyses/log_self_vs_other_whenPaired_LinePlot_Fig5d.m index 2cbfac3..3f04fbb 100644 --- a/Threat analyses/log_self_vs_other_whenPaired_LinePlot.m +++ b/Fig5_Threat_analyses/log_self_vs_other_whenPaired_LinePlot_Fig5d.m @@ -1,6 +1,8 @@ -%% log_self_Vs_other_whenPaired -% This script computes the coefficient of correlation between the average -% responses +%% log_self_Vs_other_whenPaired_LinePlot_Fig5d +% This script computes the average firing rate of individual units +% during threats towards the subject vs. his partner when paired. It outputs +% panels in Fig 5d (left panels). +% Created by C. Testard July 2023. Last update Jan 2024. %Set session list is_mac = 1; @@ -20,7 +22,7 @@ channel_flag = "all"; randomsample=0; %subsample neurons to match between brain areas unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY multi-unit cluster +with_MUC =1;%0: MUC is excluded; 1:MUC is included; 2:ONLY multi-unit cluster isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units smooth= 1; % 1: smooth the data; 0: do not smooth sigma = 1*temp_resolution;%set the smoothing window size (sigma) @@ -43,7 +45,7 @@ [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); + is_mac, with_MUC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') diff --git a/Threat analyses/log_umap_state_distances_threat.m b/Fig5_Threat_analyses/log_umap_distances_between_threat_and_rest_Fig5e.m similarity index 91% rename from Threat analyses/log_umap_state_distances_threat.m rename to Fig5_Threat_analyses/log_umap_distances_between_threat_and_rest_Fig5e.m index 9e14b24..521ba4f 100644 --- a/Threat analyses/log_umap_state_distances_threat.m +++ b/Fig5_Threat_analyses/log_umap_distances_between_threat_and_rest_Fig5e.m @@ -1,6 +1,8 @@ -%% log_umap_state_distances_threat +%% log_umap_distances_between_threat_and_rest_Fig5e % This script applies UMAP to the data and computes the distance between -% threat data points to a baseline state (centre of mass for rest). +% threat data points to a baseline state (centre of mass for rest). The +% output will be Fig 5e. +% Created by C. Testard. Last updated Jan. 2024 %Set session list is_mac = 1; @@ -11,10 +13,8 @@ end cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -%session_range_no_partner=[1:3,11:13]; -%session_range_no_partner=[4:6,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; +session_range=[1:6,11:13,15:16,18]; +a_sessions = 1:6; h_sessions = [11:13,15:16,18]; %Set parameters with_partner =0; @@ -22,7 +22,7 @@ channel_flag = "all"; randomsample=0; %subsample neurons to match between brain areas unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster +with_MUC =1;%0: NC is excluded; 1:NC is included; 2:ONLY MU cluster isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units smooth= 1; % 1: smooth the data; 0: do not smooth sigma = 1*temp_resolution;%set the smoothing window size (sigma) @@ -33,14 +33,6 @@ time_after_threat = 60*temp_resolution; exclude_sq=0; -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end s=1; chan=1; for s =session_range %1:length(sessions) @@ -51,17 +43,10 @@ %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - end + [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... + unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... + log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... + is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); disp('Data Loaded') diff --git a/Grooming analyses/log_Dist_GroomTypes.m b/Grooming analyses/log_Dist_GroomTypes.m deleted file mode 100644 index 886aa0f..0000000 --- a/Grooming analyses/log_Dist_GroomTypes.m +++ /dev/null @@ -1,327 +0,0 @@ -%% Log_SVM_grooming_batch -% Run a linear decoder on a the neural activity for different grooming contexts -% This script allows to decode grooming: -% 1. Start vs. end -% 2. Post-threat or not -% 3. Reciprocated or not -% 4. Initiated or not -% 5. Neighbor ID -% Batch version to run across sessions. -%Camille Testard, March 2022 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -randomsample=0; -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 50;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. -threat_precedence =1; -exclude_sq=1; - -%Initialize -% mean_hitrate = cell(length(sessions),3); -% sd_hitrate = cell(length(sessions),3); -% mean_hitrate_shuffled = cell(length(sessions),3); - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan=1; - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - session_length = size(Spike_count_raster ,1); - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - block_labels = cell2mat({labels{:,12}}'); - - %% Interpolate short groom bouts - groomGive = zeros(size(behavior_labels)); - groomGive(behavior_labels== 7)=1; - groomGive_time = find(groomGive==1); - time_between_bouts = diff(groomGive_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGive_time(short_Runs(sr))+1:groomGive_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGive(idx_to_fill)=1; - end - - behavior_labels(find(groomGive==1))=7; - - %Groom get - groomGet = zeros(size(behavior_labels)); - groomGet(behavior_labels== 8)=1; - groomGet_time = find(groomGet==1); - time_between_bouts = diff(groomGet_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGet_time(short_Runs(sr))+1:groomGet_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGet(idx_to_fill)=1; - end - - behavior_labels(find(groomGet==1))=8; - - %% Create grooming type labels - - %GROOM GIVE - groomGive_bout_start = find(diff(groomGive)==1)+1; - groomGive_bout_end = find(diff(groomGive)==-1); - if length(groomGive_bout_end)0 - groomGive_bout(bout,5) = any(behavior_labels(groomGive_bout_start(bout)-30:groomGive_bout_start(bout)-1)==9 ... - |behavior_labels(groomGive_bout_start(bout)-30:groomGive_bout_start(bout)-1)==10); - else - groomGive_bout(bout,5)=0; - end - end - groomGive_bout(:,6)=7; - %4th column: was there a threat right after the groom (which could have - %cut it short) - %5th column: was there a threat preceding the grooming bout - %6th column, grooming receive or give. - - %cut too short duratiin bouts (usually occurs because smth external - %cuts the bout (aggression) - %groomGive_bout(groomGive_bout(:,3)<10) - - %GROOM GET - groomGet_bout_start = find(diff(groomGet)==1)+1; - groomGet_bout_end = find(diff(groomGet)==-1); - groomGet_duration = groomGet_bout_end-groomGet_bout_start; - groomGet_bout=[groomGet_bout_start, groomGet_bout_end, groomGet_duration]; - - for bout = 1:length(groomGet_bout_end) - - - if groomGet_bout_end(bout)+110 - groomGet_bout(bout,5) = any(behavior_labels(groomGet_bout_start(bout)-30:groomGet_bout_start(bout)-1)==9 ... - |behavior_labels(groomGet_bout_start(bout)-30:groomGet_bout_start(bout)-1)==10); - else - groomGet_bout(bout,5) =0; - end - end - groomGet_bout(:,6)=8; - - % ALL GROOMS - allGroomBouts = [groomGet_bout; groomGive_bout]; - [~, idx_sorted] = sort(allGroomBouts(:,1)); - allGroomBouts_sorted = allGroomBouts(idx_sorted,:); - allGroomBouts_sorted(:,7)=[0;(allGroomBouts_sorted(2:end,1)-allGroomBouts_sorted(1:end-1,2))]; - allGroomBouts_sorted(:,8) = abs([0; diff(allGroomBouts_sorted(:,6))]); - allGroomBouts_sorted(:,9)=0; - allGroomBouts_sorted(find(allGroomBouts_sorted(:,8)==1 & allGroomBouts_sorted(:,7)<20),9)=1; - - - beh = 1;behav=8; - for behav = [8,7] %For both groom give and groom receive - - for sim=1:1000 - groombouts = allGroomBouts_sorted(allGroomBouts_sorted(:,6)==behav,:); - bouts_to_consider = 1:size(groombouts,1); - idx_all=[];timescale_all=[]; boutid_all=[]; threatid_all=[]; recipid_all=[]; blockid_all=[]; - for b=1:length(bouts_to_consider) - idx = groombouts(bouts_to_consider(b),1):groombouts(bouts_to_consider(b),2); - timescale = round(rescale(idx)*10); - bout_id = ones(size(idx))*b; - threat_id = ones(size(idx))*groombouts(bouts_to_consider(b),5)+1; - recip_id = ones(size(idx))*groombouts(bouts_to_consider(b),9)+1; - block_id = ones(size(idx))*unique(block_labels(idx)); - - idx_all = [idx_all, idx]; - timescale_all = [timescale_all, timescale]; - boutid_all = [boutid_all, bout_id]; - threatid_all = [threatid_all, threat_id]; - recipid_all = [recipid_all, recip_id]; - blockid_all = [blockid_all, block_id]; - - end - groom_type_label = [threatid_all; recipid_all; boutid_all; timescale_all; blockid_all]; - groom_categ=1; - [~, sorted_idx] = sort(groom_type_label(groom_categ,:)); - behavior_labels_interim = groom_type_label(groom_categ,sorted_idx); - Spike_count_raster_interim = Spike_count_raster(idx_all(sorted_idx),:);%Only keep timepoints where the behaviors of interest occur in spiking data - - - %Balance number of trials per class - Labels = behavior_labels_interim; - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %use the minimum # of instances - - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials, idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Spike_count_raster_final = Spike_count_raster_interim(chosen_trials, :); - Labels = Labels(chosen_trials); - Labels_shuffled = Labels(randperm(length(Labels))); - - [~, score]=pca(zscore(Spike_count_raster_final)); - %test=squareform(pdist(zscore(Spike_count_raster_final),'cosine')); - test=squareform(pdist(score(:,1:50),'cosine')); -% figure; hold on; imagesc(test); colorbar; %caxis([0 1.5]) -% xline(find(diff(Labels)~=0)-1,'-k') -% yline(find(diff(Labels)~=0)-1,'-k') - - within_categ(sim) = mean(mean(test(Labels==2,Labels==2))); - between_categ(sim) = mean(mean(test(Labels==2,Labels==1))); - end - - - %SHUFFLED CONTROL - for sim=1:1000 - groombouts = allGroomBouts_sorted(allGroomBouts_sorted(:,6)==behav,:); - groombouts(:,5) = randsample(groombouts(:,5), size(groombouts,1)); - groombouts(:,9) = randsample(groombouts(:,9), size(groombouts,1)); - bouts_to_consider = 1:size(groombouts,1); - idx_all=[];timescale_all=[]; boutid_all=[]; threatid_all=[]; recipid_all=[]; blockid_all=[]; - for b=1:length(bouts_to_consider) - idx = groombouts(bouts_to_consider(b),1):groombouts(bouts_to_consider(b),2); - timescale = round(rescale(idx)*10); - bout_id = ones(size(idx))*b; - threat_id = ones(size(idx))*groombouts(bouts_to_consider(b),5)+1; - recip_id = ones(size(idx))*groombouts(bouts_to_consider(b),9)+1; - block_id = ones(size(idx))*unique(block_labels(idx)); - - idx_all = [idx_all, idx]; - timescale_all = [timescale_all, timescale]; - boutid_all = [boutid_all, bout_id]; - threatid_all = [threatid_all, threat_id]; - recipid_all = [recipid_all, recip_id]; - blockid_all = [blockid_all, block_id]; - - end - groom_type_label = [threatid_all; recipid_all; boutid_all; timescale_all; blockid_all]; - groom_categ=1; - [~, sorted_idx] = sort(groom_type_label(groom_categ,:)); - behavior_labels_interim = groom_type_label(groom_categ,sorted_idx); - Spike_count_raster_interim = Spike_count_raster(idx_all(sorted_idx),:);%Only keep timepoints where the behaviors of interest occur in spiking data - - - %Balance number of trials per class - Labels = behavior_labels_interim; - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %use the minimum # of instances - - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials, idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Spike_count_raster_final = Spike_count_raster_interim(chosen_trials, :); - Labels = Labels(chosen_trials); - Labels_shuffled = Labels(randperm(length(Labels))); - - [~, score]=pca(zscore(Spike_count_raster_final)); - %test=squareform(pdist(zscore(Spike_count_raster_final),'cosine')); - test=squareform(pdist(score(:,1:50),'cosine')); - % figure; hold on; imagesc(test); colorbar; %caxis([0 1.5]) - % xline(find(diff(Labels)~=0)-1,'-k') - % yline(find(diff(Labels)~=0)-1,'-k') - - within_categ_shuffle(sim) = mean(mean(test(Labels==2,Labels==2))); - between_categ_shuffle(sim) = mean(mean(test(Labels==2,Labels==1))); - end - - figure; hold on; histogram(within_categ); histogram(within_categ_shuffle) - figure; hold on; histogram(between_categ); histogram(between_categ_shuffle) - - - - end %End of give vs. receive for loop - - chan = chan +1; - end %End of channel for loop - - close all - - disp('Session done') - -end %End of session for loop diff --git a/Grooming analyses/log_grooming_TimeWarping.m b/Grooming analyses/log_grooming_TimeWarping.m deleted file mode 100644 index b6f1f01..0000000 --- a/Grooming analyses/log_grooming_TimeWarping.m +++ /dev/null @@ -1,337 +0,0 @@ -%% Log_grooming_TimeWarping.m -% Plot time warped firing rate during grooming bouts (cut or lengthened to -% match median grooming bout length). - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range = session_range_no_partner; -a_sessions = 1:6; h_sessions = [11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -randomsample=0; -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. -threat_precedence =1; -exclude_sq=1; -time_preGroom = 100; -time_postGroom = 100; -time_postThreat = 300; -time_postApproch = 60; - -%Set colormap -Cmap = [[1 0 0];...%Aggression; red - [1 0.4 0.1];...%Approach; dark orange - [0 0 0];...%But sniff; NA - [0.3 0.7 1];...%Drinking; light blue - [0 0.7 0];...%Foraging; dark green - [1 0 1];...%Groom sollicitation; magenta - [0 1 1];...%Groom partner; cyan - [0 0 1];...%Getting groomed; dark blue - [0.8 0 0];...%Threat to partner; dark red - [1 0 0];...%Threat to subject; red - [0.9 0.9 0];...%leave; dark yellow - [0 0 0];...%Lipsmack - [0.2 0.9 0.76];...%Masturbating; turquoise - [0.7 0 1];...%Mounting; light purple - [0.9 0.5 0];...%Other monkeys vocalize; orange - [1 0.8 0.1];...%Travel; yellow orange - [0 0 0];...%Proximity; NA - [0 0 0];...%Rowdy room; NA - [1 0 0];...%SP; NA - [1 0 0];...%SS; NA - [0.6314 0.5059 0.0118];...%Scratch; maroon - [0.5 0.2 0.5];...%Self-groom; dark purple - [ 1 0.07 0.65];...%Submission; dark pink - [0 0.4 0.5];...%Vocalzation; blue green - [0 0 0];...%Yawning; NA - [0.8 0.8 0.8]];%Rest; grey -Cmap_block = [[0.9 0.7 0.12];[0 0.6 0.8];[0.5 0 0];[0.8 0.8 0.8]]; - - -s=1; event1=1; event2=1; event3=1; event4=1; event5=1; event6=1;event7=1; event8=1;event9=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - - Spike_count_raster{s} = Spike_rasters'; - session_length(s) = size(Spike_count_raster{s},1); - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); - block_labels = cell2mat({labels{:,12}}'); - - - %% Interpolate short groom bouts - groomGive = zeros(size(behavior_labels)); - groomGive(behavior_labels== 7)=1; - groomGive_time = find(groomGive==1); - time_between_bouts = diff(groomGive_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGive_time(short_Runs(sr))+1:groomGive_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGive(idx_to_fill)=1; - end - - behavior_labels(find(groomGive==1))=7; - - %Groom get - groomGet = zeros(size(behavior_labels)); - groomGet(behavior_labels== 8)=1; - groomGet_time = find(groomGet==1); - time_between_bouts = diff(groomGet_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGet_time(short_Runs(sr))+1:groomGet_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGet(idx_to_fill)=1; - end - - behavior_labels(find(groomGet==1))=8; - - behavior_labels_tosave{1,s} =behavior_labels'; - block_labels_tosave{1,s} =block_labels'; - - %% Extract grooming stats (bout length and# per session) - - %GROOM GIVE - groomGive_bout_start = find(diff(groomGive)==1)+1; - groomGive_bout_end = find(diff(groomGive)==-1); - if length(groomGive_bout_end)0 - groomGive_bout(bout,5) = any(behavior_labels(groomGive_bout_start(bout)-30:groomGive_bout_start(bout)-1)==9 ... - |behavior_labels(groomGive_bout_start(bout)-30:groomGive_bout_start(bout)-1)==10); - else - groomGive_bout(bout,5)=0; - end - -% % % % %Check what the groom is followed by -% % % % if groomGive_bout_end(bout)+1200 - groomGet_bout(bout,5) = any(behavior_labels(groomGet_bout_start(bout)-30:groomGet_bout_start(bout)-1)==9 ... - |behavior_labels(groomGet_bout_start(bout)-30:groomGet_bout_start(bout)-1)==10); - else - groomGet_bout(bout,5) =0; - end - end - groomGet_bout(:,6)=8; - - % ALL GROOMS - allGroomBouts = [groomGet_bout; groomGive_bout]; - [~, idx_sorted] = sort(allGroomBouts(:,1)); - allGroomBouts_sorted = allGroomBouts(idx_sorted,:); - %Get inter-bout interval - allGroomBouts_sorted(:,7)=[0;(allGroomBouts_sorted(2:end,1)-allGroomBouts_sorted(1:end-1,2))]; - %Is previous bout grooming in the other direction - allGroomBouts_sorted(:,8) = abs([0; diff(allGroomBouts_sorted(:,6))]); - %Mark turn-taking bouts - allGroomBouts_sorted(:,9)=0; - allGroomBouts_sorted(find(allGroomBouts_sorted(:,8)==1 & allGroomBouts_sorted(:,7)<20),9)=1; - allGroomBouts_sorted(:,10)=s; - %7th column: inter-bout interval - %8th column: alternation of grooming bout - %9th column: is grooming bout turn-taking - - allGroomBouts_sorted_save{s}=allGroomBouts_sorted; - -end - -%% activity aligned to end of grooming - -s=1; -groombouts = allGroomBouts_sorted_save{s}; -groombouts = groombouts(groombouts(:,3)>10*temp_resolution,:); -neural_data = zscore(Spike_count_raster{s}); - -num_neurons = size(neural_data,1); -num_trials = size(groombouts,1); -min_duration = min(groombouts(:,3)); -max_duration = max(groombouts(:,3)); - -for bout = 1:size(groombouts,1) - firing_rates{bout} = neural_data(groombouts(bout,1):groombouts(bout,2),:); -end - -% Find the median duration of behavior A -median_duration = round(median(cellfun(@(x) size(x, 1), firing_rates))); - -% Time-warp the firing rates to the median duration -time_warped_firing_rates = cellfun(@(x) interp1(1:size(x, 1), x, linspace(1, size(x, 1), median_duration)), firing_rates, 'UniformOutput', false); - -% Compute the average firing rate per neuron during behavior A -average_firing_rate = mean(cell2mat(reshape(time_warped_firing_rates, 1, 1, [])), 3); - -average_firing_rate_vlpfc = average_firing_rate(:,strcmp(brain_label,"TEO")); -average_firing_rate_teo = average_firing_rate(:,strcmp(brain_label,"vlPFC")); -[isort1, isort2_TEO,~,clusterTEO] = mapTmap(average_firing_rate_teo); -[isort1, isort2_vlPFC] = mapTmap(average_firing_rate_vlpfc); -activity_sorted = [average_firing_rate_teo(:,isort2_TEO), average_firing_rate_vlpfc(:,isort2_vlPFC) ]; -figure; imagesc(activity_sorted'); colorbar; caxis([-0.5 0.5]) - -[coeff, score]= pca(average_firing_rate); -figure; hold on -for pc=1:5 - plot(score(:,pc)) -end - -for bout = 1:size(time_warped_firing_rates,2) - [coeff, score]= pca(time_warped_firing_rates{bout}); - figure; hold on - for pc=1:5 - plot(score(:,pc)) - - end - pause(1) -end - - -%Groom give end of bout -bouts_to_consider=1:size(groombouts,1); %find(groombouts(:,4)==0 & groombouts(:,5)==0 & groombouts(:,9)==1 & groombouts(:,6)==7 & groombouts(:,3)>10); - -timePost=60; -for bout = 1:length(bouts_to_consider) - response_to_groomGive_alignEnd{bout}= nan(size(neural_data,1),padding); - response_to_groomGive_alignEnd{bout}(:,end-groombouts(bouts_to_consider(bout),3)-timePost:end)= neural_data(:,groombouts(bouts_to_consider(bout),1):groombouts(bouts_to_consider(bout),2)+timePost); -end - -figure -imagesc(cell2mat(response_to_groomGive_alignEnd')) -caxis([-2 2]) -xline(padding-timePost) -colorbar -title("Groom give, align to end of bout") - -figure; hold on -plot(nanmean(cell2mat(response_to_groomGive_alignEnd'))) -xline(padding-timePost) -title("Groom give, align to end of bout") - -%Groom give start of bout -timePre=30; -for bout = 1:length(bouts_to_consider) - response_to_groomGive_alignStart{bout}= nan(size(neural_data,1),padding); - response_to_groomGive_alignStart{bout}(:,1:groombouts(bouts_to_consider(bout),3)+timePre)= neural_data(:,groombouts(bouts_to_consider(bout),1)-timePre+1:groombouts(bouts_to_consider(bout),2)); -end - -figure -imagesc(cell2mat(response_to_groomGive_alignStart')) -caxis([-2 2]) -xline(timePre) -colorbar -title("Groom give, align to start of bout") - -figure; hold on -plot(nanmean(cell2mat(response_to_groomGive_alignStart'))) -xline(30) -title("Groom give, align to start of bout") - -%Groom receive -bouts_to_consider=find(groombouts(:,4)==0 & groombouts(:,5)==0 & groombouts(:,6)==8); - -timePost=30; -for bout = 1:length(bouts_to_consider) - response_to_groomReceive_alignEnd{bout}= nan(size(neural_data,1),padding ); - response_to_groomReceive_alignEnd{bout}(:,end-groombouts(bouts_to_consider(bout),3)-timePost:end)= neural_data(:,groombouts(bouts_to_consider(bout),1):groombouts(bouts_to_consider(bout),2)+timePost); -end - -figure -imagesc(cell2mat(response_to_groomReceive_alignEnd')) -caxis([-2 2]) -xline(padding -timePost) -colorbar -title("Groom receive, align to end of bout") - -figure -plot(nanmean(cell2mat(response_to_groomReceive_alignEnd'))) -title("Groom receive, align to end of bout") - -%Groom receive start of bout -timePre=30; -for bout = 1:length(bouts_to_consider) - response_to_groomReceive_alignStart{bout}= nan(size(neural_data,1),padding); - response_to_groomReceive_alignStart{bout}(:,1:groombouts(bouts_to_consider(bout),3)+timePre)= neural_data(:,groombouts(bouts_to_consider(bout),1)-timePre+1:groombouts(bouts_to_consider(bout),2)); -end - -figure -imagesc(cell2mat(response_to_groomReceive_alignStart')) -caxis([-2 2]) -xline(timePre) -colorbar -title("Groom receive, align to start of bout") - -figure; hold on -plot(nanmean(cell2mat(response_to_groomReceive_alignStart'))) -xline(timePre) -title("Groom receive, align to start of bout") \ No newline at end of file diff --git a/Grooming analyses/old/log_crossValReg_groom_old.m b/Grooming analyses/old/log_crossValReg_groom_old.m deleted file mode 100644 index b01dac0..0000000 --- a/Grooming analyses/old/log_crossValReg_groom_old.m +++ /dev/null @@ -1,368 +0,0 @@ -%% log_crossValReg_groom - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -randomsample=0; -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 0; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. -threat_precedence =1; -exclude_sq=1; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - % a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - - Spike_count_raster = Spike_rasters'; - session_length(s) = size(Spike_count_raster,1); - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); - block_labels = cell2mat({labels{:,12}}'); - - - %% Interpolate short groom bouts - groomGive = zeros(size(behavior_labels)); - groomGive(behavior_labels== 7)=1; - groomGive_time = find(groomGive==1); - time_between_bouts = diff(groomGive_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGive_time(short_Runs(sr))+1:groomGive_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGive(idx_to_fill)=1; - end - - behavior_labels(find(groomGive==1))=7; - - %Groom get - groomGet = zeros(size(behavior_labels)); - groomGet(behavior_labels== 8)=1; - groomGet_time = find(groomGet==1); - time_between_bouts = diff(groomGet_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGet_time(short_Runs(sr))+1:groomGet_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGet(idx_to_fill)=1; - end - - behavior_labels(find(groomGet==1))=8; - -% behavior_labels_tosave{1,s} =behavior_labels'; -% block_labels_tosave{1,s} =block_labels'; - - %% Extract grooming stats (bout length and# per session) - - %GROOM GIVE - groomGive_bout_start = find(diff(groomGive)==1)+1; - groomGive_bout_end = find(diff(groomGive)==-1); - if length(groomGive_bout_end)0 - groomGive_bout(bout,5) = any(behavior_labels(groomGive_bout_start(bout)-30:groomGive_bout_start(bout)-1)==9 ... - |behavior_labels(groomGive_bout_start(bout)-30:groomGive_bout_start(bout)-1)==10); - else - groomGive_bout(bout,5)=0; - end - - % % % % %Check what the groom is followed by - % % % % if groomGive_bout_end(bout)+1200 - groomGet_bout(bout,5) = any(behavior_labels(groomGet_bout_start(bout)-30:groomGet_bout_start(bout)-1)==9 ... - |behavior_labels(groomGet_bout_start(bout)-30:groomGet_bout_start(bout)-1)==10); - else - groomGet_bout(bout,5) =0; - end - end - groomGet_bout(:,6)=8; - - % ALL GROOMS - allGroomBouts = [groomGet_bout; groomGive_bout]; - [~, idx_sorted] = sort(allGroomBouts(:,1)); - allGroomBouts_sorted = allGroomBouts(idx_sorted,:); - %Get inter-bout interval - allGroomBouts_sorted(:,7)=[0;(allGroomBouts_sorted(2:end,1)-allGroomBouts_sorted(1:end-1,2))]; - %Is previous bout grooming in the other direction - allGroomBouts_sorted(:,8) = abs([0; diff(allGroomBouts_sorted(:,6))]); - %Mark turn-taking bouts - allGroomBouts_sorted(:,9)=0; - allGroomBouts_sorted(find(allGroomBouts_sorted(:,8)==1 & allGroomBouts_sorted(:,7)<20),9)=1; - allGroomBouts_sorted(:,10)=s; - %7th column: inter-bout interval - %8th column: alternation of grooming bout - %9th column: is grooming bout turn-taking - - %% Extract grooming variables - - %Initialize variables which will be used to compute the different - %grooming metrics - behav_lbls=behavior_labels; - groom_behav_perSession = behavior_labels; - groomGive_perSession = behavior_labels; - groomGet_perSession = behavior_labels; - total_time=ones(size(behavior_labels)); - - %Groom give only - groomGive_perSession(groom_behav_perSession~=7)=0; groomGive_perSession(groom_behav_perSession==7)=1; - groomGiveBout_perSession=zeros(size(groomGive_perSession)); groomGiveBout_perSession(find(diff(groomGive_perSession)<0)+1)=1; - - %Groom receive only - groomGet_perSession(groom_behav_perSession~=8)=0; groomGet_perSession(groom_behav_perSession==8)=1; - groomGetBout_perSession=zeros(size(groomGive_perSession)); groomGetBout_perSession(find(diff(groomGet_perSession)<0)+1)=1; - - %Groom give or receive - groom_behav_perSession(groom_behav_perSession~=8 & groom_behav_perSession~=7)=0; - groom_behav_perSession(groom_behav_perSession==8)=1; groom_behav_perSession(groom_behav_perSession==7)=-1; - groomBout_perSession=zeros(size(groomGive_perSession)); groomBout_perSession(groomGetBout_perSession==1)=1; groomBout_perSession(groomGiveBout_perSession==1)=-1; - - %Cumulative grooming in the session (+1 if you get groomed, -1 if you groom) - cumul_groom_perSession = cumsum(groom_behav_perSession); figure; plot(cumul_groom_perSession) - cumul_groomBout_perSession= cumsum(groomBout_perSession); figure; plot(cumul_groomBout_perSession) - - %Total amount of grooming thus far in the session (cumulative) - total_groom_perSession = cumsum(abs(groom_behav_perSession)); figure; plot(total_groom_perSession) - total_groomBout_perSession = cumsum(abs(groomBout_perSession)); figure; plot(total_groomBout_perSession) - - %Total amount of groom give in the session thus far - cumul_give_perSession =cumsum(groomGive_perSession); figure; plot(cumul_give_perSession) - pGive_perSession=cumul_give_perSession./cumsum(total_time);figure; plot(pGive_perSession) - cumul_giveBouts_perSession = cumsum(groomGiveBout_perSession); figure; plot(cumul_giveBouts_perSession) - - %Total amount of groom receive in the session thus far - cumul_get_perSession =cumsum(groomGet_perSession); figure; plot(cumul_get_perSession) - pGet_perSession=cumul_get_perSession./cumsum(total_time);figure; plot(pGet_perSession) - cumul_getBouts_perSession = cumsum(groomGetBout_perSession); figure; plot(cumul_getBouts_perSession) - %pGive_perSession=cumul_give_perSession./total_groom_perSession;figure; plot(pGive_perSession) - - %Total reciprocity of groom duration - recip_groom_perSession = 1-(cumul_groom_perSession./total_groom_perSession);figure; plot(recip_groom_perSession) - recip_groomBout_perSession = 1-(cumul_groomBout_perSession./total_groomBout_perSession);figure; plot(recip_groomBout_perSession) - - - %% Predict observed grooming reciprocity from neural data - - neural_data = zscore(Spike_count_raster); - groom_idx = ismember(behavior_labels,[7,8]); - - %Predict observed reciprocity from neural data - X1= neural_data(groom_idx,:); - %[~, score]= pca(neural_data(groom_idx,:)); X1=score(:,1:10); - %y_obs = cumul_groom_obs(groom_idx)'; - y_obs = zscore(cumul_groom_obs(groom_idx)'); - %Test autocorrelation of variable you're trying to predict - %[acf, lags]=autocorr(y_obs,'NumLags',100); %HIGHLY autocorrelated - %y_obs = zscore(cumul_groom_slidingWindow_obs(groom_idx)'); - figure; plot(y_obs) - %y_obs =y_obs(1:1500); X1=X1(1:1500,:); - -% %Estimate autoregression? -% Mdl = arima(2,0,1); -% EstMdl = estimate(Mdl,y_obs) - - - %Ridge regression? - [yfit]=log_crossValModel(X1, y_obs, [1:size(X1,2)], [1:size(X1,2)], [1:size(X1,2)], 5); - - -% mdl = fitlm(X1,y_obs); -% yfit = predict(mdl,X1); Rsq_obs = corr(y_obs, yfit).^2; - - Rsq_obs = corr(y_obs, yfit').^2; % - yfit_smooth = movmean(yfit,50); - figure;hold on; plot(yfit); plot(yfit_smooth,'LineWidth',3); plot(y_obs,'LineWidth',3); legend('Prediction','Smoothed Prediction','Real') - - %Correlate observed reciprocity to individual firing rate - groom_give = nan(size(groom_all)); groom_give(groom_all==1)=2; - groom_receive = nan(size(groom_all)); groom_give(groom_all==-1)=2.1; - groom_give=groom_give(idx_all); groom_receive=groom_receive(idx_all); - - [~, pca_score]= pca(neural_data(idx_all,:)); X1=score(:,1:10); - for n= 1:size(X1,2)%1:10:200 - raw_activity(:,n) = pca_score(:,n); - smooth_activity(:,n) = movmean(raw_activity(:,n),25); - smooth_response = movmean(y_obs,50); - [correl_per_neuron(n) p(n)] = corr(raw_activity(:,n),y_obs); - -% figure; hold on; -% scatter(1:length(groom_give),groom_give,10,'b','filled'); -% scatter(1:length(groom_receive),groom_receive,10,'c','filled'); -% plot(smooth_activity(:,n)); - end - figure; histogram(correl_per_neuron) - n_of_interest = find(abs(correl_per_neuron)>0.3); - for n=1:length(n_of_interest) - figure; hold on; plot(smooth_activity(:,n_of_interest(n))); plot(y_obs); title(num2str(correl_per_neuron(n_of_interest(n)))) - end - - - %Randomized - for rand = 1:1000 - X1= neural_data(idx_all,:); - y = cumul_groom(idx_all(randsample(length(idx_all), length(idx_all))))'; %randomize response variable - mdl = fitlm(X1,y); - yfit = predict(mdl,X1); - %values = crossval(@regf,X1,y); - MAE_rand(rand) = mean(abs(yfit-y)); %mean(values(:,1));% - adjMAE_rand(rand) = MAE_obs/range(y); %mean(values(:,2));% - Rsq_rand(rand) = corr(y, yfit).^2; %mean(values(:,3)); % - disp(rand) - end - figure; histogram(adjMAE_rand(1:100)); xline(adjMAE_obs,'--r') - figure; histogram(Rsq_rand(1:100)); xline(Rsq_obs,'--r') - - - %Randomized, keeping statistics of the behavior - clear MAE_rand adjMAE_rand Rsq_rand correl_shuffle_real - rand_iter = 100; - Rsq_rand =nan(1,rand_iter); - for rand = 1:rand_iter - groom_bouts_perSession = allGroomBouts_sorted; - n_bouts = size(groom_bouts_perSession ,1); - duration_bouts = round(random('Exponential',120, 1,n_bouts)); - groom_bouts_perSession(:,3)=duration_bouts; -% groom_bouts_perSession(:,1)=sort(randsample(1:size(Spike_count_raster,1),n_bouts)); -% groom_bouts_perSession(:,2)=groom_bouts_perSession(:,1) + groom_bouts_perSession(:,3); - groom_bouts_perSession(:,6)=randsample(groom_bouts_perSession(:,6),length(groom_bouts_perSession(:,6)),'true'); - groom_all = zeros(1,size(neural_data,1)); - for bout = 1:size(groom_bouts_perSession,1) - - idx_bout{bout} = groom_bouts_perSession(bout,1):groom_bouts_perSession(bout,2); - if groom_bouts_perSession(bout,6) ==8 - groom_all(idx_bout{bout})=ones(1,length(idx_bout{bout})); - else - groom_all(idx_bout{bout}) =ones(1,length(idx_bout{bout}))*(-1); - end - end - idx_all = cell2mat(idx_bout); - cumul_groom{rand} = cumsum(groom_all); %figure; plot(cumul_groom{rand} (idx_all)) - correl_shuffle_real(rand)=corr(cumul_groom{rand} (idx_all)', y_obs); - - % % % %Simulate neural data - % % % mean_Hz = mean(Spike_count_raster); - % % % for n=1:size(Spike_count_raster,2) - % % % sim_neural_data(:,n) = poissrnd(mean_Hz(n),1,size(Spike_count_raster,1)); - % % % % [acf, lags]=autocorr(sim_neural_data(:,n)); - % % % % [acf, lags]=autocorr(Spike_count_raster(:,n)); - % % % %figure; hold on; plot(movmean(sim_neural_data(:,n),100)); plot(movmean(Spike_count_raster(:,n),100)) - % % % end - % % % sim_neural_data = zscore(sim_neural_data); - - %if correl_shuffle_real(rand)<0.1 - %return - X1= neural_data(idx_all,:); - %X1= sim_neural_data(idx_all,:); %use simulated neural data - %IMPORTANT NOTE: this simulated neural data does NOT have temporal - %auto-correlation - - %[~, score]= pca(neural_data(idx_all,:)); X1=score(:,1:10); - y = zscore(cumul_groom{rand} (idx_all)'); %[acf, lags]=autocorr(y); - [yfit]=log_crossValModel(X1, y, [1:size(X1,2)], [1:size(X1,2)], [1:size(X1,2)], 5); - Rsq_rand(rand) = corr(y, yfit').^2; %mean(values(:,3)); % - disp(rand) - %end - %clear cumul_groom yfit y - end - figure; histogram(Rsq_rand(~isnan(Rsq_rand)),10); xline(Rsq_obs,'--r','LineWidth',2) - %figure; histogram(Rsq_rand(abs(correl_shuffle_real)<0.2),10); xline(Rsq_obs,'--r') - figure; histogram(abs(correl_shuffle_real)); - figure; scatter(abs(correl_shuffle_real),Rsq_rand); [rho p]=corr(abs(correl_shuffle_real)',Rsq_rand') - - for t = find(Rsq_rand>0.4) - figure; - plot(cumul_groom{t}) - end - - - - - yfit_smooth = movmean(yfit,10); - figure;hold on; plot(yfit); plot(yfit_smooth,'LineWidth',3); plot(y,'LineWidth',3); legend('Prediction','Real') - - figure; histogram(MAE); xline(50,'--r') - sum(MAE<51)./length(MAE) -end - - diff --git a/Grooming analyses/old/log_grooming_behavior_old.m b/Grooming analyses/old/log_grooming_behavior_old.m deleted file mode 100644 index 3a52eb8..0000000 --- a/Grooming analyses/old/log_grooming_behavior_old.m +++ /dev/null @@ -1,617 +0,0 @@ -%% Log_grooming_behavior.m - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -randomsample=0; -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. -threat_precedence =1; -exclude_sq=1; -time_preGroom = 100; -time_postGroom = 100; -time_postThreat = 300; -time_postApproch = 60; - -%Set colormap -Cmap = [[1 0 0];...%Aggression; red - [1 0.4 0.1];...%Approach; dark orange - [0 0 0];...%But sniff; NA - [0.3 0.7 1];...%Drinking; light blue - [0 0.7 0];...%Foraging; dark green - [1 0 1];...%Groom sollicitation; magenta - [0 1 1];...%Groom partner; cyan - [0 0 1];...%Getting groomed; dark blue - [0.8 0 0];...%Threat to partner; dark red - [1 0 0];...%Threat to subject; red - [0.9 0.9 0];...%leave; dark yellow - [0 0 0];...%Lipsmack - [0.2 0.9 0.76];...%Masturbating; turquoise - [0.7 0 1];...%Mounting; light purple - [0.9 0.5 0];...%Other monkeys vocalize; orange - [1 0.8 0.1];...%Travel; yellow orange - [0 0 0];...%Proximity; NA - [0 0 0];...%Rowdy room; NA - [1 0 0];...%SP; NA - [1 0 0];...%SS; NA - [0.6314 0.5059 0.0118];...%Scratch; maroon - [0.5 0.2 0.5];...%Self-groom; dark purple - [ 1 0.07 0.65];...%Submission; dark pink - [0 0.4 0.5];...%Vocalzation; blue green - [0 0 0];...%Yawning; NA - [0.8 0.8 0.8]];%Rest; grey -Cmap_block = [[0.9 0.7 0.12];[0 0.6 0.8];[0.5 0 0];[0.8 0.8 0.8]]; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; event1=1; event2=1; event3=1; event4=1; event5=1; event6=1;event7=1; event8=1;event9=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - - Spike_count_raster{s} = Spike_rasters'; - session_length(s) = size(Spike_count_raster{s},1); - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); - block_labels = cell2mat({labels{:,12}}'); - - - %% Interpolate short groom bouts - groomGive = zeros(size(behavior_labels)); - groomGive(behavior_labels== 7)=1; - groomGive_time = find(groomGive==1); - time_between_bouts = diff(groomGive_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGive_time(short_Runs(sr))+1:groomGive_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGive(idx_to_fill)=1; - end - - behavior_labels(find(groomGive==1))=7; - - %Groom get - groomGet = zeros(size(behavior_labels)); - groomGet(behavior_labels== 8)=1; - groomGet_time = find(groomGet==1); - time_between_bouts = diff(groomGet_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGet_time(short_Runs(sr))+1:groomGet_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGet(idx_to_fill)=1; - end - - behavior_labels(find(groomGet==1))=8; - - - behavior_labels_tosave{1,s} =behavior_labels'; - block_labels_tosave{1,s} =block_labels'; - - %% Extract grooming stats (bout length and# per session) - - %GROOM GIVE - groomGive_bout_start = find(diff(groomGive)==1)+1; - groomGive_bout_end = find(diff(groomGive)==-1); - if length(groomGive_bout_end)0 - groomGive_bout(bout,5) = any(behavior_labels(groomGive_bout_start(bout)-60:groomGive_bout_start(bout)-1)==9 ... - |behavior_labels(groomGive_bout_start(bout)-60:groomGive_bout_start(bout)-1)==10); - else - groomGive_bout(bout,5)=0; - end - end - groomGive_bout(:,6)=7; - %4th column: was there a threat right after the groom (which could have - %cut it short) - %5th column: was there a threat preceding the grooming bout - %6th column, is grooming receive of give. - - %cut too short duratiin bouts (usually occurs because smth external - %cuts the bout (aggression) - %groomGive_bout(groomGive_bout(:,3)<10) - - %GROOM GET - groomGet_bout_start = find(diff(groomGet)==1)+1; - groomGet_bout_end = find(diff(groomGet)==-1); - groomGet_duration = groomGet_bout_end-groomGet_bout_start; - groomGet_bout=[groomGet_bout_start, groomGet_bout_end, groomGet_duration]; - - for bout = 1:length(groomGet_bout_end) - if groomGet_bout_end(bout)+110 - groomGet_bout(bout,5) = any(behavior_labels(groomGet_bout_start(bout)-60:groomGet_bout_start(bout)-1)==9 ... - |behavior_labels(groomGet_bout_start(bout)-60:groomGet_bout_start(bout)-1)==10); - else - groomGet_bout(bout,5) =0; - end - end - groomGet_bout(:,6)=8; - - % ALL GROOMS - allGroomBouts = [groomGet_bout; groomGive_bout]; - [~, idx_sorted] = sort(allGroomBouts(:,1)); - allGroomBouts_sorted = allGroomBouts(idx_sorted,:); - allGroomBouts_sorted(:,7)=[0;(allGroomBouts_sorted(2:end,1)-allGroomBouts_sorted(1:end-1,2))]; - allGroomBouts_sorted(:,8) = abs([0; diff(allGroomBouts_sorted(:,6))]); - - allGroomBouts_sorted_save{s}=allGroomBouts_sorted; - - %% Visualize sequences of event with grooming - behav = [find(behav_categ=="Groom sollicitation"),find(behav_categ=="Foraging"),... - find(behav_categ=="Groom partner"),find(behav_categ=="Getting groomed"),... - find(behav_categ=="Threat to partner"),find(behav_categ=="Threat to subject"),... - find(behav_categ=="approach")]; - %behav = [find(behav_categ=="Groom partner"),find(behav_categ=="Getting groomed")]; - behavior_labels_plot = behavior_labels; - behavior_labels_plot(find(~ismember(behavior_labels,behav)))=26; - block_labels_plot = block_labels; - block_labels_plot(find(~ismember(behavior_labels,behav)))=4; - behavior_labels_plotSave{s}=behavior_labels_plot'; - block_labels_plotSave{s}=block_labels_plot'; - %figure; imagesc(behavior_labels_plot'); colormap(Cmap); colorbar - - %% Get probablity of grooming after threat - - alone_block = find(strcmp(block_times.Behavior,"Alone.block")); - HIS_end = behavior_log.end_time(find(strcmp(behavior_log.Behavior,"HIS"))); - HIP_end = behavior_log.end_time(find(strcmp(behavior_log.Behavior,"HIP"))); - Sollicitation_end = behavior_log.end_time(find(strcmp(behavior_log.Behavior,"Grm prsnt"))); - Approach_end = behavior_log.end_time(find(strcmp(behavior_log.Behavior,"Approach"))); - shuffle = randsample(301:size(Spike_count_raster{s},1)-time_postThreat,100); - - %Threat to partner - for e=1:length(HIP_end) - if HIP_end(e)+time_postThreat1: more groom received than given - - %% Get probability of preceding events for grooming - - GroomGet_start = behavior_log.start_time(find(strcmp(behavior_log.Behavior,"Groom Receive"))); - GroomGive_start = behavior_log.start_time(find(strcmp(behavior_log.Behavior,"Groom Give"))); - - for e=1:length(GroomGet_start) - if GroomGet_start(e)-time_preGroom>1 - behav_pre_groomGet(event6,:) = behavior_labels(GroomGet_start(e)-time_preGroom+1: GroomGet_start(e)); - else - behav_pre_groomGet(event6,:) = nan(1,time_preGroom+1); - behav_pre_groomGet(event6,end-length(behavior_labels(1: GroomGet_start(e))) ... - +1:end) = behavior_labels(1: GroomGet_start(e)); - end - event6=event6+1; - - end - - for e=1:length(GroomGive_start) - if GroomGive_start(e)-time_preGroom>1 - behav_pre_groomGive(event7,:) = behavior_labels(GroomGive_start(e)-time_preGroom+1: GroomGive_start(e)); - else - behav_pre_groomGive(event7,:) = nan(1,time_preGroom); - behav_pre_groomGive(event7,end-length(behavior_labels(1: GroomGive_start(e)))+1:end) = behavior_labels(1: GroomGive_start(e)); - end - event7=event7+1; - - end - - %% Get probability of events following grooming - - GroomGet_end = behavior_log.end_time(find(strcmp(behavior_log.Behavior,"Groom Receive"))); - GroomGive_end = behavior_log.end_time(find(strcmp(behavior_log.Behavior,"Groom Give"))); - - for e=1:length(GroomGet_end) - if GroomGet_end(e)+time_postGroom3000,7)=nan; -figure; hold on -histogram(GroomBouts_across_sessions(GroomBouts_across_sessions(:,8)==1,7),200) -histogram(GroomBouts_across_sessions(GroomBouts_across_sessions(:,8)==0,7),200) -xlim([0 300]) - -%% Post-THREAT -%plot prob of grooming after a threat to subject event in paired blocks -figure; hold on -paired = sum(block_post_HIS~=3,2)~=0; -plot(nansum(behav_post_HIS(paired,:)==7)/size(behav_post_HIS(paired,:),1),'LineWidth',2) -plot(nansum(behav_post_HIS(paired,:)==8)/size(behav_post_HIS(paired,:),1),'LineWidth',2) -plot(nansum(behav_post_shuffle==7)/size(behav_post_shuffle,1),'LineWidth',2,'Color','k') -plot(nansum(behav_post_shuffle==8)/size(behav_post_shuffle,1),'LineWidth',2,'Color','k') -xlim([-20,time_postThreat]) -xline(0,'--') -legend("Groom give","Getting groomed","Shuffle") - -% % %plot prob of self-grooming after a threat to subject event in alone block -% % figure; hold on -% % alone = sum(block_post_HIS~=3,2)==0; -% % plot(nansum(behav_post_HIS(alone,:)==21)/size(behav_post_HIS(alone,:),1),'LineWidth',2) -% % plot(nansum(behav_post_HIS(alone,:)==22)/size(behav_post_HIS(alone,:),1),'LineWidth',2) -% % plot(nansum(behav_post_shuffle==22)/size(behav_post_shuffle,1),'LineWidth',2) -% % xlim([-20,300]) -% % xline(0,'--') -% % legend("Scratch","Self-groom") - -%plot prob of grooming after a threat to partner event in paired blocks -figure; hold on -paired = sum(block_post_HIP~=3,2)~=0; -plot(nansum(behav_post_HIP(paired,:)==7)/size(behav_post_HIP(paired,:),1),'LineWidth',2) -plot(nansum(behav_post_HIP(paired,:)==8)/size(behav_post_HIP(paired,:),1),'LineWidth',2) -%plot(nansum(behav_post_HIP(paired,:)==21)/size(behav_post_HIP(paired,:),1),'LineWidth',2) -plot(nansum(behav_post_shuffle==7)/size(behav_post_shuffle,1),'LineWidth',2) -xlim([-20,300]) -xline(0,'--') -legend("Groom give","Getting groomed","Shuffle") - -%% RECIPROCITY ACROSS ALL SESSIONS - -%Extract grooming reciprocity over the full course of the experiment -amos_GroomGive = sum(total_GroomGive(a_sessions)) -amos_GetGroom = sum(total_GetGroom(a_sessions)) -amos_reciprocity = reciprocity(a_sessions) -amos_total_reciprocity = 1-(amos_GroomGive-amos_GetGroom)/(amos_GroomGive+amos_GetGroom) - -hooke_GroomGive = sum(total_GroomGive(h_sessions)) -hooke_GetGroom = sum(total_GetGroom(h_sessions)) -hooke_reciprocity = reciprocity(h_sessions) -hooke_total_reciprocity = 1-(hooke_GroomGive-hooke_GetGroom)/(hooke_GroomGive+hooke_GetGroom) - -%% Get grooming transition probability -figure; histogram(behav_pre_groomGive,'Normalization','probability') -figure; hold on -idx=1:99; -plot(nansum(behav_pre_groomGive(:,idx)==8)/size(behav_pre_groomGive,1),'LineWidth',2) -plot(nansum(behav_pre_groomGive(:,idx)==7)/size(behav_pre_groomGive,1),'LineWidth',2) - - -figure; hold on -idx=1:99; -plot(nansum(behav_post_groomGive(:,idx)==8)/size(behav_post_groomGive,1),'LineWidth',2) -plot(nansum(behav_post_groomGive(:,idx)==7)/size(behav_post_groomGive,1),'LineWidth',2) - -%% plot grooming reciprocity over time, over the course of the experiment - -sessions = a_sessions; -%Amos -total_behav_amos = cell2mat(behavior_labels_tosave(sessions)); -total_behav_amos(total_behav_amos~=8 & total_behav_amos~=7)=0; -total_behav_amos(total_behav_amos==8)=1; total_behav_amos(total_behav_amos==7)=-1; -%If getting groom +1; if grooming partner: -1 -cum_groom_amos = cumsum(total_behav_amos); -sum_groom_amos = cumsum(abs(total_behav_amos)); -recip_groom_amos = 1+(cum_groom_amos./sum_groom_amos); - - -cum_GetGroom_amos=cum_groom_amos;cum_GetGroom_amos(find(total_behav_amos==0))=nan; -cum_GetGroom_amos(find(total_behav_amos==-1))=nan; -cum_GiveGroom_amos=cum_groom_amos;cum_GiveGroom_amos(find(total_behav_amos==0))=nan; -cum_GiveGroom_amos(find(total_behav_amos==1))=nan; - -% % % % groom_amos=cell2mat(behavior_labels_plotSave(a_sessions(s))); -% % % % groom_amos(groom_amos==7)=1;groom_amos(groom_amos==8)=2;groom_amos(groom_amos==26)=3; -% % % % Cmap=[0 1 1; 0 0 1; 0.9 0.9 0.9]; -% % % % figure; imagesc(groom_amos); colormap(Cmap) - -%plot absolute sum -figure; hold on; -plot(cum_groom_amos,'LineWidth',0.05,'Color','k') -plot(cum_GetGroom_amos,'LineWidth',2,'Color','b') -plot(cum_GiveGroom_amos,'LineWidth',2,'Color','c') -xline([cumsum(session_length(a_sessions))],'--k') -yline(0,'--r') -%plot reciprocity over time -figure; hold on; -plot(recip_groom_amos,'LineWidth',2) -xline([cumsum(session_length(a_sessions))],'--b') -ylim([0.8 1.2]) -yline(1,'--k') - - - -%% look whithin a session -time_integration = 100; -for s=1:length(session_range) - groom_behav_perSession = cell2mat(behavior_labels_tosave(session_range(s))); - behav_lbls=behavior_labels_tosave{session_range(s)}; - groom_behav_perSession(groom_behav_perSession~=8 & groom_behav_perSession~=7)=0; - groom_behav_perSession(groom_behav_perSession==8)=1; groom_behav_perSession(groom_behav_perSession==7)=-1; - cumul_groom_perSession{s} = cumsum(groom_behav_perSession); - total_groom_perSession{s} = cumsum(abs(groom_behav_perSession)); - recip_groom_perSession{s} = 1-abs((cumul_groom_perSession{s}./total_groom_perSession{s})); - - cum_GetGroom_amos_persesh{s}=cumul_groom_perSession{s};cum_GetGroom_amos_persesh{s}(find(groom_behav_perSession==0))=nan; - cum_GetGroom_amos_persesh{s}(find(groom_behav_perSession==-1))=nan; - cum_GiveGroom_amos_persesh{s}=cumul_groom_perSession{s};cum_GiveGroom_amos_persesh{s}(find(groom_behav_perSession==0))=nan; - cum_GiveGroom_amos_persesh{s}(find(groom_behav_perSession==1))=nan; - - cumul_groom_slidingWindow{s} = movsum(groom_behav_perSession,[time_integration 0],"omitnan"); - total_groom_slidingWindow{s} = movsum(abs(groom_behav_perSession),[time_integration 0],"omitnan"); - recip_groom_slidingWindow{s} = (cumul_groom_slidingWindow{s}./total_groom_slidingWindow{s}); - %recip_groom_slidingWindow{s}(total_groom_slidingWindow{s}<25)=nan; - - - figure; hold on; - subplot(2,1,1); hold on - plot(cumul_groom_perSession{s},'LineWidth',0.05,'Color','k') - plot(cum_GetGroom_amos_persesh{s},'LineWidth',2,'Color','b') - plot(cum_GiveGroom_amos_persesh{s},'LineWidth',2,'Color','c') - %xlim([0 6000]) - yline(0,'--r') - xline(find([abs(recip_groom_slidingWindow{s})<0.005]),'--k') - xline(find(behav_lbls==5),'-g') - xline(find(behav_lbls==9),'-m') - xline(find(behav_lbls==10),'-r') - - subplot(2,1,2); hold on - scatter(1:length(recip_groom_slidingWindow{s}),recip_groom_slidingWindow{s}) - yline(0,'--r') - %xlim([0 6000]) - xline(find([abs(recip_groom_slidingWindow{s})<0.005]),'--k') - xline(find(behav_lbls==5),'-g') - xline(find(behav_lbls==9),'-m') - xline(find(behav_lbls==10),'-r') - -end - -figure; hold on; -plot(cell2mat(cumul_groom_perSession),'LineWidth',0.05,'Color','k') -plot(cell2mat(cum_GetGroom_amos_persesh),'LineWidth',2,'Color','b') -plot(cell2mat(cum_GiveGroom_amos_persesh),'LineWidth',2,'Color','c') -yline(0,'--r') -xline([cumsum(session_length(a_sessions))],'--b') - -figure; hold -plot(cell2mat(recip_groom_perSession),'LineWidth',2,'Color','k') -yline(0,'--r') -xline([cumsum(session_length(a_sessions))],'--b') - -%% Get umap -[umap_result{s}]=run_umap(Spike_count_raster{s}, 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 3); %Run umap to get 2d embedded states -close - -figure; hold on; set(gcf,'Position',[150 250 1000 500]) - -behavior = behavior_labels_plotSave{s}; -block = block_labels_plotSave{s}; -groom_idx = find(behavior==7| behavior==8); -%Plot UMAP results color-coded by behavior -ax1=subplot(1,4,1); -scatter3(umap_result{s}(groom_idx,1), umap_result{s}(groom_idx,2),umap_result{s}(groom_idx,3),8,Cmap(behavior(groom_idx),:),'filled') -xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -%set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -title('Behavior') -set(gca,'FontSize',12); - - -%Color-coded by block -ax2=subplot(1,4,2); -scatter3(umap_result{s}(groom_idx,1), umap_result{s}(groom_idx,2),umap_result{s}(groom_idx,3),8,Cmap_block(block(groom_idx),:),'filled') -xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -%set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -title('Block') -set(gca,'FontSize',12); - - -caxis_upper = 1; -caxis_lower = -1; -cmap=flipud(cbrewer('div','RdBu', length(caxis_lower:0.01:caxis_upper))); -recip = recip_groom_slidingWindow{s}(groom_idx); %recip(recip<0.3)=0.3; -ax3=subplot(1,4,3); -scatter3(umap_result{s}(groom_idx,1), umap_result{s}(groom_idx,2),umap_result{s}(groom_idx,3),8,recip,'filled')%Cmap_recip(recip,:),'filled') -xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -colormap(cmap) -colorbar -%set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -title('Block') -set(gca,'FontSize',12); - -figure -recip = abs(recip_groom_slidingWindow{s}(groom_idx)); %recip(recip<0.3)=0.3; -scatter3(umap_result{s}(groom_idx,1), umap_result{s}(groom_idx,2),umap_result{s}(groom_idx,3),8,recip,'filled')%Cmap_recip(recip,:),'filled') -xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -colormap('parula') -colorbar -%set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -title('Block') -set(gca,'FontSize',12); - -% % Cmap_time=hot(size(Spike_count_raster{s},1)); -% % time=1:size(Spike_count_raster{s},1); -% % ax4=subplot(1,4,4); -% % scatter3(umap_result{s}(groom_idx,1), umap_result{s}(groom_idx,2),umap_result{s}(groom_idx,3),8,Cmap_time(time(groom_idx)',:),'filled') -% % xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -% % %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -% % title('Block') -% % set(gca,'FontSize',12); - -hlink = linkprop([ax1,ax2,ax3, ax4],{'CameraPosition','CameraUpVector'}); -rotate3d on -grid on - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\\]]]]]]]]]]]]]]]` -%Hooke -total_behav_hooke = cell2mat(behavior_labels_tosave(h_sessions)); -total_behav_hooke(total_behav_hooke~=8 & total_behav_hooke~=7)=0; -total_behav_hooke(total_behav_hooke==8)=1; total_behav_hooke(total_behav_hooke==7)=-1; -cum_groom_hooke = cumsum(total_behav_hooke); -sum_groom_hooke = cumsum(abs(total_behav_hooke)); -recip_groom_hooke = 1+ cum_groom_hooke./sum_groom_hooke; - - -cum_GetGroom_hooke=cum_groom_hooke;cum_GetGroom_hooke(find(total_behav_hooke==0))=nan; -cum_GetGroom_hooke(find(total_behav_hooke==-1))=nan; -cum_GiveGroom_hooke=cum_groom_hooke;cum_GiveGroom_hooke(find(total_behav_hooke==0))=nan; -cum_GiveGroom_hooke(find(total_behav_hooke==1))=nan; - -groom_hooke=cell2mat(behavior_labels_plotSave(h_sessions)); -groom_hooke(groom_hooke==7)=1;groom_hooke(groom_hooke==8)=2;groom_hooke(groom_hooke==26)=3; -Cmap=[0 1 1; 0 0 1; 0.9 0.9 0.9]; -figure; imagesc(groom_hooke); colormap(Cmap) - -figure; hold on; -plot(cum_groom_hooke,'LineWidth',0.05,'Color','k') -plot(cum_GetGroom_hooke,'LineWidth',2,'Color','b') -plot(cum_GiveGroom_hooke,'LineWidth',2,'Color','c') -xline([cumsum(session_length(a_sessions))],'--k') -yline(0,'--r') - - -figure; hold on; -plot(recip_groom_hooke,'LineWidth',2) -xline([cumsum(session_length(h_sessions))],'--b') -ylim([0 2]) -yline(1,'--k') - diff --git a/Grooming analyses/old/log_grooming_umap_old.m b/Grooming analyses/old/log_grooming_umap_old.m deleted file mode 100644 index 5c0d3c4..0000000 --- a/Grooming analyses/old/log_grooming_umap_old.m +++ /dev/null @@ -1,446 +0,0 @@ -%% Log_grooming_behavior.m - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -randomsample=0; -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. -threat_precedence =1; -exclude_sq=1; -time_preGroom = 100; -time_postGroom = 100; -time_postThreat = 300; -time_postApproch = 60; - -%Set colormap -Cmap = [[1 0 0];...%Aggression; red - [1 0.4 0.1];...%Approach; dark orange - [0 0 0];...%But sniff; NA - [0.3 0.7 1];...%Drinking; light blue - [0 0.7 0];...%Foraging; dark green - [1 0 1];...%Groom sollicitation; magenta - [0 1 1];...%Groom partner; cyan - [0 0 1];...%Getting groomed; dark blue - [0.8 0 0];...%Threat to partner; dark red - [1 0 0];...%Threat to subject; red - [0.9 0.9 0];...%leave; dark yellow - [0 0 0];...%Lipsmack - [0.2 0.9 0.76];...%Masturbating; turquoise - [0.7 0 1];...%Mounting; light purple - [0.9 0.5 0];...%Other monkeys vocalize; orange - [1 0.8 0.1];...%Travel; yellow orange - [0 0 0];...%Proximity; NA - [0 0 0];...%Rowdy room; NA - [1 0 0];...%SP; NA - [1 0 0];...%SS; NA - [0.6314 0.5059 0.0118];...%Scratch; maroon - [0.5 0.2 0.5];...%Self-groom; dark purple - [ 1 0.07 0.65];...%Submission; dark pink - [0 0.4 0.5];...%Vocalzation; blue green - [0 0 0];...%Yawning; NA - [0.8 0.8 0.8]];%Rest; grey -Cmap_block = [[0.9 0.7 0.12];[0 0.6 0.8];[0.5 0 0];[0.8 0.8 0.8]]; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; -% a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - - Spike_count_raster{s} = Spike_rasters'; - session_length(s) = size(Spike_count_raster{s},1); - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); - block_labels = cell2mat({labels{:,12}}'); - - - - %% Interpolate short groom bouts - groomGive = zeros(size(behavior_labels)); - groomGive(behavior_labels== 7)=1; - groomGive_time = find(groomGive==1); - time_between_bouts = diff(groomGive_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGive_time(short_Runs(sr))+1:groomGive_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGive(idx_to_fill)=1; - end - - behavior_labels(find(groomGive==1))=7; - - %Groom get - groomGet = zeros(size(behavior_labels)); - groomGet(behavior_labels== 8)=1; - groomGet_time = find(groomGet==1); - time_between_bouts = diff(groomGet_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGet_time(short_Runs(sr))+1:groomGet_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGet(idx_to_fill)=1; - end - - behavior_labels(find(groomGet==1))=8; - - behavior_labels_tosave{1,s} =behavior_labels'; - block_labels_tosave{1,s} =block_labels'; - - - - %% Extract grooming stats (bout length and# per session) - - %GROOM GIVE - groomGive_bout_start = find(diff(groomGive)==1)+1; - groomGive_bout_end = find(diff(groomGive)==-1); - if length(groomGive_bout_end)0 - groomGive_bout(bout,5) = any(behavior_labels(groomGive_bout_start(bout)-30:groomGive_bout_start(bout)-1)==9 ... - |behavior_labels(groomGive_bout_start(bout)-30:groomGive_bout_start(bout)-1)==10); - else - groomGive_bout(bout,5)=0; - end - end - groomGive_bout(:,6)=7; - %4th column: was there a threat right after the groom (which could have - %cut it short) - %5th column: was there a threat preceding the grooming bout - %6th column, grooming receive or give. - - %cut too short duratiin bouts (usually occurs because smth external - %cuts the bout (aggression) - %groomGive_bout(groomGive_bout(:,3)<10) - - %GROOM GET - groomGet_bout_start = find(diff(groomGet)==1)+1; - groomGet_bout_end = find(diff(groomGet)==-1); - groomGet_duration = groomGet_bout_end-groomGet_bout_start; - groomGet_bout=[groomGet_bout_start, groomGet_bout_end, groomGet_duration]; - - for bout = 1:length(groomGet_bout_end) - if groomGet_bout_end(bout)+110 - groomGet_bout(bout,5) = any(behavior_labels(groomGet_bout_start(bout)-30:groomGet_bout_start(bout)-1)==9 ... - |behavior_labels(groomGet_bout_start(bout)-30:groomGet_bout_start(bout)-1)==10); - else - groomGet_bout(bout,5) =0; - end - end - groomGet_bout(:,6)=8; - - % ALL GROOMS - allGroomBouts = [groomGet_bout; groomGive_bout]; - [~, idx_sorted] = sort(allGroomBouts(:,1)); - allGroomBouts_sorted = allGroomBouts(idx_sorted,:); - %Get inter-bout interval - allGroomBouts_sorted(:,7)=[0;(allGroomBouts_sorted(2:end,1)-allGroomBouts_sorted(1:end-1,2))]; - %Is previous bout grooming in the other direction - allGroomBouts_sorted(:,8) = abs([0; diff(allGroomBouts_sorted(:,6))]); - %Mark turn-taking bouts - allGroomBouts_sorted(:,9)=0; - allGroomBouts_sorted(find(allGroomBouts_sorted(:,8)==1 & allGroomBouts_sorted(:,7)<20),9)=1; - allGroomBouts_sorted(:,10)=s; - %7th column: inter-bout interval - %8th column: alternation of grooming bout - %9th column: is grooming bout turn-taking - - allGroomBouts_sorted_save{s}=allGroomBouts_sorted; - -end - - -s=1; -behav=[7,8]; -behavior_labels = behavior_labels_tosave{s}; -block = block_labels_tosave{s}; -groombouts = allGroomBouts_sorted_save{s}; -groom_idx = find(ismember(behavior_labels,behav)); -neural_data = zscore(Spike_count_raster{s}); -time = 1:length(behavior_labels); - - -%% UMAP -%groom_idx = find(ismember(behavior_labels,behav) & ismember(block,1)); -[umap_result{s}]=run_umap(neural_data(groom_idx,:), 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 3); %Run umap to get 2d embedded states -[~, score]=pca(neural_data(groom_idx,:)); -close - - -%find corresponding indices of groom bout start and end in subsampled data -[idx,loc] = ismember(groombouts(:,1),groom_idx); -groombouts(:,11)=loc; -[idx,loc] = ismember(groombouts(:,2),groom_idx); -groombouts(:,12)=loc; - -%Create label vector for umap plotting -bouts_to_consider = 1:size(groombouts,1); -idx_all=[];timescale_all=[]; boutid_all=[]; -for b=1:length(bouts_to_consider) - - idx = groombouts(bouts_to_consider(b),11):groombouts(bouts_to_consider(b),12); - timescale = rescale(idx); - bout_id = ones(size(idx))*b; - - idx_all = [idx_all, idx]; - timescale_all = [timescale_all, timescale]; - boutid_all = [boutid_all, bout_id]; - -end -idx_final = idx_all;%(1:find(diff(idx_all)>400)); -timescale_final = timescale_all;%(1:find(diff(idx_all)>400)); -boutid_final = boutid_all;%(1:find(diff(idx_all)>400)); - - -%% Plot UMAPs -figure; -scatter3(umap_result{s}(:,1), umap_result{s}(:,2),umap_result{s}(:,3),10,Cmap(behavior_labels(groom_idx),:),'filled') -xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -%set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -set(gca,'FontSize',12); -title('Behavior') - -figure; -scatter3(umap_result{s}(:,1), umap_result{s}(:,2),umap_result{s}(:,3),10,Cmap_block(block(groom_idx),:),'filled') -xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -%set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -set(gca,'FontSize',12); -title('Neighbor ID') - -figure -scatter3(umap_result{s}(idx_final,1), umap_result{s}(idx_final,2),umap_result{s}(idx_final,3),10,boutid_final,'filled')%Cmap_recip(recip,:),'filled') -xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -colormap(colorcube) -colorbar -%set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -set(gca,'FontSize',12); -title('Bout Number') - -figure -scatter3(umap_result{s}(idx_final,1), umap_result{s}(idx_final,2),umap_result{s}(idx_final,3),10,time(idx_final),'filled')%Cmap_recip(recip,:),'filled') -xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -colormap(hot) -colorbar -%set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -set(gca,'FontSize',12); -title('Time in session') - - -%% Plot trajectory of grooming bouts - -for b=1:length(bouts_to_consider) - idx = groombouts(bouts_to_consider(b),10):groombouts(bouts_to_consider(b),11); - timescale = rescale(idx); - bout_id = ones(size(idx))*b; - threat_id = ones(size(idx))*groombouts(bouts_to_consider(b),5)+1; - recip_id = ones(size(idx))*groombouts(bouts_to_consider(b),9)+1; - - - %plot3() - figure; hold on - plot3(smoothdata(umap_result{s}(idx,1)), smoothdata(umap_result{s}(idx,2)),smoothdata(umap_result{s}(idx,3))) - scatter3(smoothdata(umap_result{s}(idx,1)), smoothdata(umap_result{s}(idx,2)),smoothdata(umap_result{s}(idx,3)),40,timescale,'filled')%Cmap_recip(recip,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - colormap(hot) - colorbar - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - set(gca,'FontSize',12); - title('Time in bout') - pause(1) - - figure; hold on - plot3(umap_result{s}(idx,1), umap_result{s}(idx,2),umap_result{s}(idx,3)) - scatter3(umap_result{s}(idx,1), umap_result{s}(idx,2),umap_result{s}(idx,3),40,recip_id,'filled')%Cmap_recip(recip,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - colormap(cool); caxis([1 2]) - colorbar - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - set(gca,'FontSize',12); - title('Time in bout') - pause(1) - -end - -figure -scatter3(umap_result{s}(idx_final,1), umap_result{s}(idx_final,2),umap_result{s}(idx_final,3),20,threatid_final,'filled')%Cmap_recip(recip,:),'filled') -xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -colormap(parula) -colorbar -%set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -set(gca,'FontSize',12); -title('Post-Threat') - -figure -scatter3(umap_result{s}(idx_final,1), umap_result{s}(idx_final,2),umap_result{s}(idx_final,3),20,recipid_final,'filled')%Cmap_recip(recip,:),'filled') -xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -colormap(summer) -colorbar -%set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -set(gca,'FontSize',12); -title('Reciprocal') - -%% activity aligned to end of grooming - -s=1; -groombouts = allGroomBouts_sorted_save{s}; -groombouts = groombouts(groombouts(:,3)>10,:); -neural_data = zscore(Spike_count_raster{s}); -padding = 500; - -num_neurons = size(neural_data,1); -num_trials = size(groombouts,1); -min_duration = min(groombouts(:,3)); -max_duration = max(groombouts(:,3)); - -for bout = 1:size(groombouts,1) - firing_rates{bout} = neural_data(groombouts(bout,1):groombouts(bout,2),:); -end - -% Find the median duration of behavior A -median_duration = round(median(cellfun(@(x) size(x, 1), firing_rates))); - -% Time-warp the firing rates to the median duration -time_warped_firing_rates = cellfun(@(x) interp1(1:size(x, 1), x, linspace(1, size(x, 1), median_duration)), firing_rates, 'UniformOutput', false); - -% Compute the average firing rate per neuron during behavior A -average_firing_rate = mean(cell2mat(reshape(time_warped_firing_rates, 1, 1, [])), 3); - -[isort1_all] = mapTmap(average_firing_rate); -figure; imagesc(average_firing_rate'); colorbar - -%Groom give end of bout -bouts_to_consider=1:size(groombouts,1); %find(groombouts(:,4)==0 & groombouts(:,5)==0 & groombouts(:,9)==1 & groombouts(:,6)==7 & groombouts(:,3)>10); - -timePost=60; -for bout = 1:length(bouts_to_consider) - response_to_groomGive_alignEnd{bout}= nan(size(neural_data,1),padding); - response_to_groomGive_alignEnd{bout}(:,end-groombouts(bouts_to_consider(bout),3)-timePost:end)= neural_data(:,groombouts(bouts_to_consider(bout),1):groombouts(bouts_to_consider(bout),2)+timePost); -end - -figure -imagesc(cell2mat(response_to_groomGive_alignEnd')) -caxis([-2 2]) -xline(padding-timePost) -colorbar -title("Groom give, align to end of bout") - -figure; hold on -plot(nanmean(cell2mat(response_to_groomGive_alignEnd'))) -xline(padding-timePost) -title("Groom give, align to end of bout") - -%Groom give start of bout -timePre=30; -for bout = 1:length(bouts_to_consider) - response_to_groomGive_alignStart{bout}= nan(size(neural_data,1),padding); - response_to_groomGive_alignStart{bout}(:,1:groombouts(bouts_to_consider(bout),3)+timePre)= neural_data(:,groombouts(bouts_to_consider(bout),1)-timePre+1:groombouts(bouts_to_consider(bout),2)); -end - -figure -imagesc(cell2mat(response_to_groomGive_alignStart')) -caxis([-2 2]) -xline(timePre) -colorbar -title("Groom give, align to start of bout") - -figure; hold on -plot(nanmean(cell2mat(response_to_groomGive_alignStart'))) -xline(30) -title("Groom give, align to start of bout") - -%Groom receive -bouts_to_consider=find(groombouts(:,4)==0 & groombouts(:,5)==0 & groombouts(:,6)==8); - -timePost=30; -for bout = 1:length(bouts_to_consider) - response_to_groomReceive_alignEnd{bout}= nan(size(neural_data,1),padding ); - response_to_groomReceive_alignEnd{bout}(:,end-groombouts(bouts_to_consider(bout),3)-timePost:end)= neural_data(:,groombouts(bouts_to_consider(bout),1):groombouts(bouts_to_consider(bout),2)+timePost); -end - -figure -imagesc(cell2mat(response_to_groomReceive_alignEnd')) -caxis([-2 2]) -xline(padding -timePost) -colorbar -title("Groom receive, align to end of bout") - -figure -plot(nanmean(cell2mat(response_to_groomReceive_alignEnd'))) -title("Groom receive, align to end of bout") - -%Groom receive start of bout -timePre=30; -for bout = 1:length(bouts_to_consider) - response_to_groomReceive_alignStart{bout}= nan(size(neural_data,1),padding); - response_to_groomReceive_alignStart{bout}(:,1:groombouts(bouts_to_consider(bout),3)+timePre)= neural_data(:,groombouts(bouts_to_consider(bout),1)-timePre+1:groombouts(bouts_to_consider(bout),2)); -end - -figure -imagesc(cell2mat(response_to_groomReceive_alignStart')) -caxis([-2 2]) -xline(timePre) -colorbar -title("Groom receive, align to start of bout") - -figure; hold on -plot(nanmean(cell2mat(response_to_groomReceive_alignStart'))) -xline(timePre) -title("Groom receive, align to start of bout") \ No newline at end of file diff --git a/Linear decoding/log_SVM_GroomContext_batch.m b/Linear decoding/log_SVM_GroomContext_batch.m deleted file mode 100644 index 7b18469..0000000 --- a/Linear decoding/log_SVM_GroomContext_batch.m +++ /dev/null @@ -1,310 +0,0 @@ -%% Log_SVM_grooming_batch -% Run a linear decoder on a the neural activity for different grooming contexts -% This script allows to decode grooming: -% 1. Start vs. end -% 2. Post-threat or not -% 3. Reciprocated or not -% 4. Initiated or not -% 5. Neighbor ID -% Batch version to run across sessions. -%Camille Testard, March 2022 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -randomsample=0; -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. -threat_precedence =1; -exclude_sq=1; - -%Initialize -% mean_hitrate = cell(length(sessions),3); -% sd_hitrate = cell(length(sessions),3); -% mean_hitrate_shuffled = cell(length(sessions),3); - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan=1; - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - - %% Visualize sequences of event with grooming - behav = [find(behav_categ=="Groom sollicitation"),find(behav_categ=="Foraging"),... - find(behav_categ=="Groom partner"),find(behav_categ=="Getting groomed"),... - find(behav_categ=="Threat to partner"),find(behav_categ=="Threat to subject"),... - find(behav_categ=="approach")]; - behavior_labels_plot = behavior_labels; - behavior_labels_plot(find(~ismember(behavior_labels,behav)))=26; - figure; imagesc(behavior_labels_plot'); xline(block_times.start_time(2)*temp_resolution,'LineWidth',4); xline(block_times.start_time(3)*temp_resolution,'LineWidth',4); colormap(Cmap); colorbar - - - %% Decode grooming context for groom give and groom receive separately - %Beginning vs. end of grooming bout - %Grooming post-threat vs not. - %Grooming after reciprocation or not - %Groom received after grm Prsnt or not - - groom_categ_label = {'Star.vs.end', 'Post-threat','Reciprocated','Initiated','Neighbor ID'}; %label grooming categ - - beh = 1; - for behav = [7,8] %For both groom give and groom receive - - %behav = [7,8]; - groom_behav={'Give','Receive'}; - - for groom_categ = 2:5 %For all grooming contexts - %Note: after threat, almost always groom RECEIVE, not given by - %subject. Also, grooming after groom present is only for groom - %RECEIVE. - - groom_labels = groom_labels_all(:,groom_categ+1); - - - %% Select behaviors to decode - - % Select behaviors with a minimum # of occurrences - %behav =[find(matches(behav_categ,'Groom Give'))]; %find(matches(behav_categ,'Groom Give')), - - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = groom_labels(idx,:);%Same as above but in behavior labels - - %If groom label is start vs. end - if groom_categ==1 - idx_epoch = find(~ismember(behavior_labels_final,3)); %remove middle chunk - Spike_count_raster_final = Spike_count_raster_final(idx_epoch,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels_final(idx_epoch,:);%Same as above but in behavior labels - end - - behav_size=tabulate(behavior_labels_final); - disp('########################') - tabulate(behavior_labels_final) - disp('########################') - - channel = char(channel_flag); - % disp('****************************************************************************') - % disp(['Groom categ: ' groom_categ_label{groom_categ} ', Channels: ' channel ', Behavior: Groom ' groom_behav{beh}]) - % disp('****************************************************************************') - - %pause(5) - if all(behav_size(:,2)>=30) && length(behav_size(:,2))>1 %If there are at least 30 occurrence of grooming in context 'b' - - %Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - %subsample to match number of neurons across brain areas - Labels = behavior_labels_final; - if randomsample==1 && channel_flag~="all" - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - if min(num_trials)<50 %If there are less than 50 instances for a given behavior - minNumTrials = min(num_trials); %use the minimum # of instances - else - minNumTrials = 50; %impose 50 occurrences per category - end - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %end of SVM iterations - - mean_hitrate(beh,groom_categ,chan,s) = mean(hitrate); - sd_hitrate(beh,groom_categ,chan,s) = std(hitrate); - mean_hitrate_shuffled(beh,groom_categ,chan,s) = mean(hitrate_shuffled); - sd_hitrate_shuffled = std(hitrate_shuffled); - - else - - mean_hitrate(beh,groom_categ,chan,s) = nan; - sd_hitrate(beh,groom_categ,chan,s) = nan; - - end % End of "min number of grooming of category b" clause - - clear labels_id - - end %End of grooming context loop - beh = beh+1; - - end %End of give vs. receive for loop - - chan = chan +1; - end %End of channel for loop - - %% Plotting results decoding accuracy for grooming context - -% figure; set(gcf,'Position',[150 250 1300 400]) -% subplot(1,2,1);hold on; %Groom Give -% for c = 1:3 -% y = mean_hitrate(1,2:5,c,s); -% std_dev = sd_hitrate(1,2:5,c,s); -% scatter(1:4,y,60,'filled', 'MarkerFaceAlpha',0.7) -% %errorbar(y,std_dev,'s') -% end -% leg = legend(["vlPFC","TEO","All"],'Location','best'); -% title(leg,'Brain Area') -% chance_level = 1/2; -% yline(chance_level,'--','Chance level', 'FontSize',16) -% xticks([1:4]); xlim([0.8 4.2]); ylim([0.4 1]) -% xticklabels(groom_categ_label(2:5)) -% ax = gca; -% ax.FontSize = 14; -% ylabel('Mean decoding accuracy','FontSize', 18); xlabel('Grooming Give Context','FontSize', 18) -% title('Decoding accuracy for the context of groom give','FontSize', 14) -% -% subplot(1,2,2);hold on %Groom Receive -% for c = 1:3 -% y = mean_hitrate(2,2:5,c,s); -% std_dev = sd_hitrate(2,2:5,c,s); -% scatter(1:4,y,60,'filled', 'MarkerFaceAlpha',0.7) -% %errorbar(y,std_dev,'s') -% end -% leg = legend(["vlPFC","TEO","All"],'Location','best'); -% title(leg,'Brain Area') -% chance_level = 1/2; -% yline(chance_level,'--','Chance level', 'FontSize',16) -% xticks([1:4]); xlim([0.8 4.2]); ylim([0.4 1]) -% xticklabels(groom_categ_label(2:5)) -% ax = gca; -% ax.FontSize = 14; -% ylabel('Mean decoding accuracy','FontSize', 18); xlabel('Grooming Receive Context','FontSize', 18) -% title('Decoding accuracy for the context of groom receive','FontSize', 14) -% -% cd(savePath) -% saveas(gcf,['Decoding grooming given context.png']) - close all - - disp('Session done') - -end %End of session for loop - -%Change savePath for all session results folder: - cd('~/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/') -save('SVM_results_groomingCateg.mat', "mean_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ","home") -load('SVM_results_groomingCateg.mat') - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -mean_hitrate(mean_hitrate==0)=nan; -mean_hitrate_shuffled(mean_hitrate_shuffled==0)=nan; - -figure; hold on; set(gcf,'Position',[150 250 1000 700]); b=1; -for groomcat = 2:5 - - subplot(2,2,b); hold on - - %Note: beh--> 1 = grooming partner; 2=getting groomed - %groomcat --> 1 = Star.vs.end; 2=Post-threat; 3=Reciprocated; 4=Initiated; 5=Neighbor ID - - % plot decoding context for getting groomed - % except for grooming initiation which should be groom partner (getting - % groomed is too much initiated by one single individual) - if groomcat == 4 - data = squeeze(mean_hitrate(1,groomcat,:,:))'; - data_shuffled = squeeze(mean_hitrate_shuffled(1,groomcat,:,:))'; - else - data = squeeze(mean_hitrate(2,groomcat,:,:))'; - data_shuffled = squeeze(mean_hitrate_shuffled(2,groomcat,:,:))'; - end - - bp = bar([nanmean(data); nanmean(data_shuffled)],'FaceAlpha',0.2); - - sp1 = scatter(ones(size(data,1))*0.78,data(:,1), 'filled','b'); - sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); - sp1 = scatter(ones(size(data,1))*1.22,data(:,3), 'filled','y'); - - sp1 = scatter(ones(size(data,1))*1.78,data_shuffled(:,1), 'filled','b'); - sp1 = scatter(ones(size(data,1))*2,data_shuffled(:,2), 'filled','r'); - sp1 = scatter(ones(size(data,1))*2.22,data_shuffled(:,3), 'filled','y'); - - legend(bp,{'vlPFC','TEO','all'},'Location','best') - - ylabel('Decoding Accuracy'); ylim([0.45 1]) - xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) - ax = gca; - ax.FontSize = 16; - - title(groom_categ_label(groomcat)) - b=b+1; - -end \ No newline at end of file diff --git a/Linear decoding/log_SVM_GroomContext_batch_shuffle.m b/Linear decoding/log_SVM_GroomContext_batch_shuffle.m deleted file mode 100644 index 7651bc6..0000000 --- a/Linear decoding/log_SVM_GroomContext_batch_shuffle.m +++ /dev/null @@ -1,356 +0,0 @@ -%% Log_SVM_grooming_batch -% Run a linear decoder on a the neural activity for different grooming contexts -% This script allows to decode grooming: -% 1. Start vs. end -% 2. Post-threat or not -% 3. Reciprocated or not -% 4. Initiated or not -% 5. Neighbor ID -% Batch version to run across sessions. -%Camille Testard, March 2022 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -randomsample=0; -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 10;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. -threat_precedence =1; -exclude_sq=1; - -%Initialize -% mean_hitrate = cell(length(sessions),3); -% sd_hitrate = cell(length(sessions),3); -% mean_hitrate_shuffled = cell(length(sessions),3); - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan=1; - for channel_flag = ["all"] - - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - session_length = size(Spike_count_raster ,1); - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - - %Load cumul groom across all sessions - cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/']) - load('ZCumulGroom_AcrossSessions.mat') - cumul_groom_over_all_sessions = cumul_groom_AcrossSessions{s}; - - - %% Interpolate short groom bouts - groomGive = zeros(size(behavior_labels)); - groomGive(behavior_labels== 7)=1; - groomGive_time = find(groomGive==1); - time_between_bouts = diff(groomGive_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGive_time(short_Runs(sr))+1:groomGive_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGive(idx_to_fill)=1; - end - - behavior_labels(find(groomGive==1))=7; - - %Groom get - groomGet = zeros(size(behavior_labels)); - groomGet(behavior_labels== 8)=1; - groomGet_time = find(groomGet==1); - time_between_bouts = diff(groomGet_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGet_time(short_Runs(sr))+1:groomGet_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGet(idx_to_fill)=1; - end - - behavior_labels(find(groomGet==1))=8; - - %% Extract grooming stats (bout length and# per session) - - %GROOM GIVE - groomGive_bout_start = find(diff(groomGive)==1)+1; - groomGive_bout_end = find(diff(groomGive)==-1); - if length(groomGive_bout_end)0 - groomGive_bout(bout,5) = any(behavior_labels(groomGive_bout_start(bout)-30:groomGive_bout_start(bout)-1)==9 ... - |behavior_labels(groomGive_bout_start(bout)-30:groomGive_bout_start(bout)-1)==10); - else - groomGive_bout(bout,5)=0; - end - end - groomGive_bout(:,6)=7; - %4th column: was there a threat right after the groom (which could have - %cut it short) - %5th column: was there a threat preceding the grooming bout - %6th column, grooming receive or give. - - %cut too short duratiin bouts (usually occurs because smth external - %cuts the bout (aggression) - %groomGive_bout(groomGive_bout(:,3)<10) - - %GROOM GET - groomGet_bout_start = find(diff(groomGet)==1)+1; - groomGet_bout_end = find(diff(groomGet)==-1); - groomGet_duration = groomGet_bout_end-groomGet_bout_start; - groomGet_bout=[groomGet_bout_start, groomGet_bout_end, groomGet_duration]; - - for bout = 1:length(groomGet_bout_end) - if groomGet_bout_end(bout)+110 - groomGet_bout(bout,5) = any(behavior_labels(groomGet_bout_start(bout)-30:groomGet_bout_start(bout)-1)==9 ... - |behavior_labels(groomGet_bout_start(bout)-30:groomGet_bout_start(bout)-1)==10); - else - groomGet_bout(bout,5) =0; - end - end - groomGet_bout(:,6)=8; - - % ALL GROOMS - allGroomBouts = [groomGet_bout; groomGive_bout]; - [~, idx_sorted] = sort(allGroomBouts(:,1)); - allGroomBouts_sorted = allGroomBouts(idx_sorted,:); - allGroomBouts_sorted(:,7)=[0;(allGroomBouts_sorted(2:end,1)-allGroomBouts_sorted(1:end-1,2))]; - allGroomBouts_sorted(:,8) = abs([0; diff(allGroomBouts_sorted(:,6))]); - allGroomBouts_sorted(:,9)=0; - allGroomBouts_sorted(find(allGroomBouts_sorted(:,8)==1 & allGroomBouts_sorted(:,7)<20),9)=1; - - %% look whithin a session - time_integration =300; - groom_behav_perSession = behavior_labels; - groom_behav_perSession(groom_behav_perSession~=8 & groom_behav_perSession~=7)=0; - groom_behav_perSession(groom_behav_perSession==8)=1; groom_behav_perSession(groom_behav_perSession==7)=-1; - cumul_groom_perSession = cumsum(groom_behav_perSession); - total_groom_perSession = cumsum(abs(groom_behav_perSession)); - recip_groom_perSession = 1-abs((cumul_groom_perSession./total_groom_perSession)); - - cumul_groom_slidingWindow = movsum(groom_behav_perSession,[time_integration 0],"omitnan"); - total_groom_slidingWindow = movsum(abs(groom_behav_perSession),[time_integration 0],"omitnan"); - recip_groom_slidingWindow = (cumul_groom_slidingWindow./total_groom_slidingWindow); - - - %% Decode grooming context for groom give and groom receive separately - %Beginning vs. end of grooming bout - %Grooming post-threat vs not. - %Grooming after reciprocation or not - %Groom received after grm Prsnt or not - - beh = 1;behav=8; - - - for behav = [7,8] %For both groom give and groom receive - - - for groom_categ = 1:5 %For all grooming contexts - %Note: after threat, almost always groom RECEIVE, not given by - %subject. Also, grooming after groom present is only for groom - %RECEIVE. - - for rand_iter = 1:50 - allGroomBouts_sorted(:,5) = allGroomBouts_sorted(randsample(size(allGroomBouts_sorted,1),size(allGroomBouts_sorted,1)),5); - allGroomBouts_sorted(:,9) = allGroomBouts_sorted(randsample(size(allGroomBouts_sorted,1),size(allGroomBouts_sorted,1)),9); - - groom_bouts_perSession = allGroomBouts_sorted; - n_bouts = size(groom_bouts_perSession ,1); - duration_bouts = round(random('Exponential',120, 1,n_bouts)); - groom_bouts_perSession(:,3)=duration_bouts; - groom_bouts_perSession(:,6)=randsample(groom_bouts_perSession(:,6),length(groom_bouts_perSession(:,6))); - groom_all = zeros(1,size(Spike_count_raster,1)); - for bout = 1:size(groom_bouts_perSession,1) - - idx_bout{bout} = groom_bouts_perSession(bout,1):groom_bouts_perSession(bout,2); - if groom_bouts_perSession(bout,6) ==8 - groom_all(idx_bout{bout})=ones(1,length(idx_bout{bout})); - else - groom_all(idx_bout{bout}) =ones(1,length(idx_bout{bout}))*(-1); - end - end - idx_all = cell2mat(idx_bout); - cumul_groom_perSession = cumsum(groom_all); %figure; plot(cumul_groom_perSession) - - - %% Select behaviors to decode - - groombouts = allGroomBouts_sorted(allGroomBouts_sorted(:,6)==behav,:); - bouts_to_consider = randsample(size(groombouts,1),size(groombouts,1)); - idx_all=[];timescale_all=[]; boutid_all=[]; threatid_all=[]; recipid_all=[]; - for b=1:length(bouts_to_consider) - idx = groombouts(bouts_to_consider(b),1):groombouts(bouts_to_consider(b),2); - timescale = round(rescale(idx)*10); - bout_id = ones(size(idx))*b; - threat_id = ones(size(idx))*groombouts(bouts_to_consider(b),5)+1; - recip_id = ones(size(idx))*groombouts(bouts_to_consider(b),9)+1; - - idx_all = [idx_all, idx]; - timescale_all = [timescale_all, timescale]; - boutid_all = [boutid_all, bout_id]; - threatid_all = [threatid_all, threat_id]; - recipid_all = [recipid_all, recip_id]; - - end - idx_all=idx_all(1:floor(length(idx_all)./10)*10); - timescale_all = timescale_all(1:floor(length(idx_all)./10)*10); - recipid_all = recipid_all(1:floor(length(idx_all)./10)*10); - threatid_all = threatid_all(1:floor(length(idx_all)./10)*10); - boutid_all = boutid_all(1:floor(length(idx_all)./10)*10); - cumul_groom = cumul_groom_perSession(idx_all); cumul_groom_categ = round(rescale(cumul_groom)*10); - %figure; plot(cumul_groom_categ) - % recip_groom = recip_groom_perSession(idx_all); recip_groom_categ = round(recip_groom*10); - - %Shuffle by chunk to keep temporal statistics - divisors = alldivisors(length(idx_all)); - [~, idx_min]=min(abs(divisors-50)); divisor = divisors(idx_min); - cumul_groom_categ = reshape(cumul_groom_categ,[],divisor); - cumul_groom_categ = reshape(cumul_groom_categ(randperm(size(cumul_groom_categ,1)),:),[],1); -% recip_groom_categ = reshape(recip_groom_categ,[],divisor); -% recip_groom_categ = reshape(recip_groom_categ(randperm(size(recip_groom_categ,1)),:),[],1); - timescale_all = reshape(timescale_all,[],divisor); - timescale_all = reshape(timescale_all(randperm(size(timescale_all,1)),:),[],1); - - - % groom_type_label = [threatid_all; recipid_all; boutid_all; timescale_all'; recip_groom_categ'; cumul_groom_categ'; cumul_groom_over_all_sessions_categ']; - groom_type_label = [threatid_all; recipid_all; boutid_all; timescale_all'; cumul_groom_categ']; - behavior_labels_interim = groom_type_label(groom_categ,:); - Spike_count_raster_interim = Spike_count_raster(idx_all,:);%Only keep timepoints where the behaviors of interest occur in spiking data - - - behav_size=tabulate(behavior_labels_interim); - behav_final=behav_size(behav_size(:,2)>10, 1); - behavior_labels_final = behavior_labels_interim(ismember(behavior_labels_interim,behav_final)); - Spike_count_raster_final = Spike_count_raster_interim(ismember(behavior_labels_interim,behav_final),:); - - - behav_size=tabulate(behavior_labels_final); - disp('########################') - tabulate(behavior_labels_final) - disp('########################') - - - %Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - %subsample to match number of neurons across brain areas - Labels = behavior_labels_final; - if randomsample==1 && channel_flag~="all" - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min(100,min(num_trials)); %use the minimum # of instances - - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials, idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials); - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels', 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled', 5, 0, 0); - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %end of SVM iterations - - hitrate_rand(rand_iter) = mean(hitrate); - - end %end of randomizaion iteration - mean_hitrate(beh,groom_categ,chan,s) = mean(hitrate_rand); - - - end %End of grooming context loop - beh = beh+1; - end %End of give vs. receive for loop - - chan = chan +1; - end %End of channel for loop - - close all - - disp('Session done') - -end %End of session for loop diff --git a/Linear decoding/log_SVM_GroomContext_batch_v2.m b/Linear decoding/log_SVM_GroomContext_batch_v2.m deleted file mode 100644 index 4d1556d..0000000 --- a/Linear decoding/log_SVM_GroomContext_batch_v2.m +++ /dev/null @@ -1,379 +0,0 @@ -%% Log_SVM_grooming_batch -% Run a linear decoder on a the neural activity for different grooming contexts -% This script allows to decode grooming: -% 1. Start vs. end -% 2. Post-threat or not -% 3. Reciprocated or not -% 4. Initiated or not -% 5. Neighbor ID -% Batch version to run across sessions. -%Camille Testard, March 2022 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -randomsample=0; -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 25;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. -threat_precedence =1; -exclude_sq=1; - -%Initialize -% mean_hitrate = cell(length(sessions),3); -% sd_hitrate = cell(length(sessions),3); -% mean_hitrate_shuffled = cell(length(sessions),3); - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan=1; - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - session_length = size(Spike_count_raster ,1); - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - - %Load cumul groom across all sessions - cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/']) - load('ZCumulGroom_AcrossSessions.mat') - cumul_groom_over_all_sessions = cumul_groom_AcrossSessions{s}; - - - %% Interpolate short groom bouts - groomGive = zeros(size(behavior_labels)); - groomGive(behavior_labels== 7)=1; - groomGive_time = find(groomGive==1); - time_between_bouts = diff(groomGive_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGive_time(short_Runs(sr))+1:groomGive_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGive(idx_to_fill)=1; - end - - behavior_labels(find(groomGive==1))=7; - - %Groom get - groomGet = zeros(size(behavior_labels)); - groomGet(behavior_labels== 8)=1; - groomGet_time = find(groomGet==1); - time_between_bouts = diff(groomGet_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGet_time(short_Runs(sr))+1:groomGet_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGet(idx_to_fill)=1; - end - - behavior_labels(find(groomGet==1))=8; - - groom_categ_label={'Post threat','Reciprocal','Bout id','Time in bout','Reciprocity','Cumulative grooming (within session)','Cumulative grooming (over all sessions)'}; - - %% Extract grooming stats (bout length and# per session) - - %GROOM GIVE - groomGive_bout_start = find(diff(groomGive)==1)+1; - groomGive_bout_end = find(diff(groomGive)==-1); - if length(groomGive_bout_end)0 - groomGive_bout(bout,5) = any(behavior_labels(groomGive_bout_start(bout)-30:groomGive_bout_start(bout)-1)==9 ... - |behavior_labels(groomGive_bout_start(bout)-30:groomGive_bout_start(bout)-1)==10); - else - groomGive_bout(bout,5)=0; - end - end - groomGive_bout(:,6)=7; - %4th column: was there a threat right after the groom (which could have - %cut it short) - %5th column: was there a threat preceding the grooming bout - %6th column, grooming receive or give. - - %cut too short duratiin bouts (usually occurs because smth external - %cuts the bout (aggression) - %groomGive_bout(groomGive_bout(:,3)<10) - - %GROOM GET - groomGet_bout_start = find(diff(groomGet)==1)+1; - groomGet_bout_end = find(diff(groomGet)==-1); - groomGet_duration = groomGet_bout_end-groomGet_bout_start; - groomGet_bout=[groomGet_bout_start, groomGet_bout_end, groomGet_duration]; - - for bout = 1:length(groomGet_bout_end) - if groomGet_bout_end(bout)+110 - groomGet_bout(bout,5) = any(behavior_labels(groomGet_bout_start(bout)-30:groomGet_bout_start(bout)-1)==9 ... - |behavior_labels(groomGet_bout_start(bout)-30:groomGet_bout_start(bout)-1)==10); - else - groomGet_bout(bout,5) =0; - end - end - groomGet_bout(:,6)=8; - - % ALL GROOMS - allGroomBouts = [groomGet_bout; groomGive_bout]; - [~, idx_sorted] = sort(allGroomBouts(:,1)); - allGroomBouts_sorted = allGroomBouts(idx_sorted,:); - allGroomBouts_sorted(:,7)=[0;(allGroomBouts_sorted(2:end,1)-allGroomBouts_sorted(1:end-1,2))]; - allGroomBouts_sorted(:,8) = abs([0; diff(allGroomBouts_sorted(:,6))]); - allGroomBouts_sorted(:,9)=0; - allGroomBouts_sorted(find(allGroomBouts_sorted(:,8)==1 & allGroomBouts_sorted(:,7)<20),9)=1; - - %% look whithin a session - time_integration =600; - groom_behav_perSession = behavior_labels; - groom_behav_perSession(groom_behav_perSession~=8 & groom_behav_perSession~=7)=0; - - groom_behav_perSession(groom_behav_perSession==8)=1; groom_behav_perSession(groom_behav_perSession==7)=-1; - cumul_groom_perSession = cumsum(groom_behav_perSession); - cumul_groomBout_perSession = cumsum(diff(groom_behav_perSession)); figure; plot(cumul_groomBout_perSession) - total_groom_perSession = cumsum(abs(groom_behav_perSession)); - recip_groom_perSession = 1-abs((cumul_groom_perSession./total_groom_perSession)); - - cumul_groom_slidingWindow = movsum(groom_behav_perSession,[time_integration 0],"omitnan"); - total_groom_slidingWindow = movsum(abs(groom_behav_perSession),[time_integration 0],"omitnan"); - recip_groom_slidingWindow = (cumul_groom_slidingWindow./total_groom_slidingWindow); - - - %% Decode grooming context for groom give and groom receive separately - %Beginning vs. end of grooming bout - %Grooming post-threat vs not. - %Grooming after reciprocation or not - %Groom received after grm Prsnt or not - - beh = 1;behav=7; - - %shuffle - % allGroomBouts_sorted(:,5) = allGroomBouts_sorted(randsample(size(allGroomBouts_sorted,1),size(allGroomBouts_sorted,1)),5); - % allGroomBouts_sorted(:,9) = allGroomBouts_sorted(randsample(size(allGroomBouts_sorted,1),size(allGroomBouts_sorted,1)),9); - - for behav = [8,7] %For both groom give and groom receive - - - for groom_categ = 2:7 %For all grooming contexts - %Note: after threat, almost always groom RECEIVE, not given by - %subject. Also, grooming after groom present is only for groom - %RECEIVE. - - - - %% Select behaviors to decode - - groombouts = allGroomBouts_sorted(ismember(allGroomBouts_sorted(:,6),behav),:); - bouts_to_consider = 1:size(groombouts,1); - idx_all=[];timescale_all=[]; boutid_all=[]; threatid_all=[]; recipid_all=[]; - for b=1:length(bouts_to_consider) - idx = groombouts(bouts_to_consider(b),1):groombouts(bouts_to_consider(b),2); - timescale = round(rescale(idx)*10); - bout_id = ones(size(idx))*b; - threat_id = ones(size(idx))*groombouts(bouts_to_consider(b),5)+1; - recip_id = ones(size(idx))*groombouts(bouts_to_consider(b),9)+1; - - idx_all = [idx_all, idx]; - timescale_all = [timescale_all, timescale]; - boutid_all = [boutid_all, bout_id]; - threatid_all = [threatid_all, threat_id]; - recipid_all = [recipid_all, recip_id]; - - end - - %cumul_groom_over_all_sessions_categ = round(rescale(cumul_groom_over_all_sessions(idx_all))*10); - cumul_groom = cumul_groom_perSession(idx_all); cumul_groom_categ = round(rescale(cumul_groom)*10); - recip_groom = recip_groom_perSession(idx_all); recip_groom_categ = round(recip_groom*10); - - groom_type_label = [threatid_all; recipid_all; boutid_all; timescale_all; recip_groom_categ'; cumul_groom_categ']; - %[r p] = corrcoef(groom_type_label(:,3:end)'); rho{s}(beh,:)=r(1,2:end); - behavior_labels_interim = groom_type_label(groom_categ,:); - Spike_count_raster_interim = Spike_count_raster(idx_all,:);%Only keep timepoints where the behaviors of interest occur in spiking data - - - behav_size=tabulate(behavior_labels_interim); - behav_final=behav_size(behav_size(:,2)>30, 1); - behavior_labels_final = behavior_labels_interim(ismember(behavior_labels_interim,behav_final)); figure; plot(behavior_labels_final) - Spike_count_raster_final = Spike_count_raster_interim(ismember(behavior_labels_interim,behav_final),:); - -% behavior_labels_final = behavior_labels_final(500:1500); -% Spike_count_raster_final = Spike_count_raster_final(500:1500,:); -% - disp('########################') - tabulate(behavior_labels_final) - disp('########################') - - %Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - %subsample to match number of neurons across brain areas - Labels = behavior_labels_final; - if randomsample==1 && channel_flag~="all" - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = zscore(Spike_count_raster_final); - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %use the minimum # of instances - - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials, idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(sort(chosen_trials), :); - Labels = Labels(sort(chosen_trials)); figure; plot(Labels) - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}, nErr] = log_SVM_basic_function(Input_matrix, Labels', 5, 0, 0); - %NOTE: we changed the cross-validation to be continuous - %chunks instead of randomly generated folds - [hitrate_shuffled(iter), C_shuffled{iter}, nErr] = log_SVM_basic_function(Input_matrix, Labels_shuffled', 5, 0, 0); - -% if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) -% end - end %end of SVM iterations - - mean_hitrate(beh,groom_categ,chan,s) = mean(hitrate); - mean_hitrate_shuffled(beh,groom_categ,chan,s) = mean(hitrate_shuffled); - % sd_hitrate_shuffled = std(hitrate_shuffled); - - % % else - % % - % % mean_hitrate(beh,groom_categ,chan,s) = nan; - % % sd_hitrate(beh,groom_categ,chan,s) = nan; - % % - % % end % End of "min number of grooming of category b" clause - - %clear labels_id - - end %End of grooming context loop - beh = beh+1; - - end %End of give vs. receive for loop - - chan = chan +1; - end %End of channel for loop - - close all - - disp('Session done') - -end %End of session for loop - -%Change savePath for all session results folder: -cd('~/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/') -%save('SVM_groomingCateg.mat', "groom_categ_label","mean_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","session_range","behav_categ","home") -load('SVM_groomingCateg.mat') - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -mean_hitrate(mean_hitrate==0)=nan; -mean_hitrate_shuffled(mean_hitrate_shuffled==0)=nan; - -beh=2; -figure; hold on; set(gcf,'Position',[150 250 1000 700]); b=1; -for groomcat = 3:6 - - subplot(2,2,b); hold on - - data = squeeze(mean_hitrate(beh,groomcat,:,session_range))'; - data_shuffled = squeeze(mean_hitrate_shuffled(beh,groomcat,:,session_range))'; - - bp = bar([nanmean(data); nanmean(data_shuffled)],'FaceAlpha',0.2); - - sp1 = scatter(ones(size(data,1))*0.78,data(:,1), 'filled','b'); - sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); - sp1 = scatter(ones(size(data,1))*1.22,data(:,3), 'filled','y'); - - sp1 = scatter(ones(size(data,1))*1.78,data_shuffled(:,1), 'filled','b'); - sp1 = scatter(ones(size(data,1))*2,data_shuffled(:,2), 'filled','r'); - sp1 = scatter(ones(size(data,1))*2.22,data_shuffled(:,3), 'filled','y'); - - legend(bp,{'vlPFC','TEO','all'},'Location','best') - - ylabel('Decoding Accuracy'); ylim([0 1]) - xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) - ax = gca; - ax.FontSize = 16; - - title(groom_categ_label(groomcat)) - b=b+1; - -end \ No newline at end of file diff --git a/Linear decoding/log_SVM_GroomDuration.m b/Linear decoding/log_SVM_GroomDuration.m deleted file mode 100644 index 2ba3147..0000000 --- a/Linear decoding/log_SVM_GroomDuration.m +++ /dev/null @@ -1,345 +0,0 @@ -%% Log_SVM_grooming_batch -% Run a linear decoder on a the neural activity for different grooming contexts -% This script allows to decode grooming: -% 1. Start vs. end -% 2. Post-threat or not -% 3. Reciprocated or not -% 4. Initiated or not -% 5. Neighbor ID -% Batch version to run across sessions. -%Camille Testard, March 2022 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -randomsample=0; -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 50;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. -threat_precedence =1; -exclude_sq=1; - -%Initialize -% mean_hitrate = cell(length(sessions),3); -% sd_hitrate = cell(length(sessions),3); -% mean_hitrate_shuffled = cell(length(sessions),3); - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan=1; - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - session_length = size(Spike_count_raster ,1); - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - - %% Interpolate short groom bouts - groomGive = zeros(size(behavior_labels)); - groomGive(behavior_labels== 7)=1; - groomGive_time = find(groomGive==1); - time_between_bouts = diff(groomGive_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGive_time(short_Runs(sr))+1:groomGive_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGive(idx_to_fill)=1; - end - - behavior_labels(find(groomGive==1))=7; - - %Groom get - groomGet = zeros(size(behavior_labels)); - groomGet(behavior_labels== 8)=1; - groomGet_time = find(groomGet==1); - time_between_bouts = diff(groomGet_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGet_time(short_Runs(sr))+1:groomGet_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGet(idx_to_fill)=1; - end - - behavior_labels(find(groomGet==1))=8; - - %% Extract grooming stats (bout length and# per session) - - %GROOM GIVE - groomGive_bout_start = find(diff(groomGive)==1)+1; - groomGive_bout_end = find(diff(groomGive)==-1); - if length(groomGive_bout_end)0 - groomGive_bout(bout,5) = any(behavior_labels(groomGive_bout_start(bout)-30:groomGive_bout_start(bout)-1)==9 ... - |behavior_labels(groomGive_bout_start(bout)-30:groomGive_bout_start(bout)-1)==10); - else - groomGive_bout(bout,5)=0; - end - end - groomGive_bout(:,6)=7; - %4th column: was there a threat right after the groom (which could have - %cut it short) - %5th column: was there a threat preceding the grooming bout - %6th column, grooming receive or give. - - %cut too short duratiin bouts (usually occurs because smth external - %cuts the bout (aggression) - %groomGive_bout(groomGive_bout(:,3)<10) - - %GROOM GET - groomGet_bout_start = find(diff(groomGet)==1)+1; - groomGet_bout_end = find(diff(groomGet)==-1); - groomGet_duration = groomGet_bout_end-groomGet_bout_start; - groomGet_bout=[groomGet_bout_start, groomGet_bout_end, groomGet_duration]; - - for bout = 1:length(groomGet_bout_end) - if groomGet_bout_end(bout)+110 - groomGet_bout(bout,5) = any(behavior_labels(groomGet_bout_start(bout)-30:groomGet_bout_start(bout)-1)==9 ... - |behavior_labels(groomGet_bout_start(bout)-30:groomGet_bout_start(bout)-1)==10); - else - groomGet_bout(bout,5) =0; - end - end - groomGet_bout(:,6)=8; - - % ALL GROOMS - allGroomBouts = [groomGet_bout; groomGive_bout]; - [~, idx_sorted] = sort(allGroomBouts(:,1)); - allGroomBouts_sorted = allGroomBouts(idx_sorted,:); - allGroomBouts_sorted(:,7)=[0;(allGroomBouts_sorted(2:end,1)-allGroomBouts_sorted(1:end-1,2))]; - allGroomBouts_sorted(:,8) = abs([0; diff(allGroomBouts_sorted(:,6))]); - allGroomBouts_sorted(:,9)=0; - allGroomBouts_sorted(find(allGroomBouts_sorted(:,8)==1 & allGroomBouts_sorted(:,7)<20),9)=1; - - - %% Decode grooming context for groom give and groom receive separately - %Beginning vs. end of grooming bout - %Grooming post-threat vs not. - %Grooming after reciprocation or not - %Groom received after grm Prsnt or not - - beh = 1;behav=7; - - %shuffle - % allGroomBouts_sorted(:,5) = allGroomBouts_sorted(randsample(size(allGroomBouts_sorted,1),size(allGroomBouts_sorted,1)),5); - % allGroomBouts_sorted(:,9) = allGroomBouts_sorted(randsample(size(allGroomBouts_sorted,1),size(allGroomBouts_sorted,1)),9); - - for behav = [8,7] %For both groom give and groom receive - - - - %% Select behaviors to decode - - groombouts = allGroomBouts_sorted(allGroomBouts_sorted(:,6)==behav & allGroomBouts_sorted(:,3)>10,:); - bouts_to_consider = 1:size(groombouts,1); - idx_all=[];timeBout_all=[]; boutid_all =[]; - for b=1:length(bouts_to_consider) - idx = groombouts(bouts_to_consider(b),1):groombouts(bouts_to_consider(b),1)+10; - timeBout = ones(size(idx))*groombouts(bouts_to_consider(b),3); - bout_id = ones(size(idx))*b; - - boutid_all = [boutid_all, bout_id]; - idx_all = [idx_all, idx]; - timeBout_all = [timeBout_all, timeBout]; - end - - timeBout_categ = round(rescale(timeBout_all)*10); - [r p] = corrcoef(boutid_all, timeBout_categ); rho(beh,s)=r(1,2); pval(beh,s)=p(1,2); - behavior_labels_interim = timeBout_categ; - Spike_count_raster_interim = Spike_count_raster(idx_all,:);%Only keep timepoints where the behaviors of interest occur in spiking data - - - behav_size=tabulate(behavior_labels_interim); - behav_final=behav_size(behav_size(:,2)>10, 1); - behavior_labels_final = behavior_labels_interim(ismember(behavior_labels_interim,behav_final)); - Spike_count_raster_final = Spike_count_raster_interim(ismember(behavior_labels_interim,behav_final),:); - - disp('########################') - tabulate(behavior_labels_final) - disp('########################') - - %Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - %subsample to match number of neurons across brain areas - Labels = behavior_labels_final; figure; plot(Labels) - if randomsample==1 && channel_flag~="all" - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %use the minimum # of instances - - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials, idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials); - Labels_shuffled = Labels(randperm(length(Labels)));figure; plot(Labels_shuffled) - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels', 2, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled', 2, 0, 0); - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %end of SVM iterations - - mean_hitrate(beh,chan,s) = mean(hitrate); - mean_hitrate_shuffled(beh,chan,s) = mean(hitrate_shuffled); - % sd_hitrate_shuffled = std(hitrate_shuffled); - - % % else - % % - % % mean_hitrate(beh,groom_categ,chan,s) = nan; - % % sd_hitrate(beh,groom_categ,chan,s) = nan; - % % - % % end % End of "min number of grooming of category b" clause - - %clear labels_id - - beh = beh+1; - - end %End of give vs. receive for loop - - chan = chan +1; - end %End of channel for loop - - close all - - disp('Session done') - -end %End of session for loop - -%Change savePath for all session results folder: -cd('~/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/') -save('SVM_groomingDuration.mat', "mean_hitrate", "mean_hitrate_shuffled", "behav","a_sessions","h_sessions","behav_categ","home") -load('SVM_groomingCateg.mat') - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -mean_hitrate(mean_hitrate==0)=nan; -mean_hitrate_shuffled(mean_hitrate_shuffled==0)=nan; - -figure; hold on; set(gcf,'Position',[150 250 1000 700]); b=1; -for groomcat = 2:5 - - subplot(2,2,b); hold on - - %Note: beh--> 1 = grooming partner; 2=getting groomed - %groomcat --> 1 = Star.vs.end; 2=Post-threat; 3=Reciprocated; 4=Initiated; 5=Neighbor ID - - % plot decoding context for getting groomed - % except for grooming initiation which should be groom partner (getting - % groomed is too much initiated by one single individual) - if groomcat == 4 - data = squeeze(mean_hitrate(1,groomcat,:,:))'; - data_shuffled = squeeze(mean_hitrate_shuffled(1,groomcat,:,:))'; - else - data = squeeze(mean_hitrate(2,groomcat,:,:))'; - data_shuffled = squeeze(mean_hitrate_shuffled(2,groomcat,:,:))'; - end - - bp = bar([nanmean(data); nanmean(data_shuffled)],'FaceAlpha',0.2); - - sp1 = scatter(ones(size(data,1))*0.78,data(:,1), 'filled','b'); - sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); - sp1 = scatter(ones(size(data,1))*1.22,data(:,3), 'filled','y'); - - sp1 = scatter(ones(size(data,1))*1.78,data_shuffled(:,1), 'filled','b'); - sp1 = scatter(ones(size(data,1))*2,data_shuffled(:,2), 'filled','r'); - sp1 = scatter(ones(size(data,1))*2.22,data_shuffled(:,3), 'filled','y'); - - legend(bp,{'vlPFC','TEO','all'},'Location','best') - - ylabel('Decoding Accuracy'); ylim([0.45 1]) - xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) - ax = gca; - ax.FontSize = 16; - - title(groom_categ_label(groomcat)) - b=b+1; - -end \ No newline at end of file diff --git a/Linear decoding/log_SVM_GroomDuration_shuffle.m b/Linear decoding/log_SVM_GroomDuration_shuffle.m deleted file mode 100644 index f998727..0000000 --- a/Linear decoding/log_SVM_GroomDuration_shuffle.m +++ /dev/null @@ -1,349 +0,0 @@ -%% Log_SVM_grooming_batch -% Run a linear decoder on a the neural activity for different grooming contexts -% This script allows to decode grooming: -% 1. Start vs. end -% 2. Post-threat or not -% 3. Reciprocated or not -% 4. Initiated or not -% 5. Neighbor ID -% Batch version to run across sessions. -%Camille Testard, March 2022 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -randomsample=0; -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 50;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. -threat_precedence =1; -exclude_sq=1; - -%Initialize -% mean_hitrate = cell(length(sessions),3); -% sd_hitrate = cell(length(sessions),3); -% mean_hitrate_shuffled = cell(length(sessions),3); - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan=1; - for channel_flag = ["all"] - - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - session_length = size(Spike_count_raster ,1); - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - - %% Interpolate short groom bouts - groomGive = zeros(size(behavior_labels)); - groomGive(behavior_labels== 7)=1; - groomGive_time = find(groomGive==1); - time_between_bouts = diff(groomGive_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGive_time(short_Runs(sr))+1:groomGive_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGive(idx_to_fill)=1; - end - - behavior_labels(find(groomGive==1))=7; - - %Groom get - groomGet = zeros(size(behavior_labels)); - groomGet(behavior_labels== 8)=1; - groomGet_time = find(groomGet==1); - time_between_bouts = diff(groomGet_time); - short_Runs = find(time_between_bouts>1 & time_between_bouts<11); - - for sr =1:length(short_Runs) - idx_to_fill = groomGet_time(short_Runs(sr))+1:groomGet_time(short_Runs(sr))+time_between_bouts(short_Runs(sr))-1; - groomGet(idx_to_fill)=1; - end - - behavior_labels(find(groomGet==1))=8; - - %% Extract grooming stats (bout length and# per session) - - %GROOM GIVE - groomGive_bout_start = find(diff(groomGive)==1)+1; - groomGive_bout_end = find(diff(groomGive)==-1); - if length(groomGive_bout_end)0 - groomGive_bout(bout,5) = any(behavior_labels(groomGive_bout_start(bout)-30:groomGive_bout_start(bout)-1)==9 ... - |behavior_labels(groomGive_bout_start(bout)-30:groomGive_bout_start(bout)-1)==10); - else - groomGive_bout(bout,5)=0; - end - end - groomGive_bout(:,6)=7; - %4th column: was there a threat right after the groom (which could have - %cut it short) - %5th column: was there a threat preceding the grooming bout - %6th column, grooming receive or give. - - %cut too short duratiin bouts (usually occurs because smth external - %cuts the bout (aggression) - %groomGive_bout(groomGive_bout(:,3)<10) - - %GROOM GET - groomGet_bout_start = find(diff(groomGet)==1)+1; - groomGet_bout_end = find(diff(groomGet)==-1); - groomGet_duration = groomGet_bout_end-groomGet_bout_start; - groomGet_bout=[groomGet_bout_start, groomGet_bout_end, groomGet_duration]; - - for bout = 1:length(groomGet_bout_end) - if groomGet_bout_end(bout)+110 - groomGet_bout(bout,5) = any(behavior_labels(groomGet_bout_start(bout)-30:groomGet_bout_start(bout)-1)==9 ... - |behavior_labels(groomGet_bout_start(bout)-30:groomGet_bout_start(bout)-1)==10); - else - groomGet_bout(bout,5) =0; - end - end - groomGet_bout(:,6)=8; - - % ALL GROOMS - allGroomBouts = [groomGet_bout; groomGive_bout]; - [~, idx_sorted] = sort(allGroomBouts(:,1)); - allGroomBouts_sorted = allGroomBouts(idx_sorted,:); - allGroomBouts_sorted(:,7)=[0;(allGroomBouts_sorted(2:end,1)-allGroomBouts_sorted(1:end-1,2))]; - allGroomBouts_sorted(:,8) = abs([0; diff(allGroomBouts_sorted(:,6))]); - allGroomBouts_sorted(:,9)=0; - allGroomBouts_sorted(find(allGroomBouts_sorted(:,8)==1 & allGroomBouts_sorted(:,7)<20),9)=1; - - - %% Decode grooming context for groom give and groom receive separately - %Beginning vs. end of grooming bout - %Grooming post-threat vs not. - %Grooming after reciprocation or not - %Groom received after grm Prsnt or not - - beh = 1;behav=8; - - %shuffle - % allGroomBouts_sorted(:,5) = allGroomBouts_sorted(randsample(size(allGroomBouts_sorted,1),size(allGroomBouts_sorted,1)),5); - % allGroomBouts_sorted(:,9) = allGroomBouts_sorted(randsample(size(allGroomBouts_sorted,1),size(allGroomBouts_sorted,1)),9); - - for behav = [8,7] %For both groom give and groom receive - - - - for rand_iter = 1:50 - - %% Select behaviors to decode - - groombouts = allGroomBouts_sorted(allGroomBouts_sorted(:,6)==behav & allGroomBouts_sorted(:,3)>10,:); - groombouts(:,3) = groombouts(randsample(size(groombouts,1),size(groombouts,1)),3); - bouts_to_consider = 1:size(groombouts,1); - idx_all=[];timeBout_all=[]; boutid_all =[]; - for b=1:length(bouts_to_consider) - idx = groombouts(bouts_to_consider(b),1):groombouts(bouts_to_consider(b),1)+10; - timeBout = ones(size(idx))*groombouts(bouts_to_consider(b),3); - bout_id = ones(size(idx))*b; - - boutid_all = [boutid_all, bout_id]; - idx_all = [idx_all, idx]; - timeBout_all = [timeBout_all, timeBout]; - end - - timeBout_categ = round(rescale(timeBout_all)*10); - %[r p] = corrcoef(boutid_all, timeBout_categ); rho(beh,s)=r(1,2); pval(beh,s)=p(1,2); - behavior_labels_interim = timeBout_categ; - Spike_count_raster_interim = Spike_count_raster(idx_all,:);%Only keep timepoints where the behaviors of interest occur in spiking data - - - behav_size=tabulate(behavior_labels_interim); - behav_final=behav_size(behav_size(:,2)>10, 1); - behavior_labels_final = behavior_labels_interim(ismember(behavior_labels_interim,behav_final)); - Spike_count_raster_final = Spike_count_raster_interim(ismember(behavior_labels_interim,behav_final),:); - - disp('########################') - tabulate(behavior_labels_final) - disp('########################') - - %Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - %subsample to match number of neurons across brain areas - Labels = behavior_labels_final; - if randomsample==1 && channel_flag~="all" - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %use the minimum # of instances - - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials, idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials); - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels', 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled', 5, 0, 0); - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %end of SVM iterations - - mean_hitrate{s}(beh,chan,rand_iter) = mean(hitrate); - mean_hitrate_shuffled{s}(beh,chan,rand_iter) = mean(hitrate_shuffled); - % sd_hitrate_shuffled = std(hitrate_shuffled); - - % % else - % % - % % mean_hitrate(beh,groom_categ,chan,s) = nan; - % % sd_hitrate(beh,groom_categ,chan,s) = nan; - % % - % % end % End of "min number of grooming of category b" clause - - %clear labels_id - end %end of shuffling iteration - - beh = beh+1; - - end %End of give vs. receive for loop - - chan = chan +1; - end %End of channel for loop - - close all - - disp('Session done') - -end %End of session for loop - -%Change savePath for all session results folder: -cd('~/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/') -save('SVM_groomingDuration.mat', "mean_hitrate", "mean_hitrate_shuffled", "behav","a_sessions","h_sessions","behav_categ","home") -load('SVM_groomingDuration.mat') - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -mean_hitrate(mean_hitrate==0)=nan; -mean_hitrate_shuffled(mean_hitrate_shuffled==0)=nan; - -figure; hold on; set(gcf,'Position',[150 250 1000 700]); b=1; -for groomcat = 2:5 - - subplot(2,2,b); hold on - - %Note: beh--> 1 = grooming partner; 2=getting groomed - %groomcat --> 1 = Star.vs.end; 2=Post-threat; 3=Reciprocated; 4=Initiated; 5=Neighbor ID - - % plot decoding context for getting groomed - % except for grooming initiation which should be groom partner (getting - % groomed is too much initiated by one single individual) - if groomcat == 4 - data = squeeze(mean_hitrate(1,groomcat,:,:))'; - data_shuffled = squeeze(mean_hitrate_shuffled(1,groomcat,:,:))'; - else - data = squeeze(mean_hitrate(2,groomcat,:,:))'; - data_shuffled = squeeze(mean_hitrate_shuffled(2,groomcat,:,:))'; - end - - bp = bar([nanmean(data); nanmean(data_shuffled)],'FaceAlpha',0.2); - - sp1 = scatter(ones(size(data,1))*0.78,data(:,1), 'filled','b'); - sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); - sp1 = scatter(ones(size(data,1))*1.22,data(:,3), 'filled','y'); - - sp1 = scatter(ones(size(data,1))*1.78,data_shuffled(:,1), 'filled','b'); - sp1 = scatter(ones(size(data,1))*2,data_shuffled(:,2), 'filled','r'); - sp1 = scatter(ones(size(data,1))*2.22,data_shuffled(:,3), 'filled','y'); - - legend(bp,{'vlPFC','TEO','all'},'Location','best') - - ylabel('Decoding Accuracy'); ylim([0.45 1]) - xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) - ax = gca; - ax.FontSize = 16; - - title(groom_categ_label(groomcat)) - b=b+1; - -end \ No newline at end of file diff --git a/Linear decoding/log_SVM_subject_batch_byTempRes.m b/Linear decoding/log_SVM_subject_batch_byTempRes.m deleted file mode 100644 index 8c9478b..0000000 --- a/Linear decoding/log_SVM_subject_batch_byTempRes.m +++ /dev/null @@ -1,249 +0,0 @@ -%% Log_SVM_subject_batch -% Run a linear decoder on the neural activity for the subject's behavior -% (only including behaviors with a minimum # occurrence) -% Batch version of script -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp = 1; -temp_resolution_sweep = [0.1, 0.5, 1, 5, 10, 29.97, 100]; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 10;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. -threat_precedence =0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range(3:end) %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - channel_flag = ["all"]; - temp=1; - for temp_resolution = temp_resolution_sweep - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - - %Low-pass filter data - %Spike_count_raster = lowpass(Spike_rasters',0.05,1); - - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - co_occurrence = cell2mat({labels{:,5}}'); - - if null - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels,behav_categ, temp_resolution); - behavior_labels = sim_behav; - end - - if unq_behav==1%Select epochs where only one behavior happens at any given time (i.e. no co-occurrence). - idx_single = find(co_occurrence<4); %(no co-occurrence, or with RR or with proximity) - Spike_count_raster = Spike_count_raster(idx_single,:); - behavior_labels = behavior_labels(idx_single,:); - end - - if simplify - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Lump foraging and drinking - behavior_labels(behavior_labels==find(behav_categ=="Drinking"))=find(behav_categ=="Foraging"); - - %Lump grooming together - behavior_labels(behavior_labels==find(behav_categ=="Getting groomed"))=find(behav_categ=="Groom partner"); - end - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - % Remove behaviors that are ill-defined - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Vocalization')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - - % OR select behaviors manually - behav = [7,8,20];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(removecats(categorical(behavior_labels_final))); - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - - %subsample to match number of neurons across brain areas - Labels = behavior_labels_final; - if randomsample==1 - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min(min(num_trials),min_occurrences); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %End of SVM for loop - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate{s}(temp) = mean(hitrate) - sd_hitrate{s}(temp) = std(hitrate); - mean_hitrate_shuffled{s}(temp) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - - temp = temp +1; - clear labels_id - - end %end of temp for loop - - cd(savePath) - -end %End of session for loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd(['~/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_subjectBehav_byTemp.mat', "mean_hitrate","sd_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") -load('SVM_results_subjectBehav_byTemp.mat') - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data = cell2mat(mean_hitrate'); -data_shuffle = cell2mat(mean_hitrate_shuffled'); -bp = bar([mean(data(:,:)), mean(data_shuffle(:,1))],'FaceAlpha',0.2); -ylabel('Decoding Accuracy'); ylim([0 1]) -xticks([1:length(temp_resolution_sweep)]); -xticklbls = 1./temp_resolution_sweep; -xticklabels(string(xticklbls)); xlim([0.25 length(temp_resolution_sweep)+0.75]) -ax = gca; -ax.FontSize = 16; - -saveas(gcf,['SVM_results_subjectBehav_100msec.pdf']) - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% % % % % %Confusion matrices -% % % % % vlpfc = table2array(C_table{s,1,1}); TEO = table2array(C_table{s,1,2}); all_units = table2array(C_table{s,1,3}); -% % % % % D{s} = vlpfc-TEO; -% % % % % CustomAxisLabels = string(C_table{s,1,1}.Properties.VariableNames); figure; set(gcf,'Position',[150 250 1500 800]); -% % % % % subplot(2,2,1); pfc = heatmap(vlpfc,'Colormap', hot); pfc.XDisplayLabels = CustomAxisLabels; pfc.YDisplayLabels = CustomAxisLabels; title(pfc,'vlpfc confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,2); teo = heatmap(TEO,'Colormap', hot); teo.XDisplayLabels = CustomAxisLabels; teo.YDisplayLabels = CustomAxisLabels; title(teo,'TEO confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,3); all = heatmap(all_units,'Colormap', hot); all.XDisplayLabels = CustomAxisLabels; all.YDisplayLabels = CustomAxisLabels; title(all,'All units confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,4); h = heatmap(D,'Colormap', cool); h.XDisplayLabels = CustomAxisLabels; h.YDisplayLabels = CustomAxisLabels; title(h,'vlpfc-TEO confusion matrix');caxis([-50, 50]); diff --git a/Linear decoding/log_SVM_subject_increasingUnits_batch.m b/Linear decoding/log_SVM_subject_increasingUnits_batch.m deleted file mode 100644 index 63715db..0000000 --- a/Linear decoding/log_SVM_subject_increasingUnits_batch.m +++ /dev/null @@ -1,310 +0,0 @@ -%% Log_SVM_subject_batch -% Run a linear decoder on the neural activity for the subject's behavior -% (only including behaviors with a minimum # occurrence) -% Batch version of script -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:6,11:13,15:16]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 500;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. -agg_precedence =0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -unit_num_range = [1, 5, 20, 50, 80, 120, 150, 200]; - -for s =session_range(2:end) %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - %for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - co_occurrence = cell2mat({labels{:,5}}'); - - if unq_behav==1%Select epochs where only one behavior happens at any given time (i.e. no co-occurrence). - idx_single = find(co_occurrence==1 | co_occurrence==2 | co_occurrence==3); %(no co-occurrence, or with RR or with proximity) - Spike_count_raster = Spike_count_raster(idx_single,:); - behavior_labels = behavior_labels(idx_single,:); - end - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize'))); - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - - % OR select behaviors manually - % behav = [4:10,16,23];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(removecats(categorical(behavior_labels_final))); - - - %% Run SVM over increasing numbers of units, over multiple iterations - u = 1; - for unit_num = unit_num_range - - disp('Start running SVM...') - for iter = 1:num_iter - - - %Select unit to run SVM - Labels = behavior_labels_final; - Input_matrix = Spike_count_raster_final(:,randsample(size(Spike_count_raster_final,2), unit_num)); - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = 30;%min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); -% [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); -% hitrate_ratio(iter) = hitrate(iter)/hitrate_shuffled(iter); - - if mod(iter,50)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %End of SVM for loop - - disp(['SVM run for ' num2str(unit_num) ' unit(s)']) - - - % channel = char(channel_flag); - % disp('****************************************************************************') - % disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - % disp('****************************************************************************') - - mean_hitrate{s,chan}(u) = mean(hitrate); - sd_hitrate{s,chan}(u) = std(hitrate); -% mean_hitrate_shuffled{s,chan}(u) = mean(hitrate_shuffled); -% sd_hitrate_shuffled = std(hitrate_shuffled); -% mean_hitrate_ratio{s,chan}(u) = mean(hitrate_ratio); -% sd_hitrate_ratio{s,chan}(u) = std(hitrate_ratio); - - %chan = chan +1; - clear labels_id - - u=u+1; - end %End of unit number loop - chan = chan+1; - - %end %end of channel for loop - - cd(savePath) - - %Plotting results decoding accuracy for all behaviors at 1sec and lower resolution - figure; hold on; set(gcf,'Position',[150 250 700 500]) - y1 = mean_hitrate{s,1}; - %y2 = mean_hitrate{s,2}; - std_dev1 = sd_hitrate{s,1}; - %std_dev2 = sd_hitrate{s,2}; - errorbar(y1,std_dev1,'s','MarkerSize',10) - %errorbar(y2,std_dev2,'s','MarkerSize',10) - chance_level = 1/behav; - yline(chance_level,'--','Chance level', 'FontSize',16) - xticks([1:length(unit_num_range)]); xlim([0.8 length(unit_num_range)+0.2]); ylim([0 1]) - xticklabels(unit_num_range) - ax = gca; - ax.FontSize = 14; - ylabel('Decoding accuracy','FontSize', 18); xlabel('#units included','FontSize', 18) - title('Decoding accuracy for subject current behavioral states with increasing #units','FontSize', 14) - saveas(gcf, 'Decoding_subject_increasing_units.png') - - close all - -end %End of session for loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('IncreasingUnits.mat') -load('IncreasingUnits.mat') - -%Plot decoding accuracy for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 600 700]); hold on -%subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,1)); -for s = session_range_no_partner - y = mean_hitrate{s}; - %std_dev = sd_hitrate(s,:); - scatter(1:length(y),y,40,'MarkerFaceColor',cmap(s,:), 'MarkerEdgeColor','k','MarkerFaceAlpha',0.4) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1:length(unit_num_range) length(unit_num_range)+.2]); -xlim([0.8 length(unit_num_range)+.2]); ylim([0 1]) -xticklabels({'',unit_num_range,''}) -ax = gca; -ax.FontSize = 14; -ylabel('Mean decoding accuracy','FontSize', 16); xlabel('Number of units','FontSize', 16) -%title('Monkey A','FontSize', 14) - -% subplot(2,1,2);hold on; -% for s = h_sessions -% y = mean_hitrate{s}; -% %std_dev = sd_hitrate(s,:); -% scatter(1:length(y),y,40,'MarkerFaceColor',cmap(s,:), 'MarkerEdgeColor','k','MarkerFaceAlpha',0.4) -% end -% chance_level = 1/length(behav); -% yline(chance_level,'--','Chance level', 'FontSize',16) -% xticks([0.8 1:length(unit_num_range) length(unit_num_range)+.2]); -% xlim([0.8 length(unit_num_range)+.2]); ylim([0 1]) -% xticklabels({'',unit_num_range,''}) -% ax = gca; -% ax.FontSize = 14; -% ylabel('Mean decoding accuracy','FontSize', 16); xlabel('Number of units','FontSize', 16) -% title('Monkey H','FontSize', 14) - -saveas(gcf,'Decoding_subject_increasing_units_allSessions.png') -save('IncreasingUnits.mat','mean_hitrate','sd_hitrate','behav','unit_num_range','h_sessions','a_sessions') -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot decoding accuracy relative to chance for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 10]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for subject current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 10]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for subject current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && unq_behav==1 - saveas(gcf,['SVM_results_behav_NOsubsample_unique_allSessions.png']) -elseif randomsample ==1 && unq_behav==1 - saveas(gcf,['SVM_results_behav_subsample_unique_allSessions.png']) -elseif randomsample ==0 && unq_behav==0 - saveas(gcf,['SVM_results_behav_NOsubsample_NOTunique_allSessions.png']) -elseif randomsample ==1 && unq_behav==0 - saveas(gcf,['SVM_results_behav_subsample_NOTunique_allSessions.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% % % % % %Confusion matrices -% % % % % vlpfc = table2array(C_table{s,1,1}); TEO = table2array(C_table{s,1,2}); all_units = table2array(C_table{s,1,3}); -% % % % % D{s} = vlpfc-TEO; -% % % % % CustomAxisLabels = string(C_table{s,1,1}.Properties.VariableNames); figure; set(gcf,'Position',[150 250 1500 800]); -% % % % % subplot(2,2,1); pfc = heatmap(vlpfc,'Colormap', hot); pfc.XDisplayLabels = CustomAxisLabels; pfc.YDisplayLabels = CustomAxisLabels; title(pfc,'vlpfc confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,2); teo = heatmap(TEO,'Colormap', hot); teo.XDisplayLabels = CustomAxisLabels; teo.YDisplayLabels = CustomAxisLabels; title(teo,'TEO confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,3); all = heatmap(all_units,'Colormap', hot); all.XDisplayLabels = CustomAxisLabels; all.YDisplayLabels = CustomAxisLabels; title(all,'All units confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,4); h = heatmap(D,'Colormap', cool); h.XDisplayLabels = CustomAxisLabels; h.YDisplayLabels = CustomAxisLabels; title(h,'vlpfc-TEO confusion matrix');caxis([-50, 50]); diff --git a/Linear decoding/log_SVM_subject_perUnit_batch.m b/Linear decoding/log_SVM_subject_perUnit_batch.m deleted file mode 100644 index a381c67..0000000 --- a/Linear decoding/log_SVM_subject_perUnit_batch.m +++ /dev/null @@ -1,231 +0,0 @@ -%% Log_SVM_subject_batch -% Run a linear decoder on the neural activity for the subject's behavior -% (only including behaviors with a minimum # occurrence) -% Batch version of script -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:6,11:13,15:16]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. -threat_precedence=1; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence -exclude_sq = 1; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; - -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - %for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - co_occurrence = cell2mat({labels{:,5}}'); - - if unq_behav==1%Select epochs where only one behavior happens at any given time (i.e. no co-occurrence). - idx_single = find(co_occurrence==1 | co_occurrence==2 | co_occurrence==3); %(no co-occurrence, or with RR or with proximity) - Spike_count_raster = Spike_count_raster(idx_single,:); - behavior_labels = behavior_labels(idx_single,:); - end - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize'))); - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - - % OR select behaviors manually - % behav = [4:10,16,23];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(removecats(categorical(behavior_labels_final))); - uniqueLabels_session{s} = unique(behavior_labels_final); - - - %% Run SVM over increasing numbers of units, over multiple iterations - u = 1; - for unit = 1:size(Spike_count_raster_final,2) - - disp('Start running SVM...') - for iter=1:num_iter - - %Select unit to run SVM - Labels = behavior_labels_final; - Input_matrix = Spike_count_raster_final(:,unit); - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = 30;%min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); -% [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); -% hitrate_ratio(iter) = hitrate(iter)/hitrate_shuffled(iter); - - if mod(iter,50)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %End of SVM for loop - - disp(['SVM run for unit ' num2str(unit)]) - - - % channel = char(channel_flag); - % disp('****************************************************************************') - % disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - % disp('****************************************************************************') - - mean_C{s}(unit,1:25) = nan; - mean_C{s}(unit,uniqueLabels) = diag(mean(cat(3,C{:}),3)); - mean_hitrate{s}(unit) = mean(hitrate); - sd_hitrate{s}(unit) = std(hitrate); - mean_C_aboveChance{s}(unit,1:25) = mean_C{s}(unit,1:25)>(1/NumOfClasses); -% sd_hitrate_shuffled = std(hitrate_shuffled); -% mean_hitrate_ratio{s,chan}(u) = mean(hitrate_ratio); -% sd_hitrate_ratio{s,chan}(u) = std(hitrate_ratio); - - %chan = chan +1; - clear labels_id - - u=u+1; - end %End of unit number loop - chan = chan+1; - - %end %end of channel for loop - - cd(savePath) - -% % % %Plotting results decoding accuracy for all behaviors at 1sec and lower resolution -% % % figure; hold on; set(gcf,'Position',[150 250 700 500]) -% % % y1 = mean_hitrate{s,1}; -% % % %y2 = mean_hitrate{s,2}; -% % % std_dev1 = sd_hitrate{s,1}; -% % % %std_dev2 = sd_hitrate{s,2}; -% % % errorbar(y1,std_dev1,'s','MarkerSize',10) -% % % %errorbar(y2,std_dev2,'s','MarkerSize',10) -% % % chance_level = 1/behav; -% % % yline(chance_level,'--','Chance level', 'FontSize',16) -% % % xticks([1:length(unit_num_range)]); xlim([0.8 length(unit_num_range)+0.2]); ylim([0 1]) -% % % xticklabels(unit_num_range) -% % % ax = gca; -% % % ax.FontSize = 14; -% % % ylabel('Decoding accuracy','FontSize', 18); xlabel('#units included','FontSize', 18) -% % % title('Decoding accuracy for subject current behavioral states with increasing #units','FontSize', 14) -% % % saveas(gcf, 'Decoding_subject_increasing_units.png') - - close all - -end %End of session for loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_per_Unit.mat') -%load('IncreasingUnits.mat') - -hitrate_all = cell2mat(mean_hitrate); figure; histogram(hitrate_all) -C_all = cell2mat(mean_C'); -C_final = C_all(:,~all(isnan(C_all))); -behav_final = behav_categ(~all(isnan(C_all))); - -figure; hold on; -mean_dim = nanmean(C_final); [~, orderIdx] = sort(mean_dim); -boxchart(C_final(:,orderIdx)) -xticklabels(behav_final(orderIdx)) -ylabel('Decoding accuracy') - -[orderOutput, orderIdx] = sort(sum(C_final>0.1)/size(C_final,1)); -figure; hold on -scatter(1:length(orderOutput),orderOutput,40,'filled') -xticks(1:length(orderOutput)) -xticklabels(behav_final(orderIdx)) -ylabel('Prop decoding above chance') - -test=C_final>0.1 -figure; histogram(sum(test,2)) diff --git a/Linear decoding/old_code/.DS_Store b/Linear decoding/old_code/.DS_Store deleted file mode 100644 index 5008ddf..0000000 Binary files a/Linear decoding/old_code/.DS_Store and /dev/null differ diff --git a/Linear decoding/old_code/SVM_brain_area.m b/Linear decoding/old_code/SVM_brain_area.m deleted file mode 100644 index 4222917..0000000 --- a/Linear decoding/old_code/SVM_brain_area.m +++ /dev/null @@ -1,147 +0,0 @@ -%% Log_SVM_subject_batch -% Run a linear decoder on the neural activity for the subject's behavior -% (only including behaviors with a minimum # occurrence) -% Batch version of script -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 50;%Number of SVM iterations - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - disp('Data Loaded') - - Spike_count_raster_final = Spike_rasters; - behavior_labels_final = brain_label; - - tabulate(removecats(categorical(behavior_labels_final))) - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - - Labels = behavior_labels_final; - Input_matrix = Spike_count_raster_final; - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = strcmp(Labels, uniqueLabels(i)); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); - end - Labels = str2double(labels_temp); - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - if size(chosen_trials) ~= [minNumTrials*2,1] - chosen_trials = reshape(chosen_trials,[minNumTrials*2,1]); - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials)'; - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %End of SVM for loop - - mean_hitrate(s) = mean(hitrate) - sd_hitrate(s) = std(hitrate); - mean_hitrate_shuffled(s) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - % C_concat=cat(3,C{:}); %Get confusion matrix - % confusion_mat_avg=round(mean(C_concat,3)*100); %Average over SVM iterations - % rowNames{s} = {labels_id{:,1}}; colNames{s} = {labels_id{:,1}}; %Get behavior names - % C_table{s} = array2table(confusion_mat_avg,'RowNames',rowNames{s},'VariableNames',colNames{s}); - - clear labels_id - -end %End of session for loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); - -%Plot decoding accuracy for all sessions, separated by monkey -figure; hold on; -for s = a_sessions - y = mean_hitrate(s); - scatter(s, y, 'r', 'filled','MarkerFaceAlpha',0.7) -end -for s = h_sessions - y = mean_hitrate(s); - scatter(s, y, 'b', 'filled','MarkerFaceAlpha',0.7) -end -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16) -xlim([0.8 16.2]); ylim([0.4 1]) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Session#','FontSize', 18) -title('Decoding accuracy for brain area','FontSize', 14) - -saveas(gcf,['Decoding_BrainArea_allSessions.png']) -close all - diff --git a/Linear decoding/old_code/log_SVM.m b/Linear decoding/old_code/log_SVM.m deleted file mode 100644 index 451ab1c..0000000 --- a/Linear decoding/old_code/log_SVM.m +++ /dev/null @@ -1,346 +0,0 @@ -%% Log_SVM -%% Run a linear decoder on a the neural activity over all or a subset of behaviors -% This script allows to deocde a combination of factors: -% 1. Subject behavior -% 2. Partner's behavior -% 3. Predict future behavior (with specified lag time in terms of windows) -% 4. Detect past behaviors (with specified lag time in terms of windows) -% 5. Behavior shifts (irrespective of what the shift is and the TYPE of shift) -% 6. Window size used fo decoding behaviors can be manipulated -% 7. Brain area - -%% Load data - -%Set path -is_mac = 0; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -clearvars -except savePath filePath is_mac - -%Set parameters: -decode_categ =2; %1: decode partner or subject behavior; 2. Decode subject behavior change; 3.Decode block -partner =0; %Predicting partner behavior (partner =1) or subject behavior(partner=0) -predict=1; %Forward or backward lag. I.e. prediction of future behavior (predict =1) or impact of past behavior (predict=0)? - -%Set temporal resolution -temp = 1; temp_resolution = 1; -%for temp_resolution = [1/30, 1/20, 1/10, 1/5, 1/2, 1, 2, 5, 10] - %temp_resolution = [1/100,1/50,1/30, 1/20, 1/10, 1/5, 1/2, 1, 2, 5, 10] - %temp_resolution = [1, 2, 5, 10] %5sec, 2sec, 1sec,500msec, 100msec - %1 for second resolution, 10 for 100msec resolution, 100 for 10msec resolution, 1000 for msec resolution. etc. - %0.1 for 10sec resolution, 1/5 for 5sec resolution - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; channel_flag = "all"; - for channel_flag = ["vlPFC", "TEO", "all"] - - %Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac); - %filePath is the experimental data path - %Temp_resolution is the temporal resolution at which we would like to - %analyze the data - %Channel_flag specifies with channels to include: only TEO array, only - %vlPFC array or all channels - %is_mac is whether a mac or a pc is being used - disp('Data Loaded') - - Spike_count_raster_init = Spike_rasters'; - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - block_labels = cell2mat({labels{:,10}}'); %Extract block info - - %SANITY CHECK: Compute overlap between partner behavior and subject behavior - perc_overlap_allbehav = length(find(behavior_labels_subject_init == behavior_labels_partner_init))/length(behavior_labels_subject_init); - overlap = tabulate(behavior_labels_subject_init(find(behavior_labels_subject_init == behavior_labels_partner_init))); - subject_behav = tabulate(behavior_labels_subject_init); partner_behav = tabulate(behavior_labels_partner_init); - perc_overlap_perbehav = [behav_categ', overlap(:,2), overlap(:,2)./subject_behav(:,2)*100]; - %Important notes: - %1. There are some discrepancies when comparing the partner and - %subject labels. Most notably in proximity, but also RR, HIP, HIS, - %SP, SS which should be the same. - %2. I just realized that depending on what other behavior is - %co-occurring, the label can change. This may explain discrepancies - %in RR and proximity - %2 it is often the case that self-groom events co-occur. I expect - %this to be the case for foraging in Hooke-pair as well. - - %% Select subject or partner - - if decode_categ==1 - - if partner ==1 - behavior_labels_init = behavior_labels_partner_init; - else - behavior_labels_init = behavior_labels_subject_init; - end - - elseif decode_categ==2 - - subject_behav_change_reg = ones(size(labels,1), 1); %initialize - subject_behav_change_reg(find(diff(behavior_labels_subject_init)~=0)) = 2; - behavior_labels_init = subject_behav_change_reg; - %IMPORTANT NOTE: we are NOT able to decode behavior shifts vs. - %non shifts - - x=behavior_labels_subject_init(1:end-1); y=behavior_labels_subject_init(2:end); - behavior_labels_init= [sscanf(sprintf('%d%d,',[x.';y.']),'%d,'); 0]; - shifts = x-y; - - shift_categ_table= tabulate(behavior_labels_init(shifts~=0)); - shift_categ_table = shift_categ_table(shift_categ_table(:,2)~=0,:); - min_occurrences = 15; - behav = shift_categ_table(shift_categ_table(:,2)>=min_occurrences,1); - - %IMPORTANT NOTE: We are able to decode above change what the - %behavioral shift is when there is one! (that just happened or - %is just about to happen) - - elseif decode_categ==3 - - behavior_labels_init = block_labels; - end - - %% Time shift behaviors - - lag_length = 0; %Set length of time lag (in window size). Will include in a for loop eventually - - if predict %If trying to predict behavior - behavior_labels = behavior_labels_init(1+lag_length:end); - Spike_count_raster = Spike_count_raster_init(1:end-lag_length,:); - - %check the amount of labels that differ in the lag vs non-lagged versions - behavior_labels_nonlagged = behavior_labels_init(1:end-lag_length); - diff_behav_vectors = behavior_labels - behavior_labels_nonlagged; - perc_diff_before = round(length(find(diff_behav_vectors~=0))/length(diff_behav_vectors)*100); - fprintf('Percent difference between-time lagged and regular labels BEFORE selecting behaviors: %s \n', num2str(perc_diff_before)); - - else %If testing the influence of past behavior - behavior_labels = behavior_labels_init(1:end-lag_length); - Spike_count_raster = Spike_count_raster_init(1+lag_length:end,:); - - %check the amount of labels that differ in the lag vs non-lagged versions - behavior_labels_nonlagged = behavior_labels_init(1+lag_length:end,:); - diff_behav_vectors = behavior_labels - behavior_labels_nonlagged; - perc_diff_before = round(length(find(diff_behav_vectors~=0))/length(diff_behav_vectors)*100); - fprintf('Percent difference between-time lagged and regular labels BEFORE selecting behaviors: %s \n', num2str(perc_diff_before)); - end - - - %% Select behaviors to decode - - %Compute freq of behavior for the session -% behav_freq_table = tabulate(behavior_labels); -% behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences -% min_occurrences = 50; -% behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences -% behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. -% behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. -% behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - % OR select non-reciprocal behaviors (particularly important - % for decoding partner behavior) - %behav = setdiff(behav, reciprocal_set); - - % OR select behaviors manually - behav = [4:10,16,23];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - -% %Print behaviors selected -% behavs_eval = behav_categ(behav); -% disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') -% fprintf('Behaviors evaluated are: %s \n', behavs_eval); -% disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(behavior_labels_final); - - %check the amount of labels that differ in the lag vs non-lagged - %versions after selecting the behaviors of interest. - if lag_length~=0 - behavior_labels_final_nonlagged = behavior_labels_nonlagged(idx,:); - idx_diff_behav = find((behavior_labels_final - behavior_labels_final_nonlagged)~=0); - perc_diff_after = round(length(idx_diff_behav)/length(diff_behav_vectors)*100); - fprintf('Percent difference between-time lagged and regular labels AFTER selecting behaviors: %s \n', num2str(perc_diff_after)); - - %Only consider windows where the behaviors of the lagged and - %non-lagged versions do not overlap - Spike_count_raster_final = Spike_count_raster_final(idx_diff_behav,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels_final(idx_diff_behav,:);%Same as above but in behavior labels - tabulate(behavior_labels_final); - end - - %check the amount of labels that differ in the partner vs. subject - %labels after selecting the behaviors of interest. - if partner==1 - subject_behav_after_selection = behavior_labels_subject_init(idx); - partner_behav_after_selection = behavior_labels_partner_init(idx); - block_after_selection = block_labels(idx); - - overlap_partner_subject_after_selection = length(find((partner_behav_after_selection - subject_behav_after_selection)==0))/length(idx); - fprintf('Percent overlap between subject and partner labels AFTER selecting behaviors: %s \n', num2str(overlap_partner_subject_after_selection)); - alone_block_obs = length(find(block_after_selection==3))/length(idx); - fprintf('Percent observations in "alone" block AFTER selecting behaviors: %s \n', num2str(alone_block_obs)); - - %Only consider windows where the behaviors of subject and - %partner do not overlap - diff_idx = find((partner_behav_after_selection - subject_behav_after_selection)~=0); %find the indices where subject and partner behavior do not overlap - Spike_count_raster_final = Spike_count_raster_final(diff_idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels_final(diff_idx,:);%Same as above but in behavior labels - tabulate(behavior_labels_final); - -% %Only consider behaviors during the alone block -% block_after_selection_overlap_out = block_after_selection(diff_idx); -% alone_idx = find(block_after_selection_overlap_out==3); -% Spike_count_raster_final = Spike_count_raster_final(alone_idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data -% behavior_labels_final = behavior_labels_final(alone_idx,:);%Same as above but in behavior labels -% tabulate(behavior_labels_final); - end - - - %% Run SVM over multiple iterations - num_iter = 100; - - disp('Start running SVM...') - for iter = 1:num_iter - - clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... - Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... - channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %Balance number of trials per class - Labels = behavior_labels_final; - Input_matrix = Spike_count_raster_final; - - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); %labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min_occurrences;%min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate(temp, chan) = mean(hitrate) - sd_hitrate(temp, chan) = std(hitrate); - mean_hitrate_shuffled(temp, chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{temp, chan} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - chan = chan +1; - - clearvars -except is_mac savePath mean_hitrate sd_hitrate mean_hitrate_shuffled C_table temp_resolution channel_flag filePath chan temp behavs_eval behav - end - temp = temp+1; - -% end - -%rowNames = ["100sec","50sec","30sec","20sec","10sec","5sec","2sec","1sec","500msec","200msec","100ms"]; colNames = ["vlPFC","TEO","all"]; -rowNames = ["30sec","20sec","10sec","5sec","2sec","1sec","500msec","200msec","100ms"]; colNames = ["vlPFC","TEO","all"]; -%rowNames = ["1sec", "500msec","200msec", "100msec"]; colNames = ["vlPFC","TEO","all"]; -%rowNames = ["1sec"]; colNames = ["vlPFC","TEO","all"]; -result_hitrate = array2table(mean_hitrate,'RowNames',rowNames,'VariableNames',colNames) -result_sdhitrate = array2table(sd_hitrate,'RowNames',rowNames,'VariableNames',colNames) - -% save([savePath '\SVM_results_social_context.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -% writetable(result_hitrate,[savePath '\SVM_results_social_context.csv'],'WriteRowNames',true,'WriteVariableNames',true); -save([savePath '\SVM_results_' num2str(length(behav)) 'behav.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -writetable(result_hitrate,[savePath '\SVM_results_' num2str(length(behav)) 'behav.csv'],'WriteRowNames',true,'WriteVariableNames',true); - -%Plotting results for multiple behaviors at 1sec and lower resolution -figure; hold on; set(gcf,'Position',[150 250 1000 500]) -cmap = hsv(size(mean_hitrate,1)); -for b = 1:size(mean_hitrate,1) - y = mean_hitrate(b,:); - std_dev = sd_hitrate(b,:); - errorbar(y,std_dev,'s','MarkerSize',10,... - 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) - %plot(x,y,'Color','k') -end -leg = legend([rowNames, 'Chance']); -title(leg,'Window size') -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -%title('Decoding accuracy for behavioral states','FontSize', 20) -title('Decoding accuracy for social context','FontSize', 20) - -cd(savePath) -%saveas(gcf,['SVM_results_SocialContext.png']) -%saveas(gcf,['SVM_results_grooming.png']) -saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav.png']) -close all - -%Plotting for all channels across many time windows - including long one. -%For grooming in vs. out. -figure; hold on; set(gcf,'Position',[150 250 1500 700]) -cmap = hsv(size(mean_hitrate,1)); -y = mean_hitrate(:,3); y = y(end:-1:1); -x = 1:size(mean_hitrate,1); -std_dev = sd_hitrate(:,3); -errorbar(x,y,std_dev,'-s','MarkerSize',10) -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0 1:length(x) length(x)+1]);ylim([0 1]); xlim([0 10]) -xticklabels({'','100msec','200msec','500msec','1sec','2sec','5sec','10sec','20sec','30sec',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Window size','FontSize', 18) -title('Decoding accuracy for grooming by window size','FontSize', 20) - -cd(savePath) -saveas(gcf,['SVM_results_grooming_allChannels.png']) diff --git a/Linear decoding/old_code/log_SVM_CrossBrainArea_batch.m b/Linear decoding/old_code/log_SVM_CrossBrainArea_batch.m deleted file mode 100644 index a990440..0000000 --- a/Linear decoding/old_code/log_SVM_CrossBrainArea_batch.m +++ /dev/null @@ -1,284 +0,0 @@ -%% Log_SVM_subject_batch -% Run a linear decoder on the neural activity for the subject's behavior -% (only including behaviors with a minimum # occurrence) -% Batch version of script -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =1; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -mean_hitrate_baseline = nan(1,max(session_range)); -mean_hitrate = nan(1,max(session_range)); -mean_hitrate_shuffled = nan(1,max(session_range)); - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - - %Low-pass filter data - %Spike_count_raster = lowpass(Spike_rasters',0.05,1); - - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject -% behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Threat to partner"); -% behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Threat to subject"); -% behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - co_occurrence = cell2mat({labels{:,5}}'); - - if null - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels,behav_categ, temp_resolution); - behavior_labels = sim_behav; - end - - if unq_behav==1%Select epochs where only one behavior happens at any given time (i.e. no co-occurrence). - idx_single = find(co_occurrence<4); %(no co-occurrence, or with RR or with proximity) - Spike_count_raster = Spike_count_raster(idx_single,:); - behavior_labels = behavior_labels(idx_single,:); - end - - if simplify - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - end - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - % Remove behaviors that are ill-defined - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Vocalization')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - % OR select behaviors manually - %behav = [5,18];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(removecats(categorical(behavior_labels_final))); - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - - %subsample to match number of neurons across brain areas - if randomsample==1 - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - traindata = Spike_count_raster_final(:,strcmp(brain_label, "TEO")); trainlbls = behavior_labels_final; - testdata = Spike_count_raster_final(:,strcmp(brain_label, "vlPFC")); testlbls = trainlbls; - traindata = traindata(:,randsample(size(traindata,2), 110,'false')); testdata = testdata(:,randsample(size(testdata,2), 110,'false')); - - %Balance number of trials for train data - uniqueLabels = unique(trainlbls); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = trainlbls; - for i=1:NumOfClasses - idx = trainlbls == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - trainlbls = labels_temp; - - num_trials = hist(trainlbls,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(trainlbls == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - traindata = traindata(chosen_trials, :); - trainlbls = trainlbls(chosen_trials, :); - trainlbls_shuffled = trainlbls(randperm(length(trainlbls))); - - %Balance number of trials for test data - uniqueLabels = unique(testlbls); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = testlbls; - for i=1:NumOfClasses - idx = testlbls == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - testlbls = labels_temp; - - num_trials = hist(testlbls,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(testlbls == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - testdata = testdata(chosen_trials, :); - testlbls = testlbls(chosen_trials, :); - testlbls_shuffled = testlbls(randperm(length(testlbls))); - - % Run svm - [hitrate_baseline(iter)] = log_SVM_basic_function(traindata, trainlbls, 5, 0, 0); - [hitrate(iter), C{iter}] = log_SVM_ccgp_function(trainlbls,traindata,... - testlbls, testdata, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_ccgp_function(trainlbls,traindata,... - testlbls_shuffled, testdata, 0, 0); - - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %End of SVM for loop - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate_baseline(s) = mean(hitrate_baseline) - mean_hitrate(s) = mean(hitrate) - sd_hitrate(s) = std(hitrate); - mean_hitrate_shuffled(s) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); %Get confusion matrix - confusion_mat_avg=round(mean(C_concat,3)*100); %Average over SVM iterations - rowNames{s} = {labels_id{:,2}}; colNames{s} = {labels_id{:,2}}; %Get behavior names - C_table{s} = array2table(confusion_mat_avg,'RowNames',rowNames{s},'VariableNames',colNames{s}); - - clear labels_id - - - cd(savePath) - -end %End of session for loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_crossAreas.mat', "mean_hitrate","sd_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") -%load('SVM_results_subjectBehav.mat') - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data_baseline = mean_hitrate_baseline'; -data = mean_hitrate'; -data_shuffle = mean_hitrate_shuffled'; -bp = bar([nanmean(data_baseline); nanmean(data); nanmean(data_shuffle)],'FaceAlpha',0.2); - -sp1 = scatter(ones(1,size(data,1)),data_baseline, 'filled','b'); -sp1 = scatter(ones(1,size(data,1))*2,data, 'filled','b'); -sp1 = scatter(ones(1,size(data,1))*3,data_shuffle, 'filled','b'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0 1]) -xticks([1 2 3]); xticklabels({'Baseline', 'Cross-area' , 'Shuffled'}); xlim([0.25 3.75]) -ax = gca; -ax.FontSize = 16; - -saveas(gcf,['SVM_results_crossAreas.pdf']) - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% % % % % %Confusion matrices -% % % % % vlpfc = table2array(C_table{s,1,1}); TEO = table2array(C_table{s,1,2}); all_units = table2array(C_table{s,1,3}); -% % % % % D{s} = vlpfc-TEO; -% % % % % CustomAxisLabels = string(C_table{s,1,1}.Properties.VariableNames); figure; set(gcf,'Position',[150 250 1500 800]); -% % % % % subplot(2,2,1); pfc = heatmap(vlpfc,'Colormap', hot); pfc.XDisplayLabels = CustomAxisLabels; pfc.YDisplayLabels = CustomAxisLabels; title(pfc,'vlpfc confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,2); teo = heatmap(TEO,'Colormap', hot); teo.XDisplayLabels = CustomAxisLabels; teo.YDisplayLabels = CustomAxisLabels; title(teo,'TEO confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,3); all = heatmap(all_units,'Colormap', hot); all.XDisplayLabels = CustomAxisLabels; all.YDisplayLabels = CustomAxisLabels; title(all,'All units confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,4); h = heatmap(D,'Colormap', cool); h.XDisplayLabels = CustomAxisLabels; h.YDisplayLabels = CustomAxisLabels; title(h,'vlpfc-TEO confusion matrix');caxis([-50, 50]); diff --git a/Linear decoding/old_code/log_SVM_CrossNeurons_batch.m b/Linear decoding/old_code/log_SVM_CrossNeurons_batch.m deleted file mode 100644 index 0406edd..0000000 --- a/Linear decoding/old_code/log_SVM_CrossNeurons_batch.m +++ /dev/null @@ -1,280 +0,0 @@ -%% Log_SVM_subject_batch -% Run a linear decoder cross neuron populations (i.e. train based on a set -% of neurons and test based on another set of neurons) -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =1; -temp = 1; temp_resolution = 1; -channel_flag = "vlPFC"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -mean_hitrate_baseline = nan(1,max(session_range)); -mean_hitrate = nan(1,max(session_range)); -mean_hitrate_shuffled = nan(1,max(session_range)); - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - - %Low-pass filter data - %Spike_count_raster = lowpass(Spike_rasters',0.05,1); - - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject -% behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Threat to partner"); -% behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Threat to subject"); -% behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - co_occurrence = cell2mat({labels{:,5}}'); - - if null - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels,behav_categ, temp_resolution); - behavior_labels = sim_behav; - end - - if unq_behav==1%Select epochs where only one behavior happens at any given time (i.e. no co-occurrence). - idx_single = find(co_occurrence<4); %(no co-occurrence, or with RR or with proximity) - Spike_count_raster = Spike_count_raster(idx_single,:); - behavior_labels = behavior_labels(idx_single,:); - end - - if simplify - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - end - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - % Remove behaviors that are ill-defined - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Vocalization')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - % OR select behaviors manually - %behav = [5,18];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(removecats(categorical(behavior_labels_final))); - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - - %subsample to match number of neurons across brain areas - - idx_set1 = randsample(size(Spike_count_raster_final,2),round(size(Spike_count_raster_final,2)/2)); - idx_set2 = setdiff(1:size(Spike_count_raster_final,2),idx_set1)'; - - traindata = Spike_count_raster_final(:,idx_set1); trainlbls = behavior_labels_final; - testdata = Spike_count_raster_final(:,idx_set2); testlbls = trainlbls; - - %Balance number of trials for train data - uniqueLabels = unique(trainlbls); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = trainlbls; - for i=1:NumOfClasses - idx = trainlbls == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - trainlbls = labels_temp; - - num_trials = hist(trainlbls,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(trainlbls == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - traindata = traindata(chosen_trials, :); - trainlbls = trainlbls(chosen_trials, :); - trainlbls_shuffled = trainlbls(randperm(length(trainlbls))); - - %Balance number of trials for test data - uniqueLabels = unique(testlbls); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = testlbls; - for i=1:NumOfClasses - idx = testlbls == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - testlbls = labels_temp; - - num_trials = hist(testlbls,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(testlbls == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - testdata = testdata(chosen_trials, :); - testlbls = testlbls(chosen_trials, :); - testlbls_shuffled = testlbls(randperm(length(testlbls))); - - % Run svm - [hitrate_baseline(iter)] = log_SVM_basic_function(traindata, trainlbls, 5, 0, 0); - [hitrate(iter), C{iter}] = log_SVM_ccgp_function(trainlbls,traindata,... - testlbls, testdata, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_ccgp_function(trainlbls,traindata,... - testlbls_shuffled, testdata, 0, 0); - - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %End of SVM for loop - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate_baseline(s) = mean(hitrate_baseline) - mean_hitrate(s) = mean(hitrate) - sd_hitrate(s) = std(hitrate); - mean_hitrate_shuffled(s) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); %Get confusion matrix - confusion_mat_avg=round(mean(C_concat,3)*100); %Average over SVM iterations - rowNames{s} = {labels_id{:,2}}; colNames{s} = {labels_id{:,2}}; %Get behavior names - C_table{s} = array2table(confusion_mat_avg,'RowNames',rowNames{s},'VariableNames',colNames{s}); - - clear labels_id - - - cd(savePath) - -end %End of session for loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_crossAreas.mat', "mean_hitrate","sd_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") -%load('SVM_results_subjectBehav.mat') - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data_baseline = mean_hitrate_baseline'; -data = mean_hitrate'; -data_shuffle = mean_hitrate_shuffled'; -bp = bar([nanmean(data_baseline); nanmean(data); nanmean(data_shuffle)],'FaceAlpha',0.2); - -sp1 = scatter(ones(1,size(data,1)),data_baseline, 'filled','b'); -sp1 = scatter(ones(1,size(data,1))*2,data, 'filled','b'); -sp1 = scatter(ones(1,size(data,1))*3,data_shuffle, 'filled','b'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0 1]) -xticks([1 2 3]); xticklabels({'Baseline', 'Cross-area' , 'Shuffled'}); xlim([0.25 3.75]) -ax = gca; -ax.FontSize = 16; - -saveas(gcf,['SVM_results_crossAreas.pdf']) - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% % % % % %Confusion matrices -% % % % % vlpfc = table2array(C_table{s,1,1}); TEO = table2array(C_table{s,1,2}); all_units = table2array(C_table{s,1,3}); -% % % % % D{s} = vlpfc-TEO; -% % % % % CustomAxisLabels = string(C_table{s,1,1}.Properties.VariableNames); figure; set(gcf,'Position',[150 250 1500 800]); -% % % % % subplot(2,2,1); pfc = heatmap(vlpfc,'Colormap', hot); pfc.XDisplayLabels = CustomAxisLabels; pfc.YDisplayLabels = CustomAxisLabels; title(pfc,'vlpfc confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,2); teo = heatmap(TEO,'Colormap', hot); teo.XDisplayLabels = CustomAxisLabels; teo.YDisplayLabels = CustomAxisLabels; title(teo,'TEO confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,3); all = heatmap(all_units,'Colormap', hot); all.XDisplayLabels = CustomAxisLabels; all.YDisplayLabels = CustomAxisLabels; title(all,'All units confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,4); h = heatmap(D,'Colormap', cool); h.XDisplayLabels = CustomAxisLabels; h.YDisplayLabels = CustomAxisLabels; title(h,'vlpfc-TEO confusion matrix');caxis([-50, 50]); diff --git a/Linear decoding/old_code/log_SVM_ME.m b/Linear decoding/old_code/log_SVM_ME.m deleted file mode 100644 index 75beec2..0000000 --- a/Linear decoding/old_code/log_SVM_ME.m +++ /dev/null @@ -1,182 +0,0 @@ -%% Log_SVM -%% Run a linear decoder on a the neural activity over all or a subset of behaviors -% This script allows to deocde a combination of factors: -% 1. Subject behavior -% 2. Partner's behavior -% 3. Predict future behavior (with specified lag time in terms of windows) -% 4. Detect past behaviors (with specified lag time in terms of windows) -% 5. Behavior shifts (irrespective of what the shift is and the TYPE of shift) -% 6. Window size used fo decoding behaviors can be manipulated -% 7. Brain area - -%% Load data - -%Set path -is_mac = 1; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -clearvars -except savePath filePath is_mac - -%Set temporal resolution -temp = 1; temp_resolution = 1; -chan = 1; channel_flag = "all"; - -for channel_flag = ["vlPFC", "TEO", "all"] - - %Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac); - %filePath is the experimental data path - %Temp_resolution is the temporal resolution at which we would like to - %analyze the data - %Channel_flag specifies with channels to include: only TEO array, only - %vlPFC array or all channels - %is_mac is whether a mac or a pc is being used - disp('Data Loaded') - - Spike_count_raster_final = Spike_rasters'; - behavior_labels_final = cell2mat({labels{:,12}}'); %Extract unique behavior info for subject - - - %% Run SVM over multiple iterations - num_iter = 500; - - disp('Start running SVM...') - for iter = 1:num_iter - -% clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... -% Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... -% channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %Balance number of trials per class - Labels = behavior_labels_final; - Input_matrix = Spike_count_raster_final; - - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); %labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = 30;%min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate(temp, chan) = mean(hitrate) - sd_hitrate(temp, chan) = std(hitrate); - mean_hitrate_shuffled(temp, chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{temp, chan} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - chan = chan +1; - - clearvars -except is_mac savePath mean_hitrate sd_hitrate mean_hitrate_shuffled C_table temp_resolution channel_flag filePath chan temp behavs_eval behav -end -% temp = temp+1; - - - -%rowNames = ["100sec","50sec","30sec","20sec","10sec","5sec","2sec","1sec","500msec","200msec","100ms"]; colNames = ["vlPFC","TEO","all"]; -rowNames = ["30sec","20sec","10sec","5sec","2sec","1sec","500msec","200msec","100ms"]; colNames = ["vlPFC","TEO","all"]; -%rowNames = ["1sec", "500msec","200msec", "100msec"]; colNames = ["vlPFC","TEO","all"]; -%rowNames = ["1sec"]; colNames = ["vlPFC","TEO","all"]; -result_hitrate = array2table(mean_hitrate,'RowNames',rowNames,'VariableNames',colNames) -result_sdhitrate = array2table(sd_hitrate,'RowNames',rowNames,'VariableNames',colNames) - -% save([savePath '\SVM_results_social_context.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -% writetable(result_hitrate,[savePath '\SVM_results_social_context.csv'],'WriteRowNames',true,'WriteVariableNames',true); -save([savePath '\SVM_results_' num2str(length(behav)) 'behav.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -writetable(result_hitrate,[savePath '\SVM_results_' num2str(length(behav)) 'behav.csv'],'WriteRowNames',true,'WriteVariableNames',true); - -%Plotting results for multiple behaviors at 1sec and lower resolution -figure; hold on; set(gcf,'Position',[150 250 1000 500]) -cmap = hsv(size(mean_hitrate,1)); -for b = 1:size(mean_hitrate,1) - y = mean_hitrate(b,:); - std_dev = sd_hitrate(b,:); - errorbar(y,std_dev,'s','MarkerSize',10,... - 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) - %plot(x,y,'Color','k') -end -leg = legend([rowNames, 'Chance']); -title(leg,'Window size') -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -%title('Decoding accuracy for behavioral states','FontSize', 20) -title('Decoding accuracy for social context','FontSize', 20) - -cd(savePath) -%saveas(gcf,['SVM_results_SocialContext.png']) -%saveas(gcf,['SVM_results_grooming.png']) -saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav.png']) -close all - -%Plotting for all channels across many time windows - including long one. -%For grooming in vs. out. -figure; hold on; set(gcf,'Position',[150 250 1500 700]) -cmap = hsv(size(mean_hitrate,1)); -y = mean_hitrate(:,3); y = y(end:-1:1); -x = 1:size(mean_hitrate,1); -std_dev = sd_hitrate(:,3); -errorbar(x,y,std_dev,'-s','MarkerSize',10) -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0 1:length(x) length(x)+1]);ylim([0 1]); xlim([0 10]) -xticklabels({'','100msec','200msec','500msec','1sec','2sec','5sec','10sec','20sec','30sec',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Window size','FontSize', 18) -title('Decoding accuracy for grooming by window size','FontSize', 20) - -cd(savePath) -saveas(gcf,['SVM_results_grooming_allChannels.png']) diff --git a/Linear decoding/old_code/log_SVM_NeuralStates.py b/Linear decoding/old_code/log_SVM_NeuralStates.py deleted file mode 100644 index 0c8d0e1..0000000 --- a/Linear decoding/old_code/log_SVM_NeuralStates.py +++ /dev/null @@ -1,77 +0,0 @@ -# -*- coding: utf-8 -*- -""" -Created on Sun Sep 5 13:07:47 2021 - -Run linear decoder - -@author: CT -""" -#%% Import modules - -import os -import scipy.io -import numpy as np #general math stuff -import matplotlib.pyplot as plt #some plotting stuff -from sklearn import svm #This allows us to use non linear svm models if needed -from sklearn.linear_model import Perceptron #not used but another fitting technique we could use if needed -from sklearn.svm import LinearSVC #This is the linear svc model from scikit learn, can -from sklearn.model_selection import cross_validate, ShuffleSplit #Get stuff for cross validations - -## Import data -os.chdir('D:/Deuteron Data/Ready to analyze output/Amos_2021-07-29/') -full_data = scipy.io.loadmat('SVM_input.mat') - -## Run model -my_labels = full_data['Labels'] #Put labels for each time point here, labels should be numeric I think -my_labels = np.squeeze(my_labels) -my_labels_shuffled = full_data['Labels_shuffled'] -my_labels_shuffled = np.squeeze(my_labels_shuffled) -#idx = np.squeeze(np.logical_or(my_labels ==6, my_labels==5, my_labels==4)) -#my_labels_no_zero = np.squeeze(my_labels[idx]) - -my_data = full_data['Input_matrix'] #Put neural data here, needs to be time x neurons -#my_data_no_zero = my_data[idx,:] - - -linSVC = LinearSVC(max_iter = 100000, tol = 0.001)#, class_weight = 'balanced') #Perceptron(max_iter = 10000, tol = 0.001) #from scikit (presumably) #set up Perceptron classifier - -crossval = ShuffleSplit(n_splits = 10, test_size = 0.20) - -cv_results = cross_validate(linSVC, my_data, my_labels, cv=crossval) #This stores results as dictionary - -avg_score = np.mean(cv_results['test_score']) #take average of the cv_results to - -print(avg_score) - -#Notes: -# Groom receive, groom given, foraging, HIP, HIS, SP, SS yields 94% accuracy when considering both arrays. -# When only considering array 1 (TEO), accuracy drops to 93%. When considering array #2, accuracy remains at 95%. -#Adding scratch affects performance (drops from 94% to 86%, although chance level drops to 1/8) -#Adding self-groom drives performance down to 91% -#When considering 10 classes of behaviorm accuracy is still really high (82% accuracy) when considering both arrays. - -#%% Quick plotting - - - -plot_splits = 1 - -#note this can create a lot of figures as it creates out one for each split. - - -if plot_splits: - for train_index, test_index in crossval.split(my_data): - - - plt.figure() - plt.hist(my_labels[train_index]) - plt.title('training data distribution for each split') - - - for train_index, test_index in crossval.split(my_data): - - - plt.figure() - plt.hist(my_labels_no_zero[test_index]) - plt.title('testing data distribution for each split') - diff --git a/Linear decoding/old_code/log_SVM_SocialContext.m b/Linear decoding/old_code/log_SVM_SocialContext.m deleted file mode 100644 index d46376c..0000000 --- a/Linear decoding/old_code/log_SVM_SocialContext.m +++ /dev/null @@ -1,230 +0,0 @@ -%% Log_SVM -%% Run a linear decoder on a the neural activity over all or a subset of behaviors -% This script allows to deocde a combination of factors: -% 1. Subject behavior -% 2. Partner's behavior -% 3. Predict future behavior (with specified lag time in terms of windows) -% 4. Detect past behaviors (with specified lag time in terms of windows) -% 5. Behavior shifts (irrespective of what the shift is and the TYPE of shift) -% 6. Window size used fo decoding behaviors can be manipulated -% 7. Brain area - -%% Load data - -%Set path -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:18]; -session_range_with_partner=[1:3,11:13]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:18]; -end - -s=18; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - for channel_flag = ["vlPFC", "TEO", "all"] - - %Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final, unit_count]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac); - %filePath is the experimental data path - %Temp_resolution is the temporal resolution at which we would like to - %analyze the data - %Channel_flag specifies with channels to include: only TEO array, only - %vlPFC array or all channels - %is_mac is whether a mac or a pc is being used - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - block_labels = cell2mat({labels{:,11}}'); - - %% Check which behaviors occur in different blocks - - % unq_behav = unique(behavior_labels); - % behav_in_block = zeros(length(unq_behav), 3); - % for b = 1:length(unq_behav) - % for bl = 1:3 - % behav_in_block(b, bl) = length(intersect(find(behavior_labels == unq_behav(b)), find(block_labels==bl))); - % end - % end - % - % figure; hold on; set(gcf,'Position',[150 250 1500 500]) - % bar(behav_in_block, 'stacked') - % xticks(1:length(unq_behav)) - % xticklabels(behav_categ(unq_behav)); - % xtickangle(45) - % leg = legend(block_times.Behavior{:}); - % title(leg,'Block') - - %% Select behaviors to decode - - % Select behaviors which occur in multiple blocks and the - behav =[find(matches(behav_categ,'Groom Give')), find(matches(behav_categ,'Groom Receive'))]; %manually select behaviors of interest - %behav =[find(matches(behav_categ,'Threat to subject')), find(matches(behav_categ,'Threat to partner'))]; - %behav = find(matches(behav_categ,'Rest')); - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = block_labels(idx,:);%Same as above but in behavior labels - tabulate(behavior_labels_final); - - %% Run SVM over multiple iterations - num_iter = 1000; - - disp('Start running SVM...') - for iter = 1:num_iter - - % clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... - % Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... - % channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %subsample to match number of neurons across brain areas - Labels = behavior_labels_final; - if randomsample==1 && channel_flag~="all" - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = 30;%min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate(temp, chan) = mean(hitrate) - sd_hitrate(temp, chan) = std(hitrate); - mean_hitrate_shuffled(temp, chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{temp, chan} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - chan = chan +1; - - end - % temp = temp+1; - - cd(savePath) - - %Plot confusion matrices - vlpfc = table2array(C_table{:,1}); TEO = table2array(C_table{1,2}); all_units = table2array(C_table{1,3}); - D = vlpfc-TEO; - CustomAxisLabels = string(C_table{1,1}.Properties.VariableNames) - figure; pfc = heatmap(vlpfc,'Colormap', jet); pfc.XDisplayLabels = CustomAxisLabels; pfc.YDisplayLabels = CustomAxisLabels; title(pfc,'vlpfc confusion matrix'); caxis([0, 100]); - figure; teo = heatmap(TEO,'Colormap', jet); teo.XDisplayLabels = CustomAxisLabels; teo.YDisplayLabels = CustomAxisLabels; title(teo,'TEO confusion matrix'); caxis([0, 100]); - figure; all = heatmap(all_units,'Colormap', jet); all.XDisplayLabels = CustomAxisLabels; all.YDisplayLabels = CustomAxisLabels; title(all,'All units confusion matrix'); caxis([0, 100]); - figure; h = heatmap(D,'Colormap', jet); h.XDisplayLabels = CustomAxisLabels; h.YDisplayLabels = CustomAxisLabels; title(h,'vlpfc-TEO confusion matrix');caxis([-50, 50]); - - saveas(pfc,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_unique_confmatPFC.png']) - saveas(teo,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_unique_confmatTEO.png']) - saveas(all,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_unique_confmatALL.png']) - saveas(h,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_unique_confmatPFC-TEO.png']) - - close all -end - -rowNames = ["1sec"]; colNames = ["vlPFC","TEO","all"]; -result_hitrate = array2table(mean_hitrate,'RowNames',rowNames,'VariableNames',colNames) -result_hitrate_shuffled = array2table(mean_hitrate_shuffled,'RowNames',rowNames,'VariableNames',colNames) -result_sdhitrate = array2table(sd_hitrate,'RowNames',rowNames,'VariableNames',colNames) - -% save([savePath '\SVM_results_social_context.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -% writetable(result_hitrate,[savePath '\SVM_results_social_context.csv'],'WriteRowNames',true,'WriteVariableNames',true); -save([savePath '\SVM_results_' num2str(length(behav)) 'behav_subsample_unique.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -writetable(result_hitrate,[savePath '\SVM_results_' num2str(length(behav)) 'behav_subsample_unique.csv'],'WriteRowNames',true,'WriteVariableNames',true); - -%Plotting results decoding accuracy for all behaviors at 1sec and lower resolution -figure; hold on; set(gcf,'Position',[150 250 700 500]) -cmap = hsv(size(mean_hitrate,1)); -for b = 1:size(mean_hitrate,1) - y = mean_hitrate(b,:); - std_dev = sd_hitrate(b,:); - errorbar(y,std_dev,'s','MarkerSize',10,... - 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) - %plot(x,y,'Color','k') -end -%leg = legend([rowNames, 'Chance']); -%title(leg,'Window size') -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for subject current behavioral states (unique behaviors, subsample neurons)','FontSize', 14) - -cd(savePath) -saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_subsample_unique.png']) -close all - diff --git a/Linear decoding/old_code/log_SVM_SocialContext_batch.m b/Linear decoding/old_code/log_SVM_SocialContext_batch.m deleted file mode 100644 index b91fc6f..0000000 --- a/Linear decoding/old_code/log_SVM_SocialContext_batch.m +++ /dev/null @@ -1,257 +0,0 @@ -%% Log_SVM_SocialContext_batch -% Run a linear decoder on a the neural activity to decode social context -% (neighbor ID, paired or not). -% Batch version -% March 2022 Camille Testard - - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0; %lump similar behavioral categories together -min_occurrence = 30; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - for channel_flag = ["vlPFC", "TEO", "all"] - - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - block_labels = cell2mat({labels{:,12}}'); - - %% Check which behaviors occur in different blocks - - % unq_behav = unique(behavior_labels); - % behav_in_block = zeros(length(unq_behav), 3); - % for b = 1:length(unq_behav) - % for bl = 1:3 - % behav_in_block(b, bl) = length(intersect(find(behavior_labels == unq_behav(b)), find(block_labels==bl))); - % end - % end - % - % figure; hold on; set(gcf,'Position',[150 250 1500 500]) - % bar(behav_in_block, 'stacked') - % xticks(1:length(unq_behav)) - % xticklabels(behav_categ(unq_behav)); - % xtickangle(45) - % leg = legend(block_times.Behavior{:}); - % title(leg,'Block') - - %% Select behaviors to decode - - if simplify == 1 - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - behav = [1,5,7,8,18,29]; - else - behav = [4,5,7,8,9,10,24,29]; - end - - % For all behaviors - for b=1:length(behav) - - % Select behaviors which occur in multiple blocks - %behav =[find(matches(behav_categ,'Groom partner')), find(matches(behav_categ,'Getting groomed'))]; %manually select behaviors of interest - %behav =[find(matches(behav_categ,'Threat to subject')), find(matches(behav_categ,'Threat to partner'))]; - %behav = find(matches(behav_categ,'Rest')); - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav(b))); %find the indices of the behaviors considered - % y=zeros(1, size(Spike_count_raster)); y(idx)=1; - % figure; plot(1:session_length, y); ylim([-0.5, 1.5]) - % yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) - % xlabel('Time in s'); title('Baseline epochs') - % set(gca,'FontSize',15); - % close all - - % % % %Only consider indices close in time - % % % idx_close_in_time = block_times{2,"start_time_round"}-200:block_times{2,"start_time_round"}+200; - % % % idx = intersect(idx, idx_close_in_time'); - % % % hist(idx) - - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = block_labels(idx,:);%Same as above but in behavior labels - behavfreq= tabulate(behavior_labels_final); - - if ~isempty(idx) - - if all(behavfreq(:,2)>=min_occurrence) - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - Labels = behavior_labels_final; - - %subsample to match number of neurons across brain area: - if randomsample==1 && channel_flag~="all" - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - - minNumTrials = min_occurrence;%min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - %disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - mean_hitrate{b}(s, chan) = mean(hitrate) - sd_hitrate{b}(s, chan) = std(hitrate); - mean_hitrate_shuffled{b}(s, chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled{b}(s, chan) = std(hitrate_shuffled); - - clear labels_id - - disp(['Behavior: ' behav_categ(behav(b)) ' done.']) - - end %end of min # occurrence clause - end %end of if not empty loop - - end%end of behavior loop - - chan = chan +1; - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - end %end of channel loop - % temp = temp+1; - - -end %end of session loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd(['~/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_allBehavs_NeighborID.mat', "mean_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") -load('SVM_allBehavs_NeighborID.mat') - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - - -figure; hold on; set(gcf,'Position',[150 250 1500 500]); uplimit=20; - -for b=1:length(behav) - - subplot(2,4,b); hold on - -data = mean_hitrate{b}; data(data==0)=nan; -data_shuffled = mean_hitrate_shuffled{b}; data_shuffled(data_shuffled==0)=nan; -bp = bar([nanmean(data(:,:)); nanmean(data_shuffled(:,:))],'FaceAlpha',0.2); - -sp1 = scatter(ones(size(data,1))*0.77,data(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*1.22,data(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data,1))*1.77,data_shuffled(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1))*2,data_shuffled(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*2.22,data_shuffled(:,3), 'filled','y'); - -%legend(bp,{'vlPFC','TEO','all'},'Location','best') -title(behav_categ(behav(b))) - -ylabel('Decoding Accuracy'); ylim([0.2 1]) -xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) -ax = gca; -ax.FontSize = 16; - -end -saveas(gcf,['SVM_results_allSessions_allBehavs_SocialContext.pdf']) diff --git a/Linear decoding/old_code/log_SVM_SocialContext_batch_old.m b/Linear decoding/old_code/log_SVM_SocialContext_batch_old.m deleted file mode 100644 index 77b0a36..0000000 --- a/Linear decoding/old_code/log_SVM_SocialContext_batch_old.m +++ /dev/null @@ -1,259 +0,0 @@ -%% Log_SVM_SocialContext_batch -% Run a linear decoder on a the neural activity to decode social context -% (neighbor ID, paired or not). -% Batch version -% March 2022 Camille Testard - - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0; %lump similar behavioral categories together - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - for channel_flag = ["vlPFC", "TEO", "all"] - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - block_labels = cell2mat({labels{:,12}}'); - - %% Check which behaviors occur in different blocks - - % unq_behav = unique(behavior_labels); - % behav_in_block = zeros(length(unq_behav), 3); - % for b = 1:length(unq_behav) - % for bl = 1:3 - % behav_in_block(b, bl) = length(intersect(find(behavior_labels == unq_behav(b)), find(block_labels==bl))); - % end - % end - % - % figure; hold on; set(gcf,'Position',[150 250 1500 500]) - % bar(behav_in_block, 'stacked') - % xticks(1:length(unq_behav)) - % xticklabels(behav_categ(unq_behav)); - % xtickangle(45) - % leg = legend(block_times.Behavior{:}); - % title(leg,'Block') - - %% Select behaviors to decode - - for behav = [5, 7, 8, 9, 10, ] - - % Select behaviors which occur in multiple blocks - %behav =[find(matches(behav_categ,'Groom partner')), find(matches(behav_categ,'Getting groomed'))]; %manually select behaviors of interest - %behav =[find(matches(behav_categ,'Threat to subject')), find(matches(behav_categ,'Threat to partner'))]; - %behav = find(matches(behav_categ,'Rest')); - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered -% y=zeros(1, size(Spike_count_raster)); y(idx)=1; -% figure; plot(1:session_length, y); ylim([-0.5, 1.5]) -% yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) -% xlabel('Time in s'); title('Baseline epochs') -% set(gca,'FontSize',15); -% close all - -% % % %Only consider indices close in time -% % % idx_close_in_time = block_times{2,"start_time_round"}-200:block_times{2,"start_time_round"}+200; -% % % idx = intersect(idx, idx_close_in_time'); -% % % hist(idx) - - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = block_labels(idx,:);%Same as above but in behavior labels - tabulate(behavior_labels_final); - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - Labels = behavior_labels_final; - - %subsample to match number of neurons across brain area: - if randomsample==1 && channel_flag~="all" - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = 50;%min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate{s}(temp, chan) = mean(hitrate) - sd_hitrate{s}(temp, chan) = std(hitrate); - mean_hitrate_shuffled{s}(temp, chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{s, chan} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - chan = chan +1; - clear labels_id - - end %end of channel loop - % temp = temp+1; - - -end %end of session loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_NeighborID.mat', "mean_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") - -%Plot decoding accuracy for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for neighbor ID during grooming, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Decoding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for neighbor ID during grooming, Monkey H','FontSize', 14) - - saveas(gcf,['SVM_results_social_context_allSessions.png']) - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data = cell2mat(mean_hitrate'); -data_shuffle = cell2mat(mean_hitrate_shuffled'); -bp = bar([mean(data(:,:)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); - -sp1 = scatter(ones(size(data,1))*0.77,data(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*1.22,data(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data,1))*1.77,data_shuffle(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1))*2,data_shuffle(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*2.22,data_shuffle(:,3), 'filled','y'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0.4 1]) -xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) -ax = gca; -ax.FontSize = 16; -saveas(gcf,['SVM_results_allSessions_allUnits_SocialContext.png']) diff --git a/Linear decoding/old_code/log_SVM_Subject_confusionMatrix_batch.m b/Linear decoding/old_code/log_SVM_Subject_confusionMatrix_batch.m deleted file mode 100644 index ab1e77f..0000000 --- a/Linear decoding/old_code/log_SVM_Subject_confusionMatrix_batch.m +++ /dev/null @@ -1,217 +0,0 @@ -%% Log_SVM_subject_batch -% Run a linear decoder on the neural activity for the subject's behavior -% (only including behaviors with a minimum # occurrence) -% Batch version of script -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:6,11:13,15:16]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1;%lump similar behavioral categories together to increase sample size. -agg_precedence =0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - %for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - - %Low-pass filter data - %Spike_count_raster = lowpass(Spike_rasters',0.05,1); - - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - co_occurrence = cell2mat({labels{:,5}}'); - - if null - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels,behav_categ, temp_resolution); - behavior_labels = sim_behav; - end - - if unq_behav==1%Select epochs where only one behavior happens at any given time (i.e. no co-occurrence). - idx_single = find(co_occurrence<4); %(no co-occurrence, or with RR or with proximity) - Spike_count_raster = Spike_count_raster(idx_single,:); - behavior_labels = behavior_labels(idx_single,:); - end - - if simplify - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Lump foraging and drinking - behavior_labels(behavior_labels==find(behav_categ=="Drinking"))=find(behav_categ=="Foraging"); - - %Lump grooming together - behavior_labels(behavior_labels==find(behav_categ=="Getting groomed"))=find(behav_categ=="Groom partner"); - end - - %% Select behaviors to decode - - % select behaviors manually - behav = [1,5,7,18,29];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(removecats(categorical(behavior_labels_final))); - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - - %subsample to match number of neurons across brain areas - Labels = behavior_labels_final; - if randomsample==1 - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %End of SVM for loop - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate{s}(chan) = mean(hitrate) - sd_hitrate{s}(chan) = std(hitrate); - mean_hitrate_shuffled{s}(chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); %Get confusion matrix - confusion_mat_avg{s, chan}=round(mean(C_concat,3)*100); %Average over SVM iterations - rowNames{s} = {labels_id{:,2}}; colNames{s} = {labels_id{:,2}}; %Get behavior names - C_table{s, chan} = array2table(confusion_mat_avg{s, chan},'RowNames',rowNames{s},'VariableNames',colNames{s}); - diagonal_confMat(s,:) = diag(confusion_mat_avg{s, chan})'; - - chan = chan +1; - clear labels_id - -end %End of session for loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd(['~/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_confusionMat.mat', "mean_hitrate","sd_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") -%load('SVM_results_subjectBehav.mat') - -sesh=[1:6,11:13,15]; -behav_lbls=behav_categ(behav); -diagonal_confMat(diagonal_confMat==0)=nan; -mean_decoding = nanmean(diagonal_confMat); -[~, idx_sorted]=sort(mean_decoding); -figure; hold on -for s=sesh - scatter(1:length(behav_lbls), diagonal_confMat(s,idx_sorted), 'filled') -end -scatter(1:length(behav_lbls), mean_decoding(idx_sorted), 100,'_') -xticks(1:length(behav_lbls)); xticklabels(behav_lbls(idx_sorted)) -xlim([0.5 length(behav_lbls)+0.5]) - diff --git a/Linear decoding/old_code/log_SVM_basic_function_kernelTest.m b/Linear decoding/old_code/log_SVM_basic_function_kernelTest.m deleted file mode 100644 index 5148d45..0000000 --- a/Linear decoding/old_code/log_SVM_basic_function_kernelTest.m +++ /dev/null @@ -1,63 +0,0 @@ -function [hitrate, C] = log_SVM_basic_function(Input_matrix, Labels, kfolds, ldaflag, Normalize, kernel) -% This script was written by SdT to run SVM on the w8a dataset using the LIBSVM library - -Input_matrix = double(Input_matrix); %Augment data to double precision -Labels = double(Labels); - -if ~exist('ldaflag', 'var') %Default option is not to use lda. lda helps with lazy neurons, but overall leads to lower accuracy - ldaflag = 0; -end - -if ~exist('Normalize', 'var') %Default option is not to normalize - Normalize = 0; -end - -if Normalize == 1 - %Z-score data to normalize it. Each neuron FR is normalized to its mean across trials FR - Input_matrix = zscore(Input_matrix); -end - -total_number_trials = size(Input_matrix,1); %Find the total number of trials for which a prediction needs to be made. -Predicted_labels = zeros(size(Labels)); %Initate matrix of predicted labels for each trial -cumError = 0; %Count the number of prediction errors - -%Generate k-fold training/testing indices: -indices = crossvalind('Kfold', Labels, kfolds); -if kfolds == length(Labels) %If kfold equals the number of trials as for a leave-one out cross validation - indices = 1:length(Labels); -end - -% Run through each fold: -for fold = 1:kfolds - - testlbls = Labels(indices == fold); % test on the selected subsample - testdata = Input_matrix(indices == fold,:); %these are the data of the selected observations for testing - trainlbls = Labels(indices ~= fold); %these are the labels of the selected observations for training (all folds except one) - traindata = Input_matrix(indices ~= fold,:); %these are the data of the selected observations for training - - if ldaflag == 1 %If you wish to perform LDA - ldaopts.FisherFace = 1; - [fdaout, Weights] = fda(trainlbls, ldaopts, traindata); - testdata = testdata*Weights; % Project testdata to same LDA subspace as traindata: - traindata = fdaout; - end - - % Train/test SVM model: - % !!!!!!!!!! CHANGE SVM KERNEL HERE !!!!!!!!!!!!!!! - disp(['-t,' num2str(kernel) ', -q']) - model = svmtrain(trainlbls, traindata, ['-t,' num2str(kernel) ', -q']); %train the model using a linear kernel (-t: 0) or a RBF kernel (-t: 2) and default parameters - [svmlbls] = svmpredict(testlbls, testdata, model, '-q'); %get predicted labels given model - - nErr= length(find( (testlbls - svmlbls) ~= 0 )); %Find misclassifications - cumError = cumError + nErr; %Count number of errors - Predicted_labels(indices == fold) = svmlbls; %Keep track of the predicted labels - -end - -%Compute performance of decoder -hitrate = 1 - (cumError/total_number_trials); -%Obtain confusion matrix of predicted against real values. -C = confusionmat(Labels, Predicted_labels); -C = C ./ repmat(sum(C,2), 1, size(C,2)); - -end diff --git a/Linear decoding/old_code/log_SVM_ccgp_function.m b/Linear decoding/old_code/log_SVM_ccgp_function.m deleted file mode 100644 index 079d068..0000000 --- a/Linear decoding/old_code/log_SVM_ccgp_function.m +++ /dev/null @@ -1,47 +0,0 @@ -function [hitrate, C] = log_SVM_ccgp_function(trainlbls, traindata, testlbls, testdata, ldaflag, Normalize) -% This script was written by SdT to run SVM on the w8a dataset using the LIBSVM library -% -% Input_matrix = double(Input_matrix); %Augment data to double precision -% Labels = double(Labels); - -if ~exist('ldaflag', 'var') %Default option is not to use lda. lda helps with lazy neurons, but overall leads to lower accuracy - ldaflag = 0; -end - -if ~exist('Normalize', 'var') %Default option is not to normalize - Normalize = 0; -end - -if Normalize == 1 - %Z-score data to normalize it. Each neuron FR is normalized to its mean across trials FR - Input_matrix = zscore(Input_matrix); -end - -total_number_trials = size(testdata,1); %Find the total number of trials for which a prediction needs to be made. -Predicted_labels = zeros(size(testlbls)); %Initate matrix of predicted labels for each trial -cumError = 0; %Count the number of prediction errors - -if ldaflag == 1 %If you wish to perform LDA - ldaopts.FisherFace = 1; - [fdaout, Weights] = fda(trainlbls, ldaopts, traindata); - testdata = testdata*Weights; % Project testdata to same LDA subspace as traindata: - traindata = fdaout; -end - -% Train/test SVM model: -% !!!!!!!!!! CHANGE SVM KERNEL HERE !!!!!!!!!!!!!!! -model = svmtrain(trainlbls, traindata, '-t, 0, -q'); %train the model using a linear kernel (-t: 0) or a RBF kernel (-t: 2) and default parameters -[svmlbls] = svmpredict(testlbls, testdata, model, '-q'); %get predicted labels given model - -nErr= length(find( (testlbls - svmlbls) ~= 0 )); %Find misclassifications -cumError = cumError + nErr; %Count number of errors -Predicted_labels = svmlbls; %Keep track of the predicted labels - - -%Compute performance of decoder -hitrate = 1 - (cumError/total_number_trials); -%Obtain confusion matrix of predicted against real values. -C = confusionmat(testlbls, Predicted_labels); -C = C ./ repmat(sum(C,2), 1, size(C,2)); - -end diff --git a/Linear decoding/old_code/log_SVM_crossBlock_perBehav_batch.m b/Linear decoding/old_code/log_SVM_crossBlock_perBehav_batch.m deleted file mode 100644 index 6c19b26..0000000 --- a/Linear decoding/old_code/log_SVM_crossBlock_perBehav_batch.m +++ /dev/null @@ -1,318 +0,0 @@ -%% Log_SVM_subject_batch -% Run a linear decoder on the neural activity for the subject's behavior -% (only including behaviors with a minimum # occurrence) -% Batch version of script -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 5000;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1;%lump similar behavioral categories together to increase sample size. -agg_precedence=0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - %for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - % behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Threat to partner"); - % behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Threat to subject"); - % behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - co_occurrence = cell2mat({labels{:,5}}'); - - blocks = cell2mat({labels{:,12}}'); - - if simplify - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Lump foraging and drinking - behavior_labels(behavior_labels==find(behav_categ=="Drinking"))=find(behav_categ=="Foraging"); - - %Lump grooming together -% behavior_labels(behavior_labels==find(behav_categ=="Getting groomed"))=find(behav_categ=="Groom partner"); - end - - %% Select behaviors to decode - -% % % %Compute freq of behavior for the session -% % % behav_freq_table = tabulate(behavior_labels); -% % % behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) -% % % -% % % % Select behaviors with a minimum # of occurrences -% % % min_occurrences = 50; -% % % behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences -% % % -% % % % Remove behaviors that are ill-defined -% % % behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. -% % % behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. -% % % behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize')));%excluding scratch which is a source of confusion. -% % % behav = behav(behav~=find(matches(behav_categ,'Vocalization')));%excluding scratch which is a source of confusion. -% % % behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. -% % % behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - - % OR select behaviors manually - behav = [1,5,7,8,18,29];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_final = blocks(idx); - - traindata = Spike_count_raster_final(block_final==1,:); trainlbls = behavior_labels_final(block_final==1); - testdata = Spike_count_raster_final(block_final==2,:); testlbls = behavior_labels_final(block_final==2); - - block1_behav = tabulate(trainlbls); - block2_behav = tabulate(testlbls); - - if length(unique(testlbls))==length(behav) && length(unique(trainlbls))==length(behav) - - if all(block1_behav(behav,2)>15) && all(block2_behav(behav,2)>15) - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - - %Balance number of trials for train data - uniqueLabels = unique(trainlbls); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = trainlbls; - for i=1:NumOfClasses - idx = trainlbls == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - trainlbls = labels_temp; - - num_trials = hist(trainlbls,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(trainlbls == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - traindata = traindata(chosen_trials, :); - trainlbls = trainlbls(chosen_trials, :); - trainlbls_shuffled = trainlbls(randperm(length(trainlbls))); - - %Balance number of trials for test data - uniqueLabels = unique(testlbls); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = testlbls; - for i=1:NumOfClasses - idx = testlbls == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - testlbls = labels_temp; - - num_trials = hist(testlbls,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(testlbls == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - testdata = testdata(chosen_trials, :); - testlbls = testlbls(chosen_trials, :); - testlbls_shuffled = testlbls(randperm(length(testlbls))); - - % Run svm - [hitrate_baseline(iter), C{iter}] = log_SVM_basic_function(traindata,trainlbls, 5, 0, 0); - [hitrate(iter), C{iter}] = log_SVM_ccgp_function(trainlbls,traindata,... - testlbls, testdata, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_ccgp_function(trainlbls,traindata,... - testlbls_shuffled, testdata, 0, 0); - - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %End of SVM for loop - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate_baseline{s}(chan) = mean(hitrate_baseline) - mean_hitrate{s}(chan) = mean(hitrate) - sd_hitrate{s}(chan) = std(hitrate); - mean_hitrate_shuffled{s}(chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); %Get confusion matrix - confusion_mat_avg{s,chan}=round(mean(C_concat,3)*100); %Average over SVM iterations - ccgp_per_behav{s,chan} = diag(confusion_mat_avg{s,chan}); -% rowNames{s} = {labels_id{:,2}}; colNames{s} = {labels_id{:,2}}; %Get behavior names -% C_table{s, temp, chan} = array2table(confusion_mat_avg,'RowNames',rowNames{s},'VariableNames',colNames{s}); - - chan = chan +1; - clear labels_id - - end - end - %end %end of channel for loop - - cd(savePath) - -end %End of session for loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -% save('SVM_results_crossBlock.mat',"confusion_mat_avg","ccgp_per_behav", "mean_hitrate_baseline","mean_hitrate","sd_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") -load('SVM_results_crossBlock.mat') -% % % -% % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% % % % Bar plot decoding accuracy -% % % -% % % figure; hold on -% % % data_within = cell2mat(mean_hitrate_baseline'); -% % % data_cross = cell2mat(mean_hitrate'); -% % % data_shuffle = cell2mat(mean_hitrate_shuffled'); -% % % bp = bar([mean(data_within(:,:)); mean(data_cross(:,:)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); -% % % -% % % sp1 = scatter(ones(size(data_within,1))*0.78,data_within(:,1), 'filled','b'); -% % % sp1 = scatter(ones(size(data_within,1)),data_within(:,2), 'filled','r'); -% % % sp1 = scatter(ones(size(data_within,1))*1.22,data_within(:,3), 'filled','y'); -% % % -% % % sp1 = scatter(ones(size(data_cross,1))*1.78,data_cross(:,1), 'filled','b'); -% % % sp1 = scatter(ones(size(data_cross,1))*2,data_cross(:,2), 'filled','r'); -% % % sp1 = scatter(ones(size(data_cross,1))*2.22,data_cross(:,3), 'filled','y'); -% % % -% % % sp1 = scatter(ones(size(data_shuffle,1))*2.78,data_shuffle(:,1), 'filled','b'); -% % % sp1 = scatter(ones(size(data_shuffle,1))*3,data_shuffle(:,2), 'filled','r'); -% % % sp1 = scatter(ones(size(data_shuffle,1))*3.22,data_shuffle(:,3), 'filled','y'); -% % % -% % % legend(bp,{'vlPFC','TEO','all'},'Location','best') -% % % -% % % ylabel('Decoding Accuracy'); ylim([0.1 1]) -% % % xticks([1 2 3]); xticklabels({'Within-context','Cross-context', 'Shuffled'}); xlim([0.25 3.75]) -% % % ax = gca; -% % % ax.FontSize = 16; -% % % %saveas(gcf,['SVM_results_crossBlocks.pdf']) -% % % -% % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% % % % Plot cross-context generalizability performance per behavior -% % % % ccgp_vlpfc=cat(3,confusion_mat_avg{:,1}); -% % % % ccgp_teo=cat(3,confusion_mat_avg{:,2}); -% % % % ccgp_all=cat(3,confusion_mat_avg{:,3}); -ccgp_all=cat(3,confusion_mat_avg{:,1}); - -data = ccgp_all; -behav_select = [1,5,7,8,18,29]; -colormap={'r','g','c','b','y','k'}; -figure; hold on -[sorted_data, idx]=sort(diag(median(data,3))); behav_sorted = behav_select(idx); -scatter(1:length(behav_select),sorted_data,400,'_') -scatter(ones(size(data,3))*1,reshape(data(idx(1),idx(1),:),1,size(data,3)),80, 'filled', colormap{idx(1)}); -scatter(ones(size(data,3))*2,reshape(data(idx(2),idx(2),:),1,size(data,3)),80, 'filled', colormap{idx(2)}); -scatter(ones(size(data,3))*3,reshape(data(idx(3),idx(3),:),1,size(data,3)),80, 'filled', colormap{idx(3)}); -scatter(ones(size(data,3))*4,reshape(data(idx(4),idx(4),:),1,size(data,3)),80, 'filled', colormap{idx(4)}); -scatter(ones(size(data,3))*5,reshape(data(idx(5),idx(5),:),1,size(data,3)),80, 'filled', colormap{idx(5)}); -scatter(ones(size(data,3))*6,reshape(data(idx(6),idx(6),:),1,size(data,3)),80, 'filled', colormap{idx(6)}); -ylabel('Decoding Accuracy'); ylim([0 100]) -xticks(1:length(behav_select)); xticklabels(behav_categ(behav_sorted)); xlim([0.25 length(behav_select)+0.75]) -ax = gca; -ax.FontSize = 16; -% % % %(gcf,['CCGP_crossBlocks.pdf']) - -% % % data = cell2mat(ccgp_per_behav')'; -% % % mean_data = nanmedian(data); -% % % figure; hold on -% % % [sorted_data, idx]=sort(mean_data); behav_sorted = behav_select(idx); -% % % scatter(1:length(behav_select),sorted_data,150,'_') -% % % scatter(ones(size(data,3))*1,data(:,idx(1)),80, 'filled', colormap{idx(1)}); -% % % scatter(ones(size(data,3))*2,data(:,idx(2)),80, 'filled', colormap{idx(2)}); -% % % scatter(ones(size(data,3))*3,data(:,idx(3)),80, 'filled', colormap{idx(3)}); -% % % scatter(ones(size(data,3))*4,data(:,idx(4)),80, 'filled', colormap{idx(4)}); -% % % scatter(ones(size(data,3))*5,data(:,idx(5)),80, 'filled', colormap{idx(5)}); -% % % scatter(ones(size(data,3))*6,data(:,idx(6)),80, 'filled', colormap{idx(6)}); -% % % % scatter(ones(size(data,3))*7,data(:,idx(7)),80, 'filled','b'); -% % % % scatter(ones(size(data,3))*8,data(:,idx(8)),80, 'filled','b'); -% % % ylabel('Decoding Accuracy'); %ylim([0 100]) -% % % xticks([1:length(behav_select)]); xticklabels(behav_categ(behav_sorted)); xlim([0.25 length(behav_select)+0.75]) -% % % ax = gca; -% % % ax.FontSize = 16; \ No newline at end of file diff --git a/Linear decoding/old_code/log_SVM_crossBlock_perBehav_batch_v2.m b/Linear decoding/old_code/log_SVM_crossBlock_perBehav_batch_v2.m deleted file mode 100644 index bd2b748..0000000 --- a/Linear decoding/old_code/log_SVM_crossBlock_perBehav_batch_v2.m +++ /dev/null @@ -1,313 +0,0 @@ -%% Log_SVM_subject_batch -% Run a linear decoder on the neural activity for the subject's behavior -% (only including behaviors with a minimum # occurrence) -% Batch version of script -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 500;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1;%lump similar behavioral categories together to increase sample size. -agg_precedence=0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - %for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - co_occurrence = cell2mat({labels{:,5}}'); - - blocks = cell2mat({labels{:,12}}'); - - if simplify - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Lump foraging and drinking - %behavior_labels(behavior_labels==find(behav_categ=="Drinking"))=find(behav_categ=="Foraging"); - - %Lump grooming together - %behavior_labels(behavior_labels==find(behav_categ=="Getting groomed"))=find(behav_categ=="Groom partner"); - end - - %% Select behaviors to decode - - %Compute freq of behavior for the session - unq_behav = unique(behavior_labels); - behav_freq_table = crosstab(behavior_labels,blocks); - %behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = find(all(behav_freq_table(:,1:2)>=min_occurrences,2));%Get behaviors with a min number of occurrences - behav=unq_behav(behav); - - % Remove behaviors that are ill-defined - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Vocalization')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. - - behav_save{s} = behav; - - % OR select behaviors manually - %behav = [4,5,7,8,9,10,18,24,29];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_final = blocks(idx); - - traindata = Spike_count_raster_final(block_final==1,:); trainlbls = behavior_labels_final(block_final==1); - testdata = Spike_count_raster_final(block_final==2,:); testlbls = behavior_labels_final(block_final==2); - - block1_behav = tabulate(trainlbls); - block2_behav = tabulate(testlbls); - -% if length(unique(testlbls))==length(behav) && length(unique(trainlbls))==length(behav) -% -% if all(block1_behav(behav,2)>15) && all(block2_behav(behav,2)>15) - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - - %Balance number of trials for train data - uniqueLabels = unique(trainlbls); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = trainlbls; - for i=1:NumOfClasses - idx = trainlbls == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - trainlbls = labels_temp; - - num_trials = hist(trainlbls,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(trainlbls == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - traindata = traindata(chosen_trials, :); - trainlbls = trainlbls(chosen_trials, :); - trainlbls_shuffled = trainlbls(randperm(length(trainlbls))); - - %Balance number of trials for test data - uniqueLabels = unique(testlbls); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = testlbls; - for i=1:NumOfClasses - idx = testlbls == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - testlbls = labels_temp; - - num_trials = hist(testlbls,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(testlbls == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - testdata = testdata(chosen_trials, :); - testlbls = testlbls(chosen_trials, :); - testlbls_shuffled = testlbls(randperm(length(testlbls))); - - % Run svm - [hitrate_baseline(iter), C{iter}] = log_SVM_basic_function(traindata,trainlbls, 5, 0, 0); - [hitrate(iter), C{iter}] = log_SVM_ccgp_function(trainlbls,traindata,... - testlbls, testdata, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_ccgp_function(trainlbls,traindata,... - testlbls_shuffled, testdata, 0, 0); - - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %End of SVM for loop - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate_baseline{s}(chan) = mean(hitrate_baseline) - mean_hitrate{s}(chan) = mean(hitrate) - sd_hitrate{s}(chan) = std(hitrate); - mean_hitrate_shuffled{s}(chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); %Get confusion matrix - confusion_mat_avg{s,chan}=round(mean(C_concat,3)*100); %Average over SVM iterations - ccgp_per_behav{s,chan} = diag(confusion_mat_avg{s,chan}); - ccgp_per_behav_ratio{s,chan} = diag(confusion_mat_avg{s,chan})./(mean_hitrate_shuffled{s}*100); -% rowNames{s} = {labels_id{:,2}}; colNames{s} = {labels_id{:,2}}; %Get behavior names -% C_table{s, temp, chan} = array2table(confusion_mat_avg,'RowNames',rowNames{s},'VariableNames',colNames{s}); - - chan = chan +1; - clear labels_id -% -% end -% end - %end %end of channel for loop - - cd(savePath) - -end %End of session for loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -% save('SVM_results_crossBlock.mat',"confusion_mat_avg", "mean_hitrate_baseline","mean_hitrate","sd_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") -load('SVM_results_crossBlock.mat') - -% % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% % % % % Bar plot decoding accuracy -% % % % -% % % % figure; hold on -% % % % data_within = cell2mat(mean_hitrate_baseline'); -% % % % data_cross = cell2mat(mean_hitrate'); -% % % % data_shuffle = cell2mat(mean_hitrate_shuffled'); -% % % % bp = bar([mean(data_within(:,:)); mean(data_cross(:,:)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); -% % % % -% % % % sp1 = scatter(ones(size(data_within,1))*0.78,data_within(:,1), 'filled','b'); -% % % % sp1 = scatter(ones(size(data_within,1)),data_within(:,2), 'filled','r'); -% % % % sp1 = scatter(ones(size(data_within,1))*1.22,data_within(:,3), 'filled','y'); -% % % % -% % % % sp1 = scatter(ones(size(data_cross,1))*1.78,data_cross(:,1), 'filled','b'); -% % % % sp1 = scatter(ones(size(data_cross,1))*2,data_cross(:,2), 'filled','r'); -% % % % sp1 = scatter(ones(size(data_cross,1))*2.22,data_cross(:,3), 'filled','y'); -% % % % -% % % % sp1 = scatter(ones(size(data_shuffle,1))*2.78,data_shuffle(:,1), 'filled','b'); -% % % % sp1 = scatter(ones(size(data_shuffle,1))*3,data_shuffle(:,2), 'filled','r'); -% % % % sp1 = scatter(ones(size(data_shuffle,1))*3.22,data_shuffle(:,3), 'filled','y'); -% % % % -% % % % legend(bp,{'vlPFC','TEO','all'},'Location','best') -% % % % -% % % % ylabel('Decoding Accuracy'); ylim([0.1 1]) -% % % % xticks([1 2 3]); xticklabels({'Within-context','Cross-context', 'Shuffled'}); xlim([0.25 3.75]) -% % % % ax = gca; -% % % % ax.FontSize = 16; -% % % % saveas(gcf,['SVM_results_crossBlocks.pdf']) - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Plot cross-context generalizability performance per behavior -% ccgp_vlpfc=cat(3,confusion_mat_avg{:,1}); -% ccgp_teo=cat(3,confusion_mat_avg{:,2}); -% ccgp_all=cat(3,confusion_mat_avg{:,3}); -% ccgp_all=cat(3,confusion_mat_avg{:,1}); - -for s=session_range - ccgp_all(s,behav_save{s}) = ccgp_per_behav{s}; - ccgp_all_ratio(s,behav_save{s}) = ccgp_per_behav_ratio{s}; -end -ccgp_all(ccgp_all==0)=nan; -ccgp_all_ratio(ccgp_all_ratio==0)=nan; - -behav_select = [1,5,7,18,29];%[4,5,7,8,9,10,24,29]; -data = ccgp_all(:,behav_select); -mean_data = nanmedian(data); - -figure; hold on -[sorted_data, idx]=sort(mean_data); behav_sorted = behav_select(idx); -scatter(1:length(behav_select),sorted_data,150,'_') -scatter(ones(size(data,3))*1,data(:,idx(1)),80, 'filled','k'); -scatter(ones(size(data,3))*2,data(:,idx(2)),80, 'filled','y'); -scatter(ones(size(data,3))*3,data(:,idx(3)),80, 'filled','r'); -scatter(ones(size(data,3))*4,data(:,idx(4)),80, 'filled','g'); -scatter(ones(size(data,3))*5,data(:,idx(5)),80, 'filled','b'); -% scatter(ones(size(data,3))*6,data(:,idx(6)),80, 'filled','b'); -% scatter(ones(size(data,3))*7,data(:,idx(7)),80, 'filled','b'); -% scatter(ones(size(data,3))*8,data(:,idx(8)),80, 'filled','b'); -ylabel('Decoding Accuracy'); %ylim([0 100]) -xticks([1:length(behav_select)]); xticklabels(behav_categ(behav_sorted)); xlim([0.25 length(behav_select)+0.75]) -ax = gca; -ax.FontSize = 16; -saveas(gcf,['CCGP_crossBlocks.pdf']) diff --git a/Linear decoding/old_code/log_SVM_crossPose_Grooming_batch.m b/Linear decoding/old_code/log_SVM_crossPose_Grooming_batch.m deleted file mode 100644 index 0f644b7..0000000 --- a/Linear decoding/old_code/log_SVM_crossPose_Grooming_batch.m +++ /dev/null @@ -1,267 +0,0 @@ -%% Log_SVM_crossPose_Grooming_batch -% Run a linear decoder on the neural activity for the subject's behavior -% (only including behaviors with a minimum # occurrence) -% Batch version of script -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1;%lump similar behavioral categories together to increase sample size. - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_groom_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - - disp('Data Loaded') - - - Spike_count_raster = Spike_rasters'; - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - pose_labels = cell2mat({labels{:,14}}'); - bodypart_labels = cell2mat({labels{:,16}}'); - blocks = cell2mat({labels{:,12}}'); - - if simplify - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Lump foraging and drinking - behavior_labels(behavior_labels==find(behav_categ=="Drinking"))=find(behav_categ=="Foraging"); - - %Lump grooming together - behavior_labels(behavior_labels==find(behav_categ=="Getting groomed"))=find(behav_categ=="Groom partner"); - - behav = [1,5,7,18,29]; - end - - %% Select behaviors to decode - - % OR select behaviors manually - behav = [4,5,7:10,18];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_final = blocks(idx); - groom_labels_final = pose_labels(idx); - - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - non_groom_indices = find(~ismember(behavior_labels_final,[7,8])); - indices=crossvalind('Kfold',length(non_groom_indices),5); - train_indices = [find(groom_labels_final==1); non_groom_indices(indices<5)]; - test_indices = [find(groom_labels_final>1); non_groom_indices(indices==5)]; - - traindata = Spike_count_raster_final(train_indices,:); trainlbls = behavior_labels_final(train_indices); - testdata = Spike_count_raster_final(test_indices,:); testlbls = behavior_labels_final(test_indices); - - %Balance number of trials for train data - uniqueLabels = unique(trainlbls); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = trainlbls; - for i=1:NumOfClasses - idx = trainlbls == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - trainlbls = labels_temp; - - num_trials = hist(trainlbls,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(trainlbls == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - traindata = traindata(chosen_trials, :); - trainlbls = trainlbls(chosen_trials, :); - trainlbls_shuffled = trainlbls(randperm(length(trainlbls))); - - %Balance number of trials for test data - uniqueLabels = unique(testlbls); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = testlbls; - for i=1:NumOfClasses - idx = testlbls == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - testlbls = labels_temp; - - num_trials = hist(testlbls,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(testlbls == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - testdata = testdata(chosen_trials, :); - testlbls = testlbls(chosen_trials, :); - testlbls_shuffled = testlbls(randperm(length(testlbls))); - - % Run svm - [hitrate_baseline(iter), C{iter}] = log_SVM_basic_function(traindata,trainlbls, 5, 0, 0); - [hitrate(iter), C{iter}] = log_SVM_ccgp_function(trainlbls,traindata,... - testlbls, testdata, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_ccgp_function(trainlbls,traindata,... - testlbls_shuffled, testdata, 0, 0); - - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %End of SVM for loop - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate_baseline{s}(chan) = mean(hitrate_baseline) - mean_hitrate{s}(chan) = mean(hitrate) - sd_hitrate{s}(chan) = std(hitrate); - mean_hitrate_shuffled{s}(chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); %Get confusion matrix - confusion_mat_avg{s,chan}=round(mean(C_concat,3)*100); %Average over SVM iterations - ccgp_per_behav{s,chan} = diag(confusion_mat_avg{s,chan}); - % rowNames{s} = {labels_id{:,2}}; colNames{s} = {labels_id{:,2}}; %Get behavior names - % C_table{s, temp, chan} = array2table(confusion_mat_avg,'RowNames',rowNames{s},'VariableNames',colNames{s}); - - chan = chan +1; - clear labels_id - - end %end of channel for loop - - cd(savePath) - -end %End of session for loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_crossBlock.mat',"confusion_mat_avg", "mean_hitrate_baseline","mean_hitrate","sd_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data_within = cell2mat(mean_hitrate_baseline'); -data_cross = cell2mat(mean_hitrate'); -data_shuffle = cell2mat(mean_hitrate_shuffled'); -bp = bar([mean(data_within(:,:)); mean(data_cross(:,:)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); - -sp1 = scatter(ones(size(data_within,1))*0.78,data_within(:,1), 'filled','b'); -sp1 = scatter(ones(size(data_within,1)),data_within(:,2), 'filled','r'); -sp1 = scatter(ones(size(data_within,1))*1.22,data_within(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data_cross,1))*1.78,data_cross(:,1), 'filled','b'); -sp1 = scatter(ones(size(data_cross,1))*2,data_cross(:,2), 'filled','r'); -sp1 = scatter(ones(size(data_cross,1))*2.22,data_cross(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data_shuffle,1))*2.78,data_shuffle(:,1), 'filled','b'); -sp1 = scatter(ones(size(data_shuffle,1))*3,data_shuffle(:,2), 'filled','r'); -sp1 = scatter(ones(size(data_shuffle,1))*3.22,data_shuffle(:,3), 'filled','y'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0.1 1]) -xticks([1 2 3]); xticklabels({'Within-context','Cross-context', 'Shuffled'}); xlim([0.25 3.75]) -ax = gca; -ax.FontSize = 16; -saveas(gcf,['SVM_results_crossBlocks.pdf']) - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Plot cross-context generalizability performance per behavior -ccgp_vlpfc=cat(3,confusion_mat_avg{:,1}); -ccgp_teo=cat(3,confusion_mat_avg{:,2}); -ccgp_all=cat(3,confusion_mat_avg{:,3}); - -data = ccgp_all; - -figure; hold on -[sorted_data, idx]=sort(diag(mean(data,3))); -scatter(1:5,sorted_data,100,'filled') -scatter(ones(size(data,3))*1,reshape(data(idx(1),idx(1),:),1,size(data,3)), 'filled','k'); -scatter(ones(size(data,3))*2,reshape(data(idx(2),idx(2),:),1,size(data,3)), 'filled','y'); -scatter(ones(size(data,3))*3,reshape(data(idx(3),idx(3),:),1,size(data,3)), 'filled','r'); -scatter(ones(size(data,3))*4,reshape(data(idx(4),idx(4),:),1,size(data,3)), 'filled','g'); -scatter(ones(size(data,3))*5,reshape(data(idx(5),idx(5),:),1,size(data,3)), 'filled','b'); -ylabel('Decoding Accuracy'); ylim([0 100]) -xticks([1 2 3 4 5]); xticklabels({'Rest','Travel', 'Aggression','Foraging','Grooming'}); xlim([0.25 5.75]) -ax = gca; -ax.FontSize = 16; -saveas(gcf,['CCGP_crossBlocks.pdf']) diff --git a/Linear decoding/old_code/log_SVM_grooming.m b/Linear decoding/old_code/log_SVM_grooming.m deleted file mode 100644 index 063bd6e..0000000 --- a/Linear decoding/old_code/log_SVM_grooming.m +++ /dev/null @@ -1,253 +0,0 @@ -%% Log_SVM_grooming -%% Run a linear decoder on a the neural activity for different grooming contexts -% This script allows to deocde grooming: -% 1. Start vs. end -% 2. Post-threat or not -% 3. Reciprocated or not -% 4. Initiated or not -%Camille Testard, March 2022 - - -%% Load data - -%Set path -is_mac = 1; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -clearvars -except savePath filePath is_mac - -%Set parameters -temp = 1; temp_resolution = 1; -chan = 1; channel_flag = "all"; -randomsample=0; -with_NC=1; -isolatedOnly=0; - -%for channel_flag = ["vlPFC", "TEO", "all"] - -%Get data with specified temporal resolution and channels -%[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final, unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); -[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final, unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - -%filePath is the experimental data path -%Temp_resolution is the temporal resolution at which we would like to -%analyze the data -%Channel_flag specifies with channels to include: only TEO array, only -%vlPFC array or all channels -%is_mac is whether a mac or a pc is being used -disp('Data Loaded') - -Spike_count_raster = Spike_rasters'; -behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - -%% Add labelling for grooming -%Beginning vs. end of grooming bout -%Grooming post-threat vs not. -%Grooming after reciprocation or not -%Groom received after grm Prsnt or not - -groom_categ_label = {'Star.vs.end', 'Post-threat','Reciprocated','Initiated'}; - -b = 1; -for behav = [7,8] - groom_behav={'Give','Receive'}; - - for groom_categ = 1:4 - %Note: after threat, almost always groom RECEIVE, not given by - %subject. Also, grooming after groom present is only for groom - %RECEIVE. - - groom_labels = groom_labels_all(:,groom_categ+1); - - - %% Select behaviors to decode - - % Select behaviors with a minimum # of occurrences - %behav =[find(matches(behav_categ,'Groom Give'))]; %find(matches(behav_categ,'Groom Give')), - - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = groom_labels(idx,:);%Same as above but in behavior labels - - %If groom label is start vs. end - if groom_categ==1 - idx_epoch = find(~ismember(behavior_labels_final,3)); %remove middle chunk - Spike_count_raster_final = Spike_count_raster_final(idx_epoch,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels_final(idx_epoch,:);%Same as above but in behavior labels - end - - behav_size=tabulate(behavior_labels_final); - disp('########################') - tabulate(behavior_labels_final) - disp('########################') - - channel = char(channel_flag); - disp('****************************************************************************') - disp(['Groom categ: ' groom_categ_label{groom_categ} ', Channels: ' channel ', Behavior: Groom ' groom_behav{b}]) - disp('****************************************************************************') - - %pause(5) - if all(behav_size(:,2)>=30) && length(behav_size(:,2))>1 %If there are at least 30 occurrence of grooming in context 'b' - %% Run SVM over multiple iterations - num_iter = 100; - - disp('Start running SVM...') - for iter = 1:num_iter - - % clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... - % Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... - % channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %subsample to match number of neurons across brain areas - Labels = behavior_labels_final; - if randomsample==1 && channel_flag~="all" - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - if min(num_trials)<50 %If there are less than 50 instances for a given behavior - minNumTrials = min(num_trials); %use the minimum # of instances - else - minNumTrials = 50; %impose 50 occurrences per category - end - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - %disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - mean_hitrate(chan,b,groom_categ) = mean(hitrate); - sd_hitrate(chan,b,groom_categ) = std(hitrate); - mean_hitrate_shuffled(chan,b,groom_categ) = mean(hitrate_shuffled); - sd_hitrate_shuffled = std(hitrate_shuffled); - - else - mean_hitrate(chan,b,groom_categ) = nan; - sd_hitrate(chan,b,groom_categ) = nan; - end% End of "min number of grooming of category b" clause - - end - b = b+1; -end -chan = chan +1; -clearvars -except is_mac savePath mean_hitrate sd_hitrate mean_hitrate_shuffled sd_hitrate_shuffled C_table temp_resolution channel_flag filePath chan temp behavs_eval behav randomsample onlysingle b groom_labels_all isolatedOnly with_NC groom_categ_label -%end - -squeeze(mean_hitrate) -squeeze(sd_hitrate) -(squeeze(mean_hitrate) - squeeze(mean_hitrate_shuffled))./squeeze(mean_hitrate_shuffled) - -cd(savePath) - -rowNames = ["1sec"]; colNames = ["vlPFC","TEO","all"]; -result_hitrate = array2table(mean_hitrate,'RowNames',rowNames,'VariableNames',colNames) -result_hitrate_shuffled = array2table(mean_hitrate_shuffled,'RowNames',rowNames,'VariableNames',colNames) -result_sdhitrate = array2table(sd_hitrate,'RowNames',rowNames,'VariableNames',colNames) - -% save([savePath '\SVM_results_social_context.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -% writetable(result_hitrate,[savePath '\SVM_results_social_context.csv'],'WriteRowNames',true,'WriteVariableNames',true); -save([savePath '\SVM_results_' num2str(length(behav)) 'behav_NOsubsample_unique.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -writetable(result_hitrate,[savePath '\SVM_results_' num2str(length(behav)) 'behav_NOsubsample_unique.csv'],'WriteRowNames',true,'WriteVariableNames',true); - -groom_give_hitrate = reshape(mean_hitrate(:,1,:), [3,4]); -groom_give_hitrate_shuffled = reshape(mean_hitrate_shuffled(:,1,:), [3,4]); -groom_receive_hitrate = reshape(mean_hitrate(:,2,:), [3,4]); -groom_receive_hitrate_shuffled = reshape(mean_hitrate_shuffled(:,2,:), [3,4]); - -%Plotting results decoding accuracy for grooming give -figure; hold on; set(gcf,'Position',[150 250 700 500]) -cmap = hsv(size(groom_give_hitrate,1)); -for b = 1:size(groom_give_hitrate,1) - y = groom_give_hitrate(b,[1,3]); - % std_dev = sd_hitrate(b,:); - % errorbar(y,std_dev,'s','MarkerSize',10,... - % 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) - scatter(1:2,y,40,'filled','Color',cmap(b,:)) -end -leg = legend(["vlPFC", "TEO","All"]); -title(leg,'Brain Area') -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 2.2]); xlim([0.8 2.2]); ylim([0.4 1]) -xticklabels({'','Start vs. end','Reciprocal or not',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Grooming context','FontSize', 18) -title('Decoding accuracy for the context of grooming give','FontSize', 14) - -cd(savePath) -saveas(gcf,['Decoding grooming given context.png']) - -%Plotting results decoding accuracy for grooming receive -figure; hold on; set(gcf,'Position',[150 250 700 500]) -cmap = hsv(size(groom_receive_hitrate,1)); -for b = 1:size(groom_receive_hitrate,1) - y = groom_receive_hitrate(b,:); - y_shuffle = groom_receive_hitrate_shuffled(b,:); - % std_dev = sd_hitrate(b,:); - % errorbar(y,std_dev,'s','MarkerSize',10,... - % 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) - scatter(1:4,y,40,'filled','Color',cmap(b,:)) - % scatter(1:4,y_shuffle,40,'filled','Color','k') -end -leg = legend(["vlPFC", "TEO","All"]); -title(leg,'Brain Area') -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 4 4.2]); xlim([0.8 4.2]); ylim([0.4 1]) -xticklabels({'','Start vs. end','Post-threat or not','Reciprocal or not', 'Subject-initiated vs. not',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Grooming context','FontSize', 18) -title('Decoding accuracy for the context of grooming received','FontSize', 14) - -cd(savePath) -saveas(gcf,['Decoding grooming received context.png']) - -close all - diff --git a/Linear decoding/old_code/log_SVM_grooming_ReceiveGivePooled.m b/Linear decoding/old_code/log_SVM_grooming_ReceiveGivePooled.m deleted file mode 100644 index 267c58d..0000000 --- a/Linear decoding/old_code/log_SVM_grooming_ReceiveGivePooled.m +++ /dev/null @@ -1,256 +0,0 @@ -%% Log_SVM_grooming -%% Run a linear decoder on a the neural activity for different grooming contexts -% This script allows to deocde grooming: -% 1. Start vs. end -% 2. Post-threat or not -% 3. Reciprocated or not -% 4. Initiated or not -%Camille Testard, March 2022 - - -%% Load data - -%Set path -is_mac = 1; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -clearvars -except savePath filePath is_mac - -%Set parameters -temp = 1; temp_resolution = 1; -chan = 1; channel_flag = "all"; -randomsample=0; -with_NC=1; -isolatedOnly=0; - -%for channel_flag = ["vlPFC", "TEO", "all"] - -%Get data with specified temporal resolution and channels -%[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final, unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); -[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final, unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - -%filePath is the experimental data path -%Temp_resolution is the temporal resolution at which we would like to -%analyze the data -%Channel_flag specifies with channels to include: only TEO array, only -%vlPFC array or all channels -%is_mac is whether a mac or a pc is being used -disp('Data Loaded') - -Spike_count_raster = Spike_rasters'; -behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - -%% Add labelling for grooming -%Beginning vs. end of grooming bout -%Grooming post-threat vs not. -%Grooming after reciprocation or not -%Groom received after grm Prsnt or not - -groom_categ_label = {'Star.vs.end', 'Post-threat','Reciprocated','Initiated'}; - -b = 1; -behav = [7,8]; - groom_behav={'Give','Receive'}; - - for groom_categ = 1:4 - %Note: after threat, almost always groom RECEIVE, not given by - %subject. Also, grooming after groom present is only for groom - %RECEIVE. - - groom_labels = groom_labels_all(:,groom_categ+1); - - - %% Select behaviors to decode - - % Select behaviors with a minimum # of occurrences - %behav =[find(matches(behav_categ,'Groom Give'))]; %find(matches(behav_categ,'Groom Give')), - - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = groom_labels(idx,:);%Same as above but in behavior labels - - %If groom label is start vs. end - if groom_categ==1 - idx_epoch = find(~ismember(behavior_labels_final,3)); %remove middle chunk - Spike_count_raster_final = Spike_count_raster_final(idx_epoch,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels_final(idx_epoch,:);%Same as above but in behavior labels - end - - behav_size=tabulate(behavior_labels_final); - disp('########################') - tabulate(behavior_labels_final) - disp('########################') - - channel = char(channel_flag); - disp('****************************************************************************') - disp(['Groom categ: ' groom_categ_label{groom_categ} ', Channels: ' channel ', Behavior: Groom ' groom_behav{b}]) - disp('****************************************************************************') - - %pause(5) - if all(behav_size(:,2)>=30) && length(behav_size(:,2))>1 - %% Run SVM over multiple iterations - num_iter = 100; - - disp('Start running SVM...') - for iter = 1:num_iter - - % clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... - % Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... - % channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %subsample to match number of neurons across brain areas - Labels = behavior_labels_final; - if randomsample==1 && channel_flag~="all" - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - if min(num_trials)<50 %If there are less than 50 instances for a given behavior - minNumTrials = min(num_trials); %use the minimum # of instances - else - minNumTrials = 50; %impose 50 occurrences per category - end - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - %disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - mean_hitrate(chan,b,groom_categ) = mean(hitrate); - sd_hitrate(chan,b,groom_categ) = std(hitrate); - mean_hitrate_shuffled(chan,b,groom_categ) = mean(hitrate_shuffled); - sd_hitrate_shuffled(chan,b,groom_categ) = std(hitrate_shuffled); - - % C_concat=cat(3,C{:}); - % confusion_mat_avg=round(mean(C_concat,3)*100); - % rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - % C_table{chan,b,groom_categ} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - % - else - mean_hitrate(chan,b,groom_categ) = nan; - sd_hitrate(chan,b,groom_categ) = nan; - end - end - b = b+1; -chan = chan +1; -clearvars -except is_mac savePath mean_hitrate sd_hitrate mean_hitrate_shuffled sd_hitrate_shuffled C_table temp_resolution channel_flag filePath chan temp behavs_eval behav randomsample onlysingle b groom_labels_all isolatedOnly with_NC groom_categ_label -%end - -squeeze(mean_hitrate) -squeeze(sd_hitrate) -(squeeze(mean_hitrate) - squeeze(mean_hitrate_shuffled))./squeeze(mean_hitrate_shuffled) - -cd(savePath) - -rowNames = ["1sec"]; colNames = ["vlPFC","TEO","all"]; -result_hitrate = array2table(mean_hitrate,'RowNames',rowNames,'VariableNames',colNames) -result_hitrate_shuffled = array2table(mean_hitrate_shuffled,'RowNames',rowNames,'VariableNames',colNames) -result_sdhitrate = array2table(sd_hitrate,'RowNames',rowNames,'VariableNames',colNames) - -% save([savePath '\SVM_results_social_context.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -% writetable(result_hitrate,[savePath '\SVM_results_social_context.csv'],'WriteRowNames',true,'WriteVariableNames',true); -save([savePath '\SVM_results_' num2str(length(behav)) 'behav_NOsubsample_unique.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -writetable(result_hitrate,[savePath '\SVM_results_' num2str(length(behav)) 'behav_NOsubsample_unique.csv'],'WriteRowNames',true,'WriteVariableNames',true); - -groom_give_hitrate = reshape(mean_hitrate(:,1,:), [3,4]); -groom_give_hitrate_shuffled = reshape(mean_hitrate_shuffled(:,1,:), [3,4]); -groom_receive_hitrate = reshape(mean_hitrate(:,2,:), [3,4]); -groom_receive_hitrate_shuffled = reshape(mean_hitrate_shuffled(:,2,:), [3,4]); - -%Plotting results decoding accuracy for grooming give -figure; hold on; set(gcf,'Position',[150 250 700 500]) -cmap = hsv(size(groom_give_hitrate,1)); -for b = 1:size(groom_give_hitrate,1) - y = groom_give_hitrate(b,[1,3]); - % std_dev = sd_hitrate(b,:); - % errorbar(y,std_dev,'s','MarkerSize',10,... - % 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) - scatter(1:2,y,40,'filled','Color',cmap(b,:)) -end -leg = legend(["vlPFC", "TEO","All"]); -title(leg,'Brain Area') -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 2.2]); xlim([0.8 2.2]); ylim([0.4 1]) -xticklabels({'','Start vs. end','Reciprocal or not',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Grooming context','FontSize', 18) -title('Decoding accuracy for the context of grooming give','FontSize', 14) - -cd(savePath) -saveas(gcf,['Decoding grooming given context.png']) - -%Plotting results decoding accuracy for grooming receive -figure; hold on; set(gcf,'Position',[150 250 700 500]) -cmap = hsv(size(groom_receive_hitrate,1)); -for b = 1:size(groom_receive_hitrate,1) - y = groom_receive_hitrate(b,:); - y_shuffle = groom_receive_hitrate_shuffled(b,:); - % std_dev = sd_hitrate(b,:); - % errorbar(y,std_dev,'s','MarkerSize',10,... - % 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) - scatter(1:4,y,40,'filled','Color',cmap(b,:)) - % scatter(1:4,y_shuffle,40,'filled','Color','k') -end -leg = legend(["vlPFC", "TEO","All"]); -title(leg,'Brain Area') -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 4 4.2]); xlim([0.8 4.2]); ylim([0.4 1]) -xticklabels({'','Start vs. end','Post-threat or not','Reciprocal or not', 'Subject-initiated vs. not',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Grooming context','FontSize', 18) -title('Decoding accuracy for the context of grooming received','FontSize', 14) - -cd(savePath) -saveas(gcf,['Decoding grooming received context.png']) - -close all - diff --git a/Linear decoding/old_code/log_SVM_grooming_batch_old.m b/Linear decoding/old_code/log_SVM_grooming_batch_old.m deleted file mode 100644 index 3610e53..0000000 --- a/Linear decoding/old_code/log_SVM_grooming_batch_old.m +++ /dev/null @@ -1,573 +0,0 @@ -%% Log_SVM_grooming_batch -% Run a linear decoder on a the neural activity for different grooming contexts -% This script allows to decode grooming: -% 1. Start vs. end -% 2. Post-threat or not -% 3. Reciprocated or not -% 4. Initiated or not -% Batch version to run across sessions. -%Camille Testard, March 2022 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -randomsample=0; -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. - -%Initialize -mean_hitrate = cell(length(sessions),3); -sd_hitrate = cell(length(sessions),3); -mean_hitrate_shuffled = cell(length(sessions),3); - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan=1; - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - - %% Decode grooming context for groom give and groom receive separately - %Beginning vs. end of grooming bout - %Grooming post-threat vs not. - %Grooming after reciprocation or not - %Groom received after grm Prsnt or not - - groom_categ_label = {'Star.vs.end', 'Post-threat','Reciprocated','Initiated'}; %label grooming categ - - beh = 1; - %for behav = [7,8] %For both groom give and groom receive - - behav = [7,8]; - groom_behav={'Give','Receive'}; - - for groom_categ = 2:4 %For all grooming contexts - %Note: after threat, almost always groom RECEIVE, not given by - %subject. Also, grooming after groom present is only for groom - %RECEIVE. - - groom_labels = groom_labels_all(:,groom_categ+1); - - - %% Select behaviors to decode - - % Select behaviors with a minimum # of occurrences - %behav =[find(matches(behav_categ,'Groom Give'))]; %find(matches(behav_categ,'Groom Give')), - - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = groom_labels(idx,:);%Same as above but in behavior labels - - %If groom label is start vs. end - if groom_categ==1 - idx_epoch = find(~ismember(behavior_labels_final,3)); %remove middle chunk - Spike_count_raster_final = Spike_count_raster_final(idx_epoch,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels_final(idx_epoch,:);%Same as above but in behavior labels - end - - behav_size=tabulate(behavior_labels_final); - disp('########################') - tabulate(behavior_labels_final) - disp('########################') - - channel = char(channel_flag); - % disp('****************************************************************************') - % disp(['Groom categ: ' groom_categ_label{groom_categ} ', Channels: ' channel ', Behavior: Groom ' groom_behav{beh}]) - % disp('****************************************************************************') - - %pause(5) - if all(behav_size(:,2)>=30) && length(behav_size(:,2))>1 %If there are at least 30 occurrence of grooming in context 'b' - - %Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - %subsample to match number of neurons across brain areas - Labels = behavior_labels_final; - if randomsample==1 && channel_flag~="all" - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - if min(num_trials)<50 %If there are less than 50 instances for a given behavior - minNumTrials = min(num_trials); %use the minimum # of instances - else - minNumTrials = 50; %impose 50 occurrences per category - end - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %end of SVM iterations - - mean_hitrate{s,groom_categ}(beh,chan) = mean(hitrate); - sd_hitrate{s,groom_categ}(beh,chan) = std(hitrate); - mean_hitrate_shuffled{s,groom_categ}(beh,chan) = mean(hitrate_shuffled); - sd_hitrate_shuffled = std(hitrate_shuffled); - - else - - mean_hitrate(s,groom_categ,beh,chan) = nan; - sd_hitrate(s,groom_categ,beh,chan) = nan; - - end % End of "min number of grooming of category b" clause - - clear labels_id - - end %End of grooming context loop - beh = beh+1; - - %end %End of give vs. receive for loop - - chan = chan +1; - end %End of channel for loop - - %% Plotting results decoding accuracy for grooming context - -% % % % figure; set(gcf,'Position',[150 250 1300 400]) -% % % % subplot(1,2,1);hold on; %Groom Give -% % % % for c = 1:3 -% % % % y = mean_hitrate{s,c}(1,:); -% % % % std_dev = sd_hitrate{s,c}(1,:); -% % % % scatter(1:4,y,60,'filled', 'MarkerFaceAlpha',0.7) -% % % % % errorbar(y,std_dev,'s') -% % % % end -% % % % leg = legend(["vlPFC","TEO","All"]); -% % % % title(leg,'Brain Area') -% % % % chance_level = 1/2; -% % % % yline(chance_level,'--','Chance level', 'FontSize',16) -% % % % xticks([1:4]); xlim([0.8 4.2]); ylim([0.4 0.85]) -% % % % xticklabels(groom_categ_label) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel('Mean decoding accuracy','FontSize', 18); xlabel('Grooming Give Context','FontSize', 18) -% % % % title('Decoding accuracy for the context of groom give','FontSize', 14) -% % % % -% % % % subplot(1,2,2);hold on %Groom Receive -% % % % for c = 1:3 -% % % % y = mean_hitrate{s,c}(2,:); -% % % % std_dev = sd_hitrate{s,c}(2,:); -% % % % scatter(1:4,y,60,'filled', 'MarkerFaceAlpha',0.7) -% % % % % errorbar(y,std_dev,'s') -% % % % end -% % % % leg = legend(["vlPFC","TEO","All"]); -% % % % title(leg,'Brain Area') -% % % % chance_level = 1/2; -% % % % yline(chance_level,'--','Chance level', 'FontSize',16) -% % % % xticks([1:4]); xlim([0.8 4.2]); ylim([0.4 0.85]) -% % % % xticklabels(groom_categ_label) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel('Mean decoding accuracy','FontSize', 18); xlabel('Grooming Receive Context','FontSize', 18) -% % % % title('Decoding accuracy for the context of groom receive','FontSize', 14) -% % % % -% % % % cd(savePath) -% % % % saveas(gcf,['Decoding grooming given context.png']) -% % % % close all - -end %End of session for loop - -%Change savePath for all session results folder: - cd('~/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/') -save('SVM_results_groomingCateg.mat', "mean_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ","home") -load('SVM_results_groomingCateg.mat') - -%Plotting results decoding accuracy for grooming context -figure; set(gcf,'Position',[150 250 1000 400]) -subplot(1,2,1);hold on; %Groom Give, monkey A -cmap={'b','r','g'}; -jitter = [-0.1 0 0.1]; -for s=a_sessions - for c = 1:3 - y = mean_hitrate{s,c}(1,:); - std_dev = sd_hitrate{s,c}(1,:); - scatter(1+jitter(c):4+jitter(c),y,60,'filled', 'MarkerFaceAlpha',0.7,'MarkerFaceColor',cmap{c}) - end -end -leg = legend(["vlPFC","TEO","All"], 'Location','best'); -title(leg,'Brain Area') -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([1:4]); xlim([0.8 4.2]); ylim([0.4 1]) -xticklabels(groom_categ_label) -ax = gca; -ax.FontSize = 14; -ylabel('Mean decoding accuracy','FontSize', 18); xlabel('Grooming Give Context','FontSize', 18) -title('Decoding accuracy for the context of groom partner, Monkey A','FontSize', 14) -% -% subplot(2,2,2);hold on; %Groom Receive, monkey A -% for s=a_sessions -% for c = 1:3 -% y = mean_hitrate{s,c}(2,:); -% std_dev = sd_hitrate{s,c}(2,:); -% scatter(1+jitter(c):4+jitter(c),y,60,'filled', 'MarkerFaceAlpha',0.7,'MarkerFaceColor',cmap{c}) -% end -% end -% leg = legend(["vlPFC","TEO","All"], 'Location','best'); -% title(leg,'Brain Area') -% chance_level = 1/2; -% yline(chance_level,'--','Chance level', 'FontSize',16) -% xticks([1:4]); xlim([0.8 4.2]); ylim([0.4 1]) -% xticklabels(groom_categ_label) -% ax = gca; -% ax.FontSize = 14; -% ylabel('Mean decoding accuracy','FontSize', 18); xlabel('Grooming Give Context','FontSize', 18) -% title('Decoding accuracy for the context of getting groomed, Monkey A','FontSize', 14) - -subplot(1,2,2);hold on %Groom Give, monkey H -for s=h_sessions - for c = 1:3 - y = mean_hitrate{s,c}(1,:); - std_dev = sd_hitrate{s,c}(1,:); - scatter(1+jitter(c):4+jitter(c),y,60,'filled', 'MarkerFaceAlpha',0.7,'MarkerFaceColor',cmap{c}) - end -end -leg = legend(["vlPFC","TEO","All"], 'Location','best'); -title(leg,'Brain Area') -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([1:4]); xlim([0.8 4.2]); ylim([0.4 1]) -xticklabels(groom_categ_label) -ax = gca; -ax.FontSize = 14; -ylabel('Mean decoding accuracy','FontSize', 18); xlabel('Grooming Receive Context','FontSize', 18) -title('Decoding accuracy for the context of groom partner, Monkey H','FontSize', 14) - - -% subplot(2,2,4);hold on %Groom Receive, monkey H -% for s=h_sessions -% for c = 1:3 -% y = mean_hitrate{s,c}(2,:); -% std_dev = sd_hitrate{s,c}(2,:); -% scatter(1+jitter(c):4+jitter(c),y,60,'filled', 'MarkerFaceAlpha',0.7,'MarkerFaceColor',cmap{c}) -% % errorbar(y,std_dev,'s') -% end -% end -% leg = legend(["vlPFC","TEO","All"], 'Location','best'); -% title(leg,'Brain Area') -% chance_level = 1/2; -% yline(chance_level,'--','Chance level', 'FontSize',16) -% xticks([1:4]); xlim([0.8 4.2]); ylim([0.4 1]) -% xticklabels(groom_categ_label) -% ax = gca; -% ax.FontSize = 14; -% ylabel('Mean decoding accuracy','FontSize', 18); xlabel('Grooming Receive Context','FontSize', 18) -% title('Decoding accuracy for the context of getting groomed, Monkey H','FontSize', 14) - -cd(savePath) -%saveas(gcf,['Decoding grooming given context by area.png']) - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plotting results decoding accuracy for grooming context -figure; set(gcf,'Position',[150 250 1300 800]) -subplot(1,2,2);hold on; %Groom Give, monkey A -for s=a_sessions - y = mean_hitrate{s,3}(1,:); - std_dev = sd_hitrate{s,3}(1,:); - scatter(1:4,y,60,'filled', 'MarkerFaceAlpha',0.5) -end -leg = legend({sessions(a_sessions).name}, 'Location','bestoutside'); -title(leg,'Session') -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([1:4]); xlim([0.8 4.2]); ylim([0.4 1]) -xticklabels(groom_categ_label) -ax = gca; -ax.FontSize = 14; -ylabel('Mean decoding accuracy','FontSize', 18); xlabel('Grooming Give Context','FontSize', 18) -title('Decoding accuracy for the context of groom give, Monkey A','FontSize', 14) - -subplot(2,2,2);hold on; %Groom Receive, monkey A -for s=a_sessions - y = mean_hitrate{s,3}(2,:); - std_dev = sd_hitrate{s,3}(2,:); - scatter(1:4,y,60,'filled', 'MarkerFaceAlpha',0.5) -end -leg = legend({sessions(a_sessions).name}, 'Location','bestoutside'); -title(leg,'Session') -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([1:4]); xlim([0.8 4.2]); ylim([0.4 1]) -xticklabels(groom_categ_label) -ax = gca; -ax.FontSize = 14; -ylabel('Mean decoding accuracy','FontSize', 18); xlabel('Grooming Give Context','FontSize', 18) -title('Decoding accuracy for the context of groom receive, Monkey A','FontSize', 14) - -subplot(2,2,3);hold on %Groom Give, monkey H -for s=h_sessions - y = mean_hitrate{s,3}(1,:); - std_dev = sd_hitrate{s,3}(1,:); - scatter(1:4,y,60,'filled', 'MarkerFaceAlpha',0.5) -end -leg = legend({sessions(h_sessions).name}, 'Location','bestoutside'); -title(leg,'Session') -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([1:4]); xlim([0.8 4.2]); ylim([0.4 1]) -xticklabels(groom_categ_label) -ax = gca; -ax.FontSize = 14; -ylabel('Mean decoding accuracy','FontSize', 18); xlabel('Grooming Receive Context','FontSize', 18) -title('Decoding accuracy for the context of groom give, Monkey H','FontSize', 14) - - -subplot(2,2,4);hold on %Groom Receive, monkey H -for s=h_sessions - y = mean_hitrate{s,3}(2,:); - std_dev = sd_hitrate{s,3}(2,:); - scatter(1:4,y,60,'filled', 'MarkerFaceAlpha',0.5) -end -leg = legend({sessions(h_sessions).name}, 'Location','bestoutside'); -title(leg,'Session') -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([1:4]); xlim([0.8 4.2]); ylim([0.4 1]) -xticklabels(groom_categ_label) -ax = gca; -ax.FontSize = 14; -ylabel('Mean decoding accuracy','FontSize', 18); xlabel('Grooming Receive Context','FontSize', 18) -title('Decoding accuracy for the context of groom receive, Monkey H','FontSize', 14) - -cd(savePath) -saveas(gcf,['Decoding grooming given context_all units.png']) - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data = cell2mat(mean_hitrate'); -data_shuffle = cell2mat(mean_hitrate_shuffled'); -bp = bar([mean(data(:,1:4)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); - -sp1 = scatter(ones(size(data,1))*0.75,data(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*1.25,data(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data,1))*1.75,data_shuffle(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1))*2,data_shuffle(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*2.25,data_shuffle(:,3), 'filled','y'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0.4 0.9]) -xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) -ax = gca; -ax.FontSize = 16; -saveas(gcf,['SVM_results_allSessions_GroomingContext.png']) - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plotting results decoding accuracy for grooming context -figure; set(gcf,'Position',[150 250 1300 800]) -subplot(2,1,1);hold on; %Groom Give, monkey A -cmap={'b','r','g'}; -jitter = [-0.1 0 0.1]; - -for s=[a_sessions h_sessions] - for c = 1:3 - y = mean_hitrate{s,c}(1,:); - std_dev = sd_hitrate{s,c}(1,:); - scatter(1+jitter(c):4+jitter(c),y,60,'filled', 'MarkerFaceAlpha',0.7,'MarkerFaceColor',cmap{c}) - end -end -leg = legend(["vlPFC","TEO","All"], 'Location','best'); -title(leg,'Brain Area') -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([1:4]); xlim([0.8 4.2]); ylim([0.4 1]) -xticklabels(groom_categ_label) -ax = gca; -ax.FontSize = 14; -ylabel('Mean decoding accuracy','FontSize', 18); xlabel('Grooming Give Context','FontSize', 18) -title('Decoding accuracy for the context of groom partner, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; %Groom Receive, monkey A -for s=[a_sessions h_sessions] - for c = 1:3 - y = mean_hitrate{s,c}(2,:); - std_dev = sd_hitrate{s,c}(2,:); - scatter(1+jitter(c):4+jitter(c),y,60,'filled', 'MarkerFaceAlpha',0.7,'MarkerFaceColor',cmap{c}) - end -end -leg = legend(["vlPFC","TEO","All"], 'Location','best'); -title(leg,'Brain Area') -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([1:4]); xlim([0.8 4.2]); ylim([0.4 1]) -xticklabels(groom_categ_label) -ax = gca; -ax.FontSize = 14; -ylabel('Mean decoding accuracy','FontSize', 18); xlabel('Grooming Give Context','FontSize', 18) -title('Decoding accuracy for the context of getting groomed, Monkey A','FontSize', 14) - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plotting results decoding accuracy for grooming context --> combining -figure; set(gcf,'Position',[150 250 1300 800]) -hold on; -cmap={'b','r','y'}; -jitter = [-0.22 0 0.22]; - -s=1; -for s=[a_sessions h_sessions] - for c = 1:3 % for all brain areas - y = mean_hitrate{s,c}(1,1); g1(sesh, c) = nanmean(y); - scatter(1+jitter(c),y,60,'filled', 'MarkerFaceAlpha',0.7,'MarkerFaceColor',cmap{c}) - - y = mean_hitrate{s,c}(2,2); g2(sesh, c) = nanmean(y); - scatter(2+jitter(c),y,60,'filled', 'MarkerFaceAlpha',0.7,'MarkerFaceColor',cmap{c}) - - y = mean_hitrate{s,c}(2,3); g3(sesh, c) = nanmean(y); - scatter(3+jitter(c),y,60,'filled', 'MarkerFaceAlpha',0.7,'MarkerFaceColor',cmap{c}) - - y = mean_hitrate{s,c}(1,4); g4(sesh, c) = nanmean(y); - scatter(4+jitter(c),y,60,'filled', 'MarkerFaceAlpha',0.7,'MarkerFaceColor',cmap{c}) - end - sesh = sesh+1; -end -bp = bar([nanmean(g1); nanmean(g2); nanmean(g3); nanmean(g4)],'FaceAlpha',0.2); -bp(1).FaceColor= [0 0.4470 0.7410]; bp(2).FaceColor= [0.8500 0.3250 0.0980]; bp(3).FaceColor= [0.9290 0.6940 0.1250]; -%leg = legend(["vlPFC","TEO","All"], 'Location','best'); -%title(leg,'Brain Area') -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16, 'LineWidth',2) -xticks([1:4]); xlim([0 5]); ylim([0.4 1]) -%xticklabels({'Start vs. end grooming bout', 'Getting grooming post-threat vs. not', 'Reciprocated grooming vs. not', 'Initiated grooming vs. not'}) -xticklabels({'Start vs. end', 'Post-threat', 'Reciprocated', 'Initiated'}) -ax = gca; -ax.FontSize = 24; -ylabel('Decoding accuracy','FontSize', 24); %xlabel('Grooming Give Context','FontSize', 18) -%title('Decoding accuracy for the context of groom partner, Monkey A','FontSize', 14) -cd(savePath) -%saveas(gcf,['Decoding grooming given context by area.pdf']) - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plotting results decoding accuracy for grooming context --> combining -figure; set(gcf,'Position',[150 250 1300 800]) -hold on; -cmap={'b','r','y'}; - -g2=nan(max(h_sessions),3); g3=nan(max(h_sessions),3);g4=nan(max(h_sessions),3); -g2_shuffled=nan(max(h_sessions),3); g3_shuffled=nan(max(h_sessions),3);g4_shuffled=nan(max(h_sessions),3); - -for s=[a_sessions h_sessions] - for c = 1:3 % for all brain areas - - y = mean_hitrate{s,c}(2,2); g2(s, c) = nanmean(y); - y_shuffled = mean_hitrate_shuffled{s,c}(2,2); g2_shuffled(s, c) = nanmean(y_shuffled); - - y = mean_hitrate{s,c}(2,3); g3(s, c) = nanmean(y); - y_shuffled = mean_hitrate_shuffled{s,c}(2,3); g3_shuffled(s, c) = nanmean(y_shuffled); - - %Note no all session can do the grooming context.. try catch - try - y = mean_hitrate{s,c}(1,4); g4(s, c) = nanmean(y); - y_shuffled = mean_hitrate_shuffled{s,c}(1,4); g4_shuffled(s, c) = nanmean(y_shuffled); - catch - y=nan; y_shuffled=nan; - end - - end -end - -g2(g2==0)=nan; g2_shuffled(g2_shuffled==0)=nan; -g3(g3==0)=nan; g3_shuffled(g3_shuffled==0)=nan; -g4(g4==0)=nan; g4_shuffled(g4_shuffled==0)=nan; - -bp = bar([nanmean(g2); nanmean(g2_shuffled)],'FaceAlpha',0.2); -bp(1).FaceColor= [0 0.4470 0.7410]; bp(2).FaceColor= [0.8500 0.3250 0.0980]; bp(3).FaceColor= [0.9290 0.6940 0.1250]; -%leg = legend(["vlPFC","TEO","All"], 'Location','best'); -%title(leg,'Brain Area') -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16, 'LineWidth',2) -xticks([1:4]); xlim([0 5]); ylim([0.4 1]) -%xticklabels({'Start vs. end grooming bout', 'Getting grooming post-threat vs. not', 'Reciprocated grooming vs. not', 'Initiated grooming vs. not'}) -xticklabels({'Start vs. end', 'Post-threat', 'Reciprocated', 'Initiated'}) -ax = gca; -ax.FontSize = 24; -ylabel('Decoding accuracy','FontSize', 24); %xlabel('Grooming Give Context','FontSize', 18) -%title('Decoding accuracy for the context of groom partner, Monkey A','FontSize', 14) -cd(savePath) -%saveas(gcf,['Decoding grooming given context by area.pdf']) \ No newline at end of file diff --git a/Linear decoding/old_code/log_SVM_grooming_pose.m b/Linear decoding/old_code/log_SVM_grooming_pose.m deleted file mode 100644 index 1375dbd..0000000 --- a/Linear decoding/old_code/log_SVM_grooming_pose.m +++ /dev/null @@ -1,242 +0,0 @@ -%% Log_SVM_grooming_batch -% Run a linear decoder on a the neural activity for different grooming contexts -% This script allows to decode grooming: -% 1. Start vs. end -% 2. Post-threat or not -% 3. Reciprocated or not -% 4. Initiated or not -% Batch version to run across sessions. -%Camille Testard, March 2022 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -randomsample=0; -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. - -%Initialize -% mean_hitrate = cell(length(sessions),3); -% sd_hitrate = cell(length(sessions),3); -% mean_hitrate_shuffled = cell(length(sessions),3); - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan=1; - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_groom_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - groom_labels = cell2mat({labels{:,14}}'); - - %% Decode grooming pose for groom give and groom receive separately - %Beginning vs. end of grooming bout - %Grooming post-threat vs not. - %Grooming after reciprocation or not - %Groom received after grm Prsnt or not - - - %% Select behaviors to decode - - % Select behaviors with a minimum # of occurrences - behav = [8]; - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - groom_labels_final = groom_labels(idx,:);%Same as above but in behavior labels - groom_labels_final=groom_labels_final(groom_labels_final~=0); - - pose = unique(groom_labels_final); - - - behav_size=tabulate(groom_labels_final); - disp('########################') - tabulate(groom_labels_final) - disp('########################') - - channel = char(channel_flag); - % disp('****************************************************************************') - % disp(['Groom categ: ' groom_categ_label{groom_categ} ', Channels: ' channel ', Behavior: Groom ' groom_behav{beh}]) - % disp('****************************************************************************') - - %pause(5) - %Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - Labels = groom_labels_final; - Input_matrix = Spike_count_raster_final; - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - if min(num_trials)<50 %If there are less than 50 instances for a given behavior - minNumTrials = min(num_trials); %use the minimum # of instances - else - minNumTrials = 50; %impose 50 occurrences per category - end - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %end of SVM iterations - - mean_hitrate(chan,s) = mean(hitrate) - sd_hitrate(chan,s) = std(hitrate); - mean_hitrate_shuffled(chan,s) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - clear labels_id - - chan = chan +1; - end %End of channel for loop - - %% Plotting results decoding accuracy for grooming context - -% % % % figure; set(gcf,'Position',[150 250 1300 400]) -% % % % subplot(1,2,1);hold on; %Groom Give -% % % % for c = 1:3 -% % % % y = mean_hitrate{s,c}(1,:); -% % % % std_dev = sd_hitrate{s,c}(1,:); -% % % % scatter(1:4,y,60,'filled', 'MarkerFaceAlpha',0.7) -% % % % % errorbar(y,std_dev,'s') -% % % % end -% % % % leg = legend(["vlPFC","TEO","All"]); -% % % % title(leg,'Brain Area') -% % % % chance_level = 1/2; -% % % % yline(chance_level,'--','Chance level', 'FontSize',16) -% % % % xticks([1:4]); xlim([0.8 4.2]); ylim([0.4 0.85]) -% % % % xticklabels(groom_categ_label) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel('Mean decoding accuracy','FontSize', 18); xlabel('Grooming Give Context','FontSize', 18) -% % % % title('Decoding accuracy for the context of groom give','FontSize', 14) -% % % % -% % % % subplot(1,2,2);hold on %Groom Receive -% % % % for c = 1:3 -% % % % y = mean_hitrate{s,c}(2,:); -% % % % std_dev = sd_hitrate{s,c}(2,:); -% % % % scatter(1:4,y,60,'filled', 'MarkerFaceAlpha',0.7) -% % % % % errorbar(y,std_dev,'s') -% % % % end -% % % % leg = legend(["vlPFC","TEO","All"]); -% % % % title(leg,'Brain Area') -% % % % chance_level = 1/2; -% % % % yline(chance_level,'--','Chance level', 'FontSize',16) -% % % % xticks([1:4]); xlim([0.8 4.2]); ylim([0.4 0.85]) -% % % % xticklabels(groom_categ_label) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel('Mean decoding accuracy','FontSize', 18); xlabel('Grooming Receive Context','FontSize', 18) -% % % % title('Decoding accuracy for the context of groom receive','FontSize', 14) -% % % % -% % % % cd(savePath) -% % % % saveas(gcf,['Decoding grooming given context.png']) -% % % % close all - -end %End of session for loop - -%Change savePath for all session results folder: - cd('~/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/') -save('SVM_results_groomingCateg.mat', "mean_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ","home") -load('SVM_results_groomingCateg.mat') - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -mean_hitrate(mean_hitrate==0)=nan; -mean_hitrate_shuffled(mean_hitrate_shuffled==0)=nan; - -for groomcat = 2:4 - figure; hold on - data = squeeze(mean_hitrate(1,groomcat,:,:))'; - data_shuffled = squeeze(mean_hitrate_shuffled(1,groomcat,:,:))'; - bp = bar([nanmean(data); nanmean(data_shuffled)],'FaceAlpha',0.2); - - sp1 = scatter(ones(size(data,1))*0.78,data(:,1), 'filled','b'); - sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); - sp1 = scatter(ones(size(data,1))*1.22,data(:,3), 'filled','y'); - - sp1 = scatter(ones(size(data,1))*1.78,data_shuffle(:,1), 'filled','b'); - sp1 = scatter(ones(size(data,1))*2,data_shuffle(:,2), 'filled','r'); - sp1 = scatter(ones(size(data,1))*2.22,data_shuffle(:,3), 'filled','y'); - - legend(bp,{'vlPFC','TEO','all'},'Location','best') - - ylabel('Decoding Accuracy'); ylim([0.4 1]) - xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) - ax = gca; - ax.FontSize = 16; - - title(groom_categ_label(groomcat)) - -end \ No newline at end of file diff --git a/Linear decoding/old_code/log_SVM_neighbor_batch.m b/Linear decoding/old_code/log_SVM_neighbor_batch.m deleted file mode 100644 index 4b3029c..0000000 --- a/Linear decoding/old_code/log_SVM_neighbor_batch.m +++ /dev/null @@ -1,327 +0,0 @@ -%% Log_SVM_neighbor_batch -% Run a linear decoder on a the neural activity for the partner behaviors -% Batch version to run across sessions -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; -session_range_with_neighbor=11; - -%Set parameters -with_partner =2;% 0: no partner; 1: with partner; 2: with neighbor -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 50;%Number of SVM iterations -min_occurrences = 30;%Minimum number of occurrence per behavior -alone_block=0; %1: during alone block; 0:during paired blocks; anything else: all blocks. -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0; plot_toggle=0; %Set whether we want the null. Suppress plots when generating simulated labels -simplify=0;%lump similar behavioral categories together to increase sample size. - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -elseif with_partner ==2 - session_range = session_range_with_neighbor; - a_sessions = nan; h_sessions = 11; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=11; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly, smooth, sigma); - elseif with_partner ==2 - [Spike_rasters, labels, labels_partner, labels_neighbor, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_neighbor(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_neighbor_init = cell2mat({labels_neighbor{:,3}}'); %Extract unique behavior info for partner - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - behavior_labels_neighbor_init(behavior_labels_neighbor_init==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - block_labels = cell2mat({labels{:,11}}'); %Extract block info - alone_block_id = find(strcmp(block_times{:,"Behavior"},"Alone.block")); - - - %Only consider paired blocks - if alone_block==1 - behavior_labels_subject_init = behavior_labels_subject_init(block_labels== alone_block_id); - behavior_labels_neighbor_init = behavior_labels_neighbor_init(block_labels== alone_block_id); - Spike_count_raster = Spike_count_raster(block_labels== alone_block_id,:); - block_labels=block_labels(block_labels== alone_block_id,:); - elseif alone_block==0 - behavior_labels_subject_init = behavior_labels_subject_init(block_labels~= alone_block_id); - behavior_labels_neighbor_init = behavior_labels_neighbor_init(block_labels~= alone_block_id); - Spike_count_raster = Spike_count_raster(block_labels~= alone_block_id,:); - block_labels=block_labels(block_labels~= alone_block_id,:); - end - - - %% Select behaviors to decode - - %Only consider epochs where subject is resting - idx = find(ismember(behavior_labels_subject_init,7) &... - ~ismember(behavior_labels_neighbor_init,length(behav_categ))); %find the indices where the subject is resting and the partner is not - Spike_count_raster_temp = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_neighbor = behavior_labels_neighbor_init(idx); - behavior_labels_subject = behavior_labels_subject_init(idx); - behav = unique(behavior_labels_neighbor); - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels_neighbor); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences=30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - - %Select the correct indices. - idx_beh = find(ismember(behavior_labels_neighbor,behav)); - subject_behav_after_selection = behavior_labels_subject(idx_beh); - neighbor_behav_after_selection = behavior_labels_neighbor(idx_beh); - block_after_selection = block_labels(idx_beh); - - Spike_count_raster_final = Spike_count_raster_temp(idx_beh,:); - behavior_labels_final = neighbor_behav_after_selection; - - overlap_partner_subject_after_selection = length(find(neighbor_behav_after_selection == subject_behav_after_selection))/length(idx); - fprintf('Percent overlap between subject and partner labels AFTER selecting behaviors: %s \n', num2str(overlap_partner_subject_after_selection)) - alone_block_obs = length(find(block_after_selection==3))/length(idx); - fprintf('Percent observations in "alone" block AFTER selecting behaviors: %s \n', num2str(alone_block_obs)) - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - if null - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels_final,behav_categ, temp_resolution,plot_toggle); - Labels= sim_behav; - else - Labels = behavior_labels_final; - end - - %Balance number of trials per class - Input_matrix = Spike_count_raster_final; - - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - -% channel = char(channel_flag); -% disp('****************************************************************************') -% disp(['ID type ' num2str(partner) ', channels: ' channel '. DONE']) -% disp('****************************************************************************') -% - mean_hitrate{s}(chan) = mean(hitrate) - sd_hitrate{s}(chan) = std(hitrate); - mean_hitrate_shuffled{s}(chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{s,chan} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - chan = chan +1; - end% End of channel for loop - - clear labels_id - -end% End of session for loop - - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_partnerBehav.mat', "mean_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") - - -%Plot decoding accuracy for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Decoding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && onlysingle==1 - saveas(gcf,['SVM_results_accuracy_behav_NOsubsample_unique_allSessions_partner.png']) -elseif randomsample ==1 && onlysingle==1 - saveas(gcf,['SVM_results_accuracy_behav_subsample_unique_allSessions_partner.png']) -elseif randomsample ==0 && onlysingle==0 - saveas(gcf,['SVM_results_accuracy_behav_NOsubsample_NOTunique_allSessions_partner.png']) -elseif randomsample ==1 && onlysingle==0 - saveas(gcf,['SVM_results_accuracy_behav_subsample_NOTunique_allSessions_partner.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot decoding accuracy relative to chance for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 4]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 4]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && unq_behav==1 - saveas(gcf,['SVM_results_behav_NOsubsample_unique_allSessions_partner.png']) -elseif randomsample ==1 && unq_behav==1 - saveas(gcf,['SVM_results_behav_subsample_unique_allSessions_partner.png']) -elseif randomsample ==0 && unq_behav==0 - saveas(gcf,['SVM_results_behav_NOsubsample_NOTunique_allSessions_partner.png']) -elseif randomsample ==1 && unq_behav==0 - saveas(gcf,['SVM_results_behav_subsample_NOTunique_allSessions_partner.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data = cell2mat(mean_hitrate'); -data_shuffle = cell2mat(mean_hitrate_shuffled'); -bp = bar([mean(data(:,:)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); - -sp1 = scatter(ones(size(data,1))*0.77,data(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*1.22,data(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data,1))*1.77,data_shuffle(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1))*2,data_shuffle(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*2.22,data_shuffle(:,3), 'filled','y'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0.1 0.6]) -xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) -ax = gca; -ax.FontSize = 16; -saveas(gcf,['SVM_results_allSessions_allUnits_NEIGHBOR.png']) - diff --git a/Linear decoding/old_code/log_SVM_neighbor_batch_old.m b/Linear decoding/old_code/log_SVM_neighbor_batch_old.m deleted file mode 100644 index b8f7ecb..0000000 --- a/Linear decoding/old_code/log_SVM_neighbor_batch_old.m +++ /dev/null @@ -1,385 +0,0 @@ -%% Log_SVM_neighbor_batch -% Run a linear decoder on a the neural activity for the partner behaviors -% Batch version to run across sessions -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; -session_range_with_neighbor=11; - -%Set parameters -with_partner =2;% 0: no partner; 1: with partner; 2: with neighbor -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 50;%Number of SVM iterations -min_occurrences = 30;%Minimum number of occurrence per behavior -alone_block=0; %1: during alone block; 0:during paired blocks; anything else: all blocks. -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -elseif with_partner ==2 - session_range = session_range_with_neighbor; - a_sessions = nan; h_sessions = 11; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=11; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - elseif with_partner ==2 - [Spike_rasters, labels, labels_partner, labels_neighbor, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_neighbor(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - behavior_labels_neighbor_init = cell2mat({labels_neighbor{:,3}}'); - block_labels = cell2mat({labels{:,11}}'); %Extract block info - alone_block_id = find(strcmp(block_times{:,1},"Alone.block")); - - %SANITY CHECK: Compute overlap between partner behavior and subject behavior - perc_overlap_subject_partner = length(find(behavior_labels_subject_init == behavior_labels_partner_init))/length(behavior_labels_subject_init); - perc_overlap_subject_neighbor = length(find(behavior_labels_subject_init == behavior_labels_neighbor_init))/length(behavior_labels_subject_init); - perc_overlap_partner_neighbor = length(find(behavior_labels_partner_init == behavior_labels_neighbor_init))/length(behavior_labels_subject_init); - - %Only consider pair or alone blocks - if alone_block==1 - behav_freq_table = tabulate(behavior_labels_subject_init(block_labels== alone_block_id)); - [~, max_beh]= max(behav_freq_table(:,2)); - - idx = find(block_labels== alone_block_id & behavior_labels_subject_init==max_beh); - behavior_labels_subject_init = behavior_labels_subject_init(idx); - behavior_labels_partner_init = behavior_labels_partner_init(idx); - behavior_labels_neighbor_init = behavior_labels_neighbor_init(idx); - Spike_count_raster = Spike_count_raster(idx,:); - block_labels=block_labels(idx); - - elseif alone_block==0 - behavior_labels_subject_init = behavior_labels_subject_init(block_labels~= alone_block_id); - behavior_labels_partner_init = behavior_labels_partner_init(block_labels~= alone_block_id); - behavior_labels_neighbor_init = behavior_labels_neighbor_init(block_labels~= alone_block_id); - Spike_count_raster = Spike_count_raster(block_labels~= alone_block_id,:); - block_labels=block_labels(block_labels~= alone_block_id,:); - - elseif alone_block==3 - idx = find(behavior_labels_subject_init==7); - behavior_labels_subject_init = behavior_labels_subject_init(idx); - behavior_labels_partner_init = behavior_labels_partner_init(idx); - behavior_labels_neighbor_init = behavior_labels_neighbor_init(idx); - Spike_count_raster = Spike_count_raster(idx,:); - block_labels=block_labels(idx); - - end - - - behavior_labels = behavior_labels_neighbor_init; - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - %behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences -% behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. -% behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - % Then select non-reciprocal behaviors - behav = setdiff(behav, reciprocal_set); - - % OR select behaviors manually - %behav = [4,5,17,23,25];%manually select behaviors of interest - %Select behaviors manually to ensure that the same - %behaviors are considered for the partner and subject comparisons. - %This list could change from session to session.. I'll have to - %think of a way to automatize this. - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(removecats(categorical(behavior_labels_final))); - - - %check the amount of labels that differ in the partner vs. subject - %labels after selecting the behaviors of interest. - subject_behav_after_selection = behavior_labels_subject_init(idx); - partner_behav_after_selection = behavior_labels_partner_init(idx); - neighbor_behav_after_selection = behavior_labels_neighbor_init(idx); - block_after_selection = block_labels(idx); - - - overlap_neighbor_subject_after_selection = length(find(neighbor_behav_after_selection == subject_behav_after_selection))/length(idx); - fprintf('Percent overlap between subject and neighbor labels AFTER selecting behaviors: %s \n', num2str(overlap_neighbor_subject_after_selection)) - - overlap_neighbor_partner_after_selection = length(find(neighbor_behav_after_selection == partner_behav_after_selection))/length(idx); - fprintf('Percent overlap between partner and neighbor labels AFTER selecting behaviors: %s \n', num2str(overlap_neighbor_partner_after_selection)) - - alone_block_obs = length(find(block_after_selection==3))/length(idx); - fprintf('Percent observations in "alone" block AFTER selecting behaviors: %s \n', num2str(alone_block_obs)) - - %Only consider windows where the behaviors of subject and - %partner do not overlap - diff_idx = find(neighbor_behav_after_selection ~= subject_behav_after_selection); %find the indices where subject and partner behavior do not overlap - Spike_count_raster_final = Spike_count_raster_final(diff_idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels_final(diff_idx);%Same as above but in behavior labels - subject_behav_after_selection = subject_behav_after_selection(diff_idx); - block_after_selection_final = block_after_selection(diff_idx); - behav = unique(behavior_labels_final); - - tabulate(removecats(categorical(behavior_labels_final))); - tabulate(block_after_selection_final) - crosstab(removecats(categorical(behavior_labels_final)), block_after_selection_final) - %Note: not all behaviors are equally happening across blocks. It's - %possible that what we decode is actually block ID and not the - %behavior itself... - - %Display which behaviors will be decoded - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - % %Only consider behaviors during the alone block - % block_after_selection_overlap_out = block_after_selection(diff_idx); - % alone_idx = find(block_after_selection_overlap_out==3); - % Spike_count_raster_final = Spike_count_raster_final(alone_idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - % behavior_labels_final = behavior_labels_final(alone_idx,:);%Same as above but in behavior labels - % tabulate(behavior_labels_final); - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - % clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... - % Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... - % channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %Balance number of trials per class - Labels = behavior_labels_final; - Input_matrix = Spike_count_raster_final; - - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min_occurrences;%min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - -% channel = char(channel_flag); -% disp('****************************************************************************') -% disp(['ID type ' num2str(partner) ', channels: ' channel '. DONE']) -% disp('****************************************************************************') -% - mean_hitrate{s}(chan) = mean(hitrate) - sd_hitrate{s}(chan) = std(hitrate); - mean_hitrate_shuffled{s}(chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{s,chan} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - chan = chan +1; - end% End of channel for loop - - clear labels_id - -end% End of session for loop - - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_partnerBehav.mat', "mean_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") - - -%Plot decoding accuracy for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Decoding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && onlysingle==1 - saveas(gcf,['SVM_results_accuracy_behav_NOsubsample_unique_allSessions_partner.png']) -elseif randomsample ==1 && onlysingle==1 - saveas(gcf,['SVM_results_accuracy_behav_subsample_unique_allSessions_partner.png']) -elseif randomsample ==0 && onlysingle==0 - saveas(gcf,['SVM_results_accuracy_behav_NOsubsample_NOTunique_allSessions_partner.png']) -elseif randomsample ==1 && onlysingle==0 - saveas(gcf,['SVM_results_accuracy_behav_subsample_NOTunique_allSessions_partner.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot decoding accuracy relative to chance for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 4]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 4]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && unq_behav==1 - saveas(gcf,['SVM_results_behav_NOsubsample_unique_allSessions_partner.png']) -elseif randomsample ==1 && unq_behav==1 - saveas(gcf,['SVM_results_behav_subsample_unique_allSessions_partner.png']) -elseif randomsample ==0 && unq_behav==0 - saveas(gcf,['SVM_results_behav_NOsubsample_NOTunique_allSessions_partner.png']) -elseif randomsample ==1 && unq_behav==0 - saveas(gcf,['SVM_results_behav_subsample_NOTunique_allSessions_partner.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data = cell2mat(mean_hitrate'); -data_shuffle = cell2mat(mean_hitrate_shuffled'); -bp = bar([mean(data(:,:)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); - -sp1 = scatter(ones(size(data,1))*0.77,data(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*1.22,data(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data,1))*1.77,data_shuffle(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1))*2,data_shuffle(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*2.22,data_shuffle(:,3), 'filled','y'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0.1 0.6]) -xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) -ax = gca; -ax.FontSize = 16; -saveas(gcf,['SVM_results_allSessions_allUnits_NEIGHBOR.png']) - diff --git a/Linear decoding/old_code/log_SVM_partner.m b/Linear decoding/old_code/log_SVM_partner.m deleted file mode 100644 index 8986e37..0000000 --- a/Linear decoding/old_code/log_SVM_partner.m +++ /dev/null @@ -1,238 +0,0 @@ -%% Log_SVM_partner -%% Run a linear decoder on a the neural activity for the partner behaviors - -%% Load data - -%Set path -is_mac = 0; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -clearvars -except savePath filePath is_mac - -%Set temporal resolution -temp = 1; temp_resolution = 1; -%temp_resolution = [1, 2, 5, 10] %5sec, 2sec, 1sec,500msec, 100msec -%1 for second resolution, 10 for 100msec resolution, 100 for 10msec resolution, 1000 for msec resolution. etc. -%0.1 for 10sec resolution, 1/5 for 5sec resolution - -%Set channels: 'TEO', 'vlPFC' or 'all' -chan = 1; channel_flag = "all"; -for channel_flag = ["vlPFC", "TEO", "all"] - - %Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac); - %filePath is the experimental data path - %Temp_resolution is the temporal resolution at which we would like to - %analyze the data - %Channel_flag specifies with channels to include: only TEO array, only - %vlPFC array or all channels - %is_mac is whether a mac or a pc is being used - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - block_labels = cell2mat({labels{:,10}}'); %Extract block info - - %SANITY CHECK: Compute overlap between partner behavior and subject behavior - perc_overlap_allbehav = length(find(behavior_labels_subject_init == behavior_labels_partner_init))/length(behavior_labels_subject_init); - overlap = tabulate(behavior_labels_subject_init(find(behavior_labels_subject_init == behavior_labels_partner_init))); - subject_behav = tabulate(behavior_labels_subject_init); partner_behav = tabulate(behavior_labels_partner_init); - perc_overlap_perbehav = [behav_categ', overlap(:,2), overlap(:,2)./subject_behav(:,2)*100]; - %Important notes: - %1. There are some discrepancies when comparing the partner and - %subject labels. Most notably in proximity, but also RR, HIP, HIS, - %SP, SS which should be the same. - %2. I just realized that depending on what other behavior is - %co-occurring, the label can change. This may explain discrepancies - %in RR and proximity - %3. it is often the case that self-groom events co-occur for Amos and Lovelace. I expect - %this to be the case for foraging in Hooke-pair. - - - behavior_labels = behavior_labels_partner_init; - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 50; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding rest which is a source of confusion. - - % OR select non-reciprocal behaviors (particularly important - % for decoding partner behavior) - behav = setdiff(behav, reciprocal_set); - - % OR select behaviors manually - behav = [4,5,17,23,25];%manually select behaviors of interest - %Select behaviors manually to ensure that the same - %behaviors are considered for the partner and subject comparisons. - %This list could change from session to session.. I'll have to - %think of a way to automatize this. - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(behavior_labels_final); - - %check the amount of labels that differ in the partner vs. subject - %labels after selecting the behaviors of interest. - if partner==1 - subject_behav_after_selection = behavior_labels_subject_init(idx); - partner_behav_after_selection = behavior_labels_partner_init(idx); - block_after_selection = block_labels(idx); - - overlap_partner_subject_after_selection = length(find((partner_behav_after_selection - subject_behav_after_selection)==0))/length(idx); - fprintf('Percent overlap between subject and partner labels AFTER selecting behaviors: %s \n', num2str(overlap_partner_subject_after_selection)); - alone_block_obs = length(find(block_after_selection==3))/length(idx); - fprintf('Percent observations in "alone" block AFTER selecting behaviors: %s \n', num2str(alone_block_obs)); - - %Only consider windows where the behaviors of subject and - %partner do not overlap - diff_idx = find((partner_behav_after_selection - subject_behav_after_selection)~=0); %find the indices where subject and partner behavior do not overlap - Spike_count_raster_final = Spike_count_raster_final(diff_idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels_final(diff_idx,:);%Same as above but in behavior labels - tabulate(behavior_labels_final); - %IMPORTANT NOTE: There will be much less instances - - % %Only consider behaviors during the alone block - % block_after_selection_overlap_out = block_after_selection(diff_idx); - % alone_idx = find(block_after_selection_overlap_out==3); - % Spike_count_raster_final = Spike_count_raster_final(alone_idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - % behavior_labels_final = behavior_labels_final(alone_idx,:);%Same as above but in behavior labels - % tabulate(behavior_labels_final); - end - - - %% Run SVM over multiple iterations - num_iter = 500; - - disp('Start running SVM...') - for iter = 1:num_iter - - % clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... - % Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... - % channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %Balance number of trials per class - Labels = behavior_labels_final; - Input_matrix = Spike_count_raster_final; - - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = 30;%min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - channel = char(channel_flag); - disp('****************************************************************************') - disp(['ID type ' num2str(partner) ', channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate(chan, id) = mean(hitrate) - sd_hitrate(chan, id) = std(hitrate); - mean_hitrate_shuffled(chan, id) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{chan, id} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - id = id+1; - - clearvars -except is_mac savePath mean_hitrate sd_hitrate mean_hitrate_shuffled C_table temp_resolution channel_flag filePath chan temp behavs_eval behav id - end - chan = chan +1; -end -%temp = temp+1; - -% end - -colNames = ["subject", "partner"]; rowNames = ["vlPFC","TEO","all"]; -result_hitrate = array2table(mean_hitrate,'RowNames',rowNames,'VariableNames',colNames) -result_sdhitrate = array2table(sd_hitrate,'RowNames',rowNames,'VariableNames',colNames); - -% save([savePath '\SVM_results_social_context.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -% writetable(result_hitrate,[savePath '\SVM_results_social_context.csv'],'WriteRowNames',true,'WriteVariableNames',true); -save([savePath '\SVM_results_' num2str(length(behav)) 'behav.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -writetable(result_hitrate,[savePath '\SVM_results_' num2str(length(behav)) 'behav.csv'],'WriteRowNames',true,'WriteVariableNames',true); - -%Plotting results for multiple behaviors at 1sec and lower resolution -figure; hold on; set(gcf,'Position',[150 250 1000 500]) -cmap = hsv(size(mean_hitrate,1)); -for b = 1:size(mean_hitrate,1) - y = mean_hitrate(b,:); - std_dev = sd_hitrate(b,:); - errorbar(y,std_dev,'s','MarkerSize',10,... - 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) - %plot(x,y,'Color','k') -end -leg = legend([rowNames, 'Chance']); -title(leg,'Brain area') -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 2.2]); xlim([0.8 2.2]); ylim([0 1]) -xticklabels({'','subject','partner',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('ID','FontSize', 18) -title('Decoding accuracy for subject vs. partner behavior','FontSize', 20) -cd(savePath) -saveas(gcf,['SVM_results_SubjectVsPartner.png']) -close all diff --git a/Linear decoding/old_code/log_SVM_partner_batch.asv b/Linear decoding/old_code/log_SVM_partner_batch.asv deleted file mode 100644 index b388b48..0000000 --- a/Linear decoding/old_code/log_SVM_partner_batch.asv +++ /dev/null @@ -1,335 +0,0 @@ -%% Log_SVM_partner_batch -% Run a linear decoder on a the neural activity during rest for the partner behaviors -% Batch version to run across sessions -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =1; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -min_occurrences = 30;%Minimum number of occurrence per behavior -alone_block=0; %1: during alone block; 0:during paired blocks; anything else: all blocks. -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. - - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=18; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - block_labels = cell2mat({labels{:,11}}'); %Extract block info - alone_block_id = find(strcmp(block_times{:,"Behavior"},"Alone.block")); - - - %Only consider paired blocks - if alone_block==1 - behavior_labels_subject_init = behavior_labels_subject_init(block_labels== alone_block_id); - behavior_labels_partner_init = behavior_labels_partner_init(block_labels== alone_block_id); - Spike_count_raster = Spike_count_raster(block_labels== alone_block_id,:); - block_labels=block_labels(block_labels== alone_block_id,:); - elseif alone_block==0 - behavior_labels_subject_init = behavior_labels_subject_init(block_labels~= alone_block_id); - behavior_labels_partner_init = behavior_labels_partner_init(block_labels~= alone_block_id); - Spike_count_raster = Spike_count_raster(block_labels~= alone_block_id,:); - block_labels=block_labels(block_labels~= alone_block_id,:); - end - - - %% Select behaviors to decode - - %Only consider epochs where subject is resting - idx = find(ismember(behavior_labels_subject_init,length(behav_categ)) &... - ~ismember(behavior_labels_partner_init,length(behav_categ))); %find the indices where the subject is resting and the partner is not - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_partner = behavior_labels_partner_init(idx); - behavior_labels_subject = behavior_labels_subject_init(idx); - behav = unique(behavior_labels_partner); - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels_partner); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences=30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - %OR manually select behaviors: - behav = [5,18,24]; - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - if length(behav)>2 - - - %Select the correct indices. - idx_beh = find(ismember(behavior_labels_partner,behav)); - subject_behav_after_selection = behavior_labels_subject(idx_beh); - partner_behav_after_selection = behavior_labels_partner(idx_beh); - block_after_selection = block_labels(idx_beh); - - Spike_count_raster_final = Spike_count_raster_final(idx_beh,:); - behavior_labels_final = partner_behav_after_selection; - - overlap_partner_subject_after_selection = length(find(partner_behav_after_selection == subject_behav_after_selection))/length(idx); - fprintf('Percent overlap between subject and partner labels AFTER selecting behaviors: %s \n', num2str(overlap_partner_subject_after_selection)) - alone_block_obs = length(find(block_after_selection==3))/length(idx); - fprintf('Percent observations in "alone" block AFTER selecting behaviors: %s \n', num2str(alone_block_obs)) - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - if null - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels_final,behav_categ, temp_resolution,0); - Labels = sim_behav; - else - Labels = behavior_labels_final; - end - - %Balance number of trials per class - Input_matrix = Spike_count_raster_final; - - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - % channel = char(channel_flag); - % disp('****************************************************************************') - % disp(['ID type ' num2str(partner) ', channels: ' channel '. DONE']) - % disp('****************************************************************************') - % - mean_hitrate{s}(chan) = mean(hitrate) - sd_hitrate{s}(chan) = std(hitrate); - mean_hitrate_shuffled{s}(chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{s,chan} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - chan = chan +1; - - end %end of behavior > 1 clause. - end% End of channel for loop - - clear labels_id - -end% End of session for loop - - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_partnerBehav.mat', "mean_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") - -load('SVM_results_partnerBehav.mat') - -%Plot decoding accuracy for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Decoding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && onlysingle==1 - saveas(gcf,['SVM_results_accuracy_behav_NOsubsample_unique_allSessions_partner.png']) -elseif randomsample ==1 && onlysingle==1 - saveas(gcf,['SVM_results_accuracy_behav_subsample_unique_allSessions_partner.png']) -elseif randomsample ==0 && onlysingle==0 - saveas(gcf,['SVM_results_accuracy_behav_NOsubsample_NOTunique_allSessions_partner.png']) -elseif randomsample ==1 && onlysingle==0 - saveas(gcf,['SVM_results_accuracy_behav_subsample_NOTunique_allSessions_partner.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot decoding accuracy relative to chance for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 4]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 4]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && unq_behav==1 - saveas(gcf,['SVM_results_behav_NOsubsample_unique_allSessions_partner.png']) -elseif randomsample ==1 && unq_behav==1 - saveas(gcf,['SVM_results_behav_subsample_unique_allSessions_partner.png']) -elseif randomsample ==0 && unq_behav==0 - saveas(gcf,['SVM_results_behav_NOsubsample_NOTunique_allSessions_partner.png']) -elseif randomsample ==1 && unq_behav==0 - saveas(gcf,['SVM_results_behav_subsample_NOTunique_allSessions_partner.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data = cell2mat(mean_hitrate'); -data_shuffle = cell2mat(mean_hitrate_shuffled'); -bp = bar([mean(data(:,:)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); - -sp1 = scatter(ones(size(data,1))*0.78,data(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*1.22,data(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data,1))*1.78,data_shuffle(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1))*2,data_shuffle(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*2.22,data_shuffle(:,3), 'filled','y'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0 1]) -xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) -ax = gca; -ax.FontSize = 16; -saveas(gcf,['SVM_results_allSessions_allUnits_PARTNER.png']) - diff --git a/Linear decoding/old_code/log_SVM_partner_batch.m b/Linear decoding/old_code/log_SVM_partner_batch.m deleted file mode 100644 index f235076..0000000 --- a/Linear decoding/old_code/log_SVM_partner_batch.m +++ /dev/null @@ -1,335 +0,0 @@ -%% Log_SVM_partner_batch -% Run a linear decoder on a the neural activity during rest for the partner behaviors -% Batch version to run across sessions -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =1; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -min_occurrences = 30;%Minimum number of occurrence per behavior -alone_block=0; %1: during alone block; 0:during paired blocks; anything else: all blocks. -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. - - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - block_labels = cell2mat({labels{:,11}}'); %Extract block info - alone_block_id = find(strcmp(block_times{:,"Behavior"},"Alone.block")); - - - %Only consider paired blocks - if alone_block==1 - behavior_labels_subject_init = behavior_labels_subject_init(block_labels== alone_block_id); - behavior_labels_partner_init = behavior_labels_partner_init(block_labels== alone_block_id); - Spike_count_raster = Spike_count_raster(block_labels== alone_block_id,:); - block_labels=block_labels(block_labels== alone_block_id,:); - elseif alone_block==0 - behavior_labels_subject_init = behavior_labels_subject_init(block_labels~= alone_block_id); - behavior_labels_partner_init = behavior_labels_partner_init(block_labels~= alone_block_id); - Spike_count_raster = Spike_count_raster(block_labels~= alone_block_id,:); - block_labels=block_labels(block_labels~= alone_block_id,:); - end - - - %% Select behaviors to decode - - %Only consider epochs where subject is resting - idx = find(ismember(behavior_labels_subject_init,length(behav_categ)) &... - ~ismember(behavior_labels_partner_init,length(behav_categ))); %find the indices where the subject is resting and the partner is not - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_partner = behavior_labels_partner_init(idx); - behavior_labels_subject = behavior_labels_subject_init(idx); - behav = unique(behavior_labels_partner); - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels_partner); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences=30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - %OR manually select behaviors: - behav = [5,18]; - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - if length(behav)>1 - - - %Select the correct indices. - idx_beh = find(ismember(behavior_labels_partner,behav)); - subject_behav_after_selection = behavior_labels_subject(idx_beh); - partner_behav_after_selection = behavior_labels_partner(idx_beh); - block_after_selection = block_labels(idx_beh); - - Spike_count_raster_final = Spike_count_raster_final(idx_beh,:); - behavior_labels_final = partner_behav_after_selection; - - overlap_partner_subject_after_selection = length(find(partner_behav_after_selection == subject_behav_after_selection))/length(idx); - fprintf('Percent overlap between subject and partner labels AFTER selecting behaviors: %s \n', num2str(overlap_partner_subject_after_selection)) - alone_block_obs = length(find(block_after_selection==3))/length(idx); - fprintf('Percent observations in "alone" block AFTER selecting behaviors: %s \n', num2str(alone_block_obs)) - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - if null - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels_final,behav_categ, temp_resolution,0); - Labels = sim_behav; - else - Labels = behavior_labels_final; - end - - %Balance number of trials per class - Input_matrix = Spike_count_raster_final; - - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - % channel = char(channel_flag); - % disp('****************************************************************************') - % disp(['ID type ' num2str(partner) ', channels: ' channel '. DONE']) - % disp('****************************************************************************') - % - mean_hitrate{s}(chan) = mean(hitrate) - sd_hitrate{s}(chan) = std(hitrate); - mean_hitrate_shuffled{s}(chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{s,chan} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - chan = chan +1; - - end %end of behavior > 1 clause. - end% End of channel for loop - - clear labels_id - -end% End of session for loop - - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_partnerBehav.mat', "mean_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") - -load('SVM_results_partnerBehav.mat') - -%Plot decoding accuracy for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Decoding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && onlysingle==1 - saveas(gcf,['SVM_results_accuracy_behav_NOsubsample_unique_allSessions_partner.png']) -elseif randomsample ==1 && onlysingle==1 - saveas(gcf,['SVM_results_accuracy_behav_subsample_unique_allSessions_partner.png']) -elseif randomsample ==0 && onlysingle==0 - saveas(gcf,['SVM_results_accuracy_behav_NOsubsample_NOTunique_allSessions_partner.png']) -elseif randomsample ==1 && onlysingle==0 - saveas(gcf,['SVM_results_accuracy_behav_subsample_NOTunique_allSessions_partner.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot decoding accuracy relative to chance for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 4]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 4]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && unq_behav==1 - saveas(gcf,['SVM_results_behav_NOsubsample_unique_allSessions_partner.png']) -elseif randomsample ==1 && unq_behav==1 - saveas(gcf,['SVM_results_behav_subsample_unique_allSessions_partner.png']) -elseif randomsample ==0 && unq_behav==0 - saveas(gcf,['SVM_results_behav_NOsubsample_NOTunique_allSessions_partner.png']) -elseif randomsample ==1 && unq_behav==0 - saveas(gcf,['SVM_results_behav_subsample_NOTunique_allSessions_partner.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data = cell2mat(mean_hitrate'); -data_shuffle = cell2mat(mean_hitrate_shuffled'); -bp = bar([mean(data(:,:)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); - -sp1 = scatter(ones(size(data,1))*0.78,data(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*1.22,data(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data,1))*1.78,data_shuffle(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1))*2,data_shuffle(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*2.22,data_shuffle(:,3), 'filled','y'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0 1]) -xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) -ax = gca; -ax.FontSize = 16; -saveas(gcf,['SVM_results_allSessions_allUnits_PARTNER.png']) - diff --git a/Linear decoding/old_code/log_SVM_partner_batch_old.m b/Linear decoding/old_code/log_SVM_partner_batch_old.m deleted file mode 100644 index d333fe7..0000000 --- a/Linear decoding/old_code/log_SVM_partner_batch_old.m +++ /dev/null @@ -1,375 +0,0 @@ -%% Log_SVM_partner_batch -% Run a linear decoder on a the neural activity for the partner behaviors -% Batch version to run across sessions -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =1; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 50;%Number of SVM iterations -min_occurrences = 30;%Minimum number of occurrence per behavior -alone_block=1; %1: during alone block; 0:during paired blocks; anything else: all blocks. -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 3;%set the smoothing window size (sigma) -null=1;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. - - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - block_labels = cell2mat({labels{:,11}}'); %Extract block info - alone_block_id = find(strcmp(block_times{:,"Behavior"},"Alone.block")); - - %SANITY CHECK: Compute overlap between partner behavior and subject behavior - perc_overlap_allbehav = length(find(behavior_labels_subject_init == behavior_labels_partner_init))/length(behavior_labels_subject_init); - overlap = tabulate(behavior_labels_subject_init(behavior_labels_subject_init == behavior_labels_partner_init)); - subject_behav = tabulate(behavior_labels_subject_init); partner_behav = tabulate(behavior_labels_partner_init); - perc_overlap_perbehav = [behav_categ', overlap(:,2), overlap(:,2)./partner_behav(:,2)*100]; - %Important notes: - %1. There are some discrepancies when comparing the partner and - %subject labels. Most notably in proximity, but also RR, HIP, HIS, - %SP, SS which should be the same. - %2. I just realized that depending on what other behavior is - %co-occurring, the label can change. This may explain discrepancies - %in RR and proximity - %3. it is often the case that self-groom events co-occur for Amos and Lovelace. I expect - %this to be the case for foraging in Hooke-pair. - - - %Only consider paired blocks - if alone_block==1 - behavior_labels_subject_init = behavior_labels_subject_init(block_labels== alone_block_id); - behavior_labels_partner_init = behavior_labels_partner_init(block_labels== alone_block_id); - Spike_count_raster = Spike_count_raster(block_labels== alone_block_id,:); - block_labels=block_labels(block_labels== alone_block_id,:); - elseif alone_block==0 - behavior_labels_subject_init = behavior_labels_subject_init(block_labels~= alone_block_id); - behavior_labels_partner_init = behavior_labels_partner_init(block_labels~= alone_block_id); - Spike_count_raster = Spike_count_raster(block_labels~= alone_block_id,:); - block_labels=block_labels(block_labels~= alone_block_id,:); - end - - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels_partner_init); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - % Then select non-reciprocal behaviors - behav = setdiff(behav, reciprocal_set); - - % OR select behaviors manually - %behav = [4,5,17,23,25];%manually select behaviors of interest - %Select behaviors manually to ensure that the same - %behaviors are considered for the partner and subject comparisons. - %This list could change from session to session.. I'll have to - %think of a way to automatize this. - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels_partner_init,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - - %check the amount of labels that differ in the partner vs. subject - %labels after selecting the behaviors of interest. - subject_behav_after_selection = behavior_labels_subject_init(idx); - partner_behav_after_selection = behavior_labels_partner_init(idx); - block_after_selection = block_labels(idx); - - overlap_partner_subject_after_selection = length(find(partner_behav_after_selection == subject_behav_after_selection))/length(idx); - fprintf('Percent overlap between subject and partner labels AFTER selecting behaviors: %s \n', num2str(overlap_partner_subject_after_selection)) - alone_block_obs = length(find(block_after_selection==3))/length(idx); - fprintf('Percent observations in "alone" block AFTER selecting behaviors: %s \n', num2str(alone_block_obs)) - - %Only consider windows where the behaviors of subject and - %partner do not overlap - diff_idx = find(partner_behav_after_selection ~= subject_behav_after_selection); %find the indices where subject and partner behavior do not overlap - Spike_count_raster_final = Spike_count_raster_final(diff_idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = partner_behav_after_selection(diff_idx,:);%Same as above but in behavior labels - behavior_labels_final_subject = subject_behav_after_selection(diff_idx,:); - block_after_selection_final = block_after_selection(diff_idx); - behav = unique(behavior_labels_final); - - tabulate(removecats(categorical(behavior_labels_final))); - tabulate(block_after_selection_final) - crosstab(removecats(categorical(behavior_labels_final)), block_after_selection_final) - %Note: not all behaviors are equally happening across blocks. It's - %possible that what we decode is actually block ID and not the - %behavior itself... - - %Display which behaviors will be decoded - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - % %Only consider behaviors during the alone block - % block_after_selection_overlap_out = block_after_selection(diff_idx); - % alone_idx = find(block_after_selection_overlap_out==3); - % Spike_count_raster_final = Spike_count_raster_final(alone_idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - % behavior_labels_final = behavior_labels_final(alone_idx,:);%Same as above but in behavior labels - % tabulate(behavior_labels_final); - - if null - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels_final,behav_categ, temp_resolution); - behavior_labels_final = sim_behav; - end - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - % clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... - % Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... - % channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %Balance number of trials per class - Labels = behavior_labels_final; - Input_matrix = Spike_count_raster_final; - - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - -% channel = char(channel_flag); -% disp('****************************************************************************') -% disp(['ID type ' num2str(partner) ', channels: ' channel '. DONE']) -% disp('****************************************************************************') -% - mean_hitrate{s}(chan) = mean(hitrate) - sd_hitrate{s}(chan) = std(hitrate); - mean_hitrate_shuffled{s}(chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{s,chan} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - chan = chan +1; - end% End of channel for loop - - clear labels_id - -end% End of session for loop - - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_partnerBehav.mat', "mean_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") - -load('SVM_results_partnerBehav.mat') - -%Plot decoding accuracy for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Decoding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && onlysingle==1 - saveas(gcf,['SVM_results_accuracy_behav_NOsubsample_unique_allSessions_partner.png']) -elseif randomsample ==1 && onlysingle==1 - saveas(gcf,['SVM_results_accuracy_behav_subsample_unique_allSessions_partner.png']) -elseif randomsample ==0 && onlysingle==0 - saveas(gcf,['SVM_results_accuracy_behav_NOsubsample_NOTunique_allSessions_partner.png']) -elseif randomsample ==1 && onlysingle==0 - saveas(gcf,['SVM_results_accuracy_behav_subsample_NOTunique_allSessions_partner.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot decoding accuracy relative to chance for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 4]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 4]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && unq_behav==1 - saveas(gcf,['SVM_results_behav_NOsubsample_unique_allSessions_partner.png']) -elseif randomsample ==1 && unq_behav==1 - saveas(gcf,['SVM_results_behav_subsample_unique_allSessions_partner.png']) -elseif randomsample ==0 && unq_behav==0 - saveas(gcf,['SVM_results_behav_NOsubsample_NOTunique_allSessions_partner.png']) -elseif randomsample ==1 && unq_behav==0 - saveas(gcf,['SVM_results_behav_subsample_NOTunique_allSessions_partner.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data = cell2mat(mean_hitrate'); -data_shuffle = cell2mat(mean_hitrate_shuffled'); -bp = bar([mean(data(:,:)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); - -sp1 = scatter(ones(size(data,1))*0.78,data(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*1.22,data(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data,1))*1.78,data_shuffle(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1))*2,data_shuffle(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*2.22,data_shuffle(:,3), 'filled','y'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0 1]) -xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) -ax = gca; -ax.FontSize = 16; -saveas(gcf,['SVM_results_allSessions_allUnits_PARTNER.png']) - diff --git a/Linear decoding/old_code/log_SVM_partner_subject_function.asv b/Linear decoding/old_code/log_SVM_partner_subject_function.asv deleted file mode 100644 index 98822f8..0000000 --- a/Linear decoding/old_code/log_SVM_partner_subject_function.asv +++ /dev/null @@ -1,48 +0,0 @@ -function [hitrate, C] = log_SVM_partner_subject_function(trainlbls, traindata, testlbls, testdata, ldaflag, Normalize) -% This script was written by SdT to run SVM on the w8a dataset using the LIBSVM library -% -% Input_matrix = double(Input_matrix); %Augment data to double precision -% Labels = double(Labels); - -if ~exist('ldaflag', 'var') %Default option is not to use lda. lda helps with lazy neurons, but overall leads to lower accuracy - ldaflag = 0; -end - -if ~exist('Normalize', 'var') %Default option is not to normalize - Normalize = 0; -end - -if Normalize == 1 - %Z-score data to normalize it. Each neuron FR is normalized to its mean across trials FR - Input_matrix = zscore(Input_matrix); -end - -total_number_trials = size(testdata,1); %Find the total number of trials for which a prediction needs to be made. -Predicted_labels = zeros(size(testlbls)); %Initate matrix of predicted labels for each trial -cumError = 0; %Count the number of prediction errors - -if ldaflag == 1 %If you wish to perform LDA - ldaopts.FisherFace = 1; - [fdaout, Weights] = fda(trainlbls, ldaopts, traindata); - testdata = testdata*Weights; % Project testdata to same LDA subspace as traindata: - traindata = fdaout; -end - -% Train/test SVM model: -% !!!!!!!!!! CHANGE SVM KERNEL HERE !!!!!!!!!!!!!!! -trainlbls=trainlbls(randperm(length(trainlbls))); -model = svmtrain(trainlbls, traindata, '-t, 0, -q'); %train the model using a linear kernel (-t: 0) or a RBF kernel (-t: 2) and default parameters -[svmlbls] = svmpredict(testlbls, testdata, model, '-q'); %get predicted labels given model - -nErr= length(find( (testlbls - svmlbls) ~= 0 )); %Find misclassifications -cumError = cumError + nErr; %Count number of errors -Predicted_labels = svmlbls; %Keep track of the predicted labels - - -%Compute performance of decoder -hitrate = 1 - (cumError/total_number_trials); -%Obtain confusion matrix of predicted against real values. -C = confusionmat(testlbls, Predicted_labels); -C = C ./ repmat(sum(C,2), 1, size(C,2)); - -end diff --git a/Linear decoding/old_code/log_SVM_partner_subject_function.m b/Linear decoding/old_code/log_SVM_partner_subject_function.m deleted file mode 100644 index f2992c5..0000000 --- a/Linear decoding/old_code/log_SVM_partner_subject_function.m +++ /dev/null @@ -1,47 +0,0 @@ -function [hitrate, C] = log_SVM_partner_subject_function(trainlbls, traindata, testlbls, testdata, ldaflag, Normalize) -% This script was written by SdT to run SVM on the w8a dataset using the LIBSVM library -% -% Input_matrix = double(Input_matrix); %Augment data to double precision -% Labels = double(Labels); - -if ~exist('ldaflag', 'var') %Default option is not to use lda. lda helps with lazy neurons, but overall leads to lower accuracy - ldaflag = 0; -end - -if ~exist('Normalize', 'var') %Default option is not to normalize - Normalize = 0; -end - -if Normalize == 1 - %Z-score data to normalize it. Each neuron FR is normalized to its mean across trials FR - Input_matrix = zscore(Input_matrix); -end - -total_number_trials = size(testdata,1); %Find the total number of trials for which a prediction needs to be made. -Predicted_labels = zeros(size(testlbls)); %Initate matrix of predicted labels for each trial -cumError = 0; %Count the number of prediction errors - -if ldaflag == 1 %If you wish to perform LDA - ldaopts.FisherFace = 1; - [fdaout, Weights] = fda(trainlbls, ldaopts, traindata); - testdata = testdata*Weights; % Project testdata to same LDA subspace as traindata: - traindata = fdaout; -end - -% Train/test SVM model: -% !!!!!!!!!! CHANGE SVM KERNEL HERE !!!!!!!!!!!!!!! -model = svmtrain(trainlbls, traindata, '-t, 0, -q'); %train the model using a linear kernel (-t: 0) or a RBF kernel (-t: 2) and default parameters -[svmlbls] = svmpredict(testlbls, testdata, model, '-q'); %get predicted labels given model - -nErr= length(find( (testlbls - svmlbls) ~= 0 )); %Find misclassifications -cumError = cumError + nErr; %Count number of errors -Predicted_labels = svmlbls; %Keep track of the predicted labels - - -%Compute performance of decoder -hitrate = 1 - (cumError/total_number_trials); -%Obtain confusion matrix of predicted against real values. -C = confusionmat(testlbls, Predicted_labels); -C = C ./ repmat(sum(C,2), 1, size(C,2)); - -end diff --git a/Linear decoding/old_code/log_SVM_partner_vs_subject_batch.m b/Linear decoding/old_code/log_SVM_partner_vs_subject_batch.m deleted file mode 100644 index f8c92a0..0000000 --- a/Linear decoding/old_code/log_SVM_partner_vs_subject_batch.m +++ /dev/null @@ -1,272 +0,0 @@ -%% Log_SVM_partner_batch -% Run a linear decoder on a the neural activity for the partner behaviors -% Batch version to run across sessions -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =1; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 50;%Number of SVM iterations -min_occurrences = 30;%Minimum number of occurrence per behavior - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - block_labels = cell2mat({labels{:,11}}'); %Extract block info - - - - %% Select behaviors to decode - - % Select behaviors manually - behav = [4,5,18,24]; %focus on self-groom for now - - %Only keep the behaviors of interest - idx_sub = find(ismember(behavior_labels_subject_init,behav)); - idx_part = find(ismember(behavior_labels_partner_init,behav)); %find the indices of the behaviors considered - idx_subject = setdiff(idx_sub, idx_part); - idx_partner = setdiff(idx_part, idx_sub); - idx = [idx_subject; idx_partner]; - - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = [behavior_labels_subject_init(idx_subject); behavior_labels_partner_init(idx_partner)];%Same as above but in behavior labels - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - % clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... - % Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... - % channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %Balance number of trials per class - Labels = behavior_labels_final; - Input_matrix = Spike_count_raster_final; - - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = 30;%min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - -% channel = char(channel_flag); -% disp('****************************************************************************') -% disp(['ID type ' num2str(partner) ', channels: ' channel '. DONE']) -% disp('****************************************************************************') -% - mean_hitrate{s}(chan) = mean(hitrate) - sd_hitrate{s}(chan) = std(hitrate); - mean_hitrate_shuffled{s}(chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{s,chan} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - chan = chan +1; - end% End of channel for loop - - clear labels_id - -end% End of session for loop - - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_partnerBehav.mat', "mean_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") - - -%Plot decoding accuracy for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Decoding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && onlysingle==1 - saveas(gcf,['SVM_results_accuracy_behav_NOsubsample_unique_allSessions_partner.png']) -elseif randomsample ==1 && onlysingle==1 - saveas(gcf,['SVM_results_accuracy_behav_subsample_unique_allSessions_partner.png']) -elseif randomsample ==0 && onlysingle==0 - saveas(gcf,['SVM_results_accuracy_behav_NOsubsample_NOTunique_allSessions_partner.png']) -elseif randomsample ==1 && onlysingle==0 - saveas(gcf,['SVM_results_accuracy_behav_subsample_NOTunique_allSessions_partner.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot decoding accuracy relative to chance for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 4]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 4]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for partner current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && unq_behav==1 - saveas(gcf,['SVM_results_behav_NOsubsample_unique_allSessions_partner.png']) -elseif randomsample ==1 && unq_behav==1 - saveas(gcf,['SVM_results_behav_subsample_unique_allSessions_partner.png']) -elseif randomsample ==0 && unq_behav==0 - saveas(gcf,['SVM_results_behav_NOsubsample_NOTunique_allSessions_partner.png']) -elseif randomsample ==1 && unq_behav==0 - saveas(gcf,['SVM_results_behav_subsample_NOTunique_allSessions_partner.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data = cell2mat(mean_hitrate'); -data_shuffle = cell2mat(mean_hitrate_shuffled'); -bp = bar([mean(data(:,:)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); - -sp1 = scatter(ones(size(data,1))*0.75,data(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*1.25,data(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data,1))*1.75,data_shuffle(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1))*2,data_shuffle(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*2.25,data_shuffle(:,3), 'filled','y'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0.1 0.6]) -xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) -ax = gca; -ax.FontSize = 16; -saveas(gcf,['SVM_results_allSessions_allUnits_PARTNER.png']) - diff --git a/Linear decoding/old_code/log_SVM_social_vs_nonsocial_batch.m b/Linear decoding/old_code/log_SVM_social_vs_nonsocial_batch.m deleted file mode 100644 index c209f53..0000000 --- a/Linear decoding/old_code/log_SVM_social_vs_nonsocial_batch.m +++ /dev/null @@ -1,268 +0,0 @@ -%% Log_SVM_subject_batch -% Run a linear decoder on the neural activity for the subject's behavior -% (only including behaviors with a minimum # occurrence) -% Batch version of script -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 50;%Number of SVM iterations - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - - %Only consider activity during one of the paired blocks - block_labels = cell2mat({labels{:,11}}'); - pair2block = find(strcmp(block_times{:,1}, 'Pair2.block')); - idx_block = find(block_labels==pair2block); - Spike_count_raster = Spike_count_raster(idx_block,:); - behavior_labels = behavior_labels(idx_block); - - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 20; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - %behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - - % OR select behaviors manually - % behav = [4:10,16,23];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(removecats(categorical(behavior_labels_final))); - - - social_labels_final = ones(size(behavior_labels_final)); - social_labels_final(ismember(behavior_labels_final, social_set)) = 2; - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - - %subsample to match number of neurons across brain areas - Labels = social_labels_final; - if randomsample==1 - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = 100;%min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %End of SVM for loop - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate{s}(temp, chan) = mean(hitrate) - sd_hitrate{s}(temp, chan) = std(hitrate); - mean_hitrate_shuffled{s}(temp, chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); %Get confusion matrix - confusion_mat_avg=round(mean(C_concat,3)*100); %Average over SVM iterations - rowNames{s} = {labels_id{:,2}}; colNames{s} = {labels_id{:,2}}; %Get behavior names - C_table{s, temp, chan} = array2table(confusion_mat_avg,'RowNames',rowNames{s},'VariableNames',colNames{s}); - - chan = chan +1; - clear labels_id - - end %end of channel for loop - - cd(savePath) - - - %% Plot result - -% % %Plotting results decoding accuracy for all behaviors at 1sec and lower resolution -% % figure; hold on; set(gcf,'Position',[150 250 700 500]) -% % cmap = hsv(size(mean_hitrate,1)); -% % for b = 1:size(mean_hitrate,1) -% % y = mean_hitrate{s}(b,:); -% % std_dev = sd_hitrate{s}(b,:); -% % errorbar(y,std_dev,'s','MarkerSize',10,... -% % 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) -% % %plot(x,y,'Color','k') -% % end -% % chance_level = 1/length(behav); -% % yline(chance_level,'--','Chance level', 'FontSize',16) -% % xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -% % xticklabels({'','vlPFC','TEO','all',''}) -% % ax = gca; -% % ax.FontSize = 14; -% % ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -% % title('Decoding accuracy for subject current behavioral states','FontSize', 14) - - close all - -end %End of session for loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_SocialVsNonsocial.mat', "mean_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") -load('SVM_results_SocialVsNonsocial.mat') - -%Plot decoding accuracy for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for subject current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Decoding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for subject current behavioral states, Monkey H','FontSize', 14) - -saveas(gcf,['Decoding social vs nonsocial allSessions.png']) - -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data = cell2mat(mean_hitrate'); -data_shuffle = cell2mat(mean_hitrate_shuffled'); -bp = bar([mean(data(:,:)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); - -sp1 = scatter(ones(size(data,1))*0.75,data(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*1.25,data(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data,1))*1.75,data_shuffle(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1))*2,data_shuffle(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*2.25,data_shuffle(:,3), 'filled','y'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0.4 0.9]) -xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) -ax = gca; -ax.FontSize = 16; -saveas(gcf,['SVM_results_allSessions_SocialVsNonsocial.png']) \ No newline at end of file diff --git a/Linear decoding/old_code/log_SVM_subject.m b/Linear decoding/old_code/log_SVM_subject.m deleted file mode 100644 index 909d203..0000000 --- a/Linear decoding/old_code/log_SVM_subject.m +++ /dev/null @@ -1,221 +0,0 @@ -%% Log_SVM -%% Run a linear decoder on a the neural activity over all or a subset of behaviors -% This script allows to deocde a combination of factors: -% 1. Subject behavior -% 2. Partner's behavior -% 3. Predict future behavior (with specified lag time in terms of windows) -% 4. Detect past behaviors (with specified lag time in terms of windows) -% 5. Behavior shifts (irrespective of what the shift is and the TYPE of shift) -% 6. Window size used fo decoding behaviors can be manipulated -% 7. Brain area - - -%Set path -is_mac = 1; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -clearvars -except savePath filePath is_mac - -%Set parameters -temp = 1; temp_resolution = 1; -chan = 1; channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -onlysingle=0; %If only consider epochs where only 1 behavior happens -with_NC =1; -isolatedOnly=0; - -for channel_flag = ["vlPFC", "TEO", "all"] - - - %Load data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - %filePath is the experimental data path - %Temp_resolution is the temporal resolution at which we would like to - %analyze the data - %Channel_flag specifies with channels to include: only TEO array, only - %vlPFC array or all channels - %is_mac is whether a mac or a pc is being used - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - co_occurrence = cell2mat({labels{:,5}}'); - - if onlysingle==1%Select epochs where only one behavior happens at any given time (i.e. no co-occurrence). - idx_single = find(co_occurrence==1 | co_occurrence==2 | co_occurrence==3); %(no co-occurrence, or with RR or with proximity) - Spike_count_raster = Spike_count_raster(idx_single,:); - behavior_labels = behavior_labels(idx_single,:); - end - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - - % OR select behaviors manually - %behav = [4:10,16,23];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(behavior_labels_final); - - - %% Run SVM over multiple iterations - num_iter = 100; - - disp('Start running SVM...') - for iter = 1:num_iter - -% clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... -% Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... -% channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %subsample to match number of neurons across brain areas - Labels = behavior_labels_final; - if randomsample==1 && channel_flag~="all" - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = 30;%min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - %[hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate(temp, chan) = mean(hitrate) - sd_hitrate(temp, chan) = std(hitrate); - %mean_hitrate_shuffled(temp, chan) = mean(hitrate_shuffled) - %sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{temp, chan} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - chan = chan +1; - - clearvars -except is_mac savePath mean_hitrate sd_hitrate mean_hitrate_shuffled C_table temp_resolution channel_flag filePath chan temp behavs_eval behav randomsample onlysingle isolatedOnly with_NC -end -% temp = temp+1; - -cd(savePath) - -%Plot confusion matrices -vlpfc = table2array(C_table{:,1}); TEO = table2array(C_table{1,2}); all_units = table2array(C_table{1,3}); -D = vlpfc-TEO; -CustomAxisLabels = string(C_table{1,1}.Properties.VariableNames) -figure; pfc = heatmap(vlpfc,'Colormap', jet); pfc.XDisplayLabels = CustomAxisLabels; pfc.YDisplayLabels = CustomAxisLabels; title(pfc,'vlpfc confusion matrix'); caxis([0, 100]); -figure; teo = heatmap(TEO,'Colormap', jet); teo.XDisplayLabels = CustomAxisLabels; teo.YDisplayLabels = CustomAxisLabels; title(teo,'TEO confusion matrix'); caxis([0, 100]); -figure; all = heatmap(all_units,'Colormap', jet); all.XDisplayLabels = CustomAxisLabels; all.YDisplayLabels = CustomAxisLabels; title(all,'All units confusion matrix'); caxis([0, 100]); -figure; h = heatmap(D,'Colormap', jet); h.XDisplayLabels = CustomAxisLabels; h.YDisplayLabels = CustomAxisLabels; title(h,'vlpfc-TEO confusion matrix');caxis([-50, 50]); - -saveas(pfc,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_unique_confmatPFC.png']) -saveas(teo,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_unique_confmatTEO.png']) -saveas(all,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_unique_confmatALL.png']) -saveas(h,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_unique_confmatPFC-TEO.png']) - -close all - -rowNames = ["1sec"]; colNames = ["vlPFC","TEO","all"]; -result_hitrate = array2table(mean_hitrate,'RowNames',rowNames,'VariableNames',colNames) -result_hitrate_shuffled = array2table(mean_hitrate_shuffled,'RowNames',rowNames,'VariableNames',colNames) -result_sdhitrate = array2table(sd_hitrate,'RowNames',rowNames,'VariableNames',colNames) - -% save([savePath '\SVM_results_social_context.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -% writetable(result_hitrate,[savePath '\SVM_results_social_context.csv'],'WriteRowNames',true,'WriteVariableNames',true); -save([savePath '\SVM_results_' num2str(length(behav)) 'behav_NOsubsample_unique.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -writetable(result_hitrate,[savePath '\SVM_results_' num2str(length(behav)) 'behav_NOsubsample_unique.csv'],'WriteRowNames',true,'WriteVariableNames',true); - -%Plotting results decoding accuracy for all behaviors at 1sec and lower resolution -figure; hold on; set(gcf,'Position',[150 250 700 500]) -cmap = hsv(size(mean_hitrate,1)); -for b = 1:size(mean_hitrate,1) - y = mean_hitrate(b,:); - std_dev = sd_hitrate(b,:); - errorbar(y,std_dev,'s','MarkerSize',10,... - 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) - %plot(x,y,'Color','k') -end -%leg = legend([rowNames, 'Chance']); -%title(leg,'Window size') -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for subject current behavioral states (unique behaviors, NO subsample neurons)','FontSize', 14) - -cd(savePath) -saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_unique.png']) -close all - diff --git a/Linear decoding/old_code/log_SVM_subject_batch_old.m b/Linear decoding/old_code/log_SVM_subject_batch_old.m deleted file mode 100644 index 8de943b..0000000 --- a/Linear decoding/old_code/log_SVM_subject_batch_old.m +++ /dev/null @@ -1,417 +0,0 @@ -%% Log_SVM_subject_batch -% Run a linear decoder on the neural activity for the subject's behavior -% (only including behaviors with a minimum # occurrence) -% Batch version of script -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. -agg_precedence =0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=2; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - - %Low-pass filter data - %Spike_count_raster = lowpass(Spike_rasters',0.05,1); - - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Threat to partner"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Threat to subject"); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - co_occurrence = cell2mat({labels{:,5}}'); - - if null - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels,behav_categ, temp_resolution); - behavior_labels = sim_behav; - end - - if unq_behav==1%Select epochs where only one behavior happens at any given time (i.e. no co-occurrence). - idx_single = find(co_occurrence<4); %(no co-occurrence, or with RR or with proximity) - Spike_count_raster = Spike_count_raster(idx_single,:); - behavior_labels = behavior_labels(idx_single,:); - end - - if simplify - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - end - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 20; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - % Remove behaviors that are ill-defined - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Vocalization')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - - % OR select behaviors manually - %behav = [5,18];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(removecats(categorical(behavior_labels_final))); - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - - %subsample to match number of neurons across brain areas - Labels = behavior_labels_final; - if randomsample==1 - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %End of SVM for loop - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate{s}(chan) = mean(hitrate) - sd_hitrate{s}(chan) = std(hitrate); - mean_hitrate_shuffled{s}(chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); %Get confusion matrix - confusion_mat_avg{s, chan}=round(mean(C_concat,3)*100); %Average over SVM iterations - rowNames{s} = {labels_id{:,2}}; colNames{s} = {labels_id{:,2}}; %Get behavior names - C_table{s, chan} = array2table(confusion_mat_avg{s, chan},'RowNames',rowNames{s},'VariableNames',colNames{s}); - - chan = chan +1; - clear labels_id - - end %end of channel for loop - - cd(savePath) - - %% Plot confusion matrices - -% % figure; set(gcf,'Position',[150 250 1500 500]); -% % subplot(1,2,1) -% % hp=heatmap(confusion_mat_avg{s, 1},'Colormap', jet); caxis([0 100]); -% % hp.XDisplayLabels = rowNames{s}; hp.YDisplayLabels = colNames{s}; title(['vlPFC']) -% % ylabel('Real'); xlabel('Predicted') -% % ax = gca; -% % ax.FontSize = 14; -% % -% % subplot(1,2,2) -% % hp=heatmap(confusion_mat_avg{s, 2},'Colormap', jet); caxis([0 100]); -% % hp.XDisplayLabels = rowNames{s}; hp.YDisplayLabels = colNames{s}; title(['TEO']) -% % ylabel('Real'); xlabel('Predicted') -% % ax = gca; -% % ax.FontSize = 14; -% % saveas(gcf,['ConfusionMatrix.pdf']) - - -% vlpfc = table2array(C_table{s,1,1}); TEO = table2array(C_table{s,1,2}); all_units = table2array(C_table{s,1,3}); -% D{s} = vlpfc-TEO; -% CustomAxisLabels = string(C_table{s,1,1}.Properties.VariableNames); figure; set(gcf,'Position',[150 250 1500 800]); -% subplot(2,2,1); pfc = heatmap(vlpfc,'Colormap', hot); pfc.XDisplayLabels = CustomAxisLabels; pfc.YDisplayLabels = CustomAxisLabels; title(pfc,'vlpfc confusion matrix'); caxis([0, 100]); -% subplot(2,2,2); teo = heatmap(TEO,'Colormap', hot); teo.XDisplayLabels = CustomAxisLabels; teo.YDisplayLabels = CustomAxisLabels; title(teo,'TEO confusion matrix'); caxis([0, 100]); -% subplot(2,2,3); all = heatmap(all_units,'Colormap', hot); all.XDisplayLabels = CustomAxisLabels; all.YDisplayLabels = CustomAxisLabels; title(all,'All units confusion matrix'); caxis([0, 100]); -% subplot(2,2,4); h = heatmap(D{s},'Colormap', cool); h.XDisplayLabels = CustomAxisLabels; h.YDisplayLabels = CustomAxisLabels; title(h,'vlpfc-TEO confusion matrix');caxis([-50, 50]); - %close all - - %% Plot session result - -% rowNames = ["1sec"]; colNames = ["vlPFC","TEO","all"]; -% result_hitrate = array2table(mean_hitrate{s},'RowNames',rowNames,'VariableNames',colNames); -% result_hitrate_shuffled = array2table(mean_hitrate_shuffled{s},'RowNames',rowNames,'VariableNames',colNames); -% result_sdhitrate = array2table(sd_hitrate{s},'RowNames',rowNames,'VariableNames',colNames); - - % save([savePath '\SVM_results_' num2str(length(behav)) 'behav_NOsubsample_unique.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') - % writetable(result_hitrate,[savePath '\SVM_results_' num2str(length(behav)) 'behav_NOsubsample_unique.csv'],'WriteRowNames',true,'WriteVariableNames',true); - -% % % %Plotting results decoding accuracy for all behaviors at 1sec and lower resolution -% % % figure; hold on; set(gcf,'Position',[150 250 700 500]) -% % % cmap = hsv(size(mean_hitrate,1)); -% % % for b = 1:size(mean_hitrate,1) -% % % y = mean_hitrate{s}(b,:); -% % % std_dev = sd_hitrate{s}(b,:); -% % % errorbar(y,std_dev,'s','MarkerSize',10,... -% % % 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) -% % % %plot(x,y,'Color','k') -% % % end -% % % chance_level = 1/length(behav); -% % % yline(chance_level,'--','Chance level', 'FontSize',16) -% % % xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -% % % xticklabels({'','vlPFC','TEO','all',''}) -% % % ax = gca; -% % % ax.FontSize = 14; -% % % ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -% % % title('Decoding accuracy for subject current behavioral states','FontSize', 14) - - -end %End of session for loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_subjectBehav_newBehavPreprocess.mat', "mean_hitrate","sd_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") -%load('SVM_results_subjectBehav.mat') - -%Plot decoding accuracy for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end - -for s = a_sessions - y = mean_hitrate_shuffled{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for subject current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -for s = a_sessions - y = mean_hitrate_shuffled{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Decoding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for subject current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && unq_behav==1 - saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_unique_allSessions.png']) -elseif randomsample ==1 && unq_behav==1 - saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_subsample_unique_allSessions.png']) -elseif randomsample ==0 && unq_behav==0 - saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_NOTunique_allSessions.png']) -elseif randomsample ==1 && unq_behav==0 - saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_subsample_NOTunique_allSessions.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot decoding accuracy relative to chance for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 10]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for subject current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 10]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for subject current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && unq_behav==1 - saveas(gcf,['SVM_results_behav_NOsubsample_unique_allSessions.png']) -elseif randomsample ==1 && unq_behav==1 - saveas(gcf,['SVM_results_behav_subsample_unique_allSessions.png']) -elseif randomsample ==0 && unq_behav==0 - saveas(gcf,['SVM_results_behav_NOsubsample_NOTunique_allSessions.png']) -elseif randomsample ==1 && unq_behav==0 - saveas(gcf,['SVM_results_behav_subsample_NOTunique_allSessions.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data = cell2mat(mean_hitrate'); -data_shuffle = cell2mat(mean_hitrate_shuffled'); -bp = bar([mean(data(:,:)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); - -sp1 = scatter(ones(size(data,1))*0.77,data(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*1.22,data(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data,1))*1.77,data_shuffle(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1))*2,data_shuffle(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*2.22,data_shuffle(:,3), 'filled','y'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0 1]) -xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) -ax = gca; -ax.FontSize = 16; - -saveas(gcf,['SVM_results_subjectBehav.pdf']) - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% % % % % %Confusion matrices -% % % % % vlpfc = table2array(C_table{s,1,1}); TEO = table2array(C_table{s,1,2}); all_units = table2array(C_table{s,1,3}); -% % % % % D{s} = vlpfc-TEO; -% % % % % CustomAxisLabels = string(C_table{s,1,1}.Properties.VariableNames); figure; set(gcf,'Position',[150 250 1500 800]); -% % % % % subplot(2,2,1); pfc = heatmap(vlpfc,'Colormap', hot); pfc.XDisplayLabels = CustomAxisLabels; pfc.YDisplayLabels = CustomAxisLabels; title(pfc,'vlpfc confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,2); teo = heatmap(TEO,'Colormap', hot); teo.XDisplayLabels = CustomAxisLabels; teo.YDisplayLabels = CustomAxisLabels; title(teo,'TEO confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,3); all = heatmap(all_units,'Colormap', hot); all.XDisplayLabels = CustomAxisLabels; all.YDisplayLabels = CustomAxisLabels; title(all,'All units confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,4); h = heatmap(D,'Colormap', cool); h.XDisplayLabels = CustomAxisLabels; h.YDisplayLabels = CustomAxisLabels; title(h,'vlpfc-TEO confusion matrix');caxis([-50, 50]); diff --git a/Linear decoding/old_code/log_SVM_subject_increasingUnits_batch.asv b/Linear decoding/old_code/log_SVM_subject_increasingUnits_batch.asv deleted file mode 100644 index 07e18ce..0000000 --- a/Linear decoding/old_code/log_SVM_subject_increasingUnits_batch.asv +++ /dev/null @@ -1,309 +0,0 @@ -%% Log_SVM_subject_batch -% Run a linear decoder on the neural activity for the subject's behavior -% (only including behaviors with a minimum # occurrence) -% Batch version of script -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 500;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. -agg_precedence =0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -unit_num_range = [1, 5, 20, 50, 80, 120, 150, 200]; - -for s =session_range(2:end) %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - %for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - co_occurrence = cell2mat({labels{:,5}}'); - - if unq_behav==1%Select epochs where only one behavior happens at any given time (i.e. no co-occurrence). - idx_single = find(co_occurrence==1 | co_occurrence==2 | co_occurrence==3); %(no co-occurrence, or with RR or with proximity) - Spike_count_raster = Spike_count_raster(idx_single,:); - behavior_labels = behavior_labels(idx_single,:); - end - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize'))); - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - - % OR select behaviors manually - % behav = [4:10,16,23];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(removecats(categorical(behavior_labels_final))); - - - %% Run SVM over increasing numbers of units, over multiple iterations - u = 1; - for unit_num = unit_num_range - - disp('Start running SVM...') - for iter = 1:num_iter - - - %Select unit to run SVM - Labels = behavior_labels_final; - Input_matrix = Spike_count_raster_final(:,randsample(size(Spike_count_raster_final,2), unit_num)); - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = 30;%min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); -% [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); -% hitrate_ratio(iter) = hitrate(iter)/hitrate_shuffled(iter); - - if mod(iter,50)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %End of SVM for loop - - disp(['SVM run for ' num2str(unit_num) ' unit(s)']) - - - % channel = char(channel_flag); - % disp('****************************************************************************') - % disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - % disp('****************************************************************************') - - mean_hitrate{s,chan}(u) = mean(hitrate); - sd_hitrate{s,chan}(u) = std(hitrate); -% mean_hitrate_shuffled{s,chan}(u) = mean(hitrate_shuffled); -% sd_hitrate_shuffled = std(hitrate_shuffled); -% mean_hitrate_ratio{s,chan}(u) = mean(hitrate_ratio); -% sd_hitrate_ratio{s,chan}(u) = std(hitrate_ratio); - - %chan = chan +1; - clear labels_id - - u=u+1; - end %End of unit number loop - chan = chan+1; - - %end %end of channel for loop - - cd(savePath) - - %Plotting results decoding accuracy for all behaviors at 1sec and lower resolution - figure; hold on; set(gcf,'Position',[150 250 700 500]) - y1 = mean_hitrate{s,1}; - %y2 = mean_hitrate{s,2}; - std_dev1 = sd_hitrate{s,1}; - %std_dev2 = sd_hitrate{s,2}; - errorbar(y1,std_dev1,'s','MarkerSize',10) - %errorbar(y2,std_dev2,'s','MarkerSize',10) - chance_level = 1/behav; - yline(chance_level,'--','Chance level', 'FontSize',16) - xticks([1:length(unit_num_range)]); xlim([0.8 length(unit_num_range)+0.2]); ylim([0 1]) - xticklabels(unit_num_range) - ax = gca; - ax.FontSize = 14; - ylabel('Decoding accuracy','FontSize', 18); xlabel('#units included','FontSize', 18) - title('Decoding accuracy for subject current behavioral states with increasing #units','FontSize', 14) - saveas(gcf, 'Decoding_subject_increasing_units.png') - - close all - -end %End of session for loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('IncreasingUnits.mat') - -%Plot decoding accuracy for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate(s,:); - %std_dev = sd_hitrate(s,:); - scatter(1:length(y),y,40,'MarkerFaceColor',cmap(s,:), 'MarkerEdgeColor','k','MarkerFaceAlpha',0.4) -end -chance_level = 1; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1:length(unit_num_range) length(unit_num_range)+.2]); -xlim([0.8 length(unit_num_range)+.2]); ylim([0 12]) -xticklabels({'',unit_num_range,''}) -ax = gca; -ax.FontSize = 14; -ylabel('mean Hitrate/Shuffled hitrate','FontSize', 18); xlabel('Number of units','FontSize', 18) -title('Hitrate ratio with increasing #units, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -for s = h_sessions - y = mean_hitrate_ratio(s,:); - %std_dev = sd_hitrate_ratio(s,:); - scatter(1:length(y),y,40,'MarkerFaceColor',cmap(s,:), 'MarkerEdgeColor','k','MarkerFaceAlpha',0.4) -end -chance_level = 1; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1:length(unit_num_range) length(unit_num_range)+.2]); -xlim([0.8 length(unit_num_range)+.2]); ylim([0 12]) -xticklabels({'',unit_num_range,''}) -ax = gca; -ax.FontSize = 14; -ylabel('mean Hitrate/Shuffled hitrate','FontSize', 18); xlabel('Number of units','FontSize', 18) -title('Hitrate ratio with increasing #units, Monkey H','FontSize', 14) - -saveas(gcf,'Decoding_subject_increasing_units_allSessions.png') -save('IncreasingUnits.mat','mean_hitrate','mean_hitrate_shuffled','mean_hitrate_shuffled','sd_hitrate','sd_hitrate_ratio','sd_hitrate_shuffled') -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot decoding accuracy relative to chance for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 10]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for subject current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 10]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for subject current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && unq_behav==1 - saveas(gcf,['SVM_results_behav_NOsubsample_unique_allSessions.png']) -elseif randomsample ==1 && unq_behav==1 - saveas(gcf,['SVM_results_behav_subsample_unique_allSessions.png']) -elseif randomsample ==0 && unq_behav==0 - saveas(gcf,['SVM_results_behav_NOsubsample_NOTunique_allSessions.png']) -elseif randomsample ==1 && unq_behav==0 - saveas(gcf,['SVM_results_behav_subsample_NOTunique_allSessions.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% % % % % %Confusion matrices -% % % % % vlpfc = table2array(C_table{s,1,1}); TEO = table2array(C_table{s,1,2}); all_units = table2array(C_table{s,1,3}); -% % % % % D{s} = vlpfc-TEO; -% % % % % CustomAxisLabels = string(C_table{s,1,1}.Properties.VariableNames); figure; set(gcf,'Position',[150 250 1500 800]); -% % % % % subplot(2,2,1); pfc = heatmap(vlpfc,'Colormap', hot); pfc.XDisplayLabels = CustomAxisLabels; pfc.YDisplayLabels = CustomAxisLabels; title(pfc,'vlpfc confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,2); teo = heatmap(TEO,'Colormap', hot); teo.XDisplayLabels = CustomAxisLabels; teo.YDisplayLabels = CustomAxisLabels; title(teo,'TEO confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,3); all = heatmap(all_units,'Colormap', hot); all.XDisplayLabels = CustomAxisLabels; all.YDisplayLabels = CustomAxisLabels; title(all,'All units confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,4); h = heatmap(D,'Colormap', cool); h.XDisplayLabels = CustomAxisLabels; h.YDisplayLabels = CustomAxisLabels; title(h,'vlpfc-TEO confusion matrix');caxis([-50, 50]); diff --git a/Linear decoding/old_code/log_SVM_subject_kernelTest_batch.m b/Linear decoding/old_code/log_SVM_subject_kernelTest_batch.m deleted file mode 100644 index b2ab3e3..0000000 --- a/Linear decoding/old_code/log_SVM_subject_kernelTest_batch.m +++ /dev/null @@ -1,371 +0,0 @@ -%% Log_SVM_subject_batch -% Run a linear decoder on the neural activity for the subject's behavior -% (only including behaviors with a minimum # occurrence) -% Batch version of script -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 50;%Number of SVM iterations - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=2; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - for kernel = 0:3 - - chan = 1; - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - co_occurrence = cell2mat({labels{:,5}}'); - - if unq_behav==1%Select epochs where only one behavior happens at any given time (i.e. no co-occurrence). - idx_single = find(co_occurrence==1 | co_occurrence==2 | co_occurrence==3); %(no co-occurrence, or with RR or with proximity) - Spike_count_raster = Spike_count_raster(idx_single,:); - behavior_labels = behavior_labels(idx_single,:); - end - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - - % OR select behaviors manually - % behav = [4,5,7,8,9,10,24];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(removecats(categorical(behavior_labels_final))); - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - - %subsample to match number of neurons across brain areas - Labels = behavior_labels_final; - if randomsample==1 - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = 30;%min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function_kernelTest(Input_matrix, Labels, 5, 0, 0, kernel); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function_kernelTest(Input_matrix, Labels_shuffled, 5, 0, 0, kernel); - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %End of SVM for loop - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate{s}(kernel+1, chan) = mean(hitrate) - sd_hitrate{s}(kernel+1, chan) = std(hitrate); - mean_hitrate_shuffled{s}(kernel+1, chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); %Get confusion matrix - confusion_mat_avg=round(mean(C_concat,3)*100); %Average over SVM iterations - rowNames{s} = {labels_id{:,2}}; colNames{s} = {labels_id{:,2}}; %Get behavior names - C_table{s, kernel+1, chan} = array2table(confusion_mat_avg,'RowNames',rowNames{s},'VariableNames',colNames{s}); - - chan = chan +1; - clear labels_id - - end %end of channel loop - end %end of kernel for loop - - cd(savePath) - - %% Plot confusion matrices - % % % % % vlpfc = table2array(C_table{s,1,1}); TEO = table2array(C_table{s,1,2}); all_units = table2array(C_table{s,1,3}); - % % % % % D{s} = vlpfc-TEO; - % % % % % CustomAxisLabels = string(C_table{s,1,1}.Properties.VariableNames); figure; set(gcf,'Position',[150 250 1500 800]); - % % % % % subplot(2,2,1); pfc = heatmap(vlpfc,'Colormap', hot); pfc.XDisplayLabels = CustomAxisLabels; pfc.YDisplayLabels = CustomAxisLabels; title(pfc,'vlpfc confusion matrix'); caxis([0, 100]); - % % % % % subplot(2,2,2); teo = heatmap(TEO,'Colormap', hot); teo.XDisplayLabels = CustomAxisLabels; teo.YDisplayLabels = CustomAxisLabels; title(teo,'TEO confusion matrix'); caxis([0, 100]); - % % % % % subplot(2,2,3); all = heatmap(all_units,'Colormap', hot); all.XDisplayLabels = CustomAxisLabels; all.YDisplayLabels = CustomAxisLabels; title(all,'All units confusion matrix'); caxis([0, 100]); - % % % % % subplot(2,2,4); h = heatmap(D{s},'Colormap', cool); h.XDisplayLabels = CustomAxisLabels; h.YDisplayLabels = CustomAxisLabels; title(h,'vlpfc-TEO confusion matrix');caxis([-50, 50]); - - % % % % % if randomsample ==0 && unq_behav==1 - % % % % % saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_unique_confmat.png']) - % % % % % elseif randomsample ==1 && unq_behav==1 - % % % % % saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_subsample_unique_confmat.png']) - % % % % % elseif randomsample ==0 && unq_behav==0 - % % % % % saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_NOTunique_confmat.png']) - % % % % % elseif randomsample ==1 && unq_behav==0 - % % % % % saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_subsample_NOTunique_confmat.png']) - % % % % % end - % % % % % - % % % % % close all - - %Plot result - - % rowNames = ["1sec"]; colNames = ["vlPFC","TEO","all"]; - % result_hitrate = array2table(mean_hitrate{s},'RowNames',rowNames,'VariableNames',colNames); - % result_hitrate_shuffled = array2table(mean_hitrate_shuffled{s},'RowNames',rowNames,'VariableNames',colNames); - % result_sdhitrate = array2table(sd_hitrate{s},'RowNames',rowNames,'VariableNames',colNames); - - % save([savePath '\SVM_results_' num2str(length(behav)) 'behav_NOsubsample_unique.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') - % writetable(result_hitrate,[savePath '\SVM_results_' num2str(length(behav)) 'behav_NOsubsample_unique.csv'],'WriteRowNames',true,'WriteVariableNames',true); - - % % % %Plotting results decoding accuracy for all behaviors at 1sec and lower resolution - % % % figure; hold on; set(gcf,'Position',[150 250 700 500]) - % % % cmap = hsv(size(mean_hitrate,1)); - % % % for b = 1:size(mean_hitrate,1) - % % % y = mean_hitrate{s}(b,:); - % % % std_dev = sd_hitrate{s}(b,:); - % % % errorbar(y,std_dev,'s','MarkerSize',10,... - % % % 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) - % % % %plot(x,y,'Color','k') - % % % end - % % % chance_level = 1/length(behav); - % % % yline(chance_level,'--','Chance level', 'FontSize',16) - % % % xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) - % % % xticklabels({'','vlPFC','TEO','all',''}) - % % % ax = gca; - % % % ax.FontSize = 14; - % % % ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) - % % % title('Decoding accuracy for subject current behavioral states','FontSize', 14) - - % % % cd(savePath) - % % % - % % % if randomsample ==0 && unq_behav==1 - % % % saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_unique.png']) - % % % elseif randomsample ==1 && unq_behav==1 - % % % saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_subsample_unique.png']) - % % % elseif randomsample ==0 && unq_behav==0 - % % % saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_NOTunique.png']) - % % % elseif randomsample ==1 && unq_behav==0 - % % % saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_subsample_NOTunique.png']) - % % % end - % % % close all - -end %End of session for loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -%save('SVM_results_subjectBehav.mat', "mean_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") -%load('SVM_results_subjectBehav.mat') - -%Plot decoding accuracy for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for subject current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}; - std_dev = sd_hitrate{s}; - errorbar(y,std_dev,'s','MarkerSize',5,'MarkerFaceColor',cmap(s,:)) -end -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Decoding accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for subject current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && unq_behav==1 - saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_unique_allSessions.png']) -elseif randomsample ==1 && unq_behav==1 - saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_subsample_unique_allSessions.png']) -elseif randomsample ==0 && unq_behav==0 - saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_NOsubsample_NOTunique_allSessions.png']) -elseif randomsample ==1 && unq_behav==0 - saveas(gcf,['SVM_results_' num2str(length(behavs_eval)) 'behav_subsample_NOTunique_allSessions.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot decoding accuracy relative to chance for all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 10]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for subject current behavioral states, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 10]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for subject current behavioral states, Monkey H','FontSize', 14) - -if randomsample ==0 && unq_behav==1 - saveas(gcf,['SVM_results_behav_NOsubsample_unique_allSessions.png']) -elseif randomsample ==1 && unq_behav==1 - saveas(gcf,['SVM_results_behav_subsample_unique_allSessions.png']) -elseif randomsample ==0 && unq_behav==0 - saveas(gcf,['SVM_results_behav_NOsubsample_NOTunique_allSessions.png']) -elseif randomsample ==1 && unq_behav==0 - saveas(gcf,['SVM_results_behav_subsample_NOTunique_allSessions.png']) -end -close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data = cell2mat(mean_hitrate'); -data_shuffle = cell2mat(mean_hitrate_shuffled'); -bp = bar([mean(data(:,:)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); - -sp1 = scatter(ones(size(data,1))*0.75,data(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1)),data(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*1.25,data(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data,1))*1.75,data_shuffle(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1))*2,data_shuffle(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*2.25,data_shuffle(:,3), 'filled','y'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0 0.8]) -xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) -ax = gca; -ax.FontSize = 16; - -saveas(gcf,['SVM_results_subjectBehav.png']) - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% % % % % %Confusion matrices -% % % % % vlpfc = table2array(C_table{s,1,1}); TEO = table2array(C_table{s,1,2}); all_units = table2array(C_table{s,1,3}); -% % % % % D{s} = vlpfc-TEO; -% % % % % CustomAxisLabels = string(C_table{s,1,1}.Properties.VariableNames); figure; set(gcf,'Position',[150 250 1500 800]); -% % % % % subplot(2,2,1); pfc = heatmap(vlpfc,'Colormap', hot); pfc.XDisplayLabels = CustomAxisLabels; pfc.YDisplayLabels = CustomAxisLabels; title(pfc,'vlpfc confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,2); teo = heatmap(TEO,'Colormap', hot); teo.XDisplayLabels = CustomAxisLabels; teo.YDisplayLabels = CustomAxisLabels; title(teo,'TEO confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,3); all = heatmap(all_units,'Colormap', hot); all.XDisplayLabels = CustomAxisLabels; all.YDisplayLabels = CustomAxisLabels; title(all,'All units confusion matrix'); caxis([0, 100]); -% % % % % subplot(2,2,4); h = heatmap(D,'Colormap', cool); h.XDisplayLabels = CustomAxisLabels; h.YDisplayLabels = CustomAxisLabels; title(h,'vlpfc-TEO confusion matrix');caxis([-50, 50]); diff --git a/Linear decoding/old_code/log_SVM_timelagged.m b/Linear decoding/old_code/log_SVM_timelagged.m deleted file mode 100644 index f888721..0000000 --- a/Linear decoding/old_code/log_SVM_timelagged.m +++ /dev/null @@ -1,294 +0,0 @@ -%% Log_SVM_timelagged -%% Run a linear decoder on a the neural activity over all or a subset of behaviors - - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:18]; -session_range_with_partner=[1:3,11:13]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -min_occurrences = 30; %Min number of occurrence for a particular behavior - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; - for channel_flag = ["vlPFC", "TEO", "all"] - - pred =1; predict =1; - for predict =[1,2] %Forward or backward lag. I.e. prediction of future behavior (predict =1) or impact of past behavior (predict=2)? - - lag=1; lag_length = 0; - for lag_length = [0,2,5,10,20] %in window size - %IMPORTANT NOTE: at 1 sec, thre are very few non-overlapping - %windows which makes the comparison difficult... - - % Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - disp('Data Loaded') - - Spike_count_raster_init = Spike_rasters'; - behavior_labels_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - - - %% Time shift behaviors - - %lag_length = 0; %Set length of time lag (in window size). Will include in a for loop eventually - - if predict == 1 %If predicting future behavior - behavior_labels = behavior_labels_init(1+lag_length:end); %Time shift behavior regressor - Spike_count_raster = Spike_count_raster_init(1:end-lag_length,:); %Truncate neural data - - %check the amount of labels that differ in the lag vs non-lagged versions - behavior_labels_nonlagged = behavior_labels_init(1:end-lag_length); - diff_behav_vectors = behavior_labels - behavior_labels_nonlagged; - perc_diff_before = round(length(find(diff_behav_vectors~=0))/length(diff_behav_vectors)*100); - fprintf('Percent difference between-time lagged and regular labels BEFORE selecting behaviors: %s \n', num2str(perc_diff_before)); - - elseif predict == 2 %If predicting past behavior - behavior_labels = behavior_labels_init(1:end-lag_length); %Time shift behavior regressor - Spike_count_raster = Spike_count_raster_init(1+lag_length:end,:); %Truncate neural data - - %check the amount of labels that differ in the lag vs non-lagged versions - behavior_labels_nonlagged = behavior_labels_init(1+lag_length:end,:); - diff_behav_vectors = behavior_labels - behavior_labels_nonlagged; - perc_diff_before = round(length(find(diff_behav_vectors~=0))/length(diff_behav_vectors)*100); - fprintf('Percent difference between-time lagged and regular labels BEFORE selecting behaviors: %s \n', num2str(perc_diff_before)); - end - - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding Rowdy room which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - % OR select behaviors manually - %behav = [4,6,7,8,17,23]; %manually select behaviors of interest - %Behaviors selecte on 1 window size difference, to make sure we - %have equivalent # categories across sessions - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(behavior_labels_final); - - %check the amount of labels that differ in the lag vs non-lagged - %versions after selecting the behaviors of interest. - if lag_length~=0 - behavior_labels_final_nonlagged = behavior_labels_nonlagged(idx,:); - idx_diff_behav = find((behavior_labels_final - behavior_labels_final_nonlagged)~=0); - perc_diff_after = round(length(idx_diff_behav)/length(diff_behav_vectors)*100); - fprintf('Percent difference between-time lagged and regular labels AFTER selecting behaviors: %s \n', num2str(perc_diff_after)); - - %Only consider windows where the behaviors of the lagged and - %non-lagged versions do not overlap - Spike_count_raster_final = Spike_count_raster_final(idx_diff_behav,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels_final(idx_diff_behav,:);%Same as above but in behavior labels - behav_freq_table = tabulate(behavior_labels_final); - - %Again, only consider behaviors we have more than X - %occurrences of - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1); - idx = find(ismember(behavior_labels_final,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster_final(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels_final(idx,:); - tabulate(behavior_labels_final); - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - - end - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - % clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... - % Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... - % channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %Balance number of trials per class - Labels = behavior_labels_final; - Input_matrix = Spike_count_raster_final; - - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min_occurrences; %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(lag_length) 's window lag, channels: ' channel '. Prediction:' num2str(predict) '. DONE']) - disp(['Mean hitrate: ' num2str(round(mean(hitrate)*100,2)) '%, suffled:' num2str(round(mean(hitrate_shuffled)*100,2)) '%']) - disp('****************************************************************************') - pause(1) - - mean_hitrate{s}(chan, lag, pred) = mean(hitrate); - sd_hitrate{s}(chan, lag, pred) = std(hitrate); - mean_hitrate_shuffled{s}(chan, lag, pred) = mean(hitrate_shuffled); - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - rowNames{s} = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{s,chan,lag,pred} = array2table(confusion_mat_avg,'RowNames',rowNames{s},'VariableNames',colNames); - - lag = lag+1; - - clear labels_id - end %end of lag length loop - - pred = pred + 1; - end %End of past or future events to be decoded - - chan = chan +1; - end %End of channel loop - % temp = temp+1; - -end %End of session loop - -% mean_hitrate = permute( mean_hitrate , [1,3,2] ); sd_hitrate = permute( sd_hitrate , [1,3,2] ); -% mean_hitrate_shuffled = permute( mean_hitrate_shuffled , [1,3,2] ); - -colNames = ["0sec","2sec","5sec","10sec","20sec"]; rowNames = ["vlPFC","TEO","all"]; -result_hitrate_future = array2table(mean_hitrate(:,:,1),'RowNames',rowNames,'VariableNames',colNames) -result_hitrate_future_shuffled = array2table(mean_hitrate_shuffled(:,:,1),'RowNames',rowNames,'VariableNames',colNames) -result_sdhitrate_future = array2table(sd_hitrate(:,:,1),'RowNames',rowNames,'VariableNames',colNames); -result_hitrate_past = array2table(mean_hitrate(:,:,2),'RowNames',rowNames,'VariableNames',colNames) -result_sdhitrate_past = array2table(sd_hitrate(:,:,2),'RowNames',rowNames,'VariableNames',colNames); - -% save([savePath '\SVM_results_social_context.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -% writetable(result_hitrate,[savePath '\SVM_results_social_context.csv'],'WriteRowNames',true,'WriteVariableNames',true); -save([savePath '\SVM_timelagged_results_' num2str(length(behav)) 'behav.mat'], 'mean_hitrate', 'mean_hitrate_shuffled','sd_hitrate', 'C_table', 'behavs_eval') -writetable(result_hitrate_future,[savePath '\SVM_results_' num2str(length(behav)) 'future_behav.csv'],'WriteRowNames',true,'WriteVariableNames',true); -writetable(result_hitrate_past,[savePath '\SVM_results_' num2str(length(behav)) 'past_behav.csv'],'WriteRowNames',true,'WriteVariableNames',true); - -%Plotting results for multiple behaviors at 1sec and lower resolution -figure; hold on; set(gcf,'Position',[150 250 1000 500]) -cmap = hsv(size(mean_hitrate,2)); -for b = 1:size(mean_hitrate,1) - y = mean_hitrate(b,:,1); - std_dev = sd_hitrate(b,:,1); - errorbar(y,std_dev,'s','MarkerSize',10,... - 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) - %plot(x,y,'Color','k') -end -leg = legend([rowNames, 'Chance']); -title(leg,'Brain area') -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 4 5 5.2]); xlim([0.8 5.2]); ylim([0 1]) -xticklabels({'','0sec','2sec','5sec','10sec','20sec',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Time lag length (#windows)','FontSize', 18) -title('Decoding accuracy for future behaviors','FontSize', 20) -cd(savePath) -saveas(gcf,['SVM_results_FutureBehav.png']) -close all - -%Plotting for all channels across many time windows - including long one. -figure; hold on; set(gcf,'Position',[150 250 1000 500]) -cmap = hsv(size(mean_hitrate,2)); -for b = 1:size(mean_hitrate,1) - y = mean_hitrate(b,:,2); - std_dev = sd_hitrate(b,:,2); - errorbar(y,std_dev,'s','MarkerSize',10,... - 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) - %plot(x,y,'Color','k') -end -leg = legend([rowNames, 'Chance']); -title(leg,'Brain area') -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 4 5 5.2]); xlim([0.8 5.2]); ylim([0 1]) -xticklabels({'','0sec','2sec','5sec','10sec','20sec',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Time lag length (#windows)','FontSize', 18) -title('Decoding accuracy for past behaviors','FontSize', 20) -cd(savePath) -saveas(gcf,['SVM_results_PastBehav.png']) -close all - diff --git a/Linear decoding/old_code/log_SVM_timelagged_batch.m b/Linear decoding/old_code/log_SVM_timelagged_batch.m deleted file mode 100644 index 8aece25..0000000 --- a/Linear decoding/old_code/log_SVM_timelagged_batch.m +++ /dev/null @@ -1,293 +0,0 @@ -%% Log_SVM_timelagged -%% Run a linear decoder on a the neural activity over all or a subset of behaviors - - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:18]; -session_range_with_partner=[1:3,11:13]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -min_occurrences = 30; %Min number of occurrence for a particular behavior - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; - for channel_flag = ["vlPFC", "TEO", "all"] - - pred =1; predict =1; - for predict =[1,2] %Forward or backward lag. I.e. prediction of future behavior (predict =1) or impact of past behavior (predict=2)? - - lag=1; lag_length = 0; - for lag_length = [0,2,5,10,20] %in window size - %IMPORTANT NOTE: at 1 sec, thre are very few non-overlapping - %windows which makes the comparison difficult... - - % Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - disp('Data Loaded') - - Spike_count_raster_init = Spike_rasters'; - behavior_labels_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - - - %% Time shift behaviors - - %lag_length = 0; %Set length of time lag (in window size). Will include in a for loop eventually - - if predict == 1 %If predicting future behavior - behavior_labels = behavior_labels_init(1+lag_length:end); %Time shift behavior regressor - Spike_count_raster = Spike_count_raster_init(1:end-lag_length,:); %Truncate neural data - - %check the amount of labels that differ in the lag vs non-lagged versions - behavior_labels_nonlagged = behavior_labels_init(1:end-lag_length); - diff_behav_vectors = behavior_labels - behavior_labels_nonlagged; - perc_diff_before = round(length(find(diff_behav_vectors~=0))/length(diff_behav_vectors)*100); - fprintf('Percent difference between-time lagged and regular labels BEFORE selecting behaviors: %s \n', num2str(perc_diff_before)); - - elseif predict == 2 %If predicting past behavior - behavior_labels = behavior_labels_init(1:end-lag_length); %Time shift behavior regressor - Spike_count_raster = Spike_count_raster_init(1+lag_length:end,:); %Truncate neural data - - %check the amount of labels that differ in the lag vs non-lagged versions - behavior_labels_nonlagged = behavior_labels_init(1+lag_length:end,:); - diff_behav_vectors = behavior_labels - behavior_labels_nonlagged; - perc_diff_before = round(length(find(diff_behav_vectors~=0))/length(diff_behav_vectors)*100); - fprintf('Percent difference between-time lagged and regular labels BEFORE selecting behaviors: %s \n', num2str(perc_diff_before)); - end - - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding Rowdy room which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - % OR select behaviors manually - %behav = [4,6,7,8,17,23]; %manually select behaviors of interest - %Behaviors selecte on 1 window size difference, to make sure we - %have equivalent # categories across sessions - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(behavior_labels_final); - - %check the amount of labels that differ in the lag vs non-lagged - %versions after selecting the behaviors of interest. - if lag_length~=0 - behavior_labels_final_nonlagged = behavior_labels_nonlagged(idx,:); - idx_diff_behav = find((behavior_labels_final - behavior_labels_final_nonlagged)~=0); - perc_diff_after = round(length(idx_diff_behav)/length(diff_behav_vectors)*100); - fprintf('Percent difference between-time lagged and regular labels AFTER selecting behaviors: %s \n', num2str(perc_diff_after)); - - %Only consider windows where the behaviors of the lagged and - %non-lagged versions do not overlap - Spike_count_raster_final = Spike_count_raster_final(idx_diff_behav,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels_final(idx_diff_behav,:);%Same as above but in behavior labels - behav_freq_table = tabulate(behavior_labels_final); - - %Again, only consider behaviors we have more than X - %occurrences of - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1); - idx = find(ismember(behavior_labels_final,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster_final(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels_final(idx,:); - tabulate(behavior_labels_final); - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - - end - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - % clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... - % Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... - % channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %Balance number of trials per class - Labels = behavior_labels_final; - Input_matrix = Spike_count_raster_final; - - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min_occurrences; %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(lag_length) 's window lag, channels: ' channel '. Prediction:' num2str(predict) '. DONE']) - disp(['Mean hitrate: ' num2str(round(mean(hitrate)*100,2)) '%, suffled:' num2str(round(mean(hitrate_shuffled)*100,2)) '%']) - disp('****************************************************************************') - pause(1) - - mean_hitrate{s}(chan, lag, pred) = mean(hitrate); - sd_hitrate{s}(chan, lag, pred) = std(hitrate); - mean_hitrate_shuffled{s}(chan, lag, pred) = mean(hitrate_shuffled); - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - rowNames{s} = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{s,chan,lag,pred} = array2table(confusion_mat_avg,'RowNames',rowNames{s},'VariableNames',colNames); - - lag = lag+1; - - clear labels_id - end %end of lag length loop - - pred = pred + 1; - end %End of past or future events to be decoded - - chan = chan +1; - end %End of channel loop - % temp = temp+1; - -% mean_hitrate = permute( mean_hitrate , [1,3,2] ); sd_hitrate = permute( sd_hitrate , [1,3,2] ); -% mean_hitrate_shuffled = permute( mean_hitrate_shuffled , [1,3,2] ); - -% % colNames = ["0sec","2sec","5sec","10sec","20sec"]; rowNames = ["vlPFC","TEO","all"]; -% % result_hitrate_future = array2table(mean_hitrate(:,:,1),'RowNames',rowNames,'VariableNames',colNames) -% % result_hitrate_future_shuffled = array2table(mean_hitrate_shuffled(:,:,1),'RowNames',rowNames,'VariableNames',colNames) -% % result_sdhitrate_future = array2table(sd_hitrate(:,:,1),'RowNames',rowNames,'VariableNames',colNames); -% % result_hitrate_past = array2table(mean_hitrate(:,:,2),'RowNames',rowNames,'VariableNames',colNames) -% % result_sdhitrate_past = array2table(sd_hitrate(:,:,2),'RowNames',rowNames,'VariableNames',colNames); -% % -% % % save([savePath '\SVM_results_social_context.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -% % % writetable(result_hitrate,[savePath '\SVM_results_social_context.csv'],'WriteRowNames',true,'WriteVariableNames',true); -% % save([savePath '\SVM_timelagged_results_' num2str(length(behav)) 'behav.mat'], 'mean_hitrate', 'mean_hitrate_shuffled','sd_hitrate', 'C_table', 'behavs_eval') -% % writetable(result_hitrate_future,[savePath '\SVM_results_' num2str(length(behav)) 'future_behav.csv'],'WriteRowNames',true,'WriteVariableNames',true); -% % writetable(result_hitrate_past,[savePath '\SVM_results_' num2str(length(behav)) 'past_behav.csv'],'WriteRowNames',true,'WriteVariableNames',true); - -%Plotting results for decoding future behaviors -figure; hold on; set(gcf,'Position',[150 250 1000 500]) -cmap = hsv(size(mean_hitrate,2)); -for b = 1:size(mean_hitrate{s},1) %for all channels - y = mean_hitrate{s}(b,:,1)./mean_hitrate_shuffled{s}(b,:,1); - std_dev = sd_hitrate{s}(b,:,1); - errorbar(y,std_dev,'s','MarkerSize',10,... - 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) - %plot(x,y,'Color','k') -end -leg = legend(['vlPFC',"TEO","All"],'Location','best'); -title(leg,'Brain area') -chance_level = 1; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 4 5 5.2]); xlim([0.8 5.2]); ylim([0 10]) -xticklabels({'','0sec','2sec','5sec','10sec','20sec',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy relative to chance','FontSize', 18); xlabel('Time lag length (#windows)','FontSize', 18) -title('Decoding accuracy for future behaviors','FontSize', 20) -cd(savePath) -saveas(gcf,['SVM_results_FutureBehav.png']) -close all - -%Plotting results for decoding past behaviors -figure; hold on; set(gcf,'Position',[150 250 1000 500]) -cmap = hsv(size(mean_hitrate{s},2)); -for b = 1:size(mean_hitrate{s},1) - y = mean_hitrate{s}(b,:,2)./mean_hitrate_shuffled{s}(b,:,2); - std_dev = sd_hitrate{s}(b,:,2); - errorbar(y,std_dev,'s','MarkerSize',10,... - 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) - %plot(x,y,'Color','k') -end -leg = legend(['vlPFC',"TEO","All"],'Location','best'); -title(leg,'Brain area') -chance_level = 1; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 4 5 5.2]); xlim([0.8 5.2]); ylim([0 10]) -xticklabels({'','0sec','2sec','5sec','10sec','20sec',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Time lag length (#windows)','FontSize', 18) -title('Decoding accuracy for past behaviors','FontSize', 20) -cd(savePath) -saveas(gcf,['SVM_results_PastBehav.png']) -close all - -end %End of session loop \ No newline at end of file diff --git a/Linear decoding/old_code/log_SVM_trainBlock1_testBlock2_batch.m b/Linear decoding/old_code/log_SVM_trainBlock1_testBlock2_batch.m deleted file mode 100644 index 3d00f05..0000000 --- a/Linear decoding/old_code/log_SVM_trainBlock1_testBlock2_batch.m +++ /dev/null @@ -1,270 +0,0 @@ -%% Log_SVM_subject_batch -% Run a linear decoder on the neural activity for the subject's behavior -% (only including behaviors with a minimum # occurrence) -% Batch version of script -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =1; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1;%lump similar behavioral categories together to increase sample size. - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - - %Low-pass filter data - %Spike_count_raster = lowpass(Spike_rasters',0.05,1); - - behavior_labels = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - % behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Threat to partner"); - % behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Threat to subject"); - % behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - co_occurrence = cell2mat({labels{:,5}}'); - - blocks = cell2mat({labels{:,12}}'); - - if simplify - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - end - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 50; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - % Remove behaviors that are ill-defined - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Vocalization')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - - % OR select behaviors manually - %behav = [1,7,8,18];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_final = blocks(idx); - - traindata = Spike_count_raster_final(block_final==1,:); trainlbls = behavior_labels_final(block_final==1); - testdata = Spike_count_raster_final(block_final==2,:); testlbls = behavior_labels_final(block_final==2); - - block1_behav = tabulate(trainlbls); - block2_behav = tabulate(testlbls); - - if length(unique(testlbls))==length(behav) && length(unique(trainlbls))==length(behav) - if all(block1_behav(behav,2)>20) && all(block2_behav(behav,2)>20) - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - - %Balance number of trials for train data - uniqueLabels = unique(trainlbls); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = trainlbls; - for i=1:NumOfClasses - idx = trainlbls == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - trainlbls = labels_temp; - - num_trials = hist(trainlbls,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(trainlbls == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - traindata = traindata(chosen_trials, :); - trainlbls = trainlbls(chosen_trials, :); - trainlbls_shuffled = trainlbls(randperm(length(trainlbls))); - - %Balance number of trials for test data - uniqueLabels = unique(testlbls); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = testlbls; - for i=1:NumOfClasses - idx = testlbls == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - testlbls = labels_temp; - - num_trials = hist(testlbls,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(testlbls == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - testdata = testdata(chosen_trials, :); - testlbls = testlbls(chosen_trials, :); - testlbls_shuffled = testlbls(randperm(length(testlbls))); - - % Run svm - [hitrate_baseline(iter), C{iter}] = log_SVM_basic_function(traindata,trainlbls, 5, 0, 0); - [hitrate(iter), C{iter}] = log_SVM_ccgp_function(trainlbls,traindata,... - testlbls, testdata, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_ccgp_function(trainlbls,traindata,... - testlbls_shuffled, testdata, 0, 0); - - - if mod(iter,10)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %End of SVM for loop - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate_baseline{s}(temp, chan) = mean(hitrate_baseline) - mean_hitrate{s}(temp, chan) = mean(hitrate) - sd_hitrate{s}(temp, chan) = std(hitrate); - mean_hitrate_shuffled{s}(temp, chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); %Get confusion matrix - confusion_mat_avg=round(mean(C_concat,3)*100); %Average over SVM iterations - rowNames{s} = {labels_id{:,2}}; colNames{s} = {labels_id{:,2}}; %Get behavior names - C_table{s, temp, chan} = array2table(confusion_mat_avg,'RowNames',rowNames{s},'VariableNames',colNames{s}); - - chan = chan +1; - clear labels_id - - end - end - end %end of channel for loop - - cd(savePath) - -end %End of session for loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_crossBlock.mat', "mean_hitrate_baseline","mean_hitrate","sd_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data_within = cell2mat(mean_hitrate_baseline'); -data_cross = cell2mat(mean_hitrate'); -data_shuffle = cell2mat(mean_hitrate_shuffled'); -bp = bar([mean(data_within(:,:)); mean(data_cross(:,:)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); - -sp1 = scatter(ones(size(data_within,1))*0.78,data_within(:,1), 'filled','b'); -sp1 = scatter(ones(size(data_within,1)),data_within(:,2), 'filled','r'); -sp1 = scatter(ones(size(data_within,1))*1.22,data_within(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data_cross,1))*1.78,data_cross(:,1), 'filled','b'); -sp1 = scatter(ones(size(data_cross,1))*2,data_cross(:,2), 'filled','r'); -sp1 = scatter(ones(size(data_cross,1))*2.22,data_cross(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data_shuffle,1))*2.78,data_shuffle(:,1), 'filled','b'); -sp1 = scatter(ones(size(data_shuffle,1))*3,data_shuffle(:,2), 'filled','r'); -sp1 = scatter(ones(size(data_shuffle,1))*3.22,data_shuffle(:,3), 'filled','y'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0 1]) -xticks([1 2 3]); xticklabels({'Within-context','Cross-context', 'Shuffled'}); xlim([0.25 3.75]) -ax = gca; -ax.FontSize = 16; -saveas(gcf,['SVM_results_crossBlocks.pdf']) \ No newline at end of file diff --git a/Linear decoding/old_code/log_SVM_trainSubject_testPartner_batch.asv b/Linear decoding/old_code/log_SVM_trainSubject_testPartner_batch.asv deleted file mode 100644 index 64b7506..0000000 --- a/Linear decoding/old_code/log_SVM_trainSubject_testPartner_batch.asv +++ /dev/null @@ -1,266 +0,0 @@ -%% Log_SVM_trainSubject_testPartner_batch -% Based on the single unit responses, it seems that individual neurons -% respond in a similar direction when the subject is behaving compared to -% the partner. Is this also the case when we consider the neural population -% as a whole? Is neural population coding similar for partner vs. self? -% Train on partner behaving and test on subject behaving. -% Batch version to run across sessions -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =1; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -min_occurrences = 30;%Minimum number of occurrence per behavior -alone_block=0; %1: during alone block; 0:during paired blocks; anything else: all blocks. -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Load data - - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - session_length = size(Spike_rasters,2); % get session length - - %Extract behavior labels for subject and partner - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - - %Consider sitting in proximity to be rest - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - - %Lump approach and leave into travel - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); %Consider 'approach' to be 'Travel'. - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); %Consider 'approach' to be 'Travel'. - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="leave"))=find(behav_categ=="Travel"); %Consider 'leave' to be 'Travel'. - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); %Consider 'leave' to be 'Travel'. - - %Extract block info - block_labels = cell2mat({labels{:,11}}'); - alone_block_id = find(strcmp(block_times{:,"Behavior"},"Alone.block")); - - %Only consider paired blocks - if alone_block==1 - behavior_labels_subject_init = behavior_labels_subject_init(block_labels== alone_block_id); - behavior_labels_partner_init = behavior_labels_partner_init(block_labels== alone_block_id); - Spike_rasters = Spike_rasters(:,block_labels== alone_block_id); - block_labels=block_labels(block_labels== alone_block_id,:); - elseif alone_block==0 - behavior_labels_subject_init = behavior_labels_subject_init(block_labels~= alone_block_id); - behavior_labels_partner_init = behavior_labels_partner_init(block_labels~= alone_block_id); - Spike_rasters = Spike_rasters(:,block_labels~= alone_block_id); - block_labels=block_labels(block_labels~= alone_block_id,:); - end - - - %% Get indices for which subject and partner do not behav similarly & don't engage in reciprocal behaviors - % Select behaviors manually - behav = [5,18]; %focus on a set of behaviors which happen enough time in both the subject and partner and are not reciprocal - - % Get indices where either: - % 1. The subject is behaving and the partner is resting - idx_sub = find(ismember(behavior_labels_subject_init,behav)); - idx_part = find(ismember(behavior_labels_partner_init,behav) &... - ~ismember(behavior_labels_subject_init,behav));% &... - %block_labels~=alone_block_id); %find the indices of the behaviors considered - - Spike_rasters_partner = Spike_rasters(:,idx_part)';%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_partner = behavior_labels_partner_init(idx_part);%Same as above but in behavior labels - partner_behav = tabulate(behavior_labels_partner); - - Spike_rasters_subject = Spike_rasters(:,idx_sub)'; - behavior_labels_subject = behavior_labels_subject_init(idx_sub);block_labels = cell2mat({labels{:,11}}'); %Extract block info - subject_behav = tabulate(behavior_labels_subject); - - if length(unique(behavior_labels_partner))==length(behav) && length(unique(behavior_labels_subject))==length(behav) - if all(partner_behav(behav,2)>30) && all(subject_behav(behav,2)>30) - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - traindata = Spike_rasters_subject; trainlbls = behavior_labels_subject; - testdata = Spike_rasters_partner; testlbls = behavior_labels_partner; - - %Balance number of trials for train data - uniqueLabels = unique(trainlbls); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = trainlbls; - for i=1:NumOfClasses - idx = trainlbls == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - trainlbls = labels_temp; - - num_trials = hist(trainlbls,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(trainlbls == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - traindata = traindata(chosen_trials, :); - trainlbls = trainlbls(chosen_trials, :); - trainlbls_shuffled = trainlbls(randperm(length(trainlbls))); - - %Balance number of trials for test data - uniqueLabels = unique(testlbls); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = testlbls; - for i=1:NumOfClasses - idx = testlbls == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - testlbls = labels_temp; - - num_trials = hist(testlbls,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(testlbls == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - testdata = testdata(chosen_trials, :); - testlbls = testlbls(chosen_trials, :); - testlbls_shuffled = testlbls(randperm(length(testlbls))); - - % Run svm - [hitrate_subject(iter)] = log_SVM_basic_function(traindata, trainlbls, 5, 0, 0); - [hitrate_partner(iter)] = log_SVM_basic_function(testdata, testlbls, 5, 0, 0); - [hitrate_cross(iter), C{iter}] = log_SVM_ccgp_function(trainlbls,traindata,... - testlbls, testdata, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_ccgp_function(trainlbls,traindata,... - testlbls_shuffled, testdata, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - % channel = char(channel_flag); - % disp('****************************************************************************') - % disp(['ID type ' num2str(partner) ', channels: ' channel '. DONE']) - % disp('****************************************************************************') - % - - mean_hitrate_partner{s}(chan) = mean(hitrate_partner) - mean_hitrate_subject{s}(chan) = mean(hitrate_subject) - mean_hitrate_cross{s}(chan) = mean(hitrate_cross) - sd_hitrate{s}(chan) = std(hitrate_cross); - mean_hitrate_shuffled{s}(chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); -% rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; -% C_table{s,chan} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - chan = chan +1; - - end %end of #behavior classes clause. - end - end% End of channel for loop - -end% End of session for loop - - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_TrainSubjectTestPatrner.mat', "mean_hitrate_subject","mean_hitrate_partner","mean_hitrate_cross","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") - -load('SVM_results_TrainSubjectTestPatrner.mat') - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data_subject = cell2mat(mean_hitrate_subject'); -data_partner = cell2mat(mean_hitrate_partner'); -data_cross = cell2mat(mean_hitrate_cross'); -data_shuffle = cell2mat(mean_hitrate_shuffled'); -bp = bar([mean(data_subject(:,:)); mean(data_partner(:,:)); ... - mean(data_cross(:,:)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); - -sp1 = scatter(ones(size(data_subject,1))*0.78,data_subject(:,1), 'filled','b'); -sp1 = scatter(ones(size(data_subject,1)),data_subject(:,2), 'filled','r'); -sp1 = scatter(ones(size(data_subject,1))*1.22,data_subject(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data_partner,1))*1.78,data_partner(:,1), 'filled','b'); -sp1 = scatter(ones(size(data_partner,1))*2,data_partner(:,2), 'filled','r'); -sp1 = scatter(ones(size(data_partner,1))*2.22,data_partner(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data_cross,1))*2.78,data_cross(:,1), 'filled','b'); -sp1 = scatter(ones(size(data_cross,1))*3,data_cross(:,2), 'filled','r'); -sp1 = scatter(ones(size(data_cross,1))*3.22,data_cross(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data,1))*1.78,data_shuffle(:,1), 'filled','b'); -sp1 = scatter(ones(size(data,1))*2,data_shuffle(:,2), 'filled','r'); -sp1 = scatter(ones(size(data,1))*2.22,data_shuffle(:,3), 'filled','y'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0 1]) -xticks([1 2]); xticklabels({'Real', 'Shuffled'}); xlim([0.25 2.75]) -ax = gca; -ax.FontSize = 16; -saveas(gcf,['SVM_results_allSessions_trainSubjectTestPartner.png']) - diff --git a/Linear decoding/old_code/log_SVM_trainSubject_testPartner_batch.m b/Linear decoding/old_code/log_SVM_trainSubject_testPartner_batch.m deleted file mode 100644 index 2e37f99..0000000 --- a/Linear decoding/old_code/log_SVM_trainSubject_testPartner_batch.m +++ /dev/null @@ -1,267 +0,0 @@ -%% Log_SVM_trainSubject_testPartner_batch -% Based on the single unit responses, it seems that individual neurons -% respond in a similar direction when the subject is behaving compared to -% the partner. Is this also the case when we consider the neural population -% as a whole? Is neural population coding similar for partner vs. self? -% Train on partner behaving and test on subject behaving. -% Batch version to run across sessions -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =1; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -min_occurrences = 30;%Minimum number of occurrence per behavior -alone_block=0; %1: during alone block; 0:during paired blocks; anything else: all blocks. -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Load data - - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - session_length = size(Spike_rasters,2); % get session length - - %Extract behavior labels for subject and partner - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - - %Consider sitting in proximity to be rest - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - - %Lump approach and leave into travel - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); %Consider 'approach' to be 'Travel'. - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); %Consider 'approach' to be 'Travel'. - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="leave"))=find(behav_categ=="Travel"); %Consider 'leave' to be 'Travel'. - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); %Consider 'leave' to be 'Travel'. - - %Extract block info - block_labels = cell2mat({labels{:,11}}'); - alone_block_id = find(strcmp(block_times{:,"Behavior"},"Alone.block")); - - %Only consider paired blocks - if alone_block==1 - behavior_labels_subject_init = behavior_labels_subject_init(block_labels== alone_block_id); - behavior_labels_partner_init = behavior_labels_partner_init(block_labels== alone_block_id); - Spike_rasters = Spike_rasters(:,block_labels== alone_block_id); - block_labels=block_labels(block_labels== alone_block_id,:); - elseif alone_block==0 - behavior_labels_subject_init = behavior_labels_subject_init(block_labels~= alone_block_id); - behavior_labels_partner_init = behavior_labels_partner_init(block_labels~= alone_block_id); - Spike_rasters = Spike_rasters(:,block_labels~= alone_block_id); - block_labels=block_labels(block_labels~= alone_block_id,:); - end - - - %% Get indices for which subject and partner do not behav similarly & don't engage in reciprocal behaviors - % Select behaviors manually - behav = [5,18]; %focus on a set of behaviors which happen enough time in both the subject and partner and are not reciprocal - - % Get indices where either: - % 1. The subject is behaving and the partner is resting - idx_sub = find(ismember(behavior_labels_subject_init,behav)); - idx_part = find(ismember(behavior_labels_partner_init,behav) &... - ~ismember(behavior_labels_subject_init,behav)); - %ismember(behavior_labels_subject_init,length(behav_categ)));% &... - %block_labels~=alone_block_id); %find the indices of the behaviors considered - - Spike_rasters_partner = Spike_rasters(:,idx_part)';%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_partner = behavior_labels_partner_init(idx_part);%Same as above but in behavior labels - partner_behav = tabulate(behavior_labels_partner); - - Spike_rasters_subject = Spike_rasters(:,idx_sub)'; - behavior_labels_subject = behavior_labels_subject_init(idx_sub);block_labels = cell2mat({labels{:,11}}'); %Extract block info - subject_behav = tabulate(behavior_labels_subject); - - if length(unique(behavior_labels_partner))==length(behav) && length(unique(behavior_labels_subject))==length(behav) - if all(partner_behav(behav,2)>30) && all(subject_behav(behav,2)>30) - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - traindata = Spike_rasters_subject; trainlbls = behavior_labels_subject; - testdata = Spike_rasters_partner; testlbls = behavior_labels_partner; - - %Balance number of trials for train data - uniqueLabels = unique(trainlbls); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = trainlbls; - for i=1:NumOfClasses - idx = trainlbls == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - trainlbls = labels_temp; - - num_trials = hist(trainlbls,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(trainlbls == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - traindata = traindata(chosen_trials, :); - trainlbls = trainlbls(chosen_trials, :); - trainlbls_shuffled = trainlbls(randperm(length(trainlbls))); - - %Balance number of trials for test data - uniqueLabels = unique(testlbls); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = testlbls; - for i=1:NumOfClasses - idx = testlbls == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - testlbls = labels_temp; - - num_trials = hist(testlbls,numericLabels); %number of trials in each class - minNumTrials = min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(testlbls == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - testdata = testdata(chosen_trials, :); - testlbls = testlbls(chosen_trials, :); - testlbls_shuffled = testlbls(randperm(length(testlbls))); - - % Run svm - [hitrate_subject(iter)] = log_SVM_basic_function(traindata, trainlbls, 5, 0, 0); - [hitrate_partner(iter)] = log_SVM_basic_function(testdata, testlbls, 5, 0, 0); - [hitrate_cross(iter), C{iter}] = log_SVM_ccgp_function(trainlbls,traindata,... - testlbls, testdata, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_ccgp_function(trainlbls,traindata,... - testlbls_shuffled, testdata, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - % channel = char(channel_flag); - % disp('****************************************************************************') - % disp(['ID type ' num2str(partner) ', channels: ' channel '. DONE']) - % disp('****************************************************************************') - % - - mean_hitrate_partner{s}(chan) = mean(hitrate_partner) - mean_hitrate_subject{s}(chan) = mean(hitrate_subject) - mean_hitrate_cross{s}(chan) = mean(hitrate_cross) - sd_hitrate{s}(chan) = std(hitrate_cross); - mean_hitrate_shuffled{s}(chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - % rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - % C_table{s,chan} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - chan = chan +1; - - end %end of #behavior classes clause. - end - end% End of channel for loop - -end% End of session for loop - - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_TrainSubjectTestPatrner.mat', "mean_hitrate_subject","mean_hitrate_partner","mean_hitrate_cross","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") - -load('SVM_results_TrainSubjectTestPatrner.mat') - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - -figure; hold on -data_subject = cell2mat(mean_hitrate_subject'); -data_partner = cell2mat(mean_hitrate_partner'); -data_cross = cell2mat(mean_hitrate_cross'); -data_shuffle = cell2mat(mean_hitrate_shuffled'); -bp = bar([mean(data_subject(:,:)); mean(data_partner(:,:)); ... - mean(data_cross(:,:)); mean(data_shuffle(:,:))],'FaceAlpha',0.2); - -sp1 = scatter(ones(size(data_subject,1))*0.78,data_subject(:,1), 'filled','b'); -sp1 = scatter(ones(size(data_subject,1)),data_subject(:,2), 'filled','r'); -sp1 = scatter(ones(size(data_subject,1))*1.22,data_subject(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data_partner,1))*1.78,data_partner(:,1), 'filled','b'); -sp1 = scatter(ones(size(data_partner,1))*2,data_partner(:,2), 'filled','r'); -sp1 = scatter(ones(size(data_partner,1))*2.22,data_partner(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data_cross,1))*2.78,data_cross(:,1), 'filled','b'); -sp1 = scatter(ones(size(data_cross,1))*3,data_cross(:,2), 'filled','r'); -sp1 = scatter(ones(size(data_cross,1))*3.22,data_cross(:,3), 'filled','y'); - -sp1 = scatter(ones(size(data_shuffle,1))*3.78,data_shuffle(:,1), 'filled','b'); -sp1 = scatter(ones(size(data_shuffle,1))*4,data_shuffle(:,2), 'filled','r'); -sp1 = scatter(ones(size(data_shuffle,1))*4.22,data_shuffle(:,3), 'filled','y'); - -legend(bp,{'vlPFC','TEO','all'},'Location','best') - -ylabel('Decoding Accuracy'); ylim([0.4 1]) -xticks([1 2 3 4]); xticklabels({'Subject','Partner','Cross', 'Shuffled'}); xlim([0.25 4.75]) -ax = gca; -ax.FontSize = 16; -saveas(gcf,['SVM_results_trainSubjectTestPartner.pdf']) - diff --git a/Linear decoding/old_code/log_SVM_transitions.m b/Linear decoding/old_code/log_SVM_transitions.m deleted file mode 100644 index 01e14a4..0000000 --- a/Linear decoding/old_code/log_SVM_transitions.m +++ /dev/null @@ -1,208 +0,0 @@ -%% Log_SVM -%% Run a linear decoder to decode behavioral transitions from the neural activity - -%% Load data - -%Set path -is_mac = 0; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -clearvars -except savePath filePath is_mac - -%Set temporal resolution -temp = 1; temp_resolution = 1; -%for temp_resolution = [1/30, 1/20, 1/10, 1/5, 1/2, 1, 2, 5, 10] -%1 for second resolution, 10 for 100msec resolution, 100 for 10msec resolution, 1000 for msec resolution. etc. -%0.1 for 10sec resolution, 1/5 for 5sec resolution - -%Set channels: 'TEO', 'vlPFC' or 'all' -chan = 1; channel_flag = "all"; -for channel_flag = ["vlPFC", "TEO", "all"] - - trans = 1; transition =1; - for transition = [1,2] - - %Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac); - %filePath is the experimental data path - %Temp_resolution is the temporal resolution at which we would like to - %analyze the data - %Channel_flag specifies with channels to include: only TEO array, only - %vlPFC array or all channels - %is_mac is whether a mac or a pc is being used - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - block_labels = cell2mat({labels{:,10}}'); %Extract block info - - - %% Extract transitions - - if transition==1 - %Get behavior shift times irrespective of what the shift is - subject_behav_change_reg = ones(size(labels,1), 1); %initialize - subject_behav_change_reg(find(diff(behavior_labels_subject_init)~=0)) = 2; - behavior_labels = subject_behav_change_reg; - behav = [1,2]; - %IMPORTANT NOTE: we are NOT able to decode behavior shifts vs. - %non shifts - - elseif transition ==2 - %Get behavior shift with shift 'id' - x=behavior_labels_subject_init(1:end-1); y=behavior_labels_subject_init(2:end); - behavior_labels= [sscanf(sprintf('%d%d,',[x.';y.']),'%d,'); 0]; - shifts = x-y; - - shift_categ_table= tabulate(behavior_labels(shifts~=0)); - shift_categ_table = shift_categ_table(shift_categ_table(:,2)~=0,:); - min_occurrences = 15; - behav = shift_categ_table(shift_categ_table(:,2)>=min_occurrences,1); - - %IMPORTANT NOTE: We are able to decode above change what the - %behavioral shift is when there is one! (that just happened or - %is just about to happen) - end - - - %% Select behaviors to decode - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(behavior_labels_final); - - - %% Run SVM over multiple iterations - num_iter = 1000; - - disp('Start running SVM...') - for iter = 1:num_iter - -% clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... -% Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... -% channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %Balance number of trials per class - Labels = behavior_labels_final; - Input_matrix = Spike_count_raster_final; - - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); %labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = 15;%min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - channel = char(channel_flag); - disp('****************************************************************************') - disp(['Transition type' num2str(transition) ', channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate(chan, trans) = mean(hitrate) - sd_hitrate(chan, trans) = std(hitrate); - mean_hitrate_shuffled(chan, trans) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - %rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{chan, trans} = array2table(confusion_mat_avg); - %C_table{chan, trans} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - trans =trans+1; - - clearvars -except is_mac savePath mean_hitrate sd_hitrate mean_hitrate_shuffled C_table temp_resolution channel_flag filePath chan temp behavs_eval behav trans - end - chan = chan +1; -end -% temp = temp+1; -% end - -colNames = ["Non-specific", "Specific"]; rowNames = ["vlPFC","TEO","all"]; -result_hitrate = array2table(mean_hitrate,'RowNames',rowNames,'VariableNames',colNames) -result_sdhitrate = array2table(sd_hitrate,'RowNames',rowNames,'VariableNames',colNames); - -save([savePath '\SVM_results_' num2str(length(behav)) 'behav.mat'], 'mean_hitrate', 'sd_hitrate', 'C_table', 'result_hitrate', 'result_sdhitrate', 'behavs_eval') -writetable(result_hitrate,[savePath '\SVM_results_' num2str(length(behav)) 'behav.csv'],'WriteRowNames',true,'WriteVariableNames',true); - -%Plotting results for non-specific state transitions -figure; hold on; set(gcf,'Position',[150 250 1000 500]) - y = mean_hitrate(:,1); - std_dev = sd_hitrate(:,1); - errorbar(y,std_dev,'s','MarkerSize',10,... - 'MarkerEdgeColor','k','MarkerFaceColor','k') -% chance_level = 1/length(behav); -% yline(chance_level,'--','Chance level', 'FontSize',16, 'Color', cmap(2)) -chance_level = 1/2; -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain Area','FontSize', 18) -title('Decoding non-specific state transitions','FontSize', 20) -cd(savePath) -saveas(gcf,['Decoding non-specific state transitions.png']) -close all - - -%Plotting results for specific state transitions -figure; hold on; set(gcf,'Position',[150 250 1000 500]) -y = mean_hitrate(:,2); -std_dev = sd_hitrate(:,2); -errorbar(y,std_dev,'s','MarkerSize',10,... - 'MarkerEdgeColor','k','MarkerFaceColor','k') -chance_level = 1/length(behav); -yline(chance_level,'--','Chance level', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 1]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('Deconding accuracy','FontSize', 18); xlabel('Brain Area','FontSize', 18) -title('Decoding specific state transitions','FontSize', 20) -cd(savePath) -saveas(gcf,['Decoding specific state transitions.png']) -close all diff --git a/Linear decoding/old_code/log_SVM_transitions_batch.m b/Linear decoding/old_code/log_SVM_transitions_batch.m deleted file mode 100644 index c934166..0000000 --- a/Linear decoding/old_code/log_SVM_transitions_batch.m +++ /dev/null @@ -1,228 +0,0 @@ -%% Log_SVM_transitions_batch -% Run a linear decoder to decode behavioral transitions from the neural activity -% Batch version -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 50;%Number of SVM iterations -transition=1;%1: decode any behavior shift irrespective of what the shift is - %2: decode SPECIFIC behavioral shifts -time_around_shift = 4; %in sec - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - - %% Extract transitions - - if transition==1 - %Get behavior shift times irrespective of what the shift is - subject_behav_change_reg = ones(size(labels,1), 1); %initialize - shift_times = find(diff(behavior_labels_subject_init)~=0); - shift_idx=[]; - for st =1:length(shift_times) - shifts = shift_times(st)-time_around_shift : shift_times(st)+time_around_shift; - shift_idx = [shift_idx,shifts]; - end - min_occurrences = 50; - subject_behav_change_reg(shift_idx) = 2; - behavior_labels = subject_behav_change_reg; - behav = [1,2]; - %IMPORTANT NOTE: we are NOT able to decode behavior shifts vs. - %non shifts - - elseif transition ==2 - %Get behavior shift with shift 'id' - -% % Make proximity "rest" and remove all rest epochs -% behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). -% behavior_labels_subject = behavior_labels_subject_init(behavior_labels_subject_init~=length(behav_categ)); - - % - behavior_labels_subject = behavior_labels_subject_init; - x=behavior_labels_subject(1:end-1); y=behavior_labels_subject(2:end); - behavior_labels= [sscanf(sprintf('%d%d,',[x.';y.']),'%d,'); 0]; - shifts = x-y; - - shift_categ_table= tabulate(behavior_labels(shifts~=0)); - shift_categ_table = shift_categ_table(shift_categ_table(:,2)~=0,:); - min_occurrences = 5; - behav = shift_categ_table(shift_categ_table(:,2)>=min_occurrences,1); - - %IMPORTANT NOTE: We are able to decode above change what the - %behavioral shift is when there is one! (that just happened or - %is just about to happen) - end - - - %% Select behaviors to decode - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(behavior_labels_final); - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - -% clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... -% Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... -% channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %Balance number of trials per class - Labels = behavior_labels_final; - Input_matrix = Spike_count_raster_final; - - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); %labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min_occurrences;%min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - channel = char(channel_flag); - disp('****************************************************************************') - disp(['Transition type' num2str(transition) ', channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate{s}(chan) = mean(hitrate) - sd_hitrate{s}(chan) = std(hitrate); - mean_hitrate_shuffled{s}(chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - rowNames{s}=cell2mat(labels_id); - %rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{s,chan} = array2table(confusion_mat_avg); - %C_table{chan, trans} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - chan = chan +1; - end%end of channel loop - - clear labels_id -end %end of session loop - -%% Plot decoding accuracy relative to chance for all sessions, separated by monkey - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); - - -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 5]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for behavioral transitions, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 5]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for behavioral transitions, Monkey H','FontSize', 14) -if transition ==1 - saveas(gcf,['SVM_results_ANY_state_transitions_allSessions.png']) -else - saveas(gcf,['SVM_results_state_SPECIFIC_transitions_allSessions.png']) -end \ No newline at end of file diff --git a/Linear decoding/old_code/log_SVM_transitions_timelagged_batch.m b/Linear decoding/old_code/log_SVM_transitions_timelagged_batch.m deleted file mode 100644 index ad70628..0000000 --- a/Linear decoding/old_code/log_SVM_transitions_timelagged_batch.m +++ /dev/null @@ -1,228 +0,0 @@ -%% Log_SVM_transitions_batch -% Run a linear decoder to decode behavioral transitions BEFORE THEY OCCUR from the neural activity -% Batch version -- IN DEVELOPMENT -- -% March 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:17]; -session_range_with_partner=[1:3,11:13]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 100;%Number of SVM iterations -transition=2;%1: decode any behavior shift irrespective of what the shift is - %2: decode SPECIFIC behavioral shifts -time_around_shift = 4; %in sec - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:17]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - - %% Extract transitions - - if transition==1 - %Get behavior shift times irrespective of what the shift is - subject_behav_change_reg = ones(size(labels,1), 1); %initialize - shift_times = find(diff(behavior_labels_subject_init)~=0); - shift_idx=[]; - for st =1:length(shift_times) - shifts = shift_times(st)-time_around_shift : shift_times(st)+time_around_shift; - shift_idx = [shift_idx,shifts]; - end - min_occurrences = 50; - subject_behav_change_reg(shift_idx) = 2; - behavior_labels = subject_behav_change_reg; - behav = [1,2]; - %IMPORTANT NOTE: we are NOT able to decode behavior shifts vs. - %non shifts - - elseif transition ==2 - %Get behavior shift with shift 'id' - -% % Make proximity "rest" and remove all rest epochs -% behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). -% behavior_labels_subject = behavior_labels_subject_init(behavior_labels_subject_init~=length(behav_categ)); - - % - behavior_labels_subject = behavior_labels_subject_init; - x=behavior_labels_subject(1:end-1); y=behavior_labels_subject(2:end); - behavior_labels= [sscanf(sprintf('%d%d,',[x.';y.']),'%d,'); 0]; - shifts = x-y; - - shift_categ_table= tabulate(behavior_labels(shifts~=0)); - shift_categ_table = shift_categ_table(shift_categ_table(:,2)~=0,:); - min_occurrences = 5; - behav = shift_categ_table(shift_categ_table(:,2)>=min_occurrences,1); - - %IMPORTANT NOTE: We are able to decode above change what the - %behavioral shift is when there is one! (that just happened or - %is just about to happen) - end - - - %% Select behaviors to decode - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(behavior_labels_final); - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - -% clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... -% Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... -% channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %Balance number of trials per class - Labels = behavior_labels_final; - Input_matrix = Spike_count_raster_final; - - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); %labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min_occurrences;%min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - - channel = char(channel_flag); - disp('****************************************************************************') - disp(['Transition type' num2str(transition) ', channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate{s}(chan) = mean(hitrate) - sd_hitrate{s}(chan) = std(hitrate); - mean_hitrate_shuffled{s}(chan) = mean(hitrate_shuffled) - sd_hitrate_shuffled = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); - confusion_mat_avg=round(mean(C_concat,3)*100); - rowNames{s}=cell2mat(labels_id); - %rowNames = {labels_id{:,2}}; colNames = {labels_id{:,2}}; - C_table{s,chan} = array2table(confusion_mat_avg); - %C_table{chan, trans} = array2table(confusion_mat_avg,'RowNames',rowNames,'VariableNames',colNames); - - chan = chan +1; - end%end of channel loop - - clear labels_id -end %end of session loop - -%% Plot decoding accuracy relative to chance for all sessions, separated by monkey - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); - - -figure; set(gcf,'Position',[150 250 700 700]); -subplot(2,1,1);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = a_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 5]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for behavioral transitions, Monkey A','FontSize', 14) - -subplot(2,1,2);hold on; -cmap = hsv(size(mean_hitrate,2)); -for s = h_sessions - y = mean_hitrate{s}./mean_hitrate_shuffled{s}; - scatter(1:3, y, 60,'filled','MarkerFaceAlpha',0.6) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -chance_level = 1; -yline(chance_level,'--','At Chance', 'FontSize',16) -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 5]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel({'Multiple of chance level','hitrate/shuffled'},'FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Decoding accuracy for behavioral transitions, Monkey H','FontSize', 14) -if transition ==1 - saveas(gcf,['SVM_results_ANY_state_transitions_allSessions.png']) -else - saveas(gcf,['SVM_results_state_SPECIFIC_transitions_allSessions.png']) -end \ No newline at end of file diff --git a/Movement analyses/.DS_Store b/Movement analyses/.DS_Store deleted file mode 100644 index fd52109..0000000 Binary files a/Movement analyses/.DS_Store and /dev/null differ diff --git a/Movement analyses/old_code/log_SVM_subject_without_movement_batch_OLD.m b/Movement analyses/old_code/log_SVM_subject_without_movement_batch_OLD.m deleted file mode 100644 index 56fa099..0000000 --- a/Movement analyses/old_code/log_SVM_subject_without_movement_batch_OLD.m +++ /dev/null @@ -1,333 +0,0 @@ -%% Log_SVM_subject_without_movement_batch -% Run a linear decoder on the neural activity after regressing out movement for the subject's behavior -% (only including behaviors with a minimum # occurrence) -% Batch version of script -% October 2022 - Camille Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 30; %set temp resolution at the camera temp resolution (FPS) -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 3;%Number of SVM iterations -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0;%lump similar behavioral categories together to increase sample size. -agg_precedence=0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=15; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - chan = 1; - %for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - %Trim neural data and behavioral to align with video data - camera_start_time = behavior_log{strcmp(behavior_log{:,'Behavior'},"Camera Sync"),"start_time_round"}; - Spike_rasters_trimmed = Spike_rasters(:,camera_start_time:end); - labels_trimmed = labels(camera_start_time:end,:); - - cd(filePath) - - %Load ME - load('hooke0819_motion_energy.mat') - top_view_ME = [0; top_view_ME]; side_view_ME = [0; side_view_ME]; - - %Load DLC - dlc = readtable('hooke0819_dlc_head_alone.csv');% Load DLC key point data - dlc=dlc(1:end-1,:); %There is an extra datapoint than frame.. for now ignore the first data point - - logger_bottom = table2array(dlc(:,2:4)); logger_bottom(logger_bottom(:,3)<0.8,1:2)=nan; - logger_top = table2array(dlc(:,5:7)); logger_top(logger_top(:,3)<0.8,1:2)=nan; - nose = table2array(dlc(:,8:10)); nose(nose(:,3)<0.8,1:2)=nan; - - %Load head derived measures - head_derived_measures = load('hooke0819_head_direction.mat'); - head_direction = head_derived_measures.head_orientation_dlc; head_direction = head_direction(1:end-1,:); - quad_position = head_derived_measures.updown_position; quad_position = quad_position(1:end-1,:); - - disp('Data Loaded') - - %% Pool all the data from the alone block - - % Get alone block - %For behavior labels - lbls = cell2mat(labels_trimmed(:,3)); - lbls=lbls(1:size(dlc,1)); lbls_not_categ = lbls; - lbls = categorical(lbls); - - tabulate(lbls) - - %For spike data - Spike_rasters_final = zscore(Spike_rasters_trimmed(:,1:size(dlc,1)),0,2)'; - - %Combine mvmt predictors - logger_top_x = logger_top(:,1); - logger_top_y = logger_top(:,2); - mvmt_logger_top_x = [0; diff(logger_top(:,1))]; - mvmt_logger_top_y = [0; diff(logger_top(:,2))]; - head_mvmt = [0; diff(head_direction)]; - - mvmt = [top_view_ME, side_view_ME,... - logger_top_x, logger_top_y,... - mvmt_logger_top_x, mvmt_logger_top_y,... - head_direction, head_mvmt,... - quad_position]; - - %mvmt = [top_view_ME, side_view_ME,quad_position]; - - - %Get missing data (from deeplabcut) - [nanrow, nancol]=find(isnan(mvmt)); length(unique(nanrow))/length(lbls) - %We get ~70% missing data because Hooke spends a lot of time in a - %tiny corner. - idx_to_keep = setdiff(1:length(lbls), unique(nanrow)); - - %Remove missing data - Y = Spike_rasters_final; - Y_final = Y(idx_to_keep,:); lbls_not_categ = lbls_not_categ(idx_to_keep,:); - lbls_final = removecats(lbls(idx_to_keep)); - top_view_ME_final = zscore(top_view_ME(idx_to_keep)); - side_view_ME_final = zscore(side_view_ME(idx_to_keep)); - logger_top_x_final = zscore(logger_top_x(idx_to_keep)); - logger_top_y_final = zscore(logger_top_y(idx_to_keep)); - mvmt_logger_top_x_final = zscore(mvmt_logger_top_x(idx_to_keep)); - mvmt_logger_top_y_final = zscore(mvmt_logger_top_y(idx_to_keep)); - head_direction_final=zscore(head_direction(idx_to_keep)); - head_mvmt_final = zscore(head_mvmt(idx_to_keep)); - quad_position_final = quad_position(idx_to_keep); - %mvmt_final = mvmt(idx_to_keep,:); - - - %% Run regression - % Use adjusted Rsquared - - - for unit = 1:size(Y_final,2) %for now one unit at a time. - - %Set up predictor matrices - X_mvmt = table(top_view_ME_final, side_view_ME_final,... - logger_top_x_final,logger_top_y_final,... - mvmt_logger_top_x_final, mvmt_logger_top_y_final,... - head_direction_final,head_mvmt_final,quad_position_final,... - Y_final(:,unit)); - - Results.(['unit' num2str(unit)]) = fitlm(X_mvmt); %run linear model with mvmt only for specific unit - Y_residuals(:, unit) = Results.(['unit' num2str(unit)]).Residuals.Raw; - - X_lbls = table(lbls_final, Y_final(:,unit)); - ResultsBehav.(['unit' num2str(unit)]) = fitlm(X_lbls); %run linear model with behavior lbls only for specific unit - Y_residuals_behav(:, unit) = ResultsBehav.(['unit' num2str(unit)]).Residuals.Standardized; - - if mod(unit,10)==0 - disp(unit) - end - - end - - behavior_labels = lbls_not_categ; %Extract unique behavior info for subject - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Threat to partner"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Threat to subject"); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - - if simplify - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - end - - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - % Remove behaviors that are ill-defined or to not characterized well enough - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Vocalization')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - - % OR select behaviors manually - %behav = [5,18];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - for res = 1:3 - %1: the raw firing rates - %2: residuals after regressing out mvmt - %3: residuals after regressing out behavior - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - - if res ==1 %firing rate - Spike_count_raster_final = Y_final(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - elseif res==2 %residuals after regressing out movement - Spike_count_raster_final = Y_residuals(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - elseif res==3 %residuals after regressing out behavior - Spike_count_raster_final = Y_residuals_behav(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - end - - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(removecats(categorical(behavior_labels_final))); - - - %% Run SVM over multiple iterations - - disp('Start running SVM...') - for iter = 1:num_iter - - - %subsample to match number of neurons across brain areas - Labels = behavior_labels_final; - if randomsample==1 - Input_matrix = Spike_count_raster_final(:,randsample(unit_count(chan), min(unit_count))); - else - Input_matrix = Spike_count_raster_final; - end - - - %Balance number of trials per class - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = 300;%min(num_trials); %30; %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - if mod(iter,5)==1 - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) - end - end %End of SVM for loop - - channel = char(channel_flag); - disp('****************************************************************************') - disp([num2str(1000/temp_resolution) 'msec resolution, channels: ' channel '. DONE']) - disp('****************************************************************************') - - mean_hitrate{s}(chan,res) = mean(hitrate) - sd_hitrate{s}(chan,res) = std(hitrate); - mean_hitrate_shuffled{s}(chan,res) = mean(hitrate_shuffled) - sd_hitrate_shuffled{s}(chan,res) = std(hitrate_shuffled); - - C_concat=cat(3,C{:}); %Get confusion matrix - confusion_mat_avg{s, chan}=round(mean(C_concat,3)*100); %Average over SVM iterations - rowNames{s} = {labels_id{:,2}}; colNames{s} = {labels_id{:,2}}; %Get behavior names - C_table{s, chan} = array2table(confusion_mat_avg{s, chan},'RowNames',rowNames{s},'VariableNames',colNames{s}); - - - clear labels_id - - end %end of residuals used for loop - % chan = chan +1; - - %end %end of channel for loop - - cd(savePath) - - figure; hold on - y = [mean_hitrate{s} mean(mean_hitrate_shuffled{s})]; - bar(y) -% std_dev = [sd_hitrate{s} sd_hitrate_shuffled{s}(1)]; -% errorbar(y,std_dev,'s','MarkerSize',10) - xlim([0.5 4.5]); xticks([1:4]) - yline(1/length(behav),'LineStyle','--') - xticklabels({'Raw firing rate','Regressing out mvmt & FOV','Regressing out behavior','shuffled (chance)'}) - ylim([0 1]); ylabel('Decoding accuracy') - ax = gca; - ax.FontSize = 14; - saveas(gcf, 'SVM_regressing_outMvmt.pdf') - -end %End of session for loop - -%% Plot all sessions results - -%Change savePath for all session results folder: -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SVM_results/']); -save('SVM_results_subjectBehav.mat', "mean_hitrate","sd_hitrate","mean_hitrate_shuffled","behav","a_sessions","h_sessions","behav_categ") -%load('SVM_results_subjectBehav.mat') diff --git a/Movement analyses/old_code/log_accounting_for_mvmt.m b/Movement analyses/old_code/log_accounting_for_mvmt.m deleted file mode 100644 index 396ce9d..0000000 --- a/Movement analyses/old_code/log_accounting_for_mvmt.m +++ /dev/null @@ -1,122 +0,0 @@ -%% log_mvmt_regression -% Run a multinear regression to figure out the proportion of variance -% explained by the different predictors - - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 30; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1;%set the smoothing window size (sigma) -num_iter = 500; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=15; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/']; - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; channel_flag = "vlPFC"; - - for channel_flag = ["vlPFC", "TEO", "all"] - - - channel = char(channel_flag);%for later saving - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - - cd(filePath) - load('hooke0819_motion_energy.mat') %Load ME - top_view_ME = [0; top_view_ME]; side_view_ME = [0; side_view_ME]; - - dlc = readtable('hooke0819_dlc_head.csv');% Load DLC key point data - dlc=dlc(2:end,:); %There is an extra datapoint than frame.. for now ignore the first data point - - logger_bottom = table2array(dlc(:,2:4)); logger_bottom(logger_bottom(:,3)<0.8,1:2)=nan; - logger_top = table2array(dlc(:,5:7)); logger_top(logger_top(:,3)<0.8,1:2)=nan; - nose = table2array(dlc(:,8:10)); nose(nose(:,3)<0.8,1:2)=nan; - - disp('Data Loaded') - - - - %% Pool all the data from the alone block - - % Get alone block - %For behavior labels - lbls = cell2mat(labels(:,3)); - lbls=lbls(1:size(top_view_ME,1)); - %For spike data - Spike_rasters_final = zscore(Spike_rasters(:,1:size(top_view_ME,1)),0,2)'; - - %Combine predictors - X = [ones(size(lbls)), lbls, top_view_ME, side_view_ME];%, logger_bottom(:,1:2)]; - X_2 = [top_view_ME, side_view_ME]; - - %Check correlation structure of the predictors - predictors ={'Behavior','ME top','ME side','Logger bottom x', 'Logger bottom y','logger top x','logger top y'}; - predictors ={,'Behavior','ME top','ME side','Logger bottom x', 'Logger bottom y','logger top x','logger top y'}; - heatmap(predictors, predictors, corrcoef(X, 'rows','pairwise')) - %Note: the top and bottom logger key points are colinear. i am only - %keeping one of the two for subsequent analysis for now. - - %Get amount of missing data - [nanrow, nancol]=find(isnan(X)); length(unique(nanrow))/length(lbls) - %We get ~70% missing data because Hooke spends a lot of time in a - %tiny corner. - - %% Run regression - - mdl = fitlm(X_2,Spike_rasters_final(:,18)) - for i = 1:20 - figure;crosscorr(side_view_ME, Spike_rasters_final(:,i)) - end - [b,bint,r] = regress(Spike_rasters_final(:,18),X); - - [beta,Sigma,E,CovB,logL] = mvregress(Spike_rasters_final,X); - - - end -end - diff --git a/Movement analyses/old_code/log_headdirection copy.m b/Movement analyses/old_code/log_headdirection copy.m deleted file mode 100644 index 1a7b327..0000000 --- a/Movement analyses/old_code/log_headdirection copy.m +++ /dev/null @@ -1,52 +0,0 @@ -%% log_headmotion -% This script is meant to extract head motion from the deeplabcut -% positioning obtained from Felipe based on logger headstage front and back -% coordinates. -% SDT 10/22 - -%load session -cd('/Users/SeB/Documents/University/Postdoc/Platt/Projects/Logger project') -DLC = readmatrix(['hooke0819_td_at30cdsDLC_resnet50_macquadOct2shuffle3_4090000_filtered.csv']); -toplogger = [DLC(:,2) DLC(:,3) DLC(:,4)]; % x, y, confidence -bottomlogger = [DLC(:,5) DLC(:,6) DLC(:,7)]; - -%head orientation -head_orientation_dlc = atan2d((toplogger(:,2)-bottomlogger(:,2))*-1, toplogger(:,1)-bottomlogger(:,1)); % y = 90: he looks straight y=-90: he looks back, 0: to the right, -180; to the left -head_orientation_dlc(toplogger(:,3) <.8 | bottomlogger(:,3) <.8) = NaN; %remove missing values -figure; plot(head_orientation_dlc) - -%distance traveled by head -for t = 1:size(toplogger,1)-1 -dist_traveled(t+1) = sqrt((toplogger(t+1,1) - toplogger(t,1))^2 + (toplogger(t+1,2) - toplogger(t,2))^2); -end -dist_traveled(toplogger(:,3) <.8 | bottomlogger(:,3) <.8) = NaN; -figure; plot(dist_traveled) - -%velocity of the head -sampling_interval = 1/29.97; %video frame rate (29.97Hz) -velocity = dist_traveled/sampling_interval; -figure; plot(velocity) - -%Acceleration of the head -acceleration = diff(velocity)/sampling_interval; -acceleration = [0 acceleration]; - - - -%% Position within cage based on manual scoring of hooke0819 -monkey_down = [0 13; 37 46; 53 64; 75 97; 118 364; 480 556; 1105 1155; 1540 1552; 2101 2121; 2620 2645; 2735 2755; 3120 3120]; %Interval of time, in seconds, when the monkey is in the bottom quad. -monkey_down_frames = monkey_down*30+1; %transform to frame number - -updown_position = ones(93601,1); -for i = 1:size(monkey_down_frames,1) - - updown_position(monkey_down_frames(i,1):monkey_down_frames(i,2)) = 0; - -end - - - - -%% Calculate head-nose distance as a proxy for upward tilt (Felipe on it) -% Head_tilt = sqrt((Nose(:,1)-Head(:,1)).^2 + (Nose(:,2)-Head(:,2)).^2); % Distance between the two end points of the eye traces -% figure; plot(1:length(Head_tilt), Head_tilt) diff --git a/Movement analyses/old_code/log_headdirection.m b/Movement analyses/old_code/log_headdirection.m deleted file mode 100644 index 66f308c..0000000 --- a/Movement analyses/old_code/log_headdirection.m +++ /dev/null @@ -1,39 +0,0 @@ -%% log_headdirection -% This script is meant to extract head direction from the deeplabcut -% positioning obtained from Felipe based on logger front and back -% coordinates. I also added a vector indicating the animal's position in -% the cage (up:1) or down. -%y = 90: he looks straight y=-90: he looks back, 0: to the right, -180; to -%the left - -cd('~/Desktop/') -DLC = readmatrix(['hooke0819_td_at30cdsDLC_resnet50_macquadOct2shuffle3_4090000_filtered.csv']); -toplogger = [DLC(:,2) DLC(:,3) DLC(:,4)]; -bottomlogger = [DLC(:,5) DLC(:,6) DLC(:,7)]; - -head_orientation_dlc = atan2d((toplogger(:,2)-bottomlogger(:,2))*-1, toplogger(:,1)-bottomlogger(:,1)); % looking right is 0 deg, looking left is 180 deg - -head_orientation_dlc(toplogger(:,3) <.8 | bottomlogger(:,3) <.8) = NaN; %remove missing values - -length(find(isnan(head_orientation_dlc)))/93600 -figure;hist(bottomlogger(:,3)) %proportion missing data - -figure; plot(head_orientation_dlc) - -%Position within cage -monkey_down = [0 13; 37 46; 53 64; 75 97; 118 364; 480 556; 1105 1155; 1540 1552; 2101 2121; 2620 2645; 2735 2755; 3120 3120]; %Interval of time, in seconds, when the monkey is in the bottom quad. -monkey_down_frames = monkey_down*30+1; %transform to frame number - -updown_position = ones(93601,1); -for i = 1:size(monkey_down_frames,1) - - updown_position(monkey_down_frames(i,1):monkey_down_frames(i,2)) = 0; - -end - - - - -%% Calculate head-nose distance as a proxy for upward tilt -% Head_tilt = sqrt((Nose(:,1)-Head(:,1)).^2 + (Nose(:,2)-Head(:,2)).^2); % Distance between the two end points of the eye traces -% figure; plot(1:length(Head_tilt), Head_tilt) diff --git a/Movement analyses/old_code/log_mvmt_Threat_SelfvsOther.m b/Movement analyses/old_code/log_mvmt_Threat_SelfvsOther.m deleted file mode 100644 index 5cd6645..0000000 --- a/Movement analyses/old_code/log_mvmt_Threat_SelfvsOther.m +++ /dev/null @@ -1,259 +0,0 @@ -%% log_mvmt_Threat_SelfvsOther -% Extract movement and FOV variation of threat to self vs. other. -% C. Testard Oct. 2022 - - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 30; %frame rate -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution; %set the smoothing window size (sigma) -simplify=1; -agg_precedence = 0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=15; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/Mvmt_results']; - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; channel_flag = "all"; - - %for sig = 1:length(sigma_list) - - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - cd(filePath) - - %Trim neural and behavioral data to align with video data - camera_start_time = behavior_log{strcmp(behavior_log{:,'Behavior'},"Camera Sync"),"start_time_round"}; - labels_trimmed = labels(camera_start_time:end,:); - - %Load ME - load('hooke0819_motion_energy.mat') - top_view_ME = [0; top_view_ME]; side_view_ME = [0; side_view_ME]; - - %Load DLC - dlc = readtable('hooke0819_dlc_head_alone.csv');% Load DLC key point data - dlc=dlc(1:end-1,:); %There is an extra datapoint than frame.. for now ignore the first data point - - logger_bottom = table2array(dlc(:,2:4)); logger_bottom(logger_bottom(:,3)<0.8,1:2)=nan; - logger_top = table2array(dlc(:,5:7)); logger_top(logger_top(:,3)<0.8,1:2)=nan; - nose = table2array(dlc(:,8:10)); nose(nose(:,3)<0.8,1:2)=nan; - - %Load head derived measures - head_derived_measures = load('hooke0819_head_direction.mat'); - head_direction = head_derived_measures.head_orientation_dlc; head_direction = head_direction(1:end-1,:); - quad_position = head_derived_measures.updown_position; quad_position = quad_position(1:end-1,:); - - disp('Data Loaded') - - - - %% Pool all the data from the alone block - - % Get alone block - %For behavior labels - lbls = cell2mat(labels_trimmed(:,3)); - lbls=lbls(1:size(top_view_ME,1)); - %lbls = categorical(lbls); - - %Simplify behaviors (lumping categories together) - lbls(lbls==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - tabulate(lbls) - - %Combine mvmt predictors - logger_top_x = logger_top(:,1); - logger_top_y = logger_top(:,2); - - for t = 1:size(logger_top,1)-1 - dist_traveled(t+1) = sqrt((logger_top(t+1,1) - logger_top(t,1))^2 + (logger_top(t+1,2) - logger_top(t,2))^2); - end - dist_traveled = dist_traveled'; - - %velocity of the head - sampling_interval = .0333; %video frame rate (30Hz) - velocity = dist_traveled/sampling_interval; - - %Acceleration of the head - acceleration = diff(velocity)/sampling_interval; - acceleration = [0; acceleration]; - - mvmt_logger_top_x = [0; diff(logger_top(:,1))]; - mvmt_logger_top_y = [0; diff(logger_top(:,2))]; - - head_mvmt = [0; diff(head_direction)]; - - mvmt = [top_view_ME, side_view_ME,... - logger_top_x, logger_top_y,... - dist_traveled, velocity, acceleration,... - mvmt_logger_top_x, mvmt_logger_top_y,... - head_direction, head_mvmt,... - quad_position]; - - %mvmt = [top_view_ME, side_view_ME,quad_position]; - - - %Get missing data (from deeplabcut) - [nanrow, nancol]=find(isnan(mvmt)); length(unique(nanrow))/length(lbls) - %We get ~70% missing data because Hooke spends a lot of time in a - %tiny corner. - idx_to_keep = setdiff(1:length(lbls), unique(nanrow)); - - %Remove missing data - lbls_final = lbls(idx_to_keep); - top_view_ME_final = top_view_ME(idx_to_keep); - side_view_ME_final = side_view_ME(idx_to_keep); - logger_top_x_final = logger_top_x(idx_to_keep); - logger_top_y_final = logger_top_y(idx_to_keep); - dist_traveled_final = dist_traveled(idx_to_keep); - velocity_final = velocity(idx_to_keep); - acceleration_final = acceleration(idx_to_keep); - mvmt_logger_top_x_final = mvmt_logger_top_x(idx_to_keep); - mvmt_logger_top_y_final = mvmt_logger_top_y(idx_to_keep); - head_direction_final=head_direction(idx_to_keep); - head_mvmt_final = head_mvmt(idx_to_keep); - quad_position_final = quad_position(idx_to_keep); - %mvmt_final = mvmt(idx_to_keep,:); - - %% Only consider behaviors of interest - - behav=[9,10]; - idx_beh = ismember(lbls_final, behav); - - lbls_final = lbls_final(idx_beh); - top_view_ME_final = top_view_ME_final(idx_beh); - side_view_ME_final = side_view_ME_final(idx_beh); - logger_top_x_final = logger_top_x_final(idx_beh); - logger_top_y_final = logger_top_y_final(idx_beh); - dist_traveled_final = dist_traveled_final(idx_beh); - velocity_final = velocity_final(idx_beh); - acceleration_final = acceleration_final(idx_beh); - mvmt_logger_top_x_final = mvmt_logger_top_x_final(idx_beh); - mvmt_logger_top_y_final = mvmt_logger_top_y_final(idx_beh); - head_direction_final =head_direction_final(idx_beh); - head_mvmt_final = head_mvmt_final(idx_beh); - quad_position_final = quad_position_final(idx_beh); - - - %% Quantify movements within behaviors - - cd(savePath) - -% %For motion energy -% unq_beh = [9 10];%unique(lbls_final); -% figure; xlim([0 50000]); hold on -% for b=1:length(unq_beh) -% %subplot(1,length(unq_beh),b) -% histogram(top_view_ME_final(lbls_final==unq_beh(b)),'BinWidth',2000,'Normalization','pdf') -% end -% legend(behav_categ(unq_beh)) - - figure; hold on; set(gcf,'Position',[150 250 1200 300]); - - %For field of view - subplot(1,3,1); hold on - for b=1:length(unq_beh) - %subplot(1,length(unq_beh),b) - histogram(head_direction_final(lbls_final==unq_beh(b)),'BinWidth',10,'Normalization','pdf') - xlim([-180 180]); %ylim([0 0.045]) - ylabel('Proportion'); xlabel('Degrees visual angle') - end - legend(behav_categ(unq_beh), 'Location','northwest') - - %For x position - subplot(1,3,2); hold on - for b=1:length(unq_beh) - histogram(logger_top_x_final(lbls_final==unq_beh(b)),'BinWidth',20,'Normalization','pdf') - xlim([150 700]); ylim([0 0.027]) - ylabel('Proportion'); xlabel('Enclosure x pos.') - end - %legend(behav_categ(unq_beh)) - - %For head movement - subplot(1,3,3); hold on - for b=1:length(unq_beh) - data = dist_traveled_final(lbls_final==unq_beh(b)); - mean(data) - histogram(data,'BinWidth',2,'Normalization','pdf') - xlim([0 60]); %ylim([0 0.045]) - ylabel('Proportion'); xlabel('Distance traveled') - end - %legend(behav_categ(unq_beh)) - -% %For acceleration -% figure; hold on; -% for b=1:length(unq_beh) -% data = acceleration_final(lbls_final==unq_beh(b)); -% mean(data) -% histogram(data)%,'BinWidth',500,'Normalization','pdf') -% %xlim([0 40]); %ylim([0 0.045]) -% ylabel('Proportion'); xlabel('Acceleratiion') -% end -% legend(behav_categ(unq_beh)) -% -% %For velocity -% figure; hold on; -% for b=1:length(unq_beh) -% data = velocity_final(lbls_final==unq_beh(b)); -% mean(data) -% histogram(data,'BinWidth',100,'Normalization','pdf') -% %xlim([0 40]); %ylim([0 0.045]) -% ylabel('Proportion'); xlabel('Velocity') -% end -% legend(behav_categ(unq_beh)) - -% %For y position -% figure; hold on; -% for b=1:length(unq_beh) -% histogram(logger_top_y_final(lbls_final==unq_beh(b)),'BinWidth',10,'Normalization','pdf') -% xlim([0 800]); ylim([0 0.02]) -% ylabel('Proportion'); xlabel('Enclosure y pos.') -% end -% legend(behav_categ(unq_beh)) - saveas(gcf,'Mvmt_Threat_SelfVsOther.pdf') - -end - diff --git a/Movement analyses/old_code/log_mvmt_perBehav.m b/Movement analyses/old_code/log_mvmt_perBehav.m deleted file mode 100644 index 5e0af57..0000000 --- a/Movement analyses/old_code/log_mvmt_perBehav.m +++ /dev/null @@ -1,227 +0,0 @@ -%% log_mvmt_perBehav -% Extract movement variation per behavior, as well as overlap across -% behaviors -% C. Testard Oct. 2022 - - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 30; %frame rate -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution; %set the smoothing window size (sigma) -simplify=1; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=15; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/']; - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; channel_flag = "all"; - - %for sig = 1:length(sigma_list) - - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - cd(filePath) - - %Trim neural and behavioral data to align with video data - camera_start_time = behavior_log{strcmp(behavior_log{:,'Behavior'},"Camera Sync"),"start_time_round"}; - Spike_rasters_trimmed = Spike_rasters(:,camera_start_time:end); - labels_trimmed = labels(camera_start_time:end,:); - - %Load ME - load('hooke0819_motion_energy.mat') - top_view_ME = [0; top_view_ME]; side_view_ME = [0; side_view_ME]; - - %Load DLC - dlc = readtable('hooke0819_dlc_head.csv');% Load DLC key point data - dlc=dlc(1:end-1,:); %There is an extra datapoint than frame.. for now ignore the first data point - - logger_bottom = table2array(dlc(:,2:4)); logger_bottom(logger_bottom(:,3)<0.8,1:2)=nan; - logger_top = table2array(dlc(:,5:7)); logger_top(logger_top(:,3)<0.8,1:2)=nan; - nose = table2array(dlc(:,8:10)); nose(nose(:,3)<0.8,1:2)=nan; - - %Load head derived measures - head_derived_measures = load('hooke0819_head_direction.mat'); - head_direction = head_derived_measures.head_orientation_dlc; head_direction = head_direction(1:end-1,:); - quad_position = head_derived_measures.updown_position; quad_position = quad_position(1:end-1,:); - - disp('Data Loaded') - - - - %% Pool all the data from the alone block - - % Get alone block - %For behavior labels - lbls = cell2mat(labels_trimmed(:,3)); - lbls=lbls(1:size(top_view_ME,1)); - %lbls = categorical(lbls); - - %Simplify behaviors (lumping categories together) - lbls(lbls==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Threat to partner"); - lbls(lbls==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Threat to subject"); - lbls(lbls==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - if simplify - %Simplify behavioral catagories - %Lump all aggressive interactions together - lbls(lbls==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - lbls(lbls==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - lbls(lbls==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Aggression"); - lbls(lbls==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - lbls(lbls==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - lbls(lbls==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - end - - tabulate(lbls) - - %Combine mvmt predictors - logger_top_x = logger_top(:,1); - logger_top_y = logger_top(:,2); - mvmt_logger_top_x = [0; diff(logger_top(:,1))]; - mvmt_logger_top_y = [0; diff(logger_top(:,2))]; - head_mvmt = [0; diff(head_direction)]; - - mvmt = [top_view_ME, side_view_ME,... - logger_top_x, logger_top_y,... - mvmt_logger_top_x, mvmt_logger_top_y,... - head_direction, head_mvmt,... - quad_position]; - - %mvmt = [top_view_ME, side_view_ME,quad_position]; - - - %Get missing data (from deeplabcut) - [nanrow, nancol]=find(isnan(mvmt)); length(unique(nanrow))/length(lbls) - %We get ~70% missing data because Hooke spends a lot of time in a - %tiny corner. - idx_to_keep = setdiff(1:length(lbls), unique(nanrow)); - - %Remove missing data - lbls_final = lbls(idx_to_keep); - top_view_ME_final = top_view_ME(idx_to_keep); - side_view_ME_final = side_view_ME(idx_to_keep); - logger_top_x_final = logger_top_x(idx_to_keep); - logger_top_y_final = logger_top_y(idx_to_keep); - mvmt_logger_top_x_final = mvmt_logger_top_x(idx_to_keep); - mvmt_logger_top_y_final = mvmt_logger_top_y(idx_to_keep); - head_direction_final=head_direction(idx_to_keep); - head_mvmt_final = head_mvmt(idx_to_keep); - quad_position_final = quad_position(idx_to_keep); - %mvmt_final = mvmt(idx_to_keep,:); - - - %% Quantify movements within behaviors - - cd(savePath) - - %For motion energy - unq_beh = [1 4 5 18 23 24 25 29];%unique(lbls_final); - figure; xlim([0 50000]); hold on - for b=1:length(unq_beh) - %subplot(1,length(unq_beh),b) - histogram(top_view_ME_final(lbls_final==unq_beh(b)),'BinWidth',2000,'Normalization','pdf') - title(behav_categ(unq_beh(b))) - pause(3) - end - - figure; xlim([0 50000]); hold on - for b=1:length(unq_beh) - %subplot(1,length(unq_beh),b) - histogram(side_view_ME_final(lbls_final==unq_beh(b)),'BinWidth',2000,'Normalization','pdf') - title(behav_categ(unq_beh(b))) - pause(3) - end - - %For field of view - figure; hold on; set(gcf,'Position',[150 250 1500 200]); - for b=1:length(unq_beh) - subplot(1,length(unq_beh),b) - histogram(head_direction_final(lbls_final==unq_beh(b)),'BinWidth',10,'Normalization','pdf') - title(behav_categ(unq_beh(b))) - xlim([-180 180]); ylim([0 0.045]) - ylabel('Proportion'); xlabel('Degrees') - end - - %For x position - figure; hold on; set(gcf,'Position',[150 250 1500 200]); - for b=1:length(unq_beh) - subplot(1,length(unq_beh),b) - histogram(logger_top_x_final(lbls_final==unq_beh(b)),'BinWidth',10,'Normalization','pdf') - title(behav_categ(unq_beh(b))) - xlim([0 800]); ylim([0 0.045]) - ylabel('Proportion'); xlabel('Enclosure x pos.') - end - - %For y position - figure; hold on; set(gcf,'Position',[150 250 1500 200]); - for b=1:length(unq_beh) - subplot(1,length(unq_beh),b) - histogram(logger_top_y_final(lbls_final==unq_beh(b)),'BinWidth',10,'Normalization','pdf') - title(behav_categ(unq_beh(b))) - xlim([0 800]); ylim([0 0.02]) - ylabel('Proportion'); xlabel('Enclosure y pos.') - end - - %% Compare movements across behaviors - - %For field of view - figure; hold on; %set(gcf,'Position',[150 250 1500 200]); - for b=1:length(unq_beh) - %subplot(1,length(unq_beh),b) - histogram(head_direction_final(lbls_final==unq_beh(b)),'BinWidth',10,'Normalization','pdf') - %title(behav_categ(unq_beh(b))) - xlim([-180 180]); ylim([0 0.045]) - ylabel('Proportion'); xlabel('Degrees') - end - legend(behav_categ(unq_beh)) - title('Field of view across behaviors') - ax = gca; - ax.FontSize = 14; -end - diff --git a/Movement analyses/old_code/log_mvmt_perBehav_wholeSession.m b/Movement analyses/old_code/log_mvmt_perBehav_wholeSession.m deleted file mode 100644 index e1d91f0..0000000 --- a/Movement analyses/old_code/log_mvmt_perBehav_wholeSession.m +++ /dev/null @@ -1,317 +0,0 @@ -%% log_mvmt_perBehav -% Extract movement variation per behavior, as well as overlap across -% behaviors -% C. Testard Oct. 2022 - - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 29.97; %frame rate -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution; %set the smoothing window size (sigma) -simplify=0; -threat_precedence =1; -exclude_sq = 0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; sesh=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/Mvmt_results']; - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; channel_flag = "all"; - - %for sig = 1:length(sigma_list) - - - %% Get data with specified temporal resolution and channels - - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - - cd(filePath) - - %Load DLC - dlc = readtable('mvmt_data_dlc.csv');% Load DLC key point data - length(find(sum(isnan(table2array(dlc)),2)==0))/size(dlc,1) %proportion of full data - - - %Trim neural and behavioral data to align with video data - camera_start_time = behavior_log{strcmp(behavior_log{:,'Behavior'},"Camera Sync"),"start_time_round"}; - camera_end_time = camera_start_time + size(dlc,1) -1; - - try - Spike_rasters_trimmed = Spike_rasters(:,camera_start_time:camera_end_time); - labels_trimmed = labels(camera_start_time:camera_end_time,:); - catch %If camera went on past end of recording (occurs in 2 sessions because of an error at the end) - Spike_rasters_trimmed = Spike_rasters(:,camera_start_time:end); - labels_trimmed = labels(camera_start_time:end,:); - dlc = dlc(1:size(labels_trimmed,1),:); - end - - disp('Data Loaded') - - - - %% Pool align neural, behavior and video-based mvmt data - - %For behavior labels - lbls = cell2mat(labels_trimmed(:,3)); - blocks = cell2mat(labels_trimmed(:,12)); - %Note: neural recordings started before camera started and ended after. So we need to trim - %neural and behavioral data on both ends to match the length of - %camera-based movement data. - - %Simplify behaviors (lumping categories together) - lbls(lbls==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - if simplify - %Simplify behavioral catagories - %Lump all aggressive interactions together - lbls(lbls==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - lbls(lbls==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - lbls(lbls==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - lbls(lbls==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - end - - tabulate(lbls) - - %Combine mvmt predictors - mvmt = table2array(dlc); - - %Get missing data (from deeplabcut) - [nanrow, nancol]=find(isnan(mvmt)); length(unique(nanrow))/length(lbls) - %We get ~50-60% missing data. - idx_to_keep = setdiff(1:length(lbls), unique(nanrow)); - - %Remove missing data - lbls_numerical = lbls;%(idx_to_keep); - lbls_string = behav_categ(lbls_numerical); - mvmt_final = mvmt;%(idx_to_keep,:); - block_numerical = blocks;%(idx_to_keep); - block_id={"female","male","alone"}; - block_string = block_id(block_numerical); - - - %Extract data table for Seb plotting - toplogger_x = mvmt_final(:,1); - toplogger_y = mvmt_final(:,2); - bottomlogger_x = mvmt_final(:,3); - bottomlogger_y = mvmt_final(:,4); - head_orientation_dlc = mvmt_final(:,5); - dist_traveled = mvmt_final(:,6); - acceleration = mvmt_final(:,7); - - predictors_to_save{sesh} = table(lbls_string',lbls_numerical,block_string', block_numerical, ... - toplogger_x, toplogger_y,... - bottomlogger_x, bottomlogger_y, head_orientation_dlc,... - dist_traveled, acceleration); - Response_to_save{sesh}=Spike_rasters_trimmed; - - -% % % % % %% Quantify movements within behaviors -% % % % % -% % % % % %Set colormap -% % % % % Cmap = [[1 0 0];[1 0.4 0.1];[0 0 0];[0.3 0.7 1];[0 0.7 0];[1 0 1];[0 1 1];... -% % % % % [0 0 1];[0.8 0 0];[1 0 0];[0 0 0];[0.2 0.9 0.76];[0 0 0];[0 0 0];[0.7 0 1];... -% % % % % [0 0 0];[0 0 0];[0.9 0.5 0];[0 0 0];[0 0 0];[0.8 0 0];[1 0 0];[0.9 0.7 0.12];[0.5 0.2 0.5];... -% % % % % [0 0 0];[0 0 0];[0.8 0.4 0.4];[0 0 0];[0.5 0.5 0.5]]; -% % % % % -% % % % % cd(savePath) -% % % % % -% % % % % %For position in enclosure -% % % % % unq_beh = [1 4 5 7 8 18 23 24 29];%unique(lbls_final); -% % % % % -% % % % % figure; hold on; set(gcf,'Position',[150 250 1800 1000]); -% % % % % for b=1:length(unq_beh) -% % % % % subplot(3,length(unq_beh),b) -% % % % % loggertopx_cv(b) = getCV(mvmt_final(lbls_final==unq_beh(b),1)); -% % % % % histogram(mvmt_final(lbls_final==unq_beh(b),1),'BinWidth',50,'Normalization','pdf','FaceColor',Cmap(unq_beh(b),:)) -% % % % % text(50,0.0035, num2str(round(loggertopx_cv(b),2))) -% % % % % xlim([0 1500]); ylim([0 0.004]) -% % % % % title(behav_categ(unq_beh(b))) -% % % % % if b==1 -% % % % % ylabel('Proportion') -% % % % % xlabel('Enclosure x pos.') -% % % % % else -% % % % % yticks('') -% % % % % end -% % % % % end -% % % % % -% % % % % -% % % % % %figure; hold on; set(gcf,'Position',[150 250 1800 200]); -% % % % % for b=1:length(unq_beh) -% % % % % subplot(3,length(unq_beh),length(unq_beh)+b) -% % % % % loggertopy_cv(b) = getCV(mvmt_final(lbls_final==unq_beh(b),2)); -% % % % % histogram(mvmt_final(lbls_final==unq_beh(b),2),'BinWidth',50,'Normalization','pdf','FaceColor',Cmap(unq_beh(b),:)) -% % % % % text(50, 0.006, num2str(round(loggertopy_cv(b),2))) -% % % % % %title(behav_categ(unq_beh(b))) -% % % % % xlim([-6 1000]); ylim([0 0.007]) -% % % % % if b==1 -% % % % % ylabel('Proportion') -% % % % % xlabel('Enclosure y pos.') -% % % % % else -% % % % % yticks('') -% % % % % end -% % % % % end -% % % % % -% % % % % %For field of view -% % % % % %figure; hold on; set(gcf,'Position',[150 250 1800 200]); -% % % % % for b=1:length(unq_beh) -% % % % % subplot(3,length(unq_beh),length(unq_beh)*2+b) -% % % % % fov_cv(b) = getCV(mvmt_final(lbls_final==unq_beh(b),5)); -% % % % % histogram(mvmt_final(lbls_final==unq_beh(b),5),'BinWidth',10,'Normalization','pdf','FaceColor',Cmap(unq_beh(b),:)) -% % % % % %title(behav_categ(unq_beh(b))) -% % % % % text(-160, 0.02, num2str(round(fov_cv(b),2))) -% % % % % xlim([-180 180]); ylim([0 0.025]) -% % % % % if b==1 -% % % % % ylabel('Proportion'); xlabel('Degrees of visual angle') -% % % % % else -% % % % % yticks('') -% % % % % end -% % % % % end -% % % % % ax = gca; -% % % % % ax.FontSize = 14; -% % % % % -% % % % % saveas(gcf, [savePath '/Mvmt_variation_within_behaviors_colored.pdf']); close all -% % % % % -% % % % % %% Compare movements across behaviors -% % % % % -% % % % % %For field of view -% % % % % figure; hold on; %set(gcf,'Position',[150 250 1500 200]); -% % % % % for b=1:length(unq_beh) -% % % % % %subplot(1,length(unq_beh),b) -% % % % % histogram(mvmt_final(lbls_final==unq_beh(b),5),'BinWidth',10,'Normalization','pdf','FaceColor',Cmap(unq_beh(b),:),'FaceAlpha',0.8) -% % % % % %title(behav_categ(unq_beh(b))) -% % % % % xlim([-180 180]); ylim([0 0.03]) -% % % % % ylabel('Proportion'); xlabel('Degrees') -% % % % % end -% % % % % legend(behav_categ(unq_beh),'Location','best') -% % % % % title('Field of view across behaviors') -% % % % % ax = gca; -% % % % % ax.FontSize = 14; -% % % % % saveas(gcf, [savePath '/Mvmt_overlap_between_behaviors.pdf']); close all -% % % % % -% % % % % %For head movement -% % % % % figure; hold on; %set(gcf,'Position',[150 250 1500 200]); -% % % % % for b=1:length(unq_beh) -% % % % % %subplot(1,length(unq_beh),b) -% % % % % histogram(mvmt_final(lbls_final==unq_beh(b),6),'BinWidth',1,'Normalization','pdf','FaceColor',Cmap(unq_beh(b),:),'FaceAlpha',0.8) -% % % % % %title(behav_categ(unq_beh(b))) -% % % % % xlim([0 50]); %ylim([0 0.03]) -% % % % % ylabel('Proportion'); xlabel('Distance traveled') -% % % % % pause(2) -% % % % % end -% % % % % legend(behav_categ(unq_beh),'Location','best') -% % % % % title('Head movement') -% % % % % ax = gca; -% % % % % ax.FontSize = 14; -% % % % % -% % % % % %Compute % field of view overlap -% % % % % for b1=1:length(unq_beh) -% % % % % for b2=1:length(unq_beh) -% % % % % hist1= histcounts(mvmt_final(lbls_final==unq_beh(b1),5),20)/length(mvmt_final(lbls_final==unq_beh(b1),5)); -% % % % % hist2= histcounts(mvmt_final(lbls_final==unq_beh(b2),5),20)/length(mvmt_final(lbls_final==unq_beh(b2),5)); -% % % % % -% % % % % bothHistograms = [hist1', hist2']; -% % % % % minCounts = min(bothHistograms, [], 2); -% % % % % maxCounts = max(bothHistograms, [], 2); -% % % % % ratios = minCounts ./ maxCounts; -% % % % % meanPercentage(b1,b2) = nanmean(ratios); -% % % % % end -% % % % % end -% % % % % %plot heatmap -% % % % % AxesLabels= behav_categ(unq_beh); -% % % % % A=tril(meanPercentage,-1); A(A==0)=nan; -% % % % % hm=heatmap(A,'Colormap',jet) -% % % % % hm.XDisplayLabels = AxesLabels; hm.YDisplayLabels = AxesLabels; -% % % % % -% % % % % meanPercentage_session(s)=nanmean(reshape(A,1,[])); -% % % % % sdPercentage_session(s)=nanstd(reshape(A,1,[])); -% % % % % -% % % % % -% % % % % %% Compare movements across blocks -% % % % % -% % % % % unq_beh = [8]; -% % % % % Cmap_block = [[0.9 0.7 0.12];[0 0.6 0.8];[0.5 0 0]]; -% % % % % -% % % % % %For field of view -% % % % % figure; hold on; %set(gcf,'Position',[150 250 1500 200]); -% % % % % for bl=1:2 -% % % % % histogram(mvmt_final(lbls_final==unq_beh & block_final==bl,5),'BinWidth',10,'Normalization','pdf','FaceColor',Cmap_block(bl,:),'FaceAlpha',0.8) -% % % % % xlim([-180 180]); ylim([0 0.03]) -% % % % % ylabel('Proportion'); xlabel('Degrees') -% % % % % end -% % % % % legend({'block1','block2'},'Location','best') -% % % % % title('Field of view across behaviors') -% % % % % ax = gca; -% % % % % ax.FontSize = 14; -% % % % % %saveas(gcf, [savePath '/Mvmt_overlap_between_behaviors.pdf']); close all -% % % % % -% % % % % %For head movement -% % % % % figure; hold on; %set(gcf,'Position',[150 250 1500 200]); -% % % % % for bl=1:2 -% % % % % -% % % % % histogram(mvmt_final(lbls_final==unq_beh & block_final==bl,6),'BinWidth',1,'Normalization','pdf','FaceColor',Cmap_block(bl,:),'FaceAlpha',0.8) -% % % % % xlim([0 50]); %ylim([0 0.03]) -% % % % % ylabel('Proportion'); xlabel('Distance traveled') -% % % % % end -% % % % % legend({'block1','block2'},'Location','best') -% % % % % title('Head movement') -% % % % % ax = gca; -% % % % % ax.FontSize = 14; -% % % % % -% % % % % %For position in enclosure -% % % % % figure; hold on; %set(gcf,'Position',[150 250 1500 200]); -% % % % % for bl=1:2 -% % % % % -% % % % % histogram(mvmt_final(lbls_final==unq_beh & block_final==bl,1),'BinWidth',50,'Normalization','pdf','FaceColor',Cmap_block(bl,:),'FaceAlpha',0.8) -% % % % % xlim([0 1500]); %ylim([0 0.03]) -% % % % % ylabel('Proportion'); xlabel('Distance traveled') -% % % % % pause(1) -% % % % % end -% % % % % legend({'block1','block2'},'Location','best') -% % % % % title('x position') -% % % % % ax = gca; -% % % % % ax.FontSize = 14; - - -sesh=sesh+1; - -disp(sesh) - -end - diff --git a/Movement analyses/old_code/log_mvmt_regression.m b/Movement analyses/old_code/log_mvmt_regression.m deleted file mode 100644 index c1086c9..0000000 --- a/Movement analyses/old_code/log_mvmt_regression.m +++ /dev/null @@ -1,188 +0,0 @@ -%% log_mvmt_regression -% Run a multinear regression to figure out the proportion of variance -% explained by the different predictors - - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 30; %frame rate -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution; %set the smoothing window size (sigma) -num_iter = 500; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=15; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/']; - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; channel_flag = "all"; - - for channel_flag = ["vlPFC", "TEO", "all"] - - - channel = char(channel_flag);%for later saving - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - - cd(filePath) - - %Load ME - load('hooke0819_motion_energy.mat') - top_view_ME = [0; top_view_ME]; side_view_ME = [0; side_view_ME]; - - %Load DLC - dlc = readtable('hooke0819_dlc_head.csv');% Load DLC key point data - dlc=dlc(1:end-1,:); %There is an extra datapoint than frame.. for now ignore the first data point - - logger_bottom = table2array(dlc(:,2:4)); logger_bottom(logger_bottom(:,3)<0.8,1:2)=nan; - logger_top = table2array(dlc(:,5:7)); logger_top(logger_top(:,3)<0.8,1:2)=nan; - nose = table2array(dlc(:,8:10)); nose(nose(:,3)<0.8,1:2)=nan; - - %Load head derived measures - head_derived_measures = load('hooke0819_head_direction.mat'); - head_direction = head_derived_measures.head_orientation_dlc; head_direction = head_direction(1:end-1,:); - quad_position = head_derived_measures.updown_position; quad_position = quad_position(1:end-1,:); - - disp('Data Loaded') - - - - %% Pool all the data from the alone block - - % Get alone block - %For behavior labels - lbls = cell2mat(labels(:,3)); - lbls=lbls(1:size(top_view_ME,1)); - lbls = categorical(lbls); - - tabulate(lbls) - - %For spike data - Spike_rasters_final = zscore(Spike_rasters(:,1:size(top_view_ME,1)),0,2)'; - - %Combine mvmt predictors - logger_top_x = logger_top(:,1); - logger_top_y = logger_top(:,2); - mvmt_logger_top_x = [0; diff(logger_top(:,1))]; - mvmt_logger_top_y = [0; diff(logger_top(:,2))]; - head_mvmt = [0; diff(head_direction)]; - - mvmt = [top_view_ME, side_view_ME,... - logger_top_x, logger_top_y,... - mvmt_logger_top_x, mvmt_logger_top_y,... - head_direction, head_mvmt,... - quad_position]; - - %mvmt = [top_view_ME, side_view_ME,quad_position]; - - - %Get missing data (from deeplabcut) - [nanrow, nancol]=find(isnan(mvmt)); length(unique(nanrow))/length(lbls) - %We get ~70% missing data because Hooke spends a lot of time in a - %tiny corner. - idx_to_keep = setdiff(1:length(lbls), unique(nanrow)); - - %Remove missing data - Y = Spike_rasters_final; - Y_final = Y(idx_to_keep,:); - lbls_final = removecats(lbls(idx_to_keep)); - top_view_ME_final = top_view_ME(idx_to_keep); - side_view_ME_final = side_view_ME(idx_to_keep); - logger_top_x_final = logger_top_x(idx_to_keep); - logger_top_y_final = logger_top_y(idx_to_keep); - mvmt_logger_top_x_final = mvmt_logger_top_x(idx_to_keep); - mvmt_logger_top_y_final = mvmt_logger_top_y(idx_to_keep); - head_direction_final=head_direction(idx_to_keep); - head_mvmt_final = head_mvmt(idx_to_keep); - quad_position_final = quad_position(idx_to_keep); - %mvmt_final = mvmt(idx_to_keep,:); - - %Set up predictor matrix - unit = 18;%randi(size(Y,2),1); %for now one unit at a time. - - X_lbls = table(lbls_final, Y_final(:,unit)); - X_mvmt = table(top_view_ME_final, side_view_ME_final,... - logger_top_x_final,logger_top_y_final,... - mvmt_logger_top_x_final, mvmt_logger_top_y_final,... - head_direction_final,quad_position_final,... - Y_final(:,unit)); - X_all = table(lbls_final, top_view_ME_final, side_view_ME_final,... - logger_top_x_final,logger_top_y_final,... - mvmt_logger_top_x_final, mvmt_logger_top_y_final,... - head_direction_final,quad_position_final,... - Y_final(:,unit)); - %X_2 = [top_view_ME, side_view_ME]; - -% % % %Check correlation structure of the predictors -% % % %predictors ={'Behavior','ME top','ME side','Logger bottom x', 'Logger bottom y','logger top x','logger top y'}; -% % % predictors ={'Behavior','ME top','ME side','Logger top x', 'Logger top y',... -% % % 'Mvmt logger top x','Mvmt logger top y', 'head_direction', 'mvmt_head_direction','quad_position'}; -% % % heatmap(predictors, predictors, corrcoef(X_all, 'rows','pairwise')) -% % % %Note: the top and bottom logger key points are colinear. i am only -% % % %keeping one of the two for subsequent analysis for now. - - - - %% Run regression - % Use adjusted Rsquared - - mdl_all = fitlm(X_all) %extract the standardized betas - - mdl_behav = fitlm(X_lbls) - - mdl_mvmt = fitlm(X_mvmt) - - for i = 1:20 - figure;crosscorr(side_view_ME, Spike_rasters_final(:,i)) - end - [b,bint,r] = regress(Spike_rasters_final(:,18),X); - - X_all = X_all(:,1:end-1); - [beta,Sigma,E,CovB,logL] = mvregress(table2mat(X_all),Y_final); - - - end -end - diff --git a/Movement analyses/old_code/log_mvmt_regression_perUnit.m b/Movement analyses/old_code/log_mvmt_regression_perUnit.m deleted file mode 100644 index 6406c08..0000000 --- a/Movement analyses/old_code/log_mvmt_regression_perUnit.m +++ /dev/null @@ -1,309 +0,0 @@ -%% log_mvmt_regression -% Run a multinear regression to figure out the proportion of variance -% explained by the different predictors - - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 30; %frame rate -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution; %set the smoothing window size (sigma) -sigma_list= [1/temp_resolution, 1, 10, 30]; -num_iter = 500; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=15; units_to_remove = []; -for s =15%session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/Mvmt_results']; - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; channel_flag = "all"; - - for sig = 1:length(sigma_list) - - - sigma = sigma_list(sig)*temp_resolution; - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - - cd(filePath) - - %Trim neural data and behavioral to align with video data - camera_start_time = behavior_log{strcmp(behavior_log{:,'Behavior'},"Camera Sync"),"start_time_round"}; - Spike_rasters_trimmed = Spike_rasters(:,camera_start_time:end); - labels_trimmed = labels(camera_start_time:end,:); - - %Load ME - load('hooke0819_motion_energy.mat') - top_view_ME = [0; top_view_ME]; side_view_ME = [0; side_view_ME]; - - %Load DLC - dlc = readtable('hooke0819_dlc_head_alone.csv');% Load DLC key point data - dlc=dlc(1:end-1,:); %There is an extra datapoint than frame.. for now ignore the first data point - - logger_top = table2array(dlc(:,2:4)); logger_top(logger_top(:,3)<0.8,1:2)=nan; - logger_bottom = table2array(dlc(:,5:7)); logger_bottom(logger_bottom(:,3)<0.8,1:2)=nan; - nose = table2array(dlc(:,8:10)); nose(nose(:,3)<0.8,1:2)=nan; - - %Load head derived measures - head_derived_measures = load('hooke0819_head_direction.mat'); - head_direction = head_derived_measures.head_orientation_dlc; head_direction = head_direction(1:end-1,:); - quad_position = head_derived_measures.updown_position; quad_position = quad_position(1:end-1,:); - - disp('Data Loaded') - - %% Pool all the data from the alone block - - % Get alone block - %For behavior labels - lbls = cell2mat(labels_trimmed(:,3)); - lbls=lbls(1:size(top_view_ME,1)); - lbls = categorical(lbls); - - tabulate(lbls) - - %For spike data - Spike_rasters_final = zscore(Spike_rasters_trimmed(:,1:size(top_view_ME,1)),0,2)'; - - %Combine mvmt predictors - logger_top_x = logger_top(:,1); - logger_top_y = logger_top(:,2); - mvmt_logger_top_x = [0; diff(logger_top(:,1))]; - mvmt_logger_top_y = [0; diff(logger_top(:,2))]; - head_mvmt = [0; diff(head_direction)]; - - mvmt = [top_view_ME, side_view_ME,... - logger_top_x, logger_top_y,... - mvmt_logger_top_x, mvmt_logger_top_y,... - head_direction, head_mvmt,... - quad_position]; - - %mvmt = [top_view_ME, side_view_ME,quad_position]; - - - %Get missing data (from deeplabcut) - [nanrow, nancol]=find(isnan(mvmt)); length(unique(nanrow))/length(lbls) - %We get ~70% missing data because Hooke spends a lot of time in a - %tiny corner. - idx_to_keep = setdiff(1:length(lbls), unique(nanrow)); - - %Remove missing data - Y{sig} = Spike_rasters_final; - Y_final{sig} = Y{sig}(idx_to_keep,:); - lbls_final = removecats(lbls(idx_to_keep)); - top_view_ME_final = zscore(top_view_ME(idx_to_keep)); - side_view_ME_final = zscore(side_view_ME(idx_to_keep)); - logger_top_x_final = zscore(logger_top_x(idx_to_keep)); - logger_top_y_final = zscore(logger_top_y(idx_to_keep)); - mvmt_logger_top_x_final = zscore(mvmt_logger_top_x(idx_to_keep)); - mvmt_logger_top_y_final = zscore(mvmt_logger_top_y(idx_to_keep)); - head_direction_final=zscore(head_direction(idx_to_keep)); - head_mvmt_final = zscore(head_mvmt(idx_to_keep)); - quad_position_final = quad_position(idx_to_keep); - %mvmt_final = mvmt(idx_to_keep,:); - - - %% Run regression - % Use adjusted Rsquared - - - for unit = 1:size(Y_final{sig} ,2) %for now one unit at a time. - - %Set up predictor matrices - X_lbls = table(lbls_final, Y_final{sig}(:,unit)); - X_mvmt = table(top_view_ME_final, side_view_ME_final,... - logger_top_x_final,logger_top_y_final,... - mvmt_logger_top_x_final, mvmt_logger_top_y_final,... - head_direction_final,head_mvmt_final,quad_position_final,... - Y_final{sig}(:,unit)); - X_FOV = table(head_direction_final,head_mvmt_final,... - Y_final{sig}(:,unit)); - X_ME = table(top_view_ME_final, side_view_ME_final,... - Y_final{sig} (:,unit)); - X_all = table(lbls_final, top_view_ME_final, side_view_ME_final,... - logger_top_x_final,logger_top_y_final,... - mvmt_logger_top_x_final, mvmt_logger_top_y_final,... - head_direction_final,head_mvmt_final,quad_position_final,... - Y_final{sig}(:,unit)); - - - mdl_all = fitlm(X_all); %run linear model with all predictors for specific unit - ResultsAll.(['sigma' num2str(sig)]).(['unit' num2str(unit)])= mdl_all; - Adj_rsq_all(sig, unit) = mdl_all.Rsquared.Adjusted; %extract adjusted Rsq - if Adj_rsq_all(sig, unit)>0.8 %these units are not biological and should be removed from the dataset - units_to_remove = [units_to_remove unit]; - Adj_rsq_all(sig, unit)=nan; - ResultsAll.(['sigma' num2str(sig)]).(['unit' num2str(unit)])=nan; - end - - - mdl_behav = fitlm(X_lbls); %run linear model with behavior only for specific unit - ResultsBehav.(['sigma' num2str(sig)]).(['unit' num2str(unit)])= mdl_behav; - Adj_rsq_behav(sig, unit) = mdl_behav.Rsquared.Adjusted; - if Adj_rsq_behav(sig, unit)>0.8 - Adj_rsq_behav(sig, unit)=nan; - ResultsBehav.(['sigma' num2str(sig)]).(['unit' num2str(unit)])=nan; - end - - % %Test shuffling method - % idx_shuffle = randperm(size(X_lbls,1)); - % X_lbls_shuffle = X_all; X_lbls_shuffle(:,2:end-1) = X_lbls_shuffle(idx_shuffle,2:end-1); - % mdl_behav_shuffle = fitlm(X_lbls); %run linear model with behavior only for specific unit - % Adj_rsq_behav_shuffle(sig, unit) = mdl_behav_shuffle.Rsquared.Adjusted; - % IMPORTANT NOTE: this leads to exactly the same result! - - mdl_mvmt = fitlm(X_mvmt); %run linear model with mvmt only for specific unit - ResultsMvmt.(['sigma' num2str(sig)]).(['unit' num2str(unit)])= mdl_mvmt; - Adj_rsq_mvmt(sig, unit) = mdl_mvmt.Rsquared.Adjusted; - if Adj_rsq_mvmt(sig, unit)>0.8 - Adj_rsq_mvmt(sig, unit)=nan; - ResultsMvmt.(['sigma' num2str(sig)]).(['unit' num2str(unit)])=nan; - end - - mdl_ME = fitlm(X_ME); %run linear model with mvmt only for specific unit - Adj_rsq_ME(sig, unit) = mdl_ME.Rsquared.Adjusted; - if Adj_rsq_ME(sig, unit)>0.8 - Adj_rsq_ME(sig, unit)=nan; - end - - if mod(unit,10)==0 - disp(unit) - end - - end - - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%') - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%') - disp(sigma) - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%') - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%') - - end - - %Change savePath for all session results folder: - cd(savePath); - save('LinearReg_results.mat','ResultsAll','ResultsBehav','ResultsMvmt','Adj_rsq_all','Adj_rsq_behav','Adj_rsq_mvmt','Adj_rsq_ME', 'brain_label','sigma_list') - load('LinearReg_results.mat') - - %plot fitted data: - sig=3; unit=40; - %find(Adj_rsq_all(sig,:)>0.6 & Adj_rsq_all(sig,:)<0.8) - %[M unit]=max(Adj_rsq_all(sig,:)) - - %using behavior only - mdl=ResultsBehav.(['sigma' num2str(sig)]).(['unit' num2str(unit)]); - Y_pred = mdl.Fitted; Y_real = Y_final{sig} (:,unit); - figure; hold on - plot(Y_real) - plot(Y_pred) - title(['Behavior only, Rsq ' num2str(Adj_rsq_behav(sig,unit))]) - - %Using mvmt only - mdl=ResultsMvmt.(['sigma' num2str(sig)]).(['unit' num2str(unit)]); - Y_residual = mdl.Residuals.Standardized; - Y_pred = mdl.Fitted; Y_real = Y_final{sig}(:,unit); - figure; hold on - plot(Y_real) - plot(Y_pred) - plot(Y_residual) - title(['Movement only, Rsq ' num2str(Adj_rsq_mvmt(sig,unit))]) - %IMPORTANT NOTE: the weird part here is that movement seems highly - %correlated with behaviora as well... - - - %Extract unique contributions - unique_behav_contribution = Adj_rsq_all - Adj_rsq_mvmt; - unique_mvmt_contribution = Adj_rsq_all - Adj_rsq_behav; - - TEO_units = find(strcmp(brain_label,'TEO')); - vlPFC_units = find(strcmp(brain_label,'vlPFC')); - - sig = 1; - - %TEO - figure; hold on - - subplot(2,2,1); hold on - boxplot([Adj_rsq_all(sig,TEO_units); Adj_rsq_behav(sig,TEO_units); Adj_rsq_mvmt(sig,TEO_units); Adj_rsq_ME(sig,TEO_units)]') - ylabel('Adjusted Rsq'); ylim([0 1]) - xticks([1:4]); xlim([0.5 4.5]) - xticklabels({'all','Behavior','movement + field of view', 'ME'}) - ax = gca; - ax.FontSize = 14; - title(['TEO, sigma = ' num2str(sigma_list(sig)) 's']) - - subplot(2,2,2); hold on - boxplot([unique_behav_contribution(sig,TEO_units); unique_mvmt_contribution(sig,TEO_units)]') - ylabel('Unique explained variance'); ylim([0 1]) - xticks([1:2]); xlim([0.5 2.5]) - xticklabels({'Behavior','Movement'}) - ax = gca; - ax.FontSize = 14; - title(['TEO unique contribution, sigma = ' num2str(sigma_list(sig)) 's']) - - %vlPFC - subplot(2,2,3); hold on - boxplot([Adj_rsq_all(sig,vlPFC_units); Adj_rsq_behav(sig,vlPFC_units); Adj_rsq_mvmt(sig,vlPFC_units); Adj_rsq_ME(sig,vlPFC_units)]') - ylabel('Adjusted Rsq'); ylim([0 1]) - xticks([1:4]); xlim([0.5 4.5]) - xticklabels({'all','Behavior','movement + field of view', 'ME'}) - ax = gca; - ax.FontSize = 14; - title(['vlPFC, sigma = ' num2str(sigma_list(sig)) 's']) - - subplot(2,2,4); hold on - boxplot([unique_behav_contribution(sig,vlPFC_units); unique_mvmt_contribution(sig,vlPFC_units)]') - ylabel('Unique explained variance'); ylim([0 1]) - xticks([1:2]); xlim([0.5 2.5]) - xticklabels({'Behavior','Movement'}) - ax = gca; - ax.FontSize = 14; - title(['vlPFC unique contribution, sigma = ' num2str(sigma_list(sig)) 's']) -end - -%IMPORTANT NOTES: -% 1. Smoothing helps model fit for both movement and behavior. -% 2. Movement full and unique contributions are smaller than behavior. - - diff --git a/Movement analyses/old_code/log_mvmt_regression_perUnit_allsession.m b/Movement analyses/old_code/log_mvmt_regression_perUnit_allsession.m deleted file mode 100644 index 0646fe0..0000000 --- a/Movement analyses/old_code/log_mvmt_regression_perUnit_allsession.m +++ /dev/null @@ -1,306 +0,0 @@ -%% log_mvmt_regression -% Run a multinear regression to figure out the proportion of variance -% explained by the different predictors - - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 30; %frame rate -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution; %set the smoothing window size (sigma) -sigma_list= 10;%[1/temp_resolution, 1, 10, 30]; -num_iter = 500; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=15; units_to_remove = []; -for s =15%session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/Mvmt_results']; - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; channel_flag = "all"; - - for sig = 1:length(sigma_list) - - - sigma = sigma_list(sig)*temp_resolution; - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - - cd(filePath) - - %Load ME - load('hooke0819_motion_energy.mat') - top_view_ME = [0; top_view_ME]; side_view_ME = [0; side_view_ME]; - - %Load DLC - dlc_alone = readtable('hooke0819_dlc_head_alone.csv');% Load DLC key point data - dlc_paired_tdl = readtable('hooke0819_dlc_head_paired_tdl.csv'); - dlc_paired_tdr = readtable('hooke0819_dlc_head_paired_tdr.csv'); - - size(Spike_rasters,2)-(size(dlc_alone,1)+size(dlc_paired_tdr,1)) - dlc=dlc(1:end-1,:); %There is an extra datapoint than frame.. for now ignore the first data point - - logger_bottom = table2array(dlc(:,2:4)); logger_bottom(logger_bottom(:,3)<0.8,1:2)=nan; - logger_top = table2array(dlc(:,5:7)); logger_top(logger_top(:,3)<0.8,1:2)=nan; - nose = table2array(dlc(:,8:10)); nose(nose(:,3)<0.8,1:2)=nan; - - %Load head derived measures - head_derived_measures = load('hooke0819_head_direction.mat'); - head_direction = head_derived_measures.head_orientation_dlc; head_direction = head_direction(1:end-1,:); - quad_position = head_derived_measures.updown_position; quad_position = quad_position(1:end-1,:); - - disp('Data Loaded') - - - - %% Pool all the data from the alone block - - % Get alone block - %For behavior labels - lbls = cell2mat(labels(:,3)); - lbls=lbls(1:size(top_view_ME,1)); - lbls = categorical(lbls); - - tabulate(lbls) - - %For spike data - Spike_rasters_final = zscore(Spike_rasters(:,1:size(top_view_ME,1)),0,2)'; - - %Combine mvmt predictors - logger_top_x = logger_top(:,1); - logger_top_y = logger_top(:,2); - mvmt_logger_top_x = [0; diff(logger_top(:,1))]; - mvmt_logger_top_y = [0; diff(logger_top(:,2))]; - head_mvmt = [0; diff(head_direction)]; - - mvmt = [top_view_ME, side_view_ME,... - logger_top_x, logger_top_y,... - mvmt_logger_top_x, mvmt_logger_top_y,... - head_direction, head_mvmt,... - quad_position]; - - %mvmt = [top_view_ME, side_view_ME,quad_position]; - - - %Get missing data (from deeplabcut) - [nanrow, nancol]=find(isnan(mvmt)); length(unique(nanrow))/length(lbls) - %We get ~70% missing data because Hooke spends a lot of time in a - %tiny corner. - idx_to_keep = setdiff(1:length(lbls), unique(nanrow)); - - %Remove missing data - Y{sig} = Spike_rasters_final; - Y_final{sig} = Y{sig}(idx_to_keep,:); - lbls_final = removecats(lbls(idx_to_keep)); - top_view_ME_final = zscore(top_view_ME(idx_to_keep)); - side_view_ME_final = zscore(side_view_ME(idx_to_keep)); - logger_top_x_final = zscore(logger_top_x(idx_to_keep)); - logger_top_y_final = zscore(logger_top_y(idx_to_keep)); - mvmt_logger_top_x_final = zscore(mvmt_logger_top_x(idx_to_keep)); - mvmt_logger_top_y_final = zscore(mvmt_logger_top_y(idx_to_keep)); - head_direction_final=zscore(head_direction(idx_to_keep)); - head_mvmt_final = zscore(head_mvmt(idx_to_keep)); - quad_position_final = quad_position(idx_to_keep); - %mvmt_final = mvmt(idx_to_keep,:); - - - %% Run regression - % Use adjusted Rsquared - - - for unit = 1:size(Y_final{sig} ,2) %for now one unit at a time. - - %Set up predictor matrices - X_lbls = table(lbls_final, Y_final{sig}(:,unit)); - X_mvmt = table(top_view_ME_final, side_view_ME_final,... - logger_top_x_final,logger_top_y_final,... - mvmt_logger_top_x_final, mvmt_logger_top_y_final,... - head_direction_final,head_mvmt_final,quad_position_final,... - Y_final{sig}(:,unit)); - X_ME = table(top_view_ME_final, side_view_ME_final,... - Y_final{sig} (:,unit)); - X_all = table(lbls_final, top_view_ME_final, side_view_ME_final,... - logger_top_x_final,logger_top_y_final,... - mvmt_logger_top_x_final, mvmt_logger_top_y_final,... - head_direction_final,head_mvmt_final,quad_position_final,... - Y_final{sig}(:,unit)); - - - mdl_all = fitlm(X_all); %run linear model with all predictors for specific unit - ResultsAll.(['sigma' num2str(sig)]).(['unit' num2str(unit)])= mdl_all; - Adj_rsq_all(sig, unit) = mdl_all.Rsquared.Adjusted; %extract adjusted Rsq - if Adj_rsq_all(sig, unit)>0.8 %these units are not biological and should be removed from the dataset - units_to_remove = [units_to_remove unit]; - Adj_rsq_all(sig, unit)=nan; - ResultsAll.(['sigma' num2str(sig)]).(['unit' num2str(unit)])=nan; - end - - - mdl_behav = fitlm(X_lbls); %run linear model with behavior only for specific unit - ResultsBehav.(['sigma' num2str(sig)]).(['unit' num2str(unit)])= mdl_behav; - Adj_rsq_behav(sig, unit) = mdl_behav.Rsquared.Adjusted; - if Adj_rsq_behav(sig, unit)>0.9 - Adj_rsq_behav(sig, unit)=nan; - ResultsBehav.(['sigma' num2str(sig)]).(['unit' num2str(unit)])=nan; - end - - % %Test shuffling method - % idx_shuffle = randperm(size(X_lbls,1)); - % X_lbls_shuffle = X_all; X_lbls_shuffle(:,2:end-1) = X_lbls_shuffle(idx_shuffle,2:end-1); - % mdl_behav_shuffle = fitlm(X_lbls); %run linear model with behavior only for specific unit - % Adj_rsq_behav_shuffle(sig, unit) = mdl_behav_shuffle.Rsquared.Adjusted; - % IMPORTANT NOTE: this leads to exactly the same result! - - mdl_mvmt = fitlm(X_mvmt); %run linear model with mvmt only for specific unit - ResultsMvmt.(['sigma' num2str(sig)]).(['unit' num2str(unit)])= mdl_mvmt; - Adj_rsq_mvmt(sig, unit) = mdl_mvmt.Rsquared.Adjusted; - if Adj_rsq_mvmt(sig, unit)>0.9 - Adj_rsq_mvmt(sig, unit)=nan; - ResultsMvmt.(['sigma' num2str(sig)]).(['unit' num2str(unit)])=nan; - end - - mdl_ME = fitlm(X_ME); %run linear model with mvmt only for specific unit - Adj_rsq_ME(sig, unit) = mdl_ME.Rsquared.Adjusted; - if Adj_rsq_ME(sig, unit)>0.9 - Adj_rsq_ME(sig, unit)=nan; - end - - if mod(unit,10)==0 - disp(unit) - end - - end - - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%') - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%') - disp(sigma) - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%') - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%') - - end - - %Change savePath for all session results folder: - cd(savePath); - save('LinearReg_results.mat','ResultsAll','ResultsBehav','ResultsMvmt','Adj_rsq_all','Adj_rsq_behav','Adj_rsq_mvmt','Adj_rsq_ME', 'brain_label','sigma_list') - load('LinearReg_results.mat') - - %plot fitted data: - sig=3; unit=40; - %find(Adj_rsq_all(sig,:)>0.6 & Adj_rsq_all(sig,:)<0.8) - %[M unit]=max(Adj_rsq_all(sig,:)) - - %using behavior only - mdl=ResultsBehav.(['sigma' num2str(sig)]).(['unit' num2str(unit)]); - Y_pred = mdl.Fitted; Y_real = Y_final{sig} (:,unit); - figure; hold on - plot(Y_real) - plot(Y_pred) - title(['Behavior only, Rsq ' num2str(Adj_rsq_behav(sig,unit))]) - - %Using mvmt only - mdl=ResultsMvmt.(['sigma' num2str(sig)]).(['unit' num2str(unit)]); - Y_residual = mdl.Residuals.Standardized; - Y_pred = mdl.Fitted; Y_real = Y_final{sig}(:,unit); - figure; hold on - plot(Y_real) - plot(Y_pred) - plot(Y_residual) - title(['Movement only, Rsq ' num2str(Adj_rsq_mvmt(sig,unit))]) - %IMPORTANT NOTE: the weird part here is that movement seems highly - %correlated with behaviora as well... - - - %Extract unique contributions - unique_behav_contribution = Adj_rsq_all - Adj_rsq_mvmt; - unique_mvmt_contribution = Adj_rsq_all - Adj_rsq_behav; - - TEO_units = find(strcmp(brain_label,'TEO')); - vlPFC_units = find(strcmp(brain_label,'vlPFC')); - - sig = 3; - - %TEO - figure; hold on - boxplot([Adj_rsq_all(sig,TEO_units); Adj_rsq_behav(sig,TEO_units); Adj_rsq_mvmt(sig,TEO_units); Adj_rsq_ME(sig,TEO_units)]') - ylabel('Adjusted Rsq') - xticks([1:4]); xlim([0.5 4.5]) - xticklabels({'all','Behavior','movement + field of view', 'ME'}) - ax = gca; - ax.FontSize = 14; - title(['TEO, sigma = ' num2str(sigma_list(sig)) 's']) - - figure; hold on - boxplot([unique_behav_contribution(sig,TEO_units); unique_mvmt_contribution(sig,TEO_units)]') - ylabel('Unique explained variance') - xticks([1:2]); xlim([0.5 2.5]) - xticklabels({'Behavior','Movement'}) - ax = gca; - ax.FontSize = 14; - title(['TEO unique contribution, sigma = ' num2str(sigma_list(sig)) 's']) - - %vlPFC - figure; hold on - boxplot([Adj_rsq_all(sig,vlPFC_units); Adj_rsq_behav(sig,vlPFC_units); Adj_rsq_mvmt(sig,vlPFC_units); Adj_rsq_ME(sig,vlPFC_units)]') - ylabel('Adjusted Rsq') - xticks([1:4]); xlim([0.5 4.5]) - xticklabels({'all','Behavior','movement + field of view', 'ME'}) - ax = gca; - ax.FontSize = 14; - title(['vlPFC, sigma = ' num2str(sigma_list(sig)) 's']) - - figure; hold on - boxplot([unique_behav_contribution(sig,vlPFC_units); unique_mvmt_contribution(sig,vlPFC_units)]') - ylabel('Unique explained variance') - xticks([1:2]); xlim([0.5 2.5]) - xticklabels({'Behavior','Movement'}) - ax = gca; - ax.FontSize = 14; - title(['vlPFC unique contribution, sigma = ' num2str(sigma_list(sig)) 's']) -end - -%IMPORTANT NOTES: -% 1. Smoothing helps model fit for both movement and behavior. -% 2. Movement full and unique contributions are smaller than behavior. - - diff --git a/Movement analyses/old_code/log_mvmt_regression_perUnit_perReg.m b/Movement analyses/old_code/log_mvmt_regression_perUnit_perReg.m deleted file mode 100644 index fa7212d..0000000 --- a/Movement analyses/old_code/log_mvmt_regression_perUnit_perReg.m +++ /dev/null @@ -1,261 +0,0 @@ -%% log_mvmt_regression_perReg -% Run a multinear regression to figure out the proportion of variance -% explained by the different predictors - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 29.97; %frame rate -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution; %set the smoothing window size (sigma) -%sigma_list= [1/temp_resolution, 1, 10, 30]; -num_iter = 500; -agg_precedence = 1; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; units_to_remove = []; -for s =15%session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/Mvmt_results']; - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; channel_flag = "all"; - - %for sig = 1:length(sigma_list) -% sigma = sigma_list(sig)*temp_resolution; - - %% Get data with specified temporal resolution and channels - - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - cd(filePath) - - %Load DLC - dlc = readtable('mvmt_data.csv');% Load DLC key point data - length(find(sum(isnan(table2array(dlc)),2)==0))/size(dlc,1) %proportion of full data - - %Trim neural data and behavioral to align with video data - camera_start_time = behavior_log{strcmp(behavior_log{:,'Behavior'},"Camera Sync"),"start_time_round"}; - camera_end_time = camera_start_time + size(dlc,1) -1; - Spike_rasters_trimmed = Spike_rasters(:,camera_start_time:camera_end_time); - labels_trimmed = labels(camera_start_time:camera_end_time,:); - -% % % %Load ME -% % % load('hooke0819_motion_energy.mat') -% % % top_view_ME = [0; top_view_ME]; side_view_ME = [0; side_view_ME]; - - - disp('Data Loaded') - - - - %% Pool all the data from the alone block - - % Get alone block - %For behavior labels - lbls = cell2mat(labels_trimmed(:,3)); - lbls = categorical(lbls); - - tabulate(lbls) - - %For spike data - Spike_rasters_final = zscore(Spike_rasters_trimmed,0,2)'; - - %Combine mvmt predictors - logger_top_x = logger_top(:,1); - logger_top_y = logger_top(:,2); - mvmt_logger_top_x = [0; diff(logger_top(:,1))]; - mvmt_logger_top_y = [0; diff(logger_top(:,2))]; - head_mvmt = [0; diff(head_direction)]; - - mvmt = [top_view_ME, side_view_ME,... - logger_top_x, logger_top_y,... - mvmt_logger_top_x, mvmt_logger_top_y,... - head_direction, head_mvmt,... - quad_position]; - - %mvmt = [top_view_ME, side_view_ME,quad_position]; - - - %Get missing data (from deeplabcut) - [nanrow, nancol]=find(isnan(mvmt)); length(unique(nanrow))/length(lbls) - %We get ~70% missing data because Hooke spends a lot of time in a - %tiny corner. - idx_to_keep = setdiff(1:length(lbls), unique(nanrow)); - - %Remove missing data - Y = Spike_rasters_final; - Y_final = Y(idx_to_keep,:); - lbls_final = removecats(lbls(idx_to_keep)); - top_view_ME_final = zscore(top_view_ME(idx_to_keep)); - side_view_ME_final = zscore(side_view_ME(idx_to_keep)); - logger_top_x_final = zscore(logger_top_x(idx_to_keep)); - logger_top_y_final = zscore(logger_top_y(idx_to_keep)); - mvmt_logger_top_x_final = zscore(mvmt_logger_top_x(idx_to_keep)); - mvmt_logger_top_y_final = zscore(mvmt_logger_top_y(idx_to_keep)); - head_direction_final=zscore(head_direction(idx_to_keep)); - head_mvmt_final = zscore(head_mvmt(idx_to_keep)); - quad_position_final = quad_position(idx_to_keep); - %mvmt_final = mvmt(idx_to_keep,:); - - - %% Run regression - % Use adjusted Rsquared - - - for unit = 1:size(Y_final ,2) %for now one unit at a time. - - %Set up predictor matrix - X_all = table(lbls_final, top_view_ME_final, side_view_ME_final,... - logger_top_x_final,logger_top_y_final,... - mvmt_logger_top_x_final, mvmt_logger_top_y_final,... - head_direction_final,head_mvmt_final,quad_position_final,... - Y_final(:,unit)); - - - %Run model for all predictors - mdl_all = fitlm(X_all); %run linear model with all predictors for specific unit - ResultsAll.(['sigma' num2str(sig)]).(['unit' num2str(unit)])= mdl_all; - Adj_rsq_all(unit,sig) = mdl_all.Rsquared.Adjusted; %extract adjusted Rsq - - if Adj_rsq_all(unit,sig)>0.8 || Adj_rsq_all(unit,sig)<0 %these units are not biological and should be removed from the dataset - - units_to_remove = [units_to_remove unit]; - Adj_rsq_all(unit,sig)=nan; - ResultsAll.(['sigma' num2str(sig)]).(['unit' num2str(unit)])=nan; - Full_rsq_perReg{sig}(unit, pred)=nan; - Unq_rsq_perReg{sig}(unit, pred) =nan; - - else - - for pred = 1:size(X_all, 2)-1 % for all predictors - - %Full contribution per regressor - mdl = fitlm(X_all(:,[pred,size(X_all, 2)])); %run linear model with only one predictor - ResultsFull.(['unit' num2str(unit)]).(['pred' num2str(pred)])= mdl; - Full_rsq_perReg{sig}(unit, pred) = mdl.Rsquared.Adjusted; %extract adjusted Rsq - - %Unique contribution per regressor - idx=setdiff(1:size(X_all,2),pred); - mdl = fitlm(X_all(:,idx)); %run linear model with all EXCEPT one predictor - ResultsUnq.(['unit' num2str(unit)]).(['pred' num2str(pred)])= mdl; - Unq_rsq_perReg{sig}(unit, pred) = Adj_rsq_all(unit,sig) - mdl.Rsquared.Adjusted; %extract adjusted Rsq - - end - end - - if mod(unit,10)==0 - disp(unit) - end - - end - - - -% disp('%%%%%%%%%%%%%%%%%%%%%%%%%%') -% disp('%%%%%%%%%%%%%%%%%%%%%%%%%%') -% disp(sigma) -% disp('%%%%%%%%%%%%%%%%%%%%%%%%%%') -% disp('%%%%%%%%%%%%%%%%%%%%%%%%%%') - -% end - - %Change savePath for all session results folder: - cd(savePath); - save('LinearReg_results_perReg.mat','ResultsFull','ResultsUnq','Adj_rsq_all','Full_rsq_perReg','Unq_rsq_perReg', 'brain_label','sigma_list') - load('LinearReg_results_perReg.mat') - - %Set unit label and plotting parameters - sig=2; - TEO_units = find(strcmp(brain_label,'TEO')); - vlPFC_units = find(strcmp(brain_label,'vlPFC')); - pred_labels = {'Behavior','ME_top','ME_side','position_x','position_y',... - 'mvmt_x','mvmt_y','head_direction (FOV)','Change in FOV','quad_position','all'}; - - %TEO - figure; set(gcf,'Position',[150 250 1000 700]); hold on - subplot(2,2,1); hold on - [~, idx_sorted]=sort(nanmean(Full_rsq_perReg{sig}(TEO_units,:))); - boxplot([Full_rsq_perReg{sig}(TEO_units,idx_sorted),Adj_rsq_all(TEO_units,sig)]) - ylabel('Full Rsq'); ylim([0 0.5]) - xticks([1:11]); xlim([0.5 11.5]) - xticklabels(pred_labels([idx_sorted,length(pred_labels)])) - ax = gca; - ax.FontSize = 14; - title(['TEO units, sigma = ' num2str(sigma_list(sig)) 's']) - - subplot(2,2,2); hold on - [~, idx_sorted]=sort(nanmean(Unq_rsq_perReg{sig}(TEO_units,:))); - boxplot([Unq_rsq_perReg{sig}(TEO_units,idx_sorted)]) - ylabel('Unique Rsq'); ylim([0 0.5]) - xticks([1:10]); xlim([0.5 10.5]) - xticklabels(pred_labels(idx_sorted)) - ax = gca; - ax.FontSize = 14; - title(['TEO units, sigma = ' num2str(sigma_list(sig)) 's']) - - %vlPFC - [~, idx_sorted]=sort(nanmean(Full_rsq_perReg{sig}(vlPFC_units,:))); - subplot(2,2,3); hold on - boxplot([Full_rsq_perReg{sig}(vlPFC_units,idx_sorted),Adj_rsq_all(vlPFC_units,sig)]) - ylabel('Full Rsq'); ylim([0 0.5]) - xticks([1:11]); xlim([0.5 11.5]) - xticklabels(pred_labels([idx_sorted,length(pred_labels)])) - ax = gca; - ax.FontSize = 14; - title(['vlPFC units, sigma = ' num2str(sigma_list(sig)) 's']) - - [~, idx_sorted]=sort(nanmean(Unq_rsq_perReg{sig}(vlPFC_units,:))); - subplot(2,2,4); hold on - boxplot([Unq_rsq_perReg{sig}(vlPFC_units,idx_sorted)]) - ylabel('Unique Rsq'); ylim([0 0.5]) - xticks([1:10]); xlim([0.5 10.5]) - xticklabels(pred_labels(idx_sorted)) - ax = gca; - ax.FontSize = 14; - title(['vlPFC units, sigma = ' num2str(sigma_list(sig)) 's']) - - saveas(gcf,['Neural variance explained by mvmt vs. behavior.pdf']) -end - -%IMPORTANT NOTES: -% 1. Smoothing helps model fit for both movement and behavior. -% 2. Movement full and unique contributions are smaller than behavior. - - diff --git a/Movement analyses/old_code/log_mvmt_regression_perUnit_perReg_Lagged.m b/Movement analyses/old_code/log_mvmt_regression_perUnit_perReg_Lagged.m deleted file mode 100644 index fcc3f0d..0000000 --- a/Movement analyses/old_code/log_mvmt_regression_perUnit_perReg_Lagged.m +++ /dev/null @@ -1,287 +0,0 @@ -%% log_mvmt_regression_perReg -% Run a multinear regression to figure out the proportion of variance -% explained by the different predictors - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 29.97; %frame rate -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution; %set the smoothing window size (sigma) -%sigma_list= [1/temp_resolution, 1, 10, 30]; -num_iter = 500; -agg_precedence = 1; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; units_to_remove = []; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/Mvmt_results']; - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; channel_flag = "all"; - - %% Get data with specified temporal resolution and channels - - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - cd(filePath) - - %Load DLC - dlc = readtable('mvmt_data.csv');% Load DLC key point data - length(find(sum(isnan(table2array(dlc)),2)==0))/size(dlc,1) %proportion of full data - - %Trim neural data and behavioral to align with video data - camera_start_time = behavior_log{strcmp(behavior_log{:,'Behavior'},"Camera Sync"),"start_time_round"}; - camera_end_time = camera_start_time + size(dlc,1) -1; - Spike_rasters_trimmed = Spike_rasters(:,camera_start_time:camera_end_time); - labels_trimmed = labels(camera_start_time:camera_end_time,:); - -% % % %Load ME -% % % load('hooke0819_motion_energy.mat') -% % % top_view_ME = [0; top_view_ME]; side_view_ME = [0; side_view_ME]; - - - disp('Data Loaded') - - - - %% Pool all the data from the alone block - - % Get alone block - %For behavior labels - lbls = cell2mat(labels_trimmed(:,3)); - lbls = categorical(lbls); - - tabulate(lbls) - - %For spike data - Spike_rasters_final = zscore(Spike_rasters_trimmed,0,2)'; - - %Combine mvmt predictors - mvmt = table2array(dlc); - %mvmt = [top_view_ME, side_view_ME,quad_position]; - - %Set regressor groups: - regGroup{1} = 1; %behavior labels - regGroup{2} = [2,3,4,5]; %position in quad - regGroup{3} = 6; % head direction or field of view - regGroup{4} = [7,8]; % head movements - regGroup{5} = 2:8; %all movements - - - %Get missing data (from deeplabcut) - [nanrow, nancol]=find(isnan(mvmt)); length(unique(nanrow))/length(lbls) - %We get ~70% missing data because Hooke spends a lot of time in a - %tiny corner. - idx_to_keep = setdiff(1:length(lbls), unique(nanrow)); - - %Remove missing data - Y = Spike_rasters_final; - Y_final = Y(idx_to_keep,:); - lbls_final = removecats(lbls(idx_to_keep)); - mvmt_final = mvmt(idx_to_keep,:); - - - %% Run regression - % Use adjusted Rsquared - - %lag movement matrix - lags= 0%[-1000 :100: 1000]; - for l=1:length(lags) - - mvmt_final_lagged = lagmatrix(mvmt_final,lags(l)); - - - for unit = 1:50%size(Y_final ,2) %for now one unit at a time. - - %unroll mvmt predictors (format for the multilinear regression) - toplogger_x = mvmt_final_lagged(:,1); - toplogger_y = mvmt_final_lagged(:,2); - bottomlogger_x = mvmt_final_lagged(:,3); - bottomlogger_y = mvmt_final_lagged(:,4); - head_orientation_dlc = mvmt_final_lagged(:,5); - dist_traveled = mvmt_final_lagged(:,6); - acceleration = mvmt_final_lagged(:,7); - - %Set up predictor matrix - X_all = table(toplogger_x, toplogger_y,... - bottomlogger_x, bottomlogger_y, head_orientation_dlc,... - dist_traveled, acceleration, Y_final(:,unit)); - - X_all=X_all(randperm(size(X_all,1)),:); - - %Run model for all predictors - mdl_all = fitlm(X_all); %run linear model with all predictors for specific unit - Adj_rsq_all{s,l}(unit) = mdl_all.Rsquared.Adjusted; %extract adjusted Rsq - -% if Adj_rsq_all{s}(unit)>0.8 || Adj_rsq_all{s}(unit)<0 %these units are not biological and should be removed from the dataset -% -% units_to_remove = [units_to_remove unit]; -% Adj_rsq_all{s}(unit)=nan; -% Full_rsq_perReg{s}(unit, pred)=nan; -% Unq_rsq_perReg{s}(unit, pred) =nan; -% -% else -% -% for pred = 1:size(regGroup,2) % for all predictors -% -% %Full contribution per regressor -% mdl = fitlm(X_all(:,[regGroup{pred},size(X_all, 2)])); %run linear model with only one predictor -% Full_rsq_perReg{s,l}(unit, pred) = mdl.Rsquared.Adjusted; %extract adjusted Rsq -% -% %Unique contribution per regressor -% idx=setdiff(1:size(X_all,2),regGroup{pred}); -% mdl = fitlm(X_all(:,idx)); %run linear model with all EXCEPT one predictor -% Unq_rsq_perReg{s,l}(unit, pred) = Adj_rsq_all{s}(unit) - mdl.Rsquared.Adjusted; %extract adjusted Rsq -% -% end -% end -% -% if mod(unit,10)==0 -% disp(unit) -% end -% - end - - disp(lags(l)) - end - - clear avg_full_fit_allRegs - for l=1:length(lags) - avg_full_fit_allRegs(l) = mean(Adj_rsq_all{s,l}); -% avg_full_fit(l,:)=mean(Full_rsq_perReg{s,l}); -% sd_full_fit(l,:)=std(Full_rsq_perReg{s,l}); -% avg_unq_fit(l,:)=mean(Unq_rsq_perReg{s,l}); - end -% figure; hold on -% plot(avg_full_fit(:,2:end)) -% %xticks(''); xticklabels(string(lags)) -% xline(16,'LineStyle','--') -% xlabel('Frames') - - [~, idx]=max(avg_full_fit_allRegs) - - figure; hold on - plot(avg_full_fit_allRegs) - xline(round(length(lags)/2),'LineStyle','--') - xlabel('Frames') - - - %Change savePath for all session results folder: -% cd(savePath); -% save('LinearReg_results_perReg.mat','ResultsFull','ResultsUnq','Adj_rsq_all','Full_rsq_perReg','Unq_rsq_perReg', 'brain_label') -% load('LinearReg_results_perReg.mat') - - %Set unit label and plotting parameters - TEO_units = find(strcmp(brain_label,'TEO')); - vlPFC_units = find(strcmp(brain_label,'vlPFC')); - pred_labels = {'Behavior','Position in quad','Field of view','Head mvmts','all'}; - - %Combining brain areas - figure; set(gcf,'Position',[150 250 800 400]); hold on - subplot(1,2,1); hold on - [~, idx_sorted]=sort(nanmean(Full_rsq_perReg{s})); - boxplot([Full_rsq_perReg{s}(:,idx_sorted),Adj_rsq_all{s}']) - ylabel('Full Rsq'); ylim([0 0.6]) - xticks([1: size(regGroup,2)+1]); xlim([0.5 size(regGroup,2)+1.5]) - xticklabels(pred_labels([idx_sorted,length(pred_labels)])) - ax = gca; - ax.FontSize = 14; - - subplot(1,2,2); hold on - [~, idx_sorted]=sort(nanmean(Unq_rsq_perReg{s})); - boxplot([Unq_rsq_perReg{s}(:,idx_sorted)]) - ylabel('Unique Rsq'); ylim([0 0.5]) - xticks([1: size(regGroup,2)]); xlim([0.5 size(regGroup,2)+0.5]) - xticklabels(pred_labels(idx_sorted)) - ax = gca; - ax.FontSize = 14; - -% % % % % % %%%% SEPARATE BY BRAIN AREA %%%% -% % % % % % %TEO -% % % % % % figure; set(gcf,'Position',[150 250 1000 700]); hold on -% % % % % % subplot(2,2,1); hold on -% % % % % % [~, idx_sorted]=sort(nanmean(Full_rsq_perReg{s}(TEO_units,:))); -% % % % % % boxplot([Full_rsq_perReg{s}(TEO_units,idx_sorted),Adj_rsq_all{s}(TEO_units)']) -% % % % % % ylabel('Full Rsq'); ylim([0 0.5]) -% % % % % % xticks([1: size(regGroup,2)+1]); xlim([0.5 size(regGroup,2)+1.5]) -% % % % % % xticklabels(pred_labels([idx_sorted,length(pred_labels)])) -% % % % % % ax = gca; -% % % % % % ax.FontSize = 14; -% % % % % % title(['TEO units']) -% % % % % % -% % % % % % subplot(2,2,3); hold on -% % % % % % [~, idx_sorted]=sort(nanmean(Unq_rsq_perReg{s}(TEO_units,:))); -% % % % % % boxplot([Unq_rsq_perReg{s}(TEO_units,idx_sorted)]) -% % % % % % ylabel('Unique Rsq'); ylim([0 0.5]) -% % % % % % xticks([1: size(regGroup,2)+1]); xlim([0.5 size(regGroup,2)+1.5]) -% % % % % % xticklabels(pred_labels(idx_sorted)) -% % % % % % ax = gca; -% % % % % % ax.FontSize = 14; -% % % % % % % title(['TEO units, sigma = ' num2str(sigma_list(sig)) 's']) -% % % % % % -% % % % % % %vlPFC -% % % % % % subplot(2,2,2); hold on -% % % % % % [~, idx_sorted]=sort(nanmean(Full_rsq_perReg{s}(vlPFC_units,:))); -% % % % % % boxplot([Full_rsq_perReg{s}(TEO_units,idx_sorted),Adj_rsq_all{s}(TEO_units)']) -% % % % % % ylabel('Full Rsq'); ylim([0 0.5]) -% % % % % % xticks([1: size(regGroup,2)+1]); xlim([0.5 size(regGroup,2)+1.5]) -% % % % % % xticklabels(pred_labels([idx_sorted,length(pred_labels)])) -% % % % % % ax = gca; -% % % % % % ax.FontSize = 14; -% % % % % % title(['vlPFC units']) -% % % % % % -% % % % % % subplot(2,2,4); hold on -% % % % % % [~, idx_sorted]=sort(nanmean(Unq_rsq_perReg{s}(vlPFC_units,:))); -% % % % % % boxplot([Unq_rsq_perReg{s}(TEO_units,idx_sorted)]) -% % % % % % ylabel('Unique Rsq'); ylim([0 0.5]) -% % % % % % xticks([1: size(regGroup,2)+1]); xlim([0.5 size(regGroup,2)+1.5]) -% % % % % % xticklabels(pred_labels(idx_sorted)) -% % % % % % ax = gca; -% % % % % % ax.FontSize = 14; - - saveas(gcf,[savePath 'Neural variance explained by mvmt vs. behavior.pdf']); close -end - -%IMPORTANT NOTES: -% 1. Smoothing helps model fit for both movement and behavior. -% 2. Movement full and unique contributions are smaller than behavior. - - diff --git a/Movement analyses/old_code/log_mvmt_regression_perUnit_perReg_v2.m b/Movement analyses/old_code/log_mvmt_regression_perUnit_perReg_v2.m deleted file mode 100644 index 7c039c0..0000000 --- a/Movement analyses/old_code/log_mvmt_regression_perUnit_perReg_v2.m +++ /dev/null @@ -1,292 +0,0 @@ -%% log_mvmt_regression_perReg -% Run a multinear regression to figure out the proportion of variance -% explained by the different predictors - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 29.97; %frame rate -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution; %set the smoothing window size (sigma) -%sigma_list= [1/temp_resolution, 1, 10, 30]; -num_iter = 500; -agg_precedence = 1; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; units_to_remove = []; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/Mvmt_results']; - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; channel_flag = "all"; - - %% Get data with specified temporal resolution and channels - - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - cd(filePath) - - %Load DLC - dlc = readtable('mvmt_data.csv');% Load DLC key point data - length(find(sum(isnan(table2array(dlc)),2)==0))/size(dlc,1) %proportion of full data - - %Trim neural data and behavioral to align with video data - camera_start_time = behavior_log{strcmp(behavior_log{:,'Behavior'},"Camera Sync"),"start_time_round"}; - camera_end_time = camera_start_time + size(dlc,1) -1; - - try - Spike_rasters_trimmed = Spike_rasters(:,camera_start_time:camera_end_time); - labels_trimmed = labels(camera_start_time:camera_end_time,:); - catch %If camera went on past end of recording (occurs in 2 sessions because of an error at the end) - Spike_rasters_trimmed = Spike_rasters(:,camera_start_time:end); - labels_trimmed = labels(camera_start_time:end,:); - dlc = dlc(1:size(labels_trimmed,1),:); - end - - - disp('Data Loaded') - - - - %% Pool all the data from the alone block - - % Get alone block - %For behavior labels - lbls = cell2mat(labels_trimmed(:,3)); - block = cell2mat(labels_trimmed(:,12)); - lbls = categorical(lbls); - - tabulate(lbls) - - %For spike data - Spike_rasters_final = zscore(Spike_rasters_trimmed,0,2)'; - - %Combine mvmt predictors - mvmt = table2array(dlc); - %mvmt = [top_view_ME, side_view_ME,quad_position]; - - %Set regressor groups: - regGroup{1}=1; %behavior labels - regGroup{2} = [2,3,4,5]; %position in quad - regGroup{3} = 6; % head direction or field of view - regGroup{4} = [7,8]; % head movements - - - %Get missing data (from deeplabcut) - [nanrow, nancol]=find(isnan(mvmt)); length(unique(nanrow))/length(lbls) - %We get ~70% missing data because Hooke spends a lot of time in a - %tiny corner. - idx_to_keep = setdiff(1:length(lbls), unique(nanrow)); - - %Remove missing data - Y = Spike_rasters_final; - Y_final = Y(idx_to_keep,:); - lbls_final = removecats(lbls(idx_to_keep)); - block_final = block(idx_to_keep); - mvmt_final = mvmt(idx_to_keep,:); - - %unroll mvmt predictors (format for the multilinear regression) - toplogger_x = mvmt_final(:,1); - toplogger_y = mvmt_final(:,2); - bottomlogger_x = mvmt_final(:,3); - bottomlogger_y = mvmt_final(:,4); - head_orientation_dlc = mvmt_final(:,5); - dist_traveled = mvmt_final(:,6); - acceleration = mvmt_final(:,7); - -% % % data{s}=table(lbls_final,block_final, toplogger_x, toplogger_y,... -% % % bottomlogger_x, bottomlogger_y, head_orientation_dlc,... -% % % dist_traveled, acceleration); - -% % % %% Run regression -% % % % Use adjusted Rsquared -% % % -% % % -% % % for unit = 1:size(Y_final ,2) %for now one unit at a time. -% % % -% % % -% % % -% % % %Set up predictor matrix -% % % X_all = table(lbls_final, block_final, toplogger_x, toplogger_y,... -% % % bottomlogger_x, bottomlogger_y, head_orientation_dlc,... -% % % dist_traveled, acceleration, Y_final(:,unit)); -% % % -% % % -% % % %Run model for all predictors -% % % mdl_all = fitlm(X_all); %run linear model with all predictors for specific unit -% % % Adj_rsq_all{s}(unit) = mdl_all.Rsquared.Adjusted; %extract adjusted Rsq -% % % -% % % if Adj_rsq_all{s}(unit)>0.8 || Adj_rsq_all{s}(unit)<0 %these units are not biological and should be removed from the dataset -% % % -% % % units_to_remove = [units_to_remove unit]; -% % % Adj_rsq_all{s}(unit)=nan; -% % % Full_rsq_perReg{s}(unit, pred)=nan; -% % % Unq_rsq_perReg{s}(unit, pred) =nan; -% % % -% % % else -% % % -% % % for pred = 1:size(regGroup,2) % for all predictors -% % % -% % % %Full contribution per regressor -% % % mdl = fitlm(X_all(:,[regGroup{pred},size(X_all, 2)])); %run linear model with only one predictor -% % % Full_rsq_perReg{s}(unit, pred) = mdl.Rsquared.Adjusted; %extract adjusted Rsq -% % % -% % % %Unique contribution per regressor -% % % idx=setdiff(1:size(X_all,2),regGroup{pred}); -% % % mdl = fitlm(X_all(:,idx)); %run linear model with all EXCEPT one predictor -% % % Unq_rsq_perReg{s}(unit, pred) = Adj_rsq_all{s}(unit) - mdl.Rsquared.Adjusted; %extract adjusted Rsq -% % % -% % % end -% % % end -% % % -% % % if mod(unit,10)==0 -% % % disp(unit) -% % % end -% % % -% % % end -% % % -% % % -% % % -% % % %Change savePath for all session results folder: -% % % % cd(savePath); -% % % % save('LinearReg_results_perReg.mat','ResultsFull','ResultsUnq','Adj_rsq_all','Full_rsq_perReg','Unq_rsq_perReg', 'brain_label') -% % % % load('LinearReg_results_perReg.mat') -% % % -% % % %Set unit label and plotting parameters -% % % TEO_units = find(strcmp(brain_label,'TEO')); -% % % vlPFC_units = find(strcmp(brain_label,'vlPFC')); -% % % pred_labels = {'Behavior','Position in quad','Field of view','Head mvmts','all'}; -% % % -% % % %Combining brain areas -% % % figure; set(gcf,'Position',[150 250 800 400]); hold on -% % % subplot(1,2,1); hold on -% % % [~, idx_sorted]=sort(nanmean(Full_rsq_perReg{s})); -% % % boxplot([Full_rsq_perReg{s}(:,idx_sorted),Adj_rsq_all{s}']) -% % % ylabel('Full Rsq'); ylim([0 0.6]) -% % % xticks([1: size(regGroup,2)+1]); xlim([0.5 size(regGroup,2)+1.5]) -% % % xticklabels(pred_labels([idx_sorted,length(pred_labels)])) -% % % ax = gca; -% % % ax.FontSize = 14; -% % % -% % % subplot(1,2,2); hold on -% % % [~, idx_sorted]=sort(nanmean(Unq_rsq_perReg{s})); -% % % boxplot([Unq_rsq_perReg{s}(:,idx_sorted)]) -% % % ylabel('Unique Rsq'); ylim([0 0.5]) -% % % xticks([1: size(regGroup,2)]); xlim([0.5 size(regGroup,2)+0.5]) -% % % xticklabels(pred_labels(idx_sorted)) -% % % ax = gca; -% % % ax.FontSize = 14; - -% % % % % % %%%% SEPARATE BY BRAIN AREA %%%% -% % % % % % %TEO -% % % % % % figure; set(gcf,'Position',[150 250 1000 700]); hold on -% % % % % % subplot(2,2,1); hold on -% % % % % % [~, idx_sorted]=sort(nanmean(Full_rsq_perReg{s}(TEO_units,:))); -% % % % % % boxplot([Full_rsq_perReg{s}(TEO_units,idx_sorted),Adj_rsq_all{s}(TEO_units)']) -% % % % % % ylabel('Full Rsq'); ylim([0 0.5]) -% % % % % % xticks([1: size(regGroup,2)+1]); xlim([0.5 size(regGroup,2)+1.5]) -% % % % % % xticklabels(pred_labels([idx_sorted,length(pred_labels)])) -% % % % % % ax = gca; -% % % % % % ax.FontSize = 14; -% % % % % % title(['TEO units']) -% % % % % % -% % % % % % subplot(2,2,3); hold on -% % % % % % [~, idx_sorted]=sort(nanmean(Unq_rsq_perReg{s}(TEO_units,:))); -% % % % % % boxplot([Unq_rsq_perReg{s}(TEO_units,idx_sorted)]) -% % % % % % ylabel('Unique Rsq'); ylim([0 0.5]) -% % % % % % xticks([1: size(regGroup,2)+1]); xlim([0.5 size(regGroup,2)+1.5]) -% % % % % % xticklabels(pred_labels(idx_sorted)) -% % % % % % ax = gca; -% % % % % % ax.FontSize = 14; -% % % % % % % title(['TEO units, sigma = ' num2str(sigma_list(sig)) 's']) -% % % % % % -% % % % % % %vlPFC -% % % % % % subplot(2,2,2); hold on -% % % % % % [~, idx_sorted]=sort(nanmean(Full_rsq_perReg{s}(vlPFC_units,:))); -% % % % % % boxplot([Full_rsq_perReg{s}(TEO_units,idx_sorted),Adj_rsq_all{s}(TEO_units)']) -% % % % % % ylabel('Full Rsq'); ylim([0 0.5]) -% % % % % % xticks([1: size(regGroup,2)+1]); xlim([0.5 size(regGroup,2)+1.5]) -% % % % % % xticklabels(pred_labels([idx_sorted,length(pred_labels)])) -% % % % % % ax = gca; -% % % % % % ax.FontSize = 14; -% % % % % % title(['vlPFC units']) -% % % % % % -% % % % % % subplot(2,2,4); hold on -% % % % % % [~, idx_sorted]=sort(nanmean(Unq_rsq_perReg{s}(vlPFC_units,:))); -% % % % % % boxplot([Unq_rsq_perReg{s}(TEO_units,idx_sorted)]) -% % % % % % ylabel('Unique Rsq'); ylim([0 0.5]) -% % % % % % xticks([1: size(regGroup,2)+1]); xlim([0.5 size(regGroup,2)+1.5]) -% % % % % % xticklabels(pred_labels(idx_sorted)) -% % % % % % ax = gca; -% % % % % % ax.FontSize = 14; - -% % % saveas(gcf,[savePath 'Neural variance explained by mvmt vs. behavior.pdf']); close -end - -%IMPORTANT NOTES: -% 1. Smoothing helps model fit for both movement and behavior. -% 2. Movement full and unique contributions are smaller than behavior. - -cd(['~/Dropbox (Penn)/Datalogger/Results/All_sessions/Mvmt_results/']); -save('Regression_results.mat', "Unq_rsq_perReg","Full_rsq_perReg","Adj_rsq_all","regGroup","pred_labels") - -unq_data = cell2mat(Unq_rsq_perReg'); -full_data = cell2mat(Full_rsq_perReg'); -rsq_all = cell2mat(Adj_rsq_all)'; - -%Combining brain areas - figure; set(gcf,'Position',[150 250 800 400]); hold on - subplot(1,2,1); hold on - [~, idx_sorted]=sort(nanmean(full_data)); - boxplot([full_data(:,idx_sorted),rsq_all]) - ylabel('Full Rsq'); ylim([0 0.8]) - xticks([1: size(regGroup,2)+1]); xlim([0.5 size(regGroup,2)+1.5]) - xticklabels(pred_labels([idx_sorted,length(pred_labels)])) - ax = gca; - ax.FontSize = 14; - - subplot(1,2,2); hold on - [~, idx_sorted]=sort(nanmean(unq_data)); - boxplot([unq_data(:,idx_sorted)]) - ylabel('Unique Rsq'); ylim([0 0.5]) - xticks([1: size(regGroup,2)]); xlim([0.5 size(regGroup,2)+0.5]) - xticklabels(pred_labels(idx_sorted)) - ax = gca; - ax.FontSize = 14; \ No newline at end of file diff --git a/Movement analyses/old_code/log_preprocess_video_dlc_aloneBlock.m b/Movement analyses/old_code/log_preprocess_video_dlc_aloneBlock.m deleted file mode 100644 index ea9c683..0000000 --- a/Movement analyses/old_code/log_preprocess_video_dlc_aloneBlock.m +++ /dev/null @@ -1,177 +0,0 @@ -%% log_preprocess_video_dlc -%Script which combines movement data from multiple video angles and -%computes movement derivatives. -%C. Testard Oct 2022 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Select session range: -session_range = session_range_no_partner; -a_sessions = 1:6; h_sessions = [11:13,15:16,18]; - -c_cutoff = 0.0001; - -s=15; -for s =session_range %1:length(sessions) - - %% load dlc video output - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; - cd(filePath) - - head = table2array(readtable('hooke0819_dlc_head_alone.csv')); head=head(2:end,:); - % Toplogger (2:4); Bottomlogger (5:7); forehead (8:10) - % x,y, likelihood - bodypose = table2array(readtable('hooke0819_tr_at30cds_pose_dlc.csv')); - % Nose(2:4); left eye (5:7); right eye (8:10); left ear (11:13); - % right ear (14:16); left shoulder (17:19); right shoulder(20:22); - % left elbow (23:25); right elbow (26:28); left wrist (29:31); - % right wrist (32:34); left hip (35:37); right hip (38:40); - % left knee (41:43); right knee (44:46); left ankle (47:49) - - %Note: right ear has a lot of non-overlapping missing data. - - - - %% Extract valid timepoints - - % top and bottom logger locations - logger_top = head(:,2:4); logger_top(logger_top(:,3)=c_cutoff)); -% histogram(find(bodypose(:,certainty_columns(i))>=c_cutoff), 'FaceAlpha',0.3) -% pause(1) - end - figure; scatter(certainty_columns, validnum(certainty_columns),'filled') - find(validnum<15000) - -% % % invalidpoints = find(bodypose(:,4)=min_occurrences,1);%Get behaviors with a min number of occurrences - - %Remove behaviors we're not interested in for now - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding Rowdy Room which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize'))); - - % OR select behaviors manually - %behav =[29] ;%unique(behavior_labels); %[4,5,7,8,9,10,24];% [4:10, 23]; %[4:8,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only consider indices with behavior of interest - idx= find(ismember(behavior_labels,behav));% & ismember(block_labels,3)); - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_labels_final = block_labels(idx); - - if null - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels_final,behav_categ, temp_resolution); - behavior_labels_final = sim_behav; - end - - %% Run pca - - [umap_result]=run_umap(Spike_count_raster_final, 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 3); %Run umap to get 2d embedded states - close - - [~, pca_result]=pca(Spike_count_raster_final); %Run umap to get 2d embedded states - pca_result=pca_result(:,1:50); - close - - channel = char(channel_flag); - - - %% Run Kmeans clustering - -% eva = evalclusters(pca_result,'kmeans','CalinskiHarabasz','KList',3:20); eva.OptimalK - n_clusters = length(behav);%eva.OptimalK; %30 - idx_cluster_pca = kmeans(pca_result,n_clusters); - idx_cluster_umap = kmeans(umap_result,n_clusters); - - - %% Set colormap - Cmap = [[1 0 0];...%Aggression; red - [1 0.4 0.1];...%Approach; dark orange - [0 0 0];...%But sniff; NA - [0.3 0.7 1];...%Drinking; light blue - [0 0.7 0];...%Foraging; dark green - [1 0 1];...%Groom sollicitation; magenta - [0 1 1];...%Groom partner; cyan - [0 0 1];...%Getting groomed; dark blue - [0.8 0 0];...%Threat to partner; dark red - [1 0 0];...%Threat to subject; red - [0.9 0.9 0];...%leave; dark yellow - [0 0 0];...%Lipsmack - [0.2 0.9 0.76];...%Masturbating; turquoise - [0.7 0 1];...%Mounting; light purple - [0.9 0.5 0];...%Other monkeys vocalize; orange - [1 0.8 0.1];...%Travel; yellow orange - [0 0 0];...%Proximity; NA - [0 0 0];...%Rowdy room; NA - [1 0 0];...%SP; red - [1 0 0];...%SS; red - [0.6314 0.5059 0.0118];...%Scratch; maroon - [0.5 0.2 0.5];...%Self-groom; dark purple - [ 1 0.07 0.65];...%Submission; dark pink - [0 0.4 0.5];...%Vocalzation; blue green - [0 0 0];...%Yawning; NA - [0.8 0.8 0.8]];%Rest; grey - - Cmap_block = [[0.9 0.7 0.12];[0 0.6 0.8];[0.5 0 0]]; - - Cmap_time = copper(length(idx)); - - Cmap_kmeans = hsv(n_clusters); - - mean_activity = int32(round(rescale(mean(Spike_count_raster_final,2)),2)*100); mean_activity(mean_activity==0)=1; - std_activity = int32(round(rescale(std(Spike_count_raster_final,[],2)),2)*100);std_activity(std_activity==0)=1; - Cmap_firingRate = jet(double(max(mean_activity))); - Cmap_firingRateStd = jet(double(max(std_activity))); - %Note: mean activity correlates with variation in activity. - - - %% Plot PCA projection in 3D space - - figure; hold on; set(gcf,'Position',[150 250 1500 500]) - -% for i=1:n_clusters -% scatter3(umap_result(idx==i,1), umap_result(idx==i,2),umap_result(idx==i,3),8,Cmap_kmeans(i,:),'filled') -% xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') -% %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -% title('Block') -% set(gca,'FontSize',12); -% pause(2) -% end - - %Plot PCA results color-coded by behavior - ax1=subplot(2,2,1); - scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),8,Cmap(behavior_labels_final,:),'filled') -% scatter3(pca_result(:,1), pca_result(:,2),pca_result(:,3),8,Cmap(behavior_labels_final,:),'filled') - xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior') - set(gca,'FontSize',12); - %saveas(gcf,[savePath '/umap_supervised_ColorCodedByBehav_' channel 'Units.png']) - %pause(5) - - - %Color-coded by block - ax2=subplot(2,2,2); - scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),8,Cmap_block(block_labels_final,:),'filled') -% scatter3(pca_result(:,1), pca_result(:,2),pca_result(:,3),8,Cmap_block(block_labels_final,:),'filled') - xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Block') - set(gca,'FontSize',12); - - - %Color-coded by kmeans cluster in pca space - ax3=subplot(2,2,3); - scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),8,Cmap_kmeans(idx_cluster_pca,:),'filled') -% scatter3(pca_result(:,1), pca_result(:,2),pca_result(:,3),8,Cmap_kmeans(idx_cluster,:),'filled') - xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Kmeans cluster in pca space') - set(gca,'FontSize',12); - - %Color-coded by kmeans cluster in umap space - ax4=subplot(2,2,4); - scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),8,Cmap_kmeans(idx_cluster_umap,:),'filled') -% scatter3(pca_result(:,1), pca_result(:,2),pca_result(:,3),8,Cmap_kmeans(idx_cluster,:),'filled') - xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Kmeans cluster in umap space') - set(gca,'FontSize',12); - -% %Color-coded by time -% ax2=subplot(1,2,2); -% scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),8,Cmap_time,'filled') -% xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') -% %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -% title('Time') -% set(gca,'FontSize',12); - -% %Color-coded by mean activity -% ax2=subplot(1,2,2); -% scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),8,Cmap_firingRate(mean_activity,:),'filled') -% xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') -% %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -% title('Mean activity') -% set(gca,'FontSize',12); - -% %Color-coded by std activity -% ax3=subplot(1,3,3); -% scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),8,Cmap_firingRateStd(std_activity,:),'filled') -% xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') -% %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -% title('Variation in of activity') -% set(gca,'FontSize',12); -% -% %sgtitle([channel ' units, PCA, ' sessions(s).name]) - - hlink = linkprop([ax1,ax2,ax3,ax4],{'CameraPosition','CameraUpVector'}); - rotate3d on - - %savefig([savePath 'Umap_3Dprojection_' channel '.fig']) - %saveas(gcf,[savePath '/umap_ColorCodedByBlock_' channel 'Units.png']) - - - % % % % %plot by cluster - % % % % [cls, pnode] = knncluster(umap_result, 200); unique(cls) - % % % % Cmap_cls = jet(length(unique(cls))); - % % % % figure;scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),8,Cmap_cls(cls,:),'filled') - % % % % xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') - % % % % %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - % % % % title('Behavior') - % % % % set(gca,'FontSize',12); - % % % % - % % % % %Find correlation between low-D embedding and high-D embedding - % % % % corr(s,chan) = pdist_plot(Spike_count_raster_final); - % % % % corr_supervised(s,chan) = pdist_plot(data,'label_column',307); - - % cd(savePath) - % OptionZ.FrameRate=30;OptionZ.Duration=15;OptionZ.Periodic=true; - % CaptureFigVid([-20,10;-380,190],['Umap_3Dprojection_' channel],OptionZ) - - %behavior_labels=categorical(behavior_labels); - - behaviors = unique(behavior_labels_final); - behav_mat = zeros(n_clusters, length(behaviors)); - for cl = 1:n_clusters - - cluster_behav = behavior_labels_final(idx_cluster_pca==cl); - behav_tabulation = tabulate(cluster_behav); - behav_mat(cl,behav_tabulation(:,1))= behav_tabulation(:,3); -% cluster_behav_distr{1,cl}=behav_tabulation{cl}(behav_tabulation{cl}(:,3)~=0,:); -% [max_behav, idx_max] = max(cluster_behav_distr{1,cl}(:,3)); -% cluster_behav_distr{2,cl} = behav_categ(cluster_behav_distr{1,cl}(idx_max,1)); -% cluster_behav_distr{3,cl} = max_behav; -% - cluster_block = block_labels_final(idx_cluster_pca==cl); - block_tabulation = tabulate(cluster_block); - block_mat(cl,block_tabulation(:,1))= block_tabulation(:,3); - -% cluster_block_distr{1,cl}=behav_tabulation(block_tabulation(:,3)~=0,:); - - end - - behav_mat_final = behav_mat(:,behaviors); - - %Get list of behaviors which actually occur - figure; - hm=heatmap(behav_mat_final); - hm.XDisplayLabels =behav_categ(behaviors); - colormap jet - - figure; - hm2=heatmap(block_mat); - hm2.XDisplayLabels =block_categ; - -% % % %For session 1, with 30 clusters self-groom separates into two -% % % %clusters. What may be different between the two? -% % % coi = find(behav_mat(:,find(strcmp(behav_categ,"Self-groom"))) >50); -% % % times=find(idx_cluster == coi(1)); -% % % time_plot = zeros(1,length(behavior_labels)); -% % % time_plot(times)=1; -% % % -% % % times2=find(idx_cluster == coi(2)); -% % % time_plot2 = zeros(1,length(behavior_labels)); -% % % time_plot2(times2)=1; -% % % -% % % figure; hold on; plot(time_plot); plot(time_plot2); ylim([-0.5 1.5]) -% % % figure; hold on; histogram(times); histogram(times2) -% % % hrs=floor(median(times)/3600) -% % % mins=floor((median(times)/3600-hrs)*60) -% % % secs=((median(times)/3600-hrs)*60-mins)*60 -% % % -% % % hrs=floor(8380/3600) -% % % mins=floor((8380/3600-hrs)*60) -% % % secs=((8380/3600-hrs)*60-mins)*60 - - - - end %end of channel for loop - - figure; hold on; set(gcf,'Position',[150 250 1000 800]) - - %Plot PCA results color-coded by behavior vlPFC - ax1=subplot(2,1,1); chan =1; - scatter3(pca_result(:,1), pca_result(:,2),pca_result(:,3),8,Cmap(behavior_labels_final,:),'filled') - xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior, vlPFC') - set(gca,'FontSize',12); - - %Color-coded by block vlPFC - ax2=subplot(2,1,2); - scatter3(pca_result(:,1), pca_result(:,2),pca_result(:,3),8,Cmap_block(block_labels_final,:),'filled') - xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Social context, vlPFC') - set(gca,'FontSize',12); - - sgtitle([sessions(s).name]) - hlink = linkprop([ax1,ax2],{'CameraPosition','CameraUpVector'}); - rotate3d on - - - figure; hold on; set(gcf,'Position',[150 250 800 800]) - %Plot PCA results color-coded by behavior TEO - ax3=subplot(2,1,1); chan =2; - scatter3(pca_result(:,1), pca_result(:,2),pca_result(:,3),8,Cmap(behavior_labels_final,:),'filled') - xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior, TEO') - set(gca,'FontSize',12); - - ax4=subplot(2,1,2); - scatter3(pca_result(:,1), pca_result(:,2),pca_result(:,3),8,Cmap_block(block_labels_final,:),'filled') - xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Social context, TEO') - set(gca,'FontSize',12); - - sgtitle([sessions(s).name]) - hlink = linkprop([ax3,ax4],{'CameraPosition','CameraUpVector'}); - rotate3d on - - - saveas(gcf,[savePath '/umap_ColorCodedBy_Units.pdf']) -% % % % savefig([savePath 'Umap_3Dprojection_bothAreas.fig']) - -% cd(savePath) -% OptionZ.FrameRate=30;OptionZ.Duration=15;OptionZ.Periodic=true; -% CaptureFigVid([-20,10;-380,190],['Umap_3Dprojection_bothAreas'],OptionZ) - - -end %end of session for loop - -figure -for s = 1:length(a_sessions) - subplot(length(a_sessions),1,s) - gscatter(pca_result{a_sessions(s),3}(:,1), pca_result{a_sessions(s),3}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('PCA 1'); ylabel('PCA 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - -for s = 1:length(h_sessions) - subplot(length(h_sessions),1,s) - gscatter(pca_result{a_sessions(s),3}(:,1), pca_result{a_sessions(s),3}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('PCA 1'); ylabel('PCA 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - diff --git a/Neural states/log_umap_kmeans_batch.m b/Neural states/log_umap_kmeans_batch.m deleted file mode 100644 index 8500273..0000000 --- a/Neural states/log_umap_kmeans_batch.m +++ /dev/null @@ -1,399 +0,0 @@ -%% log_umap_visualization_batch -% This script applied unsupervised umap on smoothed firing -% Ref: Connor Meehan, Jonathan Ebrahimian, Wayne Moore, and Stephen Meehan (2022). Uniform Manifold Approximation and Projection (UMAP) (https://www.mathworks.com/matlabcentral/fileexchange/71902), MATLAB Central File Exchange. - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1; %lump similar behavioral categories together -threat_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence -exclude_sq=1; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO"] - %channel_flag = "TEO"; - - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - - %Raw data - Spike_count_raster = zscore(Spike_rasters'); - - - %% Select behaviors to visualize - - %Extract behavior labels and frequency - behavior_labels = cell2mat({labels{:,3}}'); - - %Extract block labels - block_labels = cell2mat({labels{:,12}}'); - block_categ={"F","M","Alone"}; - - % Select behaviors - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - %Remove behaviors we're not interested in for now - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding Rowdy Room which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize'))); - - % OR select behaviors manually - %behav =[29] ;%unique(behavior_labels); %[4,5,7,8,9,10,24];% [4:10, 23]; %[4:8,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only consider indices with behavior of interest - idx= find(ismember(behavior_labels,behav));% & ismember(block_labels,3)); - Spike_count_raster_final = Spike_count_raster;%(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels;%(idx);%Same as above but in behavior labels - block_labels_final = block_labels;%(idx); - - if null - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels_final,behav_categ, temp_resolution); - behavior_labels_final = sim_behav; - end - - %% Run umap - - %Supervised -% data = [Spike_count_raster_final, behavior_labels_final]; -% [umap_result{s,chan}]=run_umap(data, 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 3,'label_column', 'end'); %Run umap to get 2d embedded states - - %Unsupervised - [umap_result{s,chan}]=run_umap(Spike_count_raster_final, 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 3); %Run umap to get 2d embedded states - close - - channel = char(channel_flag); - - - %% Run Kmeans clustering - - eva = evalclusters(umap_result{s,chan},'kmeans','CalinskiHarabasz','KList',3:20); eva.OptimalK -% eva = evalclusters(umap_result{s,chan},'kmeans','DaviesBouldin','KList',3:20);eva.OptimalK -% eva = evalclusters(umap_result{s,chan},'kmeans','gap','KList',3:20);eva.OptimalK -% eva = evalclusters(umap_result{s,chan},'kmeans','silhouette','KList',3:20);eva.OptimalK - n_clusters = 7;%eva.OptimalK; %30 - idx_cluster = kmeans(umap_result{s,chan},n_clusters); - - gm = fitgmdist(umap_result{s,chan},n_clusters); - idx_cluster = cluster(gm,umap_result{s,chan}); - - - %% Set colormap - Cmap = [[1 0 0];...%Aggression; red - [1 0.4 0.1];...%Approach; dark orange - [0 0 0];...%But sniff; NA - [0.3 0.7 1];...%Drinking; light blue - [0 0.7 0];...%Foraging; dark green - [1 0 1];...%Groom sollicitation; magenta - [0 1 1];...%Groom partner; cyan - [0 0 1];...%Getting groomed; dark blue - [0.8 0 0];...%Threat to partner; dark red - [1 0 0];...%Threat to subject; red - [0.9 0.9 0];...%leave; dark yellow - [0 0 0];...%Lipsmack - [0.2 0.9 0.76];...%Masturbating; turquoise - [0.7 0 1];...%Mounting; light purple - [0.9 0.5 0];...%Other monkeys vocalize; orange - [1 0.8 0.1];...%Travel; yellow orange - [0 0 0];...%Proximity; NA - [0 0 0];...%Rowdy room; NA - [1 0 0];...%SP; red - [1 0 0];...%SS; red - [0.6314 0.5059 0.0118];...%Scratch; maroon - [0.5 0.2 0.5];...%Self-groom; dark purple - [ 1 0.07 0.65];...%Submission; dark pink - [0 0.4 0.5];...%Vocalzation; blue green - [0 0 0];...%Yawning; NA - [0.8 0.8 0.8]];%Rest; grey - - Cmap_block = [[0.9 0.7 0.12];[0 0.6 0.8];[0.5 0 0]]; - - Cmap_time = copper(length(idx)); - - Cmap_kmeans = hsv(n_clusters); - - mean_activity = int32(round(rescale(mean(Spike_count_raster_final,2)),2)*100); mean_activity(mean_activity==0)=1; - std_activity = int32(round(rescale(std(Spike_count_raster_final,[],2)),2)*100);std_activity(std_activity==0)=1; - Cmap_firingRate = jet(double(max(mean_activity))); - Cmap_firingRateStd = jet(double(max(std_activity))); - %Note: mean activity correlates with variation in activity. - - - %% Plot UMAP projection in 3D space - - figure; hold on; set(gcf,'Position',[150 250 1500 500]) - -% for i=1:n_clusters -% scatter3(umap_result{s,chan}(idx==i,1), umap_result{s,chan}(idx==i,2),umap_result{s,chan}(idx==i,3),8,Cmap_kmeans(i,:),'filled') -% xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -% %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -% title('Block') -% set(gca,'FontSize',12); -% pause(2) -% end - - %Plot UMAP results color-coded by behavior - ax1=subplot(1,3,1); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap(behavior_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior') - set(gca,'FontSize',12); - %saveas(gcf,[savePath '/umap_supervised_ColorCodedByBehav_' channel 'Units.png']) - %pause(5) - - - %Color-coded by block - ax2=subplot(1,3,2); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_block(block_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Block') - set(gca,'FontSize',12); - - - %Color-coded by kmeans cluster - ax3=subplot(1,3,3); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_kmeans(idx_cluster,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Kmeans cluster') - set(gca,'FontSize',12); - -% %Color-coded by time -% ax2=subplot(1,2,2); -% scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_time,'filled') -% xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -% %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -% title('Time') -% set(gca,'FontSize',12); - -% %Color-coded by mean activity -% ax2=subplot(1,2,2); -% scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_firingRate(mean_activity,:),'filled') -% xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -% %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -% title('Mean activity') -% set(gca,'FontSize',12); - -% %Color-coded by std activity -% ax3=subplot(1,3,3); -% scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_firingRateStd(std_activity,:),'filled') -% xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -% %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -% title('Variation in of activity') -% set(gca,'FontSize',12); -% -% %sgtitle([channel ' units, UMAP, ' sessions(s).name]) - - hlink = linkprop([ax1,ax2, ax3],{'CameraPosition','CameraUpVector'}); - rotate3d on - - %savefig([savePath 'Umap_3Dprojection_' channel '.fig']) - %saveas(gcf,[savePath '/umap_ColorCodedByBlock_' channel 'Units.png']) - - - % % % % %plot by cluster - % % % % [cls, pnode] = knncluster(umap_result{s,chan}, 200); unique(cls) - % % % % Cmap_cls = jet(length(unique(cls))); - % % % % figure;scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_cls(cls,:),'filled') - % % % % xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - % % % % %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - % % % % title('Behavior') - % % % % set(gca,'FontSize',12); - % % % % - % % % % %Find correlation between low-D embedding and high-D embedding - % % % % corr(s,chan) = pdist_plot(Spike_count_raster_final); - % % % % corr_supervised(s,chan) = pdist_plot(data,'label_column',307); - - % cd(savePath) - % OptionZ.FrameRate=30;OptionZ.Duration=15;OptionZ.Periodic=true; - % CaptureFigVid([-20,10;-380,190],['Umap_3Dprojection_' channel],OptionZ) - - %behavior_labels=categorical(behavior_labels); - - behaviors = unique(behavior_labels_final); - behav_mat = zeros(n_clusters, length(behaviors)); - for cl = 1:n_clusters - - cluster_behav = behavior_labels_final(idx_cluster==cl); - behav_tabulation = tabulate(cluster_behav); - behav_mat(cl,behav_tabulation(:,1))= behav_tabulation(:,3); -% cluster_behav_distr{1,cl}=behav_tabulation{cl}(behav_tabulation{cl}(:,3)~=0,:); -% [max_behav, idx_max] = max(cluster_behav_distr{1,cl}(:,3)); -% cluster_behav_distr{2,cl} = behav_categ(cluster_behav_distr{1,cl}(idx_max,1)); -% cluster_behav_distr{3,cl} = max_behav; -% - cluster_block = block_labels_final(idx_cluster==cl); - block_tabulation = tabulate(cluster_block); - block_mat(cl,block_tabulation(:,1))= block_tabulation(:,3); - -% cluster_block_distr{1,cl}=behav_tabulation(block_tabulation(:,3)~=0,:); - - end - - behav_mat_final = behav_mat(:,behaviors); - - %Get list of behaviors which actually occur - figure; - hm=heatmap(behav_mat_final); - hm.XDisplayLabels =behav_categ(behaviors); - colormap jet - - figure; - hm2=heatmap(block_mat); - hm2.XDisplayLabels =block_categ; - -% % % %For session 1, with 30 clusters self-groom separates into two -% % % %clusters. What may be different between the two? -% % % coi = find(behav_mat(:,find(strcmp(behav_categ,"Self-groom"))) >50); -% % % times=find(idx_cluster == coi(1)); -% % % time_plot = zeros(1,length(behavior_labels)); -% % % time_plot(times)=1; -% % % -% % % times2=find(idx_cluster == coi(2)); -% % % time_plot2 = zeros(1,length(behavior_labels)); -% % % time_plot2(times2)=1; -% % % -% % % figure; hold on; plot(time_plot); plot(time_plot2); ylim([-0.5 1.5]) -% % % figure; hold on; histogram(times); histogram(times2) -% % % hrs=floor(median(times)/3600) -% % % mins=floor((median(times)/3600-hrs)*60) -% % % secs=((median(times)/3600-hrs)*60-mins)*60 -% % % -% % % hrs=floor(8380/3600) -% % % mins=floor((8380/3600-hrs)*60) -% % % secs=((8380/3600-hrs)*60-mins)*60 - - - - end %end of channel for loop - - figure; hold on; set(gcf,'Position',[150 250 1000 800]) - - %Plot UMAP results color-coded by behavior vlPFC - ax1=subplot(2,1,1); chan =1; - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap(behavior_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior, vlPFC') - set(gca,'FontSize',12); - - %Color-coded by block vlPFC - ax2=subplot(2,1,2); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_block(block_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Social context, vlPFC') - set(gca,'FontSize',12); - - sgtitle([sessions(s).name]) - hlink = linkprop([ax1,ax2],{'CameraPosition','CameraUpVector'}); - rotate3d on - - - figure; hold on; set(gcf,'Position',[150 250 800 800]) - %Plot UMAP results color-coded by behavior TEO - ax3=subplot(2,1,1); chan =2; - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap(behavior_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior, TEO') - set(gca,'FontSize',12); - - ax4=subplot(2,1,2); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_block(block_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Social context, TEO') - set(gca,'FontSize',12); - - sgtitle([sessions(s).name]) - hlink = linkprop([ax3,ax4],{'CameraPosition','CameraUpVector'}); - rotate3d on - - - saveas(gcf,[savePath '/umap_ColorCodedBy_Units.pdf']) -% % % % savefig([savePath 'Umap_3Dprojection_bothAreas.fig']) - -% cd(savePath) -% OptionZ.FrameRate=30;OptionZ.Duration=15;OptionZ.Periodic=true; -% CaptureFigVid([-20,10;-380,190],['Umap_3Dprojection_bothAreas'],OptionZ) - - -end %end of session for loop - -figure -for s = 1:length(a_sessions) - subplot(length(a_sessions),1,s) - gscatter(umap_result{a_sessions(s),3}(:,1), umap_result{a_sessions(s),3}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - -for s = 1:length(h_sessions) - subplot(length(h_sessions),1,s) - gscatter(umap_result{a_sessions(s),3}(:,1), umap_result{a_sessions(s),3}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - diff --git a/Neural states/log_umap_visualization_cluster_batch.m b/Neural states/log_umap_visualization_cluster_batch.m deleted file mode 100644 index 3bad6a4..0000000 --- a/Neural states/log_umap_visualization_cluster_batch.m +++ /dev/null @@ -1,265 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0; %lump similar behavioral categories together - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range(2:end) %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO"] - %channel_flag = "vlPFC"; - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - %Low-pass filter - %Spike_count_raster = lowpass(Spike_rasters',0.005,1); - %PCA - % [coeff,score,latent,tsquared,explained] = pca(Spike_rasters'); - % Spike_count_raster = score(:,1:15); - - - %% Select behaviors to visualize - - %Extract behavior labels and frequency - behavior_labels = cell2mat({labels{:,3}}'); -% behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Threat to partner"); -% behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Threat to subject"); -% behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - if simplify - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - end - - if null - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels,behav_categ, temp_resolution); - behavior_labels = sim_behav; - end - - %Extract block labels - block_labels = cell2mat({labels{:,12}}'); - block_categ = string(block_times{:,1})'; - - % Select behaviors - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 10; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - %Remove behaviors we're not interested in for now - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding Rowdy Room which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize'))); - - % OR select behaviors manually - %behav =[7] ;%unique(behavior_labels); %[4,5,7,8,9,10,24];% [4:10, 23]; %[4:8,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only consider indices with behavior of interest - idx= find(ismember(behavior_labels,behav)); - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_labels_final = block_labels(idx); - behavior_labels_final_rand = randsample(behavior_labels_final, length(behavior_labels_final)); - - %% Run umap - - %Supervised -% data = [Spike_count_raster_final, behavior_labels_final]; -% [umap_result{s,chan}]=run_umap(data, 'n_neighbors', 50, 'min_dist', 0.5, 'n_components', 3,'label_column', 'end'); %Run umap to get 2d embedded states - - %Unsupervised - [umap_result{s,chan}]=run_umap(Spike_count_raster_final, 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 3); %Run umap to get 2d embedded states - close - - channel = char(channel_flag); - - %Set colormap - Cmap = [[1 0 0];[1 0.4 0.1];[0 0 0];[0.1 0.8 0.9];[0 0.7 0];[1 0 1];[0 1 1];... - [0 0 1];[0.8 0 0];[1 0 0];[0 0 0];[0.2 0.9 0.76];[0 0 0];[0 0 0];[0.7 0 1];... - [0 0 0];[0 0 0];[0.9 0.5 0];[0 0 0];[0 0 0];[0.8 0 0];[1 0 0];[0.9 0.7 0.12];[0.5 0.2 0.5];... - [0 0 0];[0 0 0];[0.8 0.4 0.4];[0 0 0];[0.5 0.5 0.5]]; - - Cmap_block = [[0.9 0.7 0.12];[0 0.6 0.8];[0.5 0 0]]; - - Cmap_time = copper(length(idx)); -% Cmap_time(2954,:)=[0 0.8 0]; - - %% Plot UMAP projection in 3D space - - figure; hold on; set(gcf,'Position',[150 250 1500 500]) - - %Plot UMAP results color-coded by behavior - ax1=subplot(1,3,1); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap(behavior_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior') - set(gca,'FontSize',12); - %saveas(gcf,[savePath '/umap_supervised_ColorCodedByBehav_' channel 'Units.png']) - %pause(5) - - %Plot UMAP results color-coded by time - ax2=subplot(1,3,2); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_time,'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Time') - set(gca,'FontSize',12); - - %Color-coded by block - ax3=subplot(1,3,3); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_block(block_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Block') - set(gca,'FontSize',12); - - sgtitle([channel ' units, UMAP, ' sessions(s).name]) - - hlink = linkprop([ax1,ax2,ax3],{'CameraPosition','CameraUpVector'}); - rotate3d on - - savefig([savePath 'Umap_3Dprojection_' channel '.fig']) - %saveas(gcf,[savePath '/umap_ColorCodedByBlock_' channel 'Units.png']) - -% cd(savePath) -% OptionZ.FrameRate=30;OptionZ.Duration=15;OptionZ.Periodic=true; -% CaptureFigVid([-20,10;-380,190],['Umap_3Dprojection_' channel],OptionZ) - - chan=chan+1; - - end %end of channel for loop - - figure; hold on; set(gcf,'Position',[150 250 1000 800]) - - %Plot UMAP results color-coded by behavior vlPFC - ax1=subplot(2,2,1); chan =1; - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap(behavior_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior, vlPFC') - set(gca,'FontSize',12); - - %Color-coded by block vlPFC - ax2=subplot(2,2,2); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_block(block_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Social context, vlPFC') - set(gca,'FontSize',12); - - %Plot UMAP results color-coded by behavior TEO - ax3=subplot(2,2,3); chan =2; - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap(behavior_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior, TEO') - set(gca,'FontSize',12); - - ax4=subplot(2,2,4); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_block(block_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Social context, TEO') - set(gca,'FontSize',12); - - sgtitle([sessions(s).name]) - hlink = linkprop([ax1,ax2,ax3,ax4],{'CameraPosition','CameraUpVector'}); - rotate3d on - - savefig([savePath 'Umap_3Dprojection_bothAreas.fig']) - -% cd(savePath) -% OptionZ.FrameRate=30;OptionZ.Duration=15;OptionZ.Periodic=true; -% CaptureFigVid([-20,10;-380,190],['Umap_3Dprojection_bothAreas'],OptionZ) - - -end %end of session for loop - -figure -for s = 1:length(a_sessions) - subplot(length(a_sessions),1,s) - gscatter(umap_result{a_sessions(s),3}(:,1), umap_result{a_sessions(s),3}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - -for s = 1:length(h_sessions) - subplot(length(h_sessions),1,s) - gscatter(umap_result{a_sessions(s),3}(:,1), umap_result{a_sessions(s),3}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - diff --git a/Neural states/scrap_code_DO_NOT_DELETE/.DS_Store b/Neural states/scrap_code_DO_NOT_DELETE/.DS_Store deleted file mode 100644 index 5008ddf..0000000 Binary files a/Neural states/scrap_code_DO_NOT_DELETE/.DS_Store and /dev/null differ diff --git a/Neural states/scrap_code_DO_NOT_DELETE/Dev_Subspace_Analysis.m b/Neural states/scrap_code_DO_NOT_DELETE/Dev_Subspace_Analysis.m deleted file mode 100644 index 2f3b097..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/Dev_Subspace_Analysis.m +++ /dev/null @@ -1,316 +0,0 @@ -%% Ron_DoSmth -% Have fun! - -close all - -%Set parameters -is_mac = 0; %For loading the data -is_ron = 1; %For setting path -with_partner =1; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -BRs = ["TEO", "vlPFC"]; %Channels considered (sets brain region TEO vlPFC all -with_NC =1; %0: Noise cluster (NC) is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well-isolated units - - -for br = 1:2 %Do separately by brain region to see if there is a difference - - channel_flag = BRs(br) - - subsample = 1; %Toggle whether to subsample - - - - - for s = [1,2,3] %For now focus on first Amost sessions - Results(br).sesh(s).brainregion = channel_flag; - s - %Set path - if is_ron - - sessions = dir('C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_data'); sessions = sessions(3:end,:); - filePath = ['C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_data\' sessions(s).name]; - savePath = ['C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_results']; - - else - - - %Set session list - home = '~'; % set home directory - cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) - sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/partner_vs_subject']; - end - %% Load data - - %Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_Ron(filePath, temp_resolution, channel_flag, is_mac,is_ron, with_NC, isolatedOnly); - % Output: - % 1. Spike_rasters: n_neuron X time in seconds (if temp_resolution = 1) - % 2. labels: time X 11 - check the function description for - % details. Essentially subject behavior is column 3. - % 3. labels_partner: time X 11. Partner behavior is column 3. - % 4. behav_categ: string vector; behavioral category do the numbers in - % labels correspond to. - % 5. block_times: specifies the block times and ID - % 6. monkey: subjecet monkey ID - % 7. reciprocal_set: set of behaviors that are deeemed "reciprocal" - % 8. social_set: set of behaviors that are deeemed "social" - % 9. ME_final: Motion energy values - % 10. unit_count: number of units in the session - % 11. groom_labels_all: labels for grooming. Check function - % description for details - - session_length = size(Spike_rasters,2); % get session length - - if subsample - - n_2_use = 100; %for now just set using 100 neurons - - Spike_rasters = Spike_rasters(randperm(size(Spike_rasters,1),n_2_use),:); - - - end - - Spike_count_raster = Spike_rasters'; - - %Extract behavior labels for subject and partner - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - - % Set proximity as rest - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - - - - %% Subdivide data for successive dimensionality tests - %Going to just run successive tests based on what was outlined in the pitch - %Can come back to clean this up to run more efficiently but for now just - %want to confirm for this session that general idea is right. - - %Going to start with using PCA for speed and clarity (i.e. everyone gets - %PCA) - - %Before doing anything else just get the inds for all the groups we want. - - groominds_give = groom_labels_all(:,1) == 7; - groominds_receive = groom_labels_all(:,1) == 8; - - groominds_all = groominds_give + groominds_receive; %Since these are mutually exclusive and (as far as I can tell) define all groom times. - groominds_all = groominds_all>0; %To change it back to logical indexing - groominds_postthreat = groom_labels_all(:,3) == 1; - groominds_reciprocal = groom_labels_all(:, 4) == 1; - groominds_initiated = groom_labels_all(:,5) == 1; - - groominds_self = behavior_labels_subject_init == 24; %24 is label for self groom - - %Use Spike_count_raster as it is in obs x vars format these algorithms are - %expecting - - %% Test 0.1: Basic checks - Dimensionality for grooming, the rest, and all - %So far this does not match what I was predicting. The deviation isn't - %massive, but it is still surprising. Doesn't entirely sink this train of - %thought, so will contibue with other test, but need to reflect on what - %this means. - - threshold = 50; - - [eigenvec, score, eigenval, ~, var_exp] = pca(Spike_count_raster); - - Results(br).sesh(s).all.eigenvec = eigenvec; - Results(br).sesh(s).all.score = score; - Results(br).sesh(s).all.eigenval = eigenval; - Results(br).sesh(s).all.var_exp = var_exp; - - - Data_grooming = Spike_count_raster(groominds_all,:); - Data_self = Spike_count_raster(groominds_self,:); - Data_groomgive = Spike_count_raster(groominds_give,:); - Data_groomrec = Spike_count_raster(groominds_receive,:); - - - [eigenvec, score, eigenval, ~, var_exp] = pca(Data_grooming); - - Results(br).sesh(s).groomall.eigenvec = eigenvec; - Results(br).sesh(s).groomall.score = score; - Results(br).sesh(s).groomall.eigenval = eigenval; - Results(br).sesh(s).groomall.var_exp = var_exp; - - [eigenvec, score, eigenval, ~, var_exp] = pca(Data_groomgive); - - Results(br).sesh(s).groomgive.eigenvec = eigenvec; - Results(br).sesh(s).groomgive.score = score; - Results(br).sesh(s).groomgive.eigenval = eigenval; - Results(br).sesh(s).groomgive.var_exp = var_exp; - - [eigenvec, score, eigenval, ~, var_exp] = pca(Data_groomrec); - - Results(br).sesh(s).groomrec.eigenvec = eigenvec; - Results(br).sesh(s).groomrec.score = score; - Results(br).sesh(s).groomrec.eigenval = eigenval; - Results(br).sesh(s).groomrec.var_exp = var_exp; - - [eigenvec, score, eigenval, ~, var_exp] = pca(Data_self); - - Results(br).sesh(s).therest.eigenvec = eigenvec; - Results(br).sesh(s).therest.score = score; - Results(br).sesh(s).therest.eigenval = eigenval; - Results(br).sesh(s).therest.var_exp = var_exp; - - %Get number of eigen values needed to cross threshold of variance explained - - Results(br).sesh(s).all.majdim = find(cumsum(Results(br).sesh(s).all.var_exp)>threshold,1,'first'); - Results(br).sesh(s).therest.majdim = find(cumsum(Results(br).sesh(s).therest.var_exp)>threshold,1,'first'); - Results(br).sesh(s).groomall.majdim = find(cumsum(Results(br).sesh(s).groomall.var_exp)>threshold,1,'first'); - Results(br).sesh(s).groomgive.majdim = find(cumsum(Results(br).sesh(s).groomgive.var_exp)>threshold,1,'first'); - Results(br).sesh(s).groomrec.majdim = find(cumsum(Results(br).sesh(s).groomrec.var_exp)>threshold,1,'first'); - - %sorted_axis = 1:length(var_exp);%changed mind don't like this./length(var_exp); %quick trick for setting axes for eigen plots - figure - plot(1:length(Results(br).sesh(s).all.var_exp),cumsum(Results(br).sesh(s).all.var_exp),'k') - title([channel_flag ' ' num2str(s)]) - ylabel('var explained') - xlabel('Eigenvectors used') - ylim([0,110]) - hold on - plot(1:length(Results(br).sesh(s).groomall.var_exp),cumsum(Results(br).sesh(s).groomall.var_exp),'b') - plot(1:length(Results(br).sesh(s).groomgive.var_exp),cumsum(Results(br).sesh(s).groomgive.var_exp),'b--') - plot(1:length(Results(br).sesh(s).groomrec.var_exp),cumsum(Results(br).sesh(s).groomrec.var_exp),'b.-') - plot(1:length(Results(br).sesh(s).therest.var_exp),cumsum(Results(br).sesh(s).therest.var_exp),'r') - plot(1:length(var_exp), threshold * ones(size(var_exp)),'k--') %put dashed black line for majority of variance - %number beside is number of PCs needed to cross threshold var explained - legend(['All: ' num2str(Results(br).sesh(s).all.majdim)],... - ['Grooming: ' num2str(Results(br).sesh(s).groomall.majdim)],... - ['Groom Give: ' num2str(Results(br).sesh(s).groomgive.majdim)],... - ['Groom Receive:' num2str(Results(br).sesh(s).groomrec.majdim)],... - ['Groom Self: ' num2str(Results(br).sesh(s).therest.majdim)],... - 'Threshold') - - - - %% Test 1.1 Compare dimensionality: post threat, initiated, reciprocal - - %Dimensionality is effectively the same across these conditions as well as - %compared to considering all grooming. Kind of interesting to see; need to - %think more on what it means - - Data_groompt = Spike_count_raster(groominds_postthreat,:); - Data_groomrecip = Spike_count_raster(groominds_reciprocal,:); - Data_groomini = Spike_count_raster(groominds_initiated,:); - - [eigenvec, score, eigenval, ~, var_exp] = pca(Data_groompt); - - Results(br).sesh(s).groompt.eigenvec = eigenvec; - Results(br).sesh(s).groompt.score = score; - Results(br).sesh(s).groompt.eigenval = eigenval; - Results(br).sesh(s).groompt.var_exp = var_exp; - - [eigenvec, score, eigenval, ~, var_exp] = pca(Data_groomrecip); - - Results(br).sesh(s).groomrecip.eigenvec = eigenvec; - Results(br).sesh(s).groomrecip.score = score; - Results(br).sesh(s).groomrecip.eigenval = eigenval; - Results(br).sesh(s).groomrecip.var_exp = var_exp; - - [eigenvec, score, eigenval, ~, var_exp] = pca(Data_groomini); - - Results(br).sesh(s).groomini.eigenvec = eigenvec; - Results(br).sesh(s).groomini.score = score; - Results(br).sesh(s).groomini.eigenval = eigenval; - Results(br).sesh(s).groomini.var_exp = var_exp; - - - Results(br).sesh(s).groompt.majdim = find(cumsum(Results(br).sesh(s).groompt.var_exp)>threshold,1,'first'); - Results(br).sesh(s).groomrecip.majdim = find(cumsum(Results(br).sesh(s).groomrecip.var_exp)>threshold,1,'first'); - Results(br).sesh(s).groomini.majdim = find(cumsum(Results(br).sesh(s).groomini.var_exp)>threshold,1,'first'); - - - figure - plot(1:length(Results(br).sesh(s).groomall.var_exp),cumsum(Results(br).sesh(s).groomall.var_exp),'b') - title([channel_flag ' ' num2str(s)]) - ylabel('var explained') - xlabel('Eigenvectors used') - hold on - plot(1:length(Results(br).sesh(s).groompt.var_exp),cumsum(Results(br).sesh(s).groompt.var_exp),'bx') - plot(1:length(Results(br).sesh(s).groomrecip.var_exp),cumsum(Results(br).sesh(s).groomrecip.var_exp),'b--') - plot(1:length(Results(br).sesh(s).groomini.var_exp),cumsum(Results(br).sesh(s).groomini.var_exp),'b.-') - - plot(sorted_axis, threshold * ones(size(sorted_axis)),'k--') %put dashed black line for majority of variance - %number beside is number of PCs needed to cross threshold var explained - legend(['Grooming: ' num2str(Results(br).sesh(s).groomall.majdim)],... - ['Groom Post Threat: ' num2str(Results(br).sesh(s).groompt.majdim)],... - ['Groom Reciprocal: ' num2str(Results(br).sesh(s).groomrecip.majdim)],... - ['Groom Initated: ' num2str(Results(br).sesh(s).groomini.majdim)],... - 'Threshold') - - %% Test 1.2 Grooming lies in different subspaces based on context: post threat, initiated, reciprocal - %First do simple test with average angle between PCs - %Next as there was more to that paper than I thought and my first tries are - %not exactly working as expected; implement the analyses from the below - %paper to see what that gets us and if some of their processing steps are - %necessary to see this structure. - - %Not eigenvectors come out of PCA in matlab already normalized to have unit - %vector norm (i.e. norm(eigenvec(:,1)) = 1 - - %Just use maximum number need to cross threshold of variance explained from above. - num_eigenvec = max([Results(br).sesh(s).groompt.majdim,Results(br).sesh(s).groomrecip.majdim, Results(br).sesh(s).groomini.majdim]); - - - collected_eigens = cell(3,1); - - collected_eigens{1} = Results(br).sesh(s).groompt.eigenvec(:,1:num_eigenvec); - collected_eigens{2} = Results(br).sesh(s).groomrecip.eigenvec(:,1:num_eigenvec); - collected_eigens{3} = Results(br).sesh(s).groomini.eigenvec(:,1:num_eigenvec); - - angles_between = cell(3,1); %n choose k with 3 is 3!/2! (3-2)! = 3 - avg_angle = nan(3,1); - - comparison = [1 2; 1 3; 2 3]; %better ways to do this but for now just manually put in all of the comparisons we want to do - - % for comp = 1:length(angles_between) - % - % angles_between{comp} = acos(dot(collected_eigens{comparison(comp,1)},collected_eigens{comparison(comp,2)})); - % - % avg_angle(comp) = rad2deg(mean(angles_between{comp})); - % - % - % end - % - some_labels = {'post-threat to reciprocal','post-threat to initiated','reciprocal to initated'}'; - % - % table(avg_angle,some_labels,'VariableNames',{'avg angle', 'comparison'}) - - %Now trying the principal angles definition that is used in the below paper - %and seems to be more standard in the field given some reading. Take the - %SVD of the product of the two subspaces - - angles_betweensvd = cell(3,1); - principal_angle = nan(size(angles_betweensvd)); - - for comp = 1:length(angles_between) - - [~,S,~]= svd(collected_eigens{comparison(comp,1)}' * collected_eigens{comparison(comp,2)}); %want to do SVD on PC x PC matrix, hence transpose - - angles_betweensvd{comp} = rad2deg(acos(diag(S))); %singular vlaues of this matrix relate to the angles - - principal_angle(comp) = angles_betweensvd{comp}(end); %svd in matlab does descending order apparently. Larget SV is 1st principal angle - end - - t1 = table(principal_angle,some_labels,'VariableNames',{'principal angle', 'comparison'}) - - Results(br).sesh(s).groomcontext.pa = t1; - %% Test 1.3 sanity check on overlap between grooming and other behaviors - %Replicating above analysis for grooming and groom self - - - - %% Test(s) 2-? Replicating Geometry of sequence working memory in macaque prefrontal cortex analyses - %Our analyses don't exactly align due to the difference in our behavior vs - %their task, but going to try to more or fully implement what they did and see - %where it gets us. - - end -end \ No newline at end of file diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_DataHigh_NeuralStates.m b/Neural states/scrap_code_DO_NOT_DELETE/log_DataHigh_NeuralStates.m deleted file mode 100644 index d27ce40..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_DataHigh_NeuralStates.m +++ /dev/null @@ -1,137 +0,0 @@ - -%% Load data - -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) -cd(filePath) - -session = filePath(end-9:end); -monkey = filePath(end-14:end-10); - -%Load behavioral data -behavior_log = readtable(['EVENTLOG_restructured_',monkey,session,'.csv']);% Behavioral data -behavior_log{:,'start_time_round'}=round(behavior_log{:,'start_time'}); -behavior_log{:,'end_time_round'}=round(behavior_log{:,'end_time'}); -behavior_log{:,'duration_round'}=behavior_log{:,'end_time_round'}-behavior_log{:,'start_time_round'}; - -%Load neural data -load(['Neural_data_' session '.mat']) % Neural data - -%% Neural states during "awake" state. 1- Compare active behaviors states - -% Select behaviors of a particular duration: -min_duration = 1;%in sec -idx = find(behavior_log{:,'duration_round'}>=min_duration); -new_log=behavior_log(idx,:); - -% Create structure with chunked neural data per event types. -time_around_epoch = 0;%in sec -event_window=min_duration;%in sec -B = struct(); -pseudo_trial = 1; -for behav = 1:size(new_log,1) - - total_time = new_log{behav,'start_time_round'}-time_around_epoch ... - :new_log{behav,'end_time_round'}+time_around_epoch; - - divide=floor(length(total_time)./event_window); - mode=mod(length(total_time),event_window); - new_chunks = reshape(total_time(1:end-mode),divide,[]); - for ii = 1:divide - - B(pseudo_trial).data = Unit_rasters(:,new_chunks(ii,:)); - - B(pseudo_trial).condition = char(new_log{behav,'Behavior'}); - % if strcmp(new_log{behav,'Behavior'},'Resting') - % B(behav).condition = 'Non-Resting'; - % else - % B(behav).condition = 'Resting'; - % end - - B(pseudo_trial).epochStarts=1; - if strcmp(new_log{behav,'Behavior'},'HIP') - B(pseudo_trial).epochColors=[1,0,0]; - elseif strcmp(new_log{behav,'Behavior'},'Aggression') - B(pseudo_trial).epochColors=[0.6,0,1]; - elseif strcmp(new_log{behav,'Behavior'},'Approach') - B(pseudo_trial).epochColors=[0.6,1,1]; - elseif strcmp(new_log{behav,'Behavior'},'Leave') - B(pseudo_trial).epochColors=[0.2,0.2,1]; - elseif strcmp(new_log{behav,'Behavior'},'Groom Give') - B(pseudo_trial).epochColors=[0,0,1]; - elseif strcmp(new_log{behav,'Behavior'},'Groom Receive') - B(pseudo_trial).epochColors=[0,0.75,1]; - elseif strcmp(new_log{behav,'Behavior'},'Proximity') - B(pseudo_trial).epochColors=[0,0.75,1]; - elseif strcmp(new_log{behav,'Behavior'},'Self-groom') - B(pseudo_trial).epochColors=[0,1,0]; - elseif strcmp(new_log{behav,'Behavior'},'Scratch') - B(pseudo_trial).epochColors=[1,0.2,0.3]; - elseif strcmp(new_log{behav,'Behavior'},'HIS') - B(pseudo_trial).epochColors=[1,0.75,0]; - elseif strcmp(new_log{behav,'Behavior'},'Pacing/Travel') - B(pseudo_trial).epochColors=[1,0,1]; - elseif strcmp(new_log{behav,'Behavior'},'RR') - B(pseudo_trial).epochColors=[0,0.5,1]; - elseif strcmp(new_log{behav,'Behavior'},'SS') - B(pseudo_trial).epochColors=[0,0.8,0.2]; - elseif strcmp(new_log{behav,'Behavior'},'SP') - B(pseudo_trial).epochColors=[0.3,0.9,0.2]; - elseif strcmp(new_log{behav,'Behavior'},'Submission') - B(pseudo_trial).epochColors=[1,0.2,1]; - elseif strcmp(new_log{behav,'Behavior'},'Foraging') - B(pseudo_trial).epochColors=[0,0.5,0]; - elseif strcmp(new_log{behav,'Behavior'},'Drinking') - B(pseudo_trial).epochColors=[0.5,0,1]; - end - B(pseudo_trial).type='state'; - - pseudo_trial=pseudo_trial+1; - end -end - -% Select a random subsample -behaviors = unique({B.condition}); -B_subsample = B([],1); -subsample_size = 50 -for b = 1:length(behaviors) - idx=find(ismember({B.condition},behaviors(b))); - if length(idx)>subsample_size - B_subsample=[B_subsample;B(idx(randi(length(idx),50,1)))']; - else - B_subsample=[B_subsample;B(idx)']; - end -end - -%Shuffling test -B_rand = B_subsample; -randomization = randperm(length(B)); -for i = 1:length(randomization) -B_rand(i).data = B(randomization(i)).data; -end - -% % Average "chunked" trials (for better visualization): -% table_B = struct2table(B); avg_B=struct(); -% tabulate(table_B{:,'condition'}); -% behav_list = unique(table_B{:,'condition'}); -% num_events=3; avg_state=1; -% for b = 1:length(behav_list) -% -% idx=find(ismember(table_B{:,'condition'},behav_list{b})); -% divide=floor(length(idx)./num_events); -% mode=mod(length(idx),num_events); -% average_over = reshape(idx(1:end-mode),divide,[]); -% -% for avg = 1:size(average_over,1) -% avg_B(avg_state).data = mean(cat(3,B(average_over(avg,1)).data,B(average_over(avg,2)).data,... -% B(average_over(avg,3)).data,B(average_over(avg,4)).data),3); -% avg_B(avg_state).condition = behav_list{b}; -% avg_B(avg_state).epochStarts = 1; -% avg_B(avg_state).epochColors = B(average_over(avg,1)).epochColors; -% avg_B(avg_state).type='state'; -% -% avg_state=avg_state+1; -% end -% end - -% Call DataHigh -DataHigh(B_subsample,'DimReduce') \ No newline at end of file diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_DimReduction.m b/Neural states/scrap_code_DO_NOT_DELETE/log_DimReduction.m deleted file mode 100644 index b26c0e7..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_DimReduction.m +++ /dev/null @@ -1,115 +0,0 @@ - -%% Load data - -%Set path -is_mac = 1; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -% Set parameters -subsample =0;%1 or 0 -subsample_size = 200; - -temp = 1; temp_resolution = 1/5; -for temp_resolution = [1/5, 1/2, 1, 5, 10, 100] %Set temporal resolution: 5sec, 1sec, 100msec - %temp_resolution = 1/5; %1 for second resolution, 10 for 100msec resolution, 100 for 10msec resolution, 1000 for msec resolution. etc. - %0.1 for 10sec resolution, 1/5 for 5sec resolution - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; channel_flag = "all"; - for channel_flag = ["vlPFC", "TEO", "all"] - - %Get data with specified temporal resolution and channels - [Spike_rasters, labels, behav_categ]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag); - %filePath is the experimental data path - %Temp_resolution is the temporal resolution at which we would like to - %analyze the dat - %Channel_flag specifies with channels to include: only TEO array, only - %vlPFC array or all channels - disp('Data Loaded') - - %Previously loaded files individually. Keep for now. - % % % load(['Data_' num2str(1000/temp_resolution) 'msec_res.mat'])%Load data - % % % load('Labels.mat')%Behavioral labels - % % % load('Neural_data.mat') % Neural data; array1 is in TEO and array2 is in vlPFC - - Spike_count_raster = zscore(Spike_rasters,[],2)'; - - %% Select behaviors to decode - %Compute freq of behavior for the session - behavior_labels = cell2mat({labels{:,6}}'); - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=0,:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - % min_occurrences = 30; - % behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%[3,4,5,6,7,8,13,14,15,16]; - % behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - % behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = [1:3];%[4:8,17];%[1:6,9:11,16,17]; %manually select behaviors of interest - %behavs_eval = behav_categ(behav); - - %% Only keep data points where the behavior of interest occurs - if subsample==1% Select a random subsample for each behavior - idx_subsample = []; - for b = 1:length(behav) - idx = find(ismember(behavior_labels,behav(b))); %find the indices of the behaviors considered - if length(idx)>subsample_size - idx_subsample=[idx_subsample;idx(randi(length(idx),subsample_size,1))]; - else - idx_subsample=[idx_subsample;idx]; - end - end - idx_to_keep = idx_subsample;%Only keep timepoints where the behaviors of interest occur in spiking data - else % No sub-sample, consider all time points - idx_to_keep = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - end - Spike_count_raster_final = Spike_count_raster(idx_to_keep,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx_to_keep,:);%Same as above but in behavior labels - tabulate(behavior_labels_final); - - %% Implement PCA and plot results - [coeff, score, latent] = pca(Spike_count_raster_final); - - figure; plot(latent) - figure; hold on - scatter3(score(:,1), score(:,2),score(:,3),12); title('Data in PCA space'); hold off - figure; hold on - color = hsv(length(behav)); - for b = 1:length(behav) - scatter3(score(behavior_labels_final==behav(b),1), score(behavior_labels_final==behav(b),2),... - score(behavior_labels_final==behav(b),3),12,color(b,:),'filled'); - end - title('Data in PCA space, color coded by true behavior'); hold off - legend(behavs_eval'); - - %% Implement unsupervised clustering algorithm - k-means: - dim_reduced_spike = score(:,1:14); - idx = kmeans(dim_reduced_spike,3); - figure; hold on - for b = 1:length(behav) - scatter3(score(idx==b,1), score(idx==b,2),score(idx==b,3),12,color{b}) - end - title('Data in PCA space, color coded by kmean cluster id'); - - %Check result - idx_shuffled = idx(randperm(length(idx))); - behavior_labels_final_test=ones(size(behavior_labels_final)); behavior_labels_final_test(behavior_labels_final==5)=2; - result = [idx behavior_labels_final_test]; - sum(abs(idx-behavior_labels_final_test))/length(idx) - sum(abs(idx_shuffled-behavior_labels_final_test))/length(idx) - - - end -end diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality.m b/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality.m deleted file mode 100644 index 71d103b..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality.m +++ /dev/null @@ -1,111 +0,0 @@ -%%Log_dimensionality - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "vlPFC"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1;%set the smoothing window size (sigma) -var_explained_threshold=90; num_units = 100; -num_iter = 100; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - - - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - dim = nan(2,num_iter); - - for channel_flag = ["vlPFC", "TEO"] - %channel_flag = "vlPFC"; - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey,... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, ... - with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - - - %% Compute dimensionality for 100 units, over multiple iterations - - for iter = 1:num_iter - - %Select unit to run SVM - Input_matrix = Spike_count_raster(:,randsample(size(Spike_count_raster,2), num_units)); - - %PCA - [coeff,score,~,~,explained] = pca(Input_matrix); - - %Get dimensionality - var_explained = cumsum(explained); - idxl = find(var_explained>=var_explained_threshold); - dim(chan,iter) = min(idxl); - - end % end of interation loop - - chan=chan+1; - disp([channel_flag ' done']) - end %end of channel loop - - %% Plot results for the session - mean_dim_vlpfc(s) = mean(dim(1,:),2); - sd_dim_vlpfc(s) = std(dim(1,:),0,2); - mean_dim_teo(s) = mean(dim(2,:),2); - sd_dim_teo(s) = std(dim(2,:),0,2); - - -end%end of session loop - -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/Dimensionality_results/']); - - -nonzero_sessions = mean_dim_vlpfc~=0; -figure; hold on; set(gcf,'Position',[150 250 700 500]) -violin([mean_dim_vlpfc(nonzero_sessions)', mean_dim_teo(nonzero_sessions)']) -scatter(ones(1,length(session_range)),mean_dim_vlpfc(nonzero_sessions),50,'g','filled') -scatter(ones(1,length(session_range))*2,mean_dim_teo(nonzero_sessions),50,'b','filled') -xticks([1 2]); xlim([0.5 2.5]); ylim([0 35]) -xticklabels({'vlPFC','TEO'}) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 18) -title('Whole session') -saveas(gcf,'DimensionalityPerArea.pdf') \ No newline at end of file diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_old.m b/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_old.m deleted file mode 100644 index f1fd55e..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_old.m +++ /dev/null @@ -1,109 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 3;%set the smoothing window size (sigma) -var_explained_threshold=90; -num_iter = 100; numpoints = 10; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - dim = nan(2,numpoints,num_iter); - - for channel_flag = ["vlPFC", "TEO"] - %channel_flag = "vlPFC"; - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey,... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, ... - with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - - - %% Compute dimensionality over increasing numbers of units, over multiple iterations - u = 1; unit_num_range = round(linspace(1,min(unit_count),numpoints)); - - for unit_num = unit_num_range - - disp(['Unit num: ' num2str(unit_num)]) - for iter = 1:num_iter - - %Select unit to run SVM - Input_matrix = Spike_count_raster(:,randsample(unit_count(chan), unit_num)); - - %PCA - [coeff,score,~,~,explained] = pca(Input_matrix); - - %Get dimensionality - var_explained = cumsum(explained); - idxl = find(var_explained>=var_explained_threshold); - dim(chan,u,iter) = min(idxl); - - end % end of interation loop - - u=u+1; - end %end of unit loop - - chan=chan+1; - disp([channel_flag ' done']) - end %end of channel loop - - %% Plot results for the session - mean_dim_vlpfc = mean(dim(1,:,:),3); - sd_dim_vlpfc = std(dim(1,:,:),0,3); - mean_dim_teo = mean(dim(2,:,:),3); - sd_dim_teo = std(dim(2,:,:),0,3); - - figure; hold on; set(gcf,'Position',[150 250 700 500]) - errorbar(mean_dim_vlpfc, sd_dim_vlpfc,'s','MarkerSize',10) - errorbar(mean_dim_teo, sd_dim_teo,'s','MarkerSize',10) - xticks([1:length(unit_num_range)]); xlim([0.8 length(unit_num_range)+0.2]); ylim([0 max(mean_dim_vlpfc)+max(mean_dim_vlpfc)*0.1]) - xticklabels(unit_num_range) - ax = gca; - ax.FontSize = 14; - legend('vlPFC','TEO') - ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 18); xlabel('#units included','FontSize', 18) - title('Smoothing: 20s') - - -end%end of session loop diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_perBehav.m b/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_perBehav.m deleted file mode 100644 index 2dcf5c1..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_perBehav.m +++ /dev/null @@ -1,428 +0,0 @@ -%%log_Dimensionality_perBehav -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 10; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1;%set the smoothing window size (sigma) -var_explained_threshold=90; -num_iter = 500; num_units = 100; -simplify=4; -threat_precedence =1; -exclude_sq=1; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - - -%Set colormap -Cmap = [[1 0 0];...%Aggression; red - [1 0.4 0.1];...%Approach; dark orange - [0 0 0];...%But sniff; NA - [0.3 0.7 1];...%Drinking; light blue - [0 0.7 0];...%Foraging; dark green - [1 0 1];...%Groom sollicitation; magenta - [0 1 1];...%Groom partner; cyan - [0 0 1];...%Getting groomed; dark blue - [0.8 0 0];...%Threat to partner; dark red - [1 0 0];...%Threat to subject; red - [0.9 0.9 0];...%leave; dark yellow - [0 0 0];...%Lipsmack - [0.2 0.9 0.76];...%Masturbating; turquoise - [0.7 0 1];...%Mounting; light purple - [0.9 0.5 0];...%Other monkeys vocalize; orange - [1 0.8 0.1];...%Travel; yellow orange - [0 0 0];...%Proximity; NA - [0 0 0];...%Rowdy room; NA - [1 0 0];...%SP; NA - [1 0 0];...%SS; NA - [0.6314 0.5059 0.0118];...%Scratch; maroon - [0.5 0.2 0.5];...%Self-groom; dark purple - [ 1 0.07 0.65];...%Submission; dark pink - [0 0.4 0.5];...%Vocalzation; blue green - [0 0 0];...%Yawning; NA - [0.8 0.8 0.8]];%Rest; grey - -%Set behaviors of interest to compare and associated colormap -if simplify ==1 - dim = nan(max(session_range),2,5,num_iter); - min_occurrences = 50*temp_resolution; -elseif simplify ==2 - dim = nan(max(session_range),2,2,num_iter); - min_occurrences = 230*temp_resolution; -elseif simplify ==3 - dim = nan(max(session_range),2,4,num_iter); - min_occurrences = 100*temp_resolution; -else - dim = nan(max(session_range),2,8,num_iter); - min_occurrences = 50*temp_resolution; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/Dimensionality_results/']; - - chan = 1; - - %for channel_flag = ["vlPFC", "TEO"] - %channel_flag = "vlPFC"; - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - - behavior_labels = cell2mat({labels{:,3}}'); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - if simplify == 1 %Compare across 5 behavioral categories that are common across sessions - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Lump Drinking and foraging - behavior_labels(behavior_labels==find(behav_categ=="Drinking"))=find(behav_categ=="Foraging"); - - %Lump all grooming together - behavior_labels(behavior_labels==find(behav_categ=="Getting groomed"))=find(behav_categ=="Groom partner"); - behavior_labels(behavior_labels==find(behav_categ=="Groom sollicitation"))=find(behav_categ=="Groom partner"); - behavior_labels(behavior_labels==find(behav_categ=="Self-groom"))=find(behav_categ=="Groom partner"); - - behav = [1,5,7,18,29]; - - elseif simplify == 2 % Compare one behavior (grooming or foraging) to all others - - %Lump all that is not grooming together - behavior_labels(ismember(behavior_labels,find(behav_categ~="Groom partner" & behav_categ~="Getting groomed")))=find(behav_categ=="Rest"); - - behav = [find(behav_categ=="Getting groomed"),find(behav_categ=="Rest")]; - -% % %Lump all that is not foraging -% % behavior_labels(ismember(behavior_labels,find(behav_categ~="Foraging" )))=find(behav_categ=="Rest"); -% % -% % behav = [find(behav_categ=="Foraging"),find(behav_categ=="Rest")]; - - elseif simplify == 3 %Comapre grooming behaviors between each other - - %Lump all that is not grooming together - behavior_labels(ismember(behavior_labels,find(behav_categ~="Groom partner" & behav_categ~="Getting groomed" & behav_categ~="Self-groom")))=find(behav_categ=="Rest"); - - behav = [find(behav_categ=="Groom partner"),find(behav_categ=="Getting groomed"),find(behav_categ=="Self-groom"),find(behav_categ=="Rest")]; - - else %Compare all behaviors separately (without pooling across) - behav = [find(behav_categ=="Drinking"),find(behav_categ=="Foraging"),... - find(behav_categ=="Groom partner"),find(behav_categ=="Getting groomed"),... - find(behav_categ=="Threat to partner"),find(behav_categ=="Threat to subject"),... - find(behav_categ=="Self-groom"),find(behav_categ=="Rest")]; - end - - behav_freq_table = tabulate(behavior_labels); - - %% Compute dimensionality over increasing numbers of units, over multiple iterations - - idx_all_beh= find(ismember(behavior_labels,behav)); - behav_freq=tabulate(behavior_labels(idx_all_beh)); - n_per_behav{s} = behav_freq(behav_freq(:,2)>0,2); - - if all(behav_freq(behav_freq(:,2)>0,2)>min_occurrences) && length(find(behav_freq(:,2)>0))>=length(behav) - - for b = 1:length(behav) - - disp(['Behavior: ' behav_categ(behav(b))]) - - - for iter = 1:num_iter - - %Select time points - idx= find(ismember(behavior_labels, behav(b))); - idx_beh = idx(randsample(1:length(idx), min_occurrences)); %subsample number of indices - - %Select neural data - neural_data = (Spike_count_raster(idx_beh,randsample(size(Spike_count_raster,2), num_units))); - - %Compute mean firing rate, variance and correlation in response - mean_Hz{s, b}(iter,:) = mean(neural_data); - std_Hz{s, b}(iter,:) = std(neural_data); - cv_Hz{s, b}(iter,:) = std(neural_data)./mean(neural_data); - corr_data=reshape(triu(corrcoef(neural_data)), 1,[]); - correl_Hz{s, b, iter} = corr_data(corr_data~=0 & corr_data~=1); - - %PCA - Input_matrix = zscore(neural_data); - [coeff,score,latent,~,explained] = pca(Input_matrix); - - %Get dimensionality - loadings(iter,:) = sum(abs(coeff),1); %Number of units involved in each PC - var_explained_cumul(iter,:) = cumsum(explained); %Cumulative variance explained - var_explained(iter,:)= explained; %Variance explained at each PC - var_explained_logEigen(iter,:) = log10(latent); %Log of eigenvalues - var_explained_Eigen(iter,:) = latent; %eigenvalues - total_var{b}(s,iter) =sum(latent); %Total variance = sum of eigenvalues - total_vol{b}(s,iter) = sum(log10(latent)); %Total volume occupied in neural space = sum of log(eigenvalues) - idxl = find(var_explained_cumul(iter,:)>=var_explained_threshold);%get # dimensions needed to explain 90% of variation - dim{b}(s,iter) = min(idxl); - explained_var_1D{b}(s,iter) = explained(1); %Get variance explained in 1PC - Eigenval_1D{b}(s,iter) = latent(1); %Get eigenvalue of first PC - - - - end % end of interation loop - - %Average over subsampling iterations to get dimensionality - %values for the session - var_explained_cumul_mean{b}(s,:) = mean(var_explained_cumul); - var_explained_mean{b}(s,:)= mean(var_explained); - var_explained_logEigen_mean{b}(s,:) = mean(var_explained_logEigen); - var_explained_Eigen_mean{b}(s,:) = mean(var_explained_Eigen); - loadings_shesh{b,s} = mean(loadings); - - end %end of behavior loop - - - -% chan=chan+1; -% disp([channel_flag ' done']) - %end %end of channel loop - - %% Plot results for the session - - -% % % % figure; hold on -% % % % for b=1:size(var_explained_mean{s},2) -% % % % plot(var_explained_mean{s}(:,b),'LineWidth',3, 'Color', Cmap(behav(b),:)) -% % % % end -% % % % yline(90,'LineStyle','--') -% % % % xline(3,'LineStyle',':') -% % % % xlabel('Dimensions') -% % % % ylabel('Var. explained') -% % % % legend(behav_categ(behav)) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % %saveas(gcf,[savePath '/DimensionalityPerBehav_allExplainedVar.pdf']) -% % % % -% % % % figure; hold on; set(gcf,'Position',[150 250 600 400]) -% % % % -% % % % dim_plot = squeeze(squeeze(dim(s,1,:,:)))'; -% % % % mean_dim = squeeze(mean(dim(s,1,:,:),4)); [~, orderIdx] = sort(mean_dim); -% % % % violin(dim_plot(:,orderIdx), 'facecolor',Cmap(behav(orderIdx),:)) -% % % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % % xticklabels(behav_categ(behav(orderIdx))); %ylim([0 35]) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -% % % % title('Dimensionality across behaviors') - % - % saveas(gcf,[savePath '/DimensionalityPerBehav_2categ.pdf']) - - -% % % figure; hold on; set(gcf,'Position',[150 250 600 400]) -% % % -% % % dim_plot = squeeze(squeeze(var_expl_dim3(s,1,:,:)))'; -% % % mean_dim = squeeze(mean(var_expl_dim3(s,1,:,:),4)); [~, orderIdx] = sort(mean_dim); -% % % violin(dim_plot(:,orderIdx), 'facecolor',Cmap(behav(orderIdx),:)) -% % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % xticklabels(behav_categ(behav(orderIdx))); %ylim([0 35]) -% % % ax = gca; -% % % ax.FontSize = 14; -% % % ylabel(['Variance explained at 3D'],'FontSize', 14); -% % % title('Variance explained at 3D') - - -% % % figure; hold on; set(gcf,'Position',[150 250 600 400]) -% % % -% % % vol_plot = squeeze(squeeze(vol(s,1,:,:)))'; -% % % mean_vol = squeeze(mean(vol(s,1,:,:),4)); [~, orderIdx] = sort(mean_vol); -% % % violin(vol_plot(:,orderIdx), 'facecolor',Cmap(behav(orderIdx),:)) -% % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % xticklabels(behav_categ(behav(orderIdx))); %ylim([0 35]) -% % % ax = gca; -% % % ax.FontSize = 14; -% % % ylabel(['Volume in 3D space'],'FontSize', 14); -% % % title('Volume across behaviors') - % - % close all - - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - disp(s) - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - end % end of if clause - -end%end of session loop - -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/Dimensionality_results/']); -save('Dimensionality_GroomRest.mat', "dim","var_explained_mean","behav","a_sessions","h_sessions","behav_categ","Cmap") - -%% Plot results across sessions - -%load('Dimensionality_allBehav.mat') -%load('Dimensionality_GroomRest.mat') - -dim_amos = squeeze(nanmean(dim(a_sessions,:,:,:),1)); -dim_hooke = squeeze(nanmean(dim(h_sessions,:,:,:),1)); -dim_all = squeeze(nanmean(dim,1)); - -%Plot # dimensions needed to explain 90% of the variance. -%All Pooled -figure; hold on; lowlimit=15; uplimit=30; -mean_dim = squeeze(mean(dim_all(1,:,:),3)); [~, orderIdx] = sort(mean_dim); -violin(squeeze(dim_all(1,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); - - -%Plot the full relationship between # dimensions and explained variance -mean_alldims = mean(cat(3,(var_explained_mean{:})),3); -sd_alldims = std(cat(3,(var_explained_mean{:})),[],3); -figure; hold on -for b=1:size(mean_alldims,2) - y=mean_alldims(:,b); - sd=sd_alldims(:,b); - upper_lim=y+sd; - lower_lim=y-sd; - p = fill([1:length(y) length(y):-1:1],[upper_lim; flip(lower_lim)],'red'); - p.FaceColor = Cmap(behav(b),:); - p.FaceAlpha = 0.1; - p.EdgeColor = 'none'; - - plot(y,'LineWidth',3, 'Color', Cmap(behav(b),:)) -end -yline(90,'LineStyle','--') -xline(3,'LineStyle',':') -xlabel('Dimensions') -ylabel('Var. explained') -legend(behav_categ(behav)) -ax = gca; -ax.FontSize = 14; - saveas(gcf,'DimensionalityPerBehav_allExplainedVar.pdf') - -%Plot the full relationship between # dimensions and explained variance -mean_alldims_notCumul = mean(cat(3,(var_explained_notCumul_mean{:})),3); -sd_alldims_notCumul = std(cat(3,(var_explained_notCumul_mean{:})),[],3); -figure; hold on -for b=1:size(mean_alldims,2) - y=mean_alldims_notCumul(:,b); - sd=sd_alldims_notCumul(:,b); - upper_lim=y+sd; - lower_lim=y-sd; - p = fill([1:length(y) length(y):-1:1],[upper_lim; flip(lower_lim)],'red'); - p.FaceColor = Cmap(behav(b),:); - p.FaceAlpha = 0.1; - p.EdgeColor = 'none'; - - plot(y,'LineWidth',3, 'Color', Cmap(behav(b),:)) -end -xlabel('Dimensions') -ylabel('Variance (log10)') -%legend(behav_categ(behav)) -ax = gca; -ax.FontSize = 14; - -% % % % %Separate per brain area, pooling both monkeys -% % % % figure; hold on; set(gcf,'Position',[150 250 1000 500]); lowlimit=20; uplimit=35; -% % % % subplot(1,2,1); hold on -% % % % mean_dim = squeeze(mean(dim_all(1,:,:),3)); [~, orderIdx] = sort(mean_dim); -% % % % violin(squeeze(dim_all(1,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -% % % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % % xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -% % % % title('vlPFC') -% % % % -% % % % subplot(1,2,2); hold on -% % % % mean_dim_teo = squeeze(mean(dim_all(2,:,:),3)); [~, orderIdx] = sort(mean_dim_teo); -% % % % violin(squeeze(dim_all(2,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -% % % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % % xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -% % % % title('TEO') -% % % % saveas(gcf,'DimensionalityPerBehav_GroomCateg.pdf') -% % % % -% % % % -% % % % %Separated by monkey and brain area -% % % % figure; hold on; set(gcf,'Position',[150 250 1000 800]); lowlimit=25; uplimit=45; -% % % % -% % % % %Amos -% % % % subplot(2,2,1); hold on -% % % % mean_dim = squeeze(mean(dim_amos(1,:,:),3)); [~, orderIdx] = sort(mean_dim); -% % % % violin(squeeze(dim_amos(1,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -% % % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % % xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -% % % % title('Amos, vlPFC') -% % % % -% % % % subplot(2,2,2); hold on -% % % % mean_dim_teo = squeeze(mean(dim_amos(2,:,:),3)); [~, orderIdx] = sort(mean_dim_teo); -% % % % violin(squeeze(dim_amos(2,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -% % % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % % xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -% % % % title('Amos, TEO') -% % % % -% % % % %Hooke -% % % % subplot(2,2,3); hold on -% % % % mean_dim = squeeze(mean(dim_hooke(1,:,:),3)); [~, orderIdx] = sort(mean_dim); -% % % % violin(squeeze(dim_hooke(1,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -% % % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % % xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -% % % % title('Hooke, vlPFC') -% % % % -% % % % subplot(2,2,4); hold on -% % % % mean_dim_teo = squeeze(mean(dim_hooke(2,:,:),3)); [~, orderIdx] = sort(mean_dim_teo); -% % % % violin(squeeze(dim_hooke(2,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -% % % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % % xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -% % % % title('Hooke, TEO') -% % % % -% % % % saveas(gcf,'DimensionalityPerBehav_GroomCateg.pdf') \ No newline at end of file diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_perBehav_old.m b/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_perBehav_old.m deleted file mode 100644 index cf98896..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_perBehav_old.m +++ /dev/null @@ -1,276 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1;%set the smoothing window size (sigma) -var_explained_threshold=90; -num_iter = 100; UnitDatapoints = 10; -min_occurrences = 1000; -simplify=1; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -if simplify - dim = nan(length(session_range),4,2,UnitDatapoints,num_iter); -else - dim = nan(length(session_range),2,8,UnitDatapoints,num_iter); -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/Dimensionality_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO"] - %channel_flag = "vlPFC"; - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey,... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, ... - with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - - behavior_labels = cell2mat({labels{:,3}}'); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Threat to partner"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Threat to subject"); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - if simplify - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Lump Drinking and foraging - behavior_labels(behavior_labels==find(behav_categ=="Drinking"))=find(behav_categ=="Foraging"); - - %Lump all grooming together - behavior_labels(behavior_labels==find(behav_categ=="Getting groomed"))=find(behav_categ=="Groom partner"); - % behavior_labels(behavior_labels==find(behav_categ=="Groom sollicitation"))=find(behav_categ=="Groom partner"); - % behavior_labels(behavior_labels==find(behav_categ=="Self-groom"))=find(behav_categ=="Groom partner"); - - behav = [1,5,7,18,29]; - else - behav = [4,5,7,8,9,10,24,29]; - end - - if simplify - - %Lump all grooming together -% behavior_labels(behavior_labels==find(behav_categ=="Getting groomed"))=find(behav_categ=="Groom partner"); -% behavior_labels(behavior_labels==find(behav_categ=="Groom sollicitation"))=find(behav_categ=="Groom partner"); -% behavior_labels(behavior_labels==find(behav_categ=="Self-groom"))=find(behav_categ=="Groom partner"); - - %Lump all that is not grooming together - behavior_labels(ismember(behavior_labels,find(behav_categ~="Groom partner")))=find(behav_categ=="Rest"); - - behav = [find(behav_categ=="Groom partner"),find(behav_categ=="Rest")]; - else - behav = [4,5,7,8,9,10,24,29]; - end - - behav_freq_table = tabulate(behavior_labels); - - %% Compute dimensionality over increasing numbers of units, over multiple iterations - - for b = 1:length(behav) - - disp(['Behavior: ' behav_categ(behav(b))]) - - u = 1; unit_num_range = round(linspace(5,size(Spike_count_raster,2),UnitDatapoints)); - %u = 1; unit_num_range = round(linspace(5,min(unit_count),UnitDatapoints)); - for unit_num = unit_num_range - - disp(['Unit num: ' num2str(unit_num)]) - - for iter = 1:num_iter - - %Select time points to run PCA - idx= find(ismember(behavior_labels,behav(b))); - idx_beh = idx(randsample(1:length(idx),min_occurrences)); - - %Select unit to run PCA - Input_matrix = Spike_count_raster(idx_beh,randsample(size(Spike_count_raster,2), unit_num)); - - %figure; hold on; hist(corr(Input_matrix)) - - %PCA - [coeff,score,~,~,explained] = pca(Input_matrix); - - %Get dimensionality - var_explained = cumsum(explained); - idxl = find(var_explained>=var_explained_threshold); - dim(s,chan,b,u,iter) = min(idxl); - - end % end of interation loop - u=u+1; - end %end of unit loop - - end %end of behavior loop - - chan=chan+1; - disp([channel_flag ' done']) - end %end of channel loop - - %% Plot results for the session - - Cmap = [[1 0 0];[1 0.4 0.1];[0 0 0];[0.1 0.8 0.9];[0 0.7 0];[1 0 1];[0 1 1];... - [0 0 1];[0.8 0 0];[1 0 0];[0 0 0];[0.2 0.9 0.76];[0 0 0];[0 0 0];[0.7 0 1];... - [0 0 0];[0 0 0];[0.9 0.5 0];[0 0 0];[0 0 0];[0.8 0 0];[1 0 0];[0.9 0.7 0.12];[0.5 0.2 0.5];... - [0 0 0];[0 0 0];[0.8 0.4 0.4];[0 0 0];[0.5 0.5 0.5]]; - -% figure; hold on; set(gcf,'Position',[150 250 1000 400]) -% -% subplot(1,2,1); hold on -% for beh = 1:length(behav) -% mean_dim_vlpfc = squeeze(mean(dim(s,1,beh,:,:),5)); -% sd_dim_vlpfc = squeeze(std(dim(s,1,beh,:,:),0,5)); -% -% errorbar(mean_dim_vlpfc, sd_dim_vlpfc,'-s','MarkerSize',10, ... -% 'MarkerEdgeColor','k','MarkerFaceColor',Cmap(behav(beh),:)) -% % legend({behav_categ(beh)},'Location','best') -% xticks([1:length(unit_num_range)]); xlim([0.8 length(unit_num_range)+0.2]); -% xticklabels(unit_num_range); ylim([0 35]) -% ax = gca; -% ax.FontSize = 14; -% ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 18); xlabel('#units included','FontSize', 18) -% end -% title('vlPFC') -% -% subplot(1,2,2); hold on -% for beh = 1:length(behav) -% mean_dim_teo = squeeze(mean(dim(s,2,beh,:,:),5)); -% sd_dim_teo = squeeze(std(dim(s,2,beh,:,:),0,5)); -% -% errorbar(mean_dim_teo, sd_dim_teo,'-s','MarkerSize',10, ... -% 'MarkerEdgeColor','k','MarkerFaceColor',Cmap(behav(beh),:)) -% % legend({behav_categ(beh)},'Location','best') -% xticks([1:length(unit_num_range)]); xlim([0.8 length(unit_num_range)+0.2]); -% xticklabels(unit_num_range); ylim([0 35]) -% ax = gca; -% ax.FontSize = 14; -% ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 18); xlabel('#units included','FontSize', 18) -% end -% title('TEO') -% saveas(gcf,[savePath '/DimensionalityPerBehav.png']) -% -% close all - -end%end of session loop - -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/Dimensionality_results/']); -save('Dimensionality_2Classes.mat', "dim","behav","a_sessions","h_sessions","behav_categ","Cmap") - -%% Plot results across sessions -dim_amos = squeeze(mean(dim(a_sessions,:,:,:,:),1)); -dim_hooke = squeeze(mean(dim(h_sessions,:,:,:,:),1)); - -figure; hold on; set(gcf,'Position',[150 250 1000 800]); upperlim=35; - -%Amos -subplot(2,2,1); hold on -for beh = 1:length(behav) - mean_dim_vlpfc = squeeze(mean(dim_amos(1,beh,:,:),4)); - sd_dim_vlpfc = squeeze(std(dim_amos(1,beh,:,:),0,4)); - - errorbar(mean_dim_vlpfc, sd_dim_vlpfc,'-s','MarkerSize',10, ... - 'MarkerEdgeColor','k','MarkerFaceColor',Cmap(behav(beh),:)) - % legend({behav_categ(beh)},'Location','best') - xticks([1:length(unit_num_range)]); xlim([0.8 length(unit_num_range)+0.2]); - xticklabels(unit_num_range); ylim([0 upperlim]) - ax = gca; - ax.FontSize = 14; - ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 18); xlabel('#units included','FontSize', 18) -end -title('Amos, vlPFC') - -subplot(2,2,2); hold on -for beh = 1:length(behav) - mean_dim_teo = squeeze(mean(dim_amos(2,beh,:,:),4)); - sd_dim_teo = squeeze(std(dim_amos(2,beh,:,:),0,4)); - - errorbar(mean_dim_teo, sd_dim_teo,'-s','MarkerSize',10, ... - 'MarkerEdgeColor','k','MarkerFaceColor',Cmap(behav(beh),:)) - % legend({behav_categ(beh)},'Location','best') - xticks([1:length(unit_num_range)]); xlim([0.8 length(unit_num_range)+0.2]); - xticklabels(unit_num_range); ylim([0 upperlim]) - ax = gca; - ax.FontSize = 14; - ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 18); xlabel('#units included','FontSize', 18) -end -title('Amos, TEO') - -%Hooke -subplot(2,2,3); hold on -for beh = 1:length(behav) - mean_dim_vlpfc = squeeze(mean(dim_hooke(1,beh,:,:),4)); - sd_dim_vlpfc = squeeze(std(dim_hooke(1,beh,:,:),0,4)); - - errorbar(mean_dim_vlpfc, sd_dim_vlpfc,'-s','MarkerSize',10, ... - 'MarkerEdgeColor','k','MarkerFaceColor',Cmap(behav(beh),:)) - % legend({behav_categ(beh)},'Location','best') - xticks([1:length(unit_num_range)]); xlim([0.8 length(unit_num_range)+0.2]); - xticklabels(unit_num_range); ylim([0 upperlim]) - ax = gca; - ax.FontSize = 14; - ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 18); xlabel('#units included','FontSize', 18) -end -title('Hooke, vlPFC') - -subplot(2,2,4); hold on -for beh = 1:length(behav) - mean_dim_teo = squeeze(mean(dim_hooke(2,beh,:,:),4)); - sd_dim_teo = squeeze(std(dim_hooke(2,beh,:,:),0,4)); - - errorbar(mean_dim_teo, sd_dim_teo,'-s','MarkerSize',10, ... - 'MarkerEdgeColor','k','MarkerFaceColor',Cmap(behav(beh),:)) - % legend({behav_categ(beh)},'Location','best') - xticks([1:length(unit_num_range)]); xlim([0.8 length(unit_num_range)+0.2]); - xticklabels(unit_num_range); ylim([0 upperlim]) - ax = gca; - ax.FontSize = 14; - ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 18); xlabel('#units included','FontSize', 18) -end -title('Hooke, TEO') diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_perBehav_v2.m b/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_perBehav_v2.m deleted file mode 100644 index 1865999..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_perBehav_v2.m +++ /dev/null @@ -1,507 +0,0 @@ -%%log_Dimensionality_perBehav -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 10; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1;%set the smoothing window size (sigma) -var_explained_threshold=90; -num_iter = 500; num_units = 100; -simplify=4; -threat_precedence =1; -exclude_sq=1; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - - -%Set colormap -Cmap = [[1 0 0];...%Aggression; red - [1 0.4 0.1];...%Approach; dark orange - [0 0 0];...%But sniff; NA - [0.3 0.7 1];...%Drinking; light blue - [0 0.7 0];...%Foraging; dark green - [1 0 1];...%Groom sollicitation; magenta - [0 1 1];...%Groom partner; cyan - [0 0 1];...%Getting groomed; dark blue - [0.8 0 0];...%Threat to partner; dark red - [1 0 0];...%Threat to subject; red - [0.9 0.9 0];...%leave; dark yellow - [0 0 0];...%Lipsmack - [0.2 0.9 0.76];...%Masturbating; turquoise - [0.7 0 1];...%Mounting; light purple - [0.9 0.5 0];...%Other monkeys vocalize; orange - [1 0.8 0.1];...%Travel; yellow orange - [0 0 0];...%Proximity; NA - [0 0 0];...%Rowdy room; NA - [1 0 0];...%SP; NA - [1 0 0];...%SS; NA - [0.6314 0.5059 0.0118];...%Scratch; maroon - [0.5 0.2 0.5];...%Self-groom; dark purple - [ 1 0.07 0.65];...%Submission; dark pink - [0 0.4 0.5];...%Vocalzation; blue green - [0 0 0];...%Yawning; NA - [0.8 0.8 0.8]];%Rest; grey - -%Set behaviors of interest to compare and associated colormap -if simplify ==1 - min_occurrences = 50*temp_resolution; -elseif simplify ==2 - min_occurrences = 100*temp_resolution; -elseif simplify ==3 - min_occurrences = 100*temp_resolution; -else - min_occurrences = 50*temp_resolution; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/Dimensionality_results/']; - - chan = 1; - - %for channel_flag = ["vlPFC", "TEO"] - %channel_flag = "vlPFC"; - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - - behavior_labels = cell2mat({labels{:,3}}'); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - if simplify == 1 %Compare across 5 behavioral categories that are common across sessions - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Lump Drinking and foraging - behavior_labels(behavior_labels==find(behav_categ=="Drinking"))=find(behav_categ=="Foraging"); - - %Lump all grooming together - behavior_labels(behavior_labels==find(behav_categ=="Getting groomed"))=find(behav_categ=="Groom partner"); - behavior_labels(behavior_labels==find(behav_categ=="Groom sollicitation"))=find(behav_categ=="Groom partner"); - behavior_labels(behavior_labels==find(behav_categ=="Self-groom"))=find(behav_categ=="Groom partner"); - - behav = [1,5,7,18,29]; - - elseif simplify == 2 % Compare one behavior (grooming or foraging) to all others - - %Lump all that is not grooming together - behavior_labels(ismember(behavior_labels,find(behav_categ~="Groom partner" & behav_categ~="Getting groomed")))=find(behav_categ=="Rest"); - - behav = [find(behav_categ=="Getting groomed"),find(behav_categ=="Rest")]; - -% % %Lump all that is not foraging -% % behavior_labels(ismember(behavior_labels,find(behav_categ~="Foraging" )))=find(behav_categ=="Rest"); -% % -% % behav = [find(behav_categ=="Foraging"),find(behav_categ=="Rest")]; - - elseif simplify == 3 %Comapre grooming behaviors between each other - - %Lump all that is not grooming together - behavior_labels(ismember(behavior_labels,find(behav_categ~="Groom partner" & behav_categ~="Getting groomed" & behav_categ~="Self-groom")))=find(behav_categ=="Rest"); - - behav = [find(behav_categ=="Groom partner"),find(behav_categ=="Getting groomed"),find(behav_categ=="Self-groom"),find(behav_categ=="Rest")]; - - else %Compare all behaviors separately (without pooling across) - behav = [find(behav_categ=="Drinking"),find(behav_categ=="Foraging"),... - find(behav_categ=="Groom partner"),find(behav_categ=="Getting groomed"),... - find(behav_categ=="Threat to partner"),find(behav_categ=="Threat to subject"),... - find(behav_categ=="Self-groom"),find(behav_categ=="Rest")]; - end - - behav_freq_table = tabulate(behavior_labels); - - %% Compute dimensionality over increasing numbers of units, over multiple iterations - - idx_all_beh= find(ismember(behavior_labels,behav)); - behav_freq=tabulate(behavior_labels(idx_all_beh)); - n_per_behav{s} = behav_freq(behav_freq(:,2)>0,2); - - if all(behav_freq(behav_freq(:,2)>0,2)>min_occurrences) && length(find(behav_freq(:,2)>0))>=length(behav) - - for b = 1:length(behav) - - disp(['Behavior: ' behav_categ(behav(b))]) - - - for iter = 1:num_iter - - %Select time points - idx= find(ismember(behavior_labels, behav(b))); - idx_beh = idx(randsample(1:length(idx), min_occurrences)); %subsample number of indices - - %Select neural data - neural_data = (Spike_count_raster(idx_beh,randsample(size(Spike_count_raster,2), num_units))); - - %Compute mean firing rate, variance and correlation in response - mean_Hz{s, b}(iter,:) = mean(neural_data); - std_Hz{s, b}(iter,:) = std(neural_data); - cv_Hz{s, b}(iter,:) = std(neural_data)./mean(neural_data); - corr_data=reshape(triu(corrcoef(neural_data)), 1,[]); - correl_Hz{s, b}(iter,:) = corr_data; - - %PCA - Input_matrix = neural_data; - [coeff,score,latent,~,explained] = pca(Input_matrix); - - %Get dimensionality - loadings(iter,:) = sum(abs(coeff),1); %Number of units involved in each PC - var_explained_cumul(iter,:) = cumsum(explained); %Cumulative variance explained - var_explained(iter,:)= explained; %Variance explained at each PC - var_explained_logEigen(iter,:) = log10(latent); %Log of eigenvalues - var_explained_Eigen(iter,:) = latent; %eigenvalues - total_var{b}(s,iter) =sum(latent); %Total variance = sum of eigenvalues - total_vol{b}(s,iter) = sum(log10(latent)); %Total volume occupied in neural space = sum of log(eigenvalues) - idxl = find(var_explained_cumul(iter,:)>=var_explained_threshold);%get # dimensions needed to explain 90% of variation - dim{b}(s,iter) = min(idxl); - explained_var_1D{b}(s,iter) = explained(1); %Get variance explained in 1PC - Eigenval_1D{b}(s,iter) = latent(1); %Get eigenvalue of first PC - - - - end % end of interation loop - - %Average over subsampling iterations to get dimensionality - %values for the session - var_explained_cumul_mean{b}(s,:) = mean(var_explained_cumul); - var_explained_mean{b}(s,:)= mean(var_explained); - var_explained_logEigen_mean{b}(s,:) = mean(var_explained_logEigen); - var_explained_Eigen_mean{b}(s,:) = mean(var_explained_Eigen); - loadings_shesh{b,s} = mean(loadings); - dim_mean{b}(s) = mean(dim{b}(s,:)); - - end %end of behavior loop - - - -% chan=chan+1; -% disp([channel_flag ' done']) - %end %end of channel loop - - %% Plot results for the session - - -% % % % figure; hold on -% % % % for b=1:size(var_explained_cumul_mean,2) -% % % % plot(var_explained_cumul_mean{b}(s,:),'LineWidth',3, 'Color', Cmap(behav(b),:)) -% % % % end -% % % % yline(90,'LineStyle','--') -% % % % xline(3,'LineStyle',':') -% % % % xlabel('Dimensions') -% % % % ylabel('Var. explained') -% % % % legend(behav_categ(behav)) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % -% % % % figure; hold on -% % % % for b=1:size(var_explained_cumul_mean,2) -% % % % plot(var_explained_logEigen_mean{b}(s,:),'LineWidth',3, 'Color', Cmap(behav(b),:)) -% % % % end -% % % % xlabel('Dimensions') -% % % % ylabel('Var. explained') -% % % % legend(behav_categ(behav)) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % %saveas(gcf,[savePath '/DimensionalityPerBehav_allExplainedVar.pdf']) -% % % % -% % % % figure; hold on; set(gcf,'Position',[150 250 600 400]) -% % % % -% % % % dim_plot = squeeze(squeeze(dim(s,1,:,:)))'; -% % % % mean_dim = squeeze(mean(dim(s,1,:,:),4)); [~, orderIdx] = sort(mean_dim); -% % % % violin(dim_plot(:,orderIdx), 'facecolor',Cmap(behav(orderIdx),:)) -% % % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % % xticklabels(behav_categ(behav(orderIdx))); %ylim([0 35]) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -% % % % title('Dimensionality across behaviors') - % - % saveas(gcf,[savePath '/DimensionalityPerBehav_2categ.pdf']) - - -% % % figure; hold on; set(gcf,'Position',[150 250 600 400]) -% % % -% % % dim_plot = squeeze(squeeze(var_expl_dim3(s,1,:,:)))'; -% % % mean_dim = squeeze(mean(var_expl_dim3(s,1,:,:),4)); [~, orderIdx] = sort(mean_dim); -% % % violin(dim_plot(:,orderIdx), 'facecolor',Cmap(behav(orderIdx),:)) -% % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % xticklabels(behav_categ(behav(orderIdx))); %ylim([0 35]) -% % % ax = gca; -% % % ax.FontSize = 14; -% % % ylabel(['Variance explained at 3D'],'FontSize', 14); -% % % title('Variance explained at 3D') - - -% % % figure; hold on; set(gcf,'Position',[150 250 600 400]) -% % % -% % % vol_plot = squeeze(squeeze(vol(s,1,:,:)))'; -% % % mean_vol = squeeze(mean(vol(s,1,:,:),4)); [~, orderIdx] = sort(mean_vol); -% % % violin(vol_plot(:,orderIdx), 'facecolor',Cmap(behav(orderIdx),:)) -% % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % xticklabels(behav_categ(behav(orderIdx))); %ylim([0 35]) -% % % ax = gca; -% % % ax.FontSize = 14; -% % % ylabel(['Volume in 3D space'],'FontSize', 14); -% % % title('Volume across behaviors') - % - % close all - - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - disp(s) - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - end % end of if clause - -end%end of session loop - -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/Dimensionality_results/']); -save('Dimensionality_acrossBehav_100Units_50sec_8categ.mat', "dim","var_explained_cumul_mean", ... - "var_explained_logEigen_mean","behav","a_sessions","h_sessions","behav_categ","Cmap", ... - "mean_Hz","std_Hz","cv_Hz","correl_Hz") - -%% Plot results across sessions - - -%Mean firing rate -for b=1:length(behav) - mean_Hz_all(:,b) = reshape(cell2mat(mean_Hz(:,b)),[],1); -end -[~, idx_sort]=sort(nanmean(mean_Hz_all)); -figure; hold on -violin(mean_Hz_all(:,idx_sort)) -xlim([0 length(behav)+1]); ylabel('Mean firing rate'); %ylim([0 6]) -xticks([1:length(behav)]); xticklabels(behav_categ(behav(idx_sort))) - -%Variation in firing rate measured by SD -for b=1:length(behav) - std_Hz_all(:,b) = reshape(cell2mat(std_Hz(:,b)),[],1); -end -[~, idx_sort]=sort(nanmean(std_Hz_all)); -figure; hold on -violin(std_Hz_all(:,idx_sort)) -xlim([0 length(behav)+1]); ylabel('Variation in firing rate (SD)'); %ylim([0 6]) -xticks([1:length(behav)]); xticklabels(behav_categ(behav(idx_sort))) - -%Coefficient of variation -for b=1:length(behav) - cv_Hz_all(:,b) = reshape(cell2mat(cv_Hz(:,b)),[],1); -end -[~, idx_sort]=sort(nanmean(cv_Hz_all)); -figure; hold on -violin(cv_Hz_all(:,idx_sort)) -xlim([0 length(behav)+1]); ylabel('Coefficient variation'); ylim([0 6]) -xticks([1:length(behav)]); xticklabels(behav_categ(behav(idx_sort))) - - -%Correlation between neurons -for b=1:length(behav) - correl_Hz_all(:,b) = reshape(cell2mat(correl_Hz(:,b)),[],1); - idx_nan = (correl_Hz_all(:,b)== 1 | correl_Hz_all(:,b)== 0); - correl_Hz_all(idx_nan,b)=nan; -end -[~, idx_sort]=sort(nanmean(correl_Hz_all)); -figure; hold on -boxchart(correl_Hz_all(:,idx_sort)) -ylabel('Correlation between neurons'); %ylim([0 6]) -xticklabels(behav_categ(behav(idx_sort))) - - -%Variance explained cumulated -figure; hold on; clear y -for b=1:length(behav) - var_explained_cumul_mean{b}(var_explained_cumul_mean{b}==0)=nan; - y=nanmean(var_explained_cumul_mean{b})'; - plot(y,'LineWidth',3,'Color',Cmap(behav(b),:)) - xlabel('Dimensions') - ylabel('Cumulative % variance explained') - ylim([0 100]) - ax = gca; - ax.FontSize = 14; -end -yline(90,'LineStyle','-') -legend(behav_categ(behav)) - -%Log eigenvalues -figure; hold on; clear y -for b=1:length(behav) - var_explained_logEigen_mean{b}(var_explained_logEigen_mean{b}==0)=nan; - y=nanmean(var_explained_logEigen_mean{b})'; - plot(y,'LineWidth',3,'Color',Cmap(behav(b),:)) - xlabel('Dimensions') - ylabel('Log10 Eigenvalues') - ax = gca; - ax.FontSize = 14; -end -%ylim([-1.5 1.5]) -legend(behav_categ(behav)) - -%Plot # dimensions needed to explain 90% of the variance. -%All Pooled - -for b=1:length(behav) - dim{b}(dim{b}==0)=nan; - dim{b}=reshape(dim{b},[],1); -end -dim_all = cell2mat(dim); -[~, idx_sort]=sort(nanmedian(dim_all)); -figure; hold on -boxchart(dim_all(:,idx_sort)) -ylabel('# Dim to explain 90% of variance') -xticklabels(behav_categ(behav(idx_sort))) - - -%Plot the full relationship between # dimensions and explained variance -mean_alldims = mean(cat(3,(var_explained_mean{:})),3); -sd_alldims = std(cat(3,(var_explained_mean{:})),[],3); -figure; hold on -for b=1:size(mean_alldims,2) - y=mean_alldims(:,b); - sd=sd_alldims(:,b); - upper_lim=y+sd; - lower_lim=y-sd; - p = fill([1:length(y) length(y):-1:1],[upper_lim; flip(lower_lim)],'red'); - p.FaceColor = Cmap(behav(b),:); - p.FaceAlpha = 0.1; - p.EdgeColor = 'none'; - - plot(y,'LineWidth',3, 'Color', Cmap(behav(b),:)) -end -yline(90,'LineStyle','--') -xline(3,'LineStyle',':') -xlabel('Dimensions') -ylabel('Var. explained') -legend(behav_categ(behav)) -ax = gca; -ax.FontSize = 14; - saveas(gcf,'DimensionalityPerBehav_allExplainedVar.pdf') - -%Plot the full relationship between # dimensions and explained variance -mean_alldims_notCumul = mean(cat(3,(var_explained_notCumul_mean{:})),3); -sd_alldims_notCumul = std(cat(3,(var_explained_notCumul_mean{:})),[],3); -figure; hold on -for b=1:size(mean_alldims,2) - y=mean_alldims_notCumul(:,b); - sd=sd_alldims_notCumul(:,b); - upper_lim=y+sd; - lower_lim=y-sd; - p = fill([1:length(y) length(y):-1:1],[upper_lim; flip(lower_lim)],'red'); - p.FaceColor = Cmap(behav(b),:); - p.FaceAlpha = 0.1; - p.EdgeColor = 'none'; - - plot(y,'LineWidth',3, 'Color', Cmap(behav(b),:)) -end -xlabel('Dimensions') -ylabel('Variance (log10)') -%legend(behav_categ(behav)) -ax = gca; -ax.FontSize = 14; - -% % % % %Separate per brain area, pooling both monkeys -% % % % figure; hold on; set(gcf,'Position',[150 250 1000 500]); lowlimit=20; uplimit=35; -% % % % subplot(1,2,1); hold on -% % % % mean_dim = squeeze(mean(dim_all(1,:,:),3)); [~, orderIdx] = sort(mean_dim); -% % % % violin(squeeze(dim_all(1,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -% % % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % % xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -% % % % title('vlPFC') -% % % % -% % % % subplot(1,2,2); hold on -% % % % mean_dim_teo = squeeze(mean(dim_all(2,:,:),3)); [~, orderIdx] = sort(mean_dim_teo); -% % % % violin(squeeze(dim_all(2,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -% % % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % % xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -% % % % title('TEO') -% % % % saveas(gcf,'DimensionalityPerBehav_GroomCateg.pdf') -% % % % -% % % % -% % % % %Separated by monkey and brain area -% % % % figure; hold on; set(gcf,'Position',[150 250 1000 800]); lowlimit=25; uplimit=45; -% % % % -% % % % %Amos -% % % % subplot(2,2,1); hold on -% % % % mean_dim = squeeze(mean(dim_amos(1,:,:),3)); [~, orderIdx] = sort(mean_dim); -% % % % violin(squeeze(dim_amos(1,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -% % % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % % xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -% % % % title('Amos, vlPFC') -% % % % -% % % % subplot(2,2,2); hold on -% % % % mean_dim_teo = squeeze(mean(dim_amos(2,:,:),3)); [~, orderIdx] = sort(mean_dim_teo); -% % % % violin(squeeze(dim_amos(2,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -% % % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % % xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -% % % % title('Amos, TEO') -% % % % -% % % % %Hooke -% % % % subplot(2,2,3); hold on -% % % % mean_dim = squeeze(mean(dim_hooke(1,:,:),3)); [~, orderIdx] = sort(mean_dim); -% % % % violin(squeeze(dim_hooke(1,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -% % % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % % xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -% % % % title('Hooke, vlPFC') -% % % % -% % % % subplot(2,2,4); hold on -% % % % mean_dim_teo = squeeze(mean(dim_hooke(2,:,:),3)); [~, orderIdx] = sort(mean_dim_teo); -% % % % violin(squeeze(dim_hooke(2,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -% % % % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -% % % % xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -% % % % title('Hooke, TEO') -% % % % -% % % % saveas(gcf,'DimensionalityPerBehav_GroomCateg.pdf') \ No newline at end of file diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_perBlock.m b/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_perBlock.m deleted file mode 100644 index 4ef1586..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_perBlock.m +++ /dev/null @@ -1,249 +0,0 @@ -%%log_Dimensionality_perBehav -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1;%set the smoothing window size (sigma) -var_explained_threshold=90; -num_iter = 500; num_units = 100; -min_occurrences = 200; -agg_precedence=0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -% dim=nan([length(session_range),2,length(behav),2,num_iter]); - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/Dimensionality_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO", "all"] - %channel_flag = "vlPFC"; - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - - block_labels = cell2mat({labels{:,13}}'); - - - %% Select behaviors to decode - block=[0,1]; - - % For all behaviors - for bl=1:length(block) - - %Only keep the behaviors of interest - idx = find(ismember(block_labels,block(bl))); %find the indices of the behaviors considered - - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = block_labels(idx,:);%Same as above but in behavior labels - - for iter = 1:num_iter - - %Select time points to run PCA - idx = randsample(1:length(Spike_count_raster_final),min_occurrences); - - %Select unit to run PCA - Input_matrix = zscore(Spike_count_raster(idx,randsample(size(Spike_count_raster,2), num_units))); - - %figure; hold on; hist(corr(Input_matrix)) - - %PCA - [coeff,score,~,~,explained] = pca(Input_matrix); - - %Get dimensionality - var_explained = cumsum(explained); - idxl = find(var_explained>=var_explained_threshold); - dim(s,chan,bl,iter) = min(idxl); - end - - - end % end of block loop - - chan=chan+1; - disp([channel_flag ' done']) - end %end of channel loop - - %% Plot results for the session - - Cmap = [[1 0 0];[1 0.4 0.1];[0 0 0];[0.1 0.8 0.9];[0 0.7 0];[1 0 1];[0 1 1];... - [0 0 1];[0.8 0 0];[1 0 0];[0 0 0];[0.2 0.9 0.76];[0 0 0];[0 0 0];[0.7 0 1];... - [0 0 0];[0 0 0];[0.9 0.5 0];[0 0 0];[0 0 0];[0.8 0 0];[1 0 0];[0.9 0.7 0.12];[0.5 0.2 0.5];... - [0 0 0];[0 0 0];[0.8 0.4 0.4];[0 0 0];[0.5 0.5 0.5]]; -% -% blck_lbls = {'Paired','Alone'}; -% -% figure; hold on; set(gcf,'Position',[150 250 1000 400]) -% -% for b=length(behav) -% subplot(1,length(behav),b); hold on -% dim_vlpfc = squeeze(squeeze(dim(s,1,b,:,:)))'; -% mean_dim_vlpfc = squeeze(nanmean(dim(s,1,b,:,:),5)); [~, orderIdx] = sort(mean_dim_vlpfc); -% %sd_dim_vlpfc = squeeze(nanstd(dim(s,1,:,:),0,4)); -% violin(dim_vlpfc(:,orderIdx), 'facecolor',Cmap(orderIdx,:)) -% xticks([1:length(blocks)]); xlim([0.5 length(blocks)+0.5]); -% xticklabels(blck_lbls(orderIdx)); ylim([0 35]) -% ax = gca; -% ax.FontSize = 14; -% ylabel(['Dimensionality'],'FontSize', 14); -% title(behav_categ(behav(b))) -% end -% sgtitle('vlPFC') -% -% figure; hold on; set(gcf,'Position',[150 250 1000 400]) -% for b=1:length(behav) -% subplot(1,length(behav),b); hold on -% dim_vlpfc = squeeze(squeeze(dim(s,2,b,:,:)))'; -% mean_dim_vlpfc = squeeze(nanmean(dim(s,2,b,:,:),5)); [~, orderIdx] = sort(mean_dim_vlpfc); -% %sd_dim_vlpfc = squeeze(nanstd(dim(s,1,:,:),0,4)); -% violin(dim_vlpfc(:,orderIdx), 'facecolor',Cmap(orderIdx,:)) -% xticks([1:length(blocks)]); xlim([0.5 length(blocks)+0.5]); -% xticklabels(blck_lbls(orderIdx)); ylim([0 35]) -% ax = gca; -% ax.FontSize = 14; -% ylabel(['Dimensionality'],'FontSize', 14); -% end -% title('vlPFC') -% -% close all - -end%end of session loop - -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/Dimensionality_results/']); -save('Dimensionality_block.mat', "dim","block","a_sessions","h_sessions","behav_categ","Cmap") - -%% Plot results across sessions - -load('Dimensionality_block.mat') - -dim_all = squeeze(nanmean(dim(:,3,:,:),1)); -blck_lbls = {'Alone','Paired'}; - -%Pooling monkeys and areas -figure; hold on; set(gcf,'Position',[150 250 1000 500]); lowlimit=24; uplimit=29; -mean_dim = mean(dim_all,2); [~, orderIdx] = sort(mean_dim); -violin(dim_all(orderIdx,:)', 'facecolor',Cmap(orderIdx,:)) -xticks([1:length(blck_lbls)]); xlim([0.5 length(blck_lbls)+0.5]); -xticklabels(blck_lbls(orderIdx)); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -[h, p]=ttest(dim_all(1,:), dim_all(2,:)); abs(computeCohen_d(dim_all(1,:), dim_all(2,:), 'paired')) -saveas(gcf,'DimensionalityPerBlock.pdf') - - -%Pooling both monkeys -figure; hold on; set(gcf,'Position',[150 250 1000 500]); lowlimit=15; uplimit=30; -subplot(1,2,1); hold on -mean_dim_vlpfc = squeeze(mean(dim_all(1,:,:),3)); [~, orderIdx] = sort(mean_dim_vlpfc); -violin(squeeze(dim_all(1,orderIdx,:))', 'facecolor',Cmap(orderIdx,:)) -xticks([1:length(blck_lbls)]); xlim([0.5 length(blck_lbls)+0.5]); -xticklabels(blck_lbls(orderIdx)); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('vlPFC') - -subplot(1,2,2); hold on -mean_dim_teo = squeeze(mean(dim_all(2,:,:),3)); [~, orderIdx] = sort(mean_dim_teo); -violin(squeeze(dim_all(2,orderIdx,:))', 'facecolor',Cmap(orderIdx,:)) -xticks([1:length(blck_lbls)]); xlim([0.5 length(blck_lbls)+0.5]); -xticklabels(blck_lbls(orderIdx)); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('TEO') -saveas(gcf,'DimensionalityPerBehav_GroomCateg.pdf') - - - -%Separated by monkey -figure; hold on; set(gcf,'Position',[150 250 1000 800]); lowlimit=25; uplimit=45; - -%Amos -subplot(2,2,1); hold on -mean_dim_vlpfc = squeeze(mean(dim_amos(1,:,:),3)); [~, orderIdx] = sort(mean_dim_vlpfc); -violin(squeeze(dim_amos(1,orderIdx,:))', 'facecolor',Cmap(block(orderIdx),:)) -xticks([1:length(block)]); xlim([0.5 length(block)+0.5]); -xticklabels(behav_categ(block(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('Amos, vlPFC') - -subplot(2,2,2); hold on -mean_dim_teo = squeeze(mean(dim_amos(2,:,:),3)); [~, orderIdx] = sort(mean_dim_teo); -violin(squeeze(dim_amos(2,orderIdx,:))', 'facecolor',Cmap(block(orderIdx),:)) -xticks([1:length(block)]); xlim([0.5 length(block)+0.5]); -xticklabels(behav_categ(block(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('Amos, TEO') - -%Hooke -subplot(2,2,3); hold on -mean_dim_vlpfc = squeeze(mean(dim_hooke(1,:,:),3)); [~, orderIdx] = sort(mean_dim_vlpfc); -violin(squeeze(dim_hooke(1,orderIdx,:))', 'facecolor',Cmap(block(orderIdx),:)) -xticks([1:length(block)]); xlim([0.5 length(block)+0.5]); -xticklabels(behav_categ(block(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('Hooke, vlPFC') - -subplot(2,2,4); hold on -mean_dim_teo = squeeze(mean(dim_hooke(2,:,:),3)); [~, orderIdx] = sort(mean_dim_teo); -violin(squeeze(dim_hooke(2,orderIdx,:))', 'facecolor',Cmap(block(orderIdx),:)) -xticks([1:length(block)]); xlim([0.5 length(block)+0.5]); -xticklabels(behav_categ(block(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('Hooke, TEO') - -saveas(gcf,'DimensionalityPerBehav_GroomCateg.pdf') \ No newline at end of file diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_perBlock_withinBehav.m b/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_perBlock_withinBehav.m deleted file mode 100644 index 52dfd48..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_perBlock_withinBehav.m +++ /dev/null @@ -1,277 +0,0 @@ -%%log_Dimensionality_perBehav -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1;%set the smoothing window size (sigma) -var_explained_threshold=90; -num_iter = 500; num_units = 100; -min_occurrences = 100; -behav=29;%[1,5,18,29]; -agg_precedence = 1; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -dim=nan([length(session_range),3,3,num_iter]); - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/Dimensionality_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO", "all"] - %channel_flag = "vlPFC"; - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - - behavior_labels = cell2mat({labels{:,3}}'); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - block_labels = cell2mat({labels{:,12}}'); - - - %% Select behaviors to decode - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Lump Drinking and foraging - behavior_labels(behavior_labels==find(behav_categ=="Drinking"))=find(behav_categ=="Foraging"); - - % For all behaviors - for b=1:length(behav) - - disp(['Behavior: ' behav_categ(behav(b))]) - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav(b))); %find the indices of the behaviors considered - - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = block_labels(idx,:);%Same as above but in behavior labels - %behavior_labels_final(behavior_labels_final==2)=1; %pool the paired blocks together - behavfreq= tabulate(behavior_labels_final); - blocks = unique(behavior_labels_final); - - if all(behavfreq(:,2)>=min_occurrences) - - for bl = 1:length(blocks) - - for iter = 1:num_iter - - %Select time points to run PCA - idx= find(ismember(behavior_labels_final,blocks(bl))); - idx_beh = idx(randsample(1:length(idx),min_occurrences)); - - %Select unit to run PCA - Input_matrix = zscore(Spike_count_raster(idx_beh,randsample(size(Spike_count_raster,2), num_units))); - - %figure; hold on; hist(corr(Input_matrix)) - - %PCA - [coeff,score,~,~,explained] = pca(Input_matrix); - - %Get dimensionality - var_explained = cumsum(explained); - idxl = find(var_explained>=var_explained_threshold); - dim(s,chan,bl,iter) = min(idxl); - - end % end of interation loop - - end %end of block loop - - else - - disp(['Not enough occurrences across blocks for: ' behav_categ(behav(b))]) - - end % end of if clause - - end % end of behavior loop - - chan=chan+1; - disp([channel_flag ' done']) - end %end of channel loop - - %% Plot results for the session - - Cmap = [[1 0 0];[1 0.4 0.1];[0.5 0.5 0.5]]; -% -% blck_lbls = {'Paired','Alone'}; -% -% figure; hold on; set(gcf,'Position',[150 250 1000 400]) -% -% for b=length(behav) -% subplot(1,length(behav),b); hold on -% dim_vlpfc = squeeze(squeeze(dim(s,1,b,:,:)))'; -% mean_dim_vlpfc = squeeze(nanmean(dim(s,1,b,:,:),5)); [~, orderIdx] = sort(mean_dim_vlpfc); -% %sd_dim_vlpfc = squeeze(nanstd(dim(s,1,:,:),0,4)); -% violin(dim_vlpfc(:,orderIdx), 'facecolor',Cmap(orderIdx,:)) -% xticks([1:length(blocks)]); xlim([0.5 length(blocks)+0.5]); -% xticklabels(blck_lbls(orderIdx)); ylim([0 35]) -% ax = gca; -% ax.FontSize = 14; -% ylabel(['Dimensionality'],'FontSize', 14); -% title(behav_categ(behav(b))) -% end -% sgtitle('vlPFC') -% -% figure; hold on; set(gcf,'Position',[150 250 1000 400]) -% for b=1:length(behav) -% subplot(1,length(behav),b); hold on -% dim_vlpfc = squeeze(squeeze(dim(s,2,b,:,:)))'; -% mean_dim_vlpfc = squeeze(nanmean(dim(s,2,b,:,:),5)); [~, orderIdx] = sort(mean_dim_vlpfc); -% %sd_dim_vlpfc = squeeze(nanstd(dim(s,1,:,:),0,4)); -% violin(dim_vlpfc(:,orderIdx), 'facecolor',Cmap(orderIdx,:)) -% xticks([1:length(blocks)]); xlim([0.5 length(blocks)+0.5]); -% xticklabels(blck_lbls(orderIdx)); ylim([0 35]) -% ax = gca; -% ax.FontSize = 14; -% ylabel(['Dimensionality'],'FontSize', 14); -% end -% title('vlPFC') -% -% close all - - disp('Session done') - -end%end of session loop - -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/Dimensionality_results/']); -save('Dimensionality_BlockWithinRest.mat', "dim","behav","a_sessions","h_sessions","behav_categ","Cmap") - -%% Plot results across sessions - -dim_amos = squeeze(nanmean(dim(a_sessions,:,1,:,:),1)); -dim_hooke = squeeze(nanmean(dim(h_sessions,:,1,:,:),1)); -dim_all = squeeze(nanmean(dim(:,1,:,:),1)); -blck_lbls = {'Female Neighbor','Male Neighbor','Alone'}; - -%Pooling both monkeys and brain areas -figure; hold on; lowlimit=25; uplimit=35; -mean_dim = mean(dim_all,2); [~, orderIdx] = sort(mean_dim); -violin(squeeze(dim_all(orderIdx,:))', 'facecolor',Cmap(orderIdx,:)) -xticks([1:length(blck_lbls)]); xlim([0.5 length(blck_lbls)+0.5]); -xticklabels(blck_lbls(orderIdx)); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -saveas(gcf,'DimensionalityByBlock_Rest.pdf') - -%Pooling both monkeys -figure; hold on; set(gcf,'Position',[150 250 1000 500]); lowlimit=15; uplimit=30; -subplot(1,2,1); hold on -mean_dim_vlpfc = squeeze(mean(dim_all(1,:,:),3)); [~, orderIdx] = sort(mean_dim_vlpfc); -violin(squeeze(dim_all(1,orderIdx,:))', 'facecolor',Cmap(orderIdx,:)) -xticks([1:length(blck_lbls)]); xlim([0.5 length(blck_lbls)+0.5]); -xticklabels(blck_lbls(orderIdx)); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('vlPFC') - -subplot(1,2,2); hold on -mean_dim_teo = squeeze(mean(dim_all(2,:,:),3)); [~, orderIdx] = sort(mean_dim_teo); -violin(squeeze(dim_all(2,orderIdx,:))', 'facecolor',Cmap(orderIdx,:)) -xticks([1:length(blck_lbls)]); xlim([0.5 length(blck_lbls)+0.5]); -xticklabels(blck_lbls(orderIdx)); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('TEO') -saveas(gcf,'DimensionalityPerBehav_GroomCateg.pdf') - - - -%Separated by monkey -figure; hold on; set(gcf,'Position',[150 250 1000 800]); lowlimit=25; uplimit=45; - -%Amos -subplot(2,2,1); hold on -mean_dim_vlpfc = squeeze(mean(dim_amos(1,:,:),3)); [~, orderIdx] = sort(mean_dim_vlpfc); -violin(squeeze(dim_amos(1,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('Amos, vlPFC') - -subplot(2,2,2); hold on -mean_dim_teo = squeeze(mean(dim_amos(2,:,:),3)); [~, orderIdx] = sort(mean_dim_teo); -violin(squeeze(dim_amos(2,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('Amos, TEO') - -%Hooke -subplot(2,2,3); hold on -mean_dim_vlpfc = squeeze(mean(dim_hooke(1,:,:),3)); [~, orderIdx] = sort(mean_dim_vlpfc); -violin(squeeze(dim_hooke(1,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('Hooke, vlPFC') - -subplot(2,2,4); hold on -mean_dim_teo = squeeze(mean(dim_hooke(2,:,:),3)); [~, orderIdx] = sort(mean_dim_teo); -violin(squeeze(dim_hooke(2,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('Hooke, TEO') - -saveas(gcf,'DimensionalityPerBehav_GroomCateg.pdf') \ No newline at end of file diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_slidingWindow.asv b/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_slidingWindow.asv deleted file mode 100644 index 2e5d149..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_slidingWindow.asv +++ /dev/null @@ -1,167 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "vlPFC"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1;%set the smoothing window size (sigma) -var_explained_threshold=90; num_units = 100; -num_iter = 50; -window_size = 50; step = 5; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - - - for channel_flag = ["vlPFC", "TEO"] - %channel_flag = "vlPFC"; - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey,... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, ... - with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - - - %% Compute dimensionality for 100 units, over multiple iterations,over the whole session - - timepoints = window_size/2+1:step:size(Spike_count_raster,1)-window_size/2; - - for tp=1:length(timepoints) - - dim = nan(1,num_iter); - for iter = 1:num_iter - - %Select unit to run SVM - Input_matrix = Spike_count_raster(timepoints(tp)-window_size/2:timepoints(tp)+window_size/2-1,randsample(size(Spike_count_raster,2), num_units)); - - %PCA - [coeff,score,~,~,explained] = pca(Input_matrix); - - %Get dimensionality - var_explained = cumsum(explained); - idxl = find(var_explained>=var_explained_threshold); - dim(iter) = min(idxl); - - - end % end of interation loop - - disp([num2str(tp) '/' num2str(length(timepoints))]) - mean_dim(chan, tp) = mean(dim); - std_dim(chan, tp) = std(dim); - end - - chan=chan+1; - disp([channel_flag ' done']) - end %end of channel loop - - %% Plot results for the session - - vlpfc_dim = interp(mean_dim(1,:), step); vlpfc_dim = [ones(1,size(Spike_count_raster,1)-length(vlpfc_dim))*vlpfc_dim(1), vlpfc_dim]; - teo_dim = interp(mean_dim(2,:), step); teo_dim = [ones(1,size(Spike_count_raster,1)-length(teo_dim))*teo_dim(1), vlpfc_dim]; - corrcoef(vlpfc_dim,teo_dim) - - figure; hold on; set(gcf,'Position',[150 250 700 500]) - plot(vlpfc_dim) - plot(teo_dim) - - - %Get behavior indices - idx_groom = find(behavior_labels == 7); - idx_getgroom = find(behavior_labels == 8); - idx_selfgroom = find(behavior_labels == 24); - idx_forage = find(behavior_labels == 5 | behavior_labels == 4); - idx_agg = find(behavior_labels == 9 | behavior_labels == 10 |behavior_labels == 1 |behavior_labels == 21 |behavior_labels == 22); - idx_travel = find(behavior_labels == 2 | behavior_labels == 12 |behavior_labels == 18); - idx_scratch = find(behavior_labels == 23); - - xline([block_times.end_time_round(1), block_times.end_time_round(2)], "-",["Block 1 end", "Block 2 end"], "LineWidth",2); - %Full spike rate trace - plot(1:session_length, Spike_rasters_smooth(unit,1:session_length)); - - %Rest - to_plot=nan(1,session_length); to_plot(idx_rest)=Spike_rasters_smooth(unit,idx_rest); - plot(1:session_length, to_plot, "LineWidth",2, "Color",[0.5 0.5 0.5]) - - %Getting groomed - to_plot=nan(1,session_length); to_plot(idx_getgroom)=Spike_rasters_smooth(unit,idx_getgroom); - plot(1:session_length, to_plot, "LineWidth",2, "Color",'b') - - %Grooming - to_plot=nan(1,session_length); to_plot(idx_groom)=Spike_rasters_smooth(unit,idx_groom); - plot(1:session_length, to_plot, "LineWidth",2, "Color",'c') - - %Self-groom - to_plot=nan(1,session_length); to_plot(idx_selfgroom)=Spike_rasters_smooth(unit,idx_selfgroom); - plot(1:session_length, to_plot, "LineWidth",2, "Color",[0.8 0 0.8]) - - %Foraging - to_plot=nan(1,session_length); to_plot(idx_forage)=Spike_rasters_smooth(unit,idx_forage); - plot(1:session_length, to_plot, "LineWidth",2, "Color",[0 0.7 0]) - - %Aggression - to_plot=nan(1,session_length); to_plot(idx_agg)=Spike_rasters_smooth(unit,idx_agg); - plot(1:session_length, to_plot, "LineWidth",2, "Color","r") - - %Travel - to_plot=nan(1,session_length); to_plot(idx_travel)=Spike_rasters_smooth(unit,idx_travel); - plot(1:session_length, to_plot, "LineWidth",2, "Color","y") - - %Scratch - to_plot=nan(1,session_length); to_plot(idx_scratch)=Spike_rasters_smooth(unit,idx_scratch); - plot(1:session_length, to_plot, "LineWidth",2, "Color",[0.8 0.6 0]) - - legend({'','','Smooth spike trace', 'Rest','Getting groomed','Grooming','Self-groom','Foraging','Aggression','Travel','Scratch'},... - 'Location','bestoutside') - - set(gca,'FontSize',15); - ylabel('Firing rate (Hz)'); xlabel('Time (s)'); - title(['Example Unit from ' brain_label(n)]) - xticks([1:length(unit_num_range)]); xlim([0.8 length(unit_num_range)+0.2]); ylim([0 max(mean_dim_vlpfc)+max(mean_dim_vlpfc)*0.1]) - xticklabels(unit_num_range) - ax = gca; - ax.FontSize = 14; - legend('vlPFC','TEO') - ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 18); xlabel('#units included','FontSize', 18) - title('Smoothing: 20s') - - -end%end of session loop diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_slidingWindow.m b/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_slidingWindow.m deleted file mode 100644 index 0999e4f..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_Dimensionality_slidingWindow.m +++ /dev/null @@ -1,183 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "vlPFC"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1;%set the smoothing window size (sigma) -var_explained_threshold=90; num_units = 100; -num_iter = 50; -window_size = 50; step = 5; -simplify =1; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - - - for channel_flag = ["vlPFC", "TEO"] - %channel_flag = "vlPFC"; - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey,... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, ... - with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels = cell2mat({labels{:,3}}'); - - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Threat to partner"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Threat to subject"); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - if simplify == 1 %Compare across 5 behavioral categories that are common across sessions - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - end - - - %% Compute dimensionality for 100 units, over multiple iterations,over the whole session - - timepoints = window_size/2+1:step:size(Spike_count_raster,1)-window_size/2; - - for tp=1:length(timepoints) - - dim = nan(1,num_iter); - for iter = 1:num_iter - - %Select unit to run SVM - Input_matrix = Spike_count_raster(timepoints(tp)-window_size/2:timepoints(tp)+window_size/2-1,randsample(size(Spike_count_raster,2), num_units)); - - %PCA - [coeff,score,~,~,explained] = pca(Input_matrix); - - %Get dimensionality - var_explained = cumsum(explained); - idxl = find(var_explained>=var_explained_threshold); - dim(iter) = min(idxl); - - - end % end of interation loop - - disp([num2str(tp) '/' num2str(length(timepoints))]) - mean_dim(chan, tp) = mean(dim); - std_dim(chan, tp) = std(dim); - end - - chan=chan+1; - disp([channel_flag ' done']) - end %end of channel loop - - %% Plot results for the session - - vlpfc_dim = interp(mean_dim(1,:), step); vlpfc_dim = [ones(1,size(Spike_count_raster,1)-length(vlpfc_dim))*vlpfc_dim(1), vlpfc_dim]; - teo_dim = interp(mean_dim(2,:), step); teo_dim = [ones(1,size(Spike_count_raster,1)-length(teo_dim))*teo_dim(1), teo_dim]; - corrcoef(vlpfc_dim,teo_dim) - - figure; hold on; set(gcf,'Position',[150 250 1500 300]) - - %Get behavior indices - idx_rest = find(behavior_labels == length(behav_categ)); - idx_groom = find(behavior_labels == 7); - idx_getgroom = find(behavior_labels == 8); - idx_selfgroom = find(behavior_labels == 24); - idx_forage = find(behavior_labels == 5 | behavior_labels == 4); - idx_agg = find(behavior_labels == 9 | behavior_labels == 10 |behavior_labels == 1 |behavior_labels == 21 |behavior_labels == 22); - idx_travel = find(behavior_labels == 2 | behavior_labels == 12 |behavior_labels == 18); - idx_scratch = find(behavior_labels == 23); - - xline([block_times.end_time_round(1), block_times.end_time_round(2)], "-",["Block 1 end", "Block 2 end"], "LineWidth",2); - - %Full dimensionality trace - session_length = size(Spike_count_raster,1); - trace = vlpfc_dim; - plot(trace) - - %Rest - to_plot=nan(1,session_length); to_plot(idx_rest)=trace(idx_rest); - plot(1:session_length, to_plot, "LineWidth",2, "Color",[0.5 0.5 0.5]) - - %Getting groomed - to_plot=nan(1,session_length); to_plot(idx_getgroom)=trace(idx_getgroom); - plot(1:session_length, to_plot, "LineWidth",2, "Color",'b') - - %Grooming - to_plot=nan(1,session_length); to_plot(idx_groom)=trace(idx_groom); - plot(1:session_length, to_plot, "LineWidth",2, "Color",'c') - - %Self-groom - to_plot=nan(1,session_length); to_plot(idx_selfgroom)=trace(idx_selfgroom); - plot(1:session_length, to_plot, "LineWidth",2, "Color",[0.8 0 0.8]) - - %Foraging - to_plot=nan(1,session_length); to_plot(idx_forage)=trace(idx_forage); - plot(1:session_length, to_plot, "LineWidth",2, "Color",[0 0.7 0]) - - %Aggression - to_plot=nan(1,session_length); to_plot(idx_agg)=trace(idx_agg); - plot(1:session_length, to_plot, "LineWidth",2, "Color","r") - - %Travel - to_plot=nan(1,session_length); to_plot(idx_travel)=trace(idx_travel); - plot(1:session_length, to_plot, "LineWidth",2, "Color","y") - - %Scratch - to_plot=nan(1,session_length); to_plot(idx_scratch)=trace(idx_scratch); - plot(1:session_length, to_plot, "LineWidth",2, "Color",[0.8 0.6 0]) - - legend({'','','', 'Rest','Getting groomed','Grooming','Self-groom','Foraging','Aggression','Travel','Scratch'},... - 'Location','best') - - set(gca,'FontSize',15); - ylabel(['Dimensionality (' num2str(var_explained_threshold) '% of variation)']); xlabel('Time (s)'); - title('Dimensionality through time') - ax = gca; - ax.FontSize = 14; - - -end%end of session loop diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_Distances.m b/Neural states/scrap_code_DO_NOT_DELETE/log_Distances.m deleted file mode 100644 index 6ab097e..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_Distances.m +++ /dev/null @@ -1,278 +0,0 @@ -%% log_Distances -% Compute distance between neural states between and across behavioral -% categories -% C. Testard, September 2022 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1;%set the smoothing window size (sigma) -var_explained_threshold=90; -num_iter = 500; num_units = 100; -simplify=0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -if simplify ==1 - dim = nan(max(session_range),2,5,num_iter); - min_occurrences = 50; -elseif simplify ==2 - dim = nan(max(session_range),2,2,num_iter); - min_occurrences = 230; -elseif simplify ==3 - dim = nan(max(session_range),2,3,num_iter); - min_occurrences = 50; -else - dim = nan(max(session_range),2,8,num_iter); - min_occurrences = 30; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/Dimensionality_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO"] - %channel_flag = "vlPFC"; - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - - behavior_labels = cell2mat({labels{:,3}}'); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Threat to partner"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Threat to subject"); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - if simplify == 1 %Compare across 5 behavioral categories that are common across sessions - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Lump Drinking and foraging - behavior_labels(behavior_labels==find(behav_categ=="Drinking"))=find(behav_categ=="Foraging"); - - %Lump all grooming together - behavior_labels(behavior_labels==find(behav_categ=="Getting groomed"))=find(behav_categ=="Groom partner"); - behavior_labels(behavior_labels==find(behav_categ=="Groom sollicitation"))=find(behav_categ=="Groom partner"); - behavior_labels(behavior_labels==find(behav_categ=="Self-groom"))=find(behav_categ=="Groom partner"); - - behav = [1,5,7,18,29]; - - elseif simplify == 2 % Compare one behavior across all others - - %Lump all that is not grooming together - behavior_labels(ismember(behavior_labels,find(behav_categ~="Groom partner" & behav_categ~="Getting groomed")))=find(behav_categ=="Rest"); - - behav = [find(behav_categ=="Getting groomed"),find(behav_categ=="Rest")]; - - % %Lump all that is not foraging - % behavior_labels(ismember(behavior_labels,find(behav_categ~="Foraging" )))=find(behav_categ=="Rest"); - % - % behav = [find(behav_categ=="Foraging"),find(behav_categ=="Rest")]; - - elseif simplify == 3 %Comapre grooming behaviors between each other - - behav = [find(behav_categ=="Groom partner"),find(behav_categ=="Getting groomed"),find(behav_categ=="Self-groom")]; - - else %Compare all behaviors separately (without pooling across) - %behav = [4,5,7,8,9,10,24,29]; - behav = [7,8]; - - end %End of simplifying loop - - behav_freq_table = tabulate(behavior_labels); - - %% Compute dimensionality over increasing numbers of units, over multiple iterations - - idx_all_beh= find(ismember(behavior_labels,behav)); - behav_freq=tabulate(behavior_labels(idx_all_beh)); - n_per_behav{s} = behav_freq(behav_freq(:,2)>0,2); - if all(behav_freq(behav_freq(:,2)>0,2)>min_occurrences) && length(find(behav_freq(:,2)>0))>=length(behav) - - for iter = 1:num_iter - - for b = 1:length(behav) - - disp(['Behavior: ' behav_categ(behav(b))]) - - %Select time points to run PCA - idx= find(ismember(behavior_labels,behav(b))); - idx_beh = idx(randsample(1:length(idx),min_occurrences)); - - %Select unit to run PCA - Input_matrix{b} = zscore(Spike_count_raster(idx_beh,randsample(size(Spike_count_raster,2), num_units)))'; - - end - Input_matrix_full = cell2mat(Input_matrix); - %figure; hold on; hist(corr(Input_matrix)) - - %PCA - [coeff,score,~,~,explained] = pca(Input_matrix_full'); - %[coeff,score,~,~,explained] = pca(Spike_count_raster); - - %Get dimensionality - var_explained = cumsum(explained); - idxl = find(var_explained>=var_explained_threshold); - - %Get distances - D=pdist(score(:,1:20), 'cityblock'); - Z = squareform(D); - heatmap(Z,'Colormap',jet) - - end % end of interation loop - - end % end of if clause - - chan=chan+1; - disp([channel_flag ' done']) - end %end of channel loop - - %% Plot results for the session - - Cmap = [[1 0 0];[1 0.4 0.1];[0 0 0];[0.1 0.8 0.9];[0 0.7 0];[1 0 1];[0 1 1];... - [0 0 1];[0.8 0 0];[1 0 0];[0 0 0];[0.2 0.9 0.76];[0 0 0];[0 0 0];[0.7 0 1];... - [0 0 0];[0 0 0];[0.9 0.5 0];[0 0 0];[0 0 0];[0.8 0 0];[1 0 0];[0.9 0.7 0.12];[0.5 0.2 0.5];... - [0 0 0];[0 0 0];[0.8 0.4 0.4];[0 0 0];[0.5 0.5 0.5]]; - - % figure; hold on; set(gcf,'Position',[150 250 1000 400]) - % - % subplot(1,2,1); hold on - % dim_vlpfc = squeeze(squeeze(dim(s,1,:,:)))'; - % mean_dim_vlpfc = squeeze(mean(dim(s,1,:,:),4)); [~, orderIdx] = sort(mean_dim_vlpfc); - % sd_dim_vlpfc = squeeze(std(dim(s,1,:,:),0,4)); - % violin(dim_vlpfc(:,orderIdx), 'facecolor',Cmap(behav(orderIdx),:)) - % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); - % xticklabels(behav_categ(behav(orderIdx))); ylim([0 35]) - % ax = gca; - % ax.FontSize = 14; - % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); - % title('vlPFC') - % - % subplot(1,2,2); hold on - % dim_teo = squeeze(squeeze(dim(s,2,:,:)))'; - % mean_dim_teo = squeeze(mean(dim(s,2,:,:),4)); [~, orderIdx] = sort(mean_dim_teo); - % % sd_dim_vlpfc = squeeze(std(dim(s,2,:,:),0,4)); - % violin(dim_teo(:,orderIdx), 'facecolor',Cmap(behav(orderIdx),:)) - % % scatter(1:length(behav), mean_dim_vlpfc(orderIdx),40,Cmap(behav(orderIdx),:),'filled') - % % errorbar(1:length(behav), mean_dim_vlpfc(orderIdx), sd_dim_vlpfc,'-s','MarkerSize',1, ... - % % 'MarkerEdgeColor','k') - % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); - % xticklabels(behav_categ(behav(orderIdx))); ylim([0 35]) - % ax = gca; - % ax.FontSize = 14; - % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); - % title('TEO') - % sgtitle(string(sessions(s).name)) - % - % saveas(gcf,[savePath '/DimensionalityPerBehav_2categ.pdf']) - % - % close all - -end%end of session loop - -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/Dimensionality_results/']); -save('Dimensionality_2categ.mat', "dim","behav","a_sessions","h_sessions","behav_categ","Cmap") - -%% Plot results across sessions - -load('Dimensionality_2categ.mat') - -dim_amos = squeeze(nanmean(dim(a_sessions,:,:,:),1)); -dim_hooke = squeeze(mean(dim(h_sessions,:,:,:),1)); - - -figure; hold on; set(gcf,'Position',[150 250 1000 800]); lowlimit=25; uplimit=55; - -%Amos -subplot(2,2,1); hold on -mean_dim_vlpfc = squeeze(mean(dim_amos(1,:,:),3)); [~, orderIdx] = sort(mean_dim_vlpfc); -violin(squeeze(dim_amos(1,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('Amos, vlPFC') - -subplot(2,2,2); hold on -mean_dim_teo = squeeze(mean(dim_amos(2,:,:),3)); [~, orderIdx] = sort(mean_dim_teo); -violin(squeeze(dim_amos(2,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('Amos, TEO') - -%Hooke -subplot(2,2,3); hold on -mean_dim_vlpfc = squeeze(mean(dim_hooke(1,:,:),3)); [~, orderIdx] = sort(mean_dim_vlpfc); -violin(squeeze(dim_hooke(1,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('Hooke, vlPFC') - -subplot(2,2,4); hold on -mean_dim_teo = squeeze(mean(dim_hooke(2,:,:),3)); [~, orderIdx] = sort(mean_dim_teo); -violin(squeeze(dim_hooke(2,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('Hooke, TEO') - -saveas(gcf,'DimensionalityPerBehav_2categ.pdf') \ No newline at end of file diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_Distances_ThreatEvents.m b/Neural states/scrap_code_DO_NOT_DELETE/log_Distances_ThreatEvents.m deleted file mode 100644 index 043816f..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_Distances_ThreatEvents.m +++ /dev/null @@ -1,258 +0,0 @@ -%% log_Distances_ThreatEvents -% Compute distance between threat neural states, compared to non-threat -% behaviors. -% categories -% C. Testard, September 2022 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1;%set the smoothing window size (sigma) -var_explained_threshold=90; -num_iter = 500; num_units = 100; -simplify=0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -if simplify ==1 - dim = nan(max(session_range),2,5,num_iter); - min_occurrences = 50; -elseif simplify ==2 - dim = nan(max(session_range),2,2,num_iter); - min_occurrences = 230; -elseif simplify ==3 - dim = nan(max(session_range),2,3,num_iter); - min_occurrences = 50; -else - dim = nan(max(session_range),2,8,num_iter); - min_occurrences = 30; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/Dimensionality_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO"] - %channel_flag = "vlPFC"; - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels = cell2mat({labels{:,3}}'); - - %Lump behavioral labels - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Threat to partner"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Threat to subject"); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - behav_freq_table = tabulate(behavior_labels); - - behav = [7,8,9,10,18]; - - %% Compute dimensionality over increasing numbers of units, over multiple iterations - - idx_all_beh= find(ismember(behavior_labels,behav)); - behav_freq=tabulate(behavior_labels(idx_all_beh)); - n_per_behav{s} = behav_freq(behav_freq(:,2)>0,2); - - if all(behav_freq(behav_freq(:,2)>0,2)>min_occurrences) && length(find(behav_freq(:,2)>0))>=length(behav) - - for iter = 1:num_iter - - for b = 1:length(behav) - - disp(['Behavior: ' behav_categ(behav(b))]) - - %Select time points to run PCA - idx= find(ismember(behavior_labels,behav(b))); - idx_beh = idx(randsample(1:length(idx),min_occurrences)); - - %Select unit to run PCA - Input_matrix{b} = zscore(Spike_count_raster(idx_beh,randsample(size(Spike_count_raster,2), num_units)))'; - labels_matrix{b} = behavior_labels(idx_beh); - - end - Input_matrix_full = cell2mat(Input_matrix); - Labels_matrix_full = cell2mat(labels_matrix); - %figure; hold on; hist(corr(Input_matrix)) - - [umap_result]=run_umap(Input_matrix_full', 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 3); %Run umap to get 2d embedded states - figure - scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),40,Cmap(Labels_matrix_full,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior') - set(gca,'FontSize',12); - - %PCA - [coeff,score,~,~,explained] = pca(Input_matrix_full'); - %[coeff,score,~,~,explained] = pca(Spike_count_raster); - - %Get dimensionality - var_explained = cumsum(explained); - idxl = min(find(var_explained>=var_explained_threshold)); - - figure - Cmap = [[1 0 0];[1 0.4 0.1];[0 0 0];[0.1 0.8 0.9];[0 0.7 0];[1 0 1];[0 1 1];... - [0 0 1];[0.8 0 0];[1 0 0];[0 0 0];[0.2 0.9 0.76];[0 0 0];[0 0 0];[0.7 0 1];... - [0 0 0];[0 0 0];[0.9 0.5 0];[0 0 0];[0 0 0];[0.8 0 0];[1 0 0];[0.9 0.7 0.12];[0.5 0.2 0.5];... - [0 0 0];[0 0 0];[0.8 0.4 0.4];[0 0 0];[0.5 0.5 0.5]]; - - scatter3(score(:,1), score(:,2),score(:,3),40,Cmap(Labels_matrix_full,:),'filled') - - - %Get distances - D=pdist(score(:,1:3), 'cityblock'); - Z = squareform(D); - figure; heatmap(Z,'Colormap',jet) - - end % end of interation loop - - end % end of if clause - - chan=chan+1; - disp([channel_flag ' done']) - end %end of channel loop - - %% Plot results for the session - - Cmap = [[1 0 0];[1 0.4 0.1];[0 0 0];[0.1 0.8 0.9];[0 0.7 0];[1 0 1];[0 1 1];... - [0 0 1];[0.8 0 0];[1 0 0];[0 0 0];[0.2 0.9 0.76];[0 0 0];[0 0 0];[0.7 0 1];... - [0 0 0];[0 0 0];[0.9 0.5 0];[0 0 0];[0 0 0];[0.8 0 0];[1 0 0];[0.9 0.7 0.12];[0.5 0.2 0.5];... - [0 0 0];[0 0 0];[0.8 0.4 0.4];[0 0 0];[0.5 0.5 0.5]]; - - % figure; hold on; set(gcf,'Position',[150 250 1000 400]) - % - % subplot(1,2,1); hold on - % dim_vlpfc = squeeze(squeeze(dim(s,1,:,:)))'; - % mean_dim_vlpfc = squeeze(mean(dim(s,1,:,:),4)); [~, orderIdx] = sort(mean_dim_vlpfc); - % sd_dim_vlpfc = squeeze(std(dim(s,1,:,:),0,4)); - % violin(dim_vlpfc(:,orderIdx), 'facecolor',Cmap(behav(orderIdx),:)) - % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); - % xticklabels(behav_categ(behav(orderIdx))); ylim([0 35]) - % ax = gca; - % ax.FontSize = 14; - % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); - % title('vlPFC') - % - % subplot(1,2,2); hold on - % dim_teo = squeeze(squeeze(dim(s,2,:,:)))'; - % mean_dim_teo = squeeze(mean(dim(s,2,:,:),4)); [~, orderIdx] = sort(mean_dim_teo); - % % sd_dim_vlpfc = squeeze(std(dim(s,2,:,:),0,4)); - % violin(dim_teo(:,orderIdx), 'facecolor',Cmap(behav(orderIdx),:)) - % % scatter(1:length(behav), mean_dim_vlpfc(orderIdx),40,Cmap(behav(orderIdx),:),'filled') - % % errorbar(1:length(behav), mean_dim_vlpfc(orderIdx), sd_dim_vlpfc,'-s','MarkerSize',1, ... - % % 'MarkerEdgeColor','k') - % xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); - % xticklabels(behav_categ(behav(orderIdx))); ylim([0 35]) - % ax = gca; - % ax.FontSize = 14; - % ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); - % title('TEO') - % sgtitle(string(sessions(s).name)) - % - % saveas(gcf,[savePath '/DimensionalityPerBehav_2categ.pdf']) - % - % close all - -end%end of session loop - -cd([home '/Dropbox (Penn)/Datalogger/Results/All_sessions/Dimensionality_results/']); -save('Dimensionality_2categ.mat', "dim","behav","a_sessions","h_sessions","behav_categ","Cmap") - -%% Plot results across sessions - -load('Dimensionality_2categ.mat') - -dim_amos = squeeze(nanmean(dim(a_sessions,:,:,:),1)); -dim_hooke = squeeze(mean(dim(h_sessions,:,:,:),1)); - - -figure; hold on; set(gcf,'Position',[150 250 1000 800]); lowlimit=25; uplimit=55; - -%Amos -subplot(2,2,1); hold on -mean_dim_vlpfc = squeeze(mean(dim_amos(1,:,:),3)); [~, orderIdx] = sort(mean_dim_vlpfc); -violin(squeeze(dim_amos(1,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('Amos, vlPFC') - -subplot(2,2,2); hold on -mean_dim_teo = squeeze(mean(dim_amos(2,:,:),3)); [~, orderIdx] = sort(mean_dim_teo); -violin(squeeze(dim_amos(2,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('Amos, TEO') - -%Hooke -subplot(2,2,3); hold on -mean_dim_vlpfc = squeeze(mean(dim_hooke(1,:,:),3)); [~, orderIdx] = sort(mean_dim_vlpfc); -violin(squeeze(dim_hooke(1,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('Hooke, vlPFC') - -subplot(2,2,4); hold on -mean_dim_teo = squeeze(mean(dim_hooke(2,:,:),3)); [~, orderIdx] = sort(mean_dim_teo); -violin(squeeze(dim_hooke(2,orderIdx,:))', 'facecolor',Cmap(behav(orderIdx),:)) -xticks([1:length(behav)]); xlim([0.5 length(behav)+0.5]); -xticklabels(behav_categ(behav(orderIdx))); ylim([lowlimit uplimit]) -ax = gca; -ax.FontSize = 14; -ylabel(['Dims needed to explain ' num2str(var_explained_threshold) '% of variation'],'FontSize', 14); -title('Hooke, TEO') - -saveas(gcf,'DimensionalityPerBehav_2categ.pdf') \ No newline at end of file diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_PCA_visualization_batch.m b/Neural states/scrap_code_DO_NOT_DELETE/log_PCA_visualization_batch.m deleted file mode 100644 index 051ebf3..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_PCA_visualization_batch.m +++ /dev/null @@ -1,176 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1; %lump similar behavioral categories together -agg_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; - -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO"] - %channel_flag = "TEO"; - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - - - %% Select behaviors to decode - - %Extract behavior labels and frequency - behavior_labels = cell2mat({labels{:,3}}'); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Extract block labels - block_labels = cell2mat({labels{:,12}}'); - - % Select behaviors - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - %Remove behaviors we're not interested in for now - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding Rowdy Room which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize'))); - - % OR select behaviors manually - %behav =[29] ;%unique(behavior_labels); %[4,5,7,8,9,10,24];% [4:10, 23]; %[4:8,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Subset data to only consider epochs of interest - idx= find(ismember(behavior_labels,behav)); - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking dat - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_labels_final = block_labels(idx); - behavior_labels_final_rand = randsample(behavior_labels_final, length(behavior_labels_final)); - - %run PCA - [coeff,score,latent,tsquared,explained] = pca(zscore(Spike_count_raster_final)); - var_explained = cumsum(explained); idxl = find(var_explained>=80); dim = min(idxl); - pca_plot_final = score(:,1:15); - - - %% Plot PCA - - %Set colormap - Cmap = [[1 0 0];[1 0.4 0.1];[0 0 0];[0 0.4 0.5];[0 0.7 0];[1 0 1];[0 1 1];... - [0 0 1];[0.8 0 0];[1 0 0];[0 0 0];[0.2 0.9 0.76];[0 0 0];[0 0 0];[0 0 0];... - [0 0 0];[0 0 0];[0.9 0.5 0];[0 0 0];[0 0 0];[0 0 0];[0 0 0];[0.9 0.7 0.12];[0.5 0.2 0.5];... - [0 0 0];[0 0 0];[0.8 0.4 0.4];[0 0 0];[0.5 0.5 0.5]]; - - Cmap_block = [[0.9 0.7 0.12];[0 0.6 0.8];[0.5 0 0]]; - - Cmap_time = copper(length(idx)); - - - figure; hold on; set(gcf,'Position',[150 250 1500 500]) - - %Plot PCA results color-coded by behavior - ax1=subplot(1,2,1); - scatter3(pca_plot_final(:,1), pca_plot_final(:,2),pca_plot_final(:,3),12,Cmap(behavior_labels_final,:),'filled') - sum(explained(1:3)) - xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior') - set(gca,'FontSize',12); - %saveas(gcf,[savePath '/umap_supervised_ColorCodedByBehav_' channel 'Units.png']) - %pause(5) - -% %Plot UMAP results color-coded by time -% ax2=subplot(1,3,2); -% scatter3(pca_plot_final(:,1), pca_plot_final(:,2),pca_plot_final(:,3),8,Cmap_time,'filled') -% xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') -% %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -% title('Time') -% set(gca,'FontSize',12); - - %Color-coded by block - ax2=subplot(1,2,2); - scatter3(pca_plot_final(:,1), pca_plot_final(:,2),pca_plot_final(:,3),12,Cmap_block(block_labels_final,:),'filled') - xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Block') - set(gca,'FontSize',12); - %saveas(gcf,[savePath '/umap_ColorCodedByBlock_' channel 'Units.png']) - - sgtitle([channel_flag ' units, PCA']) - - hlink = linkprop([ax1,ax2],{'CameraPosition','CameraUpVector'}); - rotate3d on - - %clearvars -except temp chan savePath filePath temp_resolution is_mac - chan=chan+1; - - end %end of channel for loop - -end %end of session for loop - - diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_blockDistances_batch.m b/Neural states/scrap_code_DO_NOT_DELETE/log_blockDistances_batch.m deleted file mode 100644 index b7b3d31..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_blockDistances_batch.m +++ /dev/null @@ -1,261 +0,0 @@ -%% log_blockDistanc_batch -% This script applied unsupervised umap on smoothed firing -% Ref: Connor Meehan, Jonathan Ebrahimian, Wayne Moore, and Stephen Meehan (2022). Uniform Manifold Approximation and Projection (UMAP) (https://www.mathworks.com/matlabcentral/fileexchange/71902), MATLAB Central File Exchange. -% Jan 2023, C. Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1; %lump similar behavioral categories together -agg_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=2; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - - chan=1; - for channel_flag = ["vlPFC", "TEO", "all"] - %channel_flag = "TEO"; - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - - - - %% Select behaviors to visualize - - %Extract behavior labels and frequency - behavior_labels = cell2mat({labels{:,3}}'); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Extract block labels - block_labels = cell2mat({labels{:,12}}'); - alone_label = cell2mat({labels{:,13}}'); - - % Select behaviors - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - %Remove behaviors we're not interested in for now - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding Rowdy Room which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize'))); - - % OR select behaviors manually - behav =[5,7,8,9,10,16] ;%unique(behavior_labels); %[4,5,7,8,9,10,24];% [4:10, 23]; %[4:8,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only consider indices with behavior of interest - idx= find(ismember(behavior_labels,behav)); - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_labels_final = block_labels(idx); - alone_labels_final = alone_label(idx); - - - %% Run umap - - %Supervised - % data = [Spike_count_raster_final, behavior_labels_final]; - % [umap_result{s,chan}]=run_umap(data, 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 3,'label_column', 'end'); %Run umap to get 2d embedded states - - %Unsupervised - [umap_result]=run_umap(Spike_count_raster_final, 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 3); %Run umap to get 2d embedded states - close - - %% Run PCA - [~, pca_results] = pca(zscore(Spike_count_raster_final)); - - - - %% Plot UMAP projection in 3D space - - %Set colormap - Cmap = [[1 0 0];[1 0.4 0.1];[0 0 0];[0.1 0.8 0.9];[0 0.7 0];[1 0 1];[0 1 1];... - [0 0 1];[0.8 0 0];[1 0 0];[0 0 0];[0.2 0.9 0.76];[0 0 0];[0 0 0];[0.7 0 1];... - [0 0 0];[0 0 0];[0.9 0.5 0];[0 0 0];[0 0 0];[0.8 0 0];[1 0 0];[0.9 0.7 0.12];[0.5 0.2 0.5];... - [0 0 0];[0 0 0];[0.8 0.4 0.4];[0 0 0];[0.5 0.5 0.5]]; - - Cmap_block = [[0.9 0.7 0.12];[0 0.6 0.8];[0.5 0 0]]; - - figure; hold on; set(gcf,'Position',[150 250 1200 500]) - - %Plot UMAP results color-coded by behavior - ax1=subplot(1,2,1); - scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),12,Cmap(behavior_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior') - set(gca,'FontSize',12); - %saveas(gcf,[savePath '/umap_supervised_ColorCodedByBehav_' channel 'Units.png']) - %pause(5) - - %Color-coded by block - ax2=subplot(1,2,2); - scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),12,Cmap_block(block_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Block') - set(gca,'FontSize',12); - - %sgtitle([channel ' units, UMAP, ' sessions(s).name]) - - hlink = linkprop([ax1,ax2],{'CameraPosition','CameraUpVector'}); - rotate3d on - - - - - %% Visualize distances between social context given a particular behavior - - boi =[5,7,8,9,10,16]; - paired_presence = crosstab(behavior_labels_final, block_labels_final); paired_presence=paired_presence(:,1:2); - paired_alone_presence = crosstab(behavior_labels_final, alone_labels_final); - distances{s,chan}=nan(length(behav),3); - - for beh= 1:length(boi) %for each behavior - - distances{s,chan}(beh,1)=boi(beh); - - if all(paired_alone_presence(beh,:)>15) %if the behavior occurs both when monkey is paired and alone - %Measure the distance between the centres of mass neural state for this behavior in paired and alone blocks - - paired_activity_beh_com = mean(umap_result(behavior_labels_final==boi(beh)... - & alone_labels_final==1,:)); - - alone_activity_beh_com = mean(umap_result(behavior_labels_final==boi(beh)... - & alone_labels_final==0,:)); - - distances{s,chan}(beh,2)=pdist([paired_activity_beh_com;alone_activity_beh_com], 'cityblock'); - - end - - if all(paired_presence(beh,:)>15) %if the behavior occurs both when monkey is paired and alone - %Measure the distance between the centres of mass neural state for this behavior in paired and alone blocks - - paired_activity_beh_com = mean(umap_result(behavior_labels_final==boi(beh)... - & block_labels_final==1,:)); - - alone_activity_beh_com = mean(umap_result(behavior_labels_final==boi(beh)... - & block_labels_final==2,:)); - - distances{s,chan}(beh,3)=pdist([paired_activity_beh_com;alone_activity_beh_com], 'cityblock'); - - end - - end - - chan=chan+1; - - end %end of channel loop - - disp("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") - disp(s) - disp("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") - -end %end of session for loop - -%Save data -cd(['~/Dropbox (Penn)/Datalogger/Results/All_sessions/UMAP_results/']) -%save("blockDist_behav.mat",'distances','behav_categ','boi') -load("blockDist_behav.mat") - -i=2; %set channel -zscored_distances = distances; -for s=a_sessions - x=distances{s,i}(:,2:3); - zscored_distances{s,i}(:,2:3)= (x - nanmean(reshape(x,1,[])))/nanstd(reshape(x,1,[])); -end - -rescaled_distances = distances; -for s=a_sessions - x=distances{s,i}(:,2:3); - rescaled_distances{s,i}(:,2:3)= rescale(x,0,1); -end - -distances_allSession=cat(3,rescaled_distances{a_sessions,i}); -distances_allSession_mean = nanmean(distances_allSession,3); -figure; hold on; bar(distances_allSession_mean(:,2:3)) -scatter(0.85*ones(1,12),squeeze(distances_allSession(1,2,:)),'b','filled') -scatter(1.15*ones(1,12),squeeze(distances_allSession(1,3,:)),'r','filled') -scatter(1.85*ones(1,12),squeeze(distances_allSession(2,2,:)),'b','filled') -scatter(2.15*ones(1,12),squeeze(distances_allSession(2,3,:)),'r','filled') -scatter(2.85*ones(1,12),squeeze(distances_allSession(3,2,:)),'b','filled') -scatter(3.15*ones(1,12),squeeze(distances_allSession(3,3,:)),'r','filled') -scatter(3.85*ones(1,12),squeeze(distances_allSession(4,2,:)),'b','filled') -scatter(4.15*ones(1,12),squeeze(distances_allSession(4,3,:)),'r','filled') -scatter(4.85*ones(1,12),squeeze(distances_allSession(5,2,:)),'b','filled') -scatter(5.15*ones(1,12),squeeze(distances_allSession(5,3,:)),'r','filled') -scatter(5.85*ones(1,12),squeeze(distances_allSession(6,2,:)),'b','filled') -scatter(6.15*ones(1,12),squeeze(distances_allSession(6,3,:)),'r','filled') -xticks([1:6]) -xticklabels(behav_categ(boi)) -ylabel('Scaled eucledian distance in umap') -legend({'Alone vs. Paired', 'Female vs. Male neighbor'}) -set(gca,'fontsize', 16) - -%Need to show raw datapoints. diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_cmdscale_visualization_batch.m b/Neural states/scrap_code_DO_NOT_DELETE/log_cmdscale_visualization_batch.m deleted file mode 100644 index b8e1e01..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_cmdscale_visualization_batch.m +++ /dev/null @@ -1,167 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 0; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1; %lump similar behavioral categories together -agg_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; - -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO"] - %channel_flag = "TEO"; - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - - - %% Select behaviors to decode - - %Extract behavior labels and frequency - behavior_labels = cell2mat({labels{:,3}}'); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Extract block labels - block_labels = cell2mat({labels{:,12}}'); - - % Select behaviors - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - %Remove behaviors we're not interested in for now - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding Rowdy Room which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize'))); - - % OR select behaviors manually - %behav =[4,5,6,9,10] ;%unique(behavior_labels); %[4,5,7,8,9,10,24];% [4:10, 23]; %[4:8,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Subset data to only consider epochs of interest - idx= find(ismember(behavior_labels,behav)); - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking dat - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_labels_final = block_labels(idx); - behavior_labels_final_rand = randsample(behavior_labels_final, length(behavior_labels_final)); - - %run cmdscale - D = squareform(pdist(zscore(Spike_count_raster_final))); - cmdscale_plot_final = cmdscale(D); - - - %% Plot PCA - - %Set colormap - Cmap = [[1 0 0];[1 0.4 0.1];[0 0 0];[0 0.4 0.5];[0 0.7 0];[1 0 1];[0 1 1];... - [0 0 1];[0.8 0 0];[1 0 0];[0 0 0];[0.2 0.9 0.76];[0 0 0];[0 0 0];[0 0 0];... - [0 0 0];[0 0 0];[0.9 0.5 0];[0 0 0];[0 0 0];[0 0 0];[0 0 0];[0.9 0.7 0.12];[0.5 0.2 0.5];... - [0 0 0];[0 0 0];[0.8 0.4 0.4];[0 0 0];[0.5 0.5 0.5]]; - - Cmap_block = [[0.9 0.7 0.12];[0 0.6 0.8];[0.5 0 0]]; - - Cmap_time = copper(length(idx)); - - - figure; hold on; set(gcf,'Position',[150 250 1500 500]) - - %Plot PCA results color-coded by behavior - ax1=subplot(1,2,1); - scatter3(cmdscale_plot_final(:,1), cmdscale_plot_final(:,2),cmdscale_plot_final(:,3),14,Cmap(behavior_labels_final,:),'filled') - xlabel('mdscale 1'); ylabel('mdscale 2'); zlabel('mdscale 3'); - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior') - set(gca,'FontSize',12); - %saveas(gcf,[savePath '/umap_supervised_ColorCodedByBehav_' channel 'Units.png']) - %pause(5) - - - %Color-coded by block - ax2=subplot(1,2,2); - scatter3(cmdscale_plot_final(:,1), cmdscale_plot_final(:,2),cmdscale_plot_final(:,3),14,Cmap_block(block_labels_final,:),'filled') - xlabel('mdscale 1'); ylabel('mdscale 2'); zlabel('mdscale 3'); - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Block') - set(gca,'FontSize',12); - %saveas(gcf,[savePath '/umap_ColorCodedByBlock_' channel 'Units.png']) - - sgtitle(['Multi-dim scaling']) - - hlink = linkprop([ax1,ax2],{'CameraPosition','CameraUpVector'}); - rotate3d on - - %clearvars -except temp chan savePath filePath temp_resolution is_mac - chan=chan+1; - - end %end of channel for loop - -end %end of session for loop - - diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_neural_cosyneSim_threat.m b/Neural states/scrap_code_DO_NOT_DELETE/log_neural_cosyneSim_threat.m deleted file mode 100644 index e3424a7..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_neural_cosyneSim_threat.m +++ /dev/null @@ -1,191 +0,0 @@ -%% log_neural_cosyneSim_threat -% This script extracts and compares the mean firing rate across different threat -% instances (self vs. other; alone vs. paired). - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1; %lump similar behavioral categories together -agg_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence -time_before_threat = 30*temp_resolution; -time_after_threat = 60*temp_resolution; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; chan=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - %Extract behavior labels - behavior_labels = cell2mat({labels{:,3}}'); - %Extract block labels - block_labels = cell2mat({labels{:,13}}'); - - %Make proximity equal to rest - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); - - %% Get center of mass of rest epochs. - - idx_rest=find(behavior_labels==29); block_rest=block_labels(behavior_labels==29); - idx_rest_paired=idx_rest(block_rest==1); idx_rest_alone=idx_rest(block_rest==0); - - %get equal representation of rest during paired and alone blocks. - idx_equalBlocks=[randsample(idx_rest_paired,min(length(idx_rest_paired),length(idx_rest_alone))); randsample(idx_rest_alone,min(length(idx_rest_paired),length(idx_rest_alone)))]; - - rest_com = mean(Spike_count_raster(idx_rest,:)); - - - %% Find threat to subject onset times - - threat_to_subject_onset = behavior_log.start_time_round(find(strcmp(behavior_log.Behavior, "HIS"))); - - e=1; e2=1; - for event = 1:length(threat_to_subject_onset) - - idx = threat_to_subject_onset(event)-time_before_threat : threat_to_subject_onset(event)+time_after_threat; - - Spike_count_raster_threat = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_threat = behavior_labels(idx);%Same as above but in behavior labels - block_labels_threat = block_labels(idx); - - for i=1:length(idx) - cosyneSim{s}(event,i)=getCosineSimilarity(rest_com, Spike_count_raster_threat(i,:)); - rval=corrcoef(rest_com, Spike_count_raster_threat(i,:)); - corrSim{s}(event,i)=rval(1,2); - end - - %figure; hold on; plot(cosyneSim(event,:)); plot(corrSim(event,:)) - - mean_response{s}(event,:)=mean(Spike_count_raster_threat,2); - std_response{s}(event,:)=std(Spike_count_raster_threat,[],2); - block{s}(event)=unique(block_labels_threat); - - end - - figure; hold on; set(gcf,'Position',[150 250 700 300]); - plot(corrSim{s}(block{s}==1,:)','Color',[0.9 0.7 0.12],'LineWidth',2) - plot(corrSim{s}(block{s}==0,:)','Color',[0.5 0 0],'LineWidth',2) - xline(time_before_threat+2,'LineStyle','--') - ylabel('Correlation') - xlabel('Time') - legend('Threat when paired','','Threat when alone','','Threat onset') - set(gca,'FontSize',16); - title(['session: ' sessions(s).name]) -% -% cd(savePath) -% saveas(gcf,'Distance threat paired vs. alone.pdf') -% % pause(1); close all - - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - disp(s) - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') -end %end of session for loop - -paired=cell2mat(distance_to_baseline_umap_paired'); -alone=cell2mat(distance_to_baseline_umap_alone'); - -gap=mean(nanmean(paired(:,2:30)))-mean(nanmean(alone(:,2:30))); -alone=alone+gap; - -figure; hold on - -upper_lim=nanmean(paired)+(nanstd(paired)/sqrt(size(paired,1))); -lower_lim=nanmean(paired)-(nanstd(paired)/sqrt(size(paired,1))); -p = fill([1:length(nanmean(paired)) length(nanmean(paired)):-1:1],[upper_lim flip(lower_lim)],'red'); -p.FaceColor = [0.9 0.7 0.12]; -p.FaceAlpha = 0.3; -p.EdgeColor = 'none'; -plot(nanmean(paired),'Color',[0.9 0.7 0.12],'LineWidth',6) - - -upper_lim=nanmean(alone)+(nanstd(alone)/sqrt(size(alone,1))); -lower_lim=nanmean(alone)-(nanstd(alone)/sqrt(size(alone,1))); -p = fill([1:length(nanmean(alone)) length(nanmean(alone)):-1:1],[upper_lim flip(lower_lim)],'red'); -p.FaceColor = [0.5 0 0]; -p.FaceAlpha = 0.3; -p.EdgeColor = 'none'; -plot(nanmean(alone),'Color',[0.5 0 0],'LineWidth',6) - -xline(time_before_threat+2,'LineStyle','--') -xline(time_before_threat+30+2,'LineStyle','--') -ylabel('Distance to baseline state') -xlabel('Time') -%legend('Threat when paired','Threat when alone','Threat onset') -set(gca,'FontSize',16); - -[h,p]=ttest2(alone(:,41), paired(:,41)) - -% % % %PCA -% % % figure; hold on -% % % paired=nan(max(session_range),length(idx)+1); -% % % alone=nan(max(session_range),length(idx)+1); -% % % -% % % for s=session_range -% % % paired(s,:) = mean(distance_to_baseline_pca{s}(:,block{s}~=0),2); -% % % alone(s,:) = mean(distance_to_baseline_pca{s}(:,block{s}==0),2); -% % % -% % % plot(paired(s,:),'Color',[0.9 0.7 0.12],'LineWidth',2) -% % % plot(alone(s,:),'Color',[0.5 0 0],'LineWidth',2) -% % % -% % % pause(1) -% % % -% % % end -% % % -% % % plot(nanmean(paired),'Color',[0.9 0.7 0.12],'LineWidth',6) -% % % plot(nanmean(alone),'Color',[0.5 0 0],'LineWidth',6) -% % % -% % % xline(time_before_threat+1,'LineStyle','--') -% % % ylabel('Distance to baseline state') -% % % xlabel('Time') -% % % %legend('Threat when paired','Threat when alone','Threat onset') -% % % set(gca,'FontSize',16); -% % % -% % % diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_neural_response_to_threat_oldCode.m b/Neural states/scrap_code_DO_NOT_DELETE/log_neural_response_to_threat_oldCode.m deleted file mode 100644 index 2bc27a6..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_neural_response_to_threat_oldCode.m +++ /dev/null @@ -1,180 +0,0 @@ -%% log_neural_response_to_threat -% This script extracts and compares the mean firing rate across different threat -% instances (self vs. other; alone vs. paired). - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1; %lump similar behavioral categories together -threat_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence -time_before_threat = 30*temp_resolution; -time_after_threat = 60*temp_resolution; -include_sq=0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; chan=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, include_sq); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = zscore(Spike_rasters'); - %Extract behavior labels - behavior_labels = cell2mat({labels{:,3}}'); - %Extract block labels - block_labels = cell2mat({labels{:,13}}'); - - - %% Find threat to subject onset times - - threat_to_subject_onset = behavior_log.start_time_round(find(strcmp(behavior_log.Behavior, "HIS"))); - threat_to_partner_onset = behavior_log.start_time_round(find(strcmp(behavior_log.Behavior, "HIP"))); - - %For threat to subject - for event = 1:length(threat_to_subject_onset) - - idx = threat_to_subject_onset(event)-time_before_threat : threat_to_subject_onset(event)+time_after_threat; - - Spike_count_raster_subject{event} = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - block(event) = unique(block_labels(idx)); - - end - mean_response_subject = mean(cat(3,Spike_count_raster_subject{:}),3); - figure; plot(mean_response_subject(:,1)) - - %For threat to partner - for event = 1:length(threat_to_partner_onset) - - idx = threat_to_partner_onset(event)-time_before_threat : threat_to_partner_onset(event)+time_after_threat; - - Spike_count_raster_partner{event} = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - block(event) = unique(block_labels(idx)); - - end - - -% figure; hold on; set(gcf,'Position',[150 250 700 300]); -% plot(mean_response{s}(block==1,:)','Color',[0.9 0.7 0.12],'LineWidth',2) -% plot(mean_response{s}(block==0,:)','Color',[0.5 0 0],'LineWidth',2) -% xline(time_before_threat+2,'LineStyle','--') -% ylabel('Mean firing rate (Hz)') -% xlabel('Time') -% legend('Threat when paired','','Threat when alone','','Threat onset') -% set(gca,'FontSize',16); -% title(['session: ' sessions(s).name]) -% -% cd(savePath) -% saveas(gcf,'Distance threat paired vs. alone.pdf') -% % pause(1); close all - - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - disp(s) - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') -end %end of session for loop - -paired=cell2mat(distance_to_baseline_umap_paired'); -alone=cell2mat(distance_to_baseline_umap_alone'); - -gap=mean(nanmean(paired(:,2:30)))-mean(nanmean(alone(:,2:30))); -alone=alone+gap; - -figure; hold on - -upper_lim=nanmean(paired)+(nanstd(paired)/sqrt(size(paired,1))); -lower_lim=nanmean(paired)-(nanstd(paired)/sqrt(size(paired,1))); -p = fill([1:length(nanmean(paired)) length(nanmean(paired)):-1:1],[upper_lim flip(lower_lim)],'red'); -p.FaceColor = [0.9 0.7 0.12]; -p.FaceAlpha = 0.3; -p.EdgeColor = 'none'; -plot(nanmean(paired),'Color',[0.9 0.7 0.12],'LineWidth',6) - - -upper_lim=nanmean(alone)+(nanstd(alone)/sqrt(size(alone,1))); -lower_lim=nanmean(alone)-(nanstd(alone)/sqrt(size(alone,1))); -p = fill([1:length(nanmean(alone)) length(nanmean(alone)):-1:1],[upper_lim flip(lower_lim)],'red'); -p.FaceColor = [0.5 0 0]; -p.FaceAlpha = 0.3; -p.EdgeColor = 'none'; -plot(nanmean(alone),'Color',[0.5 0 0],'LineWidth',6) - -xline(time_before_threat+2,'LineStyle','--') -xline(time_before_threat+30+2,'LineStyle','--') -ylabel('Distance to baseline state') -xlabel('Time') -%legend('Threat when paired','Threat when alone','Threat onset') -set(gca,'FontSize',16); - -[h,p]=ttest2(alone(:,41), paired(:,41)) - -% % % %PCA -% % % figure; hold on -% % % paired=nan(max(session_range),length(idx)+1); -% % % alone=nan(max(session_range),length(idx)+1); -% % % -% % % for s=session_range -% % % paired(s,:) = mean(distance_to_baseline_pca{s}(:,block{s}~=0),2); -% % % alone(s,:) = mean(distance_to_baseline_pca{s}(:,block{s}==0),2); -% % % -% % % plot(paired(s,:),'Color',[0.9 0.7 0.12],'LineWidth',2) -% % % plot(alone(s,:),'Color',[0.5 0 0],'LineWidth',2) -% % % -% % % pause(1) -% % % -% % % end -% % % -% % % plot(nanmean(paired),'Color',[0.9 0.7 0.12],'LineWidth',6) -% % % plot(nanmean(alone),'Color',[0.5 0 0],'LineWidth',6) -% % % -% % % xline(time_before_threat+1,'LineStyle','--') -% % % ylabel('Distance to baseline state') -% % % xlabel('Time') -% % % %legend('Threat when paired','Threat when alone','Threat onset') -% % % set(gca,'FontSize',16); -% % % -% % % diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_neural_state_distances_batch.m b/Neural states/scrap_code_DO_NOT_DELETE/log_neural_state_distances_batch.m deleted file mode 100644 index 13ad919..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_neural_state_distances_batch.m +++ /dev/null @@ -1,459 +0,0 @@ -%% log_neural_state_distances_batch -% This script applied unsupervised umap on smoothed firing -% Ref: Connor Meehan, Jonathan Ebrahimian, Wayne Moore, and Stephen Meehan (2022). Uniform Manifold Approximation and Projection (UMAP) (https://www.mathworks.com/matlabcentral/fileexchange/71902), MATLAB Central File Exchange. -% Sept 2022, C. Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:6,11:13,15:16]; - -%Set parameters -with_partner =0; -temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1; %lump similar behavioral categories together -agg_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; chan=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - -% for channel_flag = ["vlPFC", "TEO", "all"] - %channel_flag = "TEO"; - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - - %Low-pass filter - %Spike_count_raster = lowpass(Spike_rasters',0.005,1); - %PCA - % [coeff,score,latent,tsquared,explained] = pca(Spike_rasters'); - % Spike_count_raster = score(:,1:15); - - - %% Select behaviors to visualize - - %Extract behavior labels and frequency - behavior_labels = cell2mat({labels{:,3}}'); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Extract block labels - block_labels = cell2mat({labels{:,12}}'); - - % Select behaviors - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - %Remove behaviors we're not interested in for now - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding Rowdy Room which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize'))); - - % OR select behaviors manually - behav=[behav;29]; - %behav =[7] ;%unique(behavior_labels); %[4,5,7,8,9,10,24];% [4:10, 23]; %[4:8,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only consider indices with behavior of interest - idx= find(ismember(behavior_labels,behav)); - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_labels_final = block_labels(idx); - behavior_labels_final_rand = randsample(behavior_labels_final, length(behavior_labels_final)); - - if null - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels_final,behav_categ, temp_resolution); - behavior_labels_final = sim_behav; - end - - %% Run umap - - %Supervised - % data = [Spike_count_raster_final, behavior_labels_final]; - % [umap_result{s,chan}]=run_umap(data, 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 3,'label_column', 'end'); %Run umap to get 2d embedded states - - %Unsupervised - [umap_result]=run_umap(Spike_count_raster_final, 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 50); %Run umap to get 2d embedded states - close - - %% Run PCA - [~, pca_results] = pca(zscore(Spike_count_raster_final)); - - - - %% Plot UMAP projection in 3D space - - %Set colormap - Cmap = [[1 0 0];[1 0.4 0.1];[0 0 0];[0.1 0.8 0.9];[0 0.7 0];[1 0 1];[0 1 1];... - [0 0 1];[0.8 0 0];[1 0 0];[0 0 0];[0.2 0.9 0.76];[0 0 0];[0 0 0];[0.7 0 1];... - [0 0 0];[0 0 0];[0.9 0.5 0];[0 0 0];[0 0 0];[0.8 0 0];[1 0 0];[0.9 0.7 0.12];[0.5 0.2 0.5];... - [0 0 0];[0 0 0];[0.8 0.4 0.4];[0 0 0];[0.5 0.5 0.5]]; - - Cmap_block = [[0.9 0.7 0.12];[0 0.6 0.8];[0.5 0 0]]; - - % % % figure; hold on; set(gcf,'Position',[150 250 1200 500]) - % % % - % % % %Plot UMAP results color-coded by behavior - % % % ax1=subplot(1,2,1); - % % % scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),12,Cmap(behavior_labels_final,:),'filled') - % % % xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - % % % %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - % % % title('Behavior') - % % % set(gca,'FontSize',12); - % % % %saveas(gcf,[savePath '/umap_supervised_ColorCodedByBehav_' channel 'Units.png']) - % % % %pause(5) - % % % - % % % %Color-coded by block - % % % ax2=subplot(1,2,2); - % % % scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),12,Cmap_block(block_labels_final,:),'filled') - % % % xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - % % % %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - % % % title('Block') - % % % set(gca,'FontSize',12); - % % % - % % % sgtitle([channel ' units, UMAP, ' sessions(s).name]) - % % % - % % % hlink = linkprop([ax1,ax2],{'CameraPosition','CameraUpVector'}); - % % % rotate3d on - - - %% Visualize distances between behavior states - %Within a block - - plot_behav_labels = behavior_labels_final(block_labels_final==1); - plot_neural_states = Spike_count_raster_final(block_labels_final==1,1:50); - plot_neural_states_umap = umap_result(block_labels_final==1,:); - plot_neural_states_pca = pca_results(block_labels_final==1,1:50); - - idx_groom = find(ismember(plot_behav_labels,7)); - idx_travel = find(ismember(plot_behav_labels,18)); - idx_getgroom = find(ismember(plot_behav_labels,8)); - %idx_selfgroom = find(ismember(plot_behav_labels,24)); - idx_forage = find(ismember(plot_behav_labels,5)); - idx_hip = find(ismember(plot_behav_labels,9)); - idx_his = find(ismember(plot_behav_labels,10)); - idx_rest = find(ismember(plot_behav_labels,29)); - idx_length = 15; - - for iter=1:500 - idx_ordered = [idx_rest(randsample(length(idx_rest),idx_length));... - idx_forage(randsample(length(idx_forage),idx_length));... - %idx_selfgroom(randsample(length(idx_selfgroom),idx_length));... - idx_groom(randsample(length(idx_groom),idx_length));... - idx_getgroom(randsample(length(idx_getgroom),idx_length));... - idx_travel(randsample(length(idx_travel),idx_length));... - idx_hip(randsample(length(idx_hip),idx_length));... - idx_his(randsample(length(idx_his),idx_length))]; - D_subsample_umap(iter,:)=pdist(plot_neural_states_umap(idx_ordered,:)); - D_subsample_pca(iter,:)=pdist(plot_neural_states_pca(idx_ordered,:)); - D_subsample(iter,:)=pdist(plot_neural_states(idx_ordered,:)); - end - %corrcoef(D_subsample_pca'); correlation across iterations is much - %lower for pca than umap. Umap is more repeatable than pca. - - D = D_subsample(randsample(500,1),:); clear D_subsample - D_umap = mean(D_subsample_umap); clear D_subsample_umap - D_pca = mean(D_subsample_pca); clear D_subsample_pca - - Z_behav{s,chan} = squareform(D); Z_behav{s,chan}(eye(size(Z_behav{s,chan}))==1) = nan; - Z_behav_umap{s,chan} = squareform(D_umap); Z_behav_umap{s,chan}(eye(size(Z_behav_umap{s,chan}))==1) = nan; - Z_behav_pca{s,chan} = squareform(D_pca); Z_behav_pca{s,chan}(eye(size(Z_behav_pca{s,chan}))==1) = nan; - -% % figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_behav{s,chan},'Colormap',flipud(hot)) -% % figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_behav_umap{s,chan},'Colormap',flipud(hot)) -% % figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_behav_pca{s,chan},'Colormap',flipud(hot)) - %saveas(gcf,[savePath '/DistanceBetweenStates_behavior.pdf']) - - Z_behav_umap_lowRes{s, chan} = sepblockfun(Z_behav_umap{s, chan},[idx_length,idx_length],@nanmean); - Z_behav_pca_lowRes{s, chan} = sepblockfun(Z_behav_pca{s, chan},[idx_length,idx_length],@nanmean); - -% % figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_behav_umap_lowRes{s,chan},'Colormap',flipud(jet)) -% % figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_behav_pca_lowRes{s,chan},'Colormap',flipud(jet)) - - - %% Visualize distances paired vs. alone - - plot_neural_states_umap = umap_result; - plot_neural_states_pca = pca_results; - - idx_block1= find(block_labels_final==1); - idx_block2= find(block_labels_final==2); - idx_block3= find(block_labels_final==3); - - idx_length = 50; - - for iter=1:500 - idx_ordered = [idx_block1(randsample(length(idx_block1),idx_length));... - idx_block2(randsample(length(idx_block2),idx_length));... - idx_block3(randsample(length(idx_block3),idx_length))]; - D_subsample_umap_block(iter,:)=pdist(plot_neural_states_umap(idx_ordered,:)); - D_subsample_pca_block(iter,:)=pdist(plot_neural_states_pca(idx_ordered,:)); - end - - D_umap = mean(D_subsample_umap_block); clear D_subsample_umap_block - D_pca = mean(D_subsample_pca_block); clear D_subsample_pca_block - - Z_umap_block{s, chan} = squareform(D_umap); Z_umap_block{s, chan}(eye(size(Z_umap_block{s, chan}))==1) = nan; - Z_pca_block{s, chan} = squareform(D_pca); Z_pca_block{s, chan}(eye(size(Z_pca_block{s, chan}))==1) = nan; - -% % figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_umap_block{s, chan},'Colormap',flipud(cool)) -% % figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_pca_block{s, chan},'Colormap',flipud(cool)) - %saveas(gcf,[savePath '/DistanceBetweenStates_PairedAloneContext.pdf']) - - Z_umap_block_lowRes{s, chan} = sepblockfun(Z_umap_block{s, chan},[idx_length,idx_length],@nanmean); - Z_pca_block_lowRes{s, chan} = sepblockfun(Z_pca_block{s, chan},[idx_length,idx_length],@nanmean); - -% % figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_umap_block_lowRes{s},'Colormap',flipud(jet)) -% % figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_pca_block_lowRes{s},'Colormap',flipud(jet)) - - - %% Visualize distances between social context given a particular behavior - beh=1; - - if ismember(beh,1) - %Pull threats to partner and subject together - behavior_labels_final(behavior_labels_final==9)=1; - behavior_labels_final(behavior_labels_final==10)=1; - end - - plot_behav_labels = behavior_labels_final(behavior_labels_final==beh); - plot_block_labels = block_labels_final(behavior_labels_final==beh); - plot_neural_states_umap = umap_result(behavior_labels_final==beh,:); - plot_neural_states_pca = pca_results(behavior_labels_final==beh,:); - - idx_block1= find(plot_block_labels==1); - idx_block2= find(plot_block_labels==2); - idx_block3= find(plot_block_labels==3); - - for iter=1:500 - if ismember(beh, [7,8]) - idx_length = 100; - idx_ordered = [idx_block1(randsample(length(idx_block1),idx_length));... - idx_block2(randsample(length(idx_block2),idx_length))]; - else - idx_length = min([length(idx_block1), length(idx_block2), length(idx_block3)]); - idx_ordered = [idx_block1(randsample(length(idx_block1),idx_length));... - idx_block2(randsample(length(idx_block2),idx_length));... - idx_block3(randsample(length(idx_block3),idx_length))]; - end - D_subsample_umap_blockBehav(iter,:)=pdist(plot_neural_states_umap(idx_ordered,:), 'cityblock'); - D_subsample_pca_blockBehav(iter,:)=pdist(plot_neural_states_pca(idx_ordered,:), 'cityblock'); - end - - %D = D_subsample_blockBehav(randsample(500,1),:); - D_umap = mean(D_subsample_umap_blockBehav);clear D_subsample_umap_blockBehav - D_pca = mean(D_subsample_pca_blockBehav);clear D_subsample_pca_blockBehav - - Z_umap_blockBehav{s, chan} = squareform(D_umap); Z_umap_blockBehav{s, chan}(eye(size(Z_umap_blockBehav{s}))==1) = nan; - Z_pca_blockBehav{s, chan} = squareform(D_pca); Z_pca_blockBehav{s, chan}(eye(size(Z_pca_blockBehav{s}))==1) = nan; - -% % figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_umap_blockBehav{s, chan},'Colormap',flipud(cool)) -% % figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_pca_blockBehav{s, chan},'Colormap',flipud(cool)) - - %saveas(gcf,[savePath '/DistanceBetweenStates_NeighborContext.pdf']) - - Z_umap_blockBehav_lowRes{s, chan} = sepblockfun(Z_umap_blockBehav{s, chan},[idx_length,idx_length],@nanmean); - Z_pca_blockBehav_lowRes{s, chan} = sepblockfun(Z_pca_blockBehav{s, chan},[idx_length,idx_length],@nanmean); - -% % figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_umap_blockBehav_lowRes{s},'Colormap',flipud(jet)) -% % figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_pca_blockBehav_lowRes{s},'Colormap',flipud(jet)) - - close all - -% end %end of channel loop - - disp("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") - disp(s) - disp("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%") - -end %end of session for loop - -%Save data -cd(['~/Dropbox (Penn)/Datalogger/Results/All_sessions/UMAP_results/']) -% save('State_distances.mat', "behav_categ","Z_behav_umap","Z_behav_pca",... -% "Z_behav_umap_lowRes", "Z_behav_pca_lowRes","Z_umap_block","Z_pca_block",... -% "Z_umap_block_lowRes","Z_pca_block_lowRes","Z_umap_blockBehav","Z_pca_blockBehav",... -% "Z_umap_blockBehav_lowRes","Z_pca_blockBehav_lowRes") -load('State_distances.mat') - -%Plot distance to threat to subject -behav=[5,7,8,18,9]; -data = cat(3,Z_behav_umap_lowRes{:}); data = squeeze(data(6,1:5,:)); -[~, idx_sorted]=sort(nanmean(data,2),1,'descend'); -figure; hold on -bp = bar([nanmean(data(idx_sorted,:),2)],'FaceAlpha',0.2); -xticks([1:5]); xticklabels(behav_categ(behav(idx_sorted))) -ylabel('Eucledian distance to Threat to Subject') -scatter(ones(size(data,2))*1,data(idx_sorted(1),:), 'filled','r'); -scatter(ones(size(data,2))*2,data(idx_sorted(2),:), 'filled','y'); -scatter(ones(size(data,2))*3,data(idx_sorted(3),:), 'filled','g'); -scatter(ones(size(data,2))*4,data(idx_sorted(4),:), 'filled','b'); -scatter(ones(size(data,2))*5,data(idx_sorted(5),:), 'filled','c'); -%saveas(gcf,['DistanceBetweenBehavStates_allSessions.pdf']) - -data_similarity = 1./(1+data); -[~, idx_sorted]=sort(nanmean(data_similarity,2),1,'descend'); -figure; hold on -bp = bar([nanmean(data_similarity(idx_sorted,:),2)],'FaceAlpha',0.2); -xticks([1:5]); xticklabels(behav_categ(behav(idx_sorted))) -ylabel('Similarity score to Threat to Subject') -scatter(ones(size(data_similarity,2))*1,data_similarity(idx_sorted(1),:), 'filled','r'); -scatter(ones(size(data_similarity,2))*2,data_similarity(idx_sorted(2),:), 'filled','y'); -scatter(ones(size(data_similarity,2))*3,data_similarity(idx_sorted(3),:), 'filled','g'); -scatter(ones(size(data_similarity,2))*4,data_similarity(idx_sorted(4),:), 'filled','b'); -scatter(ones(size(data_similarity,2))*5,data_similarity(idx_sorted(5),:), 'filled','c'); - -%UMAP -D_behav= mean(cat(3,Z_behav_umap_lowRes{:}),3); -AxesLabels = {'Forage','Groom','Get groomed','Travel','Threat partner','Threat subject'}; -idx = tril(D_behav); -D_behav(~idx) = nan; -figure; set(gcf,'Position',[150 250 800 600]); hp= heatmap(D_behav,'Colormap',hot, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -hp.XDisplayLabels = AxesLabels; hp.YDisplayLabels = AxesLabels; -saveas(gcf,['DistanceBetweenBehav_allSessions.pdf']) - -%PCA -D_behav= mean(cat(3,Z_behav_pca_lowRes{:}),3); -AxesLabels = {'Forage','Groom','Get groomed','Travel','Threat partner','Threat subject'}; -idx = tril(D_behav); -D_behav(~idx) = nan; -figure; set(gcf,'Position',[150 250 800 600]); hp= heatmap(D_behav,'Colormap',cool, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -hp.XDisplayLabels = AxesLabels; hp.YDisplayLabels = AxesLabels; - -%Behavior, separated by monkey -figure; set(gcf,'Position',[150 250 1500 300]); subplot(1,2,1); -behav_order = [2,3,4,1,5,6]; -D_behav_amos= mean(cat(3,Z_behav_umap_lowRes{a_sessions}),3); -D_behav_amos= D_behav_amos(behav_order,behav_order); -AxesLabels = {'Forage','Groom','Get groomed','Travel','Threat partner','Threat subject'}; -AxesLabels_ordered = AxesLabels(behav_order); -idx = tril(D_behav_amos); -D_behav_amos(~idx) = nan; -hp= heatmap(D_behav_amos,'Colormap',hot, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -caxis([0 10]); title('Amos') -hp.XDisplayLabels = AxesLabels_ordered; hp.YDisplayLabels = AxesLabels_ordered; - -subplot(1,2,2); -D_behav_hooke= mean(cat(3,Z_behav_umap_lowRes{h_sessions(1:end-1)}),3); -D_behav_hooke= D_behav_hooke(behav_order,behav_order); -idx = triu(D_behav_hooke); -D_behav_hooke(~idx) = nan; -hp= heatmap(D_behav_hooke,'Colormap',hot, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -caxis([0 10]); title('Hooke') -hp.XDisplayLabels = AxesLabels_ordered; hp.YDisplayLabels = AxesLabels_ordered; -saveas(gcf,['DistanceBetweenBehav_allSessions.pdf']) - -%%%%%%%%%%%%%%%%% -% By block -D_block= mean(cat(3,Z_umap_block_lowRes{:}),3); -AxesLabels = {'Female Neighbor','Male Neighbor','Alone'}; -figure; set(gcf,'Position',[150 250 800 600]); hp= heatmap(D_block,'Colormap',hot); -hp.XDisplayLabels = AxesLabels; hp.YDisplayLabels = AxesLabels; -saveas(gcf,['DistanceBetweenBlocks_allSessions.pdf']) - -%Block, separated by monkey -figure; set(gcf,'Position',[150 250 1500 300]); subplot(1,2,1); -behav_order = [1,2,3]; -D_block_amos= mean(cat(3,Z_umap_block_lowRes{a_sessions}),3); -D_block_amos= D_block_amos(behav_order,behav_order); -AxesLabels = {'Female Neighbor','Male Neighbor','Alone'}; -AxesLabels_ordered = AxesLabels(behav_order); -idx = tril(D_block_amos); -D_block_amos(~idx) = nan; -hp= heatmap(D_block_amos,'Colormap',hot, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -caxis([0 10]); title('Amos') -hp.XDisplayLabels = AxesLabels_ordered; hp.YDisplayLabels = AxesLabels_ordered; - -subplot(1,2,2); -D_block_hooke= mean(cat(3,Z_umap_block_lowRes{h_sessions(1:end-1)}),3); -D_block_hooke= D_block_hooke(behav_order,behav_order); -idx = triu(D_block_hooke); -D_block_hooke(~idx) = nan; -hp= heatmap(D_block_hooke,'Colormap',hot, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -caxis([0 10]); title('Hooke') -hp.XDisplayLabels = AxesLabels_ordered; hp.YDisplayLabels = AxesLabels_ordered; - -%%%%%%%%%%%%%%%%%%%%%%%%%% -%Behav and block - -%Block by behav, separated by monkey -figure; set(gcf,'Position',[150 250 1500 300]); subplot(1,2,1); -behav_order = [1,2,3]; -D_blockBehav= mean(cat(3,Z_blockBehav_lowRes{a_sessions}),3); -D_blockBehav= D_blockBehav(behav_order,behav_order); -AxesLabels = {'Female Neighbor','Male Neighbor','Alone'}; -AxesLabels_ordered = AxesLabels(behav_order); -idx = tril(D_blockBehav); -D_blockBehav(~idx) = nan; -hp= heatmap(D_blockBehav,'Colormap',hot, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -caxis([1 6]); title('Amos') -hp.XDisplayLabels = AxesLabels_ordered; hp.YDisplayLabels = AxesLabels_ordered; - -subplot(1,2,2); -D_blockBehav= mean(cat(3,Z_blockBehav_lowRes{h_sessions(1:end-1)}),3); -D_blockBehav= D_blockBehav(behav_order,behav_order); -idx = triu(D_blockBehav); -D_blockBehav(~idx) = nan; -hp= heatmap(D_blockBehav,'Colormap',hot, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -caxis([1 6]); title('Hooke') -hp.XDisplayLabels = AxesLabels_ordered; hp.YDisplayLabels = AxesLabels_ordered; diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_tsne_visualization_batch.m b/Neural states/scrap_code_DO_NOT_DELETE/log_tsne_visualization_batch.m deleted file mode 100644 index 5391856..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_tsne_visualization_batch.m +++ /dev/null @@ -1,173 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 0; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1; %lump similar behavioral categories together -agg_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; - -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO"] - %channel_flag = "TEO"; - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - - - %% Select behaviors to decode - - %Extract behavior labels and frequency - behavior_labels = cell2mat({labels{:,3}}'); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Extract block labels - block_labels = cell2mat({labels{:,12}}'); - - % Select behaviors - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - %Remove behaviors we're not interested in for now - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding Rowdy Room which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize'))); - - % OR select behaviors manually - %behav =[29] ;%unique(behavior_labels); %[4,5,7,8,9,10,24];% [4:10, 23]; %[4:8,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Subset data to only consider epochs of interest - idx= find(ismember(behavior_labels,behav)); - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking dat - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_labels_final = block_labels(idx); - behavior_labels_final_rand = randsample(behavior_labels_final, length(behavior_labels_final)); - - %run PCA - tsne_plot_final = tsne(zscore(Spike_count_raster_final)); - - - %% Plot PCA - - %Set colormap - Cmap = [[1 0 0];[1 0.4 0.1];[0 0 0];[0 0.4 0.5];[0 0.7 0];[1 0 1];[0 1 1];... - [0 0 1];[0.8 0 0];[1 0 0];[0 0 0];[0.2 0.9 0.76];[0 0 0];[0 0 0];[0 0 0];... - [0 0 0];[0 0 0];[0.9 0.5 0];[0 0 0];[0 0 0];[0 0 0];[0 0 0];[0.9 0.7 0.12];[0.5 0.2 0.5];... - [0 0 0];[0 0 0];[0.8 0.4 0.4];[0 0 0];[0.5 0.5 0.5]]; - - Cmap_block = [[0.9 0.7 0.12];[0 0.6 0.8];[0.5 0 0]]; - - Cmap_time = copper(length(idx)); - - - figure; hold on; set(gcf,'Position',[150 250 1500 500]) - - %Plot PCA results color-coded by behavior - ax1=subplot(1,2,1); - scatter(tsne_plot_final(:,1), tsne_plot_final(:,2),8,Cmap(behavior_labels_final,:),'filled') - xlabel('tSNE 1'); ylabel('tSNE 2'); - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior') - set(gca,'FontSize',12); - %saveas(gcf,[savePath '/umap_supervised_ColorCodedByBehav_' channel 'Units.png']) - %pause(5) - -% %Plot UMAP results color-coded by time -% ax2=subplot(1,3,2); -% scatter3(tsne_plot_final(:,1), tsne_plot_final(:,2),tsne_plot_final(:,3),8,Cmap_time,'filled') -% xlabel('PCA 1'); ylabel('PCA 2'); zlabel('PCA 3') -% %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -% title('Time') -% set(gca,'FontSize',12); - - %Color-coded by block - ax2=subplot(1,2,2); - scatter(tsne_plot_final(:,1), tsne_plot_final(:,2),8,Cmap_block(block_labels_final,:),'filled') - xlabel('tSNE 1'); ylabel('tSNE 2'); - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Block') - set(gca,'FontSize',12); - %saveas(gcf,[savePath '/umap_ColorCodedByBlock_' channel 'Units.png']) - - sgtitle(['tsne']) - - hlink = linkprop([ax1,ax2],{'CameraPosition','CameraUpVector'}); - rotate3d on - - %clearvars -except temp chan savePath filePath temp_resolution is_mac - chan=chan+1; - - end %end of channel for loop - -end %end of session for loop - - diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_state_distances_batch.m b/Neural states/scrap_code_DO_NOT_DELETE/log_umap_state_distances_batch.m deleted file mode 100644 index 7e5e6bd..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_state_distances_batch.m +++ /dev/null @@ -1,406 +0,0 @@ -%% log_umap_visualization_batch -% This script applied unsupervised umap on smoothed firing -% Ref: Connor Meehan, Jonathan Ebrahimian, Wayne Moore, and Stephen Meehan (2022). Uniform Manifold Approximation and Projection (UMAP) (https://www.mathworks.com/matlabcentral/fileexchange/71902), MATLAB Central File Exchange. - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:6,11:13,15:16]; - -%Set parameters -with_partner =0; -temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1; %lump similar behavioral categories together -threat_precedence=1; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence -exclude_sq=1; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; chan=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - - %Low-pass filter - %Spike_count_raster = lowpass(Spike_rasters',0.005,1); - %PCA - % [coeff,score,latent,tsquared,explained] = pca(Spike_rasters'); - % Spike_count_raster = score(:,1:15); - - - %% Select behaviors to visualize - - %Extract behavior labels and frequency - behavior_labels = cell2mat({labels{:,3}}'); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Extract block labels - block_labels = cell2mat({labels{:,12}}'); - - % Select behaviors - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - %Remove behaviors we're not interested in for now - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding Rowdy Room which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize'))); - - % OR select behaviors manually - %behav =[7] ;%unique(behavior_labels); %[4,5,7,8,9,10,24];% [4:10, 23]; %[4:8,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only consider indices with behavior of interest - idx= find(ismember(behavior_labels,behav)); - Spike_count_raster_final = Spike_count_raster;%(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels;%(idx);%Same as above but in behavior labels - block_labels_final = block_labels;%(idx); - - if null - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels_final,behav_categ, temp_resolution); - behavior_labels_final = sim_behav; - end - - %% Run umap - - %Supervised - % data = [Spike_count_raster_final, behavior_labels_final]; - % [umap_result{s,chan}]=run_umap(data, 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 3,'label_column', 'end'); %Run umap to get 2d embedded states - - %Unsupervised - [umap_result]=run_umap(Spike_count_raster_final, 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 20); %Run umap to get 2d embedded states - close - - %% Run PCA - [~, pca_results] = pca(Spike_count_raster_final); - - - - %% Plot UMAP projection in 3D space - - %Set colormap - Cmap = [[1 0 0];...%Aggression; red - [1 0.4 0.1];...%Approach; dark orange - [0 0 0];...%But sniff; NA - [0.3 0.7 1];...%Drinking; light blue - [0 0.7 0];...%Foraging; dark green - [1 0 1];...%Groom sollicitation; magenta - [0 1 1];...%Groom partner; cyan - [0 0 1];...%Getting groomed; dark blue - [0.8 0 0];...%Threat to partner; dark red - [1 0 0];...%Threat to subject; red - [0.9 0.9 0];...%leave; dark yellow - [0 0 0];...%Lipsmack - [0.2 0.9 0.76];...%Masturbating; turquoise - [0.7 0 1];...%Mounting; light purple - [0.9 0.5 0];...%Other monkeys vocalize; orange - [1 0.8 0.1];...%Travel; yellow orange - [0 0 0];...%Proximity; NA - [0 0 0];...%Rowdy room; NA - [1 0 0];...%SP; NA - [1 0 0];...%SS; NA - [0.6314 0.5059 0.0118];...%Scratch; maroon - [0.5 0.2 0.5];...%Self-groom; dark purple - [ 1 0.07 0.65];...%Submission; dark pink - [0 0.4 0.5];...%Vocalzation; blue green - [0 0 0];...%Yawning; NA - [0.8 0.8 0.8]];%Rest; grey - - Cmap_block = [[0.9 0.7 0.12];[0 0.6 0.8];[0.5 0 0]]; - - % % % figure; hold on; set(gcf,'Position',[150 250 1200 500]) - % % % - % % % %Plot UMAP results color-coded by behavior - % % % ax1=subplot(1,2,1); - % % % scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),12,Cmap(behavior_labels_final,:),'filled') - % % % xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - % % % %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - % % % title('Behavior') - % % % set(gca,'FontSize',12); - % % % %saveas(gcf,[savePath '/umap_supervised_ColorCodedByBehav_' channel 'Units.png']) - % % % %pause(5) - % % % - % % % %Color-coded by block - % % % ax2=subplot(1,2,2); - % % % scatter3(umap_result(:,1), umap_result(:,2),umap_result(:,3),12,Cmap_block(block_labels_final,:),'filled') - % % % xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - % % % %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - % % % title('Block') - % % % set(gca,'FontSize',12); - % % % - % % % sgtitle([channel ' units, UMAP, ' sessions(s).name]) - % % % - % % % hlink = linkprop([ax1,ax2],{'CameraPosition','CameraUpVector'}); - % % % rotate3d on - - - %% Visualize distances between beahvior states - - %Only consider distances between behaviors from the same block - plot_behav_labels = behavior_labels_final(block_labels_final==1); - plot_neural_states = Spike_count_raster_final(block_labels_final==1,:); - plot_neural_states_umap = umap_result(block_labels_final==1,:); - plot_neural_states_pca = pca_results(block_labels_final==1,1:50); - - idx_groom = find(ismember(plot_behav_labels,7)); - %idx_travel = find(ismember(plot_behav_labels,18)); - idx_getgroom = find(ismember(plot_behav_labels,8)); - %idx_selfgroom = find(ismember(plot_behav_labels,24)); - idx_forage = find(ismember(plot_behav_labels,5)); - idx_hip = find(ismember(plot_behav_labels,9)); - idx_his = find(ismember(plot_behav_labels,10)); - idx_length = 15; - - for iter=1:500 - idx_ordered = [idx_forage(randsample(length(idx_forage),idx_length));... - %idx_selfgroom(randsample(length(idx_selfgroom),idx_length));... - idx_groom(randsample(length(idx_groom),idx_length));... - idx_getgroom(randsample(length(idx_getgroom),idx_length));... - %idx_travel(randsample(length(idx_travel),idx_length));... - idx_hip(randsample(length(idx_hip),idx_length));... - idx_his(randsample(length(idx_his),idx_length))]; - D_subsample_umap(iter,:)=pdist(plot_neural_states_umap(idx_ordered,:)); - D_subsample_pca(iter,:)=pdist(plot_neural_states_pca(idx_ordered,:)); - end - - %D = D_subsample(randsample(500,1),:); - D_umap = mean(D_subsample_umap); clear D_subsample_umap - D_pca = mean(D_subsample_pca); clear D_subsample_pca - Z_behav_umap{s} = squareform(D_umap); Z_behav_umap{s}(eye(size(Z_behav_umap{s}))==1) = nan; - Z_behav_pca{s} = squareform(D_pca); Z_behav_pca{s}(eye(size(Z_behav_pca{s}))==1) = nan; - - figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_behav_umap{s},'Colormap',flipud(hot)) - figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_behav_pca{s},'Colormap',flipud(hot)) - %saveas(gcf,[savePath '/DistanceBetweenStates_behavior.pdf']) - - Z_behav_lowRes{s} = sepblockfun(Z_behav_umap{s},[idx_length,idx_length],@nanmean); - %figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_behav_lowRes{s},'Colormap',flipud(jet)) - - %Visualize distances paired vs. alone - plot_neural_states_umap = umap_result; - - idx_block1= find(block_labels_final==1); - idx_block2= find(block_labels_final==2); - idx_block3= find(block_labels_final==3); - - idx_length = 50; - - for iter=1:500 - idx_ordered = [idx_block1(randsample(length(idx_block1),idx_length));... - idx_block2(randsample(length(idx_block2),idx_length));... - idx_block3(randsample(length(idx_block3),idx_length))]; - D_subsample_block(iter,:)=pdist(plot_neural_states_umap(idx_ordered,:), 'cityblock'); - end - - %D = D_subsample_block(randsample(500,1),:); - D_umap = mean(D_subsample_block); clear D_subsample_block - Z_block{s} = squareform(D_umap); Z_block{s}(eye(size(Z_block{s}))==1) = nan; - %figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_block{s},'Colormap',flipud(cool)) - %saveas(gcf,[savePath '/DistanceBetweenStates_PairedAloneContext.pdf']) - - Z_block_lowRes{s} = sepblockfun(Z_block{s},[idx_length,idx_length],@nanmean); - %figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_block_lowRes{s},'Colormap',flipud(jet)) - - %Visualize distances between social context given a particular - %behavior - beh=length(behav_categ); - - if ismember(beh,1) - %Pull threats to partner and subject together - behavior_labels_final(behavior_labels_final==9)=1; - behavior_labels_final(behavior_labels_final==10)=1; - end - - plot_behav_labels = behavior_labels(behavior_labels==beh); - plot_block_labels = block_labels(behavior_labels==beh); - plot_neural_states_umap = umap_result(behavior_labels==beh,:); - - idx_block1= find(plot_block_labels==1); - idx_block2= find(plot_block_labels==2); - idx_block3= find(plot_block_labels==3); - - for iter=1:500 - if ismember(beh, [7,8]) - idx_length = 100; - idx_ordered = [idx_block1(randsample(length(idx_block1),idx_length));... - idx_block2(randsample(length(idx_block2),idx_length))]; - else - idx_length = 100; - idx_ordered = [idx_block1(randsample(length(idx_block1),idx_length));... - idx_block2(randsample(length(idx_block2),idx_length));... - idx_block3(randsample(length(idx_block3),idx_length))]; - end - D_subsample_blockBehav(iter,:)=pdist(plot_neural_states_umap(idx_ordered,:), 'cityblock'); - end - - %D = D_subsample_blockBehav(randsample(500,1),:); - D_umap = mean(D_subsample_blockBehav);clear D_subsample_blockBehav - Z_blockBehav{s} = squareform(D_umap); Z_blockBehav{s}(eye(size(Z_blockBehav{s}))==1) = nan; - %figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_blockBehav{s},'Colormap',flipud(cool)) - %saveas(gcf,[savePath '/DistanceBetweenStates_NeighborContext.pdf']) - - Z_blockBehav_lowRes{s} = sepblockfun(Z_blockBehav{s},[idx_length,idx_length],@nanmean); - %figure; set(gcf,'Position',[150 250 800 600]); heatmap(Z_blockBehav_lowRes{s},'Colormap',flipud(jet)) - close all - - -end %end of session for loop - -cd(['~/Dropbox (Penn)/Datalogger/Results/All_sessions/UMAP_results/']) -save('State_distances.mat', "behav_categ","Z_behav_umap","Z_behav_lowRes","Z_block","Z_block_lowRes","Z_blockBehav","Z_blockBehav_lowRes") -load('State_distances.mat') - -behav=[5,7,8,18,9]; -data = cat(3,Z_behav_lowRes{:}); data = squeeze(data(6,1:5,:)); -[~, idx_sorted]=sort(nanmean(data,2),1,'ascend'); -figure; hold on -bp = bar([nanmean(data(idx_sorted,:),2)],'FaceAlpha',0.2); -xticks([1:5]); xticklabels(behav_categ(behav(idx_sorted))) -ylabel('Eucledian distance to Threat to Subject') -scatter(ones(size(data,2))*1,data(idx_sorted(1),:), 'filled','r'); -scatter(ones(size(data,2))*2,data(idx_sorted(2),:), 'filled','y'); -scatter(ones(size(data,2))*3,data(idx_sorted(3),:), 'filled','g'); -scatter(ones(size(data,2))*4,data(idx_sorted(4),:), 'filled','b'); -scatter(ones(size(data,2))*5,data(idx_sorted(5),:), 'filled','c'); -saveas(gcf,['DistanceBetweenBehavStates_allSessions.pdf']) - -D_behav= mean(cat(3,Z_behav_lowRes{:}),3); -AxesLabels = {'Forage','Groom','Get groomed','Travel','Threat partner','Threat subject'}; -idx = tril(D_behav); -D_behav(~idx) = nan; -figure; set(gcf,'Position',[150 250 800 600]); hp= heatmap(D_behav,'Colormap',hot, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -hp.XDisplayLabels = AxesLabels; hp.YDisplayLabels = AxesLabels; -saveas(gcf,['DistanceBetweenBehav_allSessions.pdf']) - -%Behavior, separated by monkey -figure; set(gcf,'Position',[150 250 1500 300]); subplot(1,2,1); -behav_order = [2,3,4,1,5,6]; -D_behav= mean(cat(3,Z_behav_lowRes{a_sessions}),3); -D_behav= D_behav(behav_order,behav_order); -AxesLabels = {'Forage','Groom','Get groomed','Travel','Threat partner','Threat subject'}; -AxesLabels_ordered = AxesLabels(behav_order); -idx = tril(D_behav); -D_behav(~idx) = nan; -hp= heatmap(D_behav,'Colormap',hot, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -caxis([0 10]); title('Amos') -hp.XDisplayLabels = AxesLabels_ordered; hp.YDisplayLabels = AxesLabels_ordered; - -subplot(1,2,2); -D_behav= mean(cat(3,Z_behav_lowRes{h_sessions(1:end-1)}),3); -D_behav= D_behav(behav_order,behav_order); -idx = triu(D_behav); -D_behav(~idx) = nan; -hp= heatmap(D_behav,'Colormap',hot, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -caxis([0 10]); title('Hooke') -hp.XDisplayLabels = AxesLabels_ordered; hp.YDisplayLabels = AxesLabels_ordered; -saveas(gcf,['DistanceBetweenBehav_allSessions.pdf']) - -%%%%%%%%%%%%%%%%% -% By block -D_block= mean(cat(3,Z_block_lowRes{:}),3); -AxesLabels = {'Female Neighbor','Male Neighbor','Alone'}; -figure; set(gcf,'Position',[150 250 800 600]); hp= heatmap(D_block,'Colormap',hot); -hp.XDisplayLabels = AxesLabels; hp.YDisplayLabels = AxesLabels; -saveas(gcf,['DistanceBetweenBlocks_allSessions.pdf']) - -%Block, separated by monkey -figure; set(gcf,'Position',[150 250 1500 300]); subplot(1,2,1); -behav_order = [1,2,3]; -D_block= mean(cat(3,Z_block_lowRes{a_sessions}),3); -D_block= D_block(behav_order,behav_order); -AxesLabels = {'Female Neighbor','Male Neighbor','Alone'}; -AxesLabels_ordered = AxesLabels(behav_order); -idx = tril(D_block); -D_block(~idx) = nan; -hp= heatmap(D_block,'Colormap',hot, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -caxis([5 12]); title('Amos') -hp.XDisplayLabels = AxesLabels_ordered; hp.YDisplayLabels = AxesLabels_ordered; - -subplot(1,2,2); -D_block= mean(cat(3,Z_block_lowRes{h_sessions(1:end-1)}),3); -D_block= D_block(behav_order,behav_order); -idx = triu(D_block); -D_block(~idx) = nan; -hp= heatmap(D_block,'Colormap',hot, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -caxis([5 12]); title('Hooke') -hp.XDisplayLabels = AxesLabels_ordered; hp.YDisplayLabels = AxesLabels_ordered; - -%%%%%%%%%%%%%%%%%%%%%%%%%% -%Behav and block - -%Block by behav, separated by monkey -figure; set(gcf,'Position',[150 250 1500 300]); subplot(1,2,1); -behav_order = [1,2,3]; -D_blockBehav= mean(cat(3,Z_blockBehav_lowRes{a_sessions}),3); -D_blockBehav= D_blockBehav(behav_order,behav_order); -AxesLabels = {'Female Neighbor','Male Neighbor','Alone'}; -AxesLabels_ordered = AxesLabels(behav_order); -idx = tril(D_blockBehav); -D_blockBehav(~idx) = nan; -hp= heatmap(D_blockBehav,'Colormap',hot, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -caxis([1 6]); title('Amos') -hp.XDisplayLabels = AxesLabels_ordered; hp.YDisplayLabels = AxesLabels_ordered; - -subplot(1,2,2); -D_blockBehav= mean(cat(3,Z_blockBehav_lowRes{h_sessions(1:end-1)}),3); -D_blockBehav= D_blockBehav(behav_order,behav_order); -idx = triu(D_blockBehav); -D_blockBehav(~idx) = nan; -hp= heatmap(D_blockBehav,'Colormap',hot, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -caxis([1 6]); title('Hooke') -hp.XDisplayLabels = AxesLabels_ordered; hp.YDisplayLabels = AxesLabels_ordered; diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization.m b/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization.m deleted file mode 100644 index 4008314..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization.m +++ /dev/null @@ -1,172 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "vlPFC"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=2; - -%Set path -filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) -savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - -chan = 1; - -%% Get data with specified temporal resolution and channels -if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly); -else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); -end - -disp('Data Loaded') - -%Raw data -Spike_count_raster = Spike_rasters'; -%Low-pass filter -%Spike_count_raster = lowpass(Spike_rasters',0.005,1); -%PCA -% [coeff,score,latent,tsquared,explained] = pca(Spike_rasters'); -% Spike_count_raster = score(:,1:15); - - -%% Select behaviors to visualize -%Compute freq of behavior for the session -behavior_labels = cell2mat({labels{:,3}}'); -behavior_labels(behavior_labels==21)=9; %Squeeze partner = threat to partner -behavior_labels(behavior_labels==22)=10; %Squeeze subject = threat to subject -behavior_labels(behavior_labels==19)=29; %Make proximity equal to rest -block_labels = cell2mat({labels{:,11}}'); - -behav_freq_table = tabulate(behavior_labels); -behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=0,:); % Discard 0 (non-defined behaviors) -block_categ = string(block_times{:,1})'; - -% Select behaviors - -%Compute freq of behavior for the session -behav_freq_table = tabulate(behavior_labels); -behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - -% Select behaviors with a minimum # of occurrences -min_occurrences = 30; -behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - -%Remove behaviors we're not interested in for now -behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. -behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. -behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding Rowdy Room which is a source of confusion. -behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. -behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize'))); - -% OR select behaviors manually -%behav = unique(behavior_labels); %[4,5,7,8,9,10,24];% [4:10, 23]; %[4:8,17]; %manually select behaviors of interest - -%Print behaviors selected -behavs_eval = behav_categ(behav); -disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') -fprintf('Behaviors evaluated are: %s \n', behavs_eval); -disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - -% idx = []; -% for b=behav -% idx_temp = find(ismember(behavior_labels,b)); %find the indices of the behaviors considered -% subset = min(length(idx_temp), 200); -% idx_subset = randsample(idx_temp, subset,1); -% idx = [idx; idx_subset]; -% end -idx= find(ismember(behavior_labels,behav)); - -Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data -behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels -block_labels_final = block_labels(idx); -behavior_labels_final_rand = randsample(behavior_labels_final, length(behavior_labels_final)); - -%% Run umap - -%Supervised -data = [Spike_count_raster_final, behavior_labels_final]; -%[umap_result{s,chan}]=run_umap(data, 'n_neighbors', 50, 'min_dist', 0.5, 'n_components', 3,'label_column', 'end'); %Run umap to get 2d embedded states - -%Unsupervised -[umap_result{s,chan}]=run_umap(Spike_count_raster_final, 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 3); %Run umap to get 2d embedded states -close all - -channel = char(channel_flag); - -%Set colormap -Cmap = [[0 0 0];[1 0.4 0.1];[0 0 0];[0 0.6 0.8];[0 0.7 0];[1 0 1];[0 1 1];... - [0 0 1];[0.5 0 0];[1 0 0];[0 0 0];[0 0 0];[0 0 0];[0 0 0];[0 0 0];... - [0 0 0];[0 0 0];[0.9 0.7 0.12];[0 0 0];[0 0 0];[0 0 0];[0 0 0];[0.8 0.6 0];[0.6 0 0.6];... - [0 0 0];[0 0 0];[0.2 0.2 1]]; - -Cmap_block = [[1 0 0];[0 1 0];[0 0 1]]; - -Cmap_time = cool(length(idx)); - -%% Plot UMAP projection in 3D space - -figure; hold on; set(gcf,'Position',[150 250 1500 500]) - -%Plot UMAP results color-coded by behavior -ax1=subplot(1,3,1); -scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap(behavior_labels_final,:),'filled') -xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -%set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -title('Behavior') -set(gca,'FontSize',12); -%saveas(gcf,[savePath '/umap_supervised_ColorCodedByBehav_' channel 'Units.png']) -%pause(5) - -%Plot UMAP results color-coded by time -ax2=subplot(1,3,2); -scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_time,'filled') -xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -%set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -title('Time') -set(gca,'FontSize',12); - -%Color-coded by block -ax3=subplot(1,3,3); -scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_block(block_labels_final,:),'filled') -xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -%set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -title('Block') -set(gca,'FontSize',12); -%saveas(gcf,[savePath '/umap_ColorCodedByBlock_' channel 'Units.png']) - -sgtitle([channel ' units, UMAP']) -hlink = linkprop([ax1,ax2,ax3],{'CameraPosition','CameraUpVector'}); -rotate3d on diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_dynamic.m b/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_dynamic.m deleted file mode 100644 index 0646b5c..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_dynamic.m +++ /dev/null @@ -1,180 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - - %for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - - %% Select behaviors to decode - %Compute freq of behavior for the session - behavior_labels = cell2mat({labels{:,3}}'); - block_labels = cell2mat({labels{:,11}}'); - - behav_freq_table = tabulate(behavior_labels); - %behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=0,:); % Discard 0 (non-defined behaviors) - block_categ = string(block_times{:,1})'; - - % Select behaviors - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - %behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 1; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - %behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - - % OR select behaviors manually - behav = [4,5,7,8,9,10,24];% [4:10, 23]; %[4:8,17];%[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - -% idx = []; -% for b=behav -% idx_temp = find(ismember(behavior_labels,b)); %find the indices of the behaviors considered -% subset = min(length(idx_temp), 200); -% idx_subset = randsample(idx_temp, subset,1); -% idx = [idx; idx_subset]; -% end - idx= find(ismember(behavior_labels,behav)); - - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_labels_final = block_labels(idx); - behavior_labels_final_rand = randsample(behavior_labels_final, length(behavior_labels_final)); - - %% Run umap - data = [Spike_count_raster_final, behavior_labels_final]; - [umap_result{s,chan}]=run_umap(data, 'n_neighbors', 15, 'min_dist', 0.1, 'label_column', 'end'); %Run umap to get 2d embedded states - [umap_result_nonsupervised{s,chan}]=run_umap(Spike_count_raster_final, 'n_neighbors', 10, 'min_dist', 0.5); %Run umap to get 2d embedded states - - channel = char(channel_flag); - %saveas(gcf,[savePath '/umap_unsupervised_' num2str(1000/temp_resolution) 'msec_' channel '.png']) - - %Order for later plotting - labels_plot{s} = categorical(behav_categ(behavior_labels_final)); - %labels_plot{s} = categorical(behav_categ(behavior_labels_final_rand)); - ordered_labels = behav_categ(behav);%{'Drinking', 'Foraging','Self-groom','Threat to partner', 'Threat to subject','Groom Receive','Groom Give'}; - labels_order = reordercats(labels_plot{s},ordered_labels); - - %Plot results color-coded by behavior - figure - gscatter(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2), labels_order ,[],[],10, 'MarkerFaceAlpha',0.7) - legend(ordered_labels,'Location','best') - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) - title('Brain states in dimensionally reduced space') - set(gca,'FontSize',15); - saveas(gcf,[savePath '/umap_supervised_ColorCodedByBehav_' channel 'Units.png']) - - umap_output = [umap_result{1}, behavior_labels_final]; - steps = 1:200:length(umap_output); - for st = 1:length(steps)-1 - umap_output(steps(st):steps(st+1),4)=st; - end - umap_output(steps(st):end,4)=st+1; - writematrix(umap_output,'umap_output.csv') - - figure - gscatter(umap_result_nonsupervised{s,chan}(:,1), umap_result_nonsupervised{s,chan}(:,2), labels_order ,[],[],8, 'MarkerFaceAlpha',0.7) - legend(ordered_labels,'Location','best') - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) - title('Brain states in dimensionally reduced space') - set(gca,'FontSize',15); - saveas(gcf,[savePath '/umap_supervised_ColorCodedByBehav_' channel 'Units.png']) - -% %Color-coded by amount of ME -% figure -% gscatter(umap_result(:,1), umap_result(:,2), MElabels_plot,[],[],10) -% xlabel('UMAP 1'); ylabel('UMAP 2'); -% set(gca,'xtick',[]); set(gca,'ytick',[]) - -% %Color-coded by block -% figure -% gscatter(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2), labels_plot_context{s},[],[],10) -% xlabel('UMAP 1'); ylabel('UMAP 2'); -% set(gca,'xtick',[]); set(gca,'ytick',[]) -% saveas(gcf,[savePath '/umap_ColorCodedByBlock_' channel 'Units.png']) - - pause(2) - close all - - %clearvars -except temp chan savePath filePath temp_resolution is_mac - chan=chan+1; - - %end %end of channel for loop - -end %end of session for loop - -figure -for s = 1:length(a_sessions) - subplot(length(a_sessions),1,s) - gscatter(umap_result{a_sessions(s),3}(:,1), umap_result{a_sessions(s),3}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - -for s = 1:length(h_sessions) - subplot(length(h_sessions),1,s) - gscatter(umap_result{a_sessions(s),3}(:,1), umap_result{a_sessions(s),3}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_groomCateg_batch_old.m b/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_groomCateg_batch_old.m deleted file mode 100644 index c2ff645..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_groomCateg_batch_old.m +++ /dev/null @@ -1,152 +0,0 @@ - - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0; %lump similar behavioral categories together -agg_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - - %for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - - %% Select behaviors to decode - behavior_labels = cell2mat({labels{:,3}}'); - groom_categ= {'Star.vs.end', 'Post-threat','Reciprocated','Initiated'}; %label grooming categ - categ =3; - groom_labels = groom_labels_all(:,categ+1); - groom_categ_label = {'Not Reciprocated', 'Reciprocated'}; - - % Select behaviors manually - behav = 7;%[7,8];%groom receive - behavs_eval = behav_categ(behav); - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = groom_labels(idx,:);%Same as above but in behavior labels - - - %% Run umap - - %Supervised -% data = [Spike_count_raster_final, behavior_labels_final]; -% [umap_result{s,chan}]=run_umap(data, 'n_neighbors', 10, 'min_dist', 0.5, 'label_column', 'end'); %Run umap to get 2d embedded states - - %Unsupervised - [umap_result{s,chan}]=run_umap(Spike_count_raster_final, 'n_neighbors', 15, 'min_dist', 0.5,'n_components',3); %Run umap to get 2d embedded states - - channel = char(channel_flag); - %saveas(gcf,[savePath '/umap_unsupervised_' num2str(1000/temp_resolution) 'msec_' channel '.png']) - - %Order for later plotting - labels_plot{s} = categorical(groom_categ_label(behavior_labels_final)); - ordered_labels = {'Not Reciprocated', 'Reciprocated'}; - labels_order = reordercats(labels_plot{s},ordered_labels); - - %Set colormap - Cmap = [[0.8 0 0];[0 1 1]]; - - %Plot results color-coded by behavior - figure - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),15,Cmap(behavior_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior') - set(gca,'FontSize',12); - - figure - gscatter(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2), labels_order ,[],[],10, 'MarkerFaceAlpha',0.7) - legend(ordered_labels,'Location','best') - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) - title('Grooming reciprocated vs. not') - saveas(gcf,[savePath '/umap_supervised_ColorCodedGroomingCateg_' channel 'Units.png']) - - - figure - gscatter(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2), labels_order ,[],[],10, 'MarkerFaceAlpha',0.7) - legend(ordered_labels,'Location','best') - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) - title('Grooming initiated vs. not') - saveas(gcf,[savePath '/umap_supervised_ColorCodedGroomingCateg_' channel 'Units.png']) - - - close all - - %clearvars -except temp chan savePath filePath temp_resolution is_mac - chan=chan+1; - - %end %end of channel for loop - -end %end of session for loop - -figure -for s = 1:length(a_sessions) - subplot(length(a_sessions),1,s) - gscatter(umap_result{a_sessions(s),1}(:,1), umap_result{a_sessions(s),1}(:,2), labels_plot{a_sessions(s)},[],[],5) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - -for s = 1:length(h_sessions) - subplot(length(h_sessions),1,s) - gscatter(umap_result{a_sessions(s),1}(:,1), umap_result{a_sessions(s),1}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_groomCateg_batch_v2.m b/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_groomCateg_batch_v2.m deleted file mode 100644 index 9d0bbe7..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_groomCateg_batch_v2.m +++ /dev/null @@ -1,126 +0,0 @@ - - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0; %lump similar behavioral categories together -agg_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - - %for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - - %% Select behaviors to decode - behavior_labels = cell2mat({labels{:,3}}'); - - % Select behaviors manually - behav = 8;%[7,8];%groom receive - behavs_eval = behav_categ(behav); - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - - - %% Run umap - - %Unsupervised - [umap_result{s,chan}]=run_umap(Spike_count_raster_final, 'n_neighbors', 15, 'min_dist', 0.5,'n_components',3); %Run umap to get 2d embedded states - - channel = char(channel_flag); - - %Select grooming category to visualize - groom_categ= {'Star.vs.end', 'Post-threat','Reciprocated','Initiated'}; %label grooming categ - categ =4; - groom_labels = groom_labels_all(:,categ+1); - behavior_labels_final = groom_labels(idx,:);%Same as above but in behavior labels - - %Set colormap - Cmap = [[0.8 0 0];[0 1 1]]; - - %Plot results color-coded by behavior - figure - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),15,Cmap(behavior_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior') - set(gca,'FontSize',12); - - - close all - - %clearvars -except temp chan savePath filePath temp_resolution is_mac - chan=chan+1; - - %end %end of channel for loop - -end %end of session for loop - -figure -for s = 1:length(a_sessions) - subplot(length(a_sessions),1,s) - gscatter(umap_result{a_sessions(s),1}(:,1), umap_result{a_sessions(s),1}(:,2), labels_plot{a_sessions(s)},[],[],5) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - -for s = 1:length(h_sessions) - subplot(length(h_sessions),1,s) - gscatter(umap_result{a_sessions(s),1}(:,1), umap_result{a_sessions(s),1}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_groom_batch.m b/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_groom_batch.m deleted file mode 100644 index 933b399..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_groom_batch.m +++ /dev/null @@ -1,272 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0; %lump similar behavioral categories together - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range(2:end) %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO"] - %channel_flag = "vlPFC"; - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - %Low-pass filter - %Spike_count_raster = lowpass(Spike_rasters',0.005,1); - %PCA - % [coeff,score,latent,tsquared,explained] = pca(Spike_rasters'); - % Spike_count_raster = score(:,1:15); - - - %% Select behaviors to visualize - - %Extract behavior labels and frequency - behavior_labels = cell2mat({labels{:,3}}'); -% behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Threat to partner"); -% behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Threat to subject"); -% behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - if simplify - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - end - - if null - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels,behav_categ, temp_resolution); - behavior_labels = sim_behav; - end - - %Extract block labels - block_labels = cell2mat({labels{:,12}}'); - block_categ = string(block_times{:,1})'; - - % Select behaviors - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select grooming manually - behav =[7,8] ;%unique(behavior_labels); %[4,5,7,8,9,10,24];% [4:10, 23]; %[4:8,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only consider indices with behavior of interest - idx= find(ismember(behavior_labels,behav)); - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_labels_final = block_labels(idx); - behavior_labels_final_rand = randsample(behavior_labels_final, length(behavior_labels_final)); - - %Choose grooming category to represent - groom_categ= {'Star.vs.end', 'Post-threat','Reciprocated','Initiated'}; %label grooming categ - sum(groom_labels_all==2) - categ =3; - groom_labels_final = groom_labels_all(idx,categ+1); - groom_categ_label = {'Not Reciprocated', 'Reciprocated'}; - - %% Run umap - - %Supervised -% data = [Spike_count_raster_final, behavior_labels_final]; -% [umap_result{s,chan}]=run_umap(data, 'n_neighbors', 50, 'min_dist', 0.5, 'n_components', 3,'label_column', 'end'); %Run umap to get 2d embedded states - - %Unsupervised - [umap_result{s,chan}]=run_umap(Spike_count_raster_final, 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 3); %Run umap to get 2d embedded states - close - - channel = char(channel_flag); - - %Set colormap - Cmap = [[1 0 0];[1 0.4 0.1];[0 0 0];[0.1 0.8 0.9];[0 0.7 0];[1 0 1];[0 1 1];... - [0 0 1];[0.8 0 0];[1 0 0];[0 0 0];[0.2 0.9 0.76];[0 0 0];[0 0 0];[0.7 0 1];... - [0 0 0];[0 0 0];[0.9 0.5 0];[0 0 0];[0 0 0];[0.8 0 0];[1 0 0];[0.9 0.7 0.12];[0.5 0.2 0.5];... - [0 0 0];[0 0 0];[0.8 0.4 0.4];[0 0 0];[0.5 0.5 0.5]]; - - Cmap_block = [[0.9 0.7 0.12];[0 0.6 0.8];[0.5 0 0]]; - - %Cmap_time = copper(size(Spike_count_raster,1)); - Cmap_time = copper(length(idx)); - - Cmap_groom = [[0 1 0];[1 0 0]]; - - %% Plot UMAP projection in 3D space - - figure; hold on; set(gcf,'Position',[150 250 1500 500]) - - %Plot UMAP results color-coded by behavior - ax1=subplot(1,4,1); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap(behavior_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior') - set(gca,'FontSize',12); - %saveas(gcf,[savePath '/umap_supervised_ColorCodedByBehav_' channel 'Units.png']) - %pause(5) - - %Plot UMAP results color-coded by time - ax2=subplot(1,4,2); - %scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_time(idx,:),'filled') - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_time,'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Time') - set(gca,'FontSize',12); - - %Color-coded by groom-context - ax3=subplot(1,4,3); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_groom(groom_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Block') - set(gca,'FontSize',12); - - %Color-coded by block - ax4=subplot(1,4,4); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_block(block_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Block') - set(gca,'FontSize',12); - - sgtitle([channel ' units, UMAP, ' sessions(s).name]) - - hlink = linkprop([ax1,ax2,ax3,ax4],{'CameraPosition','CameraUpVector'}); - rotate3d on - - savefig([savePath 'Umap_3Dprojection_' channel '.fig']) - %saveas(gcf,[savePath '/umap_ColorCodedByBlock_' channel 'Units.png']) - -% cd(savePath) -% OptionZ.FrameRate=30;OptionZ.Duration=15;OptionZ.Periodic=true; -% CaptureFigVid([-20,10;-380,190],['Umap_3Dprojection_' channel],OptionZ) - - chan=chan+1; - - end %end of channel for loop - - figure; hold on; set(gcf,'Position',[150 250 1000 800]) - - %Plot UMAP results color-coded by behavior vlPFC - ax1=subplot(2,2,1); chan =1; - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap(behavior_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior, vlPFC') - set(gca,'FontSize',12); - - %Color-coded by block vlPFC - ax2=subplot(2,2,2); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_block(block_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Social context, vlPFC') - set(gca,'FontSize',12); - - %Plot UMAP results color-coded by behavior TEO - ax3=subplot(2,2,3); chan =2; - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap(behavior_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior, TEO') - set(gca,'FontSize',12); - - ax4=subplot(2,2,4); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_block(block_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Social context, TEO') - set(gca,'FontSize',12); - - sgtitle([sessions(s).name]) - hlink = linkprop([ax1,ax2,ax3,ax4],{'CameraPosition','CameraUpVector'}); - rotate3d on - - savefig([savePath 'Umap_3Dprojection_bothAreas.fig']) - -% cd(savePath) -% OptionZ.FrameRate=30;OptionZ.Duration=15;OptionZ.Periodic=true; -% CaptureFigVid([-20,10;-380,190],['Umap_3Dprojection_bothAreas'],OptionZ) - - -end %end of session for loop - -figure -for s = 1:length(a_sessions) - subplot(length(a_sessions),1,s) - gscatter(umap_result{a_sessions(s),3}(:,1), umap_result{a_sessions(s),3}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - -for s = 1:length(h_sessions) - subplot(length(h_sessions),1,s) - gscatter(umap_result{a_sessions(s),3}(:,1), umap_result{a_sessions(s),3}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_neighborBehav_batch.m b/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_neighborBehav_batch.m deleted file mode 100644 index 627ea48..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_neighborBehav_batch.m +++ /dev/null @@ -1,200 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =2; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -min_occurrences=30; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=11; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - - %for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - elseif with_partner ==2 - [Spike_rasters, labels, labels_partner, labels_neighbor, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_neighbor(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - behavior_labels_neighbor_init = cell2mat({labels_neighbor{:,3}}'); - block_labels = cell2mat({labels{:,11}}'); %Extract block info - alone_block_num = find(strcmp(block_times{:,1},"Alone.block")); - - %SANITY CHECK: Compute overlap between partner behavior and subject behavior - perc_overlap_subject_partner = length(find(behavior_labels_subject_init == behavior_labels_partner_init))/length(behavior_labels_subject_init); - perc_overlap_subject_neighbor = length(find(behavior_labels_subject_init == behavior_labels_neighbor_init))/length(behavior_labels_subject_init); - perc_overlap_partner_neighbor = length(find(behavior_labels_partner_init == behavior_labels_neighbor_init))/length(behavior_labels_subject_init); - - - - behavior_labels = behavior_labels_neighbor_init; - - %% Select behaviors to decode - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - % Then select non-reciprocal behaviors - behav = setdiff(behav, reciprocal_set); - - % OR select behaviors manually - %behav = [4,5,17,23,25];%manually select behaviors of interest - %Select behaviors manually to ensure that the same - %behaviors are considered for the partner and subject comparisons. - %This list could change from session to session.. I'll have to - %think of a way to automatize this. - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav) & block_labels ==2);%~=alone_block_num); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(removecats(categorical(behavior_labels_final))); - - %check the amount of labels that differ in the partner vs. subject - %labels after selecting the behaviors of interest. - subject_behav_after_selection = behavior_labels_subject_init(idx); - partner_behav_after_selection = behavior_labels_partner_init(idx); - neighbor_behav_after_selection = behavior_labels_neighbor_init(idx); - block_after_selection = block_labels(idx); - - - overlap_neighbor_subject_after_selection = length(find(neighbor_behav_after_selection == subject_behav_after_selection))/length(idx); - fprintf('Percent overlap between subject and neighbor labels AFTER selecting behaviors: %s \n', num2str(overlap_neighbor_subject_after_selection)) - - overlap_neighbor_partner_after_selection = length(find(neighbor_behav_after_selection == partner_behav_after_selection))/length(idx); - fprintf('Percent overlap between partner and neighbor labels AFTER selecting behaviors: %s \n', num2str(overlap_neighbor_partner_after_selection)) - - alone_block_obs = length(find(block_after_selection==3))/length(idx); - fprintf('Percent observations in "alone" block AFTER selecting behaviors: %s \n', num2str(alone_block_obs)) - - %Only consider windows where the behaviors of subject and - %partner do not overlap - diff_idx = find(neighbor_behav_after_selection ~= subject_behav_after_selection); %find the indices where subject and partner behavior do not overlap - Spike_count_raster_final = Spike_count_raster_final(diff_idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels_final(diff_idx,:);%Same as above but in behavior labels - block_after_selection_final = block_after_selection(diff_idx); - - %Make sure that only the behaviors with min occurrences are - %considered - behav_freq_table2= tabulate(behavior_labels_final); - behav = behav_freq_table2(behav_freq_table2(:,2)>=min_occurrences,1); - idxfinal = find(ismember(behavior_labels_final,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster_final(idxfinal,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels_final(idxfinal,:);%Same as above but in behavior labels - block_after_selection_final = block_after_selection(idxfinal); - - tabulate(removecats(categorical(behavior_labels_final))); - tabulate(block_after_selection_final) - crosstab(removecats(categorical(behavior_labels_final)), block_after_selection_final) - %Note: not all behaviors are equally happening across blocks. It's - %possible that what we decode is actually block ID and not the - %behavior itself... - - %Display which behaviors will be decoded - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - - %% Run umap - - %Supervised - data = [Spike_count_raster_final, behavior_labels_final]; - [umap_result{s,chan}]=run_umap(data, 'n_neighbors', 50, 'min_dist', 1, 'label_column', 'end'); %Run umap to get 2d embedded states - - %Unsupervised - %[umap_result{s,chan}]=run_umap(Spike_count_raster_final, 'n_neighbors', 10, 'min_dist', 0.5); %Run umap to get 2d embedded states - - channel = char(channel_flag); - %saveas(gcf,[savePath '/umap_unsupervised_' num2str(1000/temp_resolution) 'msec_' channel '.png']) - - %Order for later plotting - labels_plot{s} = categorical(behav_categ(behavior_labels_final)); - ordered_labels = string(unique(labels_plot{s})); - labels_order = reordercats(labels_plot{s},ordered_labels); - - %Plot results color-coded by behavior - figure - gscatter(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2), labels_plot{s} ,[],[],10, 'MarkerFaceAlpha',0.7) - legend(ordered_labels,'Location','best') - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) - title('Neighbor behavior') - saveas(gcf,[savePath '/umap_supervised_ColorCodedNeighborBehv_' channel 'Units.png']) - - close all - - %clearvars -except temp chan savePath filePath temp_resolution is_mac - chan=chan+1; - - %end %end of channel for loop - -end %end of session for loop - -figure -for s = 1:length(a_sessions) - subplot(length(a_sessions),1,s) - gscatter(umap_result{a_sessions(s),1}(:,1), umap_result{a_sessions(s),1}(:,2), labels_plot{a_sessions(s)},[],[],5) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - -for s = 1:length(h_sessions) - subplot(length(h_sessions),1,s) - gscatter(umap_result{a_sessions(s),1}(:,1), umap_result{a_sessions(s),1}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_neighbor_batch.m b/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_neighbor_batch.m deleted file mode 100644 index c52c63f..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_neighbor_batch.m +++ /dev/null @@ -1,132 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - - %for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - - %% Select behaviors to decode - %Compute freq of behavior for the session - behavior_labels = cell2mat({labels{:,3}}'); - block_labels = cell2mat({labels{:,11}}'); - block_categ = string(block_times{:,1})'; - - % Select behaviors - behav = [7, 8];% [4:10, 23]; %[4:8,17];%[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - idx= find(ismember(behavior_labels,behav)); - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_labels_final = block_labels(idx); - - - %% Run umap - data = [Spike_count_raster_final, block_labels_final]; - [umap_unsupervised_result{s,chan}]=run_umap(Spike_count_raster_final, 'n_neighbors', 10, 'min_dist', 0.5, 'label_column', 'end'); - [umap_result{s,chan}]=run_umap(data, 'n_neighbors', 10, 'min_dist', 0.5, 'label_column', 'end'); %Run umap to get 2d embedded states - channel = char(channel_flag); - %saveas(gcf,[savePath '/umap_unsupervised_' num2str(1000/temp_resolution) 'msec_' channel '.png']) - - %Order for later plotting - labels_plot_context{s} = categorical(block_categ(block_labels_final)); - labels_order = labels_plot_context{s}; - %labels_plot_context{s} = categorical(behav_categ(behavior_labels_final)); - labels_order = labels_plot_context{s}; - - %Plot results color-coded by behavior - figure - %gscatter(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2), labels_order ,[],[],6, 'MarkerFaceAlpha',0.7) - gscatter(umap_unsupervised_result{s,chan}(:,1), umap_unsupervised_result{s,chan}(:,2), labels_order ,[],[],6, 'MarkerFaceAlpha',0.7) - %legend({'Groom Give','Groom Receive'}) - legend({'Neighbor1','Neighbor2'}) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) - title('Neighbor identity during grooming') - saveas(gcf,[savePath '/umap_supervised_ColorCodedByNeighborID_' channel 'Units.png']) - -% %Color-coded by amount of ME -% figure -% gscatter(umap_result(:,1), umap_result(:,2), MElabels_plot,[],[],10) -% xlabel('UMAP 1'); ylabel('UMAP 2'); -% set(gca,'xtick',[]); set(gca,'ytick',[]) - -% %Color-coded by block -% figure -% gscatter(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2), labels_plot_context{s},[],[],10) -% xlabel('UMAP 1'); ylabel('UMAP 2'); -% set(gca,'xtick',[]); set(gca,'ytick',[]) -% saveas(gcf,[savePath '/umap_ColorCodedByBlock_' channel 'Units.png']) - - pause(2) - close all - - %clearvars -except temp chan savePath filePath temp_resolution is_mac - chan=chan+1; - - %end %end of channel for loop - -end %end of session for loop - -figure -for s = 1:length(a_sessions) - subplot(length(a_sessions),1,s) - gscatter(umap_result{a_sessions(s),1}(:,1), umap_result{a_sessions(s),1}(:,2), labels_plot{a_sessions(s)},[],[],5) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - -for s = 1:length(h_sessions) - subplot(length(h_sessions),1,s) - gscatter(umap_result{a_sessions(s),1}(:,1), umap_result{a_sessions(s),1}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_partner_batch.m b/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_partner_batch.m deleted file mode 100644 index 0212f77..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_partner_batch.m +++ /dev/null @@ -1,210 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =1; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1.5;%set the smoothing window size (sigma) -null =0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO"] - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - - %Extract subejct and partner video - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - - %Extract block labels - block_labels = cell2mat({labels{:,12}}'); - block_categ = string(block_times{:,1})'; - - %% Select behaviors to decode - - %Only consider epochs where subject is resting - idx = find(ismember(behavior_labels_subject_init,length(behav_categ)) &... - ~ismember(behavior_labels_partner_init,length(behav_categ))); %find the indices where the subject is resting and the partner is not - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_partner = behavior_labels_partner_init(idx); - behavior_labels_subject = behavior_labels_subject_init(idx); - behav = unique(behavior_labels_partner); - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels_partner); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences=30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - - %Select the correct indices. - idx_beh = find(ismember(behavior_labels_partner,behav)); - subject_behav_after_selection = behavior_labels_subject(idx_beh); - partner_behav_after_selection = behavior_labels_partner(idx_beh); - block_after_selection = block_labels(idx_beh); - - Spike_count_raster_final = Spike_count_raster_final(idx_beh,:); - behavior_labels_final = partner_behav_after_selection; - - overlap_partner_subject_after_selection = length(find(partner_behav_after_selection == subject_behav_after_selection))/length(idx); - fprintf('Percent overlap between subject and partner labels AFTER selecting behaviors: %s \n', num2str(overlap_partner_subject_after_selection)) - alone_block_obs = length(find(block_after_selection==3))/length(idx); - fprintf('Percent observations in "alone" block AFTER selecting behaviors: %s \n', num2str(alone_block_obs)) - - - % %Only consider behaviors during the alone block - % block_after_selection_overlap_out = block_after_selection(diff_idx); - % alone_idx = find(block_after_selection_overlap_out==3); - % Spike_count_raster_final = Spike_count_raster_final(alone_idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - % behavior_labels_final = behavior_labels_final(alone_idx,:);%Same as above but in behavior labels - % tabulate(behavior_labels_final); - - if null - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels_final,behav_categ, temp_resolution); - behavior_labels_final = sim_behav; - end - - - %% Run umap - - %Supervised -% data = [Spike_count_raster_final, behavior_labels_final]; -% [umap_result{s,chan}]=run_umap(data, 'n_neighbors', 50, 'min_dist', 0.5, 'n_components', 3,'label_column', 'end'); %Run umap to get 2d embedded states - - %Unsupervised - [umap_result{s,chan}]=run_umap(Spike_count_raster_final, 'n_neighbors', 15, 'min_dist', 1, 'n_components', 3); %Run umap to get 2d embedded states - close - - channel = char(channel_flag); - - %Set colormap - Cmap = [[0 0 0];[1 0.4 0.1];[0 0 0];[0 0.6 0.8];[0 0.7 0];[1 0 1];[0 1 1];... - [0 0 1];[0.8 0 0];[1 0 0];[0 0 0];[0.2 0.9 0.76];[0 0 0];[0 0 0];[0 0 0];... - [0 0 0];[0 0 0];[1 0.5 0];[0 0 0];[0 0 0];[0 0 0];[0 0 0];[0.9 0.7 0.12];[0.5 0.2 0.5];... - [0 0 0];[0.8 0.8 0];[0.8 0.4 0.4];[0 0 0];[0.5 0.5 0.5]]; - - Cmap_block = [[1 0 1];[0 0.6 0.8];[0.5 0 0]]; - - Cmap_time = copper(length(idx_beh)); - %Cmap_time = copper(size(Spike_count_raster,1)); - - %% Plot UMAP projection in 3D space - - figure; hold on; set(gcf,'Position',[150 250 1500 500]); pointsize = 15; - - %Plot UMAP results color-coded by subject behavior - ax1=subplot(1,3,1); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),pointsize,Cmap(subject_behav_after_selection,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior subject') - set(gca,'FontSize',12); - - %Plot UMAP results color-coded by partner behavior - ax2=subplot(1,3,2); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),pointsize,Cmap(partner_behav_after_selection,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior partner') - set(gca,'FontSize',12); - - %Plot UMAP results color-coded by time - ax3=subplot(1,3,3); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),pointsize,Cmap_time,'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Time') - set(gca,'FontSize',12); - -% %Color-coded by block -% ax3=subplot(1,3,3); -% scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),pointsize,Cmap_block(block_after_selection,:),'filled') -% xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') -% %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) -% title('Block') -% set(gca,'FontSize',12); - - sgtitle([channel ' units, UMAP, ' sessions(s).name]) - - hlink = linkprop([ax1,ax2,ax3],{'CameraPosition','CameraUpVector'}); - rotate3d on - - savefig([savePath 'Partner_Umap_3Dprojection_' channel '.fig']) - - chan=chan+1; - - end %end of channel for loop - -end %end of session for loop - -figure -for s = 1:length(a_sessions) - subplot(length(a_sessions),1,s) - gscatter(umap_result{a_sessions(s),1}(:,1), umap_result{a_sessions(s),1}(:,2), labels_plot{a_sessions(s)},[],[],5) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - -for s = 1:length(h_sessions) - subplot(length(h_sessions),1,s) - gscatter(umap_result{a_sessions(s),1}(:,1), umap_result{a_sessions(s),1}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_social_vs_asocial_batch.m b/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_social_vs_asocial_batch.m deleted file mode 100644 index 08b8375..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_social_vs_asocial_batch.m +++ /dev/null @@ -1,175 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - - %for channel_flag = ["vlPFC", "TEO", "all"] - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - disp('Data Loaded') - - Spike_count_raster = Spike_rasters'; - behavior_labels = cell2mat({labels{:,3}}'); - block_labels = cell2mat({labels{:,11}}'); - - %% Select behaviors to plot - - %Only consider activity during one of the paired blocks - block_labels = cell2mat({labels{:,11}}'); - pair2block = find(strcmp(block_times{:,1}, 'Pair2.block')); - idx_block = find(block_labels==pair2block); - Spike_count_raster = Spike_count_raster(idx_block,:); - behavior_labels = behavior_labels(idx_block); - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 20; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - %behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - - % OR select behaviors manually - % behav = [4:10,16,23];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(removecats(categorical(behavior_labels_final))); - - social_labels_final = ones(size(behavior_labels_final)); - social_labels_final(ismember(behavior_labels_final, social_set)) = 2; - social_label_names = {'asocial','social'}; - tabulate(social_labels_final) - - - %% Run umap - data = [Spike_count_raster_final, social_labels_final]; - - %Supervised UMAP - [umap_result{s,chan}]=run_umap(data, 'n_neighbors', 10, 'min_dist', 0.5, 'label_column', 'end'); %Run umap to get 2d embedded states - %Unsupervised UMAP - %[umap_result{s,chan}]=run_umap(Spike_count_raster_final, 'n_neighbors', 10, 'min_dist', 0.5); %Run umap to get 2d embedded states - - channel = char(channel_flag); - %saveas(gcf,[savePath '/umap_unsupervised_' num2str(1000/temp_resolution) 'msec_' channel '.png']) - - %Order for later plotting - labels_plot{s} = categorical(behav_categ(behavior_labels_final)); - %labels_plot{s} = categorical(behav_categ(behavior_labels_final_rand)); - ordered_labels = string(unique(labels_plot{s})); - - labels_plot{s}= categorical(social_label_names(social_labels_final)); - ordered_labels = social_label_names; - - labels_order = reordercats(labels_plot{s},ordered_labels); - - %Plot results color-coded by social vs. non-social - figure - gscatter(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2), labels_order ,[],[],6, 'MarkerFaceAlpha',0.7) - legend(ordered_labels,'Location','best') - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) - title('Brain states in dimensionally reduced space') - saveas(gcf,[savePath '/umap_supervised_ColorCodedBySocialVSNonSocial_byBehav_' channel 'Units.png']) - - %Plot results color-coded by behavior - figure - gscatter(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2), labels_order ,[],[],6, 'MarkerFaceAlpha',0.7) - legend(ordered_labels,'Location','best') - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) - title('Brain states in dimensionally reduced space') - -% %Color-coded by amount of ME -% figure -% gscatter(umap_result(:,1), umap_result(:,2), MElabels_plot,[],[],10) -% xlabel('UMAP 1'); ylabel('UMAP 2'); -% set(gca,'xtick',[]); set(gca,'ytick',[]) - -% %Color-coded by block -% figure -% gscatter(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2), labels_plot_context{s},[],[],10) -% xlabel('UMAP 1'); ylabel('UMAP 2'); -% set(gca,'xtick',[]); set(gca,'ytick',[]) -% saveas(gcf,[savePath '/umap_ColorCodedByBlock_' channel 'Units.png']) - - pause(2) - close all - - %clearvars -except temp chan savePath filePath temp_resolution is_mac - chan=chan+1; - - %end %end of channel for loop - -end %end of session for loop - -figure -for s = 1:length(a_sessions) - subplot(length(a_sessions),1,s) - gscatter(umap_result{a_sessions(s),3}(:,1), umap_result{a_sessions(s),3}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - -for s = 1:length(h_sessions) - subplot(length(h_sessions),1,s) - gscatter(umap_result{a_sessions(s),3}(:,1), umap_result{a_sessions(s),3}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - diff --git a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_units_batch.m b/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_units_batch.m deleted file mode 100644 index 1004e15..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/log_umap_visualization_units_batch.m +++ /dev/null @@ -1,188 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 0; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=0; %lump similar behavioral categories together - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range(2:end) %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - chan = 1; - - for channel_flag = ["vlPFC", "TEO"] - %channel_flag = "vlPFC"; - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - - %Unsupervised - [umap_result{s,chan}]=run_umap(Spike_rasters, 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 3); %Run umap to get 2d embedded states - close - - channel = char(channel_flag); - - %Set colormap - for n=1:length(brain_label) - if brain_label(n)=="vlPFC" - brain_label_num(n)=1; - else - brain_label_num(n)=2; - end - end - - Cmap=[1 0 0; 0 0 1]; - %% Plot UMAP projection in 3D space - - figure; hold on;% set(gcf,'Position',[150 250 1500 500]) - - %Plot UMAP results color-coded by behavior - figure; scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),15,Cmap(brain_label_num,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior') - set(gca,'FontSize',12); - %saveas(gcf,[savePath '/umap_supervised_ColorCodedByBehav_' channel 'Units.png']) - %pause(5) - - %Plot UMAP results color-coded by time - ax2=subplot(1,3,2); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_time,'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Time') - set(gca,'FontSize',12); - - %Color-coded by block - ax3=subplot(1,3,3); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_block(block_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Block') - set(gca,'FontSize',12); - - sgtitle([channel ' units, UMAP, ' sessions(s).name]) - - hlink = linkprop([ax1,ax2,ax3],{'CameraPosition','CameraUpVector'}); - rotate3d on - - savefig([savePath 'Umap_3Dprojection_' channel '.fig']) - %saveas(gcf,[savePath '/umap_ColorCodedByBlock_' channel 'Units.png']) - -% cd(savePath) -% OptionZ.FrameRate=30;OptionZ.Duration=15;OptionZ.Periodic=true; -% CaptureFigVid([-20,10;-380,190],['Umap_3Dprojection_' channel],OptionZ) - - chan=chan+1; - - end %end of channel for loop - - figure; hold on; set(gcf,'Position',[150 250 1000 800]) - - %Plot UMAP results color-coded by behavior vlPFC - ax1=subplot(2,2,1); chan =1; - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap(behavior_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior, vlPFC') - set(gca,'FontSize',12); - - %Color-coded by block vlPFC - ax2=subplot(2,2,2); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_block(block_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Social context, vlPFC') - set(gca,'FontSize',12); - - %Plot UMAP results color-coded by behavior TEO - ax3=subplot(2,2,3); chan =2; - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap(behavior_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Behavior, TEO') - set(gca,'FontSize',12); - - ax4=subplot(2,2,4); - scatter3(umap_result{s,chan}(:,1), umap_result{s,chan}(:,2),umap_result{s,chan}(:,3),8,Cmap_block(block_labels_final,:),'filled') - xlabel('UMAP 1'); ylabel('UMAP 2'); zlabel('UMAP 3') - %set(gca,'xtick',[]); set(gca,'ytick',[]); set(gca,'ztick',[]) - title('Social context, TEO') - set(gca,'FontSize',12); - - sgtitle([sessions(s).name]) - hlink = linkprop([ax1,ax2,ax3,ax4],{'CameraPosition','CameraUpVector'}); - rotate3d on - - savefig([savePath 'Umap_3Dprojection_bothAreas.fig']) - -% cd(savePath) -% OptionZ.FrameRate=30;OptionZ.Duration=15;OptionZ.Periodic=true; -% CaptureFigVid([-20,10;-380,190],['Umap_3Dprojection_bothAreas'],OptionZ) - - -end %end of session for loop - -figure -for s = 1:length(a_sessions) - subplot(length(a_sessions),1,s) - gscatter(umap_result{a_sessions(s),3}(:,1), umap_result{a_sessions(s),3}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - -for s = 1:length(h_sessions) - subplot(length(h_sessions),1,s) - gscatter(umap_result{a_sessions(s),3}(:,1), umap_result{a_sessions(s),3}(:,2), labels_plot{a_sessions(s)},[],[],10) - xlabel('UMAP 1'); ylabel('UMAP 2'); - set(gca,'xtick',[]); set(gca,'ytick',[]) -end - diff --git a/Neural states/scrap_code_DO_NOT_DELETE/umap_animate.R b/Neural states/scrap_code_DO_NOT_DELETE/umap_animate.R deleted file mode 100644 index 234467f..0000000 --- a/Neural states/scrap_code_DO_NOT_DELETE/umap_animate.R +++ /dev/null @@ -1,36 +0,0 @@ -library(gganimate) - -setwd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/Amos_2021-07-29'); - -umap_data = read.csv2('umap_output.csv',sep=',',header=FALSE) -colnames(umap_data)=c("dim1","dim2","label","window") - -# query.points = data.frame( -# i = 'stage', -# x = x, -# y = y -# ) -# -# -# p = ggplot() + -# geom_point(data=ref.points,aes(x,y)) + -# geom_point(data=query.points,aes(x,y)) + - - -p = plot.umap(umaps.all,color=color.by,file=NULL,legend=FALSE) + - ggtitle('{closest_state}') + - transition_states(iter, transition_length = 4, state_length = 1) + - ease_aes('cubic-in-out') -push.status('plot.umap') - -a = animate( - plot = p, - # renderer = gifski_renderer(loop = FALSE), - nframes = 200, width = 800, height = 640, res = 300 -) -push.status('animate') - -anim_save( - file.path('animations',paste0('umap-harmony-',prefix,'-',gsub('_','',color.by),'-all_nolegend.gif')), - a -) \ No newline at end of file diff --git a/Behavior/.DS_Store b/Preprocessing/.DS_Store similarity index 63% rename from Behavior/.DS_Store rename to Preprocessing/.DS_Store index dde69c0..039492b 100644 Binary files a/Behavior/.DS_Store and b/Preprocessing/.DS_Store differ diff --git a/Behavior/Eventlog_check_StartEnd.R b/Preprocessing/Behavioral preprocessing/Eventlog_check_StartEnd.R similarity index 100% rename from Behavior/Eventlog_check_StartEnd.R rename to Preprocessing/Behavioral preprocessing/Eventlog_check_StartEnd.R diff --git a/Behavior/Format_behavior_log.R b/Preprocessing/Behavioral preprocessing/Format_behavior_log.R similarity index 100% rename from Behavior/Format_behavior_log.R rename to Preprocessing/Behavioral preprocessing/Format_behavior_log.R diff --git a/Behavior/Format_grooming_log.R b/Preprocessing/Behavioral preprocessing/Format_grooming_log.R similarity index 100% rename from Behavior/Format_grooming_log.R rename to Preprocessing/Behavioral preprocessing/Format_grooming_log.R diff --git a/Neural preprocessing/.DS_Store b/Preprocessing/Neural preprocessing/.DS_Store similarity index 69% rename from Neural preprocessing/.DS_Store rename to Preprocessing/Neural preprocessing/.DS_Store index 6e93b26..856d1e7 100644 Binary files a/Neural preprocessing/.DS_Store and b/Preprocessing/Neural preprocessing/.DS_Store differ diff --git a/Neural preprocessing/FiltDetect.ofb b/Preprocessing/Neural preprocessing/FiltDetect.ofb similarity index 100% rename from Neural preprocessing/FiltDetect.ofb rename to Preprocessing/Neural preprocessing/FiltDetect.ofb diff --git a/Neural preprocessing/FiltDetect.ofb.log b/Preprocessing/Neural preprocessing/FiltDetect.ofb.log similarity index 100% rename from Neural preprocessing/FiltDetect.ofb.log rename to Preprocessing/Neural preprocessing/FiltDetect.ofb.log diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/README.txt b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/README.txt similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/README.txt rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/README.txt diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/maxTimestampInTicks.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/maxTimestampInTicks.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/maxTimestampInTicks.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/maxTimestampInTicks.m diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddContinuous.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddContinuous.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddContinuous.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddContinuous.m diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddContinuousWithMultipleFragments.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddContinuousWithMultipleFragments.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddContinuousWithMultipleFragments.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddContinuousWithMultipleFragments.m diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddEvent.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddEvent.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddEvent.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddEvent.m diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddInterval.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddInterval.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddInterval.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddInterval.m diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddMarker.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddMarker.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddMarker.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddMarker.m diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddNeuron.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddNeuron.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddNeuron.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddNeuron.m diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddWaveform.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddWaveform.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddWaveform.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexAddWaveform.m diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexCreateFileData.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexCreateFileData.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexCreateFileData.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/nexCreateFileData.m diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/parse_json license.txt b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/parse_json license.txt similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/parse_json license.txt rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/parse_json license.txt diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/parse_json.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/parse_json.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/parse_json.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/parse_json.m diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/readNex5File.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/readNex5File.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/readNex5File.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/readNex5File.m diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/readNexFile.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/readNexFile.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/readNexFile.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/readNexFile.m diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeNex5File.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeNex5File.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeNex5File.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeNex5File.m diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeNex5VarHeader.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeNex5VarHeader.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeNex5VarHeader.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeNex5VarHeader.m diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeNexFile.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeNexFile.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeNexFile.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeNexFile.m diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeNexVarHeader.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeNexVarHeader.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeNexVarHeader.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeNexVarHeader.m diff --git a/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeStringPaddedWithZeros.m b/Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeStringPaddedWithZeros.m similarity index 100% rename from Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeStringPaddedWithZeros.m rename to Preprocessing/Neural preprocessing/HowToReadAndWriteNexAndNex5FilesInMatlab/writeStringPaddedWithZeros.m diff --git a/Neural preprocessing/Matlab utilities/.DS_Store b/Preprocessing/Neural preprocessing/Matlab utilities/.DS_Store similarity index 100% rename from Neural preprocessing/Matlab utilities/.DS_Store rename to Preprocessing/Neural preprocessing/Matlab utilities/.DS_Store diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/BlockFileFormatExample.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/BlockFileFormatExample.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/BlockFileFormatExample.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/BlockFileFormatExample.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/DataTypeEnum.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/DataTypeEnum.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/DataTypeEnum.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/DataTypeEnum.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ExtractDataByType.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ExtractDataByType.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ExtractDataByType.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ExtractDataByType.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ExtractHeaderData.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ExtractHeaderData.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ExtractHeaderData.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ExtractHeaderData.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ExtractMotionSensorDataByType.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ExtractMotionSensorDataByType.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ExtractMotionSensorDataByType.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ExtractMotionSensorDataByType.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ExtractRTKData.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ExtractRTKData.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ExtractRTKData.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ExtractRTKData.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/FindDataBlockStart.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/FindDataBlockStart.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/FindDataBlockStart.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/FindDataBlockStart.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/FindMotionSensorBlockStart.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/FindMotionSensorBlockStart.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/FindMotionSensorBlockStart.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/FindMotionSensorBlockStart.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/GetMotionSensorTimestamp.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/GetMotionSensorTimestamp.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/GetMotionSensorTimestamp.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/GetMotionSensorTimestamp.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/GetTimestamps.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/GetTimestamps.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/GetTimestamps.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/GetTimestamps.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/HeaderConstants.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/HeaderConstants.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/HeaderConstants.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/HeaderConstants.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/MatlabBlockFileFormatExample.pdf b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/MatlabBlockFileFormatExample.pdf similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/MatlabBlockFileFormatExample.pdf rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/MatlabBlockFileFormatExample.pdf diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/MotionSensorConstants.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/MotionSensorConstants.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/MotionSensorConstants.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/MotionSensorConstants.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/MotionSensorEnum.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/MotionSensorEnum.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/MotionSensorEnum.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/MotionSensorEnum.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/PartitionConstants.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/PartitionConstants.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/PartitionConstants.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/PartitionConstants.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/RTKHeaderStruct.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/RTKHeaderStruct.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/RTKHeaderStruct.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/RTKHeaderStruct.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ScaleAudioData.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ScaleAudioData.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ScaleAudioData.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ScaleAudioData.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ScaleMotionSensorData.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ScaleMotionSensorData.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ScaleMotionSensorData.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/ScaleMotionSensorData.m diff --git a/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/SortDataByAxis.m b/Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/SortDataByAxis.m similarity index 100% rename from Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/SortDataByAxis.m rename to Preprocessing/Neural preprocessing/Matlab utilities/Block_File_Format_Matlab_Example/SortDataByAxis.m diff --git a/Neural preprocessing/Matlab utilities/ExampleAudioRatLog.m b/Preprocessing/Neural preprocessing/Matlab utilities/ExampleAudioRatLog.m similarity index 100% rename from Neural preprocessing/Matlab utilities/ExampleAudioRatLog.m rename to Preprocessing/Neural preprocessing/Matlab utilities/ExampleAudioRatLog.m diff --git a/Neural preprocessing/Matlab utilities/MotionSensorExample/Constants.m b/Preprocessing/Neural preprocessing/Matlab utilities/MotionSensorExample/Constants.m similarity index 100% rename from Neural preprocessing/Matlab utilities/MotionSensorExample/Constants.m rename to Preprocessing/Neural preprocessing/Matlab utilities/MotionSensorExample/Constants.m diff --git a/Neural preprocessing/Matlab utilities/MotionSensorExample/MotionSensorExample.m b/Preprocessing/Neural preprocessing/Matlab utilities/MotionSensorExample/MotionSensorExample.m similarity index 100% rename from Neural preprocessing/Matlab utilities/MotionSensorExample/MotionSensorExample.m rename to Preprocessing/Neural preprocessing/Matlab utilities/MotionSensorExample/MotionSensorExample.m diff --git a/Neural preprocessing/Matlab utilities/MotionSensorExample/MotionSensorUserGuide_v1.pdf b/Preprocessing/Neural preprocessing/Matlab utilities/MotionSensorExample/MotionSensorUserGuide_v1.pdf similarity index 100% rename from Neural preprocessing/Matlab utilities/MotionSensorExample/MotionSensorUserGuide_v1.pdf rename to Preprocessing/Neural preprocessing/Matlab utilities/MotionSensorExample/MotionSensorUserGuide_v1.pdf diff --git a/Neural preprocessing/Matlab utilities/MotionSensorExample/divideMSDataIntoBlocks.m b/Preprocessing/Neural preprocessing/Matlab utilities/MotionSensorExample/divideMSDataIntoBlocks.m similarity index 100% rename from Neural preprocessing/Matlab utilities/MotionSensorExample/divideMSDataIntoBlocks.m rename to Preprocessing/Neural preprocessing/Matlab utilities/MotionSensorExample/divideMSDataIntoBlocks.m diff --git a/Neural preprocessing/Matlab utilities/MotionSensorExample/extractMotionSensorData.m b/Preprocessing/Neural preprocessing/Matlab utilities/MotionSensorExample/extractMotionSensorData.m similarity index 100% rename from Neural preprocessing/Matlab utilities/MotionSensorExample/extractMotionSensorData.m rename to Preprocessing/Neural preprocessing/Matlab utilities/MotionSensorExample/extractMotionSensorData.m diff --git a/Neural preprocessing/Matlab utilities/MotionSensorExample/extractTimeStamps.m b/Preprocessing/Neural preprocessing/Matlab utilities/MotionSensorExample/extractTimeStamps.m similarity index 100% rename from Neural preprocessing/Matlab utilities/MotionSensorExample/extractTimeStamps.m rename to Preprocessing/Neural preprocessing/Matlab utilities/MotionSensorExample/extractTimeStamps.m diff --git a/Neural preprocessing/Matlab utilities/MotionSensorExample/interpolateTimeStamps.m b/Preprocessing/Neural preprocessing/Matlab utilities/MotionSensorExample/interpolateTimeStamps.m similarity index 100% rename from Neural preprocessing/Matlab utilities/MotionSensorExample/interpolateTimeStamps.m rename to Preprocessing/Neural preprocessing/Matlab utilities/MotionSensorExample/interpolateTimeStamps.m diff --git a/Neural preprocessing/Matlab utilities/MotionSensorExample/readMSData.m b/Preprocessing/Neural preprocessing/Matlab utilities/MotionSensorExample/readMSData.m similarity index 100% rename from Neural preprocessing/Matlab utilities/MotionSensorExample/readMSData.m rename to Preprocessing/Neural preprocessing/Matlab utilities/MotionSensorExample/readMSData.m diff --git a/Neural preprocessing/Matlab utilities/NeuralExtractionCode/Example.m b/Preprocessing/Neural preprocessing/Matlab utilities/NeuralExtractionCode/Example.m similarity index 100% rename from Neural preprocessing/Matlab utilities/NeuralExtractionCode/Example.m rename to Preprocessing/Neural preprocessing/Matlab utilities/NeuralExtractionCode/Example.m diff --git a/Neural preprocessing/Matlab utilities/NeuralExtractionCode/GetMetaData.m b/Preprocessing/Neural preprocessing/Matlab utilities/NeuralExtractionCode/GetMetaData.m similarity index 100% rename from Neural preprocessing/Matlab utilities/NeuralExtractionCode/GetMetaData.m rename to Preprocessing/Neural preprocessing/Matlab utilities/NeuralExtractionCode/GetMetaData.m diff --git a/Neural preprocessing/OFS Matlab Import Notes.pdf b/Preprocessing/Neural preprocessing/OFS Matlab Import Notes.pdf similarity index 100% rename from Neural preprocessing/OFS Matlab Import Notes.pdf rename to Preprocessing/Neural preprocessing/OFS Matlab Import Notes.pdf diff --git a/Neural preprocessing/RemArtifactsSortAndExport.ofb b/Preprocessing/Neural preprocessing/RemArtifactsSortAndExport.ofb similarity index 100% rename from Neural preprocessing/RemArtifactsSortAndExport.ofb rename to Preprocessing/Neural preprocessing/RemArtifactsSortAndExport.ofb diff --git a/Neural preprocessing/extract_data_forR.m b/Preprocessing/Neural preprocessing/extract_data_forR.m similarity index 100% rename from Neural preprocessing/extract_data_forR.m rename to Preprocessing/Neural preprocessing/extract_data_forR.m diff --git a/Neural preprocessing/log_GenerateDataToRes_function.m b/Preprocessing/Neural preprocessing/log_GenerateDataToRes_function.m similarity index 95% rename from Neural preprocessing/log_GenerateDataToRes_function.m rename to Preprocessing/Neural preprocessing/log_GenerateDataToRes_function.m index 96c7752..b7eef21 100644 --- a/Neural preprocessing/log_GenerateDataToRes_function.m +++ b/Preprocessing/Neural preprocessing/log_GenerateDataToRes_function.m @@ -1,7 +1,14 @@ function [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey,... unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original] = ... log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac,... - with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq) + with_MUC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq) + +%This function allows loading the data from Offline sorter (OFS), +%deuteron and behavioral annotation files, converting the data into +%data formats easily manipulable in matlab. + +%Created by C. Testard in Dec. 2021 +%Last reviewed by C. Testard in Jan. 2024 %Log GenerateDataToRes_function % Input data: @@ -14,21 +21,22 @@ % Arguments: % filePath: is the experimental data path % Temp_resolution: is the temporal resolution at which we would like to -% analyze the dat -% Channel_flag: specifies with channels to include: only TEO array, only -% vlPFC array or all channels +% analyze the data. 1:1sec; 10:100msec; 0.1:10sec +% Channel_flag: specifies with channels to include: only "TEO" array, only +% "vlPFC" array or "all" channels % is_mac: specifies whether the code is run on a mac or pc -% with_NC: specifies whether the "noise cluster" (or the first cell of -% every channel) is included (1) or not (0). If with_NC=2 then we only -% include the noise cluster (not the other neurons). +% with_MUC: specifies whether the "multi-unit cluster" (or the first cell of +% every channel) is included (1) or not (0). If with_MUC=2 then we only +% include the multi-unit cluster (not well-isolated neurons). % isolatedOnly: specifies if only the well isolated units are considered. % smooth: is the data smoothed using function conv. % sigma: size of smoothing -% threat_precedence: does aggression take precedence over threat events. +% threat_precedence: do threat event labels take over other behavior lbls. +% exclude_sq: exclude squeeze events -% This function formats the raw input data to have the following elements (output data): +% Specifically, this function formats the raw input data to have the following elements (output data): % 1. Spike_rasters: Neural data matrix, size [Time (to chosen resolution) x #neurons] -% 2. labels: Label vector which describes the behavior at time t [Time (to chosen resolution) x 4] +% 2. labels: Label vector which describes the behavior at time t [Time (to chosen resolution) x 13] % 1st column includes all behaviors in "plain english" % 2nd column behavior number code % 3rd column unique behavior code (when two occur @@ -37,7 +45,7 @@ % with another. String description. % 5th column numerical code for the type of co-occurrence % 6th column is the behavior "reciprocal" (i.e. partner behavior can -% be 100% predictted by subject behavior and vice-versa) +% be 100% predicted by subject behavior and vice-versa) % 7th column binary code reciprocal (1) vs. not (0) % 8th column is the behavior "social" or not (i.e. done with a % conspecific) @@ -47,13 +55,14 @@ % 11th column gives a corresponding numerical value to the block order in time (1st, 2nd 3rd). % 12th column is a numerical version of block ID (social context). % 13th column whether individual is paired (1) or alone (0) -% 3. labels_partner: same as above but for the partner +% 3. labels_partner: same as above but for the partner [in this version +% left blank since we did not consider the partner behavior in the study] % 4. behav_categ: behavioral categories % 5. block_times: Order and timing of blocks during the session % 6. monkey: ID of the subject monkey % 7. reciprocal_set: Set of reciprocal behaviors % 8. social_set: Set of social behaviors -% 9.unit_count: Number of units per brain area +% 9. unit_count: Number of units per brain area % 10.groom_labels_all: Label of grooming category. Columns correspond to: % 1. Behavior label. If not grooming (7 groom give, 8 groom receive), % then all categories below will be 0. If grooming, the bout can be @@ -67,7 +76,7 @@ % 13.behav_categ_original: original category labeling % Camille Testard - Created Nov. 2021 -% Last update - March 2023 +% Last update - Jan. 2024 %% Load data @@ -84,12 +93,13 @@ %Load neural data if isolatedOnly==1 - load(['Neural_data_' session '_IsolatedUnits.mat'])%only including well isolated units + load(['Neural_data_' session '_IsolatedUnits.mat'])%only including well isolated units (available for certain sessions only) else load(['Neural_data_' session '.mat']) % Load neural data; array1 is in TEO and array2 is in vlPFC end length_recording = size(Unit_rasters,2); %Unit rasters in second resolution +%get unit count num_unit_allsessions = readtable('~/Dropbox (Penn)/Datalogger/Results/All_sessions/Number of units/Session_log_num_units.csv');% Load number of unit data session_idx = find(~cellfun(@isempty,(strfind(num_unit_allsessions.session_name,session)))); unit_count = [num_unit_allsessions.num_units_vlPFC(session_idx), num_unit_allsessions.num_units_TEO(session_idx), num_unit_allsessions.num_units(session_idx)]; @@ -98,7 +108,7 @@ behavior_log = readtable('EVENTLOG_restructured.csv');% for subject block_log = readtable('session_block_schedule.csv');% for block info -% Assign threat to partner and threat to sibject labels to squeeze events. +% Assign threat to partner and threat to subject labels to sq events. if exclude_sq index=find(strcmp('SS',behavior_log{:,'Behavior'})); behavior_log{index,'Behavior'}={'HIS'}; @@ -125,6 +135,7 @@ behavior_log(end-2:end,:) = []; labels_partner=[]; + %% Neural data Chan_name = fieldnames(SpikeData); %Identify channel names @@ -170,7 +181,7 @@ %Create spike matrix structure -if with_NC==0 %If don't include noise cluster +if with_MUC==0 %If don't include multi-unit cluster unit=1; for i = channels %For all channels if length(SpikeData.(Chan_name{i}))>1 %If there are sorted units on this channel @@ -192,7 +203,7 @@ end end end -elseif with_NC==2 %if ONLY include the noise cluster +elseif with_MUC==2 %if ONLY include the noise cluster unit=1; for i = channels %For all channels if ~isempty(SpikeData.(Chan_name{i})) %If there are sorted units on this channel @@ -262,9 +273,7 @@ %% Get behavior label vector for each time bin at specified resolution %Create behavior key -%behav_categ = unique(behavior_log{:,'Behavior'}); %Get all the unique behaviors -%Set it constant across sessions. This makes coding consistent across -%sessions. +%Set it constant across sessions. behav_categ = ["Aggression","Proximity","Groom Give", "HIP","Foraging", "Vocalization", "Masturbating",... "Submission", "Approach","Yawning","Self-groom","HIS","Other monkeys vocalize", "Lip smack",... "Groom Receive","Leave","Drinking","Pacing/Travel","Scratch","RR", "Butt sniff","Grm prsnt","Mounting","SS","SP"]; @@ -297,7 +306,6 @@ % name of the behavior label %Create event time intervals: -%For subject monkey start_times = behavior_log{:,'start_time_round'}; end_times = behavior_log{:,'end_time_round'}; Intervals = [start_times end_times]; diff --git a/Neural preprocessing/log_GetMetaData.m b/Preprocessing/Neural preprocessing/log_GetMetaData.m similarity index 100% rename from Neural preprocessing/log_GetMetaData.m rename to Preprocessing/Neural preprocessing/log_GetMetaData.m diff --git a/Neural preprocessing/log_Prepare_NeuroExplorer_data_for_OFS.m b/Preprocessing/Neural preprocessing/log_Prepare_NeuroExplorer_data_for_OFS.m similarity index 100% rename from Neural preprocessing/log_Prepare_NeuroExplorer_data_for_OFS.m rename to Preprocessing/Neural preprocessing/log_Prepare_NeuroExplorer_data_for_OFS.m diff --git a/Neural preprocessing/log_Prepare_NeuroExplorer_data_for_OFS_Combined_Channels.m b/Preprocessing/Neural preprocessing/log_Prepare_NeuroExplorer_data_for_OFS_Combined_Channels.m similarity index 100% rename from Neural preprocessing/log_Prepare_NeuroExplorer_data_for_OFS_Combined_Channels.m rename to Preprocessing/Neural preprocessing/log_Prepare_NeuroExplorer_data_for_OFS_Combined_Channels.m diff --git a/Neural preprocessing/log_Read_sorted_NEX_file.m b/Preprocessing/Neural preprocessing/log_Read_sorted_NEX_file.m similarity index 100% rename from Neural preprocessing/log_Read_sorted_NEX_file.m rename to Preprocessing/Neural preprocessing/log_Read_sorted_NEX_file.m diff --git a/Neural preprocessing/log_Read_sorted_NEX_file_CombinedChannels.m b/Preprocessing/Neural preprocessing/log_Read_sorted_NEX_file_CombinedChannels.m similarity index 100% rename from Neural preprocessing/log_Read_sorted_NEX_file_CombinedChannels.m rename to Preprocessing/Neural preprocessing/log_Read_sorted_NEX_file_CombinedChannels.m diff --git a/Neural preprocessing/log_extract_neural_data.m b/Preprocessing/Neural preprocessing/log_extract_neural_data.m similarity index 100% rename from Neural preprocessing/log_extract_neural_data.m rename to Preprocessing/Neural preprocessing/log_extract_neural_data.m diff --git a/Neural preprocessing/lol_Extract_NEX_data.m b/Preprocessing/Neural preprocessing/lol_Extract_NEX_data.m similarity index 100% rename from Neural preprocessing/lol_Extract_NEX_data.m rename to Preprocessing/Neural preprocessing/lol_Extract_NEX_data.m diff --git a/Neural preprocessing/old_code/log_GenerateDataToRes_Example_function.m b/Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_Example_function.m similarity index 100% rename from Neural preprocessing/old_code/log_GenerateDataToRes_Example_function.m rename to Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_Example_function.m diff --git a/Neural preprocessing/old_code/log_GenerateDataToRes_Ron.m b/Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_Ron.m similarity index 100% rename from Neural preprocessing/old_code/log_GenerateDataToRes_Ron.m rename to Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_Ron.m diff --git a/Neural preprocessing/old_code/log_GenerateDataToRes_function_backup.m b/Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_function_backup.m similarity index 100% rename from Neural preprocessing/old_code/log_GenerateDataToRes_function_backup.m rename to Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_function_backup.m diff --git a/Neural preprocessing/old_code/log_GenerateDataToRes_function_hmm.m b/Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_function_hmm.m similarity index 100% rename from Neural preprocessing/old_code/log_GenerateDataToRes_function_hmm.m rename to Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_function_hmm.m diff --git a/Neural preprocessing/old_code/log_GenerateDataToRes_function_neighbor.m b/Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_function_neighbor.m similarity index 100% rename from Neural preprocessing/old_code/log_GenerateDataToRes_function_neighbor.m rename to Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_function_neighbor.m diff --git a/Neural preprocessing/old_code/log_GenerateDataToRes_function_old.m b/Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_function_old.m similarity index 100% rename from Neural preprocessing/old_code/log_GenerateDataToRes_function_old.m rename to Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_function_old.m diff --git a/Neural preprocessing/log_GenerateDataToRes_function_temp.asv b/Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_function_temp.asv similarity index 100% rename from Neural preprocessing/log_GenerateDataToRes_function_temp.asv rename to Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_function_temp.asv diff --git a/Neural preprocessing/log_GenerateDataToRes_groom_function.asv b/Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_groom_function.asv similarity index 100% rename from Neural preprocessing/log_GenerateDataToRes_groom_function.asv rename to Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_groom_function.asv diff --git a/Neural preprocessing/old_code/log_GenerateDataToRes_groom_function.m b/Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_groom_function.m similarity index 100% rename from Neural preprocessing/old_code/log_GenerateDataToRes_groom_function.m rename to Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GenerateDataToRes_groom_function.m diff --git a/Neural preprocessing/log_GetNumUnits.m b/Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GetNumUnits.m similarity index 100% rename from Neural preprocessing/log_GetNumUnits.m rename to Preprocessing/Neural preprocessing/old_code_DO-NOT-USE/log_GetNumUnits.m diff --git a/README.md b/README.md index 2548b85..bbb7305 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Neural signatures of natural behavior in socializing macaques -This repository contains the code and resources associated with our research on the neural underpinnings of natural primate social behavior. +This repository contains the code associated with our research on the neural underpinnings of natural social behavior in primates (rhesus macaques). To access to raw data, go to: https://osf.io/e2xsu/ Our work, titled "_Neural signatures of natural behavior in freely-socializing macaques_", was conducted by Testard & Tremblay et al. (2023) and can be accessed [here](https://www.biorxiv.org/content/10.1101/2023.07.05.547833v1). @@ -10,24 +10,7 @@ Our understanding of the neurobiology of primate behavior largely derives from a ### Repo Contents -This repository contains all the code used for the analysis and visualization of the data collected in our study. It's organized into several directories: +This repository contains all the code used for the analysis and visualization of the behavioral and neural data collected in our study. Most of the code is in Matlab with some pieces in R (behavioral data visualization only). +The code is organized into several directories which are split by figure content and topic. We also include a folder for external toolboxes used in the code and a folder for preprcossing the data. Each script takes the preprocessed and formatted data as input (starts by running the function "log_GenerateDatToRes_function.m"). Make sure to add the full github directory to your path in Matlab. -_Behavior_: ... - -_GLM_ - -_Linear decoding_ - -_Movement analysis_ - -_Neural preprocessing_ - -_Neural states_ - -_Ridge regression_ - -_SanityChecksToyExamples_ - -_Single neuron_ - -_Toolboxes__ +If you have any questions about the code or data please reach Dr. Camille Testard at ctestard@fas.harvard.edu or camille.testard94@gmail.com diff --git a/Single neuron/HierarchichalClustering.m b/Single neuron/HierarchichalClustering.m deleted file mode 100644 index 8a7724a..0000000 --- a/Single neuron/HierarchichalClustering.m +++ /dev/null @@ -1,124 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1; %lump similar behavioral categories together -threat_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence -exclude_sq=1; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/PCA_results/']; - - chan = 1; - - - %% Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - Spike_count_raster = zscore(Spike_rasters'); - - session_with_buffer = [250*temp_resolution:size(Spike_count_raster,1)-250*temp_resolution]; - idx = [500:2500]; %idx = session_with_buffer; - activity_all = Spike_count_raster(idx,:)'; - - X=clusterdata(activity_all,'Distance','correlation','Linkage','ward','maxclust',10); - %expects neuron by time - %figure; histogram(X) - - %Sort data and plot - [~, sorted_idx]=sort(X); - sorted_activity_all=activity_all(sorted_idx,:); - figure; imagesc(sorted_activity_all); colorbar; caxis([-2 2]) - - - %Extract behavior labels - behavior_labels = cell2mat({labels{:,3}}');%Get behavior label from labels structure - - - %Set colormap - %uisetcolor([0.6 0.8 1]) - Cmap = [[1 0 0];...%Aggression; red - [1 0.4 0.1];...%Approach; dark orange - [0 0 0];...%But sniff; NA - [0.3 0.7 1];...%Drinking; light blue - [0 0.7 0];...%Foraging; dark green - [1 0 1];...%Groom sollicitation; magenta - [0 1 1];...%Groom partner; cyan - [0 0 1];...%Getting groomed; dark blue - [0.8 0 0];...%Threat to partner; dark red - [1 0 0];...%Threat to subject; red - [0.9 0.9 0];...%leave; dark yellow - [0 0 0];...%Lipsmack - [0.2 0.9 0.76];...%Masturbating; turquoise - [0.7 0 1];...%Mounting; light purple - [0.9 0.5 0];...%Other monkeys vocalize; orange - [1 0.8 0.1];...%Travel; yellow orange - [0 0 0];...%Proximity; NA - [0 0 0];...%Rowdy room; NA - [0.6314 0.5059 0.0118];...%Scratch; maroon - [0.5 0.2 0.5];...%Self-groom; dark purple - [ 1 0.07 0.65];...%Submission; dark pink - [0 0.4 0.5];...%Vocalzation; blue green - [0 0 0];...%Yawning; NA - [0.8 0.8 0.8]];%Rest; grey - - %eva = evalclusters(activity_all','Linkage','DaviesBouldin','KList',1:100); eva.OptimalK - - %Step 1. Linkage - tree = linkage(activity_all,'ward'); - - %Step 2. calculate the distance - D = pdist(activity_all); - - %Step 3. Optimal branches - leafOrder = optimalleaforder(tree,D); - - %Step 4. Order and plot the output - sorted_activity_all=activity_all(leafOrder,:); - figure; imagesc(sorted_activity_all); colorbar; caxis([-2 2]) - figure; imagesc(behavior_labels(idx)'); xline(block_times.start_time(2)*temp_resolution,'LineWidth',4); xline(block_times.start_time(3)*temp_resolution,'LineWidth',4); colormap(Cmap); colorbar - - %Step 5. Get the dendrogram - figure() - Z = dendrogram(tree,0,'Reorder',leafOrder,'Orientation','left','ColorThreshold',100); - - %Step 6. Get clusters - T = cluster(tree,'Cutoff',100); -end \ No newline at end of file diff --git a/Single neuron/log_LifetimeSparseness_subsample_selectedBehav_batch.m b/Single neuron/log_LifetimeSparseness_subsample_selectedBehav_batch.m deleted file mode 100644 index 8923dc9..0000000 --- a/Single neuron/log_LifetimeSparseness_subsample_selectedBehav_batch.m +++ /dev/null @@ -1,425 +0,0 @@ -%% Log_LifetimeSparseness_batch -% This script computes firing rate of individual neuron under different -% behavioral conditions. Then, it computes a cohen's d (or effect size) -% difference between the distribution of firing rates during behavior X -% with a baseline firing rate (during rest). -% Jan 2023, C. Testard - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -plot_toggle = 0; %0: suppress plotting; 1:plot -select_behav=0; %If only plot heatmap for desired behavior -with_partner = 0; %need partner info? 0: No; 1:yes -temp_resolution = 10; %Temporal resolution of firing rate. 1: 1sec; 10:100msec; 0.1: 10sec -channel_flag = "all"; %Channels considered. vlPFC, TEO or all -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -min_occurrence =50*temp_resolution; %Minimum number of occurrences in the session needed to be considered for this analysis. -cohend_cutoff=0.3; p_cutoff=0.005;%Set "significance" thresholds -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want a null response by simulating a behavioral sequence with similar statistics -threat_precedence=1; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence -n_iter=100; -exclude_sq = 1; - -% %Initialize session batch variables: -% n_behav = length(behav_categ)-1; -% mean_cohend_per_behav = nan(max(session_range_no_partner), n_behav); -% median_cohend_per_behav = nan(max(session_range_no_partner), n_behav); -% std_cohend_per_behav = nan(max(session_range_no_partner), n_behav); -% se_cohend_per_behav = nan(max(session_range_no_partner), n_behav); -% prop_selective_per_behav = nan(max(session_range_no_partner), n_behav); -% num_selective_behav_per_neuron=cell(1,max(session_range_no_partner)); -% n_per_behav = nan(max(session_range_no_partner),n_behav); -% -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/Subject_behav']; - - - %% Load data - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - session_length = size(Spike_rasters,2); % get session length - - %Extract behavior labels - behavior_labels = cell2mat({labels{:,3}}');%Get behavior label from labels structure - - %Simplify labels - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); %Consider 'approach' to be 'Travel'. - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); %Consider 'approach' to be 'Travel'. - - %Exclude behavior of others - behavior_labels(behavior_labels==find(behav_categ=="Rowdy Room"))=length(behav_categ)+1; - behavior_labels(behavior_labels==find(behav_categ=="Other monkeys vocalize"))=length(behav_categ)+1; - - - %% Set parameters - behav = [find(behav_categ=="Drinking"),find(behav_categ=="Foraging"),... - find(behav_categ=="Groom partner"),find(behav_categ=="Getting groomed"),... - find(behav_categ=="Threat to partner"),find(behav_categ=="Threat to subject"),... - find(behav_categ=="Self-groom"),find(behav_categ=="Rest")]; - unqLabels = behav; %Get unique behavior labels (exclude rest) - n_neurons(s) = size(Spike_rasters,1); %Get number of neurons - n_behav = length(unqLabels); %Get number of unique behavior labels - - %Estimate "baseline" neural firing distribution. - idx_rest=find(behavior_labels==length(behav_categ));%Get idx of "rest" epochs. - mean_baseline = mean(Spike_rasters(:,idx_rest),2); - std_baseline = std(Spike_rasters(:,idx_rest),0,2); - - %Check visually that baseline is taken from epochs throughout the session - if plot_toggle - y=zeros(1, session_length); y(idx_rest)=1; - figure; plot(1:session_length, y); ylim([-0.5, 1.5]) - yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) - xlabel('Time in s'); title('Baseline epochs') - set(gca,'FontSize',15); - saveas(gcf, [savePath '/Baseline_epochs.png']); pause(2); close all - end - - - %% Compute cohen's d - - %Initialize matrices - cohend = nan(n_iter,n_neurons(s),n_behav); - mean_beh = nan(n_iter,n_neurons(s), n_behav); - std_beh = nan(n_iter,n_neurons(s), n_behav); - p = nan(n_iter,n_neurons(s), n_behav); - - for n = 1:n_neurons(s) %for all neurons - - for b = 1:n_behav %for all behaviors - idx = find(behavior_labels == unqLabels(b)); %get idx where behavior b occurred - n_per_behav(s,b)=length(idx);%get the number of time points for behavior b - - if n_per_behav(s,b)>min_occurrence % if behavior occurs at least during 'min_occurrence' time points - - - %Over multiple iterations - for sample=1:n_iter - %Subsample idx for behavior to match min (similar statistical power - %across behaviors) - idx_behav = randsample(idx,min_occurrence); - - mean_beh(sample,n,b)=mean(Spike_rasters(n, idx_behav),2); %get the mean firing rate during behavior b - std_beh(sample,n,b)=std(Spike_rasters(n, idx_behav),0,2); %get the standard deviation firing rate during behavior b - - %Compute a cohen d between the distribution of firing rate - %during behavior b and a baseline state (rest) - cohend(sample,n,b) = (mean_beh(sample,n,b)-mean_baseline(n)) ./ sqrt( ((n_per_behav(s,b)-1)*(std_beh(sample,n,b).^2) + (length(idx_rest)-1)*(std_baseline(n).^2)) / (n_per_behav(s,b)+length(idx_rest)-2) ); %Compute cohen d - - %get p-value from ttest comparing the distribution of - %firing rate during behavior b and rest. - [~, p(sample,n,b)] = ttest2(Spike_rasters(n, idx_behav), Spike_rasters(n,idx_rest)); - end - - end - - end - end - - mean_beh = squeeze(nanmean(mean_beh,1)); - std_beh = squeeze(nanmean(std_beh,1)); - cohend = squeeze(nanmean(cohend,1)); - p = squeeze(nanmean(p,1)); - - %Correct for multiple comparisons using Benjamini & Hochberg/Yekutieli false discovery rate control procedure for a set of statistical tests - %Ref: David Groppe (2022). fdr_bh (https://www.mathworks.com/matlabcentral/fileexchange/27418-fdr_bh), MATLAB Central File Exchange. Retrieved November 4, 2022. - [adj_h, ~, ~, adj_p]=fdr_bh(p,p_cutoff); - save_adj_p{s}=adj_p; - - %Save for later plotting across sessions - save_cohend{s}=cohend; - save_p{s}=adj_p; - - %sort columns in ascending order - [~, orderIdx] = sort(nanmean(cohend), 'ascend'); - cohend_sorted = cohend(:,orderIdx); - p_sorted = p(:,orderIdx); - - %Threshold using FDR_corrected p-value - cohend_thresh = adj_h.*cohend; cohend_thresh(cohend_thresh==0)=nan; - cohend_thresh_sorted = adj_h.*cohend_sorted; cohend_thresh_sorted(cohend_thresh_sorted==0)=nan; - - - %% Plot heatmaps - AxesLabels_sorted = behav_categ(orderIdx); - AxesLabels = behav_categ(1:end-1); - caxis_upper = 1.5; - caxis_lower = -1.5; - cmap=flipud(cbrewer('div','RdBu', length(caxis_lower:0.01:caxis_upper))); - - if plot_toggle - - %Plot ordered heatmap - figure; %set(gcf,'Position',[150 250 1000 500]); - [nanrow nancol]=find(~isnan(cohend_sorted)); nancol = unique(nancol); - order_units = [find(strcmp(brain_label,"TEO")), find(strcmp(brain_label,"vlPFC"))]; - %unit_lim = length(find(strcmp(brain_label,"TEO")))+1; yline(unit_lim); %plot the - %brain area limit - hp=heatmap(cohend_sorted(:,nancol), 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted(nancol); caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap']) - ax = gca; - ax.FontSize = 14; - %saveas(gcf, [savePath '/Cohend_heatmap_sorted.pdf']); close all - - %Plot ordered heatmap thresholded - figure; %set(gcf,'Position',[150 250 1000 500]); - hp=heatmap(cohend_thresh_sorted(:,nancol), 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted(nancol); caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); - title(['Cohens-d heatmap, p<' num2str(p_cutoff)]) - ax = gca; - ax.FontSize = 14; - %saveas(gcf, [savePath '/Cohend_heatmap_sorted_thresholded.pdf']); close all - - if select_behav==1; - % Plot ordered heatmap for desired behaviors - behav = [4,5,18,24]; - desired_beh = ismember(AxesLabels_sorted, behav_categ(behav)); - hp=heatmap(cohend_sorted(order_units,desired_beh), 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted(desired_beh); caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap']) - ax = gca; - ax.FontSize = 14; - saveas(gcf, [savePath '/Cohend_heatmap_sorted_Subject2Partner.pdf']); close all - - % Plot ordered heatmap thresholded for desired behaviors - desired_beh = ismember(AxesLabels_sorted, behav_categ(behav)); - hp=heatmap(cohend_thresh_sorted(order_units,desired_beh), 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted(desired_beh); caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap']) - ax = gca; - ax.FontSize = 14; - saveas(gcf, [savePath '/Cohend_heatmap_sorted_threshoolded_Subject2Partner.pdf']); close all - end - - %Includes both p-value and cohen d as thresholds -% figure; hold on; set(gcf,'Position',[150 250 1500 800]); -% subplot(2,2,1); hp=heatmap(cohend, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap') -% subplot(2,2,2); hp=heatmap(cohend_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) -% subplot(2,2,3); hp=heatmap(cohend_shuffle, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap SHUFFLED') -% subplot(2,2,4); hp=heatmap(cohend_shuffle_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap SHUFFLED, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) -% saveas(gcf, [savePath '/Cohend_heatmap_all_units.png']) - end - - %pause(2); - close all - - %%%%%%%%%%%%%%%%%%%%%%%%%%%% - %% Summary figures - - % % % % figure; scatter(nanmean(abs(cohend_thresh)), n_per_behav(s,:)); corrcoef(nanmean(abs(cohend_thresh)), n_per_behav(s,:),'rows','pairwise'); - % % % % figure; scatter(sum(~isnan(cohend_thresh)), n_per_behav(s,:)); corrcoef(sum(~isnan(cohend_thresh)), n_per_behav(s,:),'rows','pairwise'); - - mean_cohend_per_behav(s,:) = nanmean(cohend_thresh); - median_cohend_per_behav(s,:) = nanmedian(cohend_thresh); - std_cohend_per_behav(s,:) = nanstd(cohend_thresh); - se_cohend_per_behav(s,:) = nanstd(cohend_thresh)./sqrt(sum(~isnan(cohend_thresh))); - prop_selective_per_behav(s,:) = sum(~isnan(cohend_thresh))/n_neurons(s); - num_selective_behav_per_neuron{s} = sum(~isnan(cohend_thresh),2); - num_selective_behav_per_neuron_TEO{s} = sum(~isnan(cohend_thresh(find(strcmp(brain_label,"TEO")),:)),2); - num_selective_behav_per_neuron_vlPFC{s} = sum(~isnan(cohend_thresh(find(strcmp(brain_label,"vlPFC")),:)),2); - - if plot_toggle - - %Plot the distribution of effect sizes for each behavior - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_cohend_per_behav(s,:)); - boxchart(cohend_thresh(:,idx_sort)) - % scatter(1:length(idx_sort),mean_cohend_per_behav(idx_sort),60,'filled') - % errorbar(mean_cohend_per_behav(idx_sort), std_cohend_per_behav(idx_sort),'LineWidth',1.5) - % legend({'mean','standard deviation'},'Location','best') - %ylim([-2.5 2.5]); xlim([0 n_behav+1]) - ylabel(['Cohens-d, p<' num2str(p_cutoff)]) - yline(0,'LineStyle','--') - text(1,1.75,'Increased firing relative to baseline','FontSize',14) - text(1,-1.75,'Decreased firing relative to baseline','FontSize',14) - %xticks(1:n_behav) - xticklabels(AxesLabels(idx_sort)) - set(gca,'FontSize',15); - title('Distribution of effect size per behavior') -% saveas(gcf, [savePath '/Distribution_cohend_per_behavior.pdf']); %pause(2); close all - - %Plot the proprotion of selective neurons per behavior - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~,idx_sort]=sort(prop_selective_per_behav(s,:),'descend'); - scatter(1:n_behav,prop_selective_per_behav(s,idx_sort),60,'filled') - ylabel('Prop. selective units') - xticks(1:n_behav); xlim([0 n_behav+1]); ylim([0 1]) - xticklabels(AxesLabels(idx_sort)) - set(gca,'FontSize',15); - title(['Proportion of selective units per behavior']) -% saveas(gcf, [savePath '/Proportion_units_selective_per_behav.pdf']); %pause(2); close all - - % Variance in single neuron selectivity - mean_cohend_per_neuron = nanmean(cohend_thresh,2); - std_cohend_per_neuron = nanstd(cohend_thresh,0,2); - - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_cohend_per_neuron); - scatter(1:length(idx_sort),mean_cohend_per_neuron(idx_sort),20,'filled') - errorbar(mean_cohend_per_neuron(idx_sort), std_cohend_per_neuron(idx_sort),'LineWidth',1.5) - legend({'mean','standard deviation'},'Location','best') - ylim([-2 2]); xlim([0 n_neurons(s)+1]) - ylabel(['Cohens-d, p<' num2str(p_cutoff)]); xlabel('Units') - yline(0,'LineStyle','--') - text(10,1.5,'Increased firing relative to baseline','FontSize',14) - text(10,-1.5,'Decreased firing relative to baseline','FontSize',14) - set(gca,'FontSize',15); - title('Distribution of effect size across all units') -% saveas(gcf, [savePath '/Distribution_cohend_all_units.png']); pause(2); close all - - %Number of behaviors a single neuron is selective for - figure; hold on; histogram(multiunit_data) - histogram(num_selective_behav_per_neuron{s}) - legend({'Multi-unit','Well-isolated'}) - xlabel('Number of behavior a given neuron is selective to') - ylabel('Number of neurons') - title('Distribution of the number of behaviors single units are selective for') - set(gca,'FontSize',15); -% saveas(gcf, [savePath '/Distribution_number_selective_behavior_per_unit.png']); %pause(2); close all - - end - - % % %%%%%%%%%%%%%%%%%%%%%%%% - % % %Single neuron selectivity: - % % figure; set(gcf,'Position',[150 250 1000 300]); - % % data = cohend_thresh(1,:); - % % [~, idx_sorted]=sort(data); - % % scatter(1:n_behav, cohend_thresh(1,idx_sorted),40, 'filled') - % % ylim([caxis_lower caxis_upper]); xlim([0 n_behav+1]) - % % ylabel(['Cohens-d, p<' num2str(cutoff)]) - % % yline(0,'LineStyle','--') - % % xticks(1:n_behav) - % % xticklabels(AxesLabels(idx_sorted)) - % % set(gca,'FontSize',15); - - disp('Session done') - -end - -%% Results across sessions - -%Change savePath for all session results folder: -savePath = [home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SingleUnit_results/']; - -%Plot massive heatmap -all_sessions_data = cell2mat(save_cohend'); -[~, sortIdx]= sort(nanmedian(all_sessions_data,1)); -all_sessions_data_sorted = all_sessions_data(:,sortIdx); AxesLabels_sorted = AxesLabels(sortIdx); -[nanrow nancol]=find(~isnan(all_sessions_data_sorted)); nancol = unique(nancol); -figure; %set(gcf,'Position',[150 250 1000 500]); -hp=heatmap(all_sessions_data_sorted(:,nancol), 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted(nancol); caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap']) -ax = gca; -ax.FontSize = 14; -%saveas(gcf, [savePath '/Cohend_AllSessions.pdf']); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot proportion of selective units per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(prop_selective_per_behav)); -for s = a_sessions - prop_selective_per_behav(s,prop_selective_per_behav(s,:)==0)=nan; - scatter(1:length(idx_sort),prop_selective_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - prop_selective_per_behav(s,prop_selective_per_behav(s,:)==0)=nan; - scatter(1:length(idx_sort),prop_selective_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey H') -sgtitle('Proportion of selective units per behavior','FontSize',20) -%saveas(gcf, [savePath '/Proportion_selective_units_per_behavior.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot number of behaviors a single neuron is selective for across all sessions, separated by monkey - -figure -histogram(vertcat(num_selective_behav_per_neuron{:})) -length(find(vertcat(num_selective_behav_per_neuron{:})~=0))/sum(n_neurons) -length(find(vertcat(num_selective_behav_per_neuron{:})>1))/sum(n_neurons) -xlabel('Number of behaviors a given unit is selective for') -ylabel('Neuron count') -set(gca,'FontSize',15); -% saveas(gcf, [savePath '/Number_selective_behavior_per_unit.pdf']); - -figure; hold on -histogram(vertcat(num_selective_behav_per_neuron_TEO{:})) -histogram(vertcat(num_selective_behav_per_neuron_vlPFC{:})) -legend({'TEO','vlPFC'}) -xlabel('Number of behaviors a given unit is selective for') -ylabel('Neuron count') -set(gca,'FontSize',15); -xlim([0 12]) -% saveas(gcf, [savePath '/Number_selective_behavior_per_unit_byArea.pdf']); - -% % % -% % % figure; set(gcf,'Position',[150 250 1000 700]); -% % % subplot(2,1,1); hold on -% % % for s=a_sessions -% % % histogram(num_selective_behav_per_neuron{s}, 'FaceAlpha',0.3) -% % % prop_not_tuned(s) = length(find(num_selective_behav_per_neuron{s}==0))/n_neurons(s); -% % % prop_tuned_more_than_one_behav(s) = length(find(num_selective_behav_per_neuron{s}>0))/n_neurons(s); -% % % end -% % % legend({sessions(a_sessions).name},'Location','eastoutside') -% % % set(gca,'FontSize',15); -% % % title('Monkey A') -% % % -% % % subplot(2,1,2); hold on -% % % for s=h_sessions -% % % histogram(num_selective_behav_per_neuron{s}, 'FaceAlpha',0.3) -% % % prop_not_tuned(s) = length(find(num_selective_behav_per_neuron{s}==0))/n_neurons(s); -% % % prop_tuned_more_than_one_behav(s) = length(find(num_selective_behav_per_neuron{s}>0))/n_neurons(s); -% % % end -% % % legend({sessions(h_sessions).name},'Location','eastoutside') -% % % xlabel('Number of behaviors a given neuron is selective for') -% % % set(gca,'FontSize',15); -% % % title('Monkey H') -% % % -% % % sgtitle('Distribution of the number of behaviors single units are selective for','FontSize',20) -% % % %saveas(gcf, [savePath '/Number_selective_behavior_per_unit.png']); pause(2); close all -% % % -% % % mean(prop_not_tuned(prop_not_tuned>0)) -% % % mean(prop_tuned_more_than_one_behav(prop_tuned_more_than_one_behav>0)) \ No newline at end of file diff --git a/Single neuron/log_PSTH_vCircularShuffle.m b/Single neuron/log_PSTH_vCircularShuffle.m deleted file mode 100644 index 06bf81e..0000000 --- a/Single neuron/log_PSTH_vCircularShuffle.m +++ /dev/null @@ -1,344 +0,0 @@ -%% Log_PETH.m -% Produce peri-event time hitograms. -% C. Testard, July 2023 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range=[1:6,11:13,15:16,18]; -a_sessions = 1:6; h_sessions = [11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -threat_precedence =0; -exclude_sq=1; -plot_toggle=0; -shuffle = 0; -n_shuffle = 20; -time_pre = 10*temp_resolution; -time_post = 30*temp_resolution; - -%Set colormap -%uisetcolor([0.6 0.8 1]) -Cmap = [[1 0 0];...%Aggression; red - [1 0.4 0.1];...%Approach; dark orange - [0 0 0];...%But sniff; NA - [0.3 0.7 1];...%Drinking; light blue - [0 0.7 0];...%Foraging; dark green - [1 0 1];...%Groom sollicitation; magenta - [0 1 1];...%Groom partner; cyan - [0 0 1];...%Getting groomed; dark blue - [0.8 0 0];...%Threat to partner; dark red - [1 0 0];...%Threat to subject; red - [0.9 0.9 0];...%leave; dark yellow - [0 0 0];...%Lipsmack - [0.2 0.9 0.76];...%Masturbating; turquoise - [0.7 0 1];...%Mounting; light purple - [0.9 0.5 0];...%Other monkeys vocalize; orange - [1 0.8 0.1];...%Travel; yellow orange - [0 0 0];...%Proximity; NA - [0 0 0];...%Rowdy room; NA - [0 0 0];...%SP; NA - [0 0 0];...%SS; NA - [0.6314 0.5059 0.0118];...%Scratch; maroon - [0.5 0.2 0.5];...%Self-groom; dark purple - [ 1 0.07 0.65];...%Submission; dark pink - [0 0.4 0.5];...%Vocalzation; blue green - [0 0 0];...%Yawning; NA - [0.8 0.8 0.8]];%Rest; grey - -s=2; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/Example_units']; - - %% Load data - - % Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - Spike_rasters = Spike_rasters'; - Spike_rasters_zscore = zscore(Spike_rasters); - - %Extract behavior labels - behavior_labels = cell2mat({labels{:,3}}');%Get behavior label from labels structure - - - %% Adjust behavior log - - behavior_log_adj = behavior_log; - - %remove point behaviors (<5sec) - behavior_log_adj=behavior_log_adj(behavior_log_adj{:,'duration_round'}>=time_pre,:); - - %remove camera sync - behavior_log_adj(strcmp(behavior_log_adj{:,'Behavior'},'Camera Sync'),:)=[]; - - %pool aggression, HIS and HIP - behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'HIS'), "Behavior"} = repmat({'Aggression'},size(behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'HIS'), "Behavior"})); - behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'HIP'), "Behavior"} = repmat({'Aggression'},size(behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'HIP'), "Behavior"})); - - %Set proximity, RR, OMV and scratch to rest - behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'Proximity'), "Behavior"} = repmat({'Rest'},size(behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'Proximity'), "Behavior"})); - %behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'RR'), "Behavior"} = repmat({'Rest'},size(behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'RR'), "Behavior"})); - behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'Other monkeys vocalize'), "Behavior"} = repmat({'Rest'},size(behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'Other monkeys vocalize'), "Behavior"})); - %behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'Scratch'), "Behavior"} = repmat({'Rest'},size(behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'Scratch'), "Behavior"})); - - %Combine approach, leave and travel - behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'Pacing/Travel'), "Behavior"} = repmat({'Travel'},size(behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'Pacing/Travel'), "Behavior"})); - behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'Approach'), "Behavior"} = repmat({'Travel'},size(behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'Approach'), "Behavior"})); - behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'Leave'), "Behavior"} = repmat({'Travel'},size(behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'Leave'), "Behavior"})); - - %Do the same for vector-based behavior label - %Simplify behavioral categories - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - behavior_labels(behavior_labels==find(behav_categ=="Rowdy Room"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - behavior_labels(behavior_labels==find(behav_categ=="Yawning"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Get transition stats: - [counts, behav_states] = groupcounts(behavior_log_adj{:,'Behavior'}); - - transition_mat = nan(length(behav_states)); - for b = 1:length(behav_states) - for b_next = 1:length(behav_states) - - if b ~= b_next - idx = find(strcmp(behavior_log_adj{:,'Behavior'},behav_states(b))); - - if max(idx)==size(behavior_log_adj,1) - idx(end)=[]; - end - - transition_mat(b,b_next) = length(find(strcmp(behavior_log_adj{idx+1,'Behavior'},behav_states(b_next)))); - end - end - end - - % %Plot transition matrix - % hp=heatmap(transition_mat); - % hp.XDisplayLabels = behav_states; hp.YDisplayLabels = behav_states; - % ylabel('Preceding behavior') - % xlabel('Following behavior') - - freq_beh = nansum(transition_mat,2); - beh_list = behav_states;%(freq_beh>5); - beh_list=beh_list(~contains(beh_list,'Rest')); - beh_list_save{s} = beh_list; - - - %% Extract PETH align to event offset - - for beh = 1:length(beh_list) - - clear ibi - clear firing_rate - clear behavior - % clear response_idx - clear p_val - - %define index of behavior - beh_idx=find((strcmp(behavior_log_adj{:,'Behavior'},beh_list{beh})==1 & behavior_log_adj{:,'duration_round'}>time_pre)==1); - num_events(beh) = length(beh_idx); %num events - - %Since we look at the activity post-event, make sure behavior is not - %the last in behavioral log - if max(beh_idx)==size(behavior_log_adj,1) - beh_idx(end)=[]; - end - - %extract subsequent behavior - [~, idx_sorted]= sort(behavior_log_adj{beh_idx+1,'Behavior'}); - beh_idx_sorted = beh_idx(idx_sorted); - next_beh = behavior_log_adj{beh_idx_sorted+1,'Behavior'}; - - %Remove behaviors that occur too late in the session - beh_idx_sorted = beh_idx_sorted(behavior_log_adj{beh_idx_sorted,'end_time_round'}+time_postsize(Spike_rasters,1)); - for i=1:length(idx_to_fix) - behavior_log_adj_shuffle{idx_to_fix(i),'end_time_round'} = behavior_log_adj_shuffle{idx_to_fix(i),'end_time_round'} -size(Spike_rasters,1); - end - - %define index of behavior - beh_idx=find((strcmp(behavior_log_adj_shuffle{:,'Behavior'},beh_list{beh})==1 & behavior_log_adj_shuffle{:,'duration_round'}>time_pre)==1); - - - for i=1:length(beh_idx_sorted) - offset = behavior_log_adj_shuffle{beh_idx(i),'end_time_round'}; - if offset>size(Spike_rasters,1)-time_post - offset = size(Spike_rasters,1)-time_post-1; - elseif offset<=time_pre - offset = time_pre+1; - end - firing_rate_shuffle(i,:) = Spike_rasters([offset-time_pre:offset, offset+1:offset+time_post],n); - end - - %Get response index based on mean activity - pre= mean(mean(firing_rate_shuffle(:,1:time_pre))); - post=mean(mean(firing_rate_shuffle(:,time_pre+1:time_pre+time_pre))); - response_idx_shuffle(shuffle_i)=(post-pre)/(pre+post); - - end - - %Get pvalue based on shuffling - pval(n,beh)= length(find(abs(response_idx_shuffle)>abs(response_idx{s}(n,beh))))./n_shuffle; - - if pval(n,beh)<0.05 - response_idx_thresholded{s}(n,beh)=response_idx{s}(n,beh); - else - response_idx_thresholded{s}(n,beh)=nan; - end - - disp(num2str(n)) - - end %end of neuron loop - - disp(beh_list{beh}) - - end %end of behavior loop -% % -% % %correct for multiple comparisons -% % alpha=0.01; -% % [corrected_p{s}, h]=bonf_holm(p_val,alpha); -% % length(find(corrected_p{s}block_times{3,"start_time"} - behavior_log{b,'Block'}=3; - else - behavior_log{b,'Block'}=2; - end - - end - - %pool aggression, HIS and HIP - behavior_log{strcmp(behavior_log{:,'Behavior'},'HIS'), "Behavior"} = repmat({'Aggression'},size(behavior_log{strcmp(behavior_log{:,'Behavior'},'HIS'), "Behavior"})); - behavior_log{strcmp(behavior_log{:,'Behavior'},'HIP'), "Behavior"} = repmat({'Aggression'},size(behavior_log{strcmp(behavior_log{:,'Behavior'},'HIP'), "Behavior"})); - - %Set proximity, RR, OMV and scratch to rest - behavior_log{strcmp(behavior_log{:,'Behavior'},'Proximity'), "Behavior"} = repmat({'Rest'},size(behavior_log{strcmp(behavior_log{:,'Behavior'},'Proximity'), "Behavior"})); - behavior_log{strcmp(behavior_log{:,'Behavior'},'RR'), "Behavior"} = repmat({'Rest'},size(behavior_log{strcmp(behavior_log{:,'Behavior'},'RR'), "Behavior"})); - behavior_log{strcmp(behavior_log{:,'Behavior'},'Other monkeys vocalize'), "Behavior"} = repmat({'Rest'},size(behavior_log{strcmp(behavior_log{:,'Behavior'},'Other monkeys vocalize'), "Behavior"})); - - %Find behavior of interest - beh_list = {'Groom Give','Groom Receive','Self-groom','Foraging','Aggression','Rest'}; - beh=6; - beh_idx=find((strcmp(behavior_log{:,'Behavior'},beh_list{beh})==1 & behavior_log{:,'duration_round'}>10*temp_resolution)==1); - length(beh_idx) %num events - - - %Plot firing rate for each context - - %Note: Neurons #266, 96, 2 show stronger response when alone. - - - %inialize color scheme - colorscheme={[0 0.4470 0.7410], [0.9290 0.6940 0.1250], [0.6350 0.0780 0.1840]}; - time_post_onset = 30*temp_resolution; - - for n=randi(size(Spike_rasters,2),1,20)%1:size(Spike_rasters,2) - - %initialize firing rate matrix - firing_rate=nan(length(beh_idx),max(behavior_log{beh_idx,'duration_round'})+1); - - for i=1:length(beh_idx) - onset=behavior_log{beh_idx(i),'start_time'}; - offset=behavior_log{beh_idx(i),'end_time'}; - length_idx(i)=length(onset:offset); - firing_rate(i,1:length_idx(i)) = Spike_rasters_zscore(onset:offset,n); - - %plot(firing_rate(i,:),'Color',colorscheme{behavior_log{beh_idx(i),'Block'}}) - - end - firing_rate_block1 = firing_rate(behavior_log{beh_idx,'Block'}==1,:); - firing_rate_block2 = firing_rate(behavior_log{beh_idx,'Block'}==2,:); - firing_rate_block3 = firing_rate(behavior_log{beh_idx,'Block'}==3,:); - - figure; hold on - y = nanmean(firing_rate_block1(:,1:time_post_onset)); - x = 1:numel(y); - sem_dev=nanstd(firing_rate_block1(:,1:time_post_onset))./sqrt(size(firing_rate_block1,1)); - curve1 = y+sem_dev; - curve2 = y-sem_dev; - x2 = [x, fliplr(x)]; - inBetween = [curve1, fliplr(curve2)]; - fill(x2, inBetween, [0.9 0.9 0.9]); - plot(x,y,'LineWidth',2,'Color', colorscheme{1}) - - y = nanmean(firing_rate_block2(:,1:time_post_onset)); - x = 1:numel(y); - sem_dev=nanstd(firing_rate_block2(:,1:time_post_onset))./sqrt(size(firing_rate_block2,1)); - curve1 = y+sem_dev; - curve2 = y-sem_dev; - x2 = [x, fliplr(x)]; - inBetween = [curve1, fliplr(curve2)]; - fill(x2, inBetween, [0.9 0.9 0.9]); - plot(x,y,'LineWidth',2,'Color', colorscheme{2}) - - y = nanmean(firing_rate_block3(:,1:time_post_onset)); - x = 1:numel(y); - sem_dev=nanstd(firing_rate_block3(:,1:time_post_onset))./sqrt(size(firing_rate_block3,1)); - curve1 = y+sem_dev; - curve2 = y-sem_dev; - x2 = [x, fliplr(x)]; - inBetween = [curve1, fliplr(curve2)]; - fill(x2, inBetween, [0.9 0.9 0.9]); - plot(x,y,'LineWidth',2,'Color', colorscheme{3}) - - xlabel('seconds') - ylabel('Z-scored Hz') - title(['Unit #' num2str(n)]) - - - % figure; - % [~, sorted_block1]=sort(length_idx(behavior_log{beh_idx,'Block'}==1)); - % [~, sorted_block2]=sort(length_idx(behavior_log{beh_idx,'Block'}==2)); - % heatmap([firing_rate_block1(sorted_block1,:);... - % firing_rate_block2(sorted_block2,:)]); colormap cool; colorbar - % % % imagesc(firing_rate(sorted,:)); colormap([0 0 0; cool(200)]); colorbar - % % % yline(find(diff(behavior_log{beh_idx,'Block'})==1)+0.5,'LineWidth',5) - - end - - - -end \ No newline at end of file diff --git a/Single neuron/log_glm_behav_vs_context_maxAcrossRegressors.m b/Single neuron/log_glm_behav_vs_context_maxAcrossRegressors.m deleted file mode 100644 index 8bd8bdc..0000000 --- a/Single neuron/log_glm_behav_vs_context_maxAcrossRegressors.m +++ /dev/null @@ -1,214 +0,0 @@ -%Extract mean and STD behaviors and neurons - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -threat_precedence =1; -exclude_sq=1; -plot_toggle=0; -warning('off','all') - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/Example_units']; - - %% Load data - - %% Get data with specified temporal resolution and channels - - %Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - Spike_count_raster = zscore(Spike_rasters'); %Z-score firing rate data - - %Extract behavior labels - behavior_labels= cell2mat({labels{:,3}}');%Get behavior label from labels structure - context = cell2mat({labels{:,12}}'); context_categ={"female","male","alone"}; - paired_or_not = cell2mat({labels{:,13}}'); - - %% Select behaviors - - %Extract behavior labels and frequency - behavior_labels = cell2mat({labels{:,3}}'); - - %Extract block labels - block_labels = cell2mat({labels{:,12}}'); - block_categ={"F","M","Alone"}; - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - %Remove behaviors we're not interested - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding Rowdy Room which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize'))); - - % OR select behaviors manually - %behav =[29] ;%unique(behavior_labels); %[4,5,7,8,9,10,24];% [4:10, 23]; %[4:8,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only consider indices with behavior of interest - idx= find(ismember(behavior_labels,behav));% & ismember(block_labels,3)); - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_labels_final = block_labels(idx); - - %% Create predictor matrix - %Which contains all behaviors which fit the includion criteria (min30sec in the session) and contexts - - behavior_final = dummyvar(categorical(behavior_labels_final)); %behavior_final = behavior_final(:,[7,8,9]); - context_final = dummyvar(categorical(block_labels_final));%Same as above but in behavior labels - all_predictors=[behavior_final(:,1:size(behavior_final,2)-1), context_final(:,[1,3])]; - - - %% Run linear models for each unit - for unit = 1:size(Spike_count_raster,2) - - NeuralResponse = Spike_count_raster_final(:,unit);%Only keep timepoints where the behaviors of interest occur in spiking data - - %Full fit - mdl =fitlm(all_predictors,NeuralResponse); - Rsq_full{s}(unit)=mdl.Rsquared.Ordinary; - % RsqAdj{s}(unit,1)=mdl.Rsquared.Adjusted; - - %Rsq for each regressors - for reg = 1:size(all_predictors,2) - - %initialize predictor matrix - pred_mat = all_predictors(:,reg); - -% %Shuffle all predictors EXCEPT predictor of interest -% shuffled_reg = ~ismember(1:size(all_predictors,2), reg); -% pred_mat(:, shuffled_reg) = all_predictors(randsample(size(behavior_final,1),size(behavior_final,1)), shuffled_reg); - - %Fit model - mdl =fitlm(pred_mat,NeuralResponse); - Rsq{s}(unit,reg)=mdl.Rsquared.Ordinary; - end - - %Extract most predictive behavior regressor - Rsq_full{s}(unit,2) = max(Rsq{s}(unit,1:size(behavior_final,2)-1)); - - %Extract most predictive context regressor - Rsq_full{s}(unit,3) = max(Rsq{s}(unit,end-1:end)); - - end %end of units - - %Separate the two brain areas - Rsq_teo{s} = Rsq_full{s}(strcmp(brain_label,'TEO'),:); - Rsq_vlpfc{s} = Rsq_full{s}(strcmp(brain_label,'vlPFC'),:); - - disp(s) - disp('done.') - -end %end of session - -%% Save results -cd(['~/Dropbox (Penn)/Datalogger/Results/All_sessions/SingleUnit_results/']); -save('Glm_context_behav_maxAcrossReg.mat', "Rsq_full","Rsq_teo","Rsq_vlpfc","a_sessions","h_sessions","behav_categ") -load('Glm_context_behav_maxAcrossReg.mat') - -%% Plot output -Rsq_all_neurons = cat(1,Rsq_full{:}); -Rsq_teo_neurons = cat(1,Rsq_teo{:}); -Rsq_vlpfc_neurons = cat(1,Rsq_vlpfc{:}); - -%Pooled monkeys -limits = [0 0.5]; - -figure; -subplot(1,2,1); hold on -scatter(Rsq_teo_neurons(:,3), Rsq_teo_neurons(:,2),'filled','r') -plot(limits,limits,'k','DisplayName','Diagonal'); -ylabel('Behavior') -xlabel('Context'); title('TEO') -xlim(limits); ylim(limits) -grid on - -subplot(1,2,2); hold on -scatter(Rsq_vlpfc_neurons(:,3), Rsq_vlpfc_neurons(:,2),'filled','b') -plot(limits,limits,'k','DisplayName','Diagonal'); -ylabel('Behavior') -xlabel('Context'); title('vlPFC') -xlim(limits); ylim(limits) -grid on - -%Separated by monkey -Rsq_teo_neurons_amos = cat(1,Rsq_teo{a_sessions}); -Rsq_vlpfc_neurons_amos = cat(1,Rsq_vlpfc{a_sessions}); -Rsq_teo_neurons_hooke = cat(1,Rsq_teo{h_sessions}); -Rsq_vlpfc_neurons_hooke = cat(1,Rsq_vlpfc{h_sessions}); - -figure; -subplot(1,2,1); hold on -scatter(Rsq_teo_neurons_amos(:,3), Rsq_teo_neurons_amos(:,2),'filled','r') -scatter(Rsq_vlpfc_neurons_hooke(:,3), Rsq_vlpfc_neurons_hooke(:,2),'filled','b') -plot(limits,limits,'k','DisplayName','Diagonal'); -ylabel('Behavior') -xlabel('Context') -xlim(limits); ylim(limits) -title('TEO') -grid on -ax = gca; -ax.FontSize = 16; - -subplot(1,2,2); hold on -scatter(Rsq_vlpfc_neurons_hooke(:,3), Rsq_vlpfc_neurons_hooke(:,2),'filled','r') -scatter(Rsq_vlpfc_neurons_amos(:,3), Rsq_vlpfc_neurons_amos(:,2),'filled','b') -legend({'Hooke','Amos'}, 'Location','best') -plot(limits,limits,'k','DisplayName','Diagonal'); -ylabel('Behavior') -xlabel('Context') -xlim(limits); ylim(limits) -title('vlPFC') -grid on -ax = gca; -ax.FontSize = 16; \ No newline at end of file diff --git a/Single neuron/old_code/.DS_Store b/Single neuron/old_code/.DS_Store deleted file mode 100644 index 5008ddf..0000000 Binary files a/Single neuron/old_code/.DS_Store and /dev/null differ diff --git a/Single neuron/old_code/NumUnitsPlot.R b/Single neuron/old_code/NumUnitsPlot.R deleted file mode 100644 index 09b911d..0000000 --- a/Single neuron/old_code/NumUnitsPlot.R +++ /dev/null @@ -1,26 +0,0 @@ - - -library(ggplot2) -library(lubridate) - -setwd('~/Dropbox (Penn)/Datalogger/Results/All_sessions/Number of units/') -data=read.csv('Session_log_num_units_final.csv') -data$monkey=as.factor(data$monkey) -#data$date=ymd(data$date) - -setwd(paste('~/Dropbox (Penn)/Datalogger/Results/')) -ggplot(data = data, aes(x=date, y=num.units, color = monkey))+ - geom_point(size = 3)+ theme_classic(base_size=16)+ - theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))+ - ylim(200, 350)+ylab('Number of units') -ggsave('Number of units for all sessions.pdf') - -setwd(paste('~/Dropbox (Penn)/Datalogger/Results/')) -ggplot(data = data, aes(x=date, y=num.units.TEO))+ - geom_point(size = 3, color='red')+ - geom_point(aes(x=date, y=num.units.vlPFC), size = 3, color='blue')+ - theme_classic(base_size=16)+ - facet_wrap(~monkey)+ - theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))+ - ylim(100, 200)+ylab('Number of units') -ggsave('Number of units split by area.pdf') diff --git a/Single neuron/old_code/log_Correlation.asv b/Single neuron/old_code/log_Correlation.asv deleted file mode 100644 index d442b9d..0000000 --- a/Single neuron/old_code/log_Correlation.asv +++ /dev/null @@ -1,136 +0,0 @@ -%% Log_FiringRatePerBehavior -% This script generates firing rate of individual neuron under different -% behavioral conditions (considering individual secons as independent). - -% Load data -%Set path -is_mac = 1; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -clearvars -except savePath filePath is_mac - -%% Set paramaters and load data - -%Set parameters -temp_resolution = 1; -channel_flag = "all"; -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; - -%Get data with specified temporal resolution and channels -%[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final]= log_GenerateDataToRes_function_basic(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); -[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - -session_length = size(Spike_rasters,2); % get session length - -%Extract behavior labels -behavior_labels = cell2mat({labels{:,3}}');%Get behavior label from labels structure -behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - -%Set parameters -unqLabels = 1:max(behavior_labels); %Get unique behavior labels (exclude rest) -n_neurons = size(Spike_rasters,1); %Get number of neurons -n_behav = length(unqLabels); %Get number of unique behavior labels - -%Estimate "baseline" neural firing distribution. -idx_rest=find(behavior_labels==length(behav_categ));%Get idx of "rest" epochs. -mean_baseline = mean(Spike_rasters(:,idx_rest),2); -std_baseline = std(Spike_rasters(:,idx_rest),0,2); - -% %Check visually that baseline is taken from epochs throughout the session -% y=zeros(1, session_length); y(idx_rest)=1; -% figure; plot(1:session_length, y); ylim([-0.5, 1.5]) -% yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) -% xlabel('Time in s'); title('Baseline epochs') - -%% Compute cohen's d - -n_per_behav = nan(1,n_behav); -cohens_d = nan(1,n_behav); -mean_corr = nan(1,n_behav); -mean_corr_pos = nan(1,n_behav); -mean_corr_neg = nan(1,n_behav); -resp_mat = cell(1,n_behav); -resp_mat_rand = cell(1,n_behav); - -for b = 1:n_behav - idx = find(behavior_labels == unqLabels(b)); %get idx where behavior b occurred - n_per_behav(b)=length(idx); - - %if n_per_behav(b)>10 - - if length(idx)0),[],1)); - cohens_d(b) = computeCohen_d(reshape(correl_matrix,[],1), reshape(correl_matrix_rand,[],1)); - - %end - -end - - -figure; set(gcf,'Position',[150 250 1000 500]); -[~, idx_sort]=sort(mean_corr); -scatter(1:length(mean_corr_neg), mean_corr(idx_sort), 'filled') -xlim([0 length(mean_corr_neg)+1]); -ylabel('Mean pairwise correlation'); title('Mean pairwise correlation across different behaviors') -xticks(1:length(mean_corr_neg)); xticklabels(behav_categ(idx_sort)) -set(gca,'FontSize',15); - -figure; set(gcf,'Position',[150 250 1000 500]); -[~, idx_sort]=sort(mean_corr_neg); -scatter(1:length(mean_corr_neg), mean_corr_neg(idx_sort), 'filled') -xlim([0 length(mean_corr_neg)+1]); -ylabel('Mean pairwise neg. correlation'); title('Mean pairwise NEGATIVE correlation across different behaviors') -xticks(1:length(mean_corr_neg)); xticklabels(behav_categ(idx_sort)) -set(gca,'FontSize',15); -saveas(gcf, [savePath '/Mean_pairwise_neg_correl_per_behav.png']); close all - -figure; set(gcf,'Position',[150 250 1000 500]); -[~, idx_sort]=sort(mean_corr_pos); -scatter(1:length(mean_corr_pos), mean_corr_pos(idx_sort), 'filled') -xlim([0 length(mean_corr_pos)+1]); -ylabel('Mean pairwise pos. correlation'); title('Mean pairwise POSITIVE correlation across different behaviors') -xticks(1:length(mean_corr_pos)); xticklabels(behav_categ(idx_sort)) -set(gca,'FontSize',15); -saveas(gcf, [savePath '/Mean_pairwise_neg_correl_per_behav.png']); close all - -figure; set(gcf,'Position',[150 250 1000 500]); -[~, idx_sort]=sort(cohens_d); -scatter(1:length(cohens_d), cohens_d(idx_sort), 'filled') -yline(0,'LineStyle','--') -text(15,0.05,'Increased pairwise correlation relative to baseline','FontSize',14) -text(15,-0.05,'Decreased pairwise correlation to baseline','FontSize',14) -xlim([0 length(cohens_d)+1]); ylim([-0.5 0.5]) -ylabel('Effect size'); title('Difference in distribution of pairwise correlation between behavior and baseline') -xticks(1:length(cohens_d)); xticklabels(behav_categ(idx_sort)) -set(gca,'FontSize',15); -%saveas(gcf, [savePath '/Distribution_difference_pairwise_correl_relative2baseline.png']); close all - diff --git a/Single neuron/old_code/log_Correlation.m b/Single neuron/old_code/log_Correlation.m deleted file mode 100644 index e387c73..0000000 --- a/Single neuron/old_code/log_Correlation.m +++ /dev/null @@ -1,136 +0,0 @@ -%% Log_FiringRatePerBehavior -% This script generates firing rate of individual neuron under different -% behavioral conditions (considering individual secons as independent). - -% Load data -%Set path -is_mac = 1; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -clearvars -except savePath filePath is_mac - -%% Set paramaters and load data - -%Set parameters -temp_resolution = 1; -channel_flag = "all"; -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; - -%Get data with specified temporal resolution and channels -%[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final]= log_GenerateDataToRes_function_basic(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); -[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - -session_length = size(Spike_rasters,2); % get session length - -%Extract behavior labels -behavior_labels = cell2mat({labels{:,3}}');%Get behavior label from labels structure -behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - -%Set parameters -unqLabels = 1:max(behavior_labels); %Get unique behavior labels (exclude rest) -n_neurons = size(Spike_rasters,1); %Get number of neurons -n_behav = length(unqLabels); %Get number of unique behavior labels - -%Estimate "baseline" neural firing distribution. -idx_rest=find(behavior_labels==length(behav_categ));%Get idx of "rest" epochs. -mean_baseline = mean(Spike_rasters(:,idx_rest),2); -std_baseline = std(Spike_rasters(:,idx_rest),0,2); - -% %Check visually that baseline is taken from epochs throughout the session -% y=zeros(1, session_length); y(idx_rest)=1; -% figure; plot(1:session_length, y); ylim([-0.5, 1.5]) -% yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) -% xlabel('Time in s'); title('Baseline epochs') - -%% Compute cohen's d - -n_per_behav = nan(1,n_behav); -cohens_d = nan(1,n_behav); -mean_corr = nan(1,n_behav); -mean_corr_pos = nan(1,n_behav); -mean_corr_neg = nan(1,n_behav); -resp_mat = cell(1,n_behav); -resp_mat_rand = cell(1,n_behav); - -for b = 1:n_behav - idx = find(behavior_labels == unqLabels(b)); %get idx where behavior b occurred - n_per_behav(b)=length(idx); - - %if n_per_behav(b)>10 - - if length(idx)0),[],1)); - cohens_d(b) = computeCohen_d(reshape(correl_matrix,[],1), reshape(correl_matrix_rand,[],1)); - - %end - -end - - -figure; set(gcf,'Position',[150 250 1000 500]); -[~, idx_sort]=sort(mean_corr); -scatter(1:length(mean_corr_neg), mean_corr(idx_sort), 'filled') -xlim([0 length(mean_corr_neg)+1]); -ylabel('Mean pairwise correlation'); title('Mean pairwise correlation across different behaviors') -xticks(1:length(mean_corr_neg)); xticklabels(behav_categ(idx_sort)) -set(gca,'FontSize',15); - -figure; set(gcf,'Position',[150 250 1000 500]); -[~, idx_sort]=sort(mean_corr_neg); -scatter(1:length(mean_corr_neg), mean_corr_neg(idx_sort), 'filled') -xlim([0 length(mean_corr_neg)+1]); -ylabel('Mean pairwise neg. correlation'); title('Mean pairwise NEGATIVE correlation across different behaviors') -xticks(1:length(mean_corr_neg)); xticklabels(behav_categ(idx_sort)) -set(gca,'FontSize',15); -saveas(gcf, [savePath '/Mean_pairwise_neg_correl_per_behav.png']); close all - -figure; set(gcf,'Position',[150 250 1000 500]); -[~, idx_sort]=sort(mean_corr_pos); -scatter(1:length(mean_corr_pos), mean_corr_pos(idx_sort), 'filled') -xlim([0 length(mean_corr_pos)+1]); -ylabel('Mean pairwise pos. correlation'); title('Mean pairwise POSITIVE correlation across different behaviors') -xticks(1:length(mean_corr_pos)); xticklabels(behav_categ(idx_sort)) -set(gca,'FontSize',15); -saveas(gcf, [savePath '/Mean_pairwise_pos_correl_per_behav.png']); close all - -figure; set(gcf,'Position',[150 250 1000 500]); -[~, idx_sort]=sort(cohens_d); -scatter(1:length(cohens_d), cohens_d(idx_sort), 'filled') -yline(0,'LineStyle','--') -text(15,0.05,'Increased pairwise correlation relative to baseline','FontSize',14) -text(15,-0.05,'Decreased pairwise correlation to baseline','FontSize',14) -xlim([0 length(cohens_d)+1]); ylim([-0.5 0.5]) -ylabel('Effect size'); title('Difference in distribution of pairwise correlation between behavior and baseline') -xticks(1:length(cohens_d)); xticklabels(behav_categ(idx_sort)) -set(gca,'FontSize',15); -%saveas(gcf, [savePath '/Distribution_difference_pairwise_correl_relative2baseline.png']); close all - diff --git a/Single neuron/old_code/log_Correlation_batch.m b/Single neuron/old_code/log_Correlation_batch.m deleted file mode 100644 index 16f5d86..0000000 --- a/Single neuron/old_code/log_Correlation_batch.m +++ /dev/null @@ -1,269 +0,0 @@ -%% Log_Correlation_batch -% This script computes pairwise correlations of neuron under different -% behavioral conditions. - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:18]; -session_range_with_partner=[1:3,11:13]; - - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1;%set the smoothing window size (sigma) -simplify = 0; - -%Initialize session batch variables: -n_behav=28; -n_per_behav = nan(length(sessions),n_behav); -cohens_d = nan(length(sessions),n_behav); -mean_corr = nan(length(sessions),n_behav); -mean_corr_pos = nan(length(sessions),n_behav); -mean_corr_neg = nan(length(sessions),n_behav); - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/']; - - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey,... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, ... - with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - session_length = size(Spike_rasters,2); % get session length - Spike_rasters = zscore(Spike_rasters,0,2); - - %Get correlation between units - correl_across_units = corrcoef(Spike_rasters'); -% caxis_upper = 1; -% caxis_lower = -1; -% cmap=flipud(cbrewer('div','RdBu', length(caxis_lower:0.01:caxis_upper))); -% heatmap(tril(correl_across_units,-1),'Colormap',cmap);caxis([caxis_lower caxis_upper]); -% plot(range(tril(correl_across_units,-1))) - - %Extract behavior labels - behavior_labels = cell2mat({labels{:,3}}');%Get behavior label from labels structure - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Threat to partner"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Threat to subject"); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - if simplify - %Simplify behavioral catagories - %Lump all aggressive interactions together - behavior_labels(behavior_labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Aggression"); - behavior_labels(behavior_labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Lump Drinking and foraging - behavior_labels(behavior_labels==find(behav_categ=="Drinking"))=find(behav_categ=="Foraging"); - - %Lump all grooming together - behavior_labels(behavior_labels==find(behav_categ=="Getting groomed"))=find(behav_categ=="Groom partner"); - % behavior_labels(behavior_labels==find(behav_categ=="Groom sollicitation"))=find(behav_categ=="Groom partner"); - % behavior_labels(behavior_labels==find(behav_categ=="Self-groom"))=find(behav_categ=="Groom partner"); - - unqLabels = [1,5,7,29]; - else - unqLabels = 1:max(behavior_labels); %Get unique behavior labels (exclude rest) - end - - %Set parameters - n_neurons = size(Spike_rasters,1); %Get number of neurons - n_behav = length(unqLabels); %Get number of unique behavior labels - - %Estimate "baseline" neural firing distribution. - idx_rest=find(behavior_labels==length(behav_categ));%Get idx of "rest" epochs. - mean_baseline = mean(Spike_rasters(:,idx_rest),2); - std_baseline = std(Spike_rasters(:,idx_rest),0,2); - - % %Check visually that baseline is taken from epochs throughout the session - % y=zeros(1, session_length); y(idx_rest)=1; - % figure; plot(1:session_length, y); ylim([-0.5, 1.5]) - % yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) - % xlabel('Time in s'); title('Baseline epochs') - - %% Compute pairwise correlations - - for b = 1:n_behav - idx = find(behavior_labels == unqLabels(b)); %get idx where behavior b occurred - n_per_behav(b)=length(idx); - - if n_per_behav(b)>10 - - if length(idx)0)); - cohens_d(s,b) = computeCohen_d(reshape(squeeze(correl_matrix(b,:,:)),[],1), reshape(correl_matrix_rand,[],1)); - - end - - end - - %Plot distribution of correlation for each unit, separated by - %behavioral category -% % units = randsample(size(Spike_rasters,1),10); -% % for n1 = 1:length(units) -% % figure(units(n1)); hold on; -% % errorbar(nanmean(correl_matrix(:,units(n1),:),3), nanstd(correl_matrix(:,units(n1),:),[],3),'s','MarkerSize',10) -% % scatter(1:4,nanmean(correl_matrix(:,units(n1),:),3),40,'r','filled') -% % yline(0,'LineStyle','--') -% % xticks(1:4); xticklabels({'Aggression','Foraging','Groom','Rest'}) -% % xlim([0 5]); ylabel('Pairwise correlation'); title(['Unit: ' num2str(units(n1))]) -% % end - - - figure; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_corr(s,:)); - scatter(1:length(mean_corr(s,:)), mean_corr(s,idx_sort), 'filled') - xlim([0 length(mean_corr(s,:))+1]); - ylabel('Mean pairwise correlation'); title('Mean pairwise correlation across different behaviors') - xticks(1:length(mean_corr_neg(s,:))); xticklabels(behav_categ(idx_sort)) - set(gca,'FontSize',15); - saveas(gcf, [savePath 'Pairwise_corr/Mean_pairwise_neg_correl_per_behav.png']); close all - - figure; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_corr_neg(s,:)); - scatter(1:length(mean_corr_neg(s,:)), mean_corr_neg(s,idx_sort), 'filled') - xlim([0 length(mean_corr_neg(s,:))+1]); - ylabel('Mean pairwise neg. correlation'); title('Mean pairwise NEGATIVE correlation across different behaviors') - xticks(1:length(mean_corr_neg(s,:))); xticklabels(behav_categ(idx_sort)) - set(gca,'FontSize',15); - saveas(gcf, [savePath 'Pairwise_corr/Mean_pairwise_neg_correl_per_behav.png']); close all - - figure; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_corr_pos(s,:)); - scatter(1:length(mean_corr_pos(s,:)), mean_corr_pos(s,idx_sort), 'filled') - xlim([0 length(mean_corr_pos(s,:))+1]); - ylabel('Mean pairwise pos. correlation'); title('Mean pairwise POSITIVE correlation across different behaviors') - xticks(1:length(mean_corr_pos(s,:))); xticklabels(behav_categ(idx_sort)) - set(gca,'FontSize',15); - saveas(gcf, [savePath 'Pairwise_corr/Mean_pairwise_pos_correl_per_behav.png']); close all - - figure; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(cohens_d(s,:)); - scatter(1:length(cohens_d(s,:)), cohens_d(s,idx_sort), 'filled') - yline(0,'LineStyle','--') - text(1,0.05,'Increased pairwise correlation relative to baseline','FontSize',14) - text(1,-0.05,'Decreased pairwise correlation to baseline','FontSize',14) - xlim([0 length(cohens_d(s,:))+1]); ylim([-0.5 0.5]) - ylabel('Effect size'); title('Difference in distribution of pairwise correlation between behavior and baseline') - xticks(1:length(cohens_d(s,:))); xticklabels(behav_categ(idx_sort)) - set(gca,'FontSize',15); - saveas(gcf, [savePath 'Pairwise_corr/Distribution_difference_pairwise_correl_relative2baseline.png']); close all - -end - -%Change savePath for all session results folder: -savePath = [home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SingleUnit_results/']; - -%Plot positive pairwise correlation per behavior, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(mean_corr_pos)); -for s = a_sessions - scatter(1:length(idx_sort),mean_corr_pos(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([0 0.6]); xlim([0 n_behav+1]) -ylabel(['Mean pairwise correlation']) -xticks(1:n_behav) -xticklabels(behav_categ(idx_sort)) -set(gca,'FontSize',15); -title('Pairwise POSITIVE correlation per behavior, Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - scatter(1:length(idx_sort),mean_corr_pos(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([0 0.6]); xlim([0 n_behav+1]) -ylabel(['Mean pairwise correlation']) -xticks(1:n_behav) -xticklabels(behav_categ(idx_sort)) -set(gca,'FontSize',15); -title('Pairwise POSITIVE correlation per behavior, Monkey H') -saveas(gcf, [savePath '/Pairwise_pos_corr_per_behavior.png']); close all - -%Plot negative pairwise correlation per behavior, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(mean_corr_neg)); -for s = a_sessions - scatter(1:length(idx_sort),mean_corr_neg(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([-0.6 0]); xlim([0 n_behav+1]) -ylabel(['Mean pairwise correlation']) -xticks(1:n_behav) -xticklabels(behav_categ(idx_sort)) -set(gca,'FontSize',15); -title('Pairwise NEGATIVE correlation per behavior, Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - scatter(1:length(idx_sort),mean_corr_neg(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([-0.6 0]); xlim([0 n_behav+1]) -ylabel(['Mean pairwise correlation']) -xticks(1:n_behav) -xticklabels(behav_categ(idx_sort)) -set(gca,'FontSize',15); -title('Pairwise NEGATIVE correlation per behavior, Monkey H') -saveas(gcf, [savePath '/Pairwise_neg_corr_per_behavior.png']); close all - diff --git a/Single neuron/old_code/log_FiringRatePerBehavior.asv b/Single neuron/old_code/log_FiringRatePerBehavior.asv deleted file mode 100644 index d0fec1e..0000000 --- a/Single neuron/old_code/log_FiringRatePerBehavior.asv +++ /dev/null @@ -1,221 +0,0 @@ -%% Log_FiringRatePerBehavior -% This script generates firing rate of individual neuron under different -% behavioral conditions (considering individual secons as independent). - -% Load data -%Set path -is_mac = 1; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -clearvars -except savePath filePath is_mac - -%Set parameters -temp_resolution = 1; -channel_flag = "all"; -with_NC =1; isolatedOnly=1; - -%Get data with specified temporal resolution and channels -[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - -session_length = size(Spike_rasters,2); % get session length - -%Gt behavior label -behavior_labels = cell2mat({labels{:,3}}');%Get behavior label -behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (mark as "undefined"). - -%Get firing rate during a baseline time (when monkey doesn't engage in a -%particular behavior) -idx_rest=find(behavior_labels==length(behav_categ)); -baseline_firing = mean(Spike_rasters_final(:,idx_rest),2); - -%eliminate units with very low firing rate -mean_firing_rate = mean(Spike_rasters,2); -min_firing_units = find(mean_firing_rate>0); -Spike_rasters_final = Spike_rasters(min_firing_units,:) ; - -%Standerdize Unit rasters -Spike_raster_zscore = zscore(Spike_rasters_final,0, 2); %Z-score -Spike_raster_meandivided = Spike_rasters_final./mean_firing_rate; %Divide by the mean -Spike_raster_relative2baseline = Spike_rasters_final./baseline_firing; %Divide by baseline firing - -%Set parameters -unqLabels = 1:max(behavior_labels)-1; %Get unique beahvior labels -n_neurons = size(Spike_rasters_final,1); %Get number of neurons -n_behav = length(unqLabels); %Get number of unique behavior labels - -%Get firing rate per behavior -response_matrix = cell(n_neurons,n_behav); %initialize response matrix -med_response_matrix = zeros(n_neurons,n_behav); %initialize median response matrix -mean_response_matrix = zeros(n_neurons,n_behav); %initialize mean response matrix -sd_response_matrix = zeros(n_neurons,n_behav); %initialize sd response matrix -group = cell(1,n_behav); %initialize group label matrix -for unit = 1:n_neurons %for all units - for b = 1:n_behav %for all behaviors - idx = find(behavior_labels == unqLabels(b)); %get idx where behavior b occurred - response_matrix{unit, b} = Spike_raster_relative2baseline(unit, idx); %Spike_raster_zscore(unit, idx); %get firing rate response during behavior "b" - mean_response_matrix(unit,b) = mean(response_matrix{unit, b}); %extract mean response for all epochs where behavior "b" occurred - med_response_matrix(unit,b) = median(response_matrix{unit, b});%extract median response for all epochs where behavior "b" occurred - sd_response_matrix(unit,b) = std(response_matrix{unit, b});%extract sd response for all epochs where behavior "b" occurred - group{1,b} = b* ones(1,length(idx)); %Keep track of behavior - end -end - -%Test difference in firing rate between behaviors. -%Plot firing rate per behavior - -%Select subset of behaviors with a minimum occurrence -behav_freq_table = tabulate(behavior_labels); -behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) -min_occurrences = 90; -behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences -%behav = [4:10,16:17,20:23];%[4:6,11,17]; %select behaviors manually - -%Initialize -preferred_behav = strings(1,n_neurons); group_label_full =[]; -p = zeros(1,n_neurons); unit = 1; -subsample = 1; %If subsample or resample observations. 1 subsample; 0 no subsampling. -colormap_social = repmat("b",1,n_behav); colormap_social(social_set)='r'; %Colormap - -for unit = randsample(1:n_neurons, n_neurons) %for all units - - colors = colormap_social(behav); %cool(length(behav)); %set colors - median_resp = med_response_matrix(unit,behav); %Get median response to order plot - resp_mat = {response_matrix{unit,behav}}; - n_per_behav = cell2mat(cellfun(@length,resp_mat,'uni',false)); - - subgroup = {group{1,behav}}; - sub_behav_categ = {behav_categ{behav}}; - - group_label_full =[]; - if subsample %if balance observations across behaviors - for b = 1:length(behav) %balance observations for all behaviors - group_label =[]; - n_obs = length(resp_mat{b}); - if n_obs>=min_occurrences - resp_mat{b} = resp_mat{b}(randsample(1:n_obs,min_occurrences)); - else - resp_mat{b} = resp_mat{b}(randsample(1:n_obs,min_occurrences, true)); - end - median_resp(b) = median(resp_mat{b}); - if n_obs>0 - group_label = categorical(repelem({behav_categ{behav(b)}}, [min_occurrences*ones(1,length(behav(b)))])); - end - group_label_full = [group_label_full,group_label]; - end - group_label_final = group_label_full; - else %if don't balance observations - group_label_final = categorical({behav_categ{[subgroup{1,:}]'}}); - end - - %sort according to median response - [~,idx]=sort(median_resp,'descend'); - preferred_behav(unit) = sub_behav_categ{idx(1)}; - groupDescend = reordercats(group_label_final,{sub_behav_categ{idx}}); - randgroupDescend = randsample(groupDescend, length(groupDescend)); %randomize for "chance level" - - %Run non-parametric anova (issue that samples are not independent...) - [p(unit), tbl, stats]= kruskalwallis([resp_mat{1,:}]',groupDescend,'off'); - [p_rand(unit), tbl, stats]= kruskalwallis([resp_mat{1,:}]',randgroupDescend,'off'); -% effect_size(unit)= max(abs(diff(stats.means))); - - %Plot if unit is selective: - %Note: as the test is run now, all units are selective... -% % % % if p(unit)<0.01 -% % % % figure(unit); hold on; set(gcf,'Position',[150 250 1000 500]) -% % % % boxplot([resp_mat{1,:}]',groupDescend) -% % % % h = findobj(gca,'Tag','Box'); -% % % % for j=1:length(h) -% % % % patch(get(h(j),'XData'),get(h(j),'YData'),colors(j),'FaceAlpha',.5); -% % % % end -% % % % ylabel('Z-scored firing rate') -% % % % text(0.1,2.5,['Kruskall Wallis, p-value = ' num2str(round(p(unit),4))],'FontSize',16) -% % % % %leg = legend(behav_categ(behav)); -% % % % %ylim([-1.5,3]) -% % % % %title(leg,'Behavior') -% % % % title(['Firing rate per behavior, unit# ' num2str(unit)], 'Fontsize',18) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % xtickangle(45) -% % % % pause(1) -% % % % end - - %Get overlapping histogram plots - %figure; hold on; histogram([resp_mat{1,1}]'); histogram([resp_mat{1,2}]') - -% %Plotting all behaviors -% [~,idx]=sort(med_response_matrix(unit,:),'descend'); -% preferred_behav(unit) = behav_categ{idx(1)}; -% group_label = categorical({behav_categ{[group{1,:}]'}}); -% figure(unit) -% groupDescend = reordercats(group_label,{behav_categ{idx}}); -% boxplot([response_matrix{unit,:}]',groupDescend, 'PlotStyle','compact') - - close all - -% disp([num2str(unit) '/' num2str(n_neurons)]) -end - -%Plot histogram of pvalues -figure; hold on; set(gcf,'Position',[150 250 1300 400]) -subplot(2,2,1) -histogram(p, 30,'Normalization','probability'); title("Histogram of p-values") -xlabel('Kruskall-Wallis p-value') -prop_selective_units = length(find(p<0.001))/length(p)*100; -text(0.1, 0.5, ['Proportion of selective units (p<0.001): ' num2str(round(prop_selective_units,2)) '%']) - -%Plot histogram of pvalues for randomized version -figure; hold on; set(gcf,'Position',[150 250 1300 400]) -subplot(2,2,2) -histogram(p_rand, 30,'Normalization','probability'); title("Histogram of p-values") -xlabel('Kruskall-Wallis p-value') -prop_selective_units = length(find(p<0.001))/length(p)*100; -text(0.1, 0.5, ['Proportion of selective units (p<0.001): ' num2str(round(prop_selective_units,2)) '%']) - -% %Plot histogram of effect size -% subplot(2,2,2) -% histogram(effect_size);title("Histogram of effect size") -% xlabel('Difference of means between groups (effect size)') - -%Plot the preferred behaviors for all units - any important bias? -subplot(2,2,3) -A = preferred_behav; -countA = countmember(behav_categ(behav),A); -[~, idx] = sort(countA); -B={behav_categ(behav(idx)) countA(idx)/n_neurons}; -bar(B{2}); -set(gca,'XTickLabel',B{1}); xtickangle(45) -title("Preferred behavior all units") - -%Plot preferred behavior for units that are statistically selective -subplot(2,2,4) -A = preferred_behav(p<0.001); -countA = countmember(behav_categ(behav),A); -[~, idx] = sort(countA); -B={behav_categ(behav(idx)) countA(idx)/n_neurons}; -bar(B{2}); -set(gca,'XTickLabel',B{1}); xtickangle(45) -title("Preferred behavior selective units (p<0.001)") - -if with_NC == 0 - sgtitle('Selectivity and preferred behavior for all units except noise cluster (1st channel)') - saveas(gcf, [savePath '/Preferred_behavior/Preferred_activity_NoNC_units.png']) -elseif isolatedOnly - sgtitle('Selectivity and preferred behavior for isolated units') - saveas(gcf, [savePath '/Preferred_behavior/Preferred_activity_isolated_units.png']) -else - sgtitle('Selectivity and preferred behavior for all units') - saveas(gcf, [savePath '/Preferred_behavior/Preferred_activity_all_units.png']) -end - diff --git a/Single neuron/old_code/log_FiringRatePerBehavior.m b/Single neuron/old_code/log_FiringRatePerBehavior.m deleted file mode 100644 index ad1ce07..0000000 --- a/Single neuron/old_code/log_FiringRatePerBehavior.m +++ /dev/null @@ -1,220 +0,0 @@ -%% Log_FiringRatePerBehavior -% This script generates firing rate of individual neuron under different -% behavioral conditions (considering individual secons as independent). - -% Load data -%Set path -is_mac = 1; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -clearvars -except savePath filePath is_mac - -%Set parameters -temp_resolution = 1; -channel_flag = "all"; -with_NC =0; isolatedOnly=0; - -%Get data with specified temporal resolution and channels -[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - -session_length = size(Spike_rasters,2); % get session length - -%Gt behavior label -behavior_labels = cell2mat({labels{:,3}}');%Get behavior label -behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (mark as "undefined"). - -%Get firing rate during a baseline time (when monkey doesn't engage in a -%particular behavior) -idx_rest=find(behavior_labels==length(behav_categ)); -baseline_firing = mean(Spike_rasters(:,idx_rest),2); - -%eliminate units with very low firing rate -mean_firing_rate = mean(Spike_rasters,2); -min_firing_units = find(mean_firing_rate>0); -Spike_rasters_final = Spike_rasters(min_firing_units,:) ; - -%Standerdize Unit rasters -Spike_raster_zscore = zscore(Spike_rasters_final,0, 2); %Z-score -Spike_raster_meandivided = Spike_rasters_final./mean_firing_rate; %Divide by the mean -Spike_raster_relative2baseline = Spike_rasters_final./baseline_firing; %Divide by baseline firing - -%Set parameters -unqLabels = 1:max(behavior_labels)-1; %Get unique beahvior labels -n_neurons = size(Spike_rasters_final,1); %Get number of neurons -n_behav = length(unqLabels); %Get number of unique behavior labels - -%Get firing rate per behavior -response_matrix = cell(n_neurons,n_behav); %initialize response matrix -med_response_matrix = zeros(n_neurons,n_behav); %initialize median response matrix -mean_response_matrix = zeros(n_neurons,n_behav); %initialize mean response matrix -sd_response_matrix = zeros(n_neurons,n_behav); %initialize sd response matrix -group = cell(1,n_behav); %initialize group label matrix -for unit = 1:n_neurons %for all units - for b = 1:n_behav %for all behaviors - idx = find(behavior_labels == unqLabels(b)); %get idx where behavior b occurred - response_matrix{unit, b} = Spike_raster_relative2baseline(unit, idx); %Spike_raster_zscore(unit, idx); %get firing rate response during behavior "b" - mean_response_matrix(unit,b) = mean(response_matrix{unit, b}); %extract mean response for all epochs where behavior "b" occurred - med_response_matrix(unit,b) = median(response_matrix{unit, b});%extract median response for all epochs where behavior "b" occurred - sd_response_matrix(unit,b) = std(response_matrix{unit, b});%extract sd response for all epochs where behavior "b" occurred - group{1,b} = b* ones(1,length(idx)); %Keep track of behavior - end -end - -%Test difference in firing rate between behaviors. -%Plot firing rate per behavior - -%Select subset of behaviors with a minimum occurrence -behav_freq_table = tabulate(behavior_labels); -behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) -min_occurrences = 90; -behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences -%behav = [4:10,16:17,20:23];%[4:6,11,17]; %select behaviors manually - -%Initialize -preferred_behav = strings(1,n_neurons); group_label_full =[]; -p = zeros(1,n_neurons); unit = 1; -subsample = 1; %If subsample or resample observations. 1 subsample; 0 no subsampling. -colormap_social = repmat("b",1,n_behav); colormap_social(social_set)='r'; %Colormap - -for unit = randsample(1:n_neurons, n_neurons) %for all units - - colors = colormap_social(behav); %cool(length(behav)); %set colors - median_resp = med_response_matrix(unit,behav); %Get median response to order plot - resp_mat = {response_matrix{unit,behav}}; - n_per_behav = cell2mat(cellfun(@length,resp_mat,'uni',false)); - - subgroup = {group{1,behav}}; - sub_behav_categ = {behav_categ{behav}}; - - group_label_full =[]; - if subsample %if balance observations across behaviors - for b = 1:length(behav) %balance observations for all behaviors - group_label =[]; - n_obs = length(resp_mat{b}); - if n_obs>=min_occurrences - resp_mat{b} = resp_mat{b}(randsample(1:n_obs,min_occurrences)); - else - resp_mat{b} = resp_mat{b}(randsample(1:n_obs,min_occurrences, true)); - end - median_resp(b) = median(resp_mat{b}); - if n_obs>0 - group_label = categorical(repelem({behav_categ{behav(b)}}, [min_occurrences*ones(1,length(behav(b)))])); - end - group_label_full = [group_label_full,group_label]; - end - group_label_final = group_label_full; - else %if don't balance observations - group_label_final = categorical({behav_categ{[subgroup{1,:}]'}}); - end - - %sort according to median response - [~,idx]=sort(median_resp,'descend'); - preferred_behav(unit) = sub_behav_categ{idx(1)}; - groupDescend = reordercats(group_label_final,{sub_behav_categ{idx}}); - randgroupDescend = randsample(groupDescend, length(groupDescend)); %randomize for "chance level" - - %Run non-parametric anova (issue that samples are not independent...) - [p(unit), tbl, stats]= kruskalwallis([resp_mat{1,:}]',groupDescend,'off'); - [p_rand(unit), tbl, stats]= kruskalwallis([resp_mat{1,:}]',randgroupDescend,'off'); -% effect_size(unit)= max(abs(diff(stats.means))); - - %Plot if unit is selective: - %Note: as the test is run now, all units are selective... -% % % % if p(unit)<0.01 -% % % % figure(unit); hold on; set(gcf,'Position',[150 250 1000 500]) -% % % % boxplot([resp_mat{1,:}]',groupDescend) -% % % % h = findobj(gca,'Tag','Box'); -% % % % for j=1:length(h) -% % % % patch(get(h(j),'XData'),get(h(j),'YData'),colors(j),'FaceAlpha',.5); -% % % % end -% % % % ylabel('Z-scored firing rate') -% % % % text(0.1,2.5,['Kruskall Wallis, p-value = ' num2str(round(p(unit),4))],'FontSize',16) -% % % % %leg = legend(behav_categ(behav)); -% % % % %ylim([-1.5,3]) -% % % % %title(leg,'Behavior') -% % % % title(['Firing rate per behavior, unit# ' num2str(unit)], 'Fontsize',18) -% % % % ax = gca; -% % % % ax.FontSize = 14; -% % % % xtickangle(45) -% % % % pause(1) -% % % % end - - %Get overlapping histogram plots - %figure; hold on; histogram([resp_mat{1,1}]'); histogram([resp_mat{1,2}]') - -% %Plotting all behaviors -% [~,idx]=sort(med_response_matrix(unit,:),'descend'); -% preferred_behav(unit) = behav_categ{idx(1)}; -% group_label = categorical({behav_categ{[group{1,:}]'}}); -% figure(unit) -% groupDescend = reordercats(group_label,{behav_categ{idx}}); -% boxplot([response_matrix{unit,:}]',groupDescend, 'PlotStyle','compact') - - close all - -% disp([num2str(unit) '/' num2str(n_neurons)]) -end - -%Plot histogram of pvalues -figure; hold on; set(gcf,'Position',[150 250 1300 700]) -subplot(2,2,1) -histogram(p, 30,'Normalization','probability'); title("Histogram of p-values") -xlabel('Kruskall-Wallis p-value'); ylim([0 1]) -prop_selective_units = length(find(p<0.001))/length(p)*100; -text(0.1, 0.5, ['Proportion of selective units (p<0.001): ' num2str(round(prop_selective_units,2)) '%']) - -%Plot histogram of pvalues for randomized version -subplot(2,2,2) -histogram(p_rand, 30,'Normalization','probability'); title("Histogram of p-values") -xlabel('Kruskall-Wallis p-value'); ylim([0 1]) -prop_selective_units = length(find(p_rand<0.001))/length(p_rand)*100; -text(0.1, 0.5, ['Proportion of selective units (p<0.001): ' num2str(round(prop_selective_units,2)) '%']) - -% %Plot histogram of effect size -% subplot(2,2,2) -% histogram(effect_size);title("Histogram of effect size") -% xlabel('Difference of means between groups (effect size)') - -%Plot the preferred behaviors for all units - any important bias? -subplot(2,2,3) -A = preferred_behav; -countA = countmember(behav_categ(behav),A); -[~, idx] = sort(countA); -B={behav_categ(behav(idx)) countA(idx)/n_neurons}; -bar(B{2}); -set(gca,'XTickLabel',B{1}); xtickangle(45) -title("Preferred behavior all units") - -%Plot preferred behavior for units that are statistically selective -subplot(2,2,4) -A = preferred_behav(p<0.001); -countA = countmember(behav_categ(behav),A); -[~, idx] = sort(countA); -B={behav_categ(behav(idx)) countA(idx)/n_neurons}; -bar(B{2}); -set(gca,'XTickLabel',B{1}); xtickangle(45) -title("Preferred behavior selective units (p<0.001)") - -if with_NC == 0 - sgtitle('Selectivity and preferred behavior for all units except noise cluster (1st channel)') - saveas(gcf, [savePath '/Preferred_behavior/Preferred_activity_NoNC_units.png']) -elseif isolatedOnly - sgtitle('Selectivity and preferred behavior for isolated units') - saveas(gcf, [savePath '/Preferred_behavior/Preferred_activity_isolated_units.png']) -else - sgtitle('Selectivity and preferred behavior for all units') - saveas(gcf, [savePath '/Preferred_behavior/Preferred_activity_all_units.png']) -end - diff --git a/Single neuron/old_code/log_MeanFiring_Kurtosis_batch.m b/Single neuron/old_code/log_MeanFiring_Kurtosis_batch.m deleted file mode 100644 index 1e5b331..0000000 --- a/Single neuron/old_code/log_MeanFiring_Kurtosis_batch.m +++ /dev/null @@ -1,154 +0,0 @@ -%% Log_MeanFiring_Kurtosis_batch -% This script extracts the mean z-scored firing rate of individual neuron under different -% behavioral conditions. -% C. Testard Jan 2023 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -plot_toggle = 0; -select_behav=0; -with_partner = 0; -temp_resolution = 1; %Temporal resolution of firing rate. 1: 1sec; 10:100msec; 0.1: 10sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -min_occurrence =30*temp_resolution; -cohend_cutoff=0.3; p_cutoff=0.01;%Set thresholds -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size in sec (sigma) -null=0;%Set whether we want the null -agg_precedence =1; - - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/Subject_behav']; - - - %% Load data - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - session_length = size(Spike_rasters,2); % get session length - - n_neurons(s) = size(Spike_rasters,1); %Get number of neurons - - %Estimate mean firing rate across units - mean_firing{s,1} = mean(Spike_rasters')'; - mean_firing_TEO{s,1} = mean(Spike_rasters(strcmp(brain_label,'TEO'),:)')'; %TEO - mean_firing_vlPFC{s,1} = mean(Spike_rasters(strcmp(brain_label,'vlPFC'),:)')'; %vlPFC - - %Estimate "baseline" neural firing distribution. - Spike_rasters_zscore = zscore(Spike_rasters,0,2); - kurtosis_firing{s,1}= kurtosis(Spike_rasters_zscore')'; - kurtosis_firing_TEO{s,1}= kurtosis(Spike_rasters_zscore(strcmp(brain_label,'TEO'),:)')'; - kurtosis_firing_vlPFC{s,1}= kurtosis(Spike_rasters_zscore(strcmp(brain_label,'vlPFC'),:)')'; - - %corr(mean(Spike_rasters(:,10:end-10)')', kurtosis(Spike_rasters_zscore')') - - - -end - -close all - -edges=linspace(0,100,20); -teo_mean = cell2mat(mean_firing_TEO); -vlpfc_mean = cell2mat(mean_firing_vlPFC); -figure; hold on -histogram(teo_mean,edges); histogram(vlpfc_mean,edges) - -teo_kurtosis = cell2mat(kurtosis_firing_TEO); -vlpfc_kurtosis = cell2mat(kurtosis_firing_vlPFC); -figure; hold on -histogram(teo_kurtosis,edges); histogram(vlpfc_kurtosis,edges) - - -%% Results across sessions - -%Change savePath for all session results folder: -savePath = [home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SingleUnit_results/']; - -%Plot massive heatmap -figure; set(gcf,'Position',[150 250 1500 400]); -subplot(1,2,1) -all_sessions_data_a = cell2mat(save_meanBehav(a_sessions)'); -[~, sortIdx]= sort(nanmean(all_sessions_data_a,1)); -all_sessions_data_sorted_a = all_sessions_data_a(:,sortIdx); AxesLabels_sorted_a = AxesLabels(sortIdx); -[nanrow nancol_a]=find(~isnan(all_sessions_data_sorted_a)); nancol_a = unique(nancol_a); -hp=heatmap(all_sessions_data_sorted_a(:,nancol_a), 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',jet); hp.XDisplayLabels = AxesLabels_sorted_a(nancol_a); caxis([-2 2]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Amos']) -ax = gca; -ax.FontSize = 14; - -subplot(1,2,2) -all_sessions_data_h = cell2mat(save_meanBehav(h_sessions)'); -[~, sortIdx]= sort(nanmean(all_sessions_data_h,1)); -all_sessions_data_sorted_h = all_sessions_data_h(:,sortIdx); AxesLabels_sorted_h = AxesLabels(sortIdx); -[nanrow nancol_h]=find(~isnan(all_sessions_data_sorted_h)); nancol_h = unique(nancol_h); -hp=heatmap(all_sessions_data_sorted_h(:,nancol_h), 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',jet); hp.XDisplayLabels = AxesLabels_sorted_h(nancol_h); caxis([-2 2]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Hooke']) -ax = gca; -ax.FontSize = 14; -%saveas(gcf, [savePath '/Zscore_heatmap.pdf']); close all - - -%Plot heatmap binarized across sessions -figure; set(gcf,'Position',[150 250 1500 400]); -subplot(1,2,1) -all_sessions_data_binary = sign(all_sessions_data_sorted_a); -hp=heatmap(all_sessions_data_binary(:,nancol_a), 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',jet); hp.XDisplayLabels = AxesLabels_sorted_a(nancol_a); caxis([-1.5 1.5]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Amos']) -ax = gca; -ax.FontSize = 14; - -subplot(1,2,2) -all_sessions_data_binary = sign(all_sessions_data_sorted_h); -hp=heatmap(all_sessions_data_binary(:,nancol_h), 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',jet); hp.XDisplayLabels = AxesLabels_sorted_h(nancol_h); caxis([-1.5 1.5]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Hooke']) -ax = gca; -ax.FontSize = 14; -%saveas(gcf, [savePath '/Zscore_heatmap_binarized.pdf']); close all - -%Plot violins -figure; set(gcf,'Position',[150 250 1500 400]); -subplot(1,2,1) -violin(all_sessions_data_sorted_a(:,nancol_a)) -yline(0,'LineStyle','--') -xticks(1:length(nancol_a)); xticklabels(AxesLabels_sorted_a(nancol_a)) -ylabel('Z-scored firight rate'); title('Amos') - -subplot(1,2,2) -violin(all_sessions_data_sorted_h(:,nancol_h)) -yline(0,'LineStyle','--') -xticks(1:length(nancol_h)); xticklabels(AxesLabels_sorted_h(nancol_h)) -ylabel('Z-scored firight rate'); title('Hooke') -%saveas(gcf, [savePath '/Zscore_VIOLINPLOT.pdf']); close all diff --git a/Single neuron/old_code/log_PETH_behavior.m b/Single neuron/old_code/log_PETH_behavior.m deleted file mode 100644 index be2385f..0000000 --- a/Single neuron/old_code/log_PETH_behavior.m +++ /dev/null @@ -1,38 +0,0 @@ -%% log_PETH_behavior -% Firing rate aligned to onset or offset of behavior - -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) -cd(filePath) - -session = filePath(end-9:end); -monkey = filePath(end-14:end-10); - -%behav_log = readtable('behavioral_log_session1.csv'); -behavior_log = readtable(['EVENTLOG_restructured_',monkey,session,'.csv']);% Behavioral data -load('Neural_data.mat') % Neural data; array1 is in TEO and array2 is in vlPFC -session_length = size(Unit_rasters,2); -n_neurons = size(Unit_rasters,1); - -%Preprocessing: round times in behavioral log -behavior_log{:,'start_time_round'}=round(behavior_log{:,'start_time'}); -behavior_log{:,'end_time_round'}=round(behavior_log{:,'end_time'}); -behavior_log{:,'duration_round'}=behavior_log{:,'end_time_round'}-behavior_log{:,'start_time_round'}; - -for unit = 1:n_neurons - - events = find(strcmp('Groom Give', behavior_log{:,'Behavior'})); - event_times = behavior_log{events,'start_time_round'}; - intervals = [event_times-4 event_times+4]; - figure; hold on - for e = 1:length(event_times) - PETH(e,:) = Unit_rasters(unit,intervals(e,1):intervals(e,2)); - plot(PETH(e,:), 'Color',[0, 0, 1, 0.1]) - end - plot(mean(PETH,1), LineWidth=5), 'Color',[0, 0.8, 1, 0.2]; ylabel('Firing rate (Hz)'); title(['Event onset Unit #' num2str(unit)]) - line([5 5],[0, max(PETH(:))], 'LineStyle','--', 'Color','red') - - pause(1) - - close all - -end \ No newline at end of file diff --git a/Single neuron/old_code/log_PSTH_old.m b/Single neuron/old_code/log_PSTH_old.m deleted file mode 100644 index b10f4c8..0000000 --- a/Single neuron/old_code/log_PSTH_old.m +++ /dev/null @@ -1,224 +0,0 @@ -%% Log_PETH.m -% Produce peri-event time hitograms. -% C. Testard, July 2023 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range=[1:6,11:13,15:16,18]; -a_sessions = 1:6; h_sessions = [11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -threat_precedence =0; -exclude_sq=1; -plot_toggle=0; - - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/Example_units']; - - %% Load data - - % Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - Spike_rasters = Spike_rasters'; - Spike_rasters_zscore = zscore(Spike_rasters); - - %% Adjust behavior log - - behavior_log_adj = behavior_log; - - %remove point behaviors (<5sec) - behavior_log_adj=behavior_log_adj(behavior_log_adj{:,'duration_round'}>5,:); - - %pool aggression, HIS and HIP - behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'HIS'), "Behavior"} = repmat({'Aggression'},size(behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'HIS'), "Behavior"})); - behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'HIP'), "Behavior"} = repmat({'Aggression'},size(behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'HIP'), "Behavior"})); - - %Set proximity, RR and scratch to rest - behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'Proximity'), "Behavior"} = repmat({'Rest'},size(behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'Proximity'), "Behavior"})); - behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'RR'), "Behavior"} = repmat({'Rest'},size(behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'RR'), "Behavior"})); - behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'Scratch'), "Behavior"} = repmat({'Rest'},size(behavior_log_adj{strcmp(behavior_log_adj{:,'Behavior'},'Scratch'), "Behavior"})); - - - %% Extract PETH align to event onset - - %Set parameters - time_pre = 2; - time_post = 20; - beh_list = {'Groom Give','Groom Receive','Self-groom','Foraging','Aggression'}; - beh=5; - - %define index of behavior - beh_idx=find((strcmp(behavior_log_adj{:,'Behavior'},beh_list{beh})==1 & behavior_log_adj{:,'duration_round'}>time_post)==1); - length(beh_idx) %num events - - %If the grooming bout occurs less than 10sec after the previous one, - %consider the same bout. - for i = 1:length(beh_idx)-1 - ibi(i+1) = behavior_log_adj{beh_idx(i+1),'start_time'} - behavior_log_adj{beh_idx(i),'end_time'}; - end - beh_idx = beh_idx(ibi>10); - clear ibi - - %extract previous behavior - [~, idx_sorted]= sort(behavior_log_adj{beh_idx-1,'Behavior'}); - beh_idx_sorted = beh_idx(idx_sorted); - prev_beh = behavior_log_adj{beh_idx_sorted-1,'Behavior'}; - - %Plot PETH as a heatmap - for n=135%randi(size(Spike_rasters,2),1,5) - - for i=1:length(beh_idx_sorted) - onset = behavior_log_adj{beh_idx_sorted(i),'start_time_round'}; - firing_rate(i,:) = Spike_rasters([onset-time_pre:onset-1, onset+1:onset+time_post],n); - end - firing_rate_norm = firing_rate./firing_rate(:,1); - [~, sorted]=sort(mean(firing_rate,2)); - [~, sorted_norm]=sort(mean(firing_rate_norm,2)); - - figure; hold on - %idx_order = mapTmap(firing_rate); - imagesc(firing_rate(sorted,:)); colorbar - yticks([1:size(firing_rate,1)]) - yticklabels(prev_beh) - xline(time_pre+0.5, 'LineWidth',10) - xlabel('sec') - title('Raw firing rate') - - figure; hold on - imagesc(firing_rate_norm(sorted_norm,:)); colorbar - yticks([1:size(firing_rate,1)]) - yticklabels(prev_beh) - xline(time_pre+0.5, 'LineWidth',10) - xlabel('sec') - title('Normalized') - -% subplot(2,1,2); hold on -% y=mean(firing_rate); -% x = 1:numel(y); -% sem_dev=std(firing_rate)./sqrt(size(firing_rate,1)); -% curve1 = y+sem_dev; -% curve2 = y-sem_dev; -% x2 = [x, fliplr(x)]; -% inBetween = [curve1, fliplr(curve2)]; -% fill(x2, inBetween, 'c'); -% hold on; -% plot(x,y,'LineWidth',2) -% xline(time_pre+0.5, 'LineWidth',20) - - end - clear firing_rate - clear firing_rate_norm - - - %% Extract PETH align to event offset - - %Set parameters - time_pre = 5; - time_post = 5; - beh_list = {'Groom Give','Groom Receive','Self-groom','Rest','Aggression'}; - beh=4; - clear ibi - clear firing_rate - - %define index of behavior - beh_idx=find((strcmp(behavior_log_adj{:,'Behavior'},beh_list{beh})==1 & behavior_log_adj{:,'duration_round'}>time_pre)==1); - length(beh_idx) %num events - - %If the grooming bout occurs less than 10sec after the previous one, - %consider the same bout. - for i = 1:length(beh_idx)-1 - ibi(i+1) = behavior_log_adj{beh_idx(i+1),'start_time'} - behavior_log_adj{beh_idx(i),'end_time'}; - end - - %Merge bouts - merge_idx = find(ibi<10); - for i = 2:length(merge_idx) - - e = beh_idx(merge_idx(i)); - behavior_log_adj{e-1,'end_time'} = behavior_log_adj{e,'end_time'}; - - end - behavior_log_adj(beh_idx(merge_idx(2:end)),:)=[]; - clear ibi - - %Adjust durations - behavior_log_adj{:,'duration_s'} = behavior_log_adj{:,'end_time'} - behavior_log_adj{:,'start_time'}; - behavior_log_adj{:,'end_time_round'} = round(behavior_log_adj{:,'end_time'}); - behavior_log_adj{:,'start_time_round'} = round(behavior_log_adj{:,'start_time'}); - behavior_log_adj{:,'duration_round'} = round(behavior_log_adj{:,'duration_s'}); - - %Re-define index of behavior - beh_idx=find((strcmp(behavior_log_adj{:,'Behavior'},beh_list{beh})==1 & behavior_log_adj{:,'duration_round'}>time_pre)==1); - length(beh_idx) %num events - - - %extract subsequent behavior - [~, idx_sorted]= sort(behavior_log_adj{beh_idx+1,'Behavior'}); - beh_idx_sorted = beh_idx(idx_sorted); - next_beh = behavior_log_adj{beh_idx_sorted+1,'Behavior'} - - %Plot PETH as a heatmap - for n=randi(size(Spike_rasters,2),1,5) - - for i=1:length(beh_idx_sorted) - offset = behavior_log_adj{beh_idx_sorted(i),'end_time_round'}; - firing_rate(i,:) = Spike_rasters([offset-time_pre:offset-1, offset+1:offset+time_post],n); - end - - figure; hold on - imagesc(firing_rate); colorbar - yticks([1:size(firing_rate,1)]) - yticklabels(next_beh) - xline(time_pre+0.5, 'LineWidth',10) - xlabel('sec') - title('Raw firing rate') - - behavs = unique(next_beh); - baseline_firing = mean(firing_rate(:,1:3)); - figure; hold on - for b=1:length(behavs) - - if length(find(strcmp(next_beh,behavs(b))))>1 - post_firing = mean(firing_rate(strcmp(next_beh,behavs(b)),time_pre+1:time_pre+time_post)); - else - post_firing = firing_rate(strcmp(next_beh,behavs(b)),time_pre+1:time_pre+time_post); - end - - trace = [baseline_firing, post_firing]; - plot(trace, 'LineWidth', 2) - - end - xline(3,'LineStyle','--') - legend(behavs) - - end - - -end \ No newline at end of file diff --git a/Single neuron/old_code/log_PopulationSparseness_batch.m b/Single neuron/old_code/log_PopulationSparseness_batch.m deleted file mode 100644 index fd2581b..0000000 --- a/Single neuron/old_code/log_PopulationSparseness_batch.m +++ /dev/null @@ -1,89 +0,0 @@ -%% Log_PopulationSparseness_batch -% This script computes the mean z-scored firing rate of individual neuron under different -% behavioral conditions. -% C. Testard Jan 2023 - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -plot_toggle = 0; -select_behav=0; -with_partner = 0; -temp_resolution = 1; %Temporal resolution of firing rate. 1: 1sec; 10:100msec; 0.1: 10sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -min_occurrence =30*temp_resolution; -cohend_cutoff=0.3; p_cutoff=0.01;%Set thresholds -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size in sec (sigma) -null=0;%Set whether we want the null -agg_precedence =1; - - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/Subject_behav']; - - - %% Load data - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - session_length = size(Spike_rasters,2); % get session length - - %Extract behavior labels - behavior_labels = cell2mat({labels{:,3}}');%Get behavior label from labels structure - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - - - n_neurons = size(Spike_rasters,1); %Get number of neurons - - %Estimate "baseline" neural firing distribution. - idx_rest = find(behavior_labels==length(behav_categ)); - baseline = Spike_rasters(:,idx_rest); - baseline_std = std(baseline'); - baseline_mean = mean(baseline'); - - %Get population sparsness - behav_firing = Spike_rasters(:,behavior_labels~=length(behav_categ)); - for ti = 1:size(behav_firing,2) - active(:,ti)=(behav_firing(:,ti)>(baseline_mean+baseline_std)' | behav_firing(:,ti)<(baseline_mean-baseline_std)'); - end - - num_units_active{s} = sum(active); - prop_units_active{s} = sum(active)./n_neurons; - -end - diff --git a/Single neuron/old_code/log_SingleNeuronTuning.asv b/Single neuron/old_code/log_SingleNeuronTuning.asv deleted file mode 100644 index c6ffe74..0000000 --- a/Single neuron/old_code/log_SingleNeuronTuning.asv +++ /dev/null @@ -1,270 +0,0 @@ -%% Log_FiringRatePerBehavior -% This script generates firing rate of individual neuron under different -% behavioral conditions (considering individual secons as independent). - -% Load data -%Set path -is_mac = 1; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -clearvars -except savePath filePath is_mac - -%Set parameters -temp_resolution = 1; -channel_flag = "all"; -with_NC =1; isolatedOnly=0; - -%Get data with specified temporal resolution and channels -[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - -session_length = size(Spike_rasters,2); % get session length - -%eliminate units with very low firing rate -mean_firing_rate = mean(Spike_rasters,2); -min_firing_units = find(mean_firing_rate>0); -Spike_rasters_final = Spike_rasters(min_firing_units,:) ; - -%Define "baseline" neural firing. -behavior_labels = cell2mat({labels{:,3}}');%Get behavior label -behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). -%Plot where baseline firing is taken from. Make sure its well spread out -%over the session. -idx_rest=find(behavior_labels==length(behav_categ)); -y=zeros(1, session_length); y(idx_rest)=1; -figure; plot(1:session_length, y); ylim([-1, 2]) - -baseline_firing = mean(Spike_rasters(:,idx_rest),2); -find(abs(mean_firing_rate- baseline_firing)>2)% check difference between means. - -%Standardize Unit rasters -Spike_raster_zscore = zscore(Spike_rasters,0, 2); %Z-score -Spike_raster_meandivided = Spike_rasters_final./mean_firing_rate; %Divide by the mean -Spike_raster_relative2baseline = Spike_rasters_final./baseline_firing; %Divide by baseline firing - -%Set parameters -unqLabels = 1:max(behavior_labels)-1; %Get unique behavior labels -n_neurons = size(Spike_rasters_final,1); %Get number of neurons -n_behav = length(unqLabels); %Get number of unique behavior labels -min_occurrences =90; %set the minimum number of occurrences (i.e. seconds where behavior occurs) -set_occurrences =90; - -%RESPONSIVENESS ANALYSIS: test whether firing rate for each behavior is -%different than the mean - -%Get firing rate per behavior -response_matrix = cell(n_neurons,n_behav); response_matrix_shuffle = cell(n_neurons,n_behav); %initialize response matrix -med_response_matrix = zeros(n_neurons,n_behav); %initialize median response matrix -mean_response_matrix = zeros(n_neurons,n_behav); %initialize mean response matrix -sd_response_matrix = zeros(n_neurons,n_behav); %initialize sd response matrix -h = nan(n_neurons,n_behav); h_shuffle = nan(n_neurons,n_behav); -group = cell(1,n_behav); %initialize group label matrix -p_thresh_array = [0.05, 0.01 0.005 0.001 0.0001 0.00001]; p=1; p_thresh=0.001; -responsiveness_test_perneuron = cell(1,length(p_thresh_array)); -responsiveness_test_perbehav = cell(1,length(p_thresh_array)); - -for p_thresh = p_thresh_array - for unit = 1:n_neurons %for all units - for b = 1:n_behav %for all behaviors - idx = find(behavior_labels == unqLabels(b)); %get idx where behavior b occurred - n_per_behav(b)=length(idx); - response_matrix{unit, b} = Spike_raster_relative2baseline(unit, idx); %Spike_raster_zscore(unit, idx); %get firing rate response during behavior "b" - %response_matrix_shuffle{unit, b} = Spike_raster_relative2baseline(unit, randsample(1:length(behavior_labels),length(idx))); - response_matrix_shuffle{unit, b} = Spike_raster_relative2baseline(unit, randsample(idx_rest,length(idx))); - - mean_response_matrix(unit,b) = mean(response_matrix{unit, b}); %extract mean response for all epochs where behavior "b" occurred - med_response_matrix(unit,b) = median(response_matrix{unit, b});%extract median response for all epochs where behavior "b" occurred - sd_response_matrix(unit,b) = std(response_matrix{unit, b});%extract sd response for all epochs where behavior "b" occurred - group{1,b} = b* ones(1,length(idx)); %Keep track of behavior - - if length(idx)>min_occurrences %if behavior occurred during at least 40 seconds - %figure; hist(response_matrix{unit, b}, 10); pause(1); close all %check distribution of sample - if length(idx)>=set_occurrences - sampled_data = response_matrix{unit, b}(randsample(length(idx),set_occurrences)); - sampled_data_shuffled = response_matrix_shuffle{unit, b}(randsample(length(idx),set_occurrences)); - h(unit,b) = ttest(sampled_data,1,'Alpha',p_thresh); %test if mean is different than 1. For now keep test of mean as ttest, but should be changed for a test assuming a poisson distribution - h_shuffle(unit,b) = ttest(sampled_data_shuffled,1,'Alpha',p_thresh); - else - sampled_data = response_matrix{unit, b}(randsample(length(idx),set_occurrences,true)); - sampled_data_shuffled = response_matrix_shuffle{unit, b}(randsample(length(idx),set_occurrences,true)); - h(unit,b) = ttest(sampled_data,1,'Alpha',p_thresh); %test if mean is different than 1. For now keep test of mean as ttest, but should be changed for a test assuming a poisson distribution - h_shuffle(unit,b) = ttest(sampled_data_shuffled,1,'Alpha',p_thresh); - end - else - h(unit,b)=nan; - h_shuffled(unit,b)=nan; - end - end - end - responsiveness_test_perneuron{p} = [nansum(h,2) nansum(h_shuffle,2)]; - total_selective(p)=sum(responsiveness_test_perneuron{p}(:,1)); - num_nonresponsive(p) = length(find(responsiveness_test_perneuron{p}(:,1)==0)); %Find number of units that are not responsive to any behavior - total_falsepos(p)=sum(responsiveness_test_perneuron{p}(:,2)); - - responsiveness_test_perbehav{p} = [n_per_behav; nansum(h,1); nansum(h_shuffle,1)]; - p=p+1; - -end - -%Check the proportion of false positives for each threshold -total_falsepos./total_selective -%Based on the proportion of false positives, I selected the threshold p=0.001. - -%plot the number of selective units per behavior -figure; hold on; set(gcf,'Position',[150 250 1300 500]); -for p=1:length(p_thresh_array) - plot(1:length(responsiveness_test_perbehav{p}(2,:)), responsiveness_test_perbehav{p}(2,:)/n_neurons, '-o', 'LineWidth',2) - plot(1:length(responsiveness_test_perbehav{p}(3,:)), responsiveness_test_perbehav{p}(3,:)/n_neurons, 'LineStyle','--','Color','k') -end -lgd=legend(["0.05","","0.01","", "0.005","", "0.001","", "0.0001","", "0.00001","Shuffled"]); lgd.Title.String = 'p-value threshold'; -xticks(1:length(nansum(h,1))); xticklabels(behav_categ(1:end-1)); xtickangle(45); ylim([0 1]) -xlabel("Behaviors"); ylabel('# responsive units'); title(['Number of responsive units per behavior, at multiple thresholds, min-occrruence=' num2str(min_occurrences)]) -if with_NC == 0 - saveas(gcf, [savePath '/Responsiveness_per_behavior_NoNC_units.png']) -elseif isolatedOnly - saveas(gcf, [savePath '/Responsiveness_per_behavior_isolated_units.png']) -else - saveas(gcf, [savePath '/Responsiveness_per_behavior_all_units.png']) -end - -% % %Get correlation between number of responsive units and number of trials at -% % %threshold p=0.001 -% % p=4; figure; h=scatter(responsiveness_test_perbehav{p}(1,:), responsiveness_test_perbehav{p}(2,:)); -% % [r p]=corrcoef(responsiveness_test_perbehav{p}(1,:), responsiveness_test_perbehav{p}(2,:)) - -% % %Code to check the number of false positives according to the number of -% % %"trials" (or seconds) in a behavior -% % responsiveness_test_perbehav = [n_per_behav; nansum(h,1); nansum(h_shuffle,1)]; responsiveness_test_perbehav(:,responsiveness_test_perbehav(1,:)==0)=[]; -% % figure; h=scatter(responsiveness_test_perbehav(1,:), responsiveness_test_perbehav(3,:)); -% % corrcoef(responsiveness_test_perbehav(1,:), responsiveness_test_perbehav(3,:)) -% % %Based on the correlation between false positives and number of trials -% % %considered, I exclude trials that occured for less than 40sec during the -% % %session. - -%SELECTIVITY analysis: Test difference in firing rate between behaviors. - -%Select subset of behaviors with a minimum occurrence -behav_freq_table = tabulate(behavior_labels); -behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) -boi = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences -%boi = [4:10,16:17,20:23];%[4:6,11,17]; %select behaviors manually - -%Initialize -total_iter = 10; -preferred_behav = strings(n_neurons, total_iter, length(boi), length(boi)); group_label_full =[]; -p = nan(n_neurons, total_iter, length(boi), length(boi)); unit = 1; -p_rand = nan(n_neurons, total_iter, length(boi), length(boi)); -subsample = 1; %If subsample or resample observations. 1 subsample; 0 no subsampling. -p_thresh = 0.01; - -for unit = 1:n_neurons %randsample(1:n_neurons, n_neurons) %for all units - - for iter = 1:total_iter - - for b1 = 1:length(boi)-1 - - for b2 = b1+1:length(boi) - - behav = [boi(b1),boi(b2)]; - median_resp = med_response_matrix(unit,behav); %Get median response to order plot - resp_mat = {response_matrix{unit,behav}}; - n_per_behav = cell2mat(cellfun(@length,resp_mat,'uni',false)); - - subgroup = {group{1,behav}}; - sub_behav_categ = {behav_categ{behav}}; - - group_label_full =[]; - if subsample %if balance observations across behaviors - for b = 1:length(behav) %balance observations for all behaviors - group_label =[]; - n_obs = length(resp_mat{b}); - if n_obs>=set_occurrences - resp_mat{b} = resp_mat{b}(randsample(1:n_obs,set_occurrences)); - else - resp_mat{b} = resp_mat{b}(randsample(1:n_obs,set_occurrences, true)); - end - median_resp(b) = median(resp_mat{b}); - if n_obs>0 - group_label = categorical(repelem({behav_categ{behav(b)}}, [set_occurrences*ones(1,length(behav(b)))])); - end - group_label_full = [group_label_full,group_label]; - end - group_label_final = group_label_full; - else %if don't balance observations - group_label_final = categorical({behav_categ{[subgroup{1,:}]'}}); - end - - %sort according to median response - [~,idx]=sort(median_resp,'descend'); - preferred_behav(unit, iter, b1, b2) = sub_behav_categ{idx(1)}; - groupDescend = reordercats(group_label_final,{sub_behav_categ{idx}}); - randgroupDescend = randsample(groupDescend, length(groupDescend)); %randomize for "chance level" - - %Run non-parametric anova (issue that samples are not independent...) - %[p_ks(unit), tbl_ks, stats_ks]= kruskalwallis([resp_mat{1,:}]',groupDescend,'off'); - [p(unit, iter, b1, b2), tbl, stats]= kruskalwallis([resp_mat{1,:}]',groupDescend,'off'); - [p_rand(unit, iter, b1, b2), tbl, stats]= kruskalwallis([resp_mat{1,:}]',randgroupDescend,'off'); - - end - end - %disp([num2str(iter) '/' num2str(total_iter)]) - - end - - stat_diff{unit}=squeeze(mean(squeeze(p(unit, :, :, :)),1)); - stat_diff_thresh{unit} = stat_diff{unit}; stat_diff_thresh{unit}(stat_diff{unit}<=p_thresh) =1; stat_diff_thresh{unit}(stat_diff{unit}>p_thresh)=0; - - stat_diff_rand{unit}=squeeze(mean(squeeze(p_rand(unit, :, :, :)),1)); - stat_diff_thresh_rand{unit} = stat_diff_rand{unit}; stat_diff_thresh_rand{unit}(stat_diff_rand{unit}<=p_thresh) =1; stat_diff_thresh_rand{unit}(stat_diff_rand{unit}>p_thresh)=0; - - AxesLabels = behav_categ(boi); -% % % figure; hp=heatmap(stat_diff_thresh{unit}, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -% % % hp.XDisplayLabels = AxesLabels; hp.YDisplayLabels = AxesLabels; -% % % title(['Selectivity heatmap for unit #' num2str(unit)]) -% % % saveas(hp, [savePath '/Selectivity_heatmap/selectivity_heatmap_unit_' num2str(unit) '.png']) -% % % pause(2) -% % % close all - - - disp([num2str(unit) '/' num2str(n_neurons)]) -end - -AxesLabels = behav_categ(boi); -combined_heatmap = nansum(cat(3,stat_diff_thresh{:}),3)./n_neurons; -figure; hp_all=heatmap(combined_heatmap, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -hp_all.XDisplayLabels = AxesLabels; hp_all.YDisplayLabels = AxesLabels; -caxis([0, 1]); -title(['Selectivity heatmap for all ' num2str(n_neurons) ' units, p<' num2str(p_thresh) ', min-occurences=' num2str(min_occurrences)]) -if with_NC == 0 - saveas(hp_all, [savePath '/Selectivity_heatmap/selectivity_heatmap_NoNC_units.png']) -elseif isolatedOnly - saveas(hp_all, [savePath '/Selectivity_heatmap/selectivity_heatmap_isolated_units.png']) -else - saveas(hp_all, [savePath '/Selectivity_heatmap/selectivity_heatmap_all_units.png']) -end - -AxesLabels = behav_categ(boi); -combined_heatmap = nansum(cat(3,stat_diff_thresh_rand{:}),3); -figure; hp_all=heatmap(combined_heatmap, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -hp_all.XDisplayLabels = AxesLabels; hp_all.YDisplayLabels = AxesLabels; -caxis([0, 1]); -title(['RANDOMIZED Selectivity heatmap for all ' num2str(n_neurons) ' units, p<' num2str(p_thresh) ', min-occurences=' num2str(min_occurrences)]) -if with_NC == 0 - saveas(hp_all, [savePath '/Selectivity_heatmap/selectivity_heatmap_NoNC_units_shuffled.png']) -elseif isolatedOnly - saveas(hp_all, [savePath '/Selectivity_heatmap/selectivity_heatmap_isolated_units_shuffled.png']) -else - saveas(hp_all, [savePath '/Selectivity_heatmap/selectivity_heatmap_all_units_shuffled.png']) -end - diff --git a/Single neuron/old_code/log_SingleNeuronTuning.m b/Single neuron/old_code/log_SingleNeuronTuning.m deleted file mode 100644 index d6113ad..0000000 --- a/Single neuron/old_code/log_SingleNeuronTuning.m +++ /dev/null @@ -1,270 +0,0 @@ -%% Log_FiringRatePerBehavior -% This script generates firing rate of individual neuron under different -% behavioral conditions (considering individual secons as independent). - -% Load data -%Set path -is_mac = 1; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -clearvars -except savePath filePath is_mac - -%Set parameters -temp_resolution = 1; -channel_flag = "all"; -with_NC =1; isolatedOnly=1; - -%Get data with specified temporal resolution and channels -[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - -session_length = size(Spike_rasters,2); % get session length - -%eliminate units with very low firing rate -mean_firing_rate = mean(Spike_rasters,2); -min_firing_units = find(mean_firing_rate>0); -Spike_rasters_final = Spike_rasters(min_firing_units,:) ; - -%Define "baseline" neural firing. -behavior_labels = cell2mat({labels{:,3}}');%Get behavior label -behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). -%Plot where baseline firing is taken from. Make sure its well spread out -%over the session. -idx_rest=find(behavior_labels==length(behav_categ)); -y=zeros(1, session_length); y(idx_rest)=1; -figure; plot(1:session_length, y); ylim([-1, 2]) - -baseline_firing = mean(Spike_rasters(:,idx_rest),2); -find(abs(mean_firing_rate- baseline_firing)>2)% check difference between means. - -%Standardize Unit rasters -Spike_raster_zscore = zscore(Spike_rasters,0, 2); %Z-score -Spike_raster_meandivided = Spike_rasters_final./mean_firing_rate; %Divide by the mean -Spike_raster_relative2baseline = Spike_rasters_final./baseline_firing; %Divide by baseline firing - -%Set parameters -unqLabels = 1:max(behavior_labels)-1; %Get unique behavior labels -n_neurons = size(Spike_rasters_final,1); %Get number of neurons -n_behav = length(unqLabels); %Get number of unique behavior labels -min_occurrences =90; %set the minimum number of occurrences (i.e. seconds where behavior occurs) -set_occurrences =90; - -%RESPONSIVENESS ANALYSIS: test whether firing rate for each behavior is -%different than the mean - -%Get firing rate per behavior -response_matrix = cell(n_neurons,n_behav); response_matrix_shuffle = cell(n_neurons,n_behav); %initialize response matrix -med_response_matrix = zeros(n_neurons,n_behav); %initialize median response matrix -mean_response_matrix = zeros(n_neurons,n_behav); %initialize mean response matrix -sd_response_matrix = zeros(n_neurons,n_behav); %initialize sd response matrix -h = nan(n_neurons,n_behav); h_shuffle = nan(n_neurons,n_behav); -group = cell(1,n_behav); %initialize group label matrix -p_thresh_array = [0.01 0.005 0.001 0.0001 0.00001]; p=1; p_thresh=0.001; -responsiveness_test_perneuron = cell(1,length(p_thresh_array)); -responsiveness_test_perbehav = cell(1,length(p_thresh_array)); - -for p_thresh = p_thresh_array - for unit = 1:n_neurons %for all units - for b = 1:n_behav %for all behaviors - idx = find(behavior_labels == unqLabels(b)); %get idx where behavior b occurred - n_per_behav(b)=length(idx); - response_matrix{unit, b} = Spike_raster_relative2baseline(unit, idx); %Spike_raster_zscore(unit, idx); %get firing rate response during behavior "b" - %response_matrix_shuffle{unit, b} = Spike_raster_relative2baseline(unit, randsample(1:length(behavior_labels),length(idx))); - response_matrix_shuffle{unit, b} = Spike_raster_relative2baseline(unit, randsample(idx_rest,length(idx))); - - mean_response_matrix(unit,b) = mean(response_matrix{unit, b}); %extract mean response for all epochs where behavior "b" occurred - med_response_matrix(unit,b) = median(response_matrix{unit, b});%extract median response for all epochs where behavior "b" occurred - sd_response_matrix(unit,b) = std(response_matrix{unit, b});%extract sd response for all epochs where behavior "b" occurred - group{1,b} = b* ones(1,length(idx)); %Keep track of behavior - - if length(idx)>min_occurrences %if behavior occurred during at least 40 seconds - %figure; hist(response_matrix{unit, b}, 10); pause(1); close all %check distribution of sample - if length(idx)>=set_occurrences - sampled_data = response_matrix{unit, b}(randsample(length(idx),set_occurrences)); - sampled_data_shuffled = response_matrix_shuffle{unit, b}(randsample(length(idx),set_occurrences)); - h(unit,b) = ttest(sampled_data,1,'Alpha',p_thresh); %test if mean is different than 1. For now keep test of mean as ttest, but should be changed for a test assuming a poisson distribution - h_shuffle(unit,b) = ttest(sampled_data_shuffled,1,'Alpha',p_thresh); - else - sampled_data = response_matrix{unit, b}(randsample(length(idx),set_occurrences,true)); - sampled_data_shuffled = response_matrix_shuffle{unit, b}(randsample(length(idx),set_occurrences,true)); - h(unit,b) = ttest(sampled_data,1,'Alpha',p_thresh); %test if mean is different than 1. For now keep test of mean as ttest, but should be changed for a test assuming a poisson distribution - h_shuffle(unit,b) = ttest(sampled_data_shuffled,1,'Alpha',p_thresh); - end - else - h(unit,b)=nan; - h_shuffled(unit,b)=nan; - end - end - end - responsiveness_test_perneuron{p} = [nansum(h,2) nansum(h_shuffle,2)]; - total_selective(p)=sum(responsiveness_test_perneuron{p}(:,1)); - num_nonresponsive(p) = length(find(responsiveness_test_perneuron{p}(:,1)==0)); %Find number of units that are not responsive to any behavior - total_falsepos(p)=sum(responsiveness_test_perneuron{p}(:,2)); - - responsiveness_test_perbehav{p} = [n_per_behav; nansum(h,1); nansum(h_shuffle,1)]; - p=p+1; - -end - -%Check the proportion of false positives for each threshold -total_falsepos./total_selective -%Based on the proportion of false positives, I selected the threshold p=0.001. - -%plot the number of selective units per behavior -figure; hold on; set(gcf,'Position',[150 250 1300 500]); -for p=1:length(p_thresh_array) - plot(1:length(responsiveness_test_perbehav{p}(2,:)), responsiveness_test_perbehav{p}(2,:)/n_neurons, '-o', 'LineWidth',2) - plot(1:length(responsiveness_test_perbehav{p}(3,:)), responsiveness_test_perbehav{p}(3,:)/n_neurons, 'LineStyle','--','Color','k') -end -lgd=legend(["0.01","", "0.005","", "0.001","", "0.0001","", "0.00001","Shuffled"]); lgd.Title.String = 'p-value threshold'; -xticks(1:length(nansum(h,1))); xticklabels(behav_categ(1:end-1)); xtickangle(45); ylim([0 1]) -xlabel("Behaviors"); ylabel('Proportion of responsive units'); title(['Number of responsive units per behavior, at multiple thresholds, min-occrruence=' num2str(min_occurrences)]) -if with_NC == 0 - saveas(gcf, [savePath '/Responsiveness_per_behavior_NoNC_units.png']) -elseif isolatedOnly - saveas(gcf, [savePath '/Responsiveness_per_behavior_isolated_units.png']) -else - saveas(gcf, [savePath '/Responsiveness_per_behavior_all_units.png']) -end - -% % %Get correlation between number of responsive units and number of trials at -% % %threshold p=0.001 -% % p=4; figure; h=scatter(responsiveness_test_perbehav{p}(1,:), responsiveness_test_perbehav{p}(2,:)); -% % [r p]=corrcoef(responsiveness_test_perbehav{p}(1,:), responsiveness_test_perbehav{p}(2,:)) - -% % %Code to check the number of false positives according to the number of -% % %"trials" (or seconds) in a behavior -% % responsiveness_test_perbehav = [n_per_behav; nansum(h,1); nansum(h_shuffle,1)]; responsiveness_test_perbehav(:,responsiveness_test_perbehav(1,:)==0)=[]; -% % figure; h=scatter(responsiveness_test_perbehav(1,:), responsiveness_test_perbehav(3,:)); -% % corrcoef(responsiveness_test_perbehav(1,:), responsiveness_test_perbehav(3,:)) -% % %Based on the correlation between false positives and number of trials -% % %considered, I exclude trials that occured for less than 40sec during the -% % %session. - -%SELECTIVITY analysis: Test difference in firing rate between behaviors. - -%Select subset of behaviors with a minimum occurrence -behav_freq_table = tabulate(behavior_labels); -behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) -boi = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences -%boi = [4:10,16:17,20:23];%[4:6,11,17]; %select behaviors manually - -%Initialize -total_iter = 10; -preferred_behav = strings(n_neurons, total_iter, length(boi), length(boi)); group_label_full =[]; -p = nan(n_neurons, total_iter, length(boi), length(boi)); unit = 1; -p_rand = nan(n_neurons, total_iter, length(boi), length(boi)); -subsample = 1; %If subsample or resample observations. 1 subsample; 0 no subsampling. -p_thresh = 0.01; - -for unit = 1:n_neurons %randsample(1:n_neurons, n_neurons) %for all units - - for iter = 1:total_iter - - for b1 = 1:length(boi)-1 - - for b2 = b1+1:length(boi) - - behav = [boi(b1),boi(b2)]; - median_resp = med_response_matrix(unit,behav); %Get median response to order plot - resp_mat = {response_matrix{unit,behav}}; - n_per_behav = cell2mat(cellfun(@length,resp_mat,'uni',false)); - - subgroup = {group{1,behav}}; - sub_behav_categ = {behav_categ{behav}}; - - group_label_full =[]; - if subsample %if balance observations across behaviors - for b = 1:length(behav) %balance observations for all behaviors - group_label =[]; - n_obs = length(resp_mat{b}); - if n_obs>=set_occurrences - resp_mat{b} = resp_mat{b}(randsample(1:n_obs,set_occurrences)); - else - resp_mat{b} = resp_mat{b}(randsample(1:n_obs,set_occurrences, true)); - end - median_resp(b) = median(resp_mat{b}); - if n_obs>0 - group_label = categorical(repelem({behav_categ{behav(b)}}, [set_occurrences*ones(1,length(behav(b)))])); - end - group_label_full = [group_label_full,group_label]; - end - group_label_final = group_label_full; - else %if don't balance observations - group_label_final = categorical({behav_categ{[subgroup{1,:}]'}}); - end - - %sort according to median response - [~,idx]=sort(median_resp,'descend'); - preferred_behav(unit, iter, b1, b2) = sub_behav_categ{idx(1)}; - groupDescend = reordercats(group_label_final,{sub_behav_categ{idx}}); - randgroupDescend = randsample(groupDescend, length(groupDescend)); %randomize for "chance level" - - %Run non-parametric anova (issue that samples are not independent...) - %[p_ks(unit), tbl_ks, stats_ks]= kruskalwallis([resp_mat{1,:}]',groupDescend,'off'); - [p(unit, iter, b1, b2), tbl, stats]= kruskalwallis([resp_mat{1,:}]',groupDescend,'off'); - [p_rand(unit, iter, b1, b2), tbl, stats]= kruskalwallis([resp_mat{1,:}]',randgroupDescend,'off'); - - end - end - %disp([num2str(iter) '/' num2str(total_iter)]) - - end - - stat_diff{unit}=squeeze(mean(squeeze(p(unit, :, :, :)),1)); - stat_diff_thresh{unit} = stat_diff{unit}; stat_diff_thresh{unit}(stat_diff{unit}<=p_thresh) =1; stat_diff_thresh{unit}(stat_diff{unit}>p_thresh)=0; - - stat_diff_rand{unit}=squeeze(mean(squeeze(p_rand(unit, :, :, :)),1)); - stat_diff_thresh_rand{unit} = stat_diff_rand{unit}; stat_diff_thresh_rand{unit}(stat_diff_rand{unit}<=p_thresh) =1; stat_diff_thresh_rand{unit}(stat_diff_rand{unit}>p_thresh)=0; - - AxesLabels = behav_categ(boi); -% % % figure; hp=heatmap(stat_diff_thresh{unit}, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -% % % hp.XDisplayLabels = AxesLabels; hp.YDisplayLabels = AxesLabels; -% % % title(['Selectivity heatmap for unit #' num2str(unit)]) -% % % saveas(hp, [savePath '/Selectivity_heatmap/selectivity_heatmap_unit_' num2str(unit) '.png']) -% % % pause(2) -% % % close all - - - disp([num2str(unit) '/' num2str(n_neurons)]) -end - -AxesLabels = behav_categ(boi); -combined_heatmap = nansum(cat(3,stat_diff_thresh{:}),3)./n_neurons; -figure; hp_all=heatmap(combined_heatmap, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -hp_all.XDisplayLabels = AxesLabels; hp_all.YDisplayLabels = AxesLabels; -caxis([0, 1]); -title(['Selectivity heatmap for all ' num2str(n_neurons) ' units, p<' num2str(p_thresh) ', min-occurences=' num2str(min_occurrences)]) -if with_NC == 0 - saveas(hp_all, [savePath '/Selectivity_heatmap/selectivity_heatmap_NoNC_units.png']) -elseif isolatedOnly - saveas(hp_all, [savePath '/Selectivity_heatmap/selectivity_heatmap_isolated_units.png']) -else - saveas(hp_all, [savePath '/Selectivity_heatmap/selectivity_heatmap_all_units.png']) -end - -AxesLabels = behav_categ(boi); -combined_heatmap = nansum(cat(3,stat_diff_thresh_rand{:}),3); -figure; hp_all=heatmap(combined_heatmap, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " "); -hp_all.XDisplayLabels = AxesLabels; hp_all.YDisplayLabels = AxesLabels; -caxis([0, 1]); -title(['RANDOMIZED Selectivity heatmap for all ' num2str(n_neurons) ' units, p<' num2str(p_thresh) ', min-occurences=' num2str(min_occurrences)]) -if with_NC == 0 - saveas(hp_all, [savePath '/Selectivity_heatmap/selectivity_heatmap_NoNC_units_shuffled.png']) -elseif isolatedOnly - saveas(hp_all, [savePath '/Selectivity_heatmap/selectivity_heatmap_isolated_units_shuffled.png']) -else - saveas(hp_all, [savePath '/Selectivity_heatmap/selectivity_heatmap_all_units_shuffled.png']) -end - diff --git a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd.m b/Single neuron/old_code/log_SingleNeuronTuning_Cohensd.m deleted file mode 100644 index 346fd48..0000000 --- a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd.m +++ /dev/null @@ -1,217 +0,0 @@ -%% Log_SingleNeuronTuning_Cohensd -% This script computes firing rate of individual neuron under different -% behavioral conditions. Then, it computes a cohen's d (or effect size) -% difference between the distribution of firing rates during behavior X -% with a baseline firing rate. - -%Set path -is_mac = 1; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -clearvars -except savePath filePath is_mac - -%% Set paramaters and load data - -%Set parameters -temp_resolution = 1; -channel_flag = "all"; -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; - -%Get data with specified temporal resolution and channels -%[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final]= log_GenerateDataToRes_function_basic(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); -[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - -session_length = size(Spike_rasters,2); % get session length - -%Extract behavior labels -behavior_labels = cell2mat({labels{:,3}}');%Get behavior label from labels structure -behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - -%Set parameters -unqLabels = 1:max(behavior_labels)-1; %Get unique behavior labels (exclude rest) -n_neurons = size(Spike_rasters,1); %Get number of neurons -n_behav = length(unqLabels); %Get number of unique behavior labels - -%Estimate "baseline" neural firing distribution. -idx_rest=find(behavior_labels==length(behav_categ));%Get idx of "rest" epochs. -mean_baseline = mean(Spike_rasters(:,idx_rest),2); -std_baseline = std(Spike_rasters(:,idx_rest),0,2); - -% %Check visually that baseline is taken from epochs throughout the session -% y=zeros(1, session_length); y(idx_rest)=1; -% figure; plot(1:session_length, y); ylim([-0.5, 1.5]) -% yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) -% xlabel('Time in s'); title('Baseline epochs') - -%% Compute cohen's d - -n_per_behav = nan(n_behav,1); -cohend = nan(n_neurons,n_behav); -cohend_shuffle = nan(n_neurons,n_behav); -mean_beh = nan(n_neurons, n_behav); -mean_beh_shuffle = nan(n_neurons, n_behav); -std_beh = nan(n_neurons, n_behav); -std_beh_shuffle = nan(n_neurons, n_behav); -p = nan(n_neurons, n_behav); -p_rand = nan(n_neurons, n_behav); - -for n = 1:n_neurons - - for b = 1:n_behav - idx = find(behavior_labels == unqLabels(b)); %get idx where behavior b occurred - n_per_behav(b)=length(idx); - - if n_per_behav(b)>10 - - if length(idx)=min_occurrences,1);%Get behaviors with a min number of occurrences - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - %behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - - - % OR select behaviors manually - % behav = [4:10,16,23];%[4,5,17];% [7,8]%[5,7:10,21];%[4,5,7:10];%[4:8,17]; %[1:6,9:11,16,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only keep the behaviors of interest - idx = find(ismember(behavior_labels,behav)); %find the indices of the behaviors considered - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx,:);%Same as above but in behavior labels - tabulate(removecats(categorical(behavior_labels_final))); - - - social_labels_final = ones(size(behavior_labels_final)); - social_labels_final(ismember(behavior_labels_final, social_set)) = 2; - - %% Set parameters - unqLabels = 1:max(behavior_labels)-1; %Get unique behavior labels (exclude rest) - n_neurons(s) = size(Spike_rasters,1); %Get number of neurons - n_behav = length(unqLabels); %Get number of unique behavior labels - - %Estimate "baseline" neural firing distribution. - idx_rest=find(behavior_labels==length(behav_categ));%Get idx of "rest" epochs. - mean_baseline = mean(Spike_rasters(:,idx_rest),2); - std_baseline = std(Spike_rasters(:,idx_rest),0,2); - - %Check visually that baseline is taken from epochs throughout the session - if plot_toggle - y=zeros(1, session_length); y(idx_rest)=1; - figure; plot(1:session_length, y); ylim([-0.5, 1.5]) - yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) - xlabel('Time in s'); title('Baseline epochs') - set(gca,'FontSize',15); - saveas(gcf, [savePath '/Baseline_epochs.png']); pause(2); close all - end - - %% Compute cohen's d - - cohend = nan(n_neurons(s),n_behav); - cohend_shuffle = nan(n_neurons(s),n_behav); - mean_beh = nan(n_neurons(s), n_behav); - mean_beh_shuffle = nan(n_neurons(s), n_behav); - std_beh = nan(n_neurons(s), n_behav); - std_beh_shuffle = nan(n_neurons(s), n_behav); - p = nan(n_neurons(s), n_behav); - p_rand = nan(n_neurons(s), n_behav); - - for n = 1:n_neurons(s) - - for b = 1:n_behav - idx = find(behavior_labels == unqLabels(b)); %get idx where behavior b occurred - n_per_behav(s,b)=length(idx); - - if n_per_behav(s,b)>min_occurrence - - if length(idx) cohend_cutoff & p_sorted < p_cutoff); sum(sum(h_sorted)) - h_shuffle_sorted = double(abs(cohend_shuffle_sorted) > cohend_cutoff & p_rand_sorted < p_cutoff); sum(sum(h_shuffle_sorted)) - cohend_thresh_sorted = h_sorted.*cohend_sorted; cohend_thresh_sorted(cohend_thresh_sorted==0)=nan; - cohend_shuffle_thresh_sorted = h_shuffle_sorted.*cohend_shuffle_sorted; cohend_shuffle_thresh_sorted(cohend_shuffle_thresh_sorted==0)=nan; - - - %Threshold cohens'd by a cohen's d AND p-value cutoff - h = double(abs(cohend) > cohend_cutoff & p < p_cutoff); sum(sum(h)) - h_shuffle = double(abs(cohend_shuffle) > cohend_cutoff & p_rand < p_cutoff); sum(sum(h_shuffle)) - - cohend_thresh = h.*cohend; cohend_thresh(cohend_thresh==0)=nan; - cohend_shuffle_thresh = h_shuffle.*cohend_shuffle; cohend_shuffle_thresh(cohend_shuffle_thresh==0)=nan; - - - %% Plot heatmaps - - AxesLabels_sorted = behav_categ(orderIdx); - AxesLabels = behav_categ(1:end-1); - caxis_upper = 1.5; - caxis_lower = -1.5; - cmap=flipud(cbrewer('div','RdBu', length(caxis_lower:0.01:caxis_upper))); - - if plot_toggle - %Plot ordered heatmap - [nanrow nancol]=find(~isnan(cohend_sorted)); nancol = unique(nancol); - hp=heatmap(cohend_sorted(:,nancol), 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted(nancol); caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap']) - ax = gca; - ax.FontSize = 14; - saveas(gcf, [savePath '/Cohend_heatmap_sorted.png']); close all - - %Plot ordered heatmap thresholded - figure; set(gcf,'Position',[150 250 1000 500]); - hp=heatmap(cohend_thresh_sorted, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - saveas(gcf, [savePath '/Cohend_heatmap_sorted_thresholded.png']); close all - - %Includes both p-value and cohen d as thresholds - figure; hold on; set(gcf,'Position',[150 250 1500 800]); - subplot(2,2,1); hp=heatmap(cohend, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap') - subplot(2,2,2); hp=heatmap(cohend_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - subplot(2,2,3); hp=heatmap(cohend_shuffle, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap SHUFFLED') - subplot(2,2,4); hp=heatmap(cohend_shuffle_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap SHUFFLED, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - saveas(gcf, [savePath '/Cohend_heatmap_all_units.png']) - end - - % if with_NC == 0 - % sgtitle(['Cohens-d heatmap for all units except noise cluster']) - % saveas(gcf, [savePath '/Cohend_heatmap_NoNC_units.png']) - % elseif with_NC == 2 - % sgtitle(['Cohens-d heatmap for noise clusters ONLY']) - % saveas(gcf, [savePath '/Cohend_heatmap_NC_only.png']) - % elseif isolatedOnly - % sgtitle(['Cohens-d heatmap for isolated units']) - % saveas(gcf, [savePath '/Cohend_heatmap_isolated_units.png']) - % else - % sgtitle(['Cohens-d heatmap for all units']) - % saveas(gcf, [savePath '/Cohend_heatmap_all_units.png']) - % end - - %pause(2); - close all - - %%%%%%%%%%%%%%%%%%%%%%%%%%%% - %% Summary figures - - % % % % figure; scatter(nanmean(abs(cohend_thresh)), n_per_behav(s,:)); corrcoef(nanmean(abs(cohend_thresh)), n_per_behav(s,:),'rows','pairwise'); - % % % % figure; scatter(sum(~isnan(cohend_thresh)), n_per_behav(s,:)); corrcoef(sum(~isnan(cohend_thresh)), n_per_behav(s,:),'rows','pairwise'); - - mean_cohend_per_behav(s,:) = nanmean(cohend_thresh); - median_cohend_per_behav(s,:) = nanmedian(cohend_thresh); - std_cohend_per_behav(s,:) = nanstd(cohend_thresh); - se_cohend_per_behav(s,:) = nanstd(cohend_thresh)./sqrt(sum(~isnan(cohend_thresh))); - prop_selective_per_behav(s,:) = sum(~isnan(cohend_thresh))/n_neurons(s); - num_selective_behav_per_neuron{s} = sum(~isnan(cohend_thresh),2); - - if plot_toggle - %Plot the distribution of effect sizes for each behavior - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_cohend_per_behav(s,:)); - boxplot(cohend_thresh(:,idx_sort)) - % scatter(1:length(idx_sort),mean_cohend_per_behav(idx_sort),60,'filled') - % errorbar(mean_cohend_per_behav(idx_sort), std_cohend_per_behav(idx_sort),'LineWidth',1.5) - % legend({'mean','standard deviation'},'Location','best') - ylim([-2.5 2.5]); xlim([0 n_behav+1]) - ylabel(['Cohens-d, p<' num2str(p_cutoff)]) - yline(0,'LineStyle','--') - text(1,1.75,'Increased firing relative to baseline','FontSize',14) - text(1,-1.75,'Decreased firing relative to baseline','FontSize',14) - xticks(1:n_behav) - xticklabels(AxesLabels(idx_sort)) - set(gca,'FontSize',15); - title('Distribution of effect size per behavior') - saveas(gcf, [savePath '/Distribution_cohend_per_behavior.png']); %pause(2); close all - - %Plot the proprotion of selective neurons per behavior - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~,idx_sort]=sort(prop_selective_per_behav(s,:),'descend'); - scatter(1:n_behav,prop_selective_per_behav(s,idx_sort),60,'filled') - ylabel('Prop. selective units') - xticks(1:n_behav); xlim([0 n_behav+1]); ylim([0 1]) - xticklabels(AxesLabels(idx_sort)) - set(gca,'FontSize',15); - title(['Proportion of selective units per behavior']) - saveas(gcf, [savePath '/Proportion_units_selective_per_behav.png']); %pause(2); close all - - % Variance in single neuron selectivity - mean_cohend_per_neuron = nanmean(cohend_thresh,2); - std_cohend_per_neuron = nanstd(cohend_thresh,0,2); - - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_cohend_per_neuron); - scatter(1:length(idx_sort),mean_cohend_per_neuron(idx_sort),20,'filled') - errorbar(mean_cohend_per_neuron(idx_sort), std_cohend_per_neuron(idx_sort),'LineWidth',1.5) - legend({'mean','standard deviation'},'Location','best') - ylim([-2 2]); xlim([0 n_neurons(s)+1]) - ylabel(['Cohens-d, p<' num2str(p_cutoff)]); xlabel('Units') - yline(0,'LineStyle','--') - text(10,1.5,'Increased firing relative to baseline','FontSize',14) - text(10,-1.5,'Decreased firing relative to baseline','FontSize',14) - set(gca,'FontSize',15); - title('Distribution of effect size across all units') - saveas(gcf, [savePath '/Distribution_cohend_all_units.png']); pause(2); close all - - %Number of behaviors a single neuron is selective for - figure; histogram(num_selective_behav_per_neuron{s}) - xlabel('Number of behavior a given neuron is selective to') - title('Distribution of the number of behaviors single units are selective for') - saveas(gcf, [savePath '/Distribution_number_selective_behavior_per_unit.png']); %pause(2); close all - - end - - % % %%%%%%%%%%%%%%%%%%%%%%%% - % % %Single neuron selectivity: - % % figure; set(gcf,'Position',[150 250 1000 300]); - % % data = cohend_thresh(1,:); - % % [~, idx_sorted]=sort(data); - % % scatter(1:n_behav, cohend_thresh(1,idx_sorted),40, 'filled') - % % ylim([caxis_lower caxis_upper]); xlim([0 n_behav+1]) - % % ylabel(['Cohens-d, p<' num2str(cutoff)]) - % % yline(0,'LineStyle','--') - % % xticks(1:n_behav) - % % xticklabels(AxesLabels(idx_sorted)) - % % set(gca,'FontSize',15); - - close all - -end - -%% Results across sessions - -%Change savePath for all session results folder: -savePath = [home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SingleUnit_results/']; - -%Plot mean effect size per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(mean_cohend_per_behav)); -for s = a_sessions - scatter(1:length(idx_sort),mean_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) - %pause(1) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([-2 2]); xlim([0 n_behav+1]) -ylabel(['Mean Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - scatter(1:length(idx_sort),mean_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) - %pause(1) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([-2 2]); xlim([0 n_behav+1]) -ylabel(['Mean Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey H') - -sgtitle('Mean effect sizes per behavior','FontSize',20) -%saveas(gcf, [savePath '/Mean_effect_size_per_behavior.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot median effect size per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(median_cohend_per_behav)); -for s = a_sessions - scatter(1:length(idx_sort),median_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) - %pause(1) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([-2 2]); xlim([0 n_behav+1]) -ylabel(['Median Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - scatter(1:length(idx_sort),median_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) - %pause(1) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([-2 2]); xlim([0 n_behav+1]) -ylabel(['Median Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey H') - -sgtitle('Median effect sizes per behavior','FontSize',20) -%saveas(gcf, [savePath '/Median_effect_size_per_behavior.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot proportion of selective units per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(prop_selective_per_behav)); -for s = a_sessions - prop_selective_per_behav(s,prop_selective_per_behav(s,:)==0)=nan; - scatter(1:length(idx_sort),prop_selective_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - prop_selective_per_behav(s,prop_selective_per_behav(s,:)==0)=nan; - scatter(1:length(idx_sort),prop_selective_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey H') -sgtitle('Proportion of selective units per behavior','FontSize',20) -%saveas(gcf, [savePath '/Proportion_selective_units_per_behavior.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot number of behaviors a single neuron is selective for across all sessions, separated by monkey - -figure; set(gcf,'Position',[150 250 1000 700]); -subplot(2,1,1); hold on -for s=a_sessions - histogram(num_selective_behav_per_neuron{s}, 'FaceAlpha',0.3) - prop_not_tuned(s) = length(find(num_selective_behav_per_neuron{s}==0))/n_neurons(s); - prop_tuned_more_than_one_behav(s) = length(find(num_selective_behav_per_neuron{s}>0))/n_neurons(s); -end -legend({sessions(a_sessions).name},'Location','eastoutside') -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2); hold on -for s=h_sessions - histogram(num_selective_behav_per_neuron{s}, 'FaceAlpha',0.3) - prop_not_tuned(s) = length(find(num_selective_behav_per_neuron{s}==0))/n_neurons(s); - prop_tuned_more_than_one_behav(s) = length(find(num_selective_behav_per_neuron{s}>0))/n_neurons(s); -end -legend({sessions(h_sessions).name},'Location','eastoutside') -xlabel('Number of behaviors a given neuron is selective for') -set(gca,'FontSize',15); -title('Monkey H') - -sgtitle('Distribution of the number of behaviors single units are selective for','FontSize',20) -%saveas(gcf, [savePath '/Number_selective_behavior_per_unit.png']); pause(2); close all - -mean(prop_not_tuned(prop_not_tuned>0)) -mean(prop_tuned_more_than_one_behav(prop_tuned_more_than_one_behav>0)) \ No newline at end of file diff --git a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_b2b_batch.m b/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_b2b_batch.m deleted file mode 100644 index 3c5f3f4..0000000 --- a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_b2b_batch.m +++ /dev/null @@ -1,186 +0,0 @@ -%% Log_SingleNeuronTuning_Cohensd -% This script computes firing rate of individual neuron under different -% behavioral conditions. Then, it computes a cohen's d (or effect size) -% difference between the distribution of firing rates during behavior X -% with a baseline firing rate. - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -min_occurrence =10; -cohend_cutoff=0.3; p_cutoff=0.01;%Set thresholds - -%Initialize session batch variables: -n_behav = 28; -mean_cohend_per_behav = nan(length(sessions), n_behav); -std_cohend_per_behav = nan(length(sessions), n_behav); -se_cohend_per_behav = nan(length(sessions), n_behav); -prop_selective_per_behav = nan(length(sessions), n_behav); -num_selective_behav_per_neuron=cell(1,length(sessions)); -n_per_behav = nan(length(sessions),n_behav); - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/']; - - %% Load data - - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - session_length = size(Spike_rasters,2); % get session length - - %Extract behavior labels - behavior_labels = cell2mat({labels{:,3}}');%Get behavior label from labels structure - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - - %% Set parameters - unqLabels = 1:max(behavior_labels)-1; %Get unique behavior labels (exclude rest) - n_neurons(s) = size(Spike_rasters,1); %Get number of neurons - n_behav = length(unqLabels); %Get number of unique behavior labels - - %Estimate "baseline" neural firing distribution. - idx_rest=find(behavior_labels==length(behav_categ));%Get idx of "rest" epochs. - mean_baseline = mean(Spike_rasters(:,idx_rest),2); - std_baseline = std(Spike_rasters(:,idx_rest),0,2); - -% % %Check visually that baseline is taken from epochs throughout the session -% % y=zeros(1, session_length); y(idx_rest)=1; -% % figure; plot(1:session_length, y); ylim([-0.5, 1.5]) -% % yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) -% % xlabel('Time in s'); title('Baseline epochs') -% % set(gca,'FontSize',15); -% % saveas(gcf, [savePath '/Baseline_epochs.png']); pause(2); close all - - %% Compute cohen's d - - cohend = nan(n_neurons(s),n_behav,n_behav); - cohend_shuffle = nan(n_neurons(s),n_behav,n_behav); - mean_beh = nan(n_neurons(s), n_behav,n_behav); - mean_beh_shuffle = nan(n_neurons(s), n_behav,n_behav); - std_beh = nan(n_neurons(s), n_behav,n_behav); - std_beh_shuffle = nan(n_neurons(s), n_behav,n_behav); - p = nan(n_neurons(s), n_behav,n_behav); - p_rand = nan(n_neurons(s), n_behav,n_behav); - - for n = 1:n_neurons(s) - - for b1 = 1:n_behav - idx_b1 = find(behavior_labels == unqLabels(b1)); %get idx where behavior b occurred - n_per_behav(s,b1)=length(idx_b1); - - for b2=b1:n_behav - idx_b2 = find(behavior_labels == unqLabels(b2)); - - if n_per_behav(s,b1)>min_occurrence && n_per_behav(s,b2)>min_occurrence && b1~=b2 - - %Get indices for shuffled analysis - if length(idx_b1) cohend_cutoff & p_temp < p_cutoff); sum(sum(h)) - h_shuffle = double(abs(cohend_shuffle_temp) > cohend_cutoff & p_rand_temp < p_cutoff); sum(sum(h_shuffle)) - - cohend_thresh = h.*cohend_temp; cohend_thresh(cohend_thresh==0)=nan; - cohend_shuffle_thresh = h_shuffle.*cohend_shuffle_temp; cohend_shuffle_thresh(cohend_shuffle_thresh==0)=nan; - - - %% Plot heatmaps - - AxesLabels_sorted = behav_categ(orderIdx); - AxesLabels = behav_categ(1:end-1); - caxis_upper = 1.5; - caxis_lower = -1.5; - cmap=flipud(cbrewer('div','RdBu', length(caxis_lower:0.01:caxis_upper))); - - %Includes both p-value and cohen d as thresholds - figure; hold on; set(gcf,'Position',[150 250 1500 800]); - subplot(2,2,1); hp=heatmap(cohend_temp, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = AxesLabels; title('Cohens-d heatmap') - subplot(2,2,2); hp=heatmap(cohend_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = AxesLabels; title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - subplot(2,2,3); hp=heatmap(cohend_shuffle_temp, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = AxesLabels; title('Cohens-d heatmap SHUFFLED') - subplot(2,2,4); hp=heatmap(cohend_shuffle_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = AxesLabels; title(['Cohens-d heatmap SHUFFLED, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - - pause(2) - close all - %saveas(gcf, [savePath '/Cohend_heatmap_all_units.png']) - end - -end - diff --git a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_batch_perArea.m b/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_batch_perArea.m deleted file mode 100644 index 7fd8f6b..0000000 --- a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_batch_perArea.m +++ /dev/null @@ -1,420 +0,0 @@ -%% Log_SingleNeuronTuning_Cohensd -% This script computes firing rate of individual neuron under different -% behavioral conditions. Then, it computes a cohen's d (or effect size) -% difference between the distribution of firing rates during behavior X -% with a baseline firing rate. - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -plot_toggle =0; - -%Initialize session batch variables: -n_behav = 28; -mean_cohend_per_behav_vlPFC = nan(length(sessions), n_behav); -mean_cohend_per_behav_TEO = nan(length(sessions), n_behav); - -prop_selective_per_behav_vlPFC = nan(length(sessions), n_behav); -prop_selective_per_behav_TEO = nan(length(sessions), n_behav); - -num_selective_behav_per_neuron_vlPFC=cell(1,length(sessions)); -num_selective_behav_per_neuron_TEO=cell(1,length(sessions)); - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=11; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/']; - - chan = 1; - for channel_flag = ["vlPFC", "TEO"] - - %% Load data - - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - session_length = size(Spike_rasters,2); % get session length - - %Extract behavior labels - behavior_labels = cell2mat({labels{:,3}}');%Get behavior label from labels structure - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - - %% Set parameters - unqLabels = 1:max(behavior_labels)-1; %Get unique behavior labels (exclude rest) - n_neurons(s) = size(Spike_rasters,1); %Get number of neurons - n_behav = length(unqLabels); %Get number of unique behavior labels - - %Estimate "baseline" neural firing distribution. - idx_rest=find(behavior_labels==length(behav_categ));%Get idx of "rest" epochs. - mean_baseline = mean(Spike_rasters(:,idx_rest),2); - std_baseline = std(Spike_rasters(:,idx_rest),0,2); - - %Check visually that baseline is taken from epochs throughout the session - y=zeros(1, session_length); y(idx_rest)=1; - figure; plot(1:session_length, y); ylim([-0.5, 1.5]) - yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) - xlabel('Time in s'); title('Baseline epochs') - set(gca,'FontSize',15); - saveas(gcf, [savePath '/Baseline_epochs.png']); close all - - %% Compute cohen's d - - - if chan ==1 - n=max(unit_count(1:2)); - n_per_behav = nan(2,n_behav,1); - cohend = nan(2,n,n_behav); - cohend_shuffle = nan(2,n,n_behav); - mean_beh = nan(2,n, n_behav); - mean_beh_shuffle = nan(2,n, n_behav); - std_beh = nan(2,n, n_behav); - std_beh_shuffle = nan(2,n, n_behav); - p = nan(2,n, n_behav); - p_rand = nan(2,n, n_behav); - end - - for n = 1:n_neurons(s) - - for b = 1:n_behav - idx = find(behavior_labels == unqLabels(b)); %get idx where behavior b occurred - n_per_behav(b)=length(idx); - - if n_per_behav(b)>10 - - if length(idx) cohend_cutoff & p(1,:,:) < p_cutoff); sum(sum(h_vlPFC)) - h_shuffle_vlPFC = double(abs(cohend_shuffle(1,:,:)) > cohend_cutoff & p_rand(1,:,:) < p_cutoff); sum(sum(h_shuffle_vlPFC)) - - h_TEO = double(abs(cohend(2,:,:)) > cohend_cutoff & p(2,:,:) < p_cutoff); sum(sum(h_TEO)) - h_shuffle_TEO = double(abs(cohend_shuffle(2,:,:)) > cohend_cutoff & p_rand(2,:,:) < p_cutoff); sum(sum(h_shuffle_TEO)) - - cohend_thresh_vlPFC = squeeze(h_vlPFC.*cohend(1,:,:)); cohend_thresh_vlPFC(cohend_thresh_vlPFC==0)=nan; - cohend_thresh_TEO = squeeze(h_TEO.*cohend(2,:,:)); cohend_thresh_TEO(cohend_thresh_TEO==0)=nan; - - %% Plot heatmaps - AxesLabels = behav_categ(orderIdx); - caxis_upper = 1.5; - caxis_lower = -1.5; - cmap=flipud(cbrewer('div','RdBu', length(caxis_lower:0.01:caxis_upper))); - - if plot_toggle - figure; hold on; set(gcf,'Position',[150 250 800 800]); - subplot(2,1,2); hp=heatmap(cohend_thresh_TEO, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff) ', TEO']) - subplot(2,1,1); hp=heatmap(cohend_thresh_vlPFC, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff) ', vlPFC']) - saveas(gcf, [savePath '/Cohend_heatmap_byArea.png']) - close all - end - - %%%%%%%%%%%%%%%%%%%%%%%%%%%% - %% Summary figures - - mean_cohend_per_behav_vlPFC(s,:) = nanmean(cohend_thresh_vlPFC); - mean_cohend_per_behav_TEO(s,:) = nanmean(cohend_thresh_TEO); - - prop_selective_per_behav_vlPFC(s,:) = sum(~isnan(cohend_thresh_vlPFC))/unit_count(1); - prop_selective_per_behav_TEO(s,:) = sum(~isnan(cohend_thresh_TEO))/unit_count(2); - - %Plot the distribution of effect sizes for each behavior - if plot_toggle - figure; hold on; set(gcf,'Position',[150 250 1200 500]); - [~, idx_sort]=sort(mean_cohend_per_behav_vlPFC(s,:)); - boxplot(cohend_thresh_vlPFC(:,idx_sort), 'Colors','b','OutlierSize',1, 'PlotStyle','compact') - boxplot(cohend_thresh_TEO(:,idx_sort), 'Colors','c','OutlierSize',1, 'PlotStyle','compact') - ylim([-2.5 2.5]); xlim([0 n_behav+1]) - ylabel(['Cohens-d, p<' num2str(p_cutoff)]) - yline(0,'LineStyle','--') - text(1,1.75,'Increased firing relative to baseline','FontSize',14) - text(1,-1.75,'Decreased firing relative to baseline','FontSize',14) - xticks(1:n_behav) - xticklabels(AxesLabels(idx_sort)) - set(gca,'FontSize',15); - title('Distribution of effect size per behavior') - saveas(gcf, [savePath '/Distribution_cohend_perBehav_perArea.png']); %pause(2); close all - - %Plot the proprotion of selective neurons per behavior - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~,idx_sort]=sort(prop_selective_per_behav_vlPFC(s,:),'descend'); - scatter(1:n_behav,prop_selective_per_behav_vlPFC(s,idx_sort),60,'filled', 'b') - scatter(1:n_behav,prop_selective_per_behav_TEO(s,idx_sort),60,'filled', 'c') - legend({'vlPFC','TEO'}) - ylabel('Prop. selective units') - xticks(1:n_behav); xlim([0 n_behav+1]); ylim([0 1]) - xticklabels(AxesLabels(idx_sort)) - set(gca,'FontSize',15); - title('Proportion of units selective per behavior') - saveas(gcf, [savePath '/Proportion_units_selective_perBehav_perArea.png']); %pause(2); close all - - end - - % Variance in single neuron selectivity - mean_cohend_per_neuron_vlPFC = nanmean(cohend_thresh_vlPFC,2); - std_cohend_per_neuron_vlPFC = nanstd(cohend_thresh_vlPFC,0,2); - mean_cohend_per_neuron_TEO = nanmean(cohend_thresh_TEO,2); - std_cohend_per_neuron_TEO = nanstd(cohend_thresh_TEO,0,2); - - if plot_toggle - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_cohend_per_neuron_vlPFC); - scatter(1:length(idx_sort),mean_cohend_per_neuron_vlPFC(idx_sort),20,'filled','b') - errorbar(mean_cohend_per_neuron_vlPFC(idx_sort), std_cohend_per_neuron_vlPFC(idx_sort),'LineWidth',1.5, 'Color','b') - [~, idx_sort]=sort(mean_cohend_per_neuron_TEO); - scatter(1:length(idx_sort),mean_cohend_per_neuron_TEO(idx_sort),20,'filled','c') - errorbar(mean_cohend_per_neuron_TEO(idx_sort), std_cohend_per_neuron_TEO(idx_sort),'LineWidth',1.5, 'Color','c') - legend({'mean','standard deviation'},'Location','best') - ylim([-2 2]); xlim([0 n_neurons(s)+1]) - ylabel(['Cohens-d, p<' num2str(p_cutoff)]); xlabel('Units') - yline(0,'LineStyle','--') - text(10,1.5,'Increased firing relative to baseline','FontSize',14) - text(10,-1.5,'Decreased firing relative to baseline','FontSize',14) - set(gca,'FontSize',15); - title('Distribution of effect size across all units') - saveas(gcf, [savePath '/Distribution_cohend_perArea.png']); pause(2); close all - end - - %Number of behaviors a single neuron is selective for - num_selective_behav_per_neuron_vlPFC{s} = sum(~isnan(cohend_thresh_vlPFC(1:unit_count(1),:)),2); - num_selective_behav_per_neuron_TEO{s} = sum(~isnan(cohend_thresh_TEO(1:unit_count(2),:)),2); - - if plot_toggle - figure; hold on - histogram(num_selective_behav_per_neuron_vlPFC{s}) - histogram(num_selective_behav_per_neuron_TEO{s}) - legend({'vlPFC','TEO'},'Location','best') - xlabel('Number of behavior a given neuron is selective to') - title('Distribution of the number of behaviors single units are selective for') - saveas(gcf, [savePath '/Distribution_number_selective_behavior_perUnit_perArea.png']); %pause(2); close all - end - % % %%%%%%%%%%%%%%%%%%%%%%%% - % % %Single neuron selectivity: - % % figure; set(gcf,'Position',[150 250 1000 300]); - % % data = cohend_thresh(1,:); - % % [~, idx_sorted]=sort(data); - % % scatter(1:n_behav, cohend_thresh(1,idx_sorted),40, 'filled') - % % ylim([caxis_lower caxis_upper]); xlim([0 n_behav+1]) - % % ylabel(['Cohens-d, p<' num2str(cutoff)]) - % % yline(0,'LineStyle','--') - % % xticks(1:n_behav) - % % xticklabels(AxesLabels(idx_sorted)) - % % set(gca,'FontSize',15); - - close all - - -end %end of session loop - -%% Results across sessions - -%Change savePath for all session results folder: -savePath = [home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SingleUnit_results/']; - -%Get correlation between brain areas -for s = session_range - [A B] = corrcoef(mean_cohend_per_behav_vlPFC(s,:), mean_cohend_per_behav_TEO(s,:),'Rows','complete'); - cor_TEOvlpfc(s) = A(1,2); - p_TEOvlpfc(s) = B(1,2); -end -mean(cor_TEOvlpfc(cor_TEOvlpfc>0)) - -%Plot distribution of effect size per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(mean_cohend_per_behav_vlPFC(a_sessions,:))); -%boxplot(mean_cohend_per_behav_vlPFC(a_sessions,idx_sort), 'Color','b',) -scatter(1:length(idx_sort),mean_cohend_per_behav_vlPFC(a_sessions,idx_sort),60,'filled','MarkerFaceAlpha',.7,'MarkerFaceColor','b') -%boxplot(mean_cohend_per_behav_TEO(a_sessions,idx_sort), 'Color','c') -scatter(1:length(idx_sort),mean_cohend_per_behav_TEO(a_sessions,idx_sort),60,'filled','MarkerFaceAlpha',.7,'MarkerFaceColor','c') -ylim([-1.5 1.5]); xlim([0 n_behav+1]) -ylabel(['Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Distribution of effect size per behavior, Monkey A') - -subplot(2,1,2);hold on; -scatter(1:length(idx_sort),mean_cohend_per_behav_vlPFC(h_sessions,idx_sort),60,'filled','MarkerFaceAlpha',.7,'MarkerFaceColor','b') -scatter(1:length(idx_sort),mean_cohend_per_behav_TEO(h_sessions,idx_sort),60,'filled','MarkerFaceAlpha',.7,'MarkerFaceColor','c') -ylim([-1.5 1.5]); xlim([0 n_behav+1]) -ylabel(['Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Distribution of effect size per behavior, Monkey H') -saveas(gcf, [savePath '/Distribution_effect_size_perBehav_perArea.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot proportion of selective units per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(prop_selective_per_behav_vlPFC(a_sessions,:))); -prop_selective_per_behav(s,prop_selective_per_behav_vlPFC(a_sessions,:)==0)=nan; -scatter(1:length(idx_sort),prop_selective_per_behav_vlPFC(a_sessions,idx_sort),60,'filled','MarkerFaceAlpha',.7,'MarkerFaceColor','b') -prop_selective_per_behav(s,prop_selective_per_behav_TEO(a_sessions,:)==0)=nan; -scatter(1:length(idx_sort),prop_selective_per_behav_TEO(a_sessions,idx_sort),60,'filled','MarkerFaceAlpha',.7,'MarkerFaceColor','c') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Proportion of selective units per behavior, Monkey A') - -subplot(2,1,2);hold on; -prop_selective_per_behav(s,prop_selective_per_behav_vlPFC(h_sessions,:)==0)=nan; -scatter(1:length(idx_sort),prop_selective_per_behav_vlPFC(h_sessions,idx_sort),60,'filled','MarkerFaceAlpha',.7,'MarkerFaceColor','b') -prop_selective_per_behav(s,prop_selective_per_behav_TEO(h_sessions,:)==0)=nan; -scatter(1:length(idx_sort),prop_selective_per_behav_TEO(h_sessions,idx_sort),60,'filled','MarkerFaceAlpha',.7,'MarkerFaceColor','c') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Proportion of selective units per behavior, Monkey H') -saveas(gcf, [savePath '/Proportion_selective_units_perBehav_perArea.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot number of behaviors a single neuron is selective for across all sessions, separated by monkey - -figure -histogram(vertcat(num_selective_behav_per_neuron_TEO{:})) -xlabel('Number of behaviors a given neuron is selective for') -ylabel('Neuron count') -set(gca,'FontSize',15); - - -figure; set(gcf,'Position',[150 250 1000 700]); -subplot(2,1,1); hold on -for s=a_sessions - histogram(num_selective_behav_per_neuron_TEO{s}, 'FaceColor','c','FaceAlpha',0.5) - histogram(num_selective_behav_per_neuron_vlPFC{s}, 'FaceColor','b','FaceAlpha',0.5) -end -legend({'TEO','vlPFC'},'Location','eastoutside') -xlabel('Number of behavior a given neuron is selective for') -set(gca,'FontSize',15); -title('Distribution of the number of behaviors single units are selective for, Monkey A') - -subplot(2,1,2); hold on -for s=h_sessions - histogram(num_selective_behav_per_neuron_TEO{s}, 'FaceColor','c','FaceAlpha',0.5) - histogram(num_selective_behav_per_neuron_vlPFC{s}, 'FaceColor','b','FaceAlpha',0.5) -end -legend({'TEO','vlPFC'},'Location','eastoutside') -xlabel('Number of behaviors a given neuron is selective for') -set(gca,'FontSize',15); -title('Distribution of the number of behaviors single units are selective for, Monkey H') -saveas(gcf, [savePath '/Number_selective_behavior_perUnit_perArea.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Bar plot decoding accuracy - - -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/']; - - channel_flag = "all"; - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - - n_neurons(1,s) = length(find(strcmp(brain_label,"vlPFC"))); %Get number of neurons - n_neurons(2,s) = length(find(strcmp(brain_label,"TEO"))); - - disp(['session' s]) -end - -figure; hold on -vlPFC = nan(1,max(session_range)); TEO = nan(1,max(session_range)); -for s=session_range - vlPFC(1,s) = length(find(vertcat(num_selective_behav_per_neuron_vlPFC{s})==0))/n_neurons(1,s); - TEO(1,s) = length(find(vertcat(num_selective_behav_per_neuron_TEO{s})==0))/n_neurons(2,s); - - vlPFC(2,s) = length(find(vertcat(num_selective_behav_per_neuron_vlPFC{s})==1))/n_neurons(1,s); - TEO(2,s) = length(find(vertcat(num_selective_behav_per_neuron_TEO{s})==1))/n_neurons(2,s); - - vlPFC(3,s) = length(find(vertcat(num_selective_behav_per_neuron_vlPFC{s})>1))/n_neurons(1,s); - TEO(3,s) = length(find(vertcat(num_selective_behav_per_neuron_TEO{s})>1))/n_neurons(2,s); - - scatter(0.8, vlPFC(1,s), 'filled','g'); - scatter(1, vlPFC(2,s), 'filled','g'); - scatter(1.2, vlPFC(3,s), 'filled','g'); - - scatter(1.8, TEO(1,s), 'filled','b'); - scatter(2, TEO(2,s), 'filled','b'); - scatter(2.2, TEO(3,s), 'filled','b'); - -end -ylim([0 1]); xlim([0.5 2.5]) - -ylabel('Proportion of units'); xlabel('Number of Behaviors units are selective for') -xticks([0.8 1 1.2 1.8 2 2.2]); xticklabels({'0', '1', '>1', '0', '1', '>1'}); -ax = gca; -ax.FontSize = 16; diff --git a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_drinkBaseline_batch.m b/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_drinkBaseline_batch.m deleted file mode 100644 index 9084b7c..0000000 --- a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_drinkBaseline_batch.m +++ /dev/null @@ -1,380 +0,0 @@ -%% Log_SingleNeuronTuning_Cohensd -% This script computes firing rate of individual neuron under different -% behavioral conditions. Then, it computes a cohen's d (or effect size) -% difference between the distribution of firing rates during behavior X -% with a baseline firing rate. - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -min_occurrence =10; -cohend_cutoff=0.5; p_cutoff=0.01;%Set thresholds - -%Initialize session batch variables: -n_behav = 28; -mean_cohend_per_behav = nan(length(sessions), n_behav); -std_cohend_per_behav = nan(length(sessions), n_behav); -se_cohend_per_behav = nan(length(sessions), n_behav); -prop_selective_per_behav = nan(length(sessions), n_behav); -num_selective_behav_per_neuron=cell(1,length(sessions)); -n_per_behav = nan(length(sessions),n_behav); - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/']; - - %% Load data - - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - session_length = size(Spike_rasters,2); % get session length - - %Extract behavior labels - behavior_labels = cell2mat({labels{:,3}}');%Get behavior label from labels structure - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - - %% Set parameters - unqLabels = 1:max(behavior_labels)-1; %Get unique behavior labels (exclude rest) - n_neurons(s) = size(Spike_rasters,1); %Get number of neurons - n_behav = length(unqLabels); %Get number of unique behavior labels - - %Estimate "baseline" neural firing distribution. - idx_rest=find(behavior_labels==find(behav_categ=="Drinking"));%Get idx of "drink" epochs. - mean_baseline = mean(Spike_rasters(:,idx_rest),2); - std_baseline = std(Spike_rasters(:,idx_rest),0,2); - -% % %Check visually that baseline is taken from epochs throughout the session -% % y=zeros(1, session_length); y(idx_rest)=1; -% % figure; plot(1:session_length, y); ylim([-0.5, 1.5]) -% % yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) -% % xlabel('Time in s'); title('Baseline epochs') -% % set(gca,'FontSize',15); -% % saveas(gcf, [savePath '/Baseline_epochs.png']); pause(2); close all - - %% Compute cohen's d - - cohend = nan(n_neurons(s),n_behav); - cohend_shuffle = nan(n_neurons(s),n_behav); - mean_beh = nan(n_neurons(s), n_behav); - mean_beh_shuffle = nan(n_neurons(s), n_behav); - std_beh = nan(n_neurons(s), n_behav); - std_beh_shuffle = nan(n_neurons(s), n_behav); - p = nan(n_neurons(s), n_behav); - p_rand = nan(n_neurons(s), n_behav); - - for n = 1:n_neurons(s) - - for b = 1:n_behav - idx = find(behavior_labels == unqLabels(b)); %get idx where behavior b occurred - n_per_behav(s,b)=length(idx); - - if n_per_behav(s,b)>min_occurrence - - if length(idx) cohend_cutoff & p_sorted < p_cutoff); sum(sum(h_sorted)) - h_shuffle_sorted = double(abs(cohend_shuffle_sorted) > cohend_cutoff & p_rand_sorted < p_cutoff); sum(sum(h_shuffle_sorted)) - cohend_thresh_sorted = h_sorted.*cohend_sorted; cohend_thresh_sorted(cohend_thresh_sorted==0)=nan; - cohend_shuffle_thresh_sorted = h_shuffle_sorted.*cohend_shuffle_sorted; cohend_shuffle_thresh_sorted(cohend_shuffle_thresh_sorted==0)=nan; - - - %Threshold cohens'd by a cohen's d AND p-value cutoff - h = double(abs(cohend) > cohend_cutoff & p < p_cutoff); sum(sum(h)) - h_shuffle = double(abs(cohend_shuffle) > cohend_cutoff & p_rand < p_cutoff); sum(sum(h_shuffle)) - - cohend_thresh = h.*cohend; cohend_thresh(cohend_thresh==0)=nan; - cohend_shuffle_thresh = h_shuffle.*cohend_shuffle; cohend_shuffle_thresh(cohend_shuffle_thresh==0)=nan; - - - %% Plot heatmaps - - AxesLabels_sorted = behav_categ(orderIdx); - AxesLabels = behav_categ(1:end-1); - caxis_upper = 1.5; - caxis_lower = -1.5; - cmap=flipud(cbrewer('div','RdBu', length(caxis_lower:0.01:caxis_upper))); - - %Plot ordered heatmaps - figure; set(gcf,'Position',[150 250 1000 500]); - hp=heatmap(cohend_sorted, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - %saveas(gcf, [savePath '/Cohend_heatmap_sorted.png']); close all - figure; set(gcf,'Position',[150 250 1000 500]); - hp=heatmap(cohend_thresh_sorted, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - %saveas(gcf, [savePath '/Cohend_heatmap_sorted_thresholded.png']); close all - - %Includes both p-value and cohen d as thresholds - figure; hold on; set(gcf,'Position',[150 250 1500 800]); - subplot(2,2,1); hp=heatmap(cohend, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap') - subplot(2,2,2); hp=heatmap(cohend_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - subplot(2,2,3); hp=heatmap(cohend_shuffle, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap SHUFFLED') - subplot(2,2,4); hp=heatmap(cohend_shuffle_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap SHUFFLED, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - %saveas(gcf, [savePath '/Cohend_heatmap_all_units.png']) - -% if with_NC == 0 -% sgtitle(['Cohens-d heatmap for all units except noise cluster']) -% saveas(gcf, [savePath '/Cohend_heatmap_NoNC_units.png']) -% elseif with_NC == 2 -% sgtitle(['Cohens-d heatmap for noise clusters ONLY']) -% saveas(gcf, [savePath '/Cohend_heatmap_NC_only.png']) -% elseif isolatedOnly -% sgtitle(['Cohens-d heatmap for isolated units']) -% saveas(gcf, [savePath '/Cohend_heatmap_isolated_units.png']) -% else -% sgtitle(['Cohens-d heatmap for all units']) -% saveas(gcf, [savePath '/Cohend_heatmap_all_units.png']) -% end - - %pause(2); - close all - - %%%%%%%%%%%%%%%%%%%%%%%%%%%% - %% Summary figures - - figure; scatter(n_per_behav(s,:), nanmean(abs(cohend_thresh))); xlabel('n'); ylabel('Mean abs(cohend)') - corrcoef(nanmean(abs(cohend_thresh)), n_per_behav(s,:),'rows','pairwise'); - figure; scatter(n_per_behav(s,:), sum(~isnan(cohend_thresh))); xlabel('n'); ylabel('# Selective units') - corrcoef(sum(~isnan(cohend_thresh)), n_per_behav(s,:),'rows','pairwise'); - - mean_cohend_per_behav(s,:) = nanmean(cohend_thresh); - std_cohend_per_behav(s,:) = nanstd(cohend_thresh); - se_cohend_per_behav(s,:) = nanstd(cohend_thresh)./sqrt(sum(~isnan(cohend_thresh))); - prop_selective_per_behav(s,:) = sum(~isnan(cohend_thresh))/n_neurons(s); - - %Plot the distribution of effect sizes for each behavior - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_cohend_per_behav(s,:)); - boxplot(cohend_thresh(:,idx_sort)) - % scatter(1:length(idx_sort),mean_cohend_per_behav(idx_sort),60,'filled') - % errorbar(mean_cohend_per_behav(idx_sort), std_cohend_per_behav(idx_sort),'LineWidth',1.5) - % legend({'mean','standard deviation'},'Location','best') - ylim([-2.5 2.5]); xlim([0 n_behav+1]) - ylabel(['Cohens-d, p<' num2str(p_cutoff)]) - yline(0,'LineStyle','--') - text(1,1.75,'Increased firing relative to baseline','FontSize',14) - text(1,-1.75,'Decreased firing relative to baseline','FontSize',14) - xticks(1:n_behav) - xticklabels(AxesLabels(idx_sort)) - set(gca,'FontSize',15); - title('Distribution of effect size per behavior') - %saveas(gcf, [savePath '/Distribution_cohend_per_behavior.png']); %pause(2); close all - - %Plot the proprotion of selective neurons per behavior - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~,idx_sort]=sort(prop_selective_per_behav(s,:),'descend'); - scatter(1:n_behav,prop_selective_per_behav(s,idx_sort),60,'filled') - ylabel('Prop. selective units') - xticks(1:n_behav); xlim([0 n_behav+1]); ylim([0 1]) - xticklabels(AxesLabels(idx_sort)) - set(gca,'FontSize',15); - title(['Proportion of selective units per behavior']) - %saveas(gcf, [savePath '/Proportion_units_selective_per_behav.png']); %pause(2); close all - - % Variance in single neuron selectivity - mean_cohend_per_neuron = nanmean(cohend_thresh,2); - std_cohend_per_neuron = nanstd(cohend_thresh,0,2); - - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_cohend_per_neuron); - scatter(1:length(idx_sort),mean_cohend_per_neuron(idx_sort),20,'filled') - errorbar(mean_cohend_per_neuron(idx_sort), std_cohend_per_neuron(idx_sort),'LineWidth',1.5) - legend({'mean','standard deviation'},'Location','best') - ylim([-2 2]); xlim([0 n_neurons(s)+1]) - ylabel(['Cohens-d, p<' num2str(p_cutoff)]); xlabel('Units') - yline(0,'LineStyle','--') - text(10,1.5,'Increased firing relative to baseline','FontSize',14) - text(10,-1.5,'Decreased firing relative to baseline','FontSize',14) - set(gca,'FontSize',15); - title('Distribution of effect size across all units') - %saveas(gcf, [savePath '/Distribution_cohend_all_units.png']); pause(2); close all - - %Number of behaviors a single neuron is selective for - num_selective_behav_per_neuron{s} = sum(~isnan(cohend_thresh),2); - figure; histogram(num_selective_behav_per_neuron{s}) - xlabel('Number of behavior a given neuron is selective to') - title('Distribution of the number of behaviors single units are selective for') - %saveas(gcf, [savePath '/Distribution_number_selective_behavior_per_unit.png']); %pause(2); close all - - close all - -end - -%% Results across sessions - -%Change savePath for all session results folder: -savePath = [home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SingleUnit_results/']; - -%Plot distribution of effect size per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(mean_cohend_per_behav)); -for s = a_sessions - scatter(1:length(idx_sort),mean_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) - %pause(1) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([-1.2 2]); xlim([0 n_behav+1]) -ylabel(['Mean Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - scatter(1:length(idx_sort),mean_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) - %pause(1) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([-1.2 2]); xlim([0 n_behav+1]) -ylabel(['Mean Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey H') - -sgtitle('Distribution of effect sizes per behavior','FontSize',20) -%saveas(gcf, [savePath '/Distribution_effect_size_per_behavior.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot proportion of selective units per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(prop_selective_per_behav)); -for s = a_sessions - prop_selective_per_behav(s,prop_selective_per_behav(s,:)==0)=nan; - scatter(1:length(idx_sort),prop_selective_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - prop_selective_per_behav(s,prop_selective_per_behav(s,:)==0)=nan; - scatter(1:length(idx_sort),prop_selective_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey H') -sgtitle('Proportion of selective units per behavior','FontSize',20) -%saveas(gcf, [savePath '/Proportion_selective_units_per_behavior.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot number of behaviors a single neuron is selective for across all sessions, separated by monkey - -figure; set(gcf,'Position',[150 250 1000 700]); -subplot(2,1,1); hold on -for s=a_sessions - histogram(num_selective_behav_per_neuron{s}, 'FaceAlpha',0.3) - prop_not_tuned(s) = length(find(num_selective_behav_per_neuron{s}==0))/n_neurons(s); - prop_tuned_more_than_one_behav(s) = length(find(num_selective_behav_per_neuron{s}>0))/n_neurons(s); -end -legend({sessions(a_sessions).name},'Location','eastoutside') -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2); hold on -for s=h_sessions - histogram(num_selective_behav_per_neuron{s}, 'FaceAlpha',0.3) - prop_not_tuned(s) = length(find(num_selective_behav_per_neuron{s}==0))/n_neurons(s); - prop_tuned_more_than_one_behav(s) = length(find(num_selective_behav_per_neuron{s}>0))/n_neurons(s); -end -legend({sessions(h_sessions).name},'Location','eastoutside') -xlabel('Number of behaviors a given neuron is selective for') -set(gca,'FontSize',15); -title('Monkey H') - -sgtitle('Distribution of the number of behaviors single units are selective for','FontSize',20) -%saveas(gcf, [savePath '/Number_selective_behavior_per_unit.png']); pause(2); close all - -mean(prop_not_tuned(prop_not_tuned>0)) -mean(prop_tuned_more_than_one_behav(prop_tuned_more_than_one_behav>0)) \ No newline at end of file diff --git a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_grooming.m b/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_grooming.m deleted file mode 100644 index 4deeccf..0000000 --- a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_grooming.m +++ /dev/null @@ -1,234 +0,0 @@ -%% Log_SingleNeuronTuning_Cohensd -% This script computes firing rate of individual neuron under different -% behavioral conditions. Then, it computes a cohen's d (or effect size) -% difference between the distribution of firing rates during behavior X -% with a baseline firing rate. - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units - -%Initialize session batch variables: -n_behav = 4; -mean_cohend_per_behav = nan(length(sessions), n_behav); -std_cohend_per_behav = nan(length(sessions), n_behav); -se_cohend_per_behav = nan(length(sessions), n_behav); -prop_selective_per_behav = nan(length(sessions), n_behav); -num_selective_behav_per_neuron=cell(1,length(sessions)); -n_per_behav = nan(length(sessions),n_behav); - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/']; - - %% Load data - - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - session_length = size(Spike_rasters,2); % get session length - - %Extract behavior labels - behavior_labels = groom_labels_all(:,2:end);%Get behavior label from labels structure - groom_categ_label = {'Star.vs.end', 'Post-threat.vs.not','Reciprocated.vs.not','Initiated.vs.not'}; - - %Set parameters - unqLabels = 1:size(behavior_labels,2); %Get unique behavior labels (exclude rest) - n_neurons = size(Spike_rasters,1); %Get number of neurons - n_behav = size(behavior_labels,2); %Get number of unique behavior labels - - - %% Compute cohen's d - - n_per_behav = nan(n_behav,1); - cohend = nan(n_neurons,n_behav); - cohend_shuffle = nan(n_neurons,n_behav); - mean_beh = nan(n_neurons, n_behav); - mean_beh_shuffle = nan(n_neurons, n_behav); - std_beh = nan(n_neurons, n_behav); - std_beh_shuffle = nan(n_neurons, n_behav); - p = nan(n_neurons, n_behav); - p_rand = nan(n_neurons, n_behav); - - for n = 1:n_neurons - - for b = 1:n_behav - - idx = find(behavior_labels(:,b) == 2); %get idx where grooming in context b occurred - idx_rest = find(behavior_labels(:,b) == 1); %rest of grooming indices - - %Estimate basline firing rate distribution - mean_baseline = mean(Spike_rasters(:,idx_rest),2); - std_baseline = std(Spike_rasters(:,idx_rest),0,2); - - n_per_behav(b)=length(idx); - - if n_per_behav(b)>20 - - if length(idx)20 - - if length(idx)10 - - if length(idx) cohend_cutoff); sum(sum(h_sorted)) - h_shuffle_sorted = double(abs(cohend_shuffle_sorted) > cohend_cutoff); sum(sum(h_shuffle_sorted)) - cohend_thresh_sorted = h_sorted.*cohend_sorted; cohend_thresh_sorted(cohend_thresh_sorted==0)=nan; - cohend_shuffle_thresh_sorted = h_shuffle_sorted.*cohend_shuffle_sorted; cohend_shuffle_thresh_sorted(cohend_shuffle_thresh_sorted==0)=nan; - - - %Threshold heatmap by a cohen's d - h = double(abs(cohend) > cohend_cutoff); sum(sum(h)) - h_shuffle = double(abs(cohend_shuffle) > cohend_cutoff); sum(sum(h_shuffle)) - - cohend_thresh = h.*cohend; cohend_thresh(cohend_thresh==0)=nan; - cohend_shuffle_thresh = h_shuffle.*cohend_shuffle; cohend_shuffle_thresh(cohend_shuffle_thresh==0)=nan; - - - %% Plot heatmaps - - AxesLabels_sorted = behav_categ(orderIdx); - AxesLabels = behav_categ(1:end-1); - caxis_upper = 1.5; - caxis_lower = -1.5; - cmap=flipud(cbrewer('div','RdBu', length(caxis_lower:0.01:caxis_upper))); - - %Plot ordered heatmaps - figure; set(gcf,'Position',[150 250 1000 500]); - hp=heatmap(cohend_sorted, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - saveas(gcf, [savePath '/Cohend_heatmap_sorted_min10.png']); close all - figure; set(gcf,'Position',[150 250 1000 500]); - hp=heatmap(cohend_thresh_sorted, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - saveas(gcf, [savePath '/Cohend_heatmap_sorted_CohenDonly_thresholded_min10.png']); close all - - %Only cohen d as threshold - figure; hold on; set(gcf,'Position',[150 250 1500 800]); - subplot(2,2,1); hp=heatmap(cohend, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap') - subplot(2,2,2); hp=heatmap(cohend_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, cohend>' num2str(cohend_cutoff)]) - subplot(2,2,3); hp=heatmap(cohend_shuffle, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap SHUFFLED') - subplot(2,2,4); hp=heatmap(cohend_shuffle_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap SHUFFLED, cohend>' num2str(cohend_cutoff)]) - saveas(gcf, [savePath '/CohendOnly_heatmap_all_units.png']) - - - %pause(2); - close all - - %%%%%%%%%%%%%%%%%%%%%%%%%%%% - %% Summary figures - - figure; scatter(nanmean(abs(cohend_thresh)), n_per_behav(s,:)); corrcoef(nanmean(abs(cohend_thresh)), n_per_behav(s,:),'rows','pairwise'); - figure; scatter(sum(~isnan(cohend_thresh)), n_per_behav(s,:)); corrcoef(sum(~isnan(cohend_thresh)), n_per_behav(s,:),'rows','pairwise'); - - mean_cohend_per_behav(s,:) = nanmean(cohend_thresh); - std_cohend_per_behav(s,:) = nanstd(cohend_thresh); - se_cohend_per_behav(s,:) = nanstd(cohend_thresh)./sqrt(sum(~isnan(cohend_thresh))); - prop_selective_per_behav(s,:) = sum(~isnan(cohend_thresh))/n_neurons(s); - - %Plot the distribution of effect sizes for each behavior - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_cohend_per_behav(s,:)); - boxplot(cohend_thresh(:,idx_sort)) - % scatter(1:length(idx_sort),mean_cohend_per_behav(idx_sort),60,'filled') - % errorbar(mean_cohend_per_behav(idx_sort), std_cohend_per_behav(idx_sort),'LineWidth',1.5) - % legend({'mean','standard deviation'},'Location','best') - ylim([-2.5 2.5]); xlim([0 n_behav+1]) - ylabel(['Cohens-d, p<' num2str(p_cutoff)]) - yline(0,'LineStyle','--') - text(1,1.75,'Increased firing relative to baseline','FontSize',14) - text(1,-1.75,'Decreased firing relative to baseline','FontSize',14) - xticks(1:n_behav) - xticklabels(AxesLabels(idx_sort)) - set(gca,'FontSize',15); - title('Distribution of effect size per behavior') - saveas(gcf, [savePath '/Distribution_cohend_per_behavior_min10_CohenDonly.png']); %pause(2); close all - - %Plot the proprotion of selective neurons per behavior - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~,idx_sort]=sort(prop_selective_per_behav(s,:),'descend'); - scatter(1:n_behav,prop_selective_per_behav(s,idx_sort),60,'filled') - ylabel('Prop. selective units') - xticks(1:n_behav); xlim([0 n_behav+1]); ylim([0 1]) - xticklabels(AxesLabels(idx_sort)) - set(gca,'FontSize',15); - title(['Proportion of units cohend >' num2str(cohend_cutoff) ' per behavior']) - saveas(gcf, [savePath '/Proportion_units_selective_per_behav_min10_CohenDonly.png']); %pause(2); close all - - % Variance in single neuron selectivity - mean_cohend_per_neuron = nanmean(cohend_thresh,2); - std_cohend_per_neuron = nanstd(cohend_thresh,0,2); - - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_cohend_per_neuron); - scatter(1:length(idx_sort),mean_cohend_per_neuron(idx_sort),20,'filled') - errorbar(mean_cohend_per_neuron(idx_sort), std_cohend_per_neuron(idx_sort),'LineWidth',1.5) - legend({'mean','standard deviation'},'Location','best') - ylim([-2 2]); xlim([0 n_neurons(s)+1]) - ylabel(['Cohens-d, p<' num2str(p_cutoff)]); xlabel('Units') - yline(0,'LineStyle','--') - text(10,1.5,'Increased firing relative to baseline','FontSize',14) - text(10,-1.5,'Decreased firing relative to baseline','FontSize',14) - set(gca,'FontSize',15); - title('Distribution of effect size across all units') - saveas(gcf, [savePath '/Distribution_cohend_all_units_min10_CohenDonly.png']); pause(2); close all - - %Number of behaviors a single neuron is selective for - num_selective_behav_per_neuron{s} = sum(~isnan(cohend_thresh),2); - figure; histogram(num_selective_behav_per_neuron{s}) - xlabel('Number of behavior a given neuron is selective to') - title('Distribution of the number of behaviors single units are selective for') - saveas(gcf, [savePath '/Distribution_number_selective_behavior_per_unit_min10_CohenDonly.png']); %pause(2); close all - - % % %%%%%%%%%%%%%%%%%%%%%%%% - % % %Single neuron selectivity: - % % figure; set(gcf,'Position',[150 250 1000 300]); - % % data = cohend_thresh(1,:); - % % [~, idx_sorted]=sort(data); - % % scatter(1:n_behav, cohend_thresh(1,idx_sorted),40, 'filled') - % % ylim([caxis_lower caxis_upper]); xlim([0 n_behav+1]) - % % ylabel(['Cohens-d, p<' num2str(cutoff)]) - % % yline(0,'LineStyle','--') - % % xticks(1:n_behav) - % % xticklabels(AxesLabels(idx_sorted)) - % % set(gca,'FontSize',15); - - close all - -end - -%% Results across sessions - -%Change savePath for all session results folder: -savePath = [home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SingleUnit_results/']; - -%Plot distribution of effect size per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(mean_cohend_per_behav)); -for s = a_sessions - scatter(1:length(idx_sort),mean_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([-1.5 1.5]); xlim([0 n_behav+1]) -ylabel('Mean Cohens-d') -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - scatter(1:length(idx_sort),mean_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([-1.5 1.5]); xlim([0 n_behav+1]) -ylabel('Mean Cohens-d') -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey H') - -sgtitle('Distribution of effect sizes per behavior','FontSize',20) -saveas(gcf, [savePath '/Distribution_effect_size_per_behavior_min10_CohenDonly.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot proportion of selective units per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(prop_selective_per_behav)); -for s = a_sessions - prop_selective_per_behav(s,prop_selective_per_behav(s,:)==0)=nan; - scatter(1:length(idx_sort),prop_selective_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - prop_selective_per_behav(s,prop_selective_per_behav(s,:)==0)=nan; - scatter(1:length(idx_sort),prop_selective_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey H') -sgtitle('Proportion of selective units per behavior','FontSize',20) -saveas(gcf, [savePath '/Proportion_selective_units_per_behavior_min10_CohenDonly.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot number of behaviors a single neuron is selective for across all sessions, separated by monkey - -figure; set(gcf,'Position',[150 250 1000 700]); -subplot(2,1,1); hold on -for s=a_sessions - histogram(num_selective_behav_per_neuron{s}, 'FaceAlpha',0.3) - prop_not_tuned(s) = length(find(num_selective_behav_per_neuron{s}==0))/n_neurons(s); - prop_tuned_more_than_one_behav(s) = length(find(num_selective_behav_per_neuron{s}>0))/n_neurons(s); -end -legend({sessions(a_sessions).name},'Location','eastoutside') -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2); hold on -for s=h_sessions - histogram(num_selective_behav_per_neuron{s}, 'FaceAlpha',0.3) - prop_not_tuned(s) = length(find(num_selective_behav_per_neuron{s}==0))/n_neurons(s); - prop_tuned_more_than_one_behav(s) = length(find(num_selective_behav_per_neuron{s}>0))/n_neurons(s); -end -legend({sessions(h_sessions).name},'Location','eastoutside') -xlabel('Number of behaviors a given neuron is selective for') -set(gca,'FontSize',15); -title('Monkey H') - -sgtitle('Distribution of the number of behaviors single units are selective for','FontSize',20) -saveas(gcf, [savePath '/Number_selective_behavior_per_unit_min10_CohenDonly.png']); pause(2); close all - -mean(prop_not_tuned(prop_not_tuned>0)) -mean(prop_tuned_more_than_one_behav(prop_tuned_more_than_one_behav>0)) \ No newline at end of file diff --git a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_min20obs_batch.m b/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_min20obs_batch.m deleted file mode 100644 index 84eb20e..0000000 --- a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_min20obs_batch.m +++ /dev/null @@ -1,403 +0,0 @@ -%% Log_SingleNeuronTuning_Cohensd -% This script computes firing rate of individual neuron under different -% behavioral conditions. Then, it computes a cohen's d (or effect size) -% difference between the distribution of firing rates during behavior X -% with a baseline firing rate. - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units - -%Initialize session batch variables: -n_behav = 28; -mean_cohend_per_behav = nan(length(sessions), n_behav); -std_cohend_per_behav = nan(length(sessions), n_behav); -se_cohend_per_behav = nan(length(sessions), n_behav); -prop_selective_per_behav = nan(length(sessions), n_behav); -num_selective_behav_per_neuron=cell(1,length(sessions)); -n_per_behav = nan(length(sessions),n_behav); - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/']; - - %% Load data - - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - session_length = size(Spike_rasters,2); % get session length - - %Extract behavior labels - behavior_labels = cell2mat({labels{:,3}}');%Get behavior label from labels structure - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - - %% Set parameters - unqLabels = 1:max(behavior_labels)-1; %Get unique behavior labels (exclude rest) - n_neurons(s) = size(Spike_rasters,1); %Get number of neurons - n_behav = length(unqLabels); %Get number of unique behavior labels - - %Estimate "baseline" neural firing distribution. - idx_rest=find(behavior_labels==length(behav_categ));%Get idx of "rest" epochs. - mean_baseline = mean(Spike_rasters(:,idx_rest),2); - std_baseline = std(Spike_rasters(:,idx_rest),0,2); - -% % %Check visually that baseline is taken from epochs throughout the session -% % y=zeros(1, session_length); y(idx_rest)=1; -% % figure; plot(1:session_length, y); ylim([-0.5, 1.5]) -% % yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) -% % xlabel('Time in s'); title('Baseline epochs') -% % set(gca,'FontSize',15); -% % saveas(gcf, [savePath '/Baseline_epochs.png']); pause(2); close all - - %% Compute cohen's d - - cohend = nan(n_neurons(s),n_behav); - cohend_shuffle = nan(n_neurons(s),n_behav); - mean_beh = nan(n_neurons(s), n_behav); - mean_beh_shuffle = nan(n_neurons(s), n_behav); - std_beh = nan(n_neurons(s), n_behav); - std_beh_shuffle = nan(n_neurons(s), n_behav); - p = nan(n_neurons(s), n_behav); - p_rand = nan(n_neurons(s), n_behav); - - for n = 1:n_neurons(s) - - for b = 1:n_behav - idx = find(behavior_labels == unqLabels(b)); %get idx where behavior b occurred - n_per_behav(s,b)=length(idx); - - if n_per_behav(s,b)>20 - - if length(idx) cohend_cutoff & p_sorted < p_cutoff); sum(sum(h_sorted)) - h_shuffle_sorted = double(abs(cohend_shuffle_sorted) > cohend_cutoff & p_rand_sorted < p_cutoff); sum(sum(h_shuffle_sorted)) - cohend_thresh_sorted = h_sorted.*cohend_sorted; cohend_thresh_sorted(cohend_thresh_sorted==0)=nan; - cohend_shuffle_thresh_sorted = h_shuffle_sorted.*cohend_shuffle_sorted; cohend_shuffle_thresh_sorted(cohend_shuffle_thresh_sorted==0)=nan; - - - %Threshold cohens'd by a cohen's d AND p-value cutoff - cohend_cutoff=0.5; p_cutoff=0.01; - h = double(abs(cohend) > cohend_cutoff & p < p_cutoff); sum(sum(h)) - h_shuffle = double(abs(cohend_shuffle) > cohend_cutoff & p_rand < p_cutoff); sum(sum(h_shuffle)) - - cohend_thresh = h.*cohend; cohend_thresh(cohend_thresh==0)=nan; - cohend_shuffle_thresh = h_shuffle.*cohend_shuffle; cohend_shuffle_thresh(cohend_shuffle_thresh==0)=nan; - -% % %Threshold heatmap by a cohen's d -% % cohend_cutoff=0.5; -% % h = double(abs(cohend) > cohend_cutoff); sum(sum(h)) -% % h_shuffle = double(abs(cohend_shuffle) > cohend_cutoff); sum(sum(h_shuffle)) -% % -% % cohend_thresh = h.*cohend; cohend_thresh(cohend_thresh==0)=nan; -% % cohend_shuffle_thresh = h_shuffle.*cohend_shuffle; cohend_shuffle_thresh(cohend_shuffle_thresh==0)=nan; - - - %% Plot heatmaps - - AxesLabels_sorted = behav_categ(orderIdx); - AxesLabels = behav_categ(1:end-1); - caxis_upper = 1.5; - caxis_lower = -1.5; - cmap=flipud(cbrewer('div','RdBu', length(caxis_lower:0.01:caxis_upper))); - - %Plot ordered heatmaps - figure; set(gcf,'Position',[150 250 1000 500]); - hp=heatmap(cohend_sorted, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - saveas(gcf, [savePath '/Cohend_heatmap_sorted_min20.png']); close all - figure; set(gcf,'Position',[150 250 1000 500]); - hp=heatmap(cohend_thresh_sorted, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - saveas(gcf, [savePath '/Cohend_heatmap_sorted_thresholded_min20.png']); close all - - %Includes both p-value and cohen d as thresholds - figure; hold on; set(gcf,'Position',[150 250 1500 800]); - subplot(2,2,1); hp=heatmap(cohend, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap') - subplot(2,2,2); hp=heatmap(cohend_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - subplot(2,2,3); hp=heatmap(cohend_shuffle, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap SHUFFLED') - subplot(2,2,4); hp=heatmap(cohend_shuffle_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap SHUFFLED, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - saveas(gcf, [savePath '/Cohend_heatmap_all_units_min20.png']) - -% %Only cohen d as threshold -% figure; hold on; set(gcf,'Position',[150 250 1500 800]); -% subplot(2,2,1); hp=heatmap(cohend, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap') -% subplot(2,2,2); hp=heatmap(cohend_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, cohend>' num2str(cohend_cutoff)]) -% subplot(2,2,3); hp=heatmap(cohend_shuffle, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap SHUFFLED') -% subplot(2,2,4); hp=heatmap(cohend_shuffle_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap SHUFFLED, cohend>' num2str(cohend_cutoff)]) -% saveas(gcf, [savePath '/Cohend_heatmap_all_units.png']) - -% if with_NC == 0 -% sgtitle(['Cohens-d heatmap for all units except noise cluster']) -% saveas(gcf, [savePath '/Cohend_heatmap_NoNC_units.png']) -% elseif with_NC == 2 -% sgtitle(['Cohens-d heatmap for noise clusters ONLY']) -% saveas(gcf, [savePath '/Cohend_heatmap_NC_only.png']) -% elseif isolatedOnly -% sgtitle(['Cohens-d heatmap for isolated units']) -% saveas(gcf, [savePath '/Cohend_heatmap_isolated_units.png']) -% else -% sgtitle(['Cohens-d heatmap for all units']) -% saveas(gcf, [savePath '/Cohend_heatmap_all_units.png']) -% end - - %pause(2); - close all - - %%%%%%%%%%%%%%%%%%%%%%%%%%%% - %% Summary figures - - figure; scatter(nanmean(abs(cohend_thresh)), n_per_behav(s,:)); corrcoef(nanmean(abs(cohend_thresh)), n_per_behav(s,:),'rows','pairwise'); - figure; scatter(sum(~isnan(cohend_thresh)), n_per_behav(s,:)); corrcoef(sum(~isnan(cohend_thresh)), n_per_behav(s,:),'rows','pairwise'); - - mean_cohend_per_behav(s,:) = nanmean(cohend_thresh); - std_cohend_per_behav(s,:) = nanstd(cohend_thresh); - se_cohend_per_behav(s,:) = nanstd(cohend_thresh)./sqrt(sum(~isnan(cohend_thresh))); - prop_selective_per_behav(s,:) = sum(~isnan(cohend_thresh))/n_neurons(s); - - %Plot the distribution of effect sizes for each behavior - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_cohend_per_behav(s,:)); - boxplot(cohend_thresh(:,idx_sort)) - % scatter(1:length(idx_sort),mean_cohend_per_behav(idx_sort),60,'filled') - % errorbar(mean_cohend_per_behav(idx_sort), std_cohend_per_behav(idx_sort),'LineWidth',1.5) - % legend({'mean','standard deviation'},'Location','best') - ylim([-2.5 2.5]); xlim([0 n_behav+1]) - ylabel(['Cohens-d, p<' num2str(p_cutoff)]) - yline(0,'LineStyle','--') - text(1,1.75,'Increased firing relative to baseline','FontSize',14) - text(1,-1.75,'Decreased firing relative to baseline','FontSize',14) - xticks(1:n_behav) - xticklabels(AxesLabels(idx_sort)) - set(gca,'FontSize',15); - title('Distribution of effect size per behavior') - saveas(gcf, [savePath '/Distribution_cohend_per_behavior_min20.png']); %pause(2); close all - - %Plot the proprotion of selective neurons per behavior - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~,idx_sort]=sort(prop_selective_per_behav(s,:),'descend'); - scatter(1:n_behav,prop_selective_per_behav(s,idx_sort),60,'filled') - ylabel('Prop. selective units') - xticks(1:n_behav); xlim([0 n_behav+1]); ylim([0 1]) - xticklabels(AxesLabels(idx_sort)) - set(gca,'FontSize',15); - title(['Proportion of units abs(cohend) >' num2str(cohend_thresh) 'per behavior']) - saveas(gcf, [savePath '/Proportion_units_selective_per_behav_min20.png']); %pause(2); close all - - % Variance in single neuron selectivity - mean_cohend_per_neuron = nanmean(cohend_thresh,2); - std_cohend_per_neuron = nanstd(cohend_thresh,0,2); - - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_cohend_per_neuron); - scatter(1:length(idx_sort),mean_cohend_per_neuron(idx_sort),20,'filled') - errorbar(mean_cohend_per_neuron(idx_sort), std_cohend_per_neuron(idx_sort),'LineWidth',1.5) - legend({'mean','standard deviation'},'Location','best') - ylim([-2 2]); xlim([0 n_neurons(s)+1]) - ylabel(['Cohens-d, p<' num2str(p_cutoff)]); xlabel('Units') - yline(0,'LineStyle','--') - text(10,1.5,'Increased firing relative to baseline','FontSize',14) - text(10,-1.5,'Decreased firing relative to baseline','FontSize',14) - set(gca,'FontSize',15); - title('Distribution of effect size across all units') - saveas(gcf, [savePath '/Distribution_cohend_all_units_min20.png']); pause(2); close all - - %Number of behaviors a single neuron is selective for - num_selective_behav_per_neuron{s} = sum(~isnan(cohend_thresh),2); - figure; histogram(num_selective_behav_per_neuron{s}) - xlabel('Number of behavior a given neuron is selective to') - title('Distribution of the number of behaviors single units are selective for') - saveas(gcf, [savePath '/Distribution_number_selective_behavior_per_unit_min20.png']); %pause(2); close all - - % % %%%%%%%%%%%%%%%%%%%%%%%% - % % %Single neuron selectivity: - % % figure; set(gcf,'Position',[150 250 1000 300]); - % % data = cohend_thresh(1,:); - % % [~, idx_sorted]=sort(data); - % % scatter(1:n_behav, cohend_thresh(1,idx_sorted),40, 'filled') - % % ylim([caxis_lower caxis_upper]); xlim([0 n_behav+1]) - % % ylabel(['Cohens-d, p<' num2str(cutoff)]) - % % yline(0,'LineStyle','--') - % % xticks(1:n_behav) - % % xticklabels(AxesLabels(idx_sorted)) - % % set(gca,'FontSize',15); - - close all - -end - -%% Results across sessions - -%Change savePath for all session results folder: -savePath = [home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SingleUnit_results/']; - -%Plot distribution of effect size per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(mean_cohend_per_behav)); -for s = a_sessions - scatter(1:length(idx_sort),mean_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([-1.5 1.5]); xlim([0 n_behav+1]) -ylabel(['Mean Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - scatter(1:length(idx_sort),mean_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([-1.5 1.5]); xlim([0 n_behav+1]) -ylabel(['Mean Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey H') - -sgtitle('Distribution of effect sizes per behavior','FontSize',20) -saveas(gcf, [savePath '/Distribution_effect_size_per_behavior_min20.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot proportion of selective units per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(prop_selective_per_behav)); -for s = a_sessions - prop_selective_per_behav(s,prop_selective_per_behav(s,:)==0)=nan; - scatter(1:length(idx_sort),prop_selective_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - prop_selective_per_behav(s,prop_selective_per_behav(s,:)==0)=nan; - scatter(1:length(idx_sort),prop_selective_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey H') -sgtitle('Proportion of selective units per behavior','FontSize',20) -saveas(gcf, [savePath '/Proportion_selective_units_per_behavior_min20.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot number of behaviors a single neuron is selective for across all sessions, separated by monkey - -figure; set(gcf,'Position',[150 250 1000 700]); -subplot(2,1,1); hold on -for s=a_sessions - histogram(num_selective_behav_per_neuron{s}, 'FaceAlpha',0.3) - prop_not_tuned(s) = length(find(num_selective_behav_per_neuron{s}==0))/n_neurons(s); - prop_tuned_more_than_one_behav(s) = length(find(num_selective_behav_per_neuron{s}>0))/n_neurons(s); -end -legend({sessions(a_sessions).name},'Location','eastoutside') -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2); hold on -for s=h_sessions - histogram(num_selective_behav_per_neuron{s}, 'FaceAlpha',0.3) - prop_not_tuned(s) = length(find(num_selective_behav_per_neuron{s}==0))/n_neurons(s); - prop_tuned_more_than_one_behav(s) = length(find(num_selective_behav_per_neuron{s}>0))/n_neurons(s); -end -legend({sessions(h_sessions).name},'Location','eastoutside') -xlabel('Number of behaviors a given neuron is selective for') -set(gca,'FontSize',15); -title('Monkey H') - -sgtitle('Distribution of the number of behaviors single units are selective for','FontSize',20) -saveas(gcf, [savePath '/Number_selective_behavior_per_unit_min20.png']); pause(2); close all - -mean(prop_not_tuned(prop_not_tuned>0)) -mean(prop_tuned_more_than_one_behav(prop_tuned_more_than_one_behav>0)) \ No newline at end of file diff --git a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_min20obs_onlyCohenDthresh_batch.m b/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_min20obs_onlyCohenDthresh_batch.m deleted file mode 100644 index 0174382..0000000 --- a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_min20obs_onlyCohenDthresh_batch.m +++ /dev/null @@ -1,377 +0,0 @@ -%% Log_SingleNeuronTuning_Cohensd -% This script computes firing rate of individual neuron under different -% behavioral conditions. Then, it computes a cohen's d (or effect size) -% difference between the distribution of firing rates during behavior X -% with a baseline firing rate. - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =0; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -min_occurrence=50; - -%Initialize session batch variables: -n_behav = 28; -mean_cohend_per_behav = nan(length(sessions), n_behav); -std_cohend_per_behav = nan(length(sessions), n_behav); -se_cohend_per_behav = nan(length(sessions), n_behav); -prop_selective_per_behav = nan(length(sessions), n_behav); -num_selective_behav_per_neuron=cell(1,length(sessions)); -n_per_behav = nan(length(sessions),n_behav); - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/']; - - %% Load data - - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - end - - session_length = size(Spike_rasters,2); % get session length - - %Extract behavior labels - behavior_labels = cell2mat({labels{:,3}}');%Get behavior label from labels structure - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - - %% Set parameters - unqLabels = 1:max(behavior_labels)-1; %Get unique behavior labels (exclude rest) - n_neurons(s) = size(Spike_rasters,1); %Get number of neurons - n_behav = length(unqLabels); %Get number of unique behavior labels - - %Estimate "baseline" neural firing distribution. - idx_rest=find(behavior_labels==length(behav_categ));%Get idx of "rest" epochs. - mean_baseline = mean(Spike_rasters(:,idx_rest),2); - std_baseline = std(Spike_rasters(:,idx_rest),0,2); - - % % %Check visually that baseline is taken from epochs throughout the session - % % y=zeros(1, session_length); y(idx_rest)=1; - % % figure; plot(1:session_length, y); ylim([-0.5, 1.5]) - % % yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) - % % xlabel('Time in s'); title('Baseline epochs') - % % set(gca,'FontSize',15); - % % saveas(gcf, [savePath '/Baseline_epochs.png']); pause(2); close all - - %% Compute cohen's d - - cohend = nan(n_neurons(s),n_behav); - cohend_shuffle = nan(n_neurons(s),n_behav); - mean_beh = nan(n_neurons(s), n_behav); - mean_beh_shuffle = nan(n_neurons(s), n_behav); - std_beh = nan(n_neurons(s), n_behav); - std_beh_shuffle = nan(n_neurons(s), n_behav); - p = nan(n_neurons(s), n_behav); - p_rand = nan(n_neurons(s), n_behav); - - for n = 1:n_neurons(s) - - for b = 1:n_behav - idx = find(behavior_labels == unqLabels(b)); %get idx where behavior b occurred - n_per_behav(s,b)=length(idx); - - if n_per_behav(s,b)>min_occurrence - - if length(idx) cohend_cutoff); sum(sum(h_sorted)) - h_shuffle_sorted = double(abs(cohend_shuffle_sorted) > cohend_cutoff); sum(sum(h_shuffle_sorted)) - cohend_thresh_sorted = h_sorted.*cohend_sorted; cohend_thresh_sorted(cohend_thresh_sorted==0)=nan; - cohend_shuffle_thresh_sorted = h_shuffle_sorted.*cohend_shuffle_sorted; cohend_shuffle_thresh_sorted(cohend_shuffle_thresh_sorted==0)=nan; - - - %Threshold heatmap by a cohen's d - h = double(abs(cohend) > cohend_cutoff); sum(sum(h)) - h_shuffle = double(abs(cohend_shuffle) > cohend_cutoff); sum(sum(h_shuffle)) - - cohend_thresh = h.*cohend; cohend_thresh(cohend_thresh==0)=nan; - cohend_shuffle_thresh = h_shuffle.*cohend_shuffle; cohend_shuffle_thresh(cohend_shuffle_thresh==0)=nan; - - - %% Plot heatmaps - - AxesLabels_sorted = behav_categ(orderIdx); - AxesLabels = behav_categ(1:end-1); - caxis_upper = 1.5; - caxis_lower = -1.5; - cmap=flipud(cbrewer('div','RdBu', length(caxis_lower:0.01:caxis_upper))); - - %Plot ordered heatmaps - figure; set(gcf,'Position',[150 250 1000 500]); - hp=heatmap(cohend_sorted, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - %saveas(gcf, [savePath '/Cohend_heatmap_sorted_min20.png']); close all - figure; set(gcf,'Position',[150 250 1000 500]); - hp=heatmap(cohend_thresh_sorted, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - %saveas(gcf, [savePath '/Cohend_heatmap_sorted_CohenDonly_thresholded_min20.png']); close all - - %Only cohen d as threshold - figure; hold on; set(gcf,'Position',[150 250 1500 800]); - subplot(2,2,1); hp=heatmap(cohend, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap') - subplot(2,2,2); hp=heatmap(cohend_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, cohend>' num2str(cohend_cutoff)]) - subplot(2,2,3); hp=heatmap(cohend_shuffle, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap SHUFFLED') - subplot(2,2,4); hp=heatmap(cohend_shuffle_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap SHUFFLED, cohend>' num2str(cohend_cutoff)]) - %saveas(gcf, [savePath '/CohendOnly_heatmap_all_units.png']) - - - pause(2); - close all - - %%%%%%%%%%%%%%%%%%%%%%%%%%%% - %% Summary figures - - figure; scatter(nanmean(abs(cohend_thresh)), n_per_behav(s,:)); corrcoef(nanmean(abs(cohend_thresh)), n_per_behav(s,:),'rows','pairwise'); - figure; scatter(sum(~isnan(cohend_thresh)), n_per_behav(s,:)); corrcoef(sum(~isnan(cohend_thresh)), n_per_behav(s,:),'rows','pairwise'); - - mean_cohend_per_behav(s,:) = nanmean(cohend_thresh); - std_cohend_per_behav(s,:) = nanstd(cohend_thresh); - se_cohend_per_behav(s,:) = nanstd(cohend_thresh)./sqrt(sum(~isnan(cohend_thresh))); - prop_selective_per_behav(s,:) = sum(~isnan(cohend_thresh))/n_neurons(s); - - %Plot the distribution of effect sizes for each behavior - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_cohend_per_behav(s,:)); - boxplot(cohend_thresh(:,idx_sort)) - % scatter(1:length(idx_sort),mean_cohend_per_behav(idx_sort),60,'filled') - % errorbar(mean_cohend_per_behav(idx_sort), std_cohend_per_behav(idx_sort),'LineWidth',1.5) - % legend({'mean','standard deviation'},'Location','best') - ylim([-2.5 2.5]); xlim([0 n_behav+1]) - ylabel(['Cohens-d, p<' num2str(p_cutoff)]) - yline(0,'LineStyle','--') - text(1,1.75,'Increased firing relative to baseline','FontSize',14) - text(1,-1.75,'Decreased firing relative to baseline','FontSize',14) - xticks(1:n_behav) - xticklabels(AxesLabels(idx_sort)) - set(gca,'FontSize',15); - title('Distribution of effect size per behavior') - %saveas(gcf, [savePath '/Distribution_cohend_per_behavior_min20_CohenDonly.png']); %pause(2); close all - - %Plot the proprotion of selective neurons per behavior - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~,idx_sort]=sort(prop_selective_per_behav(s,:),'descend'); - scatter(1:n_behav,prop_selective_per_behav(s,idx_sort),60,'filled') - ylabel('Prop. selective units') - xticks(1:n_behav); xlim([0 n_behav+1]); ylim([0 1]) - xticklabels(AxesLabels(idx_sort)) - set(gca,'FontSize',15); - title(['Proportion of units cohend >' num2str(cohend_cutoff) ' per behavior']) - %saveas(gcf, [savePath '/Proportion_units_selective_per_behav_min20_CohenDonly.png']); %pause(2); close all - - % Variance in single neuron selectivity - mean_cohend_per_neuron = nanmean(cohend_thresh,2); - std_cohend_per_neuron = nanstd(cohend_thresh,0,2); - - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_cohend_per_neuron); - scatter(1:length(idx_sort),mean_cohend_per_neuron(idx_sort),20,'filled') - errorbar(mean_cohend_per_neuron(idx_sort), std_cohend_per_neuron(idx_sort),'LineWidth',1.5) - legend({'mean','standard deviation'},'Location','best') - ylim([-2 2]); xlim([0 n_neurons(s)+1]) - ylabel(['Cohens-d, p<' num2str(p_cutoff)]); xlabel('Units') - yline(0,'LineStyle','--') - text(10,1.5,'Increased firing relative to baseline','FontSize',14) - text(10,-1.5,'Decreased firing relative to baseline','FontSize',14) - set(gca,'FontSize',15); - title('Distribution of effect size across all units') - %saveas(gcf, [savePath '/Distribution_cohend_all_units_min20_CohenDonly.png']); pause(2); close all - - %Number of behaviors a single neuron is selective for - num_selective_behav_per_neuron{s} = sum(~isnan(cohend_thresh),2); - figure; histogram(num_selective_behav_per_neuron{s}) - xlabel('Number of behavior a given neuron is selective to') - title('Distribution of the number of behaviors single units are selective for') - %saveas(gcf, [savePath '/Distribution_number_selective_behavior_per_unit_min20_CohenDonly.png']); %pause(2); close all - - % % %%%%%%%%%%%%%%%%%%%%%%%% - % % %Single neuron selectivity: - % % figure; set(gcf,'Position',[150 250 1000 300]); - % % data = cohend_thresh(1,:); - % % [~, idx_sorted]=sort(data); - % % scatter(1:n_behav, cohend_thresh(1,idx_sorted),40, 'filled') - % % ylim([caxis_lower caxis_upper]); xlim([0 n_behav+1]) - % % ylabel(['Cohens-d, p<' num2str(cutoff)]) - % % yline(0,'LineStyle','--') - % % xticks(1:n_behav) - % % xticklabels(AxesLabels(idx_sorted)) - % % set(gca,'FontSize',15); - - close all - -end - -%% Results across sessions - -%Change savePath for all session results folder: -savePath = [home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SingleUnit_results/']; - -%Plot distribution of effect size per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(mean_cohend_per_behav)); -for s = a_sessions - scatter(1:length(idx_sort),mean_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([-1.5 1.5]); xlim([0 n_behav+1]) -ylabel(['Mean Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - scatter(1:length(idx_sort),mean_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([-1.5 1.5]); xlim([0 n_behav+1]) -ylabel(['Mean Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey H') - -sgtitle('Distribution of effect sizes per behavior','FontSize',20) -%saveas(gcf, [savePath '/Distribution_effect_size_per_behavior_min20_CohenDonly.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot proportion of selective units per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(prop_selective_per_behav)); -for s = a_sessions - prop_selective_per_behav(s,prop_selective_per_behav(s,:)==0)=nan; - scatter(1:length(idx_sort),prop_selective_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - prop_selective_per_behav(s,prop_selective_per_behav(s,:)==0)=nan; - scatter(1:length(idx_sort),prop_selective_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey H') -sgtitle('Proportion of selective units per behavior','FontSize',20) -%saveas(gcf, [savePath '/Proportion_selective_units_per_behavior_min20_CohenDonly.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot number of behaviors a single neuron is selective for across all sessions, separated by monkey - -figure; set(gcf,'Position',[150 250 1000 700]); -subplot(2,1,1); hold on -for s=a_sessions - histogram(num_selective_behav_per_neuron{s}, 'FaceAlpha',0.3) - prop_not_tuned(s) = length(find(num_selective_behav_per_neuron{s}==0))/n_neurons(s); - prop_tuned_more_than_one_behav(s) = length(find(num_selective_behav_per_neuron{s}>0))/n_neurons(s); -end -legend({sessions(a_sessions).name},'Location','eastoutside') -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2); hold on -for s=h_sessions - histogram(num_selective_behav_per_neuron{s}, 'FaceAlpha',0.3) - prop_not_tuned(s) = length(find(num_selective_behav_per_neuron{s}==0))/n_neurons(s); - prop_tuned_more_than_one_behav(s) = length(find(num_selective_behav_per_neuron{s}>0))/n_neurons(s); -end -legend({sessions(h_sessions).name},'Location','eastoutside') -xlabel('Number of behaviors a given neuron is selective for') -set(gca,'FontSize',15); -title('Monkey H') - -sgtitle('Distribution of the number of behaviors single units are selective for','FontSize',20) -%saveas(gcf, [savePath '/Number_selective_behavior_per_unit_min20_CohenDonly.png']); pause(2); close all - -mean(prop_not_tuned(prop_not_tuned>0)) -mean(prop_tuned_more_than_one_behav(prop_tuned_more_than_one_behav>0)) \ No newline at end of file diff --git a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_neighbor_vs_partner_batch.m b/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_neighbor_vs_partner_batch.m deleted file mode 100644 index 8ad4b8d..0000000 --- a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_neighbor_vs_partner_batch.m +++ /dev/null @@ -1,512 +0,0 @@ -%% Log_SingleNeuronTuning_Cohensd_neighbor_vs_partner -% This script computes firing rate of individual neuron under different -% behavioral conditions for both neighbor and partner behavior. -% Then, it computes a cohen's d (or effect size) -% difference between the distribution of firing rates during behavior X -% with firing rate during a baseline state (rest/proximity). - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_neighbor=[1:6,11:13,15:16]; -session_range_with_neighbor=[11]; - -%Set parameters -with_partner =2; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -plot_toggle = 0; %Plot individual session plots - -%Select session range: -session_range = session_range_with_neighbor; -a_sessions = 3; h_sessions = 2; - -%Initialize variables: -nbeh = 4; -median_cohend_per_behav_partner = nan(max(session_range), nbeh); -num_selective_per_behav_partner = nan(max(session_range), nbeh); -prop_selective_per_behav_partner = nan(max(session_range), nbeh); -median_cohend_per_behav_neighbor = nan(max(session_range), nbeh); -num_selective_per_behav_neighbor = nan(max(session_range), nbeh); -prop_selective_per_behav_neighbor = nan(max(session_range), nbeh); -num_either_neighbor_or_partner = nan(max(session_range), nbeh); -num_neither = nan(max(session_range), nbeh); -prop_either_neighbor_or_partner = nan(max(session_range), nbeh); -prop_neither = nan(max(session_range), nbeh); -num_neighbor_and_partner = nan(max(session_range), nbeh); -prop_neighbor_and_partner = nan(max(session_range), nbeh); -num_neighbor_but_notPartner = nan(max(session_range), nbeh); -prop_neighbor_but_notPartner = nan(max(session_range), nbeh); -num_partner_but_notPartner = nan(max(session_range), nbeh); -prop_partner_but_notPartner = nan(max(session_range), nbeh); -prop_same_direction = nan(max(session_range), nbeh); - -s=11; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/neighbor_vs_partner']; - - %% Load data - - %Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, labels_neighbor, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_neighbor(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - - session_length = size(Spike_rasters,2); % get session length - Spike_count_raster = Spike_rasters'; - - %Extract behavior labels for partner and neighbor - behavior_labels_subject_init = cell2mat({labels{:,3}}'); - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - behavior_labels_neighbor_init = cell2mat({labels_neighbor{:,3}}'); %Extract unique behavior info for neighbor - - - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - behavior_labels_neighbor_init(behavior_labels_neighbor_init==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - - %Specifcially for Hooke who doesn't self-groom much... - %behavior_labels_neighbor_init(behavior_labels_neighbor_init==find(behav_categ=="Self-groom"))=find(behav_categ=="Groom partner"); - - %% Get baseline firing from epochs where the neighbor rests idle. - - %Estimate "baseline" neural firing distribution. - %idx_rest= setdiff(find(behavior_labels_partner_init ==length(behav_categ)), idx_neighbor);%Get idx of "rest" epochs. - idx_rest = intersect(find(behavior_labels_subject_init ==length(behav_categ)),... - intersect(find(behavior_labels_partner_init ==length(behav_categ)),... - find(behavior_labels_neighbor_init ==length(behav_categ)))); - baseline_firing = Spike_rasters(:,idx_rest); - mean_baseline = mean(baseline_firing,2); - std_baseline = std(Spike_rasters(:,idx_rest),0,2); - - %% Get indices for which partner and neighbor do not behav similarly & don't engage in reciprocal behaviors - % Select behaviors manually - behav = [4,5,18,24]; %focus on a set of behaviors which happen enough time in both the partner and neighbor and are not reciprocal - - % Get indices where either: - % 1. The partner is behaving and the neighbor is resting - idx_sub = find(ismember(behavior_labels_partner_init,behav)); - idx_neighbor = find(ismember(behavior_labels_neighbor_init,behav)); %find the indices of the behaviors considered - idx_partner = setdiff(idx_sub, idx_neighbor); - idx_neighbor = setdiff(idx_neighbor, idx_sub); - idx = [idx_partner; idx_neighbor]; - - Spike_count_raster_neighbor = Spike_count_raster(idx_neighbor,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_neighbor = behavior_labels_neighbor_init(idx_neighbor);%Same as above but in behavior labels - - Spike_count_raster_partner = Spike_count_raster(idx_partner,:); - behavior_labels_partner = behavior_labels_partner_init(idx_partner); - - %Check what the partner is doing during neighbor behavior. -% behavior_labels_partner_during_neighbor_behav = behavior_labels_partner_init(idx_neighbor); -% behavior_labels_neighbor_during_partner_behav = behavior_labels_neighbor_init(idx_partner); -% figure; hold on; subplot(2,1,1); hist(behavior_labels_neighbor, 30); title('Neighbor behavior'); subplot(2,1,2); hist(behavior_labels_partner_during_neighbor_behav, 30); title('Partner behavior during neighbor idx') -% figure; hold on; subplot(2,1,1); hist(behavior_labels_partner, 30); title('Partner behavior'); subplot(2,1,2); hist(behavior_labels_neighbor_during_partner_behav, 30); title('Neighbor behavior during partner idx') - - - - %% Set parameters - unqLabels = behav; %Get unique behavior labels (exclude rest) - n_neurons(s) = size(Spike_rasters,1); %Get number of neurons - n_behav = length(unqLabels); %Get number of unique behavior labels - - - %% Compute cohen's d - - n_per_behav = nan(n_behav,2); - cohend = nan(n_neurons(s),n_behav,2); - cohend_shuffle = nan(n_neurons(s),n_behav,2); - mean_beh = nan(n_neurons(s), n_behav,2); - mean_beh_shuffle = nan(n_neurons(s), n_behav,2); - std_beh = nan(n_neurons(s), n_behav,2); - std_beh_shuffle = nan(n_neurons(s), n_behav,2); - p = nan(n_neurons(s), n_behav,2); - p_rand = nan(n_neurons(s), n_behav,2); - - for n = 1:n_neurons(s) - - for b = 1:n_behav - - idxp = find(behavior_labels_neighbor == unqLabels(b)); %get idx where behavior b occurred - idxs = find(behavior_labels_partner == unqLabels(b)); - n_per_behav(b,1)=length(idxs); n_per_behav(b,2)=length(idxp); - - - if n_per_behav(b,1)>10 & n_per_behav(b,2)>10 - - if length(idx)10 & n_per_behav(b,2)>10 - - if length(idx)min_occurrence - - if length(idx) cohend_cutoff & p_sorted < p_cutoff); sum(sum(h_sorted)) - h_shuffle_sorted = double(abs(cohend_shuffle_sorted) > cohend_cutoff & p_rand_sorted < p_cutoff); sum(sum(h_shuffle_sorted)) - cohend_thresh_sorted = h_sorted.*cohend_sorted; cohend_thresh_sorted(cohend_thresh_sorted==0)=nan; - cohend_shuffle_thresh_sorted = h_shuffle_sorted.*cohend_shuffle_sorted; cohend_shuffle_thresh_sorted(cohend_shuffle_thresh_sorted==0)=nan; - - - %Threshold cohens'd by a cohen's d AND p-value cutoff - h = double(abs(cohend) > cohend_cutoff & p < p_cutoff); sum(sum(h)) - h_shuffle = double(abs(cohend_shuffle) > cohend_cutoff & p_rand < p_cutoff); sum(sum(h_shuffle)) - - cohend_thresh = h.*cohend; cohend_thresh(cohend_thresh==0)=nan; - cohend_shuffle_thresh = h_shuffle.*cohend_shuffle; cohend_shuffle_thresh(cohend_shuffle_thresh==0)=nan; - - - %% Plot heatmaps - - AxesLabels_sorted = behav_categ(orderIdx); - AxesLabels = behav_categ(1:end-1); - caxis_upper = 1.5; - caxis_lower = -1.5; - cmap=flipud(cbrewer('div','RdBu', length(caxis_lower:0.01:caxis_upper))); - - if plot_toggle - %Plot ordered heatmaps - figure; set(gcf,'Position',[150 250 1000 500]); - hp=heatmap(cohend_sorted, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - %saveas(gcf, [savePath '/Cohend_heatmap_sorted.png']); close all - figure; set(gcf,'Position',[150 250 1000 500]); - hp=heatmap(cohend_thresh_sorted, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - %saveas(gcf, [savePath '/Cohend_heatmap_sorted_thresholded.png']); close all - - %Includes both p-value and cohen d as thresholds - figure; hold on; set(gcf,'Position',[150 250 1500 800]); - subplot(2,2,1); hp=heatmap(cohend, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap') - subplot(2,2,2); hp=heatmap(cohend_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - subplot(2,2,3); hp=heatmap(cohend_shuffle, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap SHUFFLED') - subplot(2,2,4); hp=heatmap(cohend_shuffle_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap SHUFFLED, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - %saveas(gcf, [savePath '/Cohend_heatmap_all_units.png']) - - % if with_NC == 0 - % sgtitle(['Cohens-d heatmap for all units except noise cluster']) - % saveas(gcf, [savePath '/Cohend_heatmap_NoNC_units.png']) - % elseif with_NC == 2 - % sgtitle(['Cohens-d heatmap for noise clusters ONLY']) - % saveas(gcf, [savePath '/Cohend_heatmap_NC_only.png']) - % elseif isolatedOnly - % sgtitle(['Cohens-d heatmap for isolated units']) - % saveas(gcf, [savePath '/Cohend_heatmap_isolated_units.png']) - % else - % sgtitle(['Cohens-d heatmap for all units']) - % saveas(gcf, [savePath '/Cohend_heatmap_all_units.png']) - % end - - %pause(2); - close all - end - - %%%%%%%%%%%%%%%%%%%%%%%%%%%% - %% Summary figures - - % % % figure; scatter(n_per_behav(s,:), nanmean(abs(cohend_thresh))); xlabel('n'); ylabel('Mean abs(cohend)') - % % % corrcoef(nanmean(abs(cohend_thresh)), n_per_behav(s,:),'rows','pairwise'); - % % % figure; scatter(n_per_behav(s,:), sum(~isnan(cohend_thresh))); xlabel('n'); ylabel('# Selective units') - % % % corrcoef(sum(~isnan(cohend_thresh)), n_per_behav(s,:),'rows','pairwise'); - - mean_cohend_per_behav(s,:) = nanmean(cohend_thresh); - median_cohend_per_behav(s,:) = nanmedian(cohend_thresh); - std_cohend_per_behav(s,:) = nanstd(cohend_thresh); - se_cohend_per_behav(s,:) = nanstd(cohend_thresh)./sqrt(sum(~isnan(cohend_thresh))); - prop_selective_per_behav(s,:) = sum(~isnan(cohend_thresh))/n_neurons(s); - num_selective_behav_per_neuron{s} = sum(~isnan(cohend_thresh),2); - - if plot_toggle - %Plot the distribution of effect sizes for each behavior - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_cohend_per_behav(s,:)); - boxplot(cohend_thresh(:,idx_sort)) - % scatter(1:length(idx_sort),mean_cohend_per_behav(idx_sort),60,'filled') - % errorbar(mean_cohend_per_behav(idx_sort), std_cohend_per_behav(idx_sort),'LineWidth',1.5) - % legend({'mean','standard deviation'},'Location','best') - ylim([-2.5 2.5]); xlim([0 n_behav+1]) - ylabel(['Cohens-d, p<' num2str(p_cutoff)]) - yline(0,'LineStyle','--') - text(1,1.75,'Increased firing relative to baseline','FontSize',14) - text(1,-1.75,'Decreased firing relative to baseline','FontSize',14) - xticks(1:n_behav) - xticklabels(AxesLabels(idx_sort)) - set(gca,'FontSize',15); - title('Distribution of effect size per behavior') - %saveas(gcf, [savePath '/Distribution_cohend_per_behavior.png']); %pause(2); close all - - %Plot the proprotion of selective neurons per behavior - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~,idx_sort]=sort(prop_selective_per_behav(s,:),'descend'); - scatter(1:n_behav,prop_selective_per_behav(s,idx_sort),60,'filled') - ylabel('Prop. selective units') - xticks(1:n_behav); xlim([0 n_behav+1]); ylim([0 1]) - xticklabels(AxesLabels(idx_sort)) - set(gca,'FontSize',15); - title(['Proportion of selective units per behavior']) - %saveas(gcf, [savePath '/Proportion_units_selective_per_behav.png']); %pause(2); close all - - % Variance in single neuron selectivity - mean_cohend_per_neuron = nanmean(cohend_thresh,2); - std_cohend_per_neuron = nanstd(cohend_thresh,0,2); - - figure; hold on; set(gcf,'Position',[150 250 1000 500]); - [~, idx_sort]=sort(mean_cohend_per_neuron); - scatter(1:length(idx_sort),mean_cohend_per_neuron(idx_sort),20,'filled') - errorbar(mean_cohend_per_neuron(idx_sort), std_cohend_per_neuron(idx_sort),'LineWidth',1.5) - legend({'mean','standard deviation'},'Location','best') - ylim([-2 2]); xlim([0 n_neurons(s)+1]) - ylabel(['Cohens-d, p<' num2str(p_cutoff)]); xlabel('Units') - yline(0,'LineStyle','--') - text(10,1.5,'Increased firing relative to baseline','FontSize',14) - text(10,-1.5,'Decreased firing relative to baseline','FontSize',14) - set(gca,'FontSize',15); - title('Distribution of effect size across all units') - %saveas(gcf, [savePath '/Distribution_cohend_all_units.png']); pause(2); close all - - %Number of behaviors a single neuron is selective for - figure; histogram(num_selective_behav_per_neuron{s}) - xlabel('Number of behavior a given neuron is selective to') - title('Distribution of the number of behaviors single units are selective for') - %saveas(gcf, [savePath '/Distribution_number_selective_behavior_per_unit.png']); %pause(2); close all - - close all - end - - clear firing_baseline - -end - -%% Results across sessions - -%Change savePath for all session results folder: -savePath = [home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SingleUnit_results/']; - -%Plot mean effect size per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(mean_cohend_per_behav)); -for s = a_sessions - scatter(1:length(idx_sort),mean_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) - %pause(1) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([-2 2]); xlim([0 n_behav+1]) -ylabel(['Mean Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - scatter(1:length(idx_sort),mean_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) - %pause(1) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([-2 2]); xlim([0 n_behav+1]) -ylabel(['Mean Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey H') - -sgtitle('Mean effect sizes per behavior','FontSize',20) -saveas(gcf, [savePath '/Mean_effect_size_per_behavior.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot median effect size per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(median_cohend_per_behav)); -for s = a_sessions - scatter(1:length(idx_sort),median_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) - %pause(1) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([-2 2]); xlim([0 n_behav+1]) -ylabel(['Median Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - scatter(1:length(idx_sort),median_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) - %pause(1) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([-2 2]); xlim([0 n_behav+1]) -ylabel(['Median Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey H') - -sgtitle('Median effect sizes per behavior','FontSize',20) -saveas(gcf, [savePath '/Median_effect_size_per_behavior.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot proportion of selective units per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(prop_selective_per_behav)); -for s = a_sessions - prop_selective_per_behav(s,prop_selective_per_behav(s,:)==0)=nan; - scatter(1:length(idx_sort),prop_selective_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - prop_selective_per_behav(s,prop_selective_per_behav(s,:)==0)=nan; - scatter(1:length(idx_sort),prop_selective_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Monkey H') -sgtitle('Proportion of selective units per behavior','FontSize',20) -saveas(gcf, [savePath '/Proportion_selective_units_per_behavior.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot number of behaviors a single neuron is selective for across all sessions, separated by monkey - -figure; set(gcf,'Position',[150 250 1000 700]); -subplot(2,1,1); hold on -for s=a_sessions - histogram(num_selective_behav_per_neuron{s}, 'FaceAlpha',0.3) - prop_not_tuned(s) = length(find(num_selective_behav_per_neuron{s}==0))/n_neurons(s); - prop_tuned_more_than_one_behav(s) = length(find(num_selective_behav_per_neuron{s}>0))/n_neurons(s); -end -legend({sessions(a_sessions).name},'Location','eastoutside') -set(gca,'FontSize',15); -title('Monkey A') - -subplot(2,1,2); hold on -for s=h_sessions - histogram(num_selective_behav_per_neuron{s}, 'FaceAlpha',0.3) - prop_not_tuned(s) = length(find(num_selective_behav_per_neuron{s}==0))/n_neurons(s); - prop_tuned_more_than_one_behav(s) = length(find(num_selective_behav_per_neuron{s}>0))/n_neurons(s); -end -legend({sessions(h_sessions).name},'Location','eastoutside') -xlabel('Number of behaviors a given neuron is selective for') -set(gca,'FontSize',15); -title('Monkey H') - -sgtitle('Distribution of the number of behaviors single units are selective for','FontSize',20) -saveas(gcf, [savePath '/Number_selective_behavior_per_unit.png']); pause(2); close all - -mean(prop_not_tuned(prop_not_tuned>0)) -mean(prop_tuned_more_than_one_behav(prop_tuned_more_than_one_behav>0)) \ No newline at end of file diff --git a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_partner_batch.m b/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_partner_batch.m deleted file mode 100644 index 3ae833c..0000000 --- a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_partner_batch.m +++ /dev/null @@ -1,428 +0,0 @@ -%% Log_SingleNeuronTuning_Cohensd -% This script computes firing rate of individual neuron under different -% behavioral conditions. Then, it computes a cohen's d (or effect size) -% difference between the distribution of firing rates during behavior X -% with a baseline firing rate. - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; - -%Set parameters -with_partner =1; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -cohend_cutoff=0; p_cutoff=0.01;%Set thresholds -plot_toggle = 1; -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) - - -% % %Initialize session batch variables: -% % n_behav = 28; -% % mean_cohend_per_behav = nan(length(sessions), n_behav ); -% % prop_selective_per_behav = nan(length(sessions), n_behav ); -% % num_selective_behav_per_neuron=cell(1,length(sessions)); - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - %savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/Partner_behav']; - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/partner_vs_subject']; - - %% Load data - - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data loaded') - - session_length = size(Spike_rasters,2); % get session length - - %Extract behavioral labels - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); %Consider 'approach' to be 'Travel'. - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); %Consider 'approach' to be 'Travel'. - - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="leave"))=find(behav_categ=="Travel"); %Consider 'leave' to be 'Travel'. - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); %Consider 'leave' to be 'Travel'. - - block_labels = cell2mat({labels{:,11}}'); %Extract block info - alone_block_id = find(strcmp(block_times{:,"Behavior"},"Alone.block")); - - %% Get baseline firing from epochs where the subject rests idle. - - %Estimate "baseline" neural firing distribution. - %idx_rest = intersect(find(behavior_labels_subject_init ==length(behav_categ)), find(behavior_labels_partner_init ==length(behav_categ))); - idx_rest = find(behavior_labels_subject_init ==length(behav_categ)); - baseline_firing = Spike_rasters(:,idx_rest); - mean_baseline = mean(baseline_firing,2); - std_baseline = std(Spike_rasters(:,idx_rest),0,2); - - % %Check visually that baseline is taken from epochs throughout the session - % y=zeros(1, session_length); y(idx_rest)=1; - % figure; plot(1:session_length, y); ylim([-0.5, 1.5]) - % yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) - % xlabel('Time in s'); title('Baseline epochs') - % set(gca,'FontSize',15); - % close all - - %% Select non-reciprocal behaviors and time points where the behavior differs between partner and subject - - % Select non-reciprocal behaviors - %behav = 1:length(behav_categ)-1; %exclude rest - behav = [4,5,18,24]; - %behav = setdiff(behav, reciprocal_set); - - % Only keep the behaviors of interest - idx = find(ismember(behavior_labels_partner_init,behav) &... - ~ismember(behavior_labels_subject_init,behav) &... - block_labels~=alone_block_id); %find the indices of the behaviors considered - Spike_raster_final = Spike_rasters(:,idx);%Only keep timepoints where the behaviors of interest occur in spiking data - - %Only consider windows where the behaviors of subject and - %partner do not overlap - subject_behav_after_selection = behavior_labels_subject_init(idx); - partner_behav_after_selection = behavior_labels_partner_init(idx); - behavior_labels_final = partner_behav_after_selection;%Same as above but in behavior labels - - behav_freq_table = tabulate(behavior_labels_final); - -% % Select behaviors with a minimum # of occurrences -% min_occurrences=30; -% behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %% Set parameters - unqLabels = 1:length(behav_categ)-1; %Get unique behavior labels (exclude rest) - n_neurons(s) = size(Spike_rasters,1); %Get number of neurons - n_behav = length(unqLabels); %Get number of unique behavior labels - - %% Compute cohen's d - - n_per_behav = nan(n_behav,1); - cohend = nan(n_neurons(s),n_behav); - cohend_shuffle = nan(n_neurons(s),n_behav); - mean_beh = nan(n_neurons(s), n_behav); - mean_beh_shuffle = nan(n_neurons(s), n_behav); - std_beh = nan(n_neurons(s), n_behav); - std_beh_shuffle = nan(n_neurons(s), n_behav); - p = nan(n_neurons(s), n_behav); - p_rand = nan(n_neurons(s), n_behav); - - for n = 1:n_neurons(s) - - for b = 1:n_behav - idx = find(behavior_labels_final == unqLabels(b)); %get idx where behavior b occurred - n_per_behav(b)=length(idx); - - if n_per_behav(b)>10 - - if length(idx) cohend_cutoff & p_sorted < p_cutoff); sum(sum(h_sorted)) - h_shuffle_sorted = double(abs(cohend_shuffle_sorted) > cohend_cutoff & p_rand_sorted < p_cutoff); sum(sum(h_shuffle_sorted)) - cohend_thresh_sorted = h_sorted.*cohend_sorted; cohend_thresh_sorted(cohend_thresh_sorted==0)=nan; - cohend_shuffle_thresh_sorted = h_shuffle_sorted.*cohend_shuffle_sorted; cohend_shuffle_thresh_sorted(cohend_shuffle_thresh_sorted==0)=nan; - - - %Threshold cohens'd by a cohen's d AND p-value cutoff - h = double(abs(cohend) > cohend_cutoff & p < p_cutoff); sum(sum(h)) - h_shuffle = double(abs(cohend_shuffle) > cohend_cutoff & p_rand < p_cutoff); sum(sum(h_shuffle)) - - cohend_thresh = h.*cohend; cohend_thresh(cohend_thresh==0)=nan; - cohend_shuffle_thresh = h_shuffle.*cohend_shuffle; cohend_shuffle_thresh(cohend_shuffle_thresh==0)=nan; - - %% Get summarized values - mean_cohend_per_behav(s,:) = nanmean(cohend_thresh); - prop_selective_per_behav(s,:) = sum(~isnan(cohend_thresh))/n_neurons(s); - num_selective_behav_per_neuron{s} = sum(~isnan(cohend_thresh),2); - - - %% Plot heatmaps - - AxesLabels_sorted = behav_categ(orderIdx); - AxesLabels = behav_categ; - caxis_upper = 1.5; - caxis_lower = -1.5; - cmap=flipud(cbrewer('div','RdBu', length(caxis_lower:0.01:caxis_upper))); - - if plot_toggle - %Plot ordered heatmap - figure; %set(gcf,'Position',[150 250 1000 500]); - [nanrow nancol]=find(~isnan(cohend_sorted)); nancol = unique(nancol); - hp=heatmap(cohend_sorted(:,nancol), 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted(nancol); caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap']) - ax = gca; - ax.FontSize = 14; - saveas(gcf, [savePath '/Cohend_heatmap_sorted_partner.pdf']); close all - - %Plot ordered heatmap thresholded - figure; %set(gcf,'Position',[150 250 1000 500]); - hp=heatmap(cohend_thresh_sorted(:,nancol), 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels_sorted(nancol); caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) - ax = gca; - ax.FontSize = 14; - saveas(gcf, [savePath '/Cohend_heatmap_sorted_thresholded_partner.pdf']); close all - -% %Includes both p-value and cohen d as thresholds -% figure; hold on; set(gcf,'Position',[150 250 1500 800]); -% subplot(2,2,1); hp=heatmap(cohend, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap') -% subplot(2,2,2); hp=heatmap(cohend_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) -% subplot(2,2,3); hp=heatmap(cohend_shuffle, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Cohens-d heatmap SHUFFLED') -% subplot(2,2,4); hp=heatmap(cohend_shuffle_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cmap); hp.XDisplayLabels = AxesLabels; caxis([caxis_lower caxis_upper]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title(['Cohens-d heatmap SHUFFLED, p<' num2str(p_cutoff) ' and cohend>' num2str(cohend_cutoff)]) -% saveas(gcf, [savePath '/Cohend_heatmap_all_units_partner.png']) - - %%%%%%%%%%%%%%%%%%%%%%%%%%%% - %% Summary figures - -% % figure; scatter(nanmean(abs(cohend_thresh)), n_per_behav); corrcoef(nanmean(abs(cohend_thresh)), n_per_behav,'rows','pairwise') -% % figure; scatter(sum(~isnan(cohend_thresh)), n_per_behav); corrcoef(sum(~isnan(cohend_thresh)), n_per_behav,'rows','pairwise') - - -% % %Plot the distribution of effect sizes for each behavior -% % figure; hold on; set(gcf,'Position',[150 250 1000 500]); -% % boxchart(cohend_thresh_sorted(:,nancol)) -% % % scatter(1:length(idx_sort),mean_cohend_per_behav(idx_sort),60,'filled') -% % % errorbar(mean_cohend_per_behav(idx_sort), std_cohend_per_behav(idx_sort),'LineWidth',1.5) -% % % legend({'mean','standard deviation'},'Location','best') -% % ylim([-2.5 2.5]); %xlim([0 length(nancol)+1]) -% % ylabel(['Cohens-d, p<' num2str(p_cutoff)]) -% % yline(0,'LineStyle','--') -% % text(1,1.75,'Increased firing relative to baseline','FontSize',14) -% % text(1,-1.75,'Decreased firing relative to baseline','FontSize',14) -% % %xticks(1:length(nancol)) -% % xticklabels(AxesLabels_sorted(nancol)) -% % set(gca,'FontSize',15); -% % title('Distribution of effect size per partner behavior') -% % saveas(gcf, [savePath '/Distribution_cohend_per_PARTNER_behavior.png']); close all - -% % %Plot the proportion of selective neurons per behavior -% % figure; hold on; set(gcf,'Position',[150 250 1000 500]); -% % [~,idx_sort]=sort(prop_selective_per_behav(s,:),'descend'); -% % scatter(1:n_behav,prop_selective_per_behav(s,idx_sort),60,'filled') -% % ylabel('Prop. selective units') -% % xticks(1:n_behav); xlim([0 n_behav+1]); ylim([0 1]) -% % xticklabels(AxesLabels(idx_sort)) -% % set(gca,'FontSize',15); -% % title('Proportion of units selective per partner behavior') -% % saveas(gcf, [savePath '/Proportion_units_selective_per_PARTNER_behav.png']); close all -% % -% % % Variance in single neuron selectivity -% % mean_cohend_per_neuron = nanmean(cohend_thresh,2); -% % std_cohend_per_neuron = nanstd(cohend_thresh,0,2); -% % -% % figure; hold on; set(gcf,'Position',[150 250 1000 500]); -% % [~, idx_sort]=sort(mean_cohend_per_neuron); -% % scatter(1:length(idx_sort),mean_cohend_per_neuron(idx_sort),20,'filled') -% % errorbar(mean_cohend_per_neuron(idx_sort), std_cohend_per_neuron(idx_sort),'LineWidth',1.5) -% % legend({'mean','standard deviation'},'Location','best') -% % ylim([-2 2]); xlim([0 n_neurons(s)+1]) -% % ylabel(['Cohens-d, p<' num2str(p_cutoff)]); xlabel('Units') -% % yline(0,'LineStyle','--') -% % text(10,1.5,'Increased firing relative to baseline','FontSize',14) -% % text(10,-1.5,'Decreased firing relative to baseline','FontSize',14) -% % set(gca,'FontSize',15); -% % title('Distribution of effect size across all units, PARTNER') -% % saveas(gcf, [savePath '/Distribution_cohend_all_units_PARTNER.png']); close all -% % -% % %Number of behaviors a single neuron is selective for -% % figure; histogram(num_selective_behav_per_neuron{s}) -% % xlabel('Number of behavior a given neuron is selective to') -% % title('Distribution of the number of behaviors single units are selective for') -% % saveas(gcf, [savePath '/Distribution_number_selective_PARTNER_behavior_per_unit.png']); close all - - % % %%%%%%%%%%%%%%%%%%%%%%%% - % % %Single neuron selectivity: - % % figure; set(gcf,'Position',[150 250 1000 300]); - % % data = cohend_thresh(1,:); - % % [~, idx_sorted]=sort(data); - % % scatter(1:n_behav, cohend_thresh(1,idx_sorted),40, 'filled') - % % ylim([caxis_lower caxis_upper]); xlim([0 n_behav+1]) - % % ylabel(['Cohens-d, p<' num2str(cutoff)]) - % % yline(0,'LineStyle','--') - % % xticks(1:n_behav) - % % xticklabels(AxesLabels(idx_sorted)) - % % set(gca,'FontSize',15); - end - - close all - - -end - -%% Results across sessions - -%Change savePath for all session results folder: -savePath = [home '/Dropbox (Penn)/Datalogger/Results/All_sessions/SingleUnit_results/']; -mean_cohend_per_behav(mean_cohend_per_behav==0)=NaN; -prop_selective_per_behav(prop_selective_per_behav==0)=NaN; - - -%Plot distribution of effect size per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(mean_cohend_per_behav)); -for s = a_sessions - scatter(1:length(idx_sort),mean_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([-1.5 1.5]); xlim([0 n_behav+1]) -ylabel(['Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Distribution of effect size per behavior, Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - scatter(1:length(idx_sort),mean_cohend_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) - %errorbar(mean_cohend_per_behav(s,idx_sort), std_cohend_per_behav(s,idx_sort),'LineWidth',1.5) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([-1.5 1.5]); xlim([0 n_behav+1]) -ylabel(['Cohens-d, p<' num2str(p_cutoff)]) -yline(0,'LineStyle','--') -% text(20,0.15,'Increased firing relative to baseline','FontSize',14) -% text(20,-0.15,'Decreased firing relative to baseline','FontSize',14) -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Distribution of effect size per behavior, Monkey H') -saveas(gcf, [savePath '/Distribution_effect_size_per_behavior_PARTNER.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot proportion of selective units per behavior across all sessions, separated by monkey -figure; set(gcf,'Position',[150 250 1000 800]); -subplot(2,1,1);hold on; -[~, idx_sort]=sort(nanmean(prop_selective_per_behav)); -for s = a_sessions - prop_selective_per_behav(s,prop_selective_per_behav(s,:)==0)=nan; - scatter(1:length(idx_sort),prop_selective_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) -end -legend({sessions(a_sessions).name},'Location','eastoutside') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Proportion of selective units per behavior, Monkey A') - -subplot(2,1,2);hold on; -for s = h_sessions - prop_selective_per_behav(s,prop_selective_per_behav(s,:)==0)=nan; - scatter(1:length(idx_sort),prop_selective_per_behav(s,idx_sort),60,'filled','MarkerFaceAlpha',.7) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -ylim([0 1]); xlim([0 n_behav+1]) -ylabel(['Proportion of selective units']) -yline(0,'LineStyle','--') -xticks(1:n_behav) -xticklabels(AxesLabels(idx_sort)) -set(gca,'FontSize',15); -title('Proportion of selective units per behavior, Monkey H') -saveas(gcf, [savePath '/Proportion_selective_units_per_behavior_PARTNER.png']); pause(2); close all - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%Plot number of behaviors a single neuron is selective for across all sessions, separated by monkey - -figure -histogram(vertcat(num_selective_behav_per_neuron{:})) -length(find(vertcat(num_selective_behav_per_neuron{:})~=0))/sum(n_neurons) -length(find(vertcat(num_selective_behav_per_neuron{:})>1))/sum(n_neurons) -xlabel('Number of behaviors a given neuron is selective for') -ylabel('Neuron count') -set(gca,'FontSize',15); - -figure; set(gcf,'Position',[150 250 1000 700]); -subplot(2,1,1); hold on -for s=a_sessions - histogram(num_selective_behav_per_neuron{s}) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -xlabel('Number of behavior a given neuron is selective for') -set(gca,'FontSize',15); -title('Distribution of the number of behaviors single units are selective for, Monkey A') - -subplot(2,1,2); hold on -for s=h_sessions - histogram(num_selective_behav_per_neuron{s}) -end -legend({sessions(h_sessions).name},'Location','eastoutside') -xlabel('Number of behaviors a given neuron is selective for') -set(gca,'FontSize',15); -title('Distribution of the number of behaviors single units are selective for, Monkey H') -saveas(gcf, [savePath '/Number_selective_behavior_per_unit_PARTNER.png']); pause(2); close all - diff --git a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_partner_vs_subject_batch.m b/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_partner_vs_subject_batch.m deleted file mode 100644 index 1a70374..0000000 --- a/Single neuron/old_code/log_SingleNeuronTuning_Cohensd_partner_vs_subject_batch.m +++ /dev/null @@ -1,529 +0,0 @@ -%% Log_SingleNeuronTuning_Cohensd_partner_vs_subject -% This script computes firing rate of individual neuron under different -% behavioral conditions for both partner and subject behavior. -% Then, it computes a cohen's d (or effect size) -% difference between the distribution of firing rates during behavior X -% with firing rate during a baseline state (rest/proximity). - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =1; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units -plot_toggle = 1; %Plot individual session plots -min_occurrence =30; -cohend_cutoff=0.3; p_cutoff=0.01;%Set thresholds -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -%Initialize variables: -nbeh = 4; -median_cohend_per_behav_subject = nan(max(session_range), nbeh); -num_selective_per_behav_subject = nan(max(session_range), nbeh); -prop_selective_per_behav_subject = nan(max(session_range), nbeh); -median_cohend_per_behav_partner = nan(max(session_range), nbeh); -num_selective_per_behav_partner = nan(max(session_range), nbeh); -prop_selective_per_behav_partner = nan(max(session_range), nbeh); -num_either_partner_or_subject = nan(max(session_range), nbeh); -num_neither = nan(max(session_range), nbeh); -prop_either_partner_or_subject = nan(max(session_range), nbeh); -prop_neither = nan(max(session_range), nbeh); -num_partner_and_subject = nan(max(session_range), nbeh); -prop_partner_and_subject = nan(max(session_range), nbeh); -num_partner_but_notSubject = nan(max(session_range), nbeh); -prop_partner_but_notSubject = nan(max(session_range), nbeh); -num_subject_but_notSubject = nan(max(session_range), nbeh); -prop_subject_but_notSubject = nan(max(session_range), nbeh); -prop_same_direction = nan(max(session_range), nbeh); - -s=1; -for s =session_range(3:6) %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/partner_vs_subject']; - - %% Load data - - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - session_length = size(Spike_rasters,2); % get session length - - %Extract behavior labels for subject and partner - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); %Consider 'approach' to be 'Travel'. - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); %Consider 'approach' to be 'Travel'. - - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="leave"))=find(behav_categ=="Travel"); %Consider 'leave' to be 'Travel'. - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); %Consider 'leave' to be 'Travel'. - - block_labels = cell2mat({labels{:,11}}'); %Extract block info - alone_block_id = find(strcmp(block_times{:,"Behavior"},"Alone.block")); - - %% Get baseline firing from epochs where the partner rests idle. - - %Estimate "baseline" neural firing distribution. - idx_rest = intersect(find(behavior_labels_subject_init ==length(behav_categ)), find(behavior_labels_partner_init ==length(behav_categ))); -% idx_rest = find(behavior_labels_subject_init ==length(behav_categ)); - baseline_firing = Spike_rasters(:,idx_rest); - mean_baseline = mean(baseline_firing,2); - std_baseline = std(Spike_rasters(:,idx_rest),0,2); - - %% Get indices for which subject and partner do not behav similarly & don't engage in reciprocal behaviors - % Select behaviors manually - behav = [4,5,18,24]; %focus on a set of behaviors which happen enough time in both the subject and partner and are not reciprocal - - % Get indices where either: - % 1. The subject is behaving and the partner is resting - idx_sub = find(ismember(behavior_labels_subject_init,behav)); - idx_part = find(ismember(behavior_labels_partner_init,behav) &... - ~ismember(behavior_labels_subject_init,behav));% &... - %block_labels~=alone_block_id); %find the indices of the behaviors considered - - Spike_rasters_partner = Spike_rasters(:,idx_part);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_partner = behavior_labels_partner_init(idx_part);%Same as above but in behavior labels - - Spike_rasters_subject = Spike_rasters(:,idx_sub); - behavior_labels_subject = behavior_labels_subject_init(idx_sub); - - %Check what the subject is doing during partner behavior. -% behavior_labels_subject_during_partner_behav = behavior_labels_subject_init(idx_partner); -% behavior_labels_partner_during_subject_behav = behavior_labels_partner_init(idx_subject); -% figure; hold on; subplot(2,1,1); hist(behavior_labels_partner, 30); title('Partner behavior'); subplot(2,1,2); hist(behavior_labels_subject_during_partner_behav, 30); title('Subject behavior during partner idx') -% figure; hold on; subplot(2,1,1); hist(behavior_labels_subject, 30); title('Subject behavior'); subplot(2,1,2); hist(behavior_labels_partner_during_subject_behav, 30); title('Partner behavior during subject idx') - - - - %% Set parameters - unqLabels = behav; %Get unique behavior labels (exclude rest) - n_neurons(s) = size(Spike_rasters,1); %Get number of neurons - n_behav = length(unqLabels); %Get number of unique behavior labels - - - %% Compute cohen's d - - n_per_behav = nan(n_behav,2); - cohend = nan(n_neurons(s),n_behav,2); - cohend_shuffle = nan(n_neurons(s),n_behav,2); - mean_beh = nan(n_neurons(s), n_behav,2); - mean_beh_shuffle = nan(n_neurons(s), n_behav,2); - std_beh = nan(n_neurons(s), n_behav,2); - std_beh_shuffle = nan(n_neurons(s), n_behav,2); - p = nan(n_neurons(s), n_behav,2); - p_rand = nan(n_neurons(s), n_behav,2); - - for n = 1:n_neurons(s) - - for b = 1:n_behav - - idxp = find(behavior_labels_partner == unqLabels(b)); %get idx where behavior b occurred - idxs = find(behavior_labels_subject == unqLabels(b)); - n_per_behav(b,1)=length(idxs); n_per_behav(b,2)=length(idxp); - - - if n_per_behav(b,1)>10 & n_per_behav(b,2)>10 - - if length(idxs)min_occurrences); -sqW_shuffle_minobs = sqW(:,n_per_behav>min_occurrences); - - p_values = 1 - normcdf(abs(sqW),0,1); %Looks like computation of p-value only works for positive Wstats. - p_values_shuffle = 1 - normcdf(abs(sqW_shuffle),0,1); -cutoff = 0.0000001; %set as desired - -h = double(p_values < cutoff); sum(sum(h)) -h_shuffle = double(p_values_shuffle < cutoff); sum(sum(h_shuffle)) - -sqW_thresh = h.*sqW; sqW_thresh(sqW_thresh==0)=nan; -sqW_shuffle_thresh = h_shuffle.*sqW_shuffle; sqW_shuffle_thresh(sqW_shuffle_thresh==0)=nan; - -AxesLabels = behav_categ(1:end-1); -% figure; hp=heatmap(h); hp.XDisplayLabels = AxesLabels; -% figure; hp=heatmap(h_shuffle); hp.XDisplayLabels = AxesLabels; - -figure; hold on; set(gcf,'Position',[150 250 1500 800]); -subplot(2,2,1); hp=heatmap(sqW, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cool); hp.XDisplayLabels = AxesLabels; caxis([-50 50]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Wald-test heatmap') -subplot(2,2,2); hp=heatmap(sqW_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cool); hp.XDisplayLabels = AxesLabels; caxis([-50 50]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Wald-test heatmap thresholded') -subplot(2,2,3); hp=heatmap(sqW_shuffle, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cool); hp.XDisplayLabels = AxesLabels; caxis([-50 50]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Wald-test heatmap SHUFFLED') -subplot(2,2,4); hp=heatmap(sqW_shuffle_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cool); hp.XDisplayLabels = AxesLabels; caxis([-50 50]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Wald-test heatmap SHUFFLED thesholded') - -if with_NC == 0 - sgtitle(['Wald statistics heatmap for all units except noise cluster']) - saveas(gcf, [savePath '/Selectivity_heatmap/Wald_stats_heatmap_NoNC_units.png']) -elseif isolatedOnly - sgtitle(['Wald statistics heatmap for isolated units']) - saveas(gcf, [savePath '/Selectivity_heatmap/Wald_stats_heatmap_isolated_units.png']) -else - sgtitle(['Wald statistics heatmap for all units']) - saveas(gcf, [savePath '/Selectivity_heatmap/Wald_stats_heatmap_all_units.png']) -end - -histogram(sqW); nanmean(nanmean(sqW)) - -close all - diff --git a/Single neuron/old_code/log_SingleNeuronTuning_poissonFit_v2.m b/Single neuron/old_code/log_SingleNeuronTuning_poissonFit_v2.m deleted file mode 100644 index 7dbe52a..0000000 --- a/Single neuron/old_code/log_SingleNeuronTuning_poissonFit_v2.m +++ /dev/null @@ -1,132 +0,0 @@ -%% Log_FiringRatePerBehavior -% This script generates firing rate of individual neuron under different -% behavioral conditions (considering individual secons as independent). - -% Load data -%Set path -is_mac = 1; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -clearvars -except savePath filePath is_mac - -%Set parameters -temp_resolution = 1; -channel_flag = "all"; -with_NC =0; isolatedOnly=0; - -%Get data with specified temporal resolution and channels -[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - -session_length = size(Spike_rasters,2); % get session length - -%Extract behavior labels -behavior_labels = cell2mat({labels{:,3}}');%Get behavior label from labels structure -behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - -%Set parameters -unqLabels = 1:max(behavior_labels)-1; %Get unique behavior labels (exclude rest) -n_neurons = size(Spike_rasters,1); %Get number of neurons -n_behav = length(unqLabels); %Get number of unique behavior labels -min_occurrences =90; %set the minimum number of occurrences for a behavior to be considered in statistical analyses (i.e. seconds where behavior occurs) -%set_occurrences =90; - -%Estimate "baseline" neural firing distribution. -idx_rest=find(behavior_labels==length(behav_categ));%Get idx of "rest" epochs. -baseline_firing = mean(Spike_rasters(:,idx_rest),2); - -%Check visually that baseline is taken from epochs throughout the session -y=zeros(1, session_length); y(idx_rest)=1; -figure; plot(1:session_length, y); ylim([-0.5, 1.5]) -yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) -xlabel('Time in s'); title('Baseline epochs') - -%Fit Poisson -%hat because we are getting it from the fit - -lambda_hat_baseline = nan(n_neurons,1); %Just doing whole session as baseline -lambda_hb_ci = nan(n_neurons,2); %confidence interval for above -lambda_hat_behav = nan(n_neurons,n_behav); %One for each behavior for each neuron -lambda_hbeh_ci = nan(n_neurons,2,n_behav); %confidence interval for above -lambda_hat_behav_shuffle = nan(n_neurons, n_behav); %One for each behavior for each neuron -lambda_hbeh_ci_shuffle = nan(n_neurons,2, n_behav); %confidence interval for above -for n = 1:n_neurons - - [lambda_hat_baseline(n,1), lambda_hb_ci(n,:)] = poissfit(Spike_rasters(n,idx_rest)); %Estimating mean firing rate - - for b = 1:n_behav - idx = find(behavior_labels == unqLabels(b)); %get idx where behavior b occurred - n_per_behav(b)=length(idx); - [lambda_hat_behav(n,b), lambda_hbeh_ci(n,:,b)] = poissfit(Spike_rasters(n, idx)); - [lambda_hat_behav_shuffle(n,b), lambda_hbeh_ci_shuffle(n,:,b)] = poissfit(Spike_rasters(n, randsample(idx_rest,length(idx)))); - %[lambda_hat_behav_shuffle(n,b), lambda_hbeh_ci_shuffle(n,:,b)] = poissfit(Spike_rasters(n, randsample(1:length(behavior_labels),length(idx)))); - end - -end - - -%Review logic behind (i.e. my manuscript) this but go from 95% ci to se using below -lambda_hb_se = diff(lambda_hb_ci,1,2)/2/1.96; -lambda_hbeh_se = squeeze(diff(lambda_hbeh_ci,1,2)/2/1.96); %Get width of confidence interval, scale back to se -lambda_hbeh_se_shuffle = squeeze(diff(lambda_hbeh_ci_shuffle,1,2)/2/1.96); %Get width of confidence interval, scale back to se - - -sqW = nan(size(lambda_hat_behav)); -sqW_shuffle = nan(size(lambda_hat_behav)); - -for n = 1:n_neurons - for b = 1:n_behav - sqW(n,b) = (lambda_hat_behav(n,b) - lambda_hat_baseline(n,1))./sqrt(lambda_hbeh_se(n,b).^2 + lambda_hb_se(n).^2); - sqW_shuffle(n,b) = (lambda_hat_behav_shuffle(n,b) - lambda_hat_baseline(n,1))./sqrt(lambda_hbeh_se_shuffle(n,b).^2 + lambda_hb_se(n).^2); - end -end - -sqW_minobs = sqW(:,n_per_behav>min_occurrences); -sqW_shuffle_minobs = sqW(:,n_per_behav>min_occurrences); - - p_values = 1 - normcdf(abs(sqW),0,1); %Looks like computation of p-value only works for positive Wstats. - p_values_shuffle = 1 - normcdf(abs(sqW_shuffle),0,1); -cutoff = 0.0000001; %set as desired - -h = double(p_values < cutoff); sum(sum(h)) -h_shuffle = double(p_values_shuffle < cutoff); sum(sum(h_shuffle)) - -sqW_thresh = h.*sqW; sqW_thresh(sqW_thresh==0)=nan; -sqW_shuffle_thresh = h_shuffle.*sqW_shuffle; sqW_shuffle_thresh(sqW_shuffle_thresh==0)=nan; - -AxesLabels = behav_categ(1:end-1); -% figure; hp=heatmap(h); hp.XDisplayLabels = AxesLabels; -% figure; hp=heatmap(h_shuffle); hp.XDisplayLabels = AxesLabels; - -figure; hold on; set(gcf,'Position',[150 250 1500 800]); -subplot(2,2,1); hp=heatmap(sqW, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cool); hp.XDisplayLabels = AxesLabels; caxis([-50 50]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Wald-test heatmap') -subplot(2,2,2); hp=heatmap(sqW_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cool); hp.XDisplayLabels = AxesLabels; caxis([-50 50]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Wald-test heatmap thresholded') -subplot(2,2,3); hp=heatmap(sqW_shuffle, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cool); hp.XDisplayLabels = AxesLabels; caxis([-50 50]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Wald-test heatmap SHUFFLED') -subplot(2,2,4); hp=heatmap(sqW_shuffle_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cool); hp.XDisplayLabels = AxesLabels; caxis([-50 50]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Wald-test heatmap SHUFFLED thesholded') - -if with_NC == 0 - sgtitle(['Wald statistics heatmap for all units except noise cluster']) - saveas(gcf, [savePath '/Selectivity_heatmap/Wald_stats_heatmap_NoNC_units.png']) -elseif isolatedOnly - sgtitle(['Wald statistics heatmap for isolated units']) - saveas(gcf, [savePath '/Selectivity_heatmap/Wald_stats_heatmap_isolated_units.png']) -else - sgtitle(['Wald statistics heatmap for all units']) - saveas(gcf, [savePath '/Selectivity_heatmap/Wald_stats_heatmap_all_units.png']) -end - -histogram(sqW); nanmean(nanmean(sqW)) - -close all - diff --git a/Single neuron/old_code/log_SingleNeuronTuning_v2.asv b/Single neuron/old_code/log_SingleNeuronTuning_v2.asv deleted file mode 100644 index 4cd799c..0000000 --- a/Single neuron/old_code/log_SingleNeuronTuning_v2.asv +++ /dev/null @@ -1,130 +0,0 @@ -%% Log_FiringRatePerBehavior -% This script generates firing rate of individual neuron under different -% behavioral conditions (considering individual secons as independent). - -% Load data -%Set path -is_mac = 1; -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') -end -savePath = uigetdir('', 'Please select the result directory'); - -clearvars -except savePath filePath is_mac - -%Set parameters -temp_resolution = 1; -channel_flag = "all"; -with_NC =1; isolatedOnly=0; - -%Get data with specified temporal resolution and channels -[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - -session_length = size(Spike_rasters,2); % get session length - -%Extract behavior labels -behavior_labels = cell2mat({labels{:,3}}');%Get behavior label from labels structure -behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - -%Set parameters -unqLabels = 1:max(behavior_labels)-1; %Get unique behavior labels (exclude rest) -n_neurons = size(Spike_rasters,1); %Get number of neurons -n_behav = length(unqLabels); %Get number of unique behavior labels -min_occurrences =90; %set the minimum number of occurrences for a behavior to be considered in statistical analyses (i.e. seconds where behavior occurs) -%set_occurrences =90; - -%Estimate "baseline" neural firing distribution. -idx_rest=find(behavior_labels==length(behav_categ));%Get idx of "rest" epochs. -baseline_firing = mean(Spike_rasters(:,idx_rest),2); - -%Check visually that baseline is taken from epochs throughout the session -y=zeros(1, session_length); y(idx_rest)=1; -figure; plot(1:session_length, y); ylim([-0.5, 1.5]) -yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) -xlabel('Time in s'); title('Baseline epochs') - -%Fit Poisson -%hat because we are getting it from the fit - -lambda_hat_baseline = nan(n_neurons,1); %Just doing whole session as baseline -lambda_hb_ci = nan(n_neurons,2); %confidence interval for above -lambda_hat_behav = nan(n_neurons,n_behav); %One for each behavior for each neuron -lambda_hbeh_ci = nan(n_neurons,2,n_behav); %confidence interval for above -lambda_hat_behav_shuffle = nan(n_neurons, n_behav); %One for each behavior for each neuron -lambda_hbeh_ci_shuffle = nan(n_neurons,2, n_behav); %confidence interval for above -for n = 1:n_neurons - - [lambda_hat_baseline(n,1), lambda_hb_ci(n,:)] = poissfit(Spike_rasters(n,idx_rest)); %Estimating mean firing rate - - for b = 1:n_behav - idx = find(behavior_labels == unqLabels(b)); %get idx where behavior b occurred - n_per_behav(b)=length(idx); - [lambda_hat_behav(n,b), lambda_hbeh_ci(n,:,b)] = poissfit(Spike_rasters(n, idx)); - [lambda_hat_behav_shuffle(n,b), lambda_hbeh_ci_shuffle(n,:,b)] = poissfit(Spike_rasters(n, randsample(idx_rest,length(idx)))); - %[lambda_hat_behav_shuffle(n,b), lambda_hbeh_ci_shuffle(n,:,b)] = poissfit(Spike_rasters(n, randsample(1:length(behavior_labels),length(idx)))); - end - -end - - -%Review logic behind (i.e. my manuscript) this but go from 95% ci to se using below -lambda_hb_se = diff(lambda_hb_ci,1,2)/2/1.96; -lambda_hbeh_se = squeeze(diff(lambda_hbeh_ci,1,2)/2/1.96); %Get width of confidence interval, scale back to se -lambda_hbeh_se_shuffle = squeeze(diff(lambda_hbeh_ci_shuffle,1,2)/2/1.96); %Get width of confidence interval, scale back to se - - -sqW = nan(size(lambda_hat_behav)); -sqW_shuffle = nan(size(lambda_hat_behav)); - -for n = 1:n_neurons - for b = 1:n_behav - sqW(n,b) = (lambda_hat_behav(n,b) - lambda_hat_baseline(n,1))./sqrt(lambda_hbeh_se(n,b).^2 + lambda_hb_se(n).^2); - sqW_shuffle(n,b) = (lambda_hat_behav_shuffle(n,b) - lambda_hat_baseline(n,1))./sqrt(lambda_hbeh_se_shuffle(n,b).^2 + lambda_hb_se(n).^2); - end -end - -sqW_minobs = sqW(:,n_per_behav>min_occurrences); -sqW_shuffle_minobs = sqW(:,n_per_behav>min_occurrences); - - p_values = 1 - normcdf(abs(sqW),0,1); %Looks like computation of p-value only works for positive Wstats. - p_values_shuffle = 1 - normcdf(abs(sqW_shuffle),0,1); -cutoff = 0.0000001; %set as desired - -h = double(p_values < cutoff); sum(sum(h)) -h_shuffle = double(p_values_shuffle < cutoff); sum(sum(h_shuffle)) - -sqW_thresh = h.*sqW; sqW_thresh(sqW_thresh==0)=nan; -sqW_shuffle_thresh = h_shuffle.*sqW_shuffle; sqW_shuffle_thresh(sqW_shuffle_thresh==0)=nan; - -AxesLabels = behav_categ(1:end-1); -% figure; hp=heatmap(h); hp.XDisplayLabels = AxesLabels; -% figure; hp=heatmap(h_shuffle); hp.XDisplayLabels = AxesLabels; - -figure; hold on; set(gcf,'Position',[150 250 1500 800]); -subplot(2,2,1); hp=heatmap(sqW, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cold); hp.XDisplayLabels = AxesLabels; caxis([-100 50]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Wald-test heatmap') -subplot(2,2,2); hp=heatmap(sqW_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cold); hp.XDisplayLabels = AxesLabels; caxis([-100 50]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Wald-test heatmap thresholded') -subplot(2,2,3); hp=heatmap(sqW_shuffle, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cold); hp.XDisplayLabels = AxesLabels; caxis([-100 50]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Wald-test heatmap SHUFFLED') -subplot(2,2,4); hp=heatmap(sqW_shuffle_thresh, 'MissingDataColor', 'w', 'GridVisible', 'off', 'MissingDataLabel', " ",'Colormap',cold); hp.XDisplayLabels = AxesLabels; caxis([-100 50]); hp.YDisplayLabels = nan(size(hp.YDisplayData)); title('Wald-test heatmap SHUFFLED thesholded') - -if with_NC == 0 - sgtitle(['Wald statistics heatmap for all units except noise cluster']) - saveas(gcf, [savePath '/Selectivity_heatmap/Wald_stats_heatmap_NoNC_units.png']) -elseif isolatedOnly - sgtitle(['Wald statistics heatmap for isolated units']) - saveas(gcf, [savePath '/Selectivity_heatmap/Wald_stats_heatmap_isolated_units.png']) -else - sgtitle(['Wald statistics heatmap for all units']) - saveas(gcf, [savePath '/Selectivity_heatmap/Wald_stats_heatmap_all_units.png']) -end - -close all - diff --git a/Single neuron/old_code/log_Visualize_SingleUnit_Selectivity_old.m b/Single neuron/old_code/log_Visualize_SingleUnit_Selectivity_old.m deleted file mode 100644 index 399e136..0000000 --- a/Single neuron/old_code/log_Visualize_SingleUnit_Selectivity_old.m +++ /dev/null @@ -1,80 +0,0 @@ -%% Log_Visualize_SingleUnit_Selectivity -% This script computes firing rate of individual neuron under different -% behavioral conditions. Then, it computes a cohen's d (or effect size) -% difference between the distribution of firing rates during behavior X -% with a baseline firing rate. - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range=[1,2,11,12]; - -%Set parameters -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "all"; %Channels considered -with_NC =1; %0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well isolated units - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/']; - - %clearvars -except savePath filePath is_mac s home - - %% Load data - - %Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - - session_length = size(Spike_rasters,2); % get session length - - %Extract behavior labels - behavior_labels = cell2mat({labels{:,3}}');%Get behavior label from labels structure - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - - %Set parameters - unqLabels = 1:max(behavior_labels)-1; %Get unique behavior labels (exclude rest) - n_neurons = size(Spike_rasters,1); %Get number of neurons - n_behav = length(unqLabels); %Get number of unique behavior labels - - %Estimate "baseline" neural firing distribution. - idx_rest=find(behavior_labels==length(behav_categ));%Get idx of "rest" epochs. - mean_baseline = mean(Spike_rasters(:,idx_rest),2); - std_baseline = std(Spike_rasters(:,idx_rest),0,2); - - %Check visually that baseline is taken from epochs throughout the session - y=zeros(1, session_length); y(idx_rest)=1; - figure; plot(1:session_length, y); ylim([-0.5, 1.5]) - yticks([0 1]); yticklabels(["Behavior", "Rest/baseline"]) - xlabel('Time in s'); title('Baseline epochs') - set(gca,'FontSize',15); -% saveas(gcf, [savePath '/Baseline_epochs.png']); close all - - %Standerdize Unit rasters - Spike_raster_relative2baseline = Spike_rasters./mean_baseline; %Divide by baseline firing - Spike_raster_smooth = smoothdata(Spike_raster_relative2baseline,'gaussian',20); - - idx_groom = find(behavior_labels == 7); idx_getgroom = find(behavior_labels == 8); - idx_agg = find(behavior_labels == 9 | behavior_labels == 10 |behavior_labels == 1); - unit=209; - figure; hold on; set(gcf,'Position',[150 250 1500 500]); - xline([block_times.end_time_round(1), block_times.end_time_round(2)], "-",["Block 1 end", "Block 2 end"], "LineWidth",2); - plot(1:session_length, Spike_rasters(unit,1:session_length)); - plot(idx_groom, Spike_rasters(unit,idx_groom), "LineWidth",2) - plot(idx_getgroom, Spike_rasters(unit,idx_getgroom), "LineWidth",2) - plot(idx_agg, Spike_rasters(unit,idx_agg), "LineWidth",2) - plot(idx_rest, Spike_rasters(unit,idx_rest), "LineWidth",2) - - yline(1) - - -end diff --git a/Single neuron/old_code/log_extract_behav_neuron_stats.m b/Single neuron/old_code/log_extract_behav_neuron_stats.m deleted file mode 100644 index 7e8bb73..0000000 --- a/Single neuron/old_code/log_extract_behav_neuron_stats.m +++ /dev/null @@ -1,77 +0,0 @@ -%Extract mean and STD behaviors and neurons - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -agg_precedence =1; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/Example_units']; - - %% Load data - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence ); - end - - disp('Data Loaded') - - unit_num(s) = size(Spike_rasters,1); - - %Extract behavior labels - behavior_labels= cell2mat({labels{:,3}}');%Get behavior label from labels structure - bhv_lbls_tbl{s} = table(behavior_labels); - unq_behav(s) = length(unique(behavior_labels)); - -end - -unit_num(unit_num==0)=nan; -nanmean(unit_num) -nanstd(unit_num) -unq_behav(unq_behav==0)=nan; -nanmean(unq_behav) -nanstd(unq_behav) - diff --git a/Threat analyses/log_umap_state_distances_threat_singleUnit.m b/Threat analyses/log_umap_state_distances_threat_singleUnit.m deleted file mode 100644 index a5cd55f..0000000 --- a/Threat analyses/log_umap_state_distances_threat_singleUnit.m +++ /dev/null @@ -1,153 +0,0 @@ -%% log_umap_state_distances_threat -% This script applies UMAP to the data and computes the distance between -% threat data points to a baseline state (centre of mass for rest). - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -%session_range_no_partner=[1:3,11:13]; -%session_range_no_partner=[4:6,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1; %lump similar behavioral categories together -threat_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence -time_before_threat = 10*temp_resolution; -time_after_threat = 10*temp_resolution; -exclude_sq=0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; chan=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters';%zscore(Spike_rasters'); - - %% Select behaviors to visualize - - %Extract behavior labels and frequency - behavior_labels = cell2mat({labels{:,3}}'); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Extract block labels - block_labels = cell2mat({labels{:,13}}'); - - - %Only consider indices with behavior of interest - Spike_count_raster_final = Spike_count_raster;%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels;%Same as above but in behavior labels - block_labels_final = block_labels; - - - %% Get center of mass of rest epochs. - - idx_rest=find(behavior_labels==length(behav_categ)); block_rest=block_labels(behavior_labels==length(behav_categ)); - idx_rest_paired=idx_rest(block_rest==1); idx_rest_alone=idx_rest(block_rest==0); - - %get equal representation of rest during paired and alone blocks. - idx_equalBlocks=[randsample(idx_rest_paired,min(length(idx_rest_paired),length(idx_rest_alone))); randsample(idx_rest_alone,min(length(idx_rest_paired),length(idx_rest_alone)))]; - - rest_com = mean(Spike_count_raster(idx_rest,:)); - - - %% Find threat to subject onset times - - threat_to_subject_onset{s} = behavior_log.start_time_round(find(strcmp(behavior_log.Behavior, "HIS"))); - - for event = 1:length(threat_to_subject_onset{s}) - - threat_idx = threat_to_subject_onset{s}(event)+1 : threat_to_subject_onset{s}(event)+time_after_threat; - baseline_idx = threat_to_subject_onset{s}(event)-time_before_threat+1 : threat_to_subject_onset{s}(event); - - block_threat = block_labels_final(threat_idx); - - for unit=1:size(Spike_count_raster_final,2) - threat_response(unit, event)=mean(Spike_count_raster_final(threat_idx,unit)); - baseline_activity(unit) = rest_com(unit);%mean(Spike_count_raster_final(baseline_idx,unit)); - responseIdx{s}(unit, event) = abs((threat_response(unit, event)-baseline_activity(unit))/baseline_activity(unit)); - end - - block{s}(event)=unique(block_threat); - - end - - meanResponse{s}=mean(responseIdx{s}); - meanResponse_paired{s}=mean(responseIdx{s}(:,block{s}==1),2); - meanResponse_alone{s}=mean(responseIdx{s}(:,block{s}==0),2); - - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - disp(s) - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') -end %end of session for loop - -cd(['~/Dropbox (Penn)/Datalogger/Results/All_sessions/UMAP_results']); -% save('NeuralDistancesThreat.mat','distance_to_baseline_umap_paired','distance_to_baseline_umap_alone',... -% 'distance_to_baseline_pca_paired','distance_to_baseline_pca_alone','time_before_threat','time_after_threat') -% load('NeuralDistancesThreat.mat') - -limits=[0 5]; -paired=cell2mat(cat(1,meanResponse_paired(a_sessions))'); -alone=cell2mat(cat(1,meanResponse_alone(a_sessions))'); -figure; hold on -scatter(paired,alone,'filled','r') -xlabel('Absolute response index when paired') -ylabel('Absolute response index when alone') -xlim(limits); ylim(limits) - -paired=cell2mat(cat(1,meanResponse_paired(h_sessions))'); -alone=cell2mat(cat(1,meanResponse_alone(h_sessions))'); -scatter(paired,alone,'filled','b') -xlabel('Absolute response index when paired') -ylabel('Absolute response index when alone') -xlim(limits); ylim(limits) -plot(limits,limits) -legend({'Amos','Hooke','Diagonal'}) \ No newline at end of file diff --git a/Threat analyses/log_umap_state_distances_threat_to_agitation.m b/Threat analyses/log_umap_state_distances_threat_to_agitation.m deleted file mode 100644 index 69e748f..0000000 --- a/Threat analyses/log_umap_state_distances_threat_to_agitation.m +++ /dev/null @@ -1,235 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -%session_range_no_partner=[1:3,11:13]; -%session_range_no_partner=[4:6,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1; %lump similar behavioral categories together -threat_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence -time_before_threat = 30*temp_resolution; -time_after_threat = 60*temp_resolution; -exclude_sq=0; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=12; chan=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = zscore(Spike_rasters'); - - %% Select behaviors to visualize - - %Extract behavior labels and frequency - behavior_labels = cell2mat({labels{:,3}}'); - behavior_labels(behavior_labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - %Lump all travel together - behavior_labels(behavior_labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - behavior_labels(behavior_labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - %Extract block labels - block_labels = cell2mat({labels{:,12}}'); - - - %Only consider indices with behavior of interest - Spike_count_raster_final = Spike_count_raster;%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels;%Same as above but in behavior labels - block_labels_final = block_labels; - - - %% Get center of mass of rest epochs. - - idx_rest=find(behavior_labels==length(behav_categ)); block_rest=block_labels(behavior_labels==length(behav_categ)); - idx_rest_paired=idx_rest(block_rest==1); idx_rest_alone=idx_rest(block_rest==0); - - %get equal representation of rest during paired and alone blocks. - idx_equalBlocks=[randsample(idx_rest_paired,min(length(idx_rest_paired),length(idx_rest_alone))); randsample(idx_rest_alone,min(length(idx_rest_paired),length(idx_rest_alone)))]; - - rest_com = mean(Spike_count_raster(idx_rest,:)); - - %% Reduce dimensionality of data - - %using UMAP - [umap_result]=run_umap([rest_com; Spike_count_raster_final], 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 3); %Run umap to get 2d embedded states - close - - - %% Find threat to subject onset times - - threat_to_subject_onset = behavior_log.start_time_round(find(strcmp(behavior_log.Behavior, "HIS"))); - - e=1; - for event = 1:length(threat_to_subject_onset) - - idx = threat_to_subject_onset(event)-time_before_threat : threat_to_subject_onset(event)+time_after_threat; - - umap_result_final = umap_result([1,idx],:);%Only keep timepoints where the behaviors of interest occur in spiking data - - - %% Calculate distances - D_umap=pdist(umap_result_final); - Z_umap = squareform(D_umap); - - distance_to_baseline_umap{s}(e,:) = Z_umap(1,:); - - e=e+1; - - end - -% if s==12 -% distance_to_baseline_umap{s}(1,:) =[]; -% end - - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - disp(s) - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') -end %end of session for loop - -cd(['~/Dropbox (Penn)/Datalogger/Results/All_sessions/UMAP_results']); -save('Neural_agitation_corr.mat','distance_to_baseline_umap',... - 'time_before_threat','time_after_threat',... - 'a_sessions','h_sessions') -load('Neural_agitation_corr.mat') - - -%NOTE: for amos there is an agitation measure missing for one of the threat -%bouts. - -%For Amos -clear distance_to_baseline_umap_scaled -for s=a_sessions - distance_to_baseline_umap_scaled{s} = rescale(distance_to_baseline_umap{s}); -end - -%Check distance to baseline according to agitation -neural_response_umap=cell2mat(distance_to_baseline_umap_scaled'); -cd(['~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/']) -agitation = readtable('Threat_reaction_quant.xlsx'); -agitation =agitation(agitation.session_id<10,:); -[~, idx] = sort(agitation.Order_total); -agitation_ordered=agitation(idx,:); -agitation_ordered_toSubject = agitation_ordered(strcmp(agitation_ordered.target,"self") & agitation_ordered.squeeze==0,:); -agitation_ordered_toPartner = agitation_ordered(strcmp(agitation_ordered.target,"partner") & agitation_ordered.squeeze==0,:); - -neural_response_umap=neural_response_umap(~isnan(agitation_ordered_toSubject.agitation),:); -agitation_ordered_toSubject=agitation_ordered_toSubject(~isnan(agitation_ordered_toSubject.agitation),:); -for i=1:30 - [rho(i), p(i)]=corr(neural_response_umap(:,30+i),agitation_ordered_toSubject.agitation); -end -figure; -subplot(1,2,1); hold on; -plot(rho) -scatter(find(p<0.05),max(rho)+0.01,'*') -xlabel('Time from threat onset (sec)') -ylabel('Correlation agitation and neural response') -title('Amos') - -%For hooke -clear distance_to_baseline_umap_scaled -for s=h_sessions - distance_to_baseline_umap_scaled{s} = rescale(distance_to_baseline_umap{s}); -end - -%Check distance to baseline according to agitation -neural_response_umap=cell2mat(distance_to_baseline_umap_scaled'); -cd(['~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/']) -agitation = readtable('Threat_reaction_quant.xlsx'); -agitation =agitation(agitation.session_id>10,:); -[~, idx] = sort(agitation.Order_total); -agitation_ordered=agitation(idx,:); -agitation_ordered_toSubject = agitation_ordered(strcmp(agitation_ordered.target,"self") & agitation_ordered.squeeze==0,:); -agitation_ordered_toPartner = agitation_ordered(strcmp(agitation_ordered.target,"partner") & agitation_ordered.squeeze==0,:); - -neural_response_umap=neural_response_umap(~isnan(agitation_ordered_toSubject.agitation),:); -agitation_ordered_toSubject=agitation_ordered_toSubject(~isnan(agitation_ordered_toSubject.agitation),:); -for i=1:30 - [rho(i), p(i)]=corr(neural_response_umap(:,30+i),agitation_ordered_toSubject.agitation); -end -subplot(1,2,2); hold on; -plot(rho) -scatter(find(p<0.05),max(rho)+0.01,'*') -xlabel('Time from threat onset (sec)') -ylabel('Correlation agitation & neural response') -title('Hooke') - -% [~, idx_max] = max(rho); -% figure; hold on -% scatter(agitation_ordered_toSubject.agitation, neural_response_umap(:,30+idx_max),40,'filled') -% ylabel('Scaled neural distance'); xlabel('Agitation') - -% % % % figure; hold on; -% % % % plot(mean(neural_response_umap), 'LineWidth',5) -% % % % %plot(neural_response_umap', 'LineWidth',0.25) -% % % % xline(31, 'LineWidth',3, 'LineStyle','--'); %xline(75, 'LineWidth',3,'LineStyle','--') -% % % % -% % % % alone_idx = agitation_ordered_toSubject.block==0; -% % % % paired_idx = agitation_ordered_toSubject.block==1; -% % % % -% % % % figure; hold on -% % % % upper_lim=mean(neural_response_umap(paired_idx,:))+std(neural_response_umap(paired_idx,:)); -% % % % lower_lim=mean(neural_response_umap(paired_idx,:))-std(neural_response_umap(paired_idx,:)); -% % % % p = fill([1:length(neural_response_umap) length(neural_response_umap):-1:1],[upper_lim flip(lower_lim)],'red'); -% % % % p.FaceColor = [0 0 1]; -% % % % p.FaceAlpha = 0.3; -% % % % p.EdgeColor = 'none'; -% % % % plot(mean(neural_response_umap(paired_idx,:)),'b','LineWidth',6) -% % % % %plot(neural_response_umap(paired_idx,:)', 'b') -% % % % -% % % % upper_lim=mean(neural_response_umap(alone_idx,:))+std(neural_response_umap(alone_idx,:)); -% % % % lower_lim=mean(neural_response_umap(alone_idx,:))-std(neural_response_umap(alone_idx,:)); -% % % % p = fill([1:length(neural_response_umap) length(neural_response_umap):-1:1],[upper_lim flip(lower_lim)],'red'); -% % % % p.FaceColor = [1 0 0]; -% % % % p.FaceAlpha = 0.3; -% % % % p.EdgeColor = 'none'; -% % % % plot(mean(neural_response_umap(alone_idx,:)),'r', 'LineWidth',5) -% % % % %plot(neural_response_umap(alone_idx,:)', 'r') -xline(31, 'LineWidth',3, 'LineStyle','--'); -ylim([0 1]) -title('Hooke') \ No newline at end of file diff --git a/Threat analyses/old_code/log_self_vs_other_whenPaired_old.m b/Threat analyses/old_code/log_self_vs_other_whenPaired_old.m deleted file mode 100644 index 0ae7a09..0000000 --- a/Threat analyses/old_code/log_self_vs_other_whenPaired_old.m +++ /dev/null @@ -1,132 +0,0 @@ -%% log_self_Vs_other_whenPaired -% This script extracts and compares the mean firing rate across different threat -% instances (self vs. other; alone vs. paired). - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range=[1:6,11:13,15:16,18]; -a_sessions = 1:6; h_sessions = [11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY multi-unit cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1; %lump similar behavioral categories together -time_after_threat = 30*temp_resolution; -threat_precedence=1; % 0: aggression takes precedence; 1: Threat to partner and subject states take precedence -exclude_sq = 1; - -s=1; chan=1; e=1; e2=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - - %% Get data with specified temporal resolution and channels - - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq); - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - %Extract behavior labels - behavior_labels = cell2mat({labels{:,3}}'); - %Extract block labels - block_labels = cell2mat({labels{:,13}}'); - - - %% Find threat to subject onset times - - threat_to_subject_onset = behavior_log.start_time_round(find(strcmp(behavior_log.Behavior, "HIS"))); - threat_to_partner_onset = behavior_log.start_time_round(find(strcmp(behavior_log.Behavior, "HIP"))); - grooming_onset = behavior_log.start_time_round(find(strcmp(behavior_log.Behavior, "Groom Give")|strcmp(behavior_log.Behavior, "Groom Receive"))); - - %For threat to subject - for event = 1:length(threat_to_subject_onset) - - idx = threat_to_subject_onset(event) : threat_to_subject_onset(event)+time_after_threat; - idx_groom = grooming_onset(event) : grooming_onset(event)+time_after_threat; - - Spike_count_raster_subject{e,1} = Spike_count_raster(idx,:)';%Only keep timepoints where the behaviors of interest occur in spiking data - Spike_count_raster_groom{e,1} = Spike_count_raster(idx_groom,:)'; -% Spike_count_raster_groom{e,1} = Spike_count_raster(idx_groom,randsample(unit_count(3),250))'; - block_selfthreat(e) = unique(block_labels(idx)); - - e=e+1; - - end - %mean_response_subject = mean(cat(3,Spike_count_raster_subject{block==1}),3)'; - - %For threat to partner - for event = 1:length(threat_to_partner_onset) - - idx = threat_to_partner_onset(event) : threat_to_partner_onset(event)+time_after_threat; - - Spike_count_raster_partner{e2,1} = Spike_count_raster(idx,:)';%Only keep timepoints where the behaviors of interest occur in spiking data - block_otherthreat(e2) = unique(block_labels(idx)); - - e2=e2+1; - end - %mean_response_subject = mean(cat(3,Spike_count_raster_subject{block==1}),3)'; - - - -% figure; hold on; set(gcf,'Position',[150 250 700 300]); -% plot(mean_response{s}(block==1,:)','Color',[0.9 0.7 0.12],'LineWidth',2) -% plot(mean_response{s}(block==0,:)','Color',[0.5 0 0],'LineWidth',2) -% xline(time_before_threat+2,'LineStyle','--') -% ylabel('Mean firing rate (Hz)') -% xlabel('Time') -% legend('Threat when paired','','Threat when alone','','Threat onset') -% set(gca,'FontSize',16); -% title(['session: ' sessions(s).name]) -% -% cd(savePath) -% saveas(gcf,'Distance threat paired vs. alone.pdf') -% % pause(1); close all - - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - disp(s) - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - -end %end of session for loop - -%CAMILLE NOTE: FIGURE out how to get correlation coefficient. - -% Get mean response to threat to self, threat to partner, grooming -partner_threat_activity = mean(cat(1, Spike_count_raster_partner{block_otherthreat==1})); -subject_threat_activity = mean(cat(1, Spike_count_raster_subject{block_selfthreat==1})); -groom_activity = mean(cat(1, Spike_count_raster_groom{block_selfthreat==1})); - -%Plot matrices -figure; hold on -plot(partner_threat_activity); -plot(subject_threat_activity); -plot(groom_activity) - -%Get correlation between mean activity -corrcoef(subject_threat_activity,partner_threat_activity) -corrcoef(subject_threat_activity,groom_activity) - - - diff --git a/Threat analyses/old_code/log_umap_state_distances_threat_old.m b/Threat analyses/old_code/log_umap_state_distances_threat_old.m deleted file mode 100644 index 0fb07a1..0000000 --- a/Threat analyses/old_code/log_umap_state_distances_threat_old.m +++ /dev/null @@ -1,297 +0,0 @@ -%% log_umap_state_distances_threat -% This script applies UMAP to the data and computes the distance between -% threat data points to a baseline state (centre of mass for rest). - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -%session_range_no_partner=[1:3,11:13]; -%session_range_no_partner=[4:6,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; % 1: smooth the data; 0: do not smooth -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -null=0;%Set whether we want the null -simplify=1; %lump similar behavioral categories together -agg_precedence=0; % 1: aggression takes precedence; 0: Threat to partner and subject states take precedence -time_before_threat = 30*temp_resolution; -time_after_threat = 60*temp_resolution; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16,18]; -end - -s=1; chan=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/UMAP_results/']; - - - %% Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence); - end - - disp('Data Loaded') - - %Raw data - Spike_count_raster = Spike_rasters'; - %Extract behavior labels - behavior_labels = cell2mat({labels{:,3}}'); - %Extract block labels - block_labels = cell2mat({labels{:,13}}'); - - %Load threat quantification - threat_quant = readtable([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/Threat_reaction_quant.xlsx']); - threat_quant_session = threat_quant(threat_quant.session_id==s,:); - - %% Get center of mass of rest epochs. - - idx_rest=find(behavior_labels==length(behav_categ)); block_rest=block_labels(behavior_labels==length(behav_categ)); - idx_rest_paired=idx_rest(block_rest==1); idx_rest_alone=idx_rest(block_rest==0); - - %get equal representation of rest during paired and alone blocks. - idx_equalBlocks=[randsample(idx_rest_paired,min(length(idx_rest_paired),length(idx_rest_alone))); randsample(idx_rest_alone,min(length(idx_rest_paired),length(idx_rest_alone)))]; - - rest_com = mean(Spike_count_raster(idx_rest,:)); - - %% Find threat to subject onset times - - threat_to_subject_onset = behavior_log.start_time_round(find(strcmp(behavior_log.Behavior, "HIS"))); - threat_quant_subject = threat_quant_session(strcmp(threat_quant_session.target,"self"),:); - [~, idx_sorted] = sort(threat_quant_subject.order); - threat_quant_subject =threat_quant_subject(idx_sorted,:); - - %Exclude squeeze events - threat_to_subject_onset = threat_to_subject_onset(threat_quant_subject.squeeze==0,:); - threat_quant_subject = threat_quant_subject(threat_quant_subject.squeeze==0,:); - - e=1; e2=1; - for event = 1:length(threat_to_subject_onset) - - idx = threat_to_subject_onset(event)-time_before_threat : threat_to_subject_onset(event)+time_after_threat; - - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_labels_final = block_labels(idx); - behavior_labels_final_rand = randsample(behavior_labels_final, length(behavior_labels_final)); - preThreat_com = mean(Spike_count_raster_final(1:time_before_threat,:)); - - %% Run umap -% [umap_result]=run_umap([rest_com; Spike_count_raster_final], 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 50); %Run umap to get 2d embedded states - [umap_result]=run_umap([preThreat_com; Spike_count_raster_final(time_before_threat+1:end,:)], 'n_neighbors', 15, 'min_dist', 0.1, 'n_components', 50); %Run umap to get 2d embedded states - close - - %% Run PCA - %[~, pca_result] = pca(zscore([rest_com; Spike_count_raster_final])); - [~, pca_result] = pca(zscore([preThreat_com; Spike_count_raster_final(time_before_threat+1:end,:)])); - - - umap_result_final = umap_result;%Only keep timepoints where the behaviors of interest occur in spiking data - pca_result_final = pca_result(:,1:50); - - block{s}(event)=unique(block_labels_final); - agitation{s}(event)=threat_quant_subject.agitation(event); - - %% Calculate distances - D_umap=pdist(umap_result_final); - Z_umap = squareform(D_umap); - - D_pca=pdist(pca_result_final); - Z_pca = squareform(D_pca); - - if block{s}(event)==0 - distance_to_baseline_pca_alone{s}(e,:) = Z_pca(1,:); - distance_to_baseline_umap_alone{s}(e,:) = Z_umap(1,:); - e=e+1; - else - distance_to_baseline_pca_paired{s}(e2,:) = Z_pca(1,:); - distance_to_baseline_umap_paired{s}(e2,:) = Z_umap(1,:); - e2=e2+1; - end - distance_to_baseline_umap{s}(event,:) = Z_umap(1,:); - distance_to_baseline_pca{s}(event,:) = Z_pca(1,:); - - end - -% distance_paired_alone{s}=mean(distance_to_baseline_umap_paired{s}) - mean(distance_to_baseline_umap_alone{s}); - -% figure; hold on; set(gcf,'Position',[150 250 1200 300]); -% -% subplot(1,2,1); hold on -% plot(distance_to_baseline_umap_paired{s}','Color',[0.9 0.7 0.12],'LineWidth',2) -% plot(distance_to_baseline_umap_alone{s}','Color',[0.5 0 0],'LineWidth',2) -% xline(time_before_threat+2,'LineStyle','--') -% ylabel('Distance to baseline state') -% xlabel('Time') -% legend('Threat when paired','Threat when alone','Threat onset') -% set(gca,'FontSize',16); title('UMAP') -% -% subplot(1,2,2); hold on -% plot(distance_to_baseline_pca_paired{s}','Color',[0.9 0.7 0.12],'LineWidth',2) -% plot(distance_to_baseline_pca_alone{s}','Color',[0.5 0 0],'LineWidth',2) -% xline(time_before_threat+2,'LineStyle','--') -% ylabel('Distance to baseline state') -% xlabel('Time') -% legend('Threat when paired','Threat when alone','Threat onset') -% set(gca,'FontSize',16); title('PCA') -% -% sgtitle(['session:' sessions(s).name]) -% -% cd(savePath) -% saveas(gcf,'Distance threat paired vs. alone.pdf') -% % pause(1); close all - - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - disp(s) - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') -end %end of session for loop - -cd(['~/Dropbox (Penn)/Datalogger/Results/All_sessions/UMAP_results']); -save('NeuralDistancesThreat.mat','distance_to_baseline_umap_paired','distance_to_baseline_umap_alone',... - 'distance_to_baseline_pca_paired','distance_to_baseline_pca_alone','time_before_threat','time_after_threat') -load('NeuralDistancesThreat.mat') - - -response = cat(1,distance_to_baseline_umap{:}); -block_all = cat(2,block{:})'; -agitation_all=cat(2,agitation{:})'; -figure; -scatter(agitation_all, response(:,41)) -for r=1:length(response) - [correlation(r) pval(r)]=corr(agitation_all, response(:,r)); -end -figure;plot(correlation) -figure; hold on; plot(pval); yline(0.05,'--') -[rho p]=corr(agitation_all, mean(response(:,31:60),2)) - -paired=cell2mat(distance_to_baseline_umap_paired'); -alone=cell2mat(distance_to_baseline_umap_alone'); - -gap=mean(nanmean(paired(:,2:30)))-mean(nanmean(alone(:,2:30))); -alone=alone+gap; - -figure; hold on - -upper_lim=nanmean(paired)+(nanstd(paired)/sqrt(size(paired,1))); -lower_lim=nanmean(paired)-(nanstd(paired)/sqrt(size(paired,1))); -p = fill([1:length(nanmean(paired)) length(nanmean(paired)):-1:1],[upper_lim flip(lower_lim)],'red'); -p.FaceColor = [0.9 0.7 0.12]; -p.FaceAlpha = 0.3; -p.EdgeColor = 'none'; -plot(nanmean(paired),'Color',[0.9 0.7 0.12],'LineWidth',6) -plot(paired','Color',[0.9 0.7 0.12],'LineWidth',1) - - -upper_lim=nanmean(alone)+(nanstd(alone)/sqrt(size(alone,1))); -lower_lim=nanmean(alone)-(nanstd(alone)/sqrt(size(alone,1))); -p = fill([1:length(nanmean(alone)) length(nanmean(alone)):-1:1],[upper_lim flip(lower_lim)],'red'); -p.FaceColor = [0.5 0 0]; -p.FaceAlpha = 0.3; -p.EdgeColor = 'none'; -plot(nanmean(alone),'Color',[0.5 0 0],'LineWidth',6) -plot(alone','Color',[0.5 0 0],'LineWidth',1) - -xlim([2 length(alone)]) -xline(time_before_threat+2,'LineStyle','--') -xline(time_before_threat+30+2,'LineStyle','--') -ylabel('Distance to baseline state in UMAP space') -xlabel('Time (in s)') -%legend('Threat when paired','Threat when alone','Threat onset') -set(gca,'FontSize',16); - -for i=2:length(alone) - [h(i),pval(i)]=ttest2(alone(:,i), paired(:,i)); -end - -scatter(find(h==1), 6.75*ones(size(find(h==1))),30,'k','*') - -%%%%%%%%%%%%%%%%%%%%%%%% -%Plot in chronological order (see if there is habituation?) -for s=1:6 -dates_a{s}=sessions(a_sessions(s)).name(end-4:end); -dates_h{s}=sessions(h_sessions(s)).name(end-4:end); -end - -chonological_order = [1,11,2,12,3,13,4,5,15,16,6,18]; -figure -plot(alone(chonological_order,40)-paired(chonological_order,40)) -figure; hold on; timepoint=41; -subplot(1,2,1); hold on; plot(alone(a_sessions,timepoint)-paired(a_sessions,timepoint)); yline(0,'--'); xticks(1:6); xticklabels(dates_a) -subplot(1,2,2); hold on; plot(alone(h_sessions,timepoint)-paired(h_sessions,timepoint)); yline(0,'--'); xticks(1:6); xticklabels(dates_h) - -figure; hold on; timepoint=35:55; -subplot(1,2,1); hold on; -plot(mean(alone(a_sessions,timepoint),2)-mean(paired(a_sessions,timepoint),2),'LineWidth',2); yline(0,'--'); -scatter(1:6,mean(alone(a_sessions,timepoint),2)-mean(paired(a_sessions,timepoint),2),'b','filled') -% text(0.3,0.3,'alone > paired') -% text(0.3,-0.3,'alone < paired') -xticks(1:6); xticklabels(dates_a); xlabel('Date'); ylabel('Mean difference between alone and paired during threat'); ylim([-3 7]) -title('Amos') - -subplot(1,2,2); hold on; -plot(mean(alone(h_sessions,timepoint),2)-mean(paired(h_sessions,timepoint),2),'LineWidth',2,'Color','r'); yline(0,'--'); -scatter(1:6,mean(alone(h_sessions,timepoint),2)-mean(paired(h_sessions,timepoint),2),'r','filled') -% text(0.3,0.3,'alone > paired') -% text(0.3,-0.3,'alone < paired') -xticks(1:6); xticklabels(dates_h); -ylim([-3 7]) -title('Hooke') - - -% % % %PCA -% % % figure; hold on -% % % paired=nan(max(session_range),length(idx)+1); -% % % alone=nan(max(session_range),length(idx)+1); -% % % -% % % for s=session_range -% % % paired(s,:) = mean(distance_to_baseline_pca{s}(:,block{s}~=0),2); -% % % alone(s,:) = mean(distance_to_baseline_pca{s}(:,block{s}==0),2); -% % % -% % % plot(paired(s,:),'Color',[0.9 0.7 0.12],'LineWidth',2) -% % % plot(alone(s,:),'Color',[0.5 0 0],'LineWidth',2) -% % % -% % % pause(1) -% % % -% % % end -% % % -% % % plot(nanmean(paired),'Color',[0.9 0.7 0.12],'LineWidth',6) -% % % plot(nanmean(alone),'Color',[0.5 0 0],'LineWidth',6) -% % % -% % % xline(time_before_threat+1,'LineStyle','--') -% % % ylabel('Distance to baseline state') -% % % xlabel('Time') -% % % %legend('Threat when paired','Threat when alone','Threat onset') -% % % set(gca,'FontSize',16); -% % % -% % % diff --git a/Toolboxes/.DS_Store b/Toolboxes/.DS_Store index c9dce9e..dde7f54 100644 Binary files a/Toolboxes/.DS_Store and b/Toolboxes/.DS_Store differ diff --git a/Toolboxes/vennX.m b/Toolboxes/venn/vennX.m similarity index 100% rename from Toolboxes/vennX.m rename to Toolboxes/venn/vennX.m diff --git a/Varia/.DS_Store b/Varia/.DS_Store deleted file mode 100644 index 5e56322..0000000 Binary files a/Varia/.DS_Store and /dev/null differ diff --git a/Varia/GLM/log_glm_behav_vs_context_full.m b/Varia/GLM/log_glm_behav_vs_context_full.m deleted file mode 100644 index 0b3df42..0000000 --- a/Varia/GLM/log_glm_behav_vs_context_full.m +++ /dev/null @@ -1,257 +0,0 @@ -%Extract mean and STD behaviors and neurons - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -threat_precedence =1; -plot_toggle=0; -warning('off','all') - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/Example_units']; - - %% Load data - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence ); - end - - disp('Data Loaded') - - Spike_count_raster = zscore(Spike_rasters'); - - %Extract behavior labels - behavior_labels= cell2mat({labels{:,3}}');%Get behavior label from labels structure - context = cell2mat({labels{:,12}}'); context_categ={"female","male","alone"}; - paired_or_not = cell2mat({labels{:,13}}'); - - boi =[5,7,8,9,10,16,24]; - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - num_occurrence = behav_freq_table(boi,2); - min_occurrence = min(num_occurrence); - - tic - - %% Real data - - idx_final=[]; idx_total=[]; - for b=1:length(boi) - - idx= find(ismember(behavior_labels,boi(b))); %find the indices of the behaviors considered - num_samples(b)=length(idx); - idx_subsample = randsample(idx, min_occurrence); - idx_final = [idx_final; idx_subsample]; - idx_total = [idx_total; idx]; - - end - - idx_final=idx_total; - - behavior_final = dummyvar(categorical(behavior_labels(idx_final))); - context_final = dummyvar(categorical(context(idx_final)));%Same as above but in behavior labels - - %Behaviors - Foraging = behavior_final(:,1); - GroomGive = behavior_final(:,2); - GroomGet = behavior_final(:,3); - ThreatPartner = behavior_final(:,4); - ThreatSubject = behavior_final(:,5); - Travel = behavior_final(:,6); - Rest = behavior_final(:,7); - - %Context - NeighborF = context_final(:,1); - Alone = context_final(:,3); - - %Interaction - Foraging_alone = Foraging.*Alone; - ThreatPartner_alone = ThreatPartner.*Alone; - ThreatSubject_alone = ThreatSubject.*Alone; - Travel_alone = Travel.*Alone; - - Foraging_neighborF = Foraging.*NeighborF; - GroomGive_neighborF = GroomGive.*NeighborF; - GroomGet_neighborF = GroomGet.*NeighborF; - ThreatPartner_neighborF = ThreatPartner.*NeighborF; - ThreatSubject_neighborF = ThreatSubject.*NeighborF; - Travel_neighborF = Travel.*NeighborF; - - predictors_mat = [Foraging, GroomGive, GroomGet, ThreatPartner,... - ThreatSubject, Travel, NeighborF, Alone, ... - Foraging_alone,ThreatPartner_alone,ThreatSubject_alone,Travel_alone, ... - Foraging_neighborF,GroomGive_neighborF,GroomGet_neighborF, ... - ThreatPartner_neighborF, ThreatSubject_neighborF, Travel_neighborF]; -% predictors_mat = [Foraging, GroomGive, GroomGet, ThreatPartner,... -% ThreatSubject, Travel, NeighborF, Alone]; - - - %Run glm for each unit separately - for unit = 1:size(Spike_count_raster,2) - - NeuralResponse = Spike_count_raster(idx_final,unit);%Only keep timepoints where the behaviors of interest occur in spiking data - - mdl =fitlm(predictors_mat,NeuralResponse); - coeffs{s,unit}=table2array(mdl.Coefficients); - - end %end of units - - %Pool coefficient estimate and uncertainty across neurons - coeffs_pooled=cat(3,coeffs{s,:}); - coeffs_pooled_mean = mean(coeffs_pooled,3); estimate_pooled = coeffs_pooled_mean(:,1); se_mean = coeffs_pooled_mean(:,2); - for pred = 1:size(estimate_pooled,1) - within_variance(pred) = mean(coeffs_pooled(pred,2,:).^2); - between_variance(pred) = sum((coeffs_pooled(pred,1,:)-estimate_pooled(pred)).^2)/(size(coeffs_pooled,3)-1); - V_total(pred) = within_variance(pred)+ between_variance(pred)+ between_variance(pred)/size(coeffs_pooled,3); - end - se_pooled = sqrt(V_total)'; - CI = [estimate_pooled-se_pooled*1.96 estimate_pooled+se_pooled*1.96]; - data = [estimate_pooled, CI]; - -% labels={'Intercept','Foraging', 'GroomGive', 'GroomGet', 'ThreatPartner',... -% 'ThreatSubject', 'Travel', 'NeighborF', 'Alone'}; -% -% figure; hold on -% [~, idx_sorted]=sort(estimate_pooled); -% scatter(1:9,estimate_pooled(idx_sorted), 40,'filled') -% errorbar(estimate_pooled(idx_sorted),se_pooled(idx_sorted)*1.96,'LineStyle','none'); -% yline(0, 'LineStyle','--') -% ylabel('Standardized Beta') -% xticks([1:9]);ylim([-4 4]); xlim([0.5 9.5]) -% xticklabels(labels(idx_sorted)) - -% % % % labels={'Intercept','Foraging', 'GroomGive', 'GroomGet', 'ThreatPartner',... -% % % % 'ThreatSubject', 'Travel', 'NeighborF', 'Alone', ... -% % % % 'Foraging_alone','ThreatPartner_alone','ThreatSubject_alone','Travel_alone', ... -% % % % 'Foraging_neighborF','GroomGive_neighborF','GroomGet_neighborF', ... -% % % % 'ThreatPartner_neighborF', 'ThreatSubject_neighborF', 'Travel_neighborF'}; -% % % % -% % % % figure; hold on -% % % % -% % % % subplot(1,3,1); hold on -% % % % idx=1:7; -% % % % [~, idx_sorted]=sort(estimate_pooled(idx)); -% % % % scatter(1:7,estimate_pooled(idx(idx_sorted)), 40,'filled') -% % % % errorbar(estimate_pooled(idx(idx_sorted)),se_pooled(idx(idx_sorted)),'LineStyle','none'); -% % % % yline(0, 'LineStyle','--') -% % % % ylabel('Standardized Beta') -% % % % xticks([1:7]);ylim([-2.5 2.5]); xlim([0.5 7.5]) -% % % % xticklabels(labels(idx(idx_sorted))) -% % % % -% % % % subplot(1,3,2); hold on -% % % % idx=10:13; -% % % % [~, idx_sorted]=sort(estimate_pooled(idx)); -% % % % scatter(1:5,estimate_pooled([9,idx(idx_sorted)]), 40,'filled') -% % % % errorbar(estimate_pooled([9,idx(idx_sorted)]),se_pooled([9,idx(idx_sorted)]),'LineStyle','none'); -% % % % yline(0, 'LineStyle','--') -% % % % ylabel('Standardized Beta') -% % % % xticks([1:5]);ylim([-2.5 2.5]); xlim([0.5 5.5]) -% % % % xticklabels(labels([9,idx(idx_sorted)])) -% % % % -% % % % subplot(1,3,3); hold on -% % % % idx=[14:19]; -% % % % [~, idx_sorted]=sort(estimate_pooled(idx)); -% % % % scatter(1:7,estimate_pooled([8,idx(idx_sorted)]), 40,'filled') -% % % % errorbar(estimate_pooled([8,idx(idx_sorted)]),se_pooled([8,idx(idx_sorted)]),'LineStyle','none'); -% % % % yline(0, 'LineStyle','--') -% % % % ylabel('Standardized Beta') -% % % % xticks([1:7]);ylim([-2.5 2.5]); xlim([0.5 7.5]) -% % % % xticklabels(labels([8,idx(idx_sorted)])) -% % % % -% % % % %clear coeffs - -end %end of session - -%Pool coefficient estimate and uncertainty across ALL neurons - coeffs_pooled=cat(3,coeffs{:,:}); - coeffs_pooled_mean = mean(coeffs_pooled,3); estimate_pooled = coeffs_pooled_mean(:,1); se_mean = coeffs_pooled_mean(:,2); - for pred = 1:size(estimate_pooled,1) - within_variance(pred) = mean(coeffs_pooled(pred,2,:).^2); - between_variance(pred) = sum((coeffs_pooled(pred,1,:)-estimate_pooled(pred)).^2)/(size(coeffs_pooled,3)-1); - V_total(pred) = within_variance(pred)+ between_variance(pred)+ between_variance(pred)/size(coeffs_pooled,3); - end - se_pooled = sqrt(V_total)'; - CI = [estimate_pooled-se_pooled*1.96 estimate_pooled+se_pooled*1.96]; - data = [estimate_pooled, CI]; - - labels={'Intercept','Foraging', 'GroomGive', 'GroomGet', 'ThreatPartner',... - 'ThreatSubject', 'Travel', 'NeighborF', 'Alone', ... - 'Foraging_alone','ThreatPartner_alone','ThreatSubject_alone','Travel_alone', ... - 'Foraging_neighborF','GroomGive_neighborF','GroomGet_neighborF', ... - 'ThreatPartner_neighborF', 'ThreatSubject_neighborF', 'Travel_neighborF'}; - - figure; hold on - uplim = 1.1; lowlim = -1.1; - - subplot(1,3,1); hold on - idx=1:7; - [~, idx_sorted]=sort(estimate_pooled(idx)); - scatter(1:7,estimate_pooled(idx(idx_sorted)), 80,'filled') - errorbar(estimate_pooled(idx(idx_sorted)),se_pooled(idx(idx_sorted)),'LineStyle','none'); - yline(0, 'LineStyle','--') - ylabel('Standardized Beta') - xticks([1:7]);ylim([lowlim uplim]); xlim([0.5 7.5]) - xticklabels(labels(idx(idx_sorted))) - - subplot(1,3,2); hold on - idx=10:13; - [~, idx_sorted]=sort(estimate_pooled(idx)); - scatter(1:5,estimate_pooled([9,idx(idx_sorted)]), 80,'filled') - errorbar(estimate_pooled([9,idx(idx_sorted)]),se_pooled([9,idx(idx_sorted)]),'LineStyle','none'); - yline(0, 'LineStyle','--') - ylabel('Standardized Beta') - xticks([1:5]);ylim([lowlim uplim]); xlim([0.5 5.5]) - xticklabels(labels([9,idx(idx_sorted)])) - - subplot(1,3,3); hold on - idx=[14:19]; - [~, idx_sorted]=sort(estimate_pooled(idx)); - scatter(1:7,estimate_pooled([8,idx(idx_sorted)]), 80,'filled') - errorbar(estimate_pooled([8,idx(idx_sorted)]),se_pooled([8,idx(idx_sorted)]),'LineStyle','none'); - yline(0, 'LineStyle','--') - ylabel('Standardized Beta') - xticks([1:7]);ylim([lowlim uplim]); xlim([0.5 7.5]) - xticklabels(labels([8,idx(idx_sorted)])) diff --git a/Varia/GLM/old_code/GLM_CrossVal.m b/Varia/GLM/old_code/GLM_CrossVal.m deleted file mode 100644 index 6e46663..0000000 --- a/Varia/GLM/old_code/GLM_CrossVal.m +++ /dev/null @@ -1,191 +0,0 @@ -function [Y_hat,Betas,CIs,Rsquared_per,LL_per,pvalues_per,problem_neurons] = GLM_CrossVal(X,Y,kfolds,is_smooth) -%GLM_CROSSVAL Custom code to run the cross validation on our glm data -% Detailed explanation to come. For now writing this since the built in -% function for matlab doesn't seem particularly useful and the other -% function we have from T and T 2022 makes certain assumptions about the -% format of the data we don't want to deal with. Designed to work with -% any of our glm set ups - -%INPUTS: -%X - design matrix for these models -%Y - ALL spiking data (will design function to loop over all neurons) -%kfolds - number of folds to use, scalar - - - -%OUTPUTS: - - -%% Preallocate variables Set up cross validation - -%Note: GLM fit also generates a coeff for the intercept term so have to add -%1 to all the size(X,2) things. -Betas = NaN(size(X,2)+1,size(Y,2)); %Average over CV Beta for each regressors (row) for each neuron (column) -CIs = NaN(size(X,2)+1,size(Y,2),2); %Average CI for each regressor, for each neuron, first page lower bound, second upper bound; -Rsquared_per = NaN(1,size(Y,2));%Average R^2 for EACH NEURON MODEL averaged over CV -%For now doing adjusted R^2, but can change this in the CV loop -LL_per = NaN(1,size(Y,2)); -pvalues_per = NaN(size(X,2)+1,size(Y,2),kfolds); -Y_hat = NaN(size(Y)); %Prediction for all time points for each neuron, concatenated over CV - -%rng(1) %For reproducability across runs - - - - -%Divide these into approximately kfolds groups (up to a rounding) -foldCnt = floor(size(Y,1)/kfolds); - -%% Check each fold of the design matrix is full rank -%If not, keep redoing the permutaion until they are. - -%Update 2022-08-30: It is clear that this won't work as it seems need over -%95% of the data to be in the fold to guarantee that the train design -%matrix is full rank. For now, solve this by seeing what regressors they -%are. Just read an article explaining Pillow's point about regularization -%though. It can prevent rank issues by introducing an additional term that -%will change the approximately zero singular values in the matrix to a -%non-zero factor and allow for the Moore-Penrose Inverse. See this link -%for quick details: https://calculatedcontent.com/2018/09/21/rank-collapse-in-deep-learning/ -%Further update: Looks like even with NO delays variable 26 almost always -%caues an issue in at least one of the folds and sometimes variable 25 -%does. These are partner variables so maybe we can just do without for -%now? These are groom partner which make sense to be redunant with getting -%groomed in subject and foraging which is a surprise but eh. - -%Removing partner grooming subject did not fix overall problem (with the -%delays) but does seem like it may have fixed the problem when there are no -%delays. - -if kfolds >1 - - good = false; - counter = 0; - problem_reg = []; - while ~good && counter <= 100 - counter = counter+1; - display(['Making sure subsampled design matrix for all folds is full rank. Try #' num2str(counter)]) - randIdx = randperm(size(Y,1)); %Get randperm of indecies for whole session - %Need to due permentation instead of just a partition since we have the - %block design (i.e. don't want fits that are only block 1 or block 2) - X_check = cell(1,kfolds); - rank_check = false(1,kfolds); - - for iFolds = 1:kfolds - dataIdx = true(1,size(Y,1)); %Start with using all of the session (i.e. all true indecies) - - - dataIdx(randIdx(((iFolds - 1)*foldCnt) + (1:foldCnt))) = false; - - X_check{1,iFolds} = X(dataIdx,:); %Put the design matrix with these random inds into X_check - - %used qr check here to figure out which regressor is the issue - %and see if it is consistent some subset of regressors - rejReg = log_run_QR(X_check{iFolds},0); - - rank_check(iFolds) = ~any(rejReg); %Make sure that the subset matrix is full rank - - if ~rank_check(iFolds) - - problem_reg = [problem_reg find(rejReg)]; %Just keep appending to this vector than do a histogram after a set amount of tries. - - end - - end - %find(rank_check == 0)%update it is not consistently one particular - %fold, so nothing to fix there. Often getting 90% of these good - %though which is very annoying - sum(rank_check)/kfolds - good = all(rank_check); %If all are full rank, set good to true. Otherwise redo loop - - end - if counter >99 - the_info = histogram(problem_reg,'BinMethod','integers') - end -end -%% Loop over each neuron - -%Update 2022-08-29 might want to save more stuff than this, but I think -%this is okay for a first pass. -problem_neurons = false(1,size(Y,2)); - - -for n = 1:size(Y,2) %First select neuron to use then do CV proceedure for each neuron separately. -%Update 2022-08-29 I think this will make the loop easier to manage -display(['Working on neuron #' num2str(n)]) -Y_cur = Y(:,n); %Select neuron -cBeta = cell(3,kfolds); %One for the beta and one for each part of the confidence interval -cPvalues = NaN(size(X,2)+1,kfolds); %Save the P value for each coefficent for each fold. Combine later with Fisher's method -cR2 = NaN(1,kfolds); %Save the R^2 value for each neurons fit. -cLL = NaN(1,kfolds); %Save the LL value for each neurons fit. -Y_cur_hat = NaN(size(Y_cur)); %Predicted fits that will be filled in with each fold. -lastwarn('','') %empty warning tracker before each model fit -% Run cross validation loop - for iFolds = 1:kfolds - display(['CV fold #' num2str(iFolds)]) - dataIdx = true(1,size(Y_cur,1)); %Start with using all of the session (i.e. all true indecies) - - if kfolds >1 - %Set the indecies of the TEST set in the fold to false - %Only left with the training indecies as true - dataIdx(randIdx(((iFolds - 1)*foldCnt) + (1:foldCnt))) = false; - end - - if is_smooth %Use Gaussian - - mdl = fitglm(X(dataIdx,:),Y_cur(dataIdx),'linear','Distribution','normal'); - warning_thrown = lastwarn; - else %Use Poisson - - mdl = fitglm(X(dataIdx,:),Y_cur(dataIdx), 'linear','Distribution','poisson'); - warning_thrown = lastwarn; - end - - %Put results from each fold in a storage variable - cBeta{1,iFolds} = mdl.Coefficients.Estimate; %Estimates of coefficents - temp = mdl.coefCI; - cBeta{2,iFolds} = temp(:,1); %In the second cell put the low confidence interval - cBeta{3,iFolds} = temp(:,2); %In the third cell put the high confidence interval - - cPvalues(:,iFolds) = mdl.Coefficients.pValue; %P value for t-test of significance of coefficent - cR2(1,iFolds) = mdl.Rsquared.Adjusted; %R squared value adjusted for dfe for each neuron and its data. - cLL(1,iFolds) = mdl.LogLikelihood;% LL for this fit - - if kfolds>1 - Y_cur_hat(~dataIdx) = mdl.feval(X(~dataIdx,:)); %Predict from the data that WAS NOT in the training set. - else - Y_cur_hat = mdl.feval(X); - end - end - %Check warning at the end, if not empty then indicate cell was a - %problem cell. - - if ~isempty(warning_thrown) - - problem_neurons(n) = true; - - end - - %Collect results for this neuron across folds - Betas(:,n) = mean(horzcat(cBeta{1,:}),2); %Take mean value across fold - %Note when we add back actual folds this may not work and we may have - %to turn this into a for loop or otherwise deal with it. - CIs(:,n,1) = mean(horzcat(cBeta{2,:}),2); - CIs(:,n,2) = mean(horzcat(cBeta{3,:}),2); - - - - pvalues_per(:,n) = mean(cPvalues,2); %CHANGE THIS TO FISHERS METHOD!!!!! - %USING AVERAGE FOR NOW FOR CONVENIENCE - - Rsquared_per(n) = mean(cR2,2); - LL_per(n) = mean(cLL,2); - Y_hat(:,n) = Y_cur_hat; - - - - -end -%% Results are collected sufficently above I think, so just put those as the output arguments. -end - diff --git a/Varia/GLM/old_code/GLM_CrossVal_Regularized.m b/Varia/GLM/old_code/GLM_CrossVal_Regularized.m deleted file mode 100644 index 646f3de..0000000 --- a/Varia/GLM/old_code/GLM_CrossVal_Regularized.m +++ /dev/null @@ -1,150 +0,0 @@ -function [Y_hat,Betas,CIs,Rsquared_per,LL_per,pvalues_per,problem_neurons] = GLM_CrossVal_Regularized(X,Y,kfolds,is_smooth) -%GLM_CROSSVAL Custom code to run the cross validation on our glm data with -%regularization -% Same as other function but starting by trying to use lassoglm with alpha -% set close to zero so that it effectively acts like ridge regression. -% Made it own function since this will require a fair amount of edits to -% the code. - -%INPUTS: -%X - design matrix for these models -%Y - ALL spiking data (will design function to loop over all neurons) -%kfolds - number of folds to use, scalar - - - -%OUTPUTS: - - -%% Preallocate variables Set up cross validation - -%Note: GLM fit also generates a coeff for the intercept term so have to add -%1 to all the size(X,2) things. -Betas = NaN(size(X,2)+1,size(Y,2)); %Average over CV Beta for each regressors (row) for each neuron (column) -CIs = NaN(size(X,2)+1,size(Y,2),2); %Average CI for each regressor, for each neuron, first page lower bound, second upper bound; -Rsquared_per = NaN(1,size(Y,2));%Average R^2 for EACH NEURON MODEL averaged over CV -%For now doing adjusted R^2, but can change this in the CV loop -LL_per = NaN(1,size(Y,2)); -pvalues_per = NaN(size(X,2)+1,size(Y,2),kfolds); -Y_hat = NaN(size(Y)); %Prediction for all time points for each neuron, concatenated over CV - -%rng(1) %For reproducability across runs - - - - -%Divide these into approximately kfolds groups (up to a rounding) -foldCnt = floor(size(Y,1)/kfolds); - - - -if kfolds >1 - - randIdx = randperm(size(Y,1)); %Get randperm of indecies for whole session - %Need to due permentation instead of just a partition since we have the - %block design (i.e. don't want fits that are only block 1 or block 2) - - -end - -%Update 2022-08-31: losing confidence in lassoglm. May instead go for -%Pillow code with regularization which should be ridge regression -%regularization. Either way we don't get the p-value for regressors (i.e. -%significance test for the betas) or a confidence interval around the betas -%to test this. -%% Loop over each neuron - -%Update 2022-08-29 might want to save more stuff than this, but I think -%this is okay for a first pass. -problem_neurons = false(1,size(Y,2)); - - -for n = 1:size(Y,2) %First select neuron to use then do CV proceedure for each neuron separately. -%Update 2022-08-29 I think this will make the loop easier to manage -display(['Working on neuron #' num2str(n)]) -Y_cur = Y(:,n); %Select neuron -cBeta = cell(3,kfolds); %One for the beta and one for each part of the confidence interval -cPvalues = NaN(size(X,2)+1,kfolds); %Save the P value for each coefficent for each fold. Combine later with Fisher's method -cR2 = NaN(1,kfolds); %Save the R^2 value for each neurons fit. -cLL = NaN(1,kfolds); %Save the LL value for each neurons fit. -Y_cur_hat = NaN(size(Y_cur)); %Predicted fits that will be filled in with each fold. -lastwarn('','') %empty warning tracker before each model fit -% Run cross validation loop - - for iFolds = 1:kfolds - display(['CV fold #' num2str(iFolds)]) - dataIdx = true(1,size(Y_cur,1)); %Start with using all of the session (i.e. all true indecies) - - if kfolds >1 - %Set the indecies of the TEST set in the fold to false - %Only left with the training indecies as true - dataIdx(randIdx(((iFolds - 1)*foldCnt) + (1:foldCnt))) = false; - end - - - - if is_smooth %Use Gaussian - - %Annoyingly think we mave to do cross validation inside of - %cross validation to get correct level of regularization for - %each fold. - %Have to Standardize movements before we get into this loop - %don't want to standardize event data. - - [B,mdl] = lassoglm(X(dataIdx,:),Y_cur(dataIdx),'normal','CV',5,'Alpha',.1,'Standardize',false); - warning_thrown = lastwarn; - - - else %Use Poisson - - [B,mdl] = fitglm(X(dataIdx,:),Y_cur(dataIdx), 'poisson','CV',10); - warning_thrown = lastwarn; - end - - %Put results from each fold in a storage variable - cBeta{1,iFolds} = mdl.Coefficients.Estimate; %Estimates of coefficents - temp = mdl.coefCI; - cBeta{2,iFolds} = temp(:,1); %In the second cell put the low confidence interval - cBeta{3,iFolds} = temp(:,2); %In the third cell put the high confidence interval - - cPvalues(:,iFolds) = mdl.Coefficients.pValue; %P value for t-test of significance of coefficent - cR2(1,iFolds) = mdl.Rsquared.Adjusted; %R squared value adjusted for dfe for each neuron and its data. - cLL(1,iFolds) = mdl.LogLikelihood;% LL for this fit - - if kfolds>1 - Y_cur_hat(~dataIdx) = mdl.feval(X(~dataIdx,:)); %Predict from the data that WAS NOT in the training set. - else - Y_cur_hat = mdl.feval(X); - end - end - %Check warning at the end, if not empty then indicate cell was a - %problem cell. - - if ~isempty(warning_thrown) - - problem_neurons(n) = true; - - end - - %Collect results for this neuron across folds - Betas(:,n) = mean(horzcat(cBeta{1,:}),2); %Take mean value across fold - %Note when we add back actual folds this may not work and we may have - %to turn this into a for loop or otherwise deal with it. - CIs(:,n,1) = mean(horzcat(cBeta{2,:}),2); - CIs(:,n,2) = mean(horzcat(cBeta{3,:}),2); - - - - pvalues_per(:,n) = mean(cPvalues,2); %CHANGE THIS TO FISHERS METHOD!!!!! - %USING AVERAGE FOR NOW FOR CONVENIENCE - - Rsquared_per(n) = mean(cR2,2); - LL_per(n) = mean(cLL,2); - Y_hat(:,n) = Y_cur_hat; - - - - -end -%% Results are collected sufficently above I think, so just put those as the output arguments. -end \ No newline at end of file diff --git a/Varia/GLM/old_code/GLM_Firstpass.m b/Varia/GLM/old_code/GLM_Firstpass.m deleted file mode 100644 index a097f9e..0000000 --- a/Varia/GLM/old_code/GLM_Firstpass.m +++ /dev/null @@ -1,534 +0,0 @@ -%% Running Notes - -%Code to start doing GLM based analyses on datalogger data. Develop in -%this script then convert to functions/clearner code later. - -%2022-08-06 as a start just working on things based on Pillow talk at -%COSYNE to recall what we did with GLMs previously. - -%Pillow suggests avoiding glmfit as it is sensitive to dependencies and -%correlations between regressors in the design matrix. Instead suggests -%writing own function for calculating the likelihood and then using an -%optimizer in matlab itself. This mirrors work I did with grid cells way -%back in the day, so can also review that and borrow from that code. - -%Need to think of a clever way to handle the design matrices and make sure -%we are clear what regressor is which column at all times. - -%Definitely come back and make most of these functions that are just called -%from this main script later. Otherwise I think this will be a mess with -%the different models we want to run. - -%Also note that we will have to add a cross validation step once we have -%this working so we can get a better assement of the performance. - - -%% Copy loading data code from dimensionality tests / Set parameters -is_mac = 0; %For loading the data -is_ron = 1; %For setting path -with_partner =1; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -BRs = ["TEO", "vlPFC"]; %Channels considered (sets brain region TEO vlPFC all -S_list = [1]; %List of session to pull; %For now focus on first Amost sessions -RegGroupNum = 3; %Set based on how many different cases we are looking at +1 for complete model; %Change this later to match the number of analyses being ran -%Use these to set the results structure -with_NC =1; %0: Noise cluster (NC) is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well-isolated units - -Results(1).BR = BRs(1); -Results(1).Data = cell(RegGroupNum,1); -Results(1).Spikes = []; %Don't think we will have different spiking data across models -Results(1).Stats.overall = cell(RegGroupNum,1); %Stick the overall results from each analysis in a cell in this part of the structure - - -Results(2).BR = BRs(2); -Results(2).Data = cell(RegGroupNum,1); -Results(2).Spikes = []; -Results(2).Stats.overall = cell(RegGroupNum,1); %Stick the overall results from each analysis in a cell in this part of the structure - -for s = S_list - for br = 1:length(BRs) - channel_flag = BRs(br) - s - - %% Load data - - %Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_Ron(filePath, temp_resolution, channel_flag, is_mac,is_ron, with_NC, isolatedOnly); - % Output: - % 1. Spike_rasters: n_neuron X time in seconds (if temp_resolution = 1) - % 2. labels: time X 11 - check the function description for - % details. Essentially subject behavior is column 3. - % 3. labels_partner: time X 11. Partner behavior is column 3. - % 4. behav_categ: string vector; behavioral category do the numbers in - % labels correspond to. - % 5. block_times: specifies the block times and ID - % 6. monkey: subjecet monkey ID - % 7. reciprocal_set: set of behaviors that are deeemed "reciprocal" - % 8. social_set: set of behaviors that are deeemed "social" - % 9. ME_final: Motion energy values - % 10. unit_count: number of units in the session - % 11. groom_labels_all: labels for grooming. Check function - % description for details - - session_length = size(Spike_rasters,2); % get session length - subsample = 1; %Toggle whether to subsample - - if subsample - - n_2_use = 100; %for now just set using 100 neurons - - Spike_rasters = Spike_rasters(randperm(size(Spike_rasters,1),n_2_use),:); - - - end - - Spikes_final = Spike_rasters'; - -%% If smoothing do smoothing/high frequency low frequency analysis here. Put spikes in results struct - - - Results(br).Spikes = Spikes_final; - - %Pre-allocate perneuron results since now know how many neurons we - %have - - Results(br).Stats.perneuron = cell(RegGroupNum,size(Spikes_final,2)); %For now have each row be an experiment (analysis) and each column be a neuron - - -%% Set Proximity to non-identified behavior - - % 2022-08-06 update: had to change this code if allowing for multiple - % behaviors at any moment. - - %Extract behavior labels for subject and partner. Now as cell - %array with all behaviors in that moment in the cell (get ready for a lot of cellfun). Still obs x - %behaviors - - behavior_labels_subject_init = labels(:,2); %Extract all behavior info for subject - behavior_labels_partner_init = labels_partner(:,2); %Extract all behavior info for partner - - % Set proximity as rest - prox_ind = find(behav_categ=="Proximity"); - %Not exactly sure the best way to do this...not sure how to do this - %as a cellfun. For now just going to do a for loop to move on. - - - %NOTE: ASSUMES THAT REST/UNIDENTIFIED IS ALWAYS MAX NUMERIC LABEL - - for i = 1:length(behavior_labels_partner_init) - - check_inds = behavior_labels_subject_init{i,:} == prox_ind; - - if any(check_inds) - - behavior_labels_subject_init{i}(check_inds) = length(behav_categ); - - end - - check_inds = behavior_labels_partner_init{i,:} == prox_ind; - - if any(check_inds) - - behavior_labels_partner_init{i}(check_inds) = length(behav_categ); - - end - - end - - - - %% Clean behavior data - pick behavioral epochs to consider - %% Only take epochs where the monkey is behaving - %Update 2022-08-06: only include epochs where the monkey/partner is doing an - %indentifiable behavior - - %Update 2022-08-08: This will be the likely spot to start the function. - %For now just run in the cells and then make a diagram of what needs to - %be done to set up design matrix for any case. To be sure doing that - %well probably will need to actually take the time to write out the - %pipeline on paper and the implement that flow chart. - - %NOTE: ASSUMES THAT REST/UNIDENTIFIED IS ALWAYS MAX NUMERIC LABEL - - %Update 2022-08-08: For now just set this to concatenate behavior inds - %from when either the partner or subject is doing a behavior that isn't - %just rest (or proximity which has now been made rest). - - Behav_inds = zeros(size(behavior_labels_subject_init)); - - for i = 1:length(behavior_labels_subject_init) %Choose which behavior set you want to consider - - temp = [(behavior_labels_subject_init{i,:} ~= length(behav_categ))... - (behavior_labels_partner_init{i,:} ~= length(behav_categ))]; - - Behav_inds(i) = sum(temp); %If ANY behavior is not the rest behavior keep the bin - - end - - Behav_inds = Behav_inds > 0; %If ANY behavior is not the rest behavior keep the bin - - Spikes = Spikes_final(Behav_inds,:); - Behavs = [behavior_labels_subject_init(Behav_inds) behavior_labels_partner_init(Behav_inds)]; %keep separate cells so it is clear which behavior is subject and which behavior is partner - - - - - %% Get index label for different behaviors - - %This can be done together by just concatenating the behaviors for - %both subject and partner - - %Self note: make this a function later on - - %Start with just considering current state that is present. - - %Don't have to consider only having one state at a time for this - %model...maybe start with this just to match other analyses/as this - %guarantees no correlation or dependencies between regressors so we can - %just use glmfit. - - %Also starting with just considering subject behavior. - - %Observations x behavior label - obs_behav_subject = unique(cell2mat(labels(:,3))); - %Need to make a column for each behavior that happens in the session. - %Above code does assume there is no behavior that only occurs paired - %with another behavior and that was removed when only one behavior per - %moment was prioritized. I think this is a reasonable assumption, so - %will go forward with it. - - %Remove rest and proximity since we aren't considering these - %Note, if you want to just look at particular behaviors can just add - %the ones you want to remove to inds2remove. Will come back to set a - %for loop to make this easier later. - - inds2remove = [find(behav_categ == 'Proximity'), length(behav_categ)]; - - inds2remove = sum(obs_behav_subject == inds2remove,2)'; %put them into one vector with a 1 at every ind that needs to be removed - - obs_behav_subject(inds2remove>0) = []; - - %For now just repeat. Later have this be either structure or a cell - %and have a function that adds to it as needed. - - obs_behav_partner = unique(cell2mat(labels_partner(:,3))); - - inds2remove = [ find(behav_categ == 'Proximity'), length(behav_categ)]; - - inds2remove = sum(obs_behav_partner == inds2remove,2)'; %put them into one vector with a 1 at every ind that needs to be removed - - obs_behav_partner(inds2remove>0) = []; - - - obs_behav = [{obs_behav_subject}, {obs_behav_partner}]; - - - %% Get regressor to variable mapping for Design Matrix - - %2022-08-08: This again will be an important part to carefully design - %into a function. Probably make a partial X_regs for each family of - %variables (subject, partner, movement) and then concatenate at the end - %as was done in the Tremblay and Testard et al 2022 analysis. - - %For now just set up a for loop and use cell array of terms to save - %different Regressors - - X_groups = cell(1,length(obs_behav)); - - Reg_mapping = cell(1,length(obs_behav)); - - for this_mat = 1:length(obs_behav) - - X_groups{this_mat} = zeros(length(Spikes), length(obs_behav{this_mat})); - - %Note, behavior numeric labels will be larger than this matrix (e.g. - %there are 22 behaviors but one of the labels is 29.) Thus need to - %create/save mapping between numeric label in behavior_labels and the - %column of the design matrix. - - %First row is behavior label, 2nd row is original numeric label, 3rd - %row is column in design matrix (to start, if we add lags will be set - %of columns hence why this is a cell array) - - - Reg_mapping{this_mat} = cell(3,length(obs_behav{this_mat})); - - - for this_label = 1:length(obs_behav{this_mat}) - - Reg_mapping{this_mat}{1,this_label} = behav_categ{obs_behav{this_mat}(this_label)}; - - Reg_mapping{this_mat}{2,this_label} = obs_behav{this_mat}(this_label); - - Reg_mapping{this_mat}{3,this_label} = this_label; %Note will have to mess around with this if we introduce delays in regressors - - %Put ones into design matrix for indicated column whenever that behavior was present - %Update: can't just do this now that we have the potential for - %multiple behaviors at each time point. Instead for each behavior - %will loop through the session and put a 1 in the design matrix - %each time that behavior occurred. This is not efficient code, but - %for now this is fine. Try to think of something clever later on. - - for i =1:length(Behavs) - X_groups{this_mat}(i,this_label) = sum(Behavs{i,this_mat}(:) == Reg_mapping{this_mat}{2,this_label}); %Sum is just there to convert back to double/account for multiple behaviors - %We are only checking one behavior at a time in this loop so it - %won't result in a value greater than 1. - end - - end -% -% %Quick sanity check that things look roughly correct. -% figure -% imagesc(X) -% xticks([1:length(obs_behav)]) -% xticklabels(Reg_mapping(1,:)) -% xtickangle(45) -% -% %Make sure there are bins with more than one behavior now/get a sense -% %for how often that happens. -% -% figure -% histogram(sum(X,2)) - - - - - end - - %% Concatenation steps - - %2022-08-08: Need to be VERY careful here so that we can keep track of - %the relationship between "group" (subject, partner, movements, etc.), - %variable (e.g. subject self groom events) and regressors - %(e.g. all time lags for subject self groom events. Current thinking is - %in above block get each separate desigh matrix and mapping between - %variable and regressors for each "group" and then in this block put - %things together now that we know what we are working with. - - %First get labels straight then do simple horzcat on all Xs from above - - num_regingroup = cellfun(@length,Reg_mapping); %Get # of variables that are in each group of Reg_mapping - - num_regs = sum(num_regingroup); %Get total. - - temp = cumsum(num_regingroup); %For us below - - %Update: for now thinking of saving a separate var with the list of - %regressors mapping on to a concatenated Reg_mapping. - - reg_list = [1:num_regingroup(1)]; - - for group = 2:length(Reg_mapping) %Will definitely have to edit this later when include delays - %In short this code works because we are assuming there is only one - %regressor for each variable which won't be the case when we add in - %the delays. - - %...again have to write this all out to be sure we get everything taken care of - - %For each group greater than 1 add the number of regressors from all - %previous groups to regressors in that Reg_mapping - - %Annoying, but can't think of a way to change these values inline - %(i.e. to set each of the values in Reg_mapping to this new value) - %without using a for loop - - - %Just append - reg_list =[reg_list cellfun(@(x) x+temp(group-1),Reg_mapping{group}(3,:))]; - -% for reg = 1:num_regingroup(group) -% -% Reg_mapping{group}{3,reg} = temp2(reg); -% -% end - - end - - %Now just through all X's together into big X - - X = horzcat(X_groups{:}); - - %% Add code for QR check and elimination of redundant regressors - - %2022-08-10 Curious about this, so maybe steal some moments to do some - %work in the south of France - - %% Choose brain region for response matrix - - %Need to ask Camille how to get this information out. Otherwise will - %have to again run this as an outer loop at the beginning via the - %channel flag option above. It looks like the info is hard coded in - %the loading function and therefore could be passed as yet another - %output argument. For the moment just add to the top like before even - %though this is not efficient. - - Y = Spikes; - - %% GLM - - %2022-08-06 First pass just use glm fit with a poisson on the data - %Self note: need to dump these into the results structure - %...looks like glmfit may need to work on one neuron at a time which is - %a bit annoying... - - %2022-08-08 UPDATE: When do partner getting the ill-conditioned - %complaint again weirdly. Need to go back a do some checks again on - %why. Definitely not an issue of zeros. Probably an issue with - %correlations between regressors... - %This is the case - %See result of below. If it is not 1 than matrix is not full rank so - %some regressors a linearly dependent on others. - % rank(X) == size(X,2) - - %Have choices here. Can do regularization which J Pillow suggested can - %force the estimate to be better conditioned (essentially remove - %equivalent answers via prior that is set on the weights). Could also - %do QR check like was done in Trembaly and Testard et al. 2022 - %analysis and remove redunant regressors that way. - - %First going to try just using one of the X's instead of both - - %Learned lassoglm uses glmfit and doesn't work like fitglm so will need - %to most likely use Pillow code (as previously thought) to do - %regularization. - - - X_hold = X; - - dur = 1:size(Spikes,1); - num_neuro = 10; - - n_picked = randsample(size(Y,2),num_neuro); - - group_labels = {'Subject', 'Partner'}; - - for the_x = 1:length(X_groups) - - - X = X_groups{the_x}; - - for this_neuron = 1:length(n_picked) - - %the_names = 'think of a clever way to get variable names here - %maybe? otherwise just leave alone' - %tbl =array2table([X,Y(:,n_picked(this_neuron)),'VariableNames',the_names) - % - - %[Betas, Dev, Stats] = glmfit(X,Y(:,n_picked(this_neuron)),'poisson'); - %Put some thought into using updated functions which spit out a mdl - %object that contains all this info, rather than this. have fitglm - %or lassoglm which appears capable of lasso regression, close to ridge - %regression or, elastic net regression depending on parameters put - %into alpha function. - - %Change this to fitglm which will be easier to deal with the - %results from. Then do lassoglm so we can use some regularization. - - mdl = fitglm(X,Y(:,n_picked(this_neuron)),'linear','Distribution','poisson'); - Results(br).Stats.perneuron{the_x, this_neuron} = mdl; - - Y_hat = mdl.feval(X); - - figure('Position', [350 150 900 600]) - plot(dur,Y(:,n_picked(this_neuron))) - hold on - plot(dur,Y_hat) - xlim([0,length(dur)+10]) - title(['Reg Group: ' group_labels{the_x} ' '... - 'Session ' num2str(s) ' '... - BRs(br) ' ' ... - ' neuron # ' num2str(this_neuron)]) - - - end - end - - %For now at the end do the full design matrix even though we need to - %remove correlations - - X = X_hold; - - for this_neuron = 1:length(n_picked) %Also need to think a good bit about how to get these results together than through them into results structure. - - [Betas, Dev, Stats] = glmfit(X,Y(:,n_picked(this_neuron)),'poisson'); - - mdl = fitglm(X,Y(:,n_picked(this_neuron)),'linear','Distribution','poisson'); - Results(br).Stats.perneuron{end, this_neuron} = mdl; - - Y_hat = mdl.feval(X); - figure('Position', [350 150 900 600]) - plot(dur,Y(:,n_picked(this_neuron))) - hold on - plot(dur,Y_hat) - xlim([0,length(dur)+10]) - title(['Reg Group: All Regressors' ' '... - 'Session ' num2str(s) ' '... - BRs(br) ' ' ... - ' neuron # ' num2str(this_neuron)]) - - - end - end%Brain region loop (hopefully can remove this later) -end %Session loop - - - - - -%% OLD CODE - - %First pass doesn't look great/getting some warnings that need to be - %sorted out. To troubleshoot this going to just make some random X's - %to see that they don't cause an error - - %TEST 1---- - %Make random sequence, but allow for states to co-occur. Make it double instead of bool - %Okay...this gives expected result. Just guess constant value since - %the regressors are totally random - -% X = (rand(size(Spikes,1),length(obs_behav)) > 0.5)*1.0; - - %TEST 2----, - %same idea, but only let one state be active at a time - %UPDATE: This is what causes the issue. Apparently it creates - %difficulties to assign proper fits to each neuron if only one state is - %allowed to occur at a time for the whole session. Change above code - %to change this. -% -% X = rand(size(Spikes,1),length(obs_behav)); - -% for i = 1:length(X) %not the best but just need this for the quick check -% -% [~, max_ind] = max(X(i,:)); -% mask = [1:size(X,2)] == max_ind; -% X(i,~mask) = 0; -% X(i,mask) = 1; -% -% end - - -%Update: Still getting ill conditioned error, so probably need to use -%custom fit instead of glmfit. In general need to go through Pillow stuff -%again - -% TEST 3 ------ -% To get a sense for the issue. Trying shuffling regressors and -% seeing how shuffled they need to be before this warning goes away. -% -% - -%Temp block -% close all -% X = X_hold; -% shuffles = 1; %set how many times to shuffle -% var_shuffles = []; -% for this_s = 1:shuffles -% this_v = randi(size(X,2)); -% var_shuffles = [var_shuffles, this_v]; -% X = X(randperm(size(X,1)),this_v); -% end -% -% Reg_mapping{1,var_shuffles} -% - \ No newline at end of file diff --git a/Varia/GLM/old_code/GLM_Pipeline_Try1.m b/Varia/GLM/old_code/GLM_Pipeline_Try1.m deleted file mode 100644 index 791cb71..0000000 --- a/Varia/GLM/old_code/GLM_Pipeline_Try1.m +++ /dev/null @@ -1,1057 +0,0 @@ -%% Running Notes -%Start 2022-08-22 -%Making this separate script to work on the preprocessing steps (stage 1) -%and design matrix creation steps (stage 2) for the data. Separating -%things out to help prevent the massive script that was the end result of -%Tremblay and Testard 2022. - -%Will make cell of this script into function calls whenever possible - -%Also creating another script for stage 3 which will actually do the GLM -%analyses and be slotted into the outer loop from this script. - -%Again, main focus is having a MUCH cleaner end result than we had with T -%and T 2022. - -%2022-08-23 -%Focus for now on getting design matrix with state-events (what I am calling -%things like subject behavior, partner behavior, context) working and then -%will come back to get that working with kinematics as not clear when -%exactly we will have kinematics and doesn't seem like we will have them in -%time for Parma...have to see. For now need to get this to work while -%keeping track of regressors then switch to getting GLM loop to work. - -%2022-08-28: Note that need to have ridgeregression folder in this repo on -%the path for some of the functions needed to make the design matrix. -%Will also need for qr checks. We can add a line to auto add this to the -%path later if needed. - -%2022-08-29 Use smoothed firing rate instead of spiking; adjust -%accordingly. Use the new load in function from Camille and use is_mac to -%toggle file path. - -%2022-08-30 Note: Due to difference in the QR check we WILL have different -%number of regressors for different models; so all comparisons will have -%to have that adjustment. Not a deal breaker but important to note. - -%2022-08-30 Another important update: without the cross validation the -%fitting of the data is absurdly high (assuredly because we are -%overfitting) so need to fix this to get meaningful results and don't read -%into the shuffling not having a huge effect yet. -%% Stage 1 Get data loaded in and set up loops - - -%% Step 1.0: Set filenames, Set loop for sessions - -%Update notes: Make these straightfoward to change to interact with later -%functions - -%Parameters for setting Path -is_mac = 0; %For loading the data %2022-08-29 Use this to replace is_ron -is_ron = 1; %For setting path - - - -%Parameters for setting sessions to use and brain regions to look at -S_list = [1]; %List of session to pull; %For now focus on first Amost sessions -BRs = ["TEO", "vlPFC", "all"]; %Channels considered (sets brain region TEO vlPFC or all) - - - - - - -%Parameters for neural data to be used across sessions -with_NC =1; %0: Noise cluster (NC) is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well-isolated units -temp_resolution = 1; %Temporal resolution of firing rate. 1sec %%Note this will change if we introduce smoothing -smooth_fr = 1; %Toggle to use smooth spike rasters (currently not setting up this code as may require working with ms neural data and then downsampling to get back) - - - - -for s = S_list %Do analyses for each session put in S_list - - %Set path -if is_ron - - sessions = dir('C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_data'); sessions = sessions(3:end,:); - filePath = ['C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_data\' sessions(s).name]; - savePath = ['C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_results']; - -else - - - %Set session list -home = '~'; % set home directory -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) -savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/partner_vs_subject']; -end - -%% Step 1.1 Preallocate Results structure; save spikes for each brain region; performing smoothing and save results if smoothing. - - clear Results %make sure Results is cleared before each session is ran - %Preallocate one branch of Results for each BR used. Save one results file for each session. - %Put data from each brain region into appropriate Results path. - %Only Spike_raster should change so just keep the behavior and the rest - %from the last BR load in. - for br = 1:length(BRs) - - Results(br).BR = BRs(br); - channel_flag = Results(br).BR; - %Get data with specified temporal resolution and channels - %2022-08-23 note: have to change this to new function when Camille - %is done with that and rego through everything again to make sure - %there isn't a change. - - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_Ron(filePath, temp_resolution, channel_flag, is_mac,is_ron, with_NC, isolatedOnly); - % Output: - % 1. Spike_rasters: n_neuron X time in seconds (if temp_resolution = 1) - % 2. labels: time X 11 - check the function description for - % details. Essentially subject behavior is column 3. - % 3. labels_partner: time X 11. Partner behavior is column 3. - % 4. behav_categ: string vector; behavioral category do the numbers in - % labels correspond to. - % 5. block_times: specifies the block times and ID - % 6. monkey: subjecet monkey ID - % 7. reciprocal_set: set of behaviors that are deeemed "reciprocal" - % 8. social_set: set of behaviors that are deeemed "social" - % 9. ME_final: Motion energy values - % 10. unit_count: number of units in the session - % 11. groom_labels_all: labels for grooming. Check function - % description for details - Results(br).Spikes = Spike_rasters'; - - if smooth_fr - - %2022-08-29 temp solution is to just copy and paste smoothing - %code here. I'm not exactly sure which of the loading - %functions is the right one and need to review again what the - %differences are. For now do this and deal with the difference - %later tonight after sure the rest of the code runs. - sigma = 5; - gauss_range = -3*sigma:3*sigma; %calculate 3 stds out, use same resolution for convenience - smoothing_kernel = normpdf(gauss_range,0,sigma); %Set up Gaussian kernel - smoothing_kernel = smoothing_kernel/sum(smoothing_kernel); - smoothing_kernel = smoothing_kernel * 1; %Rescale to get correct firing rate - Spike_rasters_smooth = conv2(Spike_rasters, smoothing_kernel,'same'); - Spike_rasters = Spike_rasters_smooth; - Results(br).fr = Spike_rasters_smooth'; - - end - - end %End brain region loop. Should now have neural activity saved for each brain region and them together in Results - -%% Step 1.2 Get (fake) kinematics data; Get Nans - -%Later this will be the function call to get the kinematics data if that -%isn't included in the above load in code. For now just make to random -%gaussian sets and throw some random NaNs in their to work on the NaN -%clearing code (mostly borrowed from T and T 2022); - -fake_x = normrnd(0,1,size(labels,1),1); -fake_y = normrnd(0,1,size(labels,1),1); - -nan_per = 0; %Set percent (as decimal) of nans each individual fake regressor gets -fake_x(randsample(length(fake_x),round(nan_per*length(fake_x))),1) = NaN; -fake_y(randsample(length(fake_y),round(nan_per*length(fake_y))),1) = NaN; - -kinematics = [fake_x fake_y]; - -nan_counts = sum(isnan(kinematics),2); - -% figure() -% histogram(nan_counts) %Shows number of nans and their overlap (all numbers greater than 1) -% - -%If needed put code for interpolating over short runs of nans from T and T -%2022 here. For now, leaving out. - -nan_inds = nan_counts > 0; %Naturally anything that has a count greater than 0 is a time point that has an NaN - -%% Step 1.3 Set Proximity to non-identified behavior -%Update 2022-08-30 switching to only considering 1 behavior happening at -%any moment to see if this resolves rank issues in our design matrix during -%cross validation. - - behavior_labels_subject = labels(:,3); %Extract all behavior info for subject - behavior_labels_partner = labels_partner(:,3); %Extract all behavior info for partner - - % Set proximity as rest - prox_ind = find(behav_categ=="Proximity"); - %Not exactly sure the best way to do this...not sure how to do this - %as a cellfun. For now just going to do a for loop to move on. - - - %NOTE: ASSUMES THAT REST/UNIDENTIFIED IS ALWAYS MAX NUMERIC LABEL - - for i = 1:length(behavior_labels_partner) - - check_inds = behavior_labels_subject{i,:} == prox_ind; - - if any(check_inds) - - behavior_labels_subject{i}(check_inds) = length(behav_categ); - - end - - check_inds = behavior_labels_partner{i,:} == prox_ind; - - if any(check_inds) - - behavior_labels_partner{i}(check_inds) = length(behav_categ); - - end - - end - -%Temporary joining of those cells so I can easily check subject and partner behavior -Behavs = [behavior_labels_subject behavior_labels_partner]; - -%End stage 1: Now have data we will use to make design matrix in stage 2 -%and the spiking data in the form it will be used for Stage 3 - -%% Stage 2 Create regressors - -%TERMINOLOGY NOTES: -%-Variable group = family of variables put together due to belong to the -%same type of observation. Currently have: subject behavior, partner -%behavior, context, and kinematics - -%-Variable = A single state that can be observed or key point that can be -%measured. For example: grooming, agression, block id, arm_x position. - -%-Regressor = A column of the design matrix. Each variable can have -%multiple regressors. Examples: grooming could have a regressor for -%particular delays before or after the onset/offset of grooming in addition -%to having a regressor for stating whenever the animal is currently -%grooming. arm_x could have both a tracking of the position of the arm as -%well as a regressor for arm events when the arm is moved above a -%particular threshold of movement. - -%-Regressor group = Would refer to groups of -%regressors across several variables put together for a particular analysis -%that is different from the Variable group. Examples: all regressor -%relating to head and eye movement in kinematics may be grouped together in -%an analysis that focuses on a subset a kinematics and could be call the -%head-eye regressor group. All behaviors deemed affiliative by either the -%subject or partner could be grouped together for an affiliative regressor -%group. - -%State-events refer to all non-kinematic, binary regressors. - -%update 2022-08-23 trying to see to what extent code Camille already wrote -%needs to be changed. I don't love doing it this way as -%this is again goes back to having a massive script, but going to use it as -%a starting point and then change it as needed... -%This is really super janky and messy so probably just rewrite things so -%there are consistent coding conventions throughout. - -%% Step 2.0 Set options for time event kernels - -%set up sampling rate (not sure this is useful, but it's used in code further down so i'll keep it for now). -opts.Fs = temp_resolution; - -%Set up windows for time varying kernels. Multiplying factor is in seconds -%(recall FS is samples/second ),so can adjust window accordingly - -%2022-08-23: need to check this is still correct. - -opts.mPreTime = round(2 * opts.Fs); %motor pre time -opts.mPostTime = round(2 * opts.Fs); %motor post time -motorIdx = [-(opts.mPreTime: -1 : 1) 0 (1:opts.mPostTime)]; %index for design matrix to cover pre- and post motor action - - - -%% Step 2.1 Get labels for each state-event variable group - -%2022-08-23 rewrote this and borrowed from first GLM attempt because -%previous code does not work. - -%First get labels for all subject, partner, and context variables so we -%know how many variables we have to start with - -%Update 2022-08-29 need to remove proximity here! - -prox_ind = find(behav_categ == 'Proximity'); - - -%Observations x behavior label -obs_statevents_subject = unique(cell2mat(labels(:,3))); -%Need to make a column for each behavior that happens in the session. -%Above code does assume there is no behavior that only occurs paired -%with another behavior and that was removed when only one behavior per -%moment was prioritized. I think this is a reasonable assumption, so -%will go forward with it. - -%For now just repeat. Later have this be either structure or a cell -%and have a function that adds to it as needed. - - -obs_statevents_subject( obs_statevents_subject == prox_ind) = []; %Remove proximity as an observed event - -obs_statevents_partner = unique(cell2mat(labels_partner(:,3))); - -%Update 2022-08-30 removing partner groom other as this is redundant with -%getting groomed for the subject...okay know partner drinking is causing -%the issue. Again just try removing these for now so we can at least run -%the cross validation without the delays (maybe with if this is the source -%of all the issues)...it feels like this problem just keeps cropping up -%with new behaviors each time I eliminate one, but curious to see how far -%this goes, particularly since they are all partner behaviors...okay now I -%am just done as it selected rest which seems like it should definitely not -%be redundant with any behaviors...on second thought. Let's also just -%change the beginning so there is only one behavior per moment as that -%might fix a lot of this too...This did not fix the issue and there still -%are problems on some runs. -%Update: below combination looks like it consistently works at least when -%there are no delays. Trying delays now...delays still does not work. -%Okay. Let's see it run with the delays taken out so we at least have that -%and then I can go in and try the lassoglm function and if that fails/isn't good try -%to pull from the regularization function Pillow has. - - - - -%...this also speaks to need to add regularization to the regression in the hopes that should resolve the rank -%collapsing issues for the design matrix...I think this demands just using the -%regularization to deal with this issue even if that means having to go -%back in and change the code/how we get the results out. - -pgp_ind = find(behav_categ == "Groom partner"); -pgg_ind = find(behav_categ == "Getting groomed"); -pfor_ind = find(behav_categ == "Foraging"); -pdr_ind = find(behav_categ =="Drinking"); -papp_ind = find(behav_categ =="Approach"); -pagg_ind = find(behav_categ == "Aggression"); -pttp_ind = find(behav_categ=="Threat to partner"); - -obs_statevents_partner( obs_statevents_partner == prox_ind) = []; %Remove proximity as an observed event -obs_statevents_partner(obs_statevents_partner == pgp_ind) = []; %Remove partner giving grooming as this is redundant with subject receiving. -obs_statevents_partner(obs_statevents_partner == pgg_ind) = []; %Remove because vice versa for getting groomed - -%Removing all below due to rank issues. -obs_statevents_partner(obs_statevents_partner == pfor_ind) = []; -obs_statevents_partner(obs_statevents_partner == pdr_ind) = []; -% obs_statevents_partner(obs_statevents_partner == papp_ind) = []; -obs_statevents_partner(obs_statevents_partner == pagg_ind) = []; -obs_statevents_partner(obs_statevents_partner == pttp_ind) = []; - -obs_context = 1:size(block_times,1); %For now just considering the three different blocks for context. - -obs_statevents = [{obs_statevents_subject}, {obs_statevents_partner} ,{obs_context}]; %Gives number of varibles in each group of states events considered - -% -%% Step 2.2 Create base regressors for each state-event variable and regressor mapping - -%For first try trying to save each variable group into it's own cell in -%X_groups with all of the information before we combine or add additional -%regressors for time event kernel - -%Update 2022-08-23 Reg_mapping may not work as inteded on on second thought but -%leaving there for now. - -%+1 for kinematics - X_groups = cell(1,length(obs_statevents)+1); - - Reg_mapping = cell(1,length(obs_statevents)+1); - - for this_mat = 1:size(Behavs,2) %Currently this loop only works for the behaviors. Will need to write a separate and exteneded loop for Context - - X_groups{this_mat} = zeros(length(Spike_rasters), length(obs_statevents{this_mat})); - - %Note, behavior numeric labels will be larger than this matrix (e.g. - %there are 22 behaviors but one of the labels is 29.) Thus need to - %create/save mapping between numeric label in behavior_labels and the - %column of the design matrix. - - %First row is behavior label, 2nd row is original numeric label, update - %2022-08-30 don't add 3 row for now. This will be index in matrix - %after nan check then will add subsequent rows which will hold the - %indecies for the regressors associated with each variable in each - %experiment after the QR check. Also at the end going to make - %Reg_mapping a table so it is easier to navigate. - - - Reg_mapping{this_mat} = cell(2,length(obs_statevents{this_mat})); - - - for this_label = 1:length(obs_statevents{this_mat}) - - Reg_mapping{this_mat}{1,this_label} = behav_categ{obs_statevents{this_mat}(this_label)}; - - Reg_mapping{this_mat}{2,this_label} = obs_statevents{this_mat}(this_label); - -% Reg_mapping{this_mat}{3,this_label} = this_label; %Note will have to mess around with this if we introduce delays in regressors -% -% %Put ones into design matrix for indicated column whenever that behavior was present -% %Update: can't just do this now that we have the potential for -% %multiple behaviors at each time point. Instead for each behavior -% %will loop through the session and put a 1 in the design matrix -% %each time that behavior occurred. This is not efficient code, but -% %for now this is fine. Try to think of something clever later on. - - for i =1:length(Behavs) - X_groups{this_mat}(i,this_label) = sum(Behavs{i,this_mat}(:) == Reg_mapping{this_mat}{2,this_label}); %Sum is just there to convert back to double/account for multiple behaviors - %We are only checking one behavior at a time in this loop so it - %won't result in a value greater than 1. - end - - end - - - - - end %End loop for setting this up for behaviors - - %Do the same thing for context which is only block id for now. Can't - %be added easily to above loop or put into some general function since - %the information is not stored in that way. - - this_mat = this_mat+1; - - X_groups{this_mat} = zeros(length(Spike_rasters), length(obs_statevents{this_mat})); - - for this_label = 1:length(obs_statevents{this_mat}) - - Reg_mapping{this_mat}{1,this_label} = block_times.Behavior{this_label}; - - Reg_mapping{this_mat}{2,this_label} = obs_statevents{this_mat}(this_label); - -% Reg_mapping{this_mat}{3,this_label} = this_label; - - %Need to populate each block using the info in block_times table - - X_groups{this_mat}(block_times.start_time_round(this_label):... - block_times.end_time_round(this_label)... - ,this_label) = 1; %Put ones in the column for that block id based on block_times - - end - - Reg_mapping{2}(1,:) = append('partner.',Reg_mapping{2}(1,:)); %Add this now to prevent confusion later. -%% Step 2.3 First concatenation, get names and regressors - -%Combine behavioral events -behavEvents=horzcat(X_groups{1:3}); - -%Get names via Reg_mapping -behavEventNames = horzcat(Reg_mapping{1}(1,:)... - ,Reg_mapping{2}(1,:),... - Reg_mapping{3}(1,:)); %2022-08-29: Cam added more info to block (gender pairing in not alone) - - - -%Set up event type here. -%Note: not creating delays for context variables for now. Need to see if -%this messes up the next part of the code. If so I guess make context -%"whole trial" and set something up in that code. Update going to add -%zeros to the end as this should maybe work and won't get indecies -%otherwise - -%2022-08-30 update: TEST************************************************ -%Not doing any delays to see how this interacts with the full rank problem -behavEventTypes= [ones(1,length(behavEventNames)-length(obs_context))*0 zeros(1,length(obs_context))]; -% For now I will assign all behavioral events as event type 3 -%Event Type 1 = Whole trial -%Event Type 2 = from stimulus onset to the rest of the trial -%Event Type 3 = from before movement onset to after movement onset (pre & post time) - -%% Setup Design Matrix - Behavioral Events - - - -%Creates task regressors with the time varying kernels as described in Churchland -[behavR, behavIdx] = log_makeDesignMatrix(behavEvents, behavEventTypes, opts); - -%2022-08-23: Not going to do it now, but if needed/desired could make a -%loop here to put info from behavIdx into Reg_mapping. For now skipping -%because I don't think we need it. - - -%% Setup Design Matrix - Movement - -%Add later this week and take code from previous attempts. For now focus -%on getting GLM loop working (stage 3). - -moveR = []; -moveIdx = []; - - -%% Combine Design Matrix; get mapping to groups - -%Combine Design Matrix -fullR=[behavR, moveR]; - -%Collect all indecies together. -regIdx =[behavIdx; moveIdx']; %check why moveIdx is transposed.......... - -%Collect all mappings together - -Reg_mapping = horzcat(Reg_mapping{:}); -% -% %2022-08-30: Very inelegant solution, but for the sake of keeping track of -% %how the regressors map to the variables keep looping through this as we -% %remove/change regIdx and updating or dropping variables as needed. This -% %first step puts all of the indices of the regIdx with their respective -% %variables before we remove any. Use all_vars and length of unique regIdx -% %here to make sure there are no issues at the beginning, then switch to -% %using the length of Reg_mapping as we may have to remove variables as we -% %go if all their regressors are removed. -% -% all_vars =unique(regIdx); -% for vars = 1:length(all_vars) -% -% Reg_mapping{3,vars} = find(regIdx == all_vars(vars)); -% -% -% end - - -disp('Design Matrix base Setup Done') - - - -%% Remove nans -%2022-08-24 even though not yet using the kinematics leaving this in to -%make sure there aren't any issues. - -nan_per = sum(nan_inds)/size(fullR,1) * 100; %Amount of session lost, note nan_inds is calculated when kinematics are loaded - -fullR(nan_inds,:) = []; - -for br =1:length(BRs) %will need to do this for each brain region at each step when changing the data. - - Results(br).Spikes(nan_inds,:) = []; - - if smooth_fr - - Results(br).fr(nan_inds,:) = []; %note this won't work with the current place holder code. - - end - -end - - -%2020-12-19 CT: Remove regressors with less than 10 events (as in -%Musall et. al. 2019) -%2022-08-24 ask Cam about this for the GLM. We do lose several regressors. - -low_events_idx= find(sum(fullR,1)<10); -fullR(:,low_events_idx)=[]; -regIdx(low_events_idx)=[]; - - - -%% Unclear if will do this yet. For now leave commented. - -%2022-08-29: Firing rate: switch to taking out -%mean. Don't standardize - -% % % Center and Standardize Continuous Data -% % Center both analog (i.e. movement/tracking data) and neural data. -% % -% % New approach: Find columns that don't just contain zero or one values: -% % [~, columns]=find(fullR~=0 & fullR~=1); -% % analoginds = unique(columns); clear columns -% % -% % standarize analog regressors -% % fullR(:,analoginds) = (fullR(:,analoginds)- mean(fullR(:,analoginds),1))./std(fullR(:,analoginds)); -% % - -if smooth_fr - for br = 1:length(BRs) - - Results(br).fr = Results(br).fr - mean(Results(br).fr,1); - - end -end - -%% Stage 3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%% Run QR check on full design matrix. Save copy of matrix without any changes - -%Save an unaltered copy -fullR_hold = fullR; -regIdx_hold = regIdx; - -[rejIdx, median_val, min_val, max_val,] = log_run_QR(fullR, 1); %Have this spit out all of the numbers for the first round -%Then surpress plotting and extra numbers for the subsequent rounds -%Recall values of zero mean things are co-linear/redunant according ot the -%QR check. -%2022-08-28: looks like we remove about 30 regressors in the current set up - -%2022-08-29: Need to fix this part - -%2022-08-30: Updated above so there are no values in row three. Set it as -%blank for all variables then fill in the regressors they have in the -%design matrix after the nan check for low events. Fill in a forth row -%with the regressors indices after the QR check for the full matrix. Keep -%adding rows for each experiment after the QR check and then make a table -%at the end so things are clear. - -%2022-08-30 This works and leaves empty vectors for the variables that are -%gone from the low event check. Saving empty vars in rows based on when -%they are taken out for diagnostic purposes right now. - -all_vars =unique(regIdx); -for vars = 1:length(all_vars) - - Reg_mapping{3,all_vars(vars)} = find(regIdx == all_vars(vars)); - - - -end - -Empty_vars(1,:) = cellfun(@isempty, Reg_mapping(3,:)); - -%Remove regressors rejected by the QR check -fullR(:,rejIdx) = []; -regIdx(rejIdx) = []; - -all_vars =unique(regIdx); -for vars = 1:length(all_vars) - - Reg_mapping{4,all_vars(vars)} = find(regIdx == all_vars(vars)); - - - -end - -Empty_vars(2,:) = cellfun(@isempty, Reg_mapping(4,:)); - -%Update 2022-08-30: Great, this all works and Reg_mapping as the intended -%information. Now we can grab regressors by variable name and picking the -%correct row post QR check. - -%% Run first loop (main model for each brain region) -%2022-08-28 Update: First pass works, roughly 1/3 of the neurons throw -%warnings when fit though. May need to use more advanced fitting algorithm -%but for now keep this due it convenience of getting all of the stats out -%in one go. Now need to work on cross validation loop. - -%Repeat whatever is to be done for each brain region -%Update 2022-08-29 getting errors that design matrix is rank deficient -%which I think is do to getting some empty regressors for particular splits -%of the data. Not sure exactly what to do about this other than try -%different number of folds or introduce some cyclical resampling that -%checks to make sure each fold is full rank...actually doesn't look like it -%is empty regressors but actually linearly dependent regressors with the -%subsampling from the cross validation. This is a pain. Can still try -%resampling idea, but not positive that will work or if there is a way to -%guarantee that subsampling a full rank matrix will yield yet another full -%rank matrix. - - -kfolds = 10; -for br = 1:2%length(BRs) For now not running all as this seems silly - - Y = Results(br).Spikes; - - if smooth_fr %Handle this before we get here, just swap the data depending on what needs to be used. - - Y = Results(br).fr; - - end - - Results(br).Data{1} = fullR; %make a cell in the data field of each struct that hold the design matrix used for that analysis - - X = fullR; - - %2022-08-29 for now putting in as fields under Results(br).model - %2022-08-31 (late night of 08-30 actually) trying regulariztion glm - %fit with lassoglm - [Y_hat,Betas,CIs,R2_per,LL_per,pvalues,pn] = GLM_CrossVal(X,Y,kfolds,smooth_fr); - - Results(br).model(1).name = 'Full'; - - Results(br).model(1).Y_hat = Y_hat; %Estimated spike rate for all neurons - Results(br).model(1).Betas = Betas; %Beta values for each neuron (avg over cv) - Results(br).model(1).CIs = CIs; %Confidence intervals for each beta for each neuron (avg over cv) - Results(br).model(1).R2_per = R2_per; %Adjusted R^2 for each neuron (avg over cv) - Results(br).model(1).LL_per = LL_per; %Loglikelihood for the fit of each neuron (avg over cv) - Results(br).model(1).pvals = pvalues; %pvalue for each regressor for each neuron (currently average over cv but will switch to fisher's method ASAP) - Results(br).model(1).pn = pn; %Bool vector with true values for neurons that throw a warning during the fit at any point. - - end - - - - -%% Set up shuffling sets (i.e. what models we want to test) - -%2022-08-29: For now only have these to shuffle. Can add/remove as needed - -model_names = {'Full','Subj_shuff', 'Part_shuff', 'Context_shuff', 'all_shuffled'}; %Full is a place holder from above; - -%2022-08-30: Create lists of variable names to be shuffled in each "model" -%/analysis. For now just do this manually. Can set up something clever -%later if needed...Added shuffling everything as a null model/sanity check - -shuffling_vars = cell(length(model_names),1); - -%Since we are shuffling by variable group -%Take subsections of the named variables based on the individual X_groups we made earlier - -shuffling_vars{2} = {Reg_mapping{1,1:size(X_groups{1},2)}}; %Subject is always first group - -shiftamt = length(shuffling_vars{2}); - -shuffling_vars{3} = {Reg_mapping{1,shiftamt+1:... %Partner is always second group, so index limits are just shifted by variables in first group - shiftamt+size(X_groups{2},2)}}; - -shiftamt = shiftamt+length(shuffling_vars{3}); - -shuffling_vars{4} = {Reg_mapping{1,shiftamt+1:... - shiftamt+size(X_groups{3},2)}}; - -shuffling_vars{5} = {Reg_mapping{1,:}}; - -%% Make shuffled design matrices, rerun QR check -%2022-08-30 Make sure partner and subject aren't accidentally the same. -%Double checked and they are not exactly the same and think initial weird -%result may be due to the overfitting from not really cross validating. -%To be safe though making sure to shuffle the rng so they are more -%different (corr2 shows they are .6 correlated in last run so that also -%could be an issue) - -X_shuff = cell(1,length(model_names)); -regIdx_shuff = cell(1,length(model_names)); - -for shuff = 2:length(model_names) - %Get all regressors to shuffle - rng('shuffle') %Just because getting odd results with partner and subject looking similar - shuffle_inds = []; - - for var = 1:length(shuffling_vars{shuff}) %If variable is empty it won't be addeed to shuffle_ind as we are just concatenating an empty array - - var_ind = find(ismember(Reg_mapping(1,:), shuffling_vars{shuff}(var))); - - shuffle_inds = [shuffle_inds Reg_mapping{3,var}']; - - end - - %Do shuffle and save into this cell array, then perform QR check - X_temp = fullR_hold; %First put in pre QR check matrix - X_temp(:,shuffle_inds) = X_temp(randperm(size(fullR_hold,1)),shuffle_inds); %Change the shuffled inds to the shuffled version - regIdx_temp = regIdx_hold; - - [rejIdx, ~, ~, ~,] = log_run_QR(X_temp, 0); %No plotting this time so we aren't bombarded with figures. - - %Remove regressors rejected by the QR check - X_temp(:,rejIdx) = []; - regIdx_temp(rejIdx) = []; - - X_shuff{shuff} = X_temp; - regIdx_shuff{shuff} = regIdx_temp; - - %Again loop through all of variables to update Reg_mapping - - Reg_map_ind = size(Reg_mapping,1)+1; %Add a new row each time. - all_vars =unique(regIdx_shuff{shuff}); - - for vars = 1:length(all_vars) - - Reg_mapping{Reg_map_ind,all_vars(vars)} = find(regIdx_shuff{shuff} == all_vars(vars)); - - - - end - - - - -end - -%% Run second loop: Shuffled analyses - -for br = 1:length(BRs)-1 %don't do all - - Y = Results(br).Spikes; - - if smooth_fr %Handle this before we get here, just swap the data depending on what needs to be used. - - Y = Results(br).fr; - - end - - - for exper = 2:length(model_names) - fprintf(['Working on model #' num2str(exper) '\n']) - - X = X_shuff{exper}; - - - Results(br).Data{exper} = X; - - %2022-08-29 for now putting in as fields under Results(br).model - [Y_hat,Betas,CIs,R2_per,LL_per,pvalues,pn] = GLM_CrossVal(X,Y,kfolds,smooth_fr); - - Results(br).model(exper).name = model_names{exper}; - - Results(br).model(exper).Y_hat = Y_hat; %Estimated spike rate for all neurons - Results(br).model(exper).Betas = Betas; %Beta values for each neuron (avg over cv) - Results(br).model(exper).CIs = CIs; %Confidence intervals for each beta for each neuron (avg over cv) - Results(br).model(exper).R2_per = R2_per; %Adjusted R^2 for each neuron (avg over cv) - Results(br).model(exper).LL_per = LL_per; %Loglikelihood for the fit of each neuron (avg over cv) - Results(br).model(exper).pvals = pvalues; %pvalue for each regressor for each neuron (currently average over cv but will switch to fisher's method ASAP) - Results(br).model(exper).pn = pn; %Bool vector with true values for neurons that throw a warning during the fit at any point. - - - - end - - - -end - - - -%% Get Overall Statistics - -%2022-08-30: Think this is again a case of overfitting but just to note how -%bad this is, even the shuffled data - which does tank the distribution of -%well fit neurons some how manages to have an okay overall R^2 in this -%analayis. So either this analysis is being implemented wrong or the power -%of overfitting is even stronger than I thought it was. - -%For now just calculate R^2 as we did for T and T 2022 -%I think we need to adjust the R^2 for the varying amount of regressors -%(parameters) between the models, but add this in a bit. - -%Get distribution of p values for each variable as well - - -%Turn Reg_mapping into a table for ease of use later on -%NOTE******FOR NOW NEED TO MANUALLY EDIT NAMES OF COLUMNS FOR EXPERIMENTS** -%Later figure out how to automate this. -Reg_table = cell2table(Reg_mapping','VariableNames',{'Variable Name', 'Observation Label','Reg preQR', 'Reg in Fullmdl',... - 'Reg in SubShuff','Reg in PartShuff','Reg in ContShuff', 'Reg in AllShuff'}); - - -%For convenience of getting regressor values for full matrix in this loop -X_shuff{1} = fullR; %Need that info to due adjusted R^2 or other things that require knowing the degrees of freedom. - -for br = 1:length(BRs)-1 %Not doing all for now - - for md = 1:length(model_names) - %First get overall R2 between data and prediction. - - if smooth_fr - Results(br).model(md).R2_overall = corr2(Results(br).fr,Results(br).model(md).Y_hat).^2; %Add subtraction of observations over regressor numbers (using - else - Results(br).model(md).R2_overall = corr2(Results(br).Spikes,Results(br).model(md).Y_hat).^2; - end - - %Next get p_values organized by variable - - end - -end - - - -%% Do cross model comparisons -%Again for now just doing what we did in T and T 2022: delta R^2 -%Putting this in stats in first branch of Results as this is a comparison -%across models - -for br=1:length(BRs)-1 - - for diff = 2:length(model_names) - %just doing this for now to see the results quickly at the end - temp = Results(br).model(1).R2_overall - Results(br).model(diff).R2_overall; - Results(br).Stats(diff).deltaR2 = temp; - - end - -end - - - -%% Save final Results -%save(['YOUR PLACE TO SAVE COULD BE HERE!!!!!'],'Results', 'Reg_table') -end %Session loop end - - - - - - - - - - - -%% Old code sections -%% 1 Runs 1st loop with cross validation - -%2022-08-29: Commenting out for now so I can work on the function to do this I wrote -% %2022-08-28 %First just fit the model to make sure that runs/see how long it takes -% %Then set up cross validation loop: 10 Folds, 80-20 split -% %Update: doesn't take super long to run per neuron but with cross -% %validation could take a bit to run all the analyses for each brain -% %region for each session. -% -% %2022-08-29 Update: I think the cross validation will make the Stats -% %objects from the mdl much less useful as many of those can simply be -% %averaged over...maybe come up with something clever later but for -% %now take "average" Y-hat (i.e. take the prediction from all of the -% %separate 20% of the data) and take average LL as these for sure can be -% %used...further I think it will be best to make this into a function -% %that does the cross validation and fitting and spits out the needed -% %info at the end that can be loaded into the Results struct. -% -% problem_neuron = zeros(1,size(Y,2)); %Save which neurons had a warning thrown. -% -% for n = 1:size(Y,2) %For each neuron, fit a glm -% display(['Working on neuron #' num2str(n)]) -% -% %2022-08-28: Added ability to save any warnings thrown by fitglm -% -% if smooth_fr -% lastwarn('','') %reset warning. -% mdl = fitglm(X,Y(:,n),'linear','Distribution','gaussian'); %If smoothing happens use gaussian instead of poisson -% -% test1 = lastwarn; -% if ~isempty(test1) -% -% problem_neuron(1,n) = 1; %change the zero to a 1 if a warning is found -% -% end -% else -% lastwarn('','') %reset warning. -% mdl = fitglm(X,Y(:,n),'linear','Distribution','poisson'); -% -% test1 = lastwarn; -% if ~isempty(test1) -% -% problem_neuron(1,n) = 1; %change the zero to a 1 if a warning is found -% end -% end -% -% Results(br).Stats.perneuron{1, n} = mdl; -% -% Y_hat = mdl.feval(X); -% -% %After cross validation put each cross validated guess together and -% %save ad the final Y_hat in Results -% -% Results(br).Y_hat{1,n} = Y_hat; -% -% -% end -% Results(br).Warnings{1} = problem_neuron; -%% 2 that helps remove nans and unindentified behaviors from before - -% %% Step 1.4 Remove Nans -% -% %Remove Nans -% %nan_per = length(nan_inds)/length(labels) * 100; %Amount of session lost -% %to Nans -% kinematics(nan_inds,:) = []; -% behavior_labels_subject_init(nan_inds) = []; -% behavior_labels_partner_init(nan_inds) = []; -% -% for br =1:length(BRs) %will need to do this for each brain region at each step when changing the data. -% -% Results(br).Spikes(nan_inds,:) = []; -% -% if smooth_fr -% -% Results(br).fr(nan_inds,:) = []; %note this won't work with the current place holder code. -% -% end -% -% end -% -% %% Step 1.5 Remove moments without an identified behavior -% -% -% Behav_inds = zeros(size(behavior_labels_subject_init)); -% -% for i = 1:length(behavior_labels_subject_init) %Choose which behavior set you want to consider -% -% temp = [(behavior_labels_subject_init{i,:} ~= length(behav_categ))... -% (behavior_labels_partner_init{i,:} ~= length(behav_categ))]; -% -% Behav_inds(i) = sum(temp); %If ANY behavior is not the rest behavior keep the bin -% -% end -% -% Behav_inds = Behav_inds > 0; %If ANY behavior is not the rest behavior keep the bin -% -% %Keeping with above code for nans remove any inds at are NOT Behav_inds -% -% kinematics(~Behav_inds,:) = []; -% behavior_labels_subject_init(~Behav_inds) = []; -% behavior_labels_partner_init(~Behav_inds) = []; -% -% -% for br =1:length(BRs) %will need to do this for each brain region at each step when changing the data. -% -% Results(br).Spikes(~Behav_inds,:) = []; -% -% if smooth_fr -% -% Results(br).fr(~Behav_inds,:) = []; %note this won't work with the current place holder code. -% -% end -% -% end - - -%% First try at Stage 2 making the design matrix. -% %% Step 2.0 Aggregate variable groups; pass each group through function for making regressors for each variable in each group -% -% %From notes have four variable groups: subject behavior, partner behavior, kinemetics, contex/other -% %Just focusing on first 3 now. -% Var_groups = cell(1,3); -% Reg_groups = cell(2,3); %Save the output of making the design matrix for each variabel group. Concatenate later to make X_overall -% -% Var_groups{1} = behavior_labels_subject_init; -% Var_groups{2} = behavior_labels_partner_init; -% Var_groups{3} = kinematics; -% %Var_groups{4} = []; %For now not doing this but will be block id or -% %something equivalent later on for contex -% -% -% %2022-08-22 update: really need to review T and T 2022 to figure out how to -% %keep regressor inds grouped with each variable within each variable -% %group...This will be the work of this afternoon/evening and tomorrow. -% -% -% for group = 1:length(Var_groups) %going to have to think about this... -% %I think for all of them need regressor for current state/raw trace in -% %case of kinematics. Then for the first three need delay matrix, but -% %need some way to trigger that to get events first for kinematics... -% %Probably just do a data check where if variables in that group have -% %non-integer values then we treat it as kinematics and first get events -% %based on being a certain amount above a std based threshold -% -% [X_curr] = makeDesignmat_current(Var_groups(groups)); %2022-08-22 need to still write this function...borrow heavily from T and T 2022 -% -% var_inds = size(X_curr,2); %Get the number of variables in that var group -% %Need this for assigning delay regressors to correct variable group -% %when we calculate the delay regressors. -% -% if group ~=4 %Context doesn't get delay matrix -% -% [X_delays, reg_inds] = makeDesignmat_delay(Var_groups(groups)); %2022-08-22 need to still write this function...borrow heavily from T and T 2022 -% -% %Some fun math here to get the regressor indicies that map to each -% %variable in each variable group. -% -% var_inds = 'something clever' -% -% end -% -% Reg_groups{1,group} = [X_curr X_delays]; -% -% Reg_groups{2,group} = var_inds; %Save -% -% end - - - diff --git a/Varia/GLM/old_code/GLM_Pipeline_Try1_CT.m b/Varia/GLM/old_code/GLM_Pipeline_Try1_CT.m deleted file mode 100644 index f5c4ff5..0000000 --- a/Varia/GLM/old_code/GLM_Pipeline_Try1_CT.m +++ /dev/null @@ -1,1048 +0,0 @@ -%% Running Notes -%Start 2022-08-22 -%Making this separate script to work on the preprocessing steps (stage 1) -%and design matrix creation steps (stage 2) for the data. Separating -%things out to help prevent the massive script that was the end result of -%Tremblay and Testard 2022. - -%Will make cell of this script into function calls whenever possible - -%Also creating another script for stage 3 which will actually do the GLM -%analyses and be slotted into the outer loop from this script. - -%Again, main focus is having a MUCH cleaner end result than we had with T -%and T 2022. - -%2022-08-23 -%Focus for now on getting design matrix with state-events (what I am calling -%things like subject behavior, partner behavior, context) working and then -%will come back to get that working with kinematics as not clear when -%exactly we will have kinematics and doesn't seem like we will have them in -%time for Parma...have to see. For now need to get this to work while -%keeping track of regressors then switch to getting GLM loop to work. - -%2022-08-28: Note that need to have ridgeregression folder in this repo on -%the path for some of the functions needed to make the design matrix. -%Will also need for qr checks. We can add a line to auto add this to the -%path later if needed. - -%2022-08-29 Use smoothed firing rate instead of spiking; adjust -%accordingly. Use the new load in function from Camille and use is_mac to -%toggle file path. - -%2022-08-30 Note: Due to difference in the QR check we WILL have different -%number of regressors for different models; so all comparisons will have -%to have that adjustment. Not a deal breaker but important to note. - -%2022-08-30 Another important update: without the cross validation the -%fitting of the data is absurdly high (assuredly because we are -%overfitting) so need to fix this to get meaningful results and don't read -%into the shuffling not having a huge effect yet. -%% Stage 1 Get data loaded in and set up loops - - -%% Step 1.0: Set filenames, Set loop for sessions - -%Update notes: Make these straightfoward to change to interact with later -%functions - -%Parameters for setting Path -is_mac = 1; %For loading the data %2022-08-29 Use this to replace is_ron -is_ron = 0; %For setting path - - - -%Parameters for setting sessions to use and brain regions to look at -S_list = [1]; %List of session to pull; %For now focus on first Amost sessions -BRs = ["TEO", "vlPFC", "all"]; %Channels considered (sets brain region TEO vlPFC or all) - - - - -%Parameters for neural data to be used across sessions -with_NC =1; %0: Noise cluster (NC) is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well-isolated units -temp_resolution = 1; %Temporal resolution of firing rate. 1sec %%Note this will change if we introduce smoothing -smooth_fr = 1; %Toggle to use smooth spike rasters (currently not setting up this code as may require working with ms neural data and then downsampling to get back) -smooth= 0; % 1: smooth the data; 0: do not smooth -sigma = 1;%set the smoothing window size (sigma) - -s=1; -for s = S_list %Do analyses for each session put in S_list - - %Set path - if is_ron - - sessions = dir('C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_data'); sessions = sessions(3:end,:); - filePath = ['C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_data\' sessions(s).name]; - savePath = ['C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_results']; - - else - - - %Set session list - home = '~'; % set home directory - cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) - sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/partner_vs_subject']; - end - - %% Step 1.1 Preallocate Results structure; save spikes for each brain region; performing smoothing and save results if smoothing. - - clear Results %make sure Results is cleared before each session is ran - %Preallocate one branch of Results for each BR used. Save one results file for each session. - %Put data from each brain region into appropriate Results path. - %Only Spike_raster should change so just keep the behavior and the rest - %from the last BR load in. - for br = 1:length(BRs) - - Results(br).BR = BRs(br); - channel_flag = Results(br).BR; - %Get data with specified temporal resolution and channels - %2022-08-23 note: have to change this to new function when Camille - %is done with that and rego through everything again to make sure - %there isn't a change. - - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - % Output: - % 1. Spike_rasters: n_neuron X time in seconds (if temp_resolution = 1) - % 2. labels: time X 11 - check the function description for - % details. Essentially subject behavior is column 3. - % 3. labels_partner: time X 11. Partner behavior is column 3. - % 4. behav_categ: string vector; behavioral category do the numbers in - % labels correspond to. - % 5. block_times: specifies the block times and ID - % 6. monkey: subjecet monkey ID - % 7. reciprocal_set: set of behaviors that are deeemed "reciprocal" - % 8. social_set: set of behaviors that are deeemed "social" - % 9. ME_final: Motion energy values - % 10. unit_count: number of units in the session - % 11. groom_labels_all: labels for grooming. Check function - % description for details - % 12. brain_label = specify to which brain area each label - % belongs to - Results(br).Spikes = Spike_rasters'; - - if smooth_fr - - %2022-08-29 temp solution is to just copy and paste smoothing - %code here. I'm not exactly sure which of the loading - %functions is the right one and need to review again what the - %differences are. For now do this and deal with the difference - %later tonight after sure the rest of the code runs. - sigma = 5; - gauss_range = -3*sigma:3*sigma; %calculate 3 stds out, use same resolution for convenience - smoothing_kernel = normpdf(gauss_range,0,sigma); %Set up Gaussian kernel - smoothing_kernel = smoothing_kernel/sum(smoothing_kernel); - smoothing_kernel = smoothing_kernel * 1; %Rescale to get correct firing rate - Spike_rasters_smooth = conv2(Spike_rasters, smoothing_kernel,'same'); - Spike_rasters = Spike_rasters_smooth; - Results(br).fr = Spike_rasters_smooth'; - - end - - end %End brain region loop. Should now have neural activity saved for each brain region and them together in Results - - %% Step 1.2 Get (fake) kinematics data; Get Nans - - %Later this will be the function call to get the kinematics data if that - %isn't included in the above load in code. For now just make to random - %gaussian sets and throw some random NaNs in their to work on the NaN - %clearing code (mostly borrowed from T and T 2022); - - fake_x = normrnd(0,1,size(labels,1),1); - fake_y = normrnd(0,1,size(labels,1),1); - - nan_per = 0; %Set percent (as decimal) of nans each individual fake regressor gets - fake_x(randsample(length(fake_x),round(nan_per*length(fake_x))),1) = NaN; - fake_y(randsample(length(fake_y),round(nan_per*length(fake_y))),1) = NaN; - - kinematics = [fake_x fake_y]; - - nan_counts = sum(isnan(kinematics),2); - - % figure() - % histogram(nan_counts) %Shows number of nans and their overlap (all numbers greater than 1) - % - - %If needed put code for interpolating over short runs of nans from T and T - %2022 here. For now, leaving out. - - nan_inds = nan_counts > 0; %Naturally anything that has a count greater than 0 is a time point that has an NaN - - - %% Step 1.3 Set Proximity to non-identified behavior - %Update 2022-08-30 switching to only considering 1 behavior happening at - %any moment to see if this resolves rank issues in our design matrix during - %cross validation. - - % IMPORTANT NOTE: we're only getting the unique behaviors for now - % behaviors that happen simultaneously are not included) - - behavior_labels_subject = labels(:,3); %Extract all behavior info for subject - behavior_labels_partner = labels_partner(:,3); %Extract all behavior info for partner - - %CT Note: keeping proximity for now -% % Set proximity as rest -% behavior_labels_subject(behavior_labels_subject==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). -% behavior_labels_partner(behavior_labels_partner==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - - %Temporary joining of those cells so I can easily check subject and partner behavior - Behavs = [behavior_labels_subject behavior_labels_partner]; - - %End stage 1: Now have data we will use to make design matrix in stage 2 - %and the spiking data in the form it will be used for Stage 3 - - %% Stage 2 Create regressors - - %TERMINOLOGY NOTES: - %-Variable group = family of variables put together due to belong to the - %same type of observation. Currently have: subject behavior, partner - %behavior, context, and kinematics - - %-Variable = A single state that can be observed or key point that can be - %measured. For example: grooming, agression, block id, arm_x position. - - %-Regressor = A column of the design matrix. Each variable can have - %multiple regressors. Examples: grooming could have a regressor for - %particular delays before or after the onset/offset of grooming in addition - %to having a regressor for stating whenever the animal is currently - %grooming. arm_x could have both a tracking of the position of the arm as - %well as a regressor for arm events when the arm is moved above a - %particular threshold of movement. - - %-Regressor group = Would refer to groups of - %regressors across several variables put together for a particular analysis - %that is different from the Variable group. Examples: all regressor - %relating to head and eye movement in kinematics may be grouped together in - %an analysis that focuses on a subset a kinematics and could be call the - %head-eye regressor group. All behaviors deemed affiliative by either the - %subject or partner could be grouped together for an affiliative regressor - %group. - - %State-events refer to all non-kinematic, binary regressors. - - %update 2022-08-23 trying to see to what extent code Camille already wrote - %needs to be changed. I don't love doing it this way as - %this is again goes back to having a massive script, but going to use it as - %a starting point and then change it as needed... - %This is really super janky and messy so probably just rewrite things so - %there are consistent coding conventions throughout. - - %% Step 2.0 Set options for time event kernels - - %set up sampling rate (not sure this is useful, but it's used in code further down so i'll keep it for now). - opts.Fs = temp_resolution; - - %Set up windows for time varying kernels. Multiplying factor is in seconds - %(recall FS is samples/second ),so can adjust window accordingly - - %2022-08-23: need to check this is still correct. - - opts.mPreTime = round(2 * opts.Fs); %motor pre time - opts.mPostTime = round(2 * opts.Fs); %motor post time - motorIdx = [-(opts.mPreTime: -1 : 1) 0 (1:opts.mPostTime)]; %index for design matrix to cover pre- and post motor action - - - - %% Step 2.1 Get labels for each state-event variable group - - %2022-08-23 rewrote this and borrowed from first GLM attempt because - %previous code does not work. - - %First get labels for all subject, partner, and context variables so we - %know how many variables we have to start with - - %Update 2022-08-29 need to remove proximity here! - - prox_ind = find(behav_categ == 'Proximity'); - - - %Observations x behavior label - obs_statevents_subject = unique(cell2mat(Behavs(:,1))); - %Need to make a column for each behavior that happens in the session. - %Above code does assume there is no behavior that only occurs paired - %with another behavior and that was removed when only one behavior per - %moment was prioritized. I think this is a reasonable assumption, so - %will go forward with it. - - %For now just repeat. Later have this be either structure or a cell - %and have a function that adds to it as needed. - - -% % % obs_statevents_subject( obs_statevents_subject == prox_ind) = []; %Remove proximity as an observed event - - obs_statevents_partner = unique(cell2mat(Behavs(:,2))); - - %CT Note: for now we're removing regressors that are causing issues of - %co-linearity, we might not need to do this once regularization is set up. - obs_statevents_partner = setdiff(obs_statevents_partner, reciprocal_set); - - %Update 2022-08-30 removing partner groom other as this is redundant with - %getting groomed for the subject...okay know partner drinking is causing - %the issue. Again just try removing these for now so we can at least run - %the cross validation without the delays (maybe with if this is the source - %of all the issues)...it feels like this problem just keeps cropping up - %with new behaviors each time I eliminate one, but curious to see how far - %this goes, particularly since they are all partner behaviors...okay now I - %am just done as it selected rest which seems like it should definitely not - %be redundant with any behaviors...on second thought. Let's also just - %change the beginning so there is only one behavior per moment as that - %might fix a lot of this too...This did not fix the issue and there still - %are problems on some runs. - %Update: below combination looks like it consistently works at least when - %there are no delays. Trying delays now...delays still does not work. - %Okay. Let's see it run with the delays taken out so we at least have that - %and then I can go in and try the lassoglm function and if that fails/isn't good try - %to pull from the regularization function Pillow has. - - - - - %...this also speaks to need to add regularization to the regression in the hopes that should resolve the rank - %collapsing issues for the design matrix...I think this demands just using the - %regularization to deal with this issue even if that means having to go - %back in and change the code/how we get the results out. - - pgp_ind = find(behav_categ == "Groom partner"); - pgg_ind = find(behav_categ == "Getting groomed"); - pfor_ind = find(behav_categ == "Foraging"); - pdr_ind = find(behav_categ =="Drinking"); - papp_ind = find(behav_categ =="Approach"); - pagg_ind = find(behav_categ == "Aggression"); - pttp_ind = find(behav_categ=="Threat to partner"); - - obs_statevents_partner( obs_statevents_partner == prox_ind) = []; %Remove proximity as an observed event - obs_statevents_partner(obs_statevents_partner == pgp_ind) = []; %Remove partner giving grooming as this is redundant with subject receiving. - obs_statevents_partner(obs_statevents_partner == pgg_ind) = []; %Remove because vice versa for getting groomed - - %Removing all below due to rank issues. - obs_statevents_partner(obs_statevents_partner == pfor_ind) = []; - obs_statevents_partner(obs_statevents_partner == pdr_ind) = []; - % obs_statevents_partner(obs_statevents_partner == papp_ind) = []; - obs_statevents_partner(obs_statevents_partner == pagg_ind) = []; - obs_statevents_partner(obs_statevents_partner == pttp_ind) = []; - - obs_context = 1:size(block_times,1); %For now just considering the three different blocks for context. - - obs_statevents = [{obs_statevents_subject}, {obs_statevents_partner} ,{obs_context}]; %Gives number of varibles in each group of states events considered - - % - %% Step 2.2 Create base regressors for each state-event variable and regressor mapping - - %For first try trying to save each variable group into it's own cell in - %X_groups with all of the information before we combine or add additional - %regressors for time event kernel - - %Update 2022-08-23 Reg_mapping may not work as inteded on on second thought but - %leaving there for now. - - %+1 for kinematics - X_groups = cell(1,length(obs_statevents)+1); - % Design matrix for the 4 variable groups: Subject; Partner; Context; Kinematics - - Reg_mapping = cell(1,length(obs_statevents)+1); - - for this_mat = 1:size(Behavs,2) %Currently this loop only works for the behaviors. Will need to write a separate and exteneded loop for Context - - X_groups{this_mat} = zeros(length(Spike_rasters), length(obs_statevents{this_mat})); - - %Note, behavior numeric labels will be larger than this matrix (e.g. - %there are 22 behaviors but one of the labels is 29.) Thus need to - %create/save mapping between numeric label in behavior_labels and the - %column of the design matrix. - - %First row is behavior label, 2nd row is original numeric label, update - %2022-08-30 don't add 3 row for now. This will be index in matrix - %after nan check then will add subsequent rows which will hold the - %indecies for the regressors associated with each variable in each - %experiment after the QR check. Also at the end going to make - %Reg_mapping a table so it is easier to navigate. - - - Reg_mapping{this_mat} = cell(2,length(obs_statevents{this_mat})); - - - for this_label = 1:length(obs_statevents{this_mat}) - - Reg_mapping{this_mat}{1,this_label} = behav_categ{obs_statevents{this_mat}(this_label)}; - - Reg_mapping{this_mat}{2,this_label} = obs_statevents{this_mat}(this_label); - - % Reg_mapping{this_mat}{3,this_label} = this_label; %Note will have to mess around with this if we introduce delays in regressors - % - % %Put ones into design matrix for indicated column whenever that behavior was present - % %Update: can't just do this now that we have the potential for - % %multiple behaviors at each time point. Instead for each behavior - % %will loop through the session and put a 1 in the design matrix - % %each time that behavior occurred. This is not efficient code, but - % %for now this is fine. Try to think of something clever later on. - - for i =1:length(Behavs) - X_groups{this_mat}(i,this_label) = sum(Behavs{i,this_mat}(:) == Reg_mapping{this_mat}{2,this_label}); %Sum is just there to convert back to double/account for multiple behaviors - %We are only checking one behavior at a time in this loop so it - %won't result in a value greater than 1. - end - - end - - - - - end %End loop for setting this up for behaviors - - %Do the same thing for context which is only block id for now. Can't - %be added easily to above loop or put into some general function since - %the information is not stored in that way. - - this_mat = this_mat+1; - - X_groups{this_mat} = zeros(length(Spike_rasters), length(obs_statevents{this_mat})); - - for this_label = 1:length(obs_statevents{this_mat}) - - Reg_mapping{this_mat}{1,this_label} = block_times.Behavior{this_label}; - - Reg_mapping{this_mat}{2,this_label} = obs_statevents{this_mat}(this_label); - - % Reg_mapping{this_mat}{3,this_label} = this_label; - - %Need to populate each block using the info in block_times table - - X_groups{this_mat}(block_times.start_time_round(this_label):... - block_times.end_time_round(this_label)... - ,this_label) = 1; %Put ones in the column for that block id based on block_times - - end - - Reg_mapping{2}(1,:) = append('partner.',Reg_mapping{2}(1,:)); %Add this now to prevent confusion later. - - %% Step 2.3 First concatenation, get names and regressors - - %Combine behavioral events - behavEvents=horzcat(X_groups{1:3}); - - %Get names via Reg_mapping - behavEventNames = horzcat(Reg_mapping{1}(1,:)... - ,Reg_mapping{2}(1,:),... - Reg_mapping{3}(1,:)); %2022-08-29: Cam added more info to block (gender pairing in not alone) - - - - %Set up event type here. - %Note: not creating delays for context variables for now. Need to see if - %this messes up the next part of the code. If so I guess make context - %"whole trial" and set something up in that code. Update going to add - %zeros to the end as this should maybe work and won't get indecies - %otherwise - - %2022-08-30 update: TEST************************************************ - %Not doing any delays to see how this interacts with the full rank problem - %CT note: to add delays change 0 to 3. - behavEventTypes= [ones(1,length(behavEventNames)-length(obs_context))*0 zeros(1,length(obs_context))]; - % For now I will assign all behavioral events as event type 3 - %Event Type 1 = Whole trial - %Event Type 2 = from stimulus onset to the rest of the trial - %Event Type 3 = from before movement onset to after movement onset (pre & post time) - - %% Setup Design Matrix - Behavioral Events - - - - %Creates task regressors with the time varying kernels as described in Churchland - [behavR, behavIdx] = log_makeDesignMatrix(behavEvents, behavEventTypes, opts); - - %2022-08-23: Not going to do it now, but if needed/desired could make a - %loop here to put info from behavIdx into Reg_mapping. For now skipping - %because I don't think we need it. - - - %% Setup Design Matrix - Movement - - %Add later this week and take code from previous attempts. For now focus - %on getting GLM loop working (stage 3). - - moveR = []; - moveIdx = []; - - - %% Combine Design Matrix; get mapping to groups - - %Combine Design Matrix - fullR=[behavR, moveR]; - - %Collect all indecies together. - regIdx =[behavIdx; moveIdx']; %check why moveIdx is transposed.......... - - %Collect all mappings together - - Reg_mapping = horzcat(Reg_mapping{:}); - % - % %2022-08-30: Very inelegant solution, but for the sake of keeping track of - % %how the regressors map to the variables keep looping through this as we - % %remove/change regIdx and updating or dropping variables as needed. This - % %first step puts all of the indices of the regIdx with their respective - % %variables before we remove any. Use all_vars and length of unique regIdx - % %here to make sure there are no issues at the beginning, then switch to - % %using the length of Reg_mapping as we may have to remove variables as we - % %go if all their regressors are removed. - % - % all_vars =unique(regIdx); - % for vars = 1:length(all_vars) - % - % Reg_mapping{3,vars} = find(regIdx == all_vars(vars)); - % - % - % end - - - disp('Design Matrix base Setup Done') - - - - %% Remove nans - %2022-08-24 even though not yet using the kinematics leaving this in to - %make sure there aren't any issues. - - nan_per = sum(nan_inds)/size(fullR,1) * 100; %Amount of session lost, note nan_inds is calculated when kinematics are loaded - - fullR(nan_inds,:) = []; - - for br =1:length(BRs) %will need to do this for each brain region at each step when changing the data. - - Results(br).Spikes(nan_inds,:) = []; - - if smooth_fr - - Results(br).fr(nan_inds,:) = []; %note this won't work with the current place holder code. - - end - - end - - - %2020-12-19 CT: Remove regressors with less than 10 events (as in - %Musall et. al. 2019) - %2022-08-24 ask Cam about this for the GLM. We do lose several regressors. - - low_events_idx= find(sum(fullR,1)<10); - fullR(:,low_events_idx)=[]; - regIdx(low_events_idx)=[]; - - - - %% Unclear if will do this yet. For now leave commented. - - %2022-08-29: Firing rate: switch to taking out - %mean. Don't standardize - - % % % Center and Standardize Continuous Data - % % Center both analog (i.e. movement/tracking data) and neural data. - % % - % % New approach: Find columns that don't just contain zero or one values: - % % [~, columns]=find(fullR~=0 & fullR~=1); - % % analoginds = unique(columns); clear columns - % % - % % standarize analog regressors - % % fullR(:,analoginds) = (fullR(:,analoginds)- mean(fullR(:,analoginds),1))./std(fullR(:,analoginds)); - % % - - if smooth_fr - for br = 1:length(BRs) - - Results(br).fr = Results(br).fr - mean(Results(br).fr,1); - - end - end - - %% Stage 3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - %% Run QR check on full design matrix. Save copy of matrix without any changes - - %Save an unaltered copy - fullR_hold = fullR; - regIdx_hold = regIdx; - - [rejIdx, median_val, min_val, max_val,] = log_run_QR(fullR, 1); %Have this spit out all of the numbers for the first round - %Then surpress plotting and extra numbers for the subsequent rounds - %Recall values of zero mean things are co-linear/redunant according ot the - %QR check. - %2022-08-28: looks like we remove about 30 regressors in the current set up - - %2022-08-29: Need to fix this part - - %2022-08-30: Updated above so there are no values in row three. Set it as - %blank for all variables then fill in the regressors they have in the - %design matrix after the nan check for low events. Fill in a forth row - %with the regressors indices after the QR check for the full matrix. Keep - %adding rows for each experiment after the QR check and then make a table - %at the end so things are clear. - - %2022-08-30 This works and leaves empty vectors for the variables that are - %gone from the low event check. Saving empty vars in rows based on when - %they are taken out for diagnostic purposes right now. - - all_vars =unique(regIdx); - for vars = 1:length(all_vars) - - Reg_mapping{3,all_vars(vars)} = find(regIdx == all_vars(vars)); - - - - end - - Empty_vars(1,:) = cellfun(@isempty, Reg_mapping(3,:)); - - %Remove regressors rejected by the QR check - fullR(:,rejIdx) = []; - regIdx(rejIdx) = []; - - all_vars =unique(regIdx); - for vars = 1:length(all_vars) - - Reg_mapping{4,all_vars(vars)} = find(regIdx == all_vars(vars)); - - - - end - - Empty_vars(2,:) = cellfun(@isempty, Reg_mapping(4,:)); - - %Update 2022-08-30: Great, this all works and Reg_mapping as the intended - %information. Now we can grab regressors by variable name and picking the - %correct row post QR check. - - %% Run first loop (main model for each brain region) - %2022-08-28 Update: First pass works, roughly 1/3 of the neurons throw - %warnings when fit though. May need to use more advanced fitting algorithm - %but for now keep this due it convenience of getting all of the stats out - %in one go. Now need to work on cross validation loop. - - %Repeat whatever is to be done for each brain region - %Update 2022-08-29 getting errors that design matrix is rank deficient - %which I think is do to getting some empty regressors for particular splits - %of the data. Not sure exactly what to do about this other than try - %different number of folds or introduce some cyclical resampling that - %checks to make sure each fold is full rank...actually doesn't look like it - %is empty regressors but actually linearly dependent regressors with the - %subsampling from the cross validation. This is a pain. Can still try - %resampling idea, but not positive that will work or if there is a way to - %guarantee that subsampling a full rank matrix will yield yet another full - %rank matrix. - - - kfolds = 10; - for br = 1:2%length(BRs) For now not running all as this seems silly - - Y = Results(br).Spikes; - - if smooth_fr %Handle this before we get here, just swap the data depending on what needs to be used. - - Y = Results(br).fr; - - end - - Results(br).Data{1} = fullR; %make a cell in the data field of each struct that hold the design matrix used for that analysis - - X = fullR; - - %2022-08-29 for now putting in as fields under Results(br).model - %2022-08-31 (late night of 08-30 actually) trying regulariztion glm - %fit with lassoglm - [Y_hat,Betas,CIs,R2_per,LL_per,pvalues,pn] = GLM_CrossVal(X,Y,kfolds,smooth_fr); - - Results(br).model(1).name = 'Full'; - - Results(br).model(1).Y_hat = Y_hat; %Estimated spike rate for all neurons - Results(br).model(1).Betas = Betas; %Beta values for each neuron (avg over cv) - Results(br).model(1).CIs = CIs; %Confidence intervals for each beta for each neuron (avg over cv) - Results(br).model(1).R2_per = R2_per; %Adjusted R^2 for each neuron (avg over cv) - Results(br).model(1).LL_per = LL_per; %Loglikelihood for the fit of each neuron (avg over cv) - Results(br).model(1).pvals = pvalues; %pvalue for each regressor for each neuron (currently average over cv but will switch to fisher's method ASAP) - Results(br).model(1).pn = pn; %Bool vector with true values for neurons that throw a warning during the fit at any point. - - end - - - - - %% Set up shuffling sets (i.e. what models we want to test) - - %2022-08-29: For now only have these to shuffle. Can add/remove as needed - - model_names = {'Full','Subj_shuff', 'Part_shuff', 'Context_shuff', 'all_shuffled'}; %Full is a place holder from above; - - %2022-08-30: Create lists of variable names to be shuffled in each "model" - %/analysis. For now just do this manually. Can set up something clever - %later if needed...Added shuffling everything as a null model/sanity check - - shuffling_vars = cell(length(model_names),1); - - %Since we are shuffling by variable group - %Take subsections of the named variables based on the individual X_groups we made earlier - - shuffling_vars{2} = {Reg_mapping{1,1:size(X_groups{1},2)}}; %Subject is always first group - - shiftamt = length(shuffling_vars{2}); - - shuffling_vars{3} = {Reg_mapping{1,shiftamt+1:... %Partner is always second group, so index limits are just shifted by variables in first group - shiftamt+size(X_groups{2},2)}}; - - shiftamt = shiftamt+length(shuffling_vars{3}); - - shuffling_vars{4} = {Reg_mapping{1,shiftamt+1:... - shiftamt+size(X_groups{3},2)}}; - - shuffling_vars{5} = {Reg_mapping{1,:}}; - - %% Make shuffled design matrices, rerun QR check - %2022-08-30 Make sure partner and subject aren't accidentally the same. - %Double checked and they are not exactly the same and think initial weird - %result may be due to the overfitting from not really cross validating. - %To be safe though making sure to shuffle the rng so they are more - %different (corr2 shows they are .6 correlated in last run so that also - %could be an issue) - - X_shuff = cell(1,length(model_names)); - regIdx_shuff = cell(1,length(model_names)); - - for shuff = 2:length(model_names) - %Get all regressors to shuffle - rng('shuffle') %Just because getting odd results with partner and subject looking similar - shuffle_inds = []; - - for var = 1:length(shuffling_vars{shuff}) %If variable is empty it won't be addeed to shuffle_ind as we are just concatenating an empty array - - var_ind = find(ismember(Reg_mapping(1,:), shuffling_vars{shuff}(var))); - - shuffle_inds = [shuffle_inds Reg_mapping{3,var}']; - - end - - %Do shuffle and save into this cell array, then perform QR check - X_temp = fullR_hold; %First put in pre QR check matrix - X_temp(:,shuffle_inds) = X_temp(randperm(size(fullR_hold,1)),shuffle_inds); %Change the shuffled inds to the shuffled version - regIdx_temp = regIdx_hold; - - [rejIdx, ~, ~, ~,] = log_run_QR(X_temp, 0); %No plotting this time so we aren't bombarded with figures. - - %Remove regressors rejected by the QR check - X_temp(:,rejIdx) = []; - regIdx_temp(rejIdx) = []; - - X_shuff{shuff} = X_temp; - regIdx_shuff{shuff} = regIdx_temp; - - %Again loop through all of variables to update Reg_mapping - - Reg_map_ind = size(Reg_mapping,1)+1; %Add a new row each time. - all_vars =unique(regIdx_shuff{shuff}); - - for vars = 1:length(all_vars) - - Reg_mapping{Reg_map_ind,all_vars(vars)} = find(regIdx_shuff{shuff} == all_vars(vars)); - - - - end - - - - - end - - %% Run second loop: Shuffled analyses - - for br = 1:length(BRs)-1 %don't do all - - Y = Results(br).Spikes; - - if smooth_fr %Handle this before we get here, just swap the data depending on what needs to be used. - - Y = Results(br).fr; - - end - - - for exper = 2:length(model_names) - fprintf(['Working on model #' num2str(exper) '\n']) - - X = X_shuff{exper}; - - - Results(br).Data{exper} = X; - - %2022-08-29 for now putting in as fields under Results(br).model - [Y_hat,Betas,CIs,R2_per,LL_per,pvalues,pn] = GLM_CrossVal(X,Y,kfolds,smooth_fr); - - Results(br).model(exper).name = model_names{exper}; - - Results(br).model(exper).Y_hat = Y_hat; %Estimated spike rate for all neurons - Results(br).model(exper).Betas = Betas; %Beta values for each neuron (avg over cv) - Results(br).model(exper).CIs = CIs; %Confidence intervals for each beta for each neuron (avg over cv) - Results(br).model(exper).R2_per = R2_per; %Adjusted R^2 for each neuron (avg over cv) - Results(br).model(exper).LL_per = LL_per; %Loglikelihood for the fit of each neuron (avg over cv) - Results(br).model(exper).pvals = pvalues; %pvalue for each regressor for each neuron (currently average over cv but will switch to fisher's method ASAP) - Results(br).model(exper).pn = pn; %Bool vector with true values for neurons that throw a warning during the fit at any point. - - - - end - - - - end - - - - %% Get Overall Statistics - - %2022-08-30: Think this is again a case of overfitting but just to note how - %bad this is, even the shuffled data - which does tank the distribution of - %well fit neurons some how manages to have an okay overall R^2 in this - %analayis. So either this analysis is being implemented wrong or the power - %of overfitting is even stronger than I thought it was. - - %For now just calculate R^2 as we did for T and T 2022 - %I think we need to adjust the R^2 for the varying amount of regressors - %(parameters) between the models, but add this in a bit. - - %Get distribution of p values for each variable as well - - - %Turn Reg_mapping into a table for ease of use later on - %NOTE******FOR NOW NEED TO MANUALLY EDIT NAMES OF COLUMNS FOR EXPERIMENTS** - %Later figure out how to automate this. - Reg_table = cell2table(Reg_mapping','VariableNames',{'Variable Name', 'Observation Label','Reg preQR', 'Reg in Fullmdl',... - 'Reg in SubShuff','Reg in PartShuff','Reg in ContShuff', 'Reg in AllShuff'}); - - - %For convenience of getting regressor values for full matrix in this loop - X_shuff{1} = fullR; %Need that info to due adjusted R^2 or other things that require knowing the degrees of freedom. - - for br = 1:length(BRs)-1 %Not doing all for now - - for md = 1:length(model_names) - %First get overall R2 between data and prediction. - - if smooth_fr - Results(br).model(md).R2_overall = corr2(Results(br).fr,Results(br).model(md).Y_hat).^2; %Add subtraction of observations over regressor numbers (using - else - Results(br).model(md).R2_overall = corr2(Results(br).Spikes,Results(br).model(md).Y_hat).^2; - end - - %Next get p_values organized by variable - - end - - end - - - - %% Do cross model comparisons - %Again for now just doing what we did in T and T 2022: delta R^2 - %Putting this in stats in first branch of Results as this is a comparison - %across models - - for br=1:length(BRs)-1 - - for diff = 2:length(model_names) - %just doing this for now to see the results quickly at the end - temp = Results(br).model(1).R2_overall - Results(br).model(diff).R2_overall; - Results(br).Stats(diff).deltaR2 = temp; - - end - - end - - - - %% Save final Results - %save(['YOUR PLACE TO SAVE COULD BE HERE!!!!!'],'Results', 'Reg_table') -end %Session loop end - - - - - - - - - - - -%% Old code sections -%% 1 Runs 1st loop with cross validation - -%2022-08-29: Commenting out for now so I can work on the function to do this I wrote -% %2022-08-28 %First just fit the model to make sure that runs/see how long it takes -% %Then set up cross validation loop: 10 Folds, 80-20 split -% %Update: doesn't take super long to run per neuron but with cross -% %validation could take a bit to run all the analyses for each brain -% %region for each session. -% -% %2022-08-29 Update: I think the cross validation will make the Stats -% %objects from the mdl much less useful as many of those can simply be -% %averaged over...maybe come up with something clever later but for -% %now take "average" Y-hat (i.e. take the prediction from all of the -% %separate 20% of the data) and take average LL as these for sure can be -% %used...further I think it will be best to make this into a function -% %that does the cross validation and fitting and spits out the needed -% %info at the end that can be loaded into the Results struct. -% -% problem_neuron = zeros(1,size(Y,2)); %Save which neurons had a warning thrown. -% -% for n = 1:size(Y,2) %For each neuron, fit a glm -% display(['Working on neuron #' num2str(n)]) -% -% %2022-08-28: Added ability to save any warnings thrown by fitglm -% -% if smooth_fr -% lastwarn('','') %reset warning. -% mdl = fitglm(X,Y(:,n),'linear','Distribution','gaussian'); %If smoothing happens use gaussian instead of poisson -% -% test1 = lastwarn; -% if ~isempty(test1) -% -% problem_neuron(1,n) = 1; %change the zero to a 1 if a warning is found -% -% end -% else -% lastwarn('','') %reset warning. -% mdl = fitglm(X,Y(:,n),'linear','Distribution','poisson'); -% -% test1 = lastwarn; -% if ~isempty(test1) -% -% problem_neuron(1,n) = 1; %change the zero to a 1 if a warning is found -% end -% end -% -% Results(br).Stats.perneuron{1, n} = mdl; -% -% Y_hat = mdl.feval(X); -% -% %After cross validation put each cross validated guess together and -% %save ad the final Y_hat in Results -% -% Results(br).Y_hat{1,n} = Y_hat; -% -% -% end -% Results(br).Warnings{1} = problem_neuron; -%% 2 that helps remove nans and unindentified behaviors from before - -% %% Step 1.4 Remove Nans -% -% %Remove Nans -% %nan_per = length(nan_inds)/length(labels) * 100; %Amount of session lost -% %to Nans -% kinematics(nan_inds,:) = []; -% behavior_labels_subject_init(nan_inds) = []; -% behavior_labels_partner_init(nan_inds) = []; -% -% for br =1:length(BRs) %will need to do this for each brain region at each step when changing the data. -% -% Results(br).Spikes(nan_inds,:) = []; -% -% if smooth_fr -% -% Results(br).fr(nan_inds,:) = []; %note this won't work with the current place holder code. -% -% end -% -% end -% -% %% Step 1.5 Remove moments without an identified behavior -% -% -% Behav_inds = zeros(size(behavior_labels_subject_init)); -% -% for i = 1:length(behavior_labels_subject_init) %Choose which behavior set you want to consider -% -% temp = [(behavior_labels_subject_init{i,:} ~= length(behav_categ))... -% (behavior_labels_partner_init{i,:} ~= length(behav_categ))]; -% -% Behav_inds(i) = sum(temp); %If ANY behavior is not the rest behavior keep the bin -% -% end -% -% Behav_inds = Behav_inds > 0; %If ANY behavior is not the rest behavior keep the bin -% -% %Keeping with above code for nans remove any inds at are NOT Behav_inds -% -% kinematics(~Behav_inds,:) = []; -% behavior_labels_subject_init(~Behav_inds) = []; -% behavior_labels_partner_init(~Behav_inds) = []; -% -% -% for br =1:length(BRs) %will need to do this for each brain region at each step when changing the data. -% -% Results(br).Spikes(~Behav_inds,:) = []; -% -% if smooth_fr -% -% Results(br).fr(~Behav_inds,:) = []; %note this won't work with the current place holder code. -% -% end -% -% end - - -%% First try at Stage 2 making the design matrix. -% %% Step 2.0 Aggregate variable groups; pass each group through function for making regressors for each variable in each group -% -% %From notes have four variable groups: subject behavior, partner behavior, kinemetics, contex/other -% %Just focusing on first 3 now. -% Var_groups = cell(1,3); -% Reg_groups = cell(2,3); %Save the output of making the design matrix for each variabel group. Concatenate later to make X_overall -% -% Var_groups{1} = behavior_labels_subject_init; -% Var_groups{2} = behavior_labels_partner_init; -% Var_groups{3} = kinematics; -% %Var_groups{4} = []; %For now not doing this but will be block id or -% %something equivalent later on for contex -% -% -% %2022-08-22 update: really need to review T and T 2022 to figure out how to -% %keep regressor inds grouped with each variable within each variable -% %group...This will be the work of this afternoon/evening and tomorrow. -% -% -% for group = 1:length(Var_groups) %going to have to think about this... -% %I think for all of them need regressor for current state/raw trace in -% %case of kinematics. Then for the first three need delay matrix, but -% %need some way to trigger that to get events first for kinematics... -% %Probably just do a data check where if variables in that group have -% %non-integer values then we treat it as kinematics and first get events -% %based on being a certain amount above a std based threshold -% -% [X_curr] = makeDesignmat_current(Var_groups(groups)); %2022-08-22 need to still write this function...borrow heavily from T and T 2022 -% -% var_inds = size(X_curr,2); %Get the number of variables in that var group -% %Need this for assigning delay regressors to correct variable group -% %when we calculate the delay regressors. -% -% if group ~=4 %Context doesn't get delay matrix -% -% [X_delays, reg_inds] = makeDesignmat_delay(Var_groups(groups)); %2022-08-22 need to still write this function...borrow heavily from T and T 2022 -% -% %Some fun math here to get the regressor indicies that map to each -% %variable in each variable group. -% -% var_inds = 'something clever' -% -% end -% -% Reg_groups{1,group} = [X_curr X_delays]; -% -% Reg_groups{2,group} = var_inds; %Save -% -% end - - - diff --git a/Varia/GLM/old_code/GLM_Pipeline_Try2.m b/Varia/GLM/old_code/GLM_Pipeline_Try2.m deleted file mode 100644 index 7a4ebba..0000000 --- a/Varia/GLM/old_code/GLM_Pipeline_Try2.m +++ /dev/null @@ -1,908 +0,0 @@ -%% Running Notes - -%Start 2022-08-31: Second try. Decided just to use code from T and T 2022 -%(i.e. just do ridge regression instead of glm) keep current loading and -%results structure. Alter as needed to get to work with the Churchland -%functions. Determine what extra information can be pulled out of -%Churchland function (ideally something about the significance of the -%betas). - -%% Stage 1 Get data loaded in and set up loops - - -%% Step 1.0: Set filenames, Set loop for sessions - -%Update notes: Make these straightfoward to change to interact with later -%functions - -%Parameters for setting Path -is_mac = 0; %For loading the data %2022-08-29 Use this to replace is_ron -is_ron = 1; %For setting path - - - -%Parameters for setting sessions to use and brain regions to look at -S_list = [1]; %List of session to pull; %For now focus on first Amost sessions -BRs = ["TEO", "vlPFC", "all"]; %Channels considered (sets brain region TEO vlPFC or all) - - - - - - -%Parameters for neural data to be used across sessions -with_NC =1; %0: Noise cluster (NC) is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well-isolated units -temp_resolution = 1; %Temporal resolution of firing rate. 1sec %%Note this will change if we introduce smoothing -smooth_fr = 1; %Toggle to use smooth spike rasters (currently not setting up this code as may require working with ms neural data and then downsampling to get back) - - - - -for s = S_list %Do analyses for each session put in S_list - - %Set path -if is_ron - - sessions = dir('C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_data'); sessions = sessions(3:end,:); - filePath = ['C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_data\' sessions(s).name]; - savePath = ['C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_results']; - -else - - - %Set session list -home = '~'; % set home directory -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) -savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/partner_vs_subject']; -end - -%% Step 1.1 Preallocate Results structure; save spikes for each brain region; performing smoothing and save results if smoothing. - - clear Results %make sure Results is cleared before each session is ran - %Preallocate one branch of Results for each BR used. Save one results file for each session. - %Put data from each brain region into appropriate Results path. - %Only Spike_raster should change so just keep the behavior and the rest - %from the last BR load in. - for br = 1:length(BRs) - - Results(br).BR = BRs(br); - channel_flag = Results(br).BR; - %Get data with specified temporal resolution and channels - %2022-08-23 note: have to change this to new function when Camille - %is done with that and rego through everything again to make sure - %there isn't a change. - - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_Ron(filePath, temp_resolution, channel_flag, is_mac,is_ron, with_NC, isolatedOnly); - % Output: - % 1. Spike_rasters: n_neuron X time in seconds (if temp_resolution = 1) - % 2. labels: time X 11 - check the function description for - % details. Essentially subject behavior is column 3. - % 3. labels_partner: time X 11. Partner behavior is column 3. - % 4. behav_categ: string vector; behavioral category do the numbers in - % labels correspond to. - % 5. block_times: specifies the block times and ID - % 6. monkey: subjecet monkey ID - % 7. reciprocal_set: set of behaviors that are deeemed "reciprocal" - % 8. social_set: set of behaviors that are deeemed "social" - % 9. ME_final: Motion energy values - % 10. unit_count: number of units in the session - % 11. groom_labels_all: labels for grooming. Check function - % description for details - Results(br).Spikes = Spike_rasters'; - - if smooth_fr - - %2022-08-29 temp solution is to just copy and paste smoothing - %code here. I'm not exactly sure which of the loading - %functions is the right one and need to review again what the - %differences are. For now do this and deal with the difference - %later tonight after sure the rest of the code runs. - sigma = 2; - gauss_range = -3*sigma:3*sigma; %calculate 3 stds out, use same resolution for convenience - smoothing_kernel = normpdf(gauss_range,0,sigma); %Set up Gaussian kernel - smoothing_kernel = smoothing_kernel/sum(smoothing_kernel); - smoothing_kernel = smoothing_kernel * 1; %Rescale to get correct firing rate - Spike_rasters_smooth = conv2(Spike_rasters, smoothing_kernel,'same'); - Spike_rasters = Spike_rasters_smooth; - Results(br).fr = Spike_rasters_smooth'; - - end - - end %End brain region loop. Should now have neural activity saved for each brain region and them together in Results - -%% Step 1.2 Get (fake) kinematics data; Get Nans - -%Later this will be the function call to get the kinematics data if that -%isn't included in the above load in code. For now just make to random -%gaussian sets and throw some random NaNs in their to work on the NaN -%clearing code (mostly borrowed from T and T 2022); - -fake_x = normrnd(0,1,size(labels,1),1); -fake_y = normrnd(0,1,size(labels,1),1); - -nan_per = 0; %Set percent (as decimal) of nans each individual fake regressor gets -fake_x(randsample(length(fake_x),round(nan_per*length(fake_x))),1) = NaN; -fake_y(randsample(length(fake_y),round(nan_per*length(fake_y))),1) = NaN; - -kinematics = [fake_x fake_y]; - -nan_counts = sum(isnan(kinematics),2); - -% figure() -% histogram(nan_counts) %Shows number of nans and their overlap (all numbers greater than 1) -% - -%If needed put code for interpolating over short runs of nans from T and T -%2022 here. For now, leaving out. - -nan_inds = nan_counts > 0; %Naturally anything that has a count greater than 0 is a time point that has an NaN - -%% Step 1.3 Set Proximity to non-identified behavior -%Update 2022-08-30 switching to only considering 1 behavior happening at -%any moment to see if this resolves rank issues in our design matrix during -%cross validation. - -%Change this so it doesn't occur. -%Change this so you can have more than one behavior in a frame. - - behavior_labels_subject = labels(:,2); %Extract all behavior info for subject - behavior_labels_partner = labels_partner(:,2); %Extract all behavior info for partner - -% % Set proximity as rest -% prox_ind = find(behav_categ=="Proximity"); -% %Not exactly sure the best way to do this...not sure how to do this -% %as a cellfun. For now just going to do a for loop to move on. -% -% -% %NOTE: ASSUMES THAT REST/UNIDENTIFIED IS ALWAYS MAX NUMERIC LABEL -% -% for i = 1:length(behavior_labels_partner) -% -% check_inds = behavior_labels_subject{i,:} == prox_ind; -% -% if any(check_inds) -% -% behavior_labels_subject{i}(check_inds) = length(behav_categ); -% -% end -% -% check_inds = behavior_labels_partner{i,:} == prox_ind; -% -% if any(check_inds) -% -% behavior_labels_partner{i}(check_inds) = length(behav_categ); -% -% end -% -% end - -%Temporary joining of those cells so I can easily check subject and partner behavior -Behavs = [behavior_labels_subject behavior_labels_partner]; - -%End stage 1: Now have data we will use to make design matrix in stage 2 -%and the spiking data in the form it will be used for Stage 3 - -%% Stage 2 Create regressors - -%TERMINOLOGY NOTES: -%-Variable group = family of variables put together due to belong to the -%same type of observation. Currently have: subject behavior, partner -%behavior, context, and kinematics - -%-Variable = A single state that can be observed or key point that can be -%measured. For example: grooming, agression, block id, arm_x position. - -%-Regressor = A column of the design matrix. Each variable can have -%multiple regressors. Examples: grooming could have a regressor for -%particular delays before or after the onset/offset of grooming in addition -%to having a regressor for stating whenever the animal is currently -%grooming. arm_x could have both a tracking of the position of the arm as -%well as a regressor for arm events when the arm is moved above a -%particular threshold of movement. - -%-Regressor group = Would refer to groups of -%regressors across several variables put together for a particular analysis -%that is different from the Variable group. Examples: all regressor -%relating to head and eye movement in kinematics may be grouped together in -%an analysis that focuses on a subset a kinematics and could be call the -%head-eye regressor group. All behaviors deemed affiliative by either the -%subject or partner could be grouped together for an affiliative regressor -%group. - -%State-events refer to all non-kinematic, binary regressors. - - -%% Step 2.0 Set options for time event kernels - -%set up sampling rate (not sure this is useful, but it's used in code further down so i'll keep it for now). -opts.Fs = temp_resolution; - -%Set up windows for time varying kernels. Multiplying factor is in seconds -%(recall FS is samples/second ),so can adjust window accordingly - -%2022-08-23: need to check this is still correct. - -opts.mPreTime = round(2 * opts.Fs); %motor pre time -opts.mPostTime = round(2 * opts.Fs); %motor post time -motorIdx = [-(opts.mPreTime: -1 : 1) 0 (1:opts.mPostTime)]; %index for design matrix to cover pre- and post motor action -opts.folds = 10; %2022-08-31 since now using T and T 2022 code this needs to be in opts - - -%% Step 2.1 Get labels for each state-event variable group -%2022-08-31 change this to not remove proximity - -%First get labels for all subject, partner, and context variables so we -%know how many variables we have to start with - - - -%Observations x behavior label -obs_statevents_subject = unique(cell2mat(labels(:,3))); -%Need to make a column for each behavior that happens in the session. -%Above code does assume there is no behavior that only occurs paired -%with another behavior and that was removed when only one behavior per -%moment was prioritized. I think this is a reasonable assumption, so -%will go forward with it. - -%For now just repeat. Later have this be either structure or a cell -%and have a function that adds to it as needed. - -obs_statevents_partner = unique(cell2mat(labels_partner(:,3))); -obs_statevents_partner = setdiff(obs_statevents_partner,reciprocal_set); -%Remove repricoal behaviors according to load in function - - -obs_context = 1:size(block_times,1); %For now just considering the three different blocks for context. - -obs_statevents = [{obs_statevents_subject}, {obs_statevents_partner} ,{obs_context}]; %Gives number of varibles in each group of states events considered - -% -%% Step 2.2 Create base regressors for each state-event variable and regressor mapping - - -%+1 for kinematics - X_groups = cell(1,length(obs_statevents)+1); - - Reg_mapping = cell(1,length(obs_statevents)+1); - - for this_mat = 1:size(Behavs,2) %This loop is geared toward behaviors or variable groups where more than one event can occur at each time point - - X_groups{this_mat} = zeros(length(Spike_rasters), length(obs_statevents{this_mat})); - - %Note, behavior numeric labels will be larger than this matrix (e.g. - %there are 22 behaviors but one of the labels is 29.) Thus need to - %create/save mapping between numeric label in behavior_labels and the - %column of the design matrix. - - %First row is behavior label, 2nd row is original numeric label, update - %2022-08-30 don't add 3 row for now. This will be index in matrix - %after nan check then will add subsequent rows which will hold the - %indecies for the regressors associated with each variable in each - %experiment after the QR check. Also at the end going to make - %Reg_mapping a table so it is easier to navigate. - - - Reg_mapping{this_mat} = cell(2,length(obs_statevents{this_mat})); - - - for this_label = 1:length(obs_statevents{this_mat}) - - Reg_mapping{this_mat}{1,this_label} = behav_categ{obs_statevents{this_mat}(this_label)}; - - Reg_mapping{this_mat}{2,this_label} = obs_statevents{this_mat}(this_label); - -% Reg_mapping{this_mat}{3,this_label} = this_label; %Note will have to mess around with this if we introduce delays in regressors -% -% %Put ones into design matrix for indicated column whenever that behavior was present -% %Update: can't just do this now that we have the potential for -% %multiple behaviors at each time point. Instead for each behavior -% %will loop through the session and put a 1 in the design matrix -% %each time that behavior occurred. This is not efficient code, but -% %for now this is fine. Try to think of something clever later on. - - for i =1:length(Behavs) - X_groups{this_mat}(i,this_label) = sum(Behavs{i,this_mat}(:) == Reg_mapping{this_mat}{2,this_label}); %Sum is just there to convert back to double/account for multiple behaviors - %We are only checking one behavior at a time in this loop so it - %won't result in a value greater than 1. - end - - end - - - - - end %End loop for setting up variable groups with multiple staets - - %Do the same thing for context which is only block id for now. Can't - %be added easily to above loop or put into some general function since - %the information is not stored in that way. - - this_mat = this_mat+1; - - X_groups{this_mat} = zeros(length(Spike_rasters), length(obs_statevents{this_mat})); - - for this_label = 1:length(obs_statevents{this_mat}) - - Reg_mapping{this_mat}{1,this_label} = block_times.Behavior{this_label}; - - Reg_mapping{this_mat}{2,this_label} = obs_statevents{this_mat}(this_label); - -% Reg_mapping{this_mat}{3,this_label} = this_label; - - %Need to populate each block using the info in block_times table - - X_groups{this_mat}(block_times.start_time_round(this_label):... - block_times.end_time_round(this_label)... - ,this_label) = 1; %Put ones in the column for that block id based on block_times - - end - - Reg_mapping{2}(1,:) = append('partner.',Reg_mapping{2}(1,:)); %Add this now to prevent confusion later. -%% Step 2.3 Setup Design Matrix - Behavioral Events - -%Combine behavioral events -behavEvents=horzcat(X_groups{1:3}); - -%Get names via Reg_mapping -behavEventNames = horzcat(Reg_mapping{1}(1,:)... - ,Reg_mapping{2}(1,:),... - Reg_mapping{3}(1,:)); %2022-08-29: Cam added more info to block (gender pairing in not alone) - - - -%Set up event type here. -behavEventTypes= [ones(1,length(behavEventNames)-length(obs_context))*3 zeros(1,length(obs_context))]; -% For now I will assign all behavioral events as event type 3 -%Event Type 1 = Whole trial -%Event Type 2 = from stimulus onset to the rest of the trial -%Event Type 3 = from before movement onset to after movement onset (pre & post time) - - - - - -%Creates task regressors with the time varying kernels as described in Churchland -[behavR, behavIdx] = log_makeDesignMatrix(behavEvents, behavEventTypes, opts); - - - -%% Step 2.4 Setup Design Matrix - Movement - -%Add later this week and take code from previous attempts. For now focus -%on getting GLM loop working (stage 3). - -moveR = []; -moveIdx = []; - - -%% Step 2.5 Combine Design Matrix; get mapping to groups - -%Combine Design Matrix -fullR=[behavR, moveR]; - -%Collect all indecies together. -regIdx =[behavIdx; moveIdx']; %check why moveIdx is transposed.......... - -%Collect all mappings together - -Reg_mapping = horzcat(Reg_mapping{:}); - - -disp('Design Matrix base Setup Done') - - - -%% Step 2.6 Remove nans - - -nan_per = sum(nan_inds)/size(fullR,1) * 100; %Amount of session lost, note nan_inds is calculated when kinematics are loaded - -fullR(nan_inds,:) = []; - -for br =1:length(BRs) %will need to do this for each brain region at each step when changing the data. - - Results(br).Spikes(nan_inds,:) = []; - - if smooth_fr - - Results(br).fr(nan_inds,:) = []; %note this won't work with the current place holder code. - - end - -end - - -%2020-12-19 CT: Remove regressors with less than 10 events (as in -%Musall et. al. 2019) - -low_events_idx= find(sum(fullR,1)<10); -fullR(:,low_events_idx)=[]; -regIdx(low_events_idx)=[]; - - - -%% Step 2. 7 Standardize and/or center - -%2022-08-29: Firing rate: switch to taking out -%mean. Don't standardize - -% % % Center and Standardize Continuous Data -% % Center both analog (i.e. movement/tracking data) and neural data. -% % -% % New approach: Find columns that don't just contain zero or one values: -% % [~, columns]=find(fullR~=0 & fullR~=1); -% % analoginds = unique(columns); clear columns -% % -% % standarize analog regressors -% % fullR(:,analoginds) = (fullR(:,analoginds)- mean(fullR(:,analoginds),1))./std(fullR(:,analoginds)); -% % - -if smooth_fr - for br = 1:length(BRs) - - Results(br).fr = Results(br).fr - mean(Results(br).fr,1); - - end -end - -%% Stage 3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%% Run QR check on full design matrix. Save copy of matrix without any changes - -%Save an unaltered copy -fullR_hold = fullR; -regIdx_hold = regIdx; - -[rejIdx, median_val, min_val, max_val,] = log_run_QR(fullR, 1); %Have this spit out all of the numbers for full model - - -all_vars =unique(regIdx); -for vars = 1:length(all_vars) - - Reg_mapping{3,all_vars(vars)} = find(regIdx == all_vars(vars)); - -end - - -%Remove regressors rejected by the QR check -fullR(:,rejIdx) = []; -regIdx(rejIdx) = []; - -all_vars =unique(regIdx); -for vars = 1:length(all_vars) - - Reg_mapping{4,all_vars(vars)} = find(regIdx == all_vars(vars)); - - - -end - - - -%% Run first loop (main model for each brain region) - -%2022-08-31: Change this to work with Churchland functions -%We technically could change the function to just take Reg_mapping as it -%has all of the information needed. But for the sake of not complicating -%things just take the needed information from Reg_mapping. Unless this -%becomes a pain then I'm just going to edit the code to take Reg_mapping -%instead. Update: this was actually a remarkably easy change. Need to -%double check that everything is working as intended - -%2022-08-31 - -regLabels = Reg_mapping(1,:); %This is always all the labels in the full design matrix -usedLabels = Reg_mapping(1,:); %This is also always all the labels since we use a shuffling paradigm. Helpfully though if we want to do subsets we can do it the same way we handle the shuffling loop set up below - -for br = 1:2%length(BRs) For now not running all as this seems silly - - Y = Results(br).Spikes; - - if smooth_fr %Handle this before we get here, just swap the data depending on what needs to be used. - - Y = Results(br).fr; - - end - - Results(br).Data{1} = fullR; %make a cell in the data field of each struct that hold the design matrix used for that analysis - - X = fullR; - - %2022-08-31 replace GLM with just ridge regression from T and T 2022. - %Adjust fields in results accordingly...looks like a pretty straight - %subsititution so far. NOTE requires the ridge regression folder in - %this repo to be on path so it has access to all the needed functions. - %And just copied over the mml function to that folder now. - - [Y_hat, Betas, ~, Idx, Ridge, Labels] = log_crossValModel(X, Y, regLabels, regIdx, regLabels, opts.folds); - Results(br).model(1).name = 'Full'; - - Results(br).model(1).Y_hat = Y_hat'; %Estimated spike rate for all neurons, transpose because of how the above function works. - Results(br).model(1).Betas = Betas; %Beta values for each neuron (cells for each cross validation fold...could average these I suppose, leaving alone for now) - - Results(br).model(1).R2_overall = corr2(Y_hat',Y).^2; %With the above function easier to get the overall here. - %Change later portion where we get the overall to get the per neuron for each model and then the diff - - %To do: figure out a way to get the below information from the above - %function. Will need some customization for sure since the easiest - %place to get the CI is to have the fisher information during the - %optimization process. Regardless, there has to be some way to estimate - %the CI around the betas for each fold and conduct some significance - %tests so we can have the below. -% Results(br).model(1).CIs = CIs; %Confidence intervals for each beta -% for each neuron (avg over cv) % We don't have this -% Results(br).model(1).pvals = pvalues; %pvalue for each regressor for each neuron (currently average over cv but will switch to fisher's method ASAP) - - - end - - - - -%% Set up shuffling sets (i.e. what models we want to test) - -%2022-08-29: For now only have these to shuffle. Can add/remove as needed - -model_names = {'Full','Subj_shuff', 'Part_shuff', 'Context_shuff', 'all_shuffled'}; %Full is a place holder from above; - -%2022-08-30: Create lists of variable names to be shuffled in each "model" -%/analysis. For now just do this manually. Can set up something clever -%later if needed...Added shuffling everything as a null model/sanity check - -shuffling_vars = cell(length(model_names),1); - -%Since we are shuffling by variable group -%Take subsections of the named variables based on the individual X_groups we made earlier - -shuffling_vars{2} = {Reg_mapping{1,1:size(X_groups{1},2)}}; %Subject is always first group - -shiftamt = length(shuffling_vars{2}); - -shuffling_vars{3} = {Reg_mapping{1,shiftamt+1:... %Partner is always second group, so index limits are just shifted by variables in first group - shiftamt+size(X_groups{2},2)}}; - -shiftamt = shiftamt+length(shuffling_vars{3}); - -shuffling_vars{4} = {Reg_mapping{1,shiftamt+1:... - shiftamt+size(X_groups{3},2)}}; - -shuffling_vars{5} = {Reg_mapping{1,:}}; - -%% Make shuffled design matrices, rerun QR check -%2022-08-30 Make sure partner and subject aren't accidentally the same. -%Double checked and they are not exactly the same and think initial weird -%result may be due to the overfitting from not really cross validating. -%To be safe though making sure to shuffle the rng so they are more -%different (corr2 shows they are .6 correlated in last run so that also -%could be an issue) - -X_shuff = cell(1,length(model_names)); -regIdx_shuff = cell(1,length(model_names)); - -for shuff = 2:length(model_names) - %Get all regressors to shuffle - rng('shuffle') %Just because getting odd results with partner and subject looking similar - shuffle_inds = []; - - for var = 1:length(shuffling_vars{shuff}) %If variable is empty it won't be addeed to shuffle_ind as we are just concatenating an empty array - - var_ind = find(ismember(Reg_mapping(1,:), shuffling_vars{shuff}(var))); - - shuffle_inds = [shuffle_inds Reg_mapping{3,var}']; - - end - - %Do shuffle and save into this cell array, then perform QR check - X_temp = fullR_hold; %First put in pre QR check matrix - X_temp(:,shuffle_inds) = X_temp(randperm(size(fullR_hold,1)),shuffle_inds); %Change the shuffled inds to the shuffled version - regIdx_temp = regIdx_hold; - - [rejIdx, ~, ~, ~,] = log_run_QR(X_temp, 0); %No plotting this time so we aren't bombarded with figures. - - %Remove regressors rejected by the QR check - X_temp(:,rejIdx) = []; - regIdx_temp(rejIdx) = []; - - X_shuff{shuff} = X_temp; - regIdx_shuff{shuff} = regIdx_temp; - - %Again loop through all of variables to update Reg_mapping - - Reg_map_ind = size(Reg_mapping,1)+1; %Add a new row each time. - all_vars =unique(regIdx_shuff{shuff}); - - for vars = 1:length(all_vars) - - Reg_mapping{Reg_map_ind,all_vars(vars)} = find(regIdx_shuff{shuff} == all_vars(vars)); - - - - end - - - - -end - -%% Run second loop: Shuffled analyses - -for br = 1:length(BRs)-1 %don't do all - - Y = Results(br).Spikes; - - if smooth_fr %Handle this before we get here, just swap the data depending on what needs to be used. - - Y = Results(br).fr; - - end - - - for exper = 2:length(model_names) - fprintf(['Working on model #' num2str(exper) '\n']) - - X = X_shuff{exper}; - - - Results(br).Data{exper} = X; - - [Y_hat, Betas, ~, Idx, Ridge, Labels] = log_crossValModel(X, Y, regLabels, regIdx_shuff{exper}, regLabels, opts.folds); - - - Results(br).model(exper).name = model_names{exper}; - Results(br).model(exper).Y_hat = Y_hat'; %Estimated spike rate for all neurons, transpose because of how the above function works. - Results(br).model(exper).Betas = Betas; %Beta values for each neuron (cells for each cross validation fold...could average these I suppose, leaving alone for now) - Results(br).model(exper).R2_overall = corr2(Y_hat',Y).^2; %Get overall R^2 here - - - - - - - end - - - -end - - - -%% Get Additional Statistics - -%Turn Reg_mapping into a table for ease of use later on -%NOTE******FOR NOW NEED TO MANUALLY EDIT NAMES OF COLUMNS FOR EXPERIMENTS** -%Later figure out how to automate this. Update have an idea using the -%append function just need to figure out how to make the whole cell array -%before passing it to this...leave alone for now - -Reg_table = cell2table(Reg_mapping','VariableNames',{'Variable Name', 'Observation Label','Reg preQR', 'Reg in Fullmdl',... - 'Reg in SubShuff','Reg in PartShuff','Reg in ContShuff', 'Reg in AllShuff'}); - -%2022-08-31: Changed this code to calculate the per neuron R2 for all -%neurons for each model. This is simply the diagonal of the correlation -%matrix between the predicted data and the real data. - -%For convenience of getting regressor values for full matrix in this loop -X_shuff{1} = fullR; %Need that info to due adjusted R^2 or other things that require knowing the degrees of freedom. - -for br = 1:length(BRs)-1 %Not doing all for now - - for md = 1:length(model_names) - %First get overall R2 between data and prediction. - - if smooth_fr - Results(br).model(md).R2_per = diag(corr(Results(br).fr,Results(br).model(md).Y_hat)).^2; - else - Results(br).model(md).R2_per = diag(corr(Results(br).Spikes,Results(br).model(md).Y_hat)).^2; - end - - %Next get p_values organized by variable - - end - -end - - - -%% Do cross model comparisons -%Again for now just doing what we did in T and T 2022: delta R^2 -%Putting this in stats in first branch of Results as this is a comparison -%across models - -for br=1:length(BRs)-1 - - for diff = 2:length(model_names) - %just doing this for now to see the results quickly at the end - temp = Results(br).model(1).R2_overall - Results(br).model(diff).R2_overall; - Results(br).Stats(diff).deltaR2 = temp; - - end - -end - - - -%% Save final Results -%save(['YOUR PLACE TO SAVE COULD BE HERE!!!!!'],'Results', 'Reg_table') -end %Session loop end - - - - - - - - - - - -%% Old code sections -%% 1 Runs 1st loop with cross validation - -%2022-08-29: Commenting out for now so I can work on the function to do this I wrote -% %2022-08-28 %First just fit the model to make sure that runs/see how long it takes -% %Then set up cross validation loop: 10 Folds, 80-20 split -% %Update: doesn't take super long to run per neuron but with cross -% %validation could take a bit to run all the analyses for each brain -% %region for each session. -% -% %2022-08-29 Update: I think the cross validation will make the Stats -% %objects from the mdl much less useful as many of those can simply be -% %averaged over...maybe come up with something clever later but for -% %now take "average" Y-hat (i.e. take the prediction from all of the -% %separate 20% of the data) and take average LL as these for sure can be -% %used...further I think it will be best to make this into a function -% %that does the cross validation and fitting and spits out the needed -% %info at the end that can be loaded into the Results struct. -% -% problem_neuron = zeros(1,size(Y,2)); %Save which neurons had a warning thrown. -% -% for n = 1:size(Y,2) %For each neuron, fit a glm -% display(['Working on neuron #' num2str(n)]) -% -% %2022-08-28: Added ability to save any warnings thrown by fitglm -% -% if smooth_fr -% lastwarn('','') %reset warning. -% mdl = fitglm(X,Y(:,n),'linear','Distribution','gaussian'); %If smoothing happens use gaussian instead of poisson -% -% test1 = lastwarn; -% if ~isempty(test1) -% -% problem_neuron(1,n) = 1; %change the zero to a 1 if a warning is found -% -% end -% else -% lastwarn('','') %reset warning. -% mdl = fitglm(X,Y(:,n),'linear','Distribution','poisson'); -% -% test1 = lastwarn; -% if ~isempty(test1) -% -% problem_neuron(1,n) = 1; %change the zero to a 1 if a warning is found -% end -% end -% -% Results(br).Stats.perneuron{1, n} = mdl; -% -% Y_hat = mdl.feval(X); -% -% %After cross validation put each cross validated guess together and -% %save ad the final Y_hat in Results -% -% Results(br).Y_hat{1,n} = Y_hat; -% -% -% end -% Results(br).Warnings{1} = problem_neuron; -%% 2 that helps remove nans and unindentified behaviors from before - -% %% Step 1.4 Remove Nans -% -% %Remove Nans -% %nan_per = length(nan_inds)/length(labels) * 100; %Amount of session lost -% %to Nans -% kinematics(nan_inds,:) = []; -% behavior_labels_subject_init(nan_inds) = []; -% behavior_labels_partner_init(nan_inds) = []; -% -% for br =1:length(BRs) %will need to do this for each brain region at each step when changing the data. -% -% Results(br).Spikes(nan_inds,:) = []; -% -% if smooth_fr -% -% Results(br).fr(nan_inds,:) = []; %note this won't work with the current place holder code. -% -% end -% -% end -% -% %% Step 1.5 Remove moments without an identified behavior -% -% -% Behav_inds = zeros(size(behavior_labels_subject_init)); -% -% for i = 1:length(behavior_labels_subject_init) %Choose which behavior set you want to consider -% -% temp = [(behavior_labels_subject_init{i,:} ~= length(behav_categ))... -% (behavior_labels_partner_init{i,:} ~= length(behav_categ))]; -% -% Behav_inds(i) = sum(temp); %If ANY behavior is not the rest behavior keep the bin -% -% end -% -% Behav_inds = Behav_inds > 0; %If ANY behavior is not the rest behavior keep the bin -% -% %Keeping with above code for nans remove any inds at are NOT Behav_inds -% -% kinematics(~Behav_inds,:) = []; -% behavior_labels_subject_init(~Behav_inds) = []; -% behavior_labels_partner_init(~Behav_inds) = []; -% -% -% for br =1:length(BRs) %will need to do this for each brain region at each step when changing the data. -% -% Results(br).Spikes(~Behav_inds,:) = []; -% -% if smooth_fr -% -% Results(br).fr(~Behav_inds,:) = []; %note this won't work with the current place holder code. -% -% end -% -% end - - -%% First try at Stage 2 making the design matrix. -% %% Step 2.0 Aggregate variable groups; pass each group through function for making regressors for each variable in each group -% -% %From notes have four variable groups: subject behavior, partner behavior, kinemetics, contex/other -% %Just focusing on first 3 now. -% Var_groups = cell(1,3); -% Reg_groups = cell(2,3); %Save the output of making the design matrix for each variabel group. Concatenate later to make X_overall -% -% Var_groups{1} = behavior_labels_subject_init; -% Var_groups{2} = behavior_labels_partner_init; -% Var_groups{3} = kinematics; -% %Var_groups{4} = []; %For now not doing this but will be block id or -% %something equivalent later on for contex -% -% -% %2022-08-22 update: really need to review T and T 2022 to figure out how to -% %keep regressor inds grouped with each variable within each variable -% %group...This will be the work of this afternoon/evening and tomorrow. -% -% -% for group = 1:length(Var_groups) %going to have to think about this... -% %I think for all of them need regressor for current state/raw trace in -% %case of kinematics. Then for the first three need delay matrix, but -% %need some way to trigger that to get events first for kinematics... -% %Probably just do a data check where if variables in that group have -% %non-integer values then we treat it as kinematics and first get events -% %based on being a certain amount above a std based threshold -% -% [X_curr] = makeDesignmat_current(Var_groups(groups)); %2022-08-22 need to still write this function...borrow heavily from T and T 2022 -% -% var_inds = size(X_curr,2); %Get the number of variables in that var group -% %Need this for assigning delay regressors to correct variable group -% %when we calculate the delay regressors. -% -% if group ~=4 %Context doesn't get delay matrix -% -% [X_delays, reg_inds] = makeDesignmat_delay(Var_groups(groups)); %2022-08-22 need to still write this function...borrow heavily from T and T 2022 -% -% %Some fun math here to get the regressor indicies that map to each -% %variable in each variable group. -% -% var_inds = 'something clever' -% -% end -% -% Reg_groups{1,group} = [X_curr X_delays]; -% -% Reg_groups{2,group} = var_inds; %Save -% -% end \ No newline at end of file diff --git a/Varia/GLM/old_code/log_glm_behav_vs_context.m b/Varia/GLM/old_code/log_glm_behav_vs_context.m deleted file mode 100644 index 0ff7760..0000000 --- a/Varia/GLM/old_code/log_glm_behav_vs_context.m +++ /dev/null @@ -1,194 +0,0 @@ -%Extract mean and STD behaviors and neurons - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -threat_precedence =1; -exclude_sq=0; -plot_toggle=0; -warning('off','all') - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/Example_units']; - - %% Load data - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq ); - end - - disp('Data Loaded') - - Spike_count_raster = zscore(Spike_rasters'); - - %Extract behavior labels - behavior_labels= cell2mat({labels{:,3}}');%Get behavior label from labels structure - context = cell2mat({labels{:,12}}'); context_categ={"female","male","alone"}; - paired_or_not = cell2mat({labels{:,13}}'); - - - behavior_final = dummyvar(categorical(behavior_labels)); %behavior_final = behavior_final(:,[7,8,9]); - context_final = dummyvar(categorical(context));%Same as above but in behavior labels - all_predictors=[behavior_final(:,1:size(behavior_final,2)-1), context_final(:,[1,3])]; - - %shuffle all non-context regressors - predictors_mat_context = all_predictors; - predictors_mat_context(:,1:size(behavior_final,2)-1) = predictors_mat_context(randsample(size(behavior_final,1),size(behavior_final,1)), 1:size(behavior_final,2)-1); - - %shuffle all non-behavior regressors - predictors_mat_behav = all_predictors; - predictors_mat_behav(:,size(behavior_final,2):end) = predictors_mat_behav(randsample(size(behavior_final,1),size(behavior_final,1)), size(behavior_final,2):end); - - %Run glm for each unit separately - %NOTE: issue with low number of predictors?! - for unit = 1:size(Spike_count_raster,2) - - NeuralResponse = Spike_count_raster(:,unit);%Only keep timepoints where the behaviors of interest occur in spiking data - - %Full fit - mdl =fitlm(all_predictors,NeuralResponse); - Rsq{s}(unit,1)=mdl.Rsquared.Ordinary; -% RsqAdj{s}(unit,1)=mdl.Rsquared.Adjusted; - - %Only considering behavior - mdl =fitlm(predictors_mat_behav,NeuralResponse); - Rsq{s}(unit,2)=mdl.Rsquared.Ordinary; -% mdl2 =fitlm(behavior_final,NeuralResponse); -% RsqAdj{s}(unit,2)=mdl2.Rsquared.Adjusted; -% [p, tbl_behav] =anova1(NeuralResponse, categorical(behavior_labels),'off'); -% etasq{s}(unit,1)= tbl_behav{2,2}./tbl_behav{4,2}; - - %Only considering context - mdl =fitlm(predictors_mat_context,NeuralResponse); - Rsq{s}(unit,3)=mdl.Rsquared.Ordinary; -% mdl2 =fitlm(context_final,NeuralResponse); -% RsqAdj{s}(unit,3)=mdl2.Rsquared.Adjusted; -% [p, tbl_context] =anova1(NeuralResponse, categorical(context),'off'); -% etasq{s}(unit,2)= tbl_context{2,2}./tbl_context{4,2}; - - end %end of units - - Rsq_teo{s} = Rsq{s}(strcmp(brain_label,'TEO'),:); - Rsq_vlpfc{s} = Rsq{s}(strcmp(brain_label,'vlPFC'),:); - -% figure; hold on -% histogram(Rsq{s}(:,1)); histogram(Rsq{s}(:,2)); histogram(Rsq{s}(:,3)) -% -% figure; hold on -% scatter(Rsq{s}(:,3), Rsq{s}(:,2),'filled') -% plot(0:0.5,'k'); -% ylabel('Behavior') -% xlabel('Context') -% xlim([0 0.5]); ylim([0 0.5]) -% grid on -% -% figure; hold on -% scatter(Rsq{s}(:,2), etasq{s}(:,1),'filled') -% plot(0:0.5,'k'); -% ylabel('Etasq') -% xlabel('Rsq') -% xlim([0 0.5]); ylim([0 0.5]) -% grid on - -%IMPORTANT NOTE: Eta squared and R squared give exactly the same result. -%Adjusted R squared is also qualitatively identical. - -disp(s) -disp('done.') - -end %end of session - -cd(['~/Dropbox (Penn)/Datalogger/Results/All_sessions/SingleUnit_results/']); -% save('Glm_context_behav.mat', "Rsq","Rsq_teo","Rsq_vlpfc","a_sessions","h_sessions","behav_categ") -load('Glm_context_behav.mat') - -Rsq_all_neurons = cat(1,Rsq{:}); -Rsq_teo_neurons = cat(1,Rsq_teo{:}); -Rsq_vlpfc_neurons = cat(1,Rsq_vlpfc{:}); - -% %Pooled monkeys -% figure; hold on -% histogram(Rsq_all_neurons(:,1)); histogram(Rsq_all_neurons(:,2)); histogram(Rsq_all_neurons(:,3)) -% -% figure; hold on -% scatter(Rsq_teo_neurons(:,3), Rsq_teo_neurons(:,2),'filled','r') -% scatter(Rsq_vlpfc_neurons(:,3), Rsq_vlpfc_neurons(:,2),'filled','b') -% plot([0 0.5],[0 0.5],'k','DisplayName','Diagonal'); -% ylabel('Behavior') -% xlabel('Context') -% xlim([0 0.5]); ylim([0 0.5]) -% grid on - -%Separated by monkey -Rsq_teo_neurons_amos = cat(1,Rsq_teo{a_sessions}); -Rsq_vlpfc_neurons_amos = cat(1,Rsq_vlpfc{a_sessions}); -Rsq_teo_neurons_hooke = cat(1,Rsq_teo{h_sessions}); -Rsq_vlpfc_neurons_hooke = cat(1,Rsq_vlpfc{h_sessions}); - -figure; -subplot(1,2,1); hold on -scatter(Rsq_teo_neurons_amos(:,3), Rsq_teo_neurons_amos(:,2),'filled','r') -scatter(Rsq_vlpfc_neurons_amos(:,3), Rsq_vlpfc_neurons_amos(:,2),'filled','b') -plot([0 0.5],[0 0.5],'k','DisplayName','Diagonal'); -ylabel('Behavior') -xlabel('Context') -xlim([0 0.5]); ylim([0 0.5]) -title('Amos') -grid on -ax = gca; -ax.FontSize = 16; - -subplot(1,2,2); hold on -scatter(Rsq_teo_neurons_hooke(:,3), Rsq_teo_neurons_hooke(:,2),'filled','r') -scatter(Rsq_vlpfc_neurons_hooke(:,3), Rsq_vlpfc_neurons_hooke(:,2),'filled','b') -legend({'TEO','vlPFC'}, 'Location','best') -plot([0 0.5],[0 0.5],'k','DisplayName','Diagonal'); -ylabel('Behavior') -xlabel('Context') -xlim([0 0.5]); ylim([0 0.5]) -title('Hooke') -grid on -ax = gca; -ax.FontSize = 16; \ No newline at end of file diff --git a/Varia/GLM/old_code/log_glm_behav_vs_context_avgAcrossRegressors.m b/Varia/GLM/old_code/log_glm_behav_vs_context_avgAcrossRegressors.m deleted file mode 100644 index 13348ee..0000000 --- a/Varia/GLM/old_code/log_glm_behav_vs_context_avgAcrossRegressors.m +++ /dev/null @@ -1,201 +0,0 @@ -%Extract mean and STD behaviors and neurons - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -threat_precedence =1; -exclude_sq=0; -plot_toggle=0; -warning('off','all') - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/Example_units']; - - %% Load data - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq ); - end - - disp('Data Loaded') - - Spike_count_raster = zscore(Spike_rasters'); - - %Extract behavior labels - behavior_labels= cell2mat({labels{:,3}}');%Get behavior label from labels structure - context = cell2mat({labels{:,12}}'); context_categ={"female","male","alone"}; - paired_or_not = cell2mat({labels{:,13}}'); - - %% Select behaviors to visualize - - %Extract behavior labels and frequency - behavior_labels = cell2mat({labels{:,3}}'); - - %Extract block labels - block_labels = cell2mat({labels{:,12}}'); - block_categ={"F","M","Alone"}; - - % Select behaviors - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - %Remove behaviors we're not interested in for now - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding Rowdy Room which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize'))); - - % OR select behaviors manually - %behav =[29] ;%unique(behavior_labels); %[4,5,7,8,9,10,24];% [4:10, 23]; %[4:8,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only consider indices with behavior of interest - idx= find(ismember(behavior_labels,behav));% & ismember(block_labels,3)); - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_labels_final = block_labels(idx); - - %% Create dummy variables - behavior_final = dummyvar(categorical(behavior_labels)); %behavior_final = behavior_final(:,[7,8,9]); - context_final = dummyvar(categorical(context));%Same as above but in behavior labels - all_predictors=[behavior_final(:,1:size(behavior_final,2)-1), context_final(:,[1,3])]; - - for unit = 1:size(Spike_count_raster,2) - - NeuralResponse = Spike_count_raster(:,unit);%Only keep timepoints where the behaviors of interest occur in spiking data - - %Full fit - mdl =fitlm(all_predictors,NeuralResponse); - Rsq_full{s}(unit)=mdl.Rsquared.Ordinary; - % RsqAdj{s}(unit,1)=mdl.Rsquared.Adjusted; - - %Average across behavior regressors - for reg = 1:size(all_predictors,2) - - pred_mat = all_predictors; - idx_reg = ~ismember(1:size(all_predictors,2), reg); - pred_mat(:, idx_reg) = all_predictors(randsample(size(behavior_final,1),size(behavior_final,1)), idx_reg); - - %Only considering behavior - mdl =fitlm(pred_mat,NeuralResponse); - Rsq{s}(unit,reg)=mdl.Rsquared.Ordinary; - end - - Rsq_full{s}(unit,2) = mean(Rsq{s}(unit,1:size(behavior_final,2)-1)); - Rsq_full{s}(unit,3) = mean(Rsq{s}(unit,end-1:end)); - - end %end of units - - Rsq_teo{s} = Rsq_full{s}(strcmp(brain_label,'TEO'),:); - Rsq_vlpfc{s} = Rsq_full{s}(strcmp(brain_label,'vlPFC'),:); - - disp(s) - disp('done.') - -end %end of session - -cd(['~/Dropbox (Penn)/Datalogger/Results/All_sessions/SingleUnit_results/']); -save('Glm_context_behav_avgAcrossReg.mat', "Rsq_full","Rsq_teo","Rsq_vlpfc","a_sessions","h_sessions","behav_categ") -load('Glm_context_behav_avgAcrossReg.mat') - -Rsq_all_neurons = cat(1,Rsq_full{:}); -Rsq_teo_neurons = cat(1,Rsq_teo{:}); -Rsq_vlpfc_neurons = cat(1,Rsq_vlpfc{:}); - -% %Pooled monkeys -% figure; hold on -% histogram(Rsq_all_neurons(:,1)); histogram(Rsq_all_neurons(:,2)); histogram(Rsq_all_neurons(:,3)) -% -% figure; hold on -% scatter(Rsq_teo_neurons(:,3), Rsq_teo_neurons(:,2),'filled','r') -% scatter(Rsq_vlpfc_neurons(:,3), Rsq_vlpfc_neurons(:,2),'filled','b') -% plot([0 0.5],[0 0.5],'k','DisplayName','Diagonal'); -% ylabel('Behavior') -% xlabel('Context') -% xlim([0 0.5]); ylim([0 0.5]) -% grid on - -%Separated by monkey -Rsq_teo_neurons_amos = cat(1,Rsq_teo{a_sessions}); -Rsq_vlpfc_neurons_amos = cat(1,Rsq_vlpfc{a_sessions}); -Rsq_teo_neurons_hooke = cat(1,Rsq_teo{h_sessions}); -Rsq_vlpfc_neurons_hooke = cat(1,Rsq_vlpfc{h_sessions}); - -figure; -subplot(1,2,1); hold on -scatter(Rsq_teo_neurons_amos(:,3), Rsq_teo_neurons_amos(:,2),'filled','r') -scatter(Rsq_vlpfc_neurons_amos(:,3), Rsq_vlpfc_neurons_amos(:,2),'filled','b') -plot([0 0.5],[0 0.5],'k','DisplayName','Diagonal'); -ylabel('Behavior') -xlabel('Context') -xlim([0 0.2]); ylim([0 0.2]) -title('Amos') -grid on -ax = gca; -ax.FontSize = 16; - -subplot(1,2,2); hold on -scatter(Rsq_teo_neurons_hooke(:,3), Rsq_teo_neurons_hooke(:,2),'filled','r') -scatter(Rsq_vlpfc_neurons_hooke(:,3), Rsq_vlpfc_neurons_hooke(:,2),'filled','b') -plot([0 0.5],[0 0.5],'k','DisplayName','Diagonal'); -ylabel('Behavior') -xlabel('Context') -xlim([0 0.2]); ylim([0 0.2]) -title('Hooke') -grid on -ax = gca; -ax.FontSize = 16; \ No newline at end of file diff --git a/Varia/GLM/old_code/log_glm_behav_vs_context_avgAcrossRegressors_behavSample.m b/Varia/GLM/old_code/log_glm_behav_vs_context_avgAcrossRegressors_behavSample.m deleted file mode 100644 index 8b66b29..0000000 --- a/Varia/GLM/old_code/log_glm_behav_vs_context_avgAcrossRegressors_behavSample.m +++ /dev/null @@ -1,211 +0,0 @@ -%Extract mean and STD behaviors and neurons - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -threat_precedence =1; -exclude_sq=1; -plot_toggle=0; -warning('off','all') - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/Example_units']; - - %% Load data - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, threat_precedence, exclude_sq ); - end - - disp('Data Loaded') - - Spike_count_raster = zscore(Spike_rasters'); - - %Extract behavior labels - behavior_labels= cell2mat({labels{:,3}}');%Get behavior label from labels structure - context = cell2mat({labels{:,12}}'); context_categ={"female","male","alone"}; - paired_or_not = cell2mat({labels{:,13}}'); - - %% Select behaviors to visualize - - %Extract behavior labels and frequency - behavior_labels = cell2mat({labels{:,3}}'); - - %Extract block labels - block_labels = cell2mat({labels{:,12}}'); - block_categ={"F","M","Alone"}; - - % Select behaviors - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - behav_freq_table = behav_freq_table(behav_freq_table(:,1)~=length(behav_categ),:); % Discard 0 (non-defined behaviors) - - % Select behaviors with a minimum # of occurrences - min_occurrences = 30; - behav = behav_freq_table(behav_freq_table(:,2)>=min_occurrences,1);%Get behaviors with a min number of occurrences - - %Remove behaviors we're not interested in for now - behav = behav(behav~=find(matches(behav_categ,'Proximity')));%excluding proximity which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Scratch')));%excluding scratch which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rowdy Room')));%excluding Rowdy Room which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Rest')));%excluding rest which is a source of confusion. - behav = behav(behav~=find(matches(behav_categ,'Other monkeys vocalize'))); - - % OR select behaviors manually - %behav =[29] ;%unique(behavior_labels); %[4,5,7,8,9,10,24];% [4:10, 23]; %[4:8,17]; %manually select behaviors of interest - - %Print behaviors selected - behavs_eval = behav_categ(behav); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - fprintf('Behaviors evaluated are: %s \n', behavs_eval); - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - %Only consider indices with behavior of interest - idx= find(ismember(behavior_labels,behav));% & ismember(block_labels,3)); - Spike_count_raster_final = Spike_count_raster(idx,:);%Only keep timepoints where the behaviors of interest occur in spiking data - behavior_labels_final = behavior_labels(idx);%Same as above but in behavior labels - block_labels_final = block_labels(idx); - - %% Create dummy variables - behavior_final = dummyvar(categorical(behavior_labels_final)); %behavior_final = behavior_final(:,[7,8,9]); - context_final = dummyvar(categorical(block_labels_final));%Same as above but in behavior labels - all_predictors=[behavior_final(:,1:size(behavior_final,2)-1), context_final(:,[1,3])]; - - for unit = 1:size(Spike_count_raster,2) - - NeuralResponse = Spike_count_raster_final(:,unit);%Only keep timepoints where the behaviors of interest occur in spiking data - - %Full fit - mdl =fitlm(all_predictors,NeuralResponse); - Rsq_full{s}(unit)=mdl.Rsquared.Ordinary; - % RsqAdj{s}(unit,1)=mdl.Rsquared.Adjusted; - - %Average across behavior regressors - for reg = 1:size(all_predictors,2) - - pred_mat = all_predictors; - idx_reg = ~ismember(1:size(all_predictors,2), reg); - pred_mat(:, idx_reg) = all_predictors(randsample(size(behavior_final,1),size(behavior_final,1)), idx_reg); - - %Only considering behavior - mdl =fitlm(pred_mat,NeuralResponse); - Rsq{s}(unit,reg)=mdl.Rsquared.Ordinary; - end - - Rsq_full{s}(unit,2) = max(Rsq{s}(unit,1:size(behavior_final,2)-1)); - Rsq_full{s}(unit,3) = max(Rsq{s}(unit,end-1:end)); - - end %end of units - - Rsq_teo{s} = Rsq_full{s}(strcmp(brain_label,'TEO'),:); - Rsq_vlpfc{s} = Rsq_full{s}(strcmp(brain_label,'vlPFC'),:); - - disp(s) - disp('done.') - -end %end of session - -cd(['~/Dropbox (Penn)/Datalogger/Results/All_sessions/SingleUnit_results/']); -save('Glm_context_behav_maxAcrossReg.mat', "Rsq_full","Rsq_teo","Rsq_vlpfc","a_sessions","h_sessions","behav_categ") -load('Glm_context_behav_maxAcrossReg.mat') - -Rsq_all_neurons = cat(1,Rsq_full{:}); -Rsq_teo_neurons = cat(1,Rsq_teo{:}); -Rsq_vlpfc_neurons = cat(1,Rsq_vlpfc{:}); - -%Pooled monkeys -limits = [0 0.5]; -figure; hold on -histogram(Rsq_all_neurons(:,1)); histogram(Rsq_all_neurons(:,2)); histogram(Rsq_all_neurons(:,3)) - -figure; -subplot(1,2,1); hold on -scatter(Rsq_teo_neurons(:,3), Rsq_teo_neurons(:,2),'filled','r') -plot(limits,limits,'k','DisplayName','Diagonal'); -ylabel('Behavior') -xlabel('Context'); title('TEO') -xlim(limits); ylim(limits) -grid on - -subplot(1,2,2); hold on -scatter(Rsq_vlpfc_neurons(:,3), Rsq_vlpfc_neurons(:,2),'filled','b') -plot(limits,limits,'k','DisplayName','Diagonal'); -ylabel('Behavior') -xlabel('Context'); title('vlPFC') -xlim(limits); ylim(limits) -grid on - -%Separated by monkey -Rsq_teo_neurons_amos = cat(1,Rsq_teo{a_sessions}); -Rsq_vlpfc_neurons_amos = cat(1,Rsq_vlpfc{a_sessions}); -Rsq_teo_neurons_hooke = cat(1,Rsq_teo{h_sessions}); -Rsq_vlpfc_neurons_hooke = cat(1,Rsq_vlpfc{h_sessions}); - -figure; -subplot(1,2,1); hold on -scatter(Rsq_teo_neurons_amos(:,3), Rsq_teo_neurons_amos(:,2),'filled','r') -scatter(Rsq_vlpfc_neurons_hooke(:,3), Rsq_vlpfc_neurons_hooke(:,2),'filled','b') -plot(limits,limits,'k','DisplayName','Diagonal'); -ylabel('Behavior') -xlabel('Context') -xlim(limits); ylim(limits) -title('TEO') -grid on -ax = gca; -ax.FontSize = 16; - -subplot(1,2,2); hold on -scatter(Rsq_vlpfc_neurons_hooke(:,3), Rsq_vlpfc_neurons_hooke(:,2),'filled','r') -scatter(Rsq_vlpfc_neurons_amos(:,3), Rsq_vlpfc_neurons_amos(:,2),'filled','b') -legend({'Hooke','Amos'}, 'Location','best') -plot(limits,limits,'k','DisplayName','Diagonal'); -ylabel('Behavior') -xlabel('Context') -xlim(limits); ylim(limits) -title('vlPFC') -grid on -ax = gca; -ax.FontSize = 16; \ No newline at end of file diff --git a/Varia/GLM/old_code/log_glm_socialContext.m b/Varia/GLM/old_code/log_glm_socialContext.m deleted file mode 100644 index 16a2a03..0000000 --- a/Varia/GLM/old_code/log_glm_socialContext.m +++ /dev/null @@ -1,189 +0,0 @@ -%Extract mean and STD behaviors and neurons - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -agg_precedence =0; -num_iter=100; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/Example_units']; - - %% Load data - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence ); - end - - disp('Data Loaded') - clear ypred - - Spike_count_raster = zscore(Spike_rasters'); - - %Extract behavior labels - behavior_labels= cell2mat({labels{:,3}}');%Get behavior label from labels structure - context = cell2mat({labels{:,12}}'); - paired_or_not = cell2mat({labels{:,13}}'); - - boi =[5,7,8,9,10,16]; - - - - %% Alone or paired - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - num_occurrence = behav_freq_table(boi,2); - min_occurrence = min(num_occurrence); - - for b=1:length(boi) - - idx= find(ismember(behavior_labels,boi(b))); %find the indices of the behaviors considered - num_samples(b) = length(idx); - - unq_blocks = unique(paired_or_not(idx)); - - if length(unq_blocks)>1 - - for i=1:num_iter - idx_final = randsample(idx, min_occurrence); - - for unit = 1:size(Spike_count_raster,2) - - Spike_count_raster_final = Spike_count_raster(idx_final,unit);%Only keep timepoints where the behaviors of interest occur in spiking data - context_final = dummyvar(categorical(paired_or_not(idx_final)));%Same as above but in behavior labels - context_mdl = context_final(:,1);%:size(context_final,2)-1); - - %[p,tbl,stats] = anova1(Spike_count_raster_final,context_mdl) ; - - mdl = fitlm(context_mdl,Spike_count_raster_final); - ypred(:,unit) = predict(mdl,context_mdl); - - end - - Rsq_paired_or_not(b,i)= corr2(Spike_count_raster(idx_final,:),ypred).^2; - figure; hold on; plot(Spike_count_raster(idx_final,unit)); plot(ypred(:,unit)) - for unit = 1:41 - r(unit)=corr2(test(unit,:),ypred(unit,:)); - end - figure; hist(r); mean(r) - corr2(ypred,test) - figure; imagesc(ypred); figure; imagesc(test) - - disp(['iter ' num2str(i)]) - end - end - - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - disp([behav_categ((boi(b))) " done."]) - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - clear ypred - end - - Rsq_paired_or_not(Rsq_paired_or_not==0)=nan; - Rsq_paired_or_not_mean{s} = nanmean(Rsq_paired_or_not,2); - %[Rsq_paired_or_not_mean{s}, num_samples'] - - - - %% Neighbor ID - - clear ypred - behavior_labels_neighborID= behavior_labels(context<3);%Get behavior label from labels structure - context_neighborID = context(context<3); - Spike_count_raster_neighborID = Spike_count_raster(context<3, :); - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels_neighborID); - num_occurrence = behav_freq_table(boi,2); - min_occurrence = min(num_occurrence(num_occurrence>0)); - - boi_paired = boi(num_occurrence>0); - - for b=1:length(boi_paired) - - idx = find(ismember(behavior_labels_neighborID,boi_paired(b))); %find the indices of the behaviors considered - - unq_blocks = unique(context_neighborID(idx)); - - if length(unq_blocks)>1 - for i=1:num_iter - idx_final = randsample(idx, min_occurrence); - - for unit = 1:size(Spike_count_raster,2) - - Spike_count_raster_final = Spike_count_raster_neighborID(idx_final,unit);%Only keep timepoints where the behaviors of interest occur in spiking data - context_final = dummyvar(categorical(context_neighborID (idx_final)));%Same as above but in behavior labels - context_mdl = context_final(:,1);%:size(context_final,2)-1); - - mdl = fitlm(context_mdl,Spike_count_raster_final); - ypred(:,unit) = predict(mdl,Spike_count_raster_final); - - end - - Rsq_neighborID(b,i)= corr2(Spike_count_raster_neighborID(idx_final,:),ypred).^2; - - disp(['iter ' num2str(i)]) - end - end - - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - disp([behav_categ((boi_paired(b))) " done."]) - disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%') - - clear ypred - end - - Rsq_neighborID(Rsq_neighborID==0)=nan; - Rsq_neighborID_mean{s} = nanmean(Rsq_neighborID,2); - %[Rsq_neighborID_mean{s}, num_samples'] - -end -cd("~/Desktop/") -save("rsq_context.mat", "Rsq_neighborID_mean","Rsq_paired_or_not_mean") - -cell2mat(Rsq_paired_or_not_mean) -cell2mat(Rsq_neighborID_mean) diff --git a/Varia/GLM/old_code/log_glm_socialContext_full.m b/Varia/GLM/old_code/log_glm_socialContext_full.m deleted file mode 100644 index 5a80818..0000000 --- a/Varia/GLM/old_code/log_glm_socialContext_full.m +++ /dev/null @@ -1,268 +0,0 @@ -%Extract mean and STD behaviors and neurons - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -agg_precedence =0; -num_iter=100; -plot_toggle=0; -warning('off','all') - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/Example_units']; - - %% Load data - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence ); - end - - disp('Data Loaded') - - Spike_count_raster = zscore(Spike_rasters'); - - %Extract behavior labels - behavior_labels= cell2mat({labels{:,3}}');%Get behavior label from labels structure - context = cell2mat({labels{:,12}}'); context_categ={"female","male","alone"}; - paired_or_not = cell2mat({labels{:,13}}'); - - boi =[5,7,8,9,10,16,24]; - - %Simulate fake labels - [sim_behav] = GenSimBehavior(behavior_labels,behav_categ, temp_resolution, 1); - [sim_context] = randsample(context, length(context)); - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - num_occurrence = behav_freq_table(boi,2); - min_occurrence = min(num_occurrence); - - tic - - %% Real data - - idx_final=[]; idx_total=[]; - for b=1:length(boi) - - idx= find(ismember(behavior_labels,boi(b))); %find the indices of the behaviors considered - num_samples(b)=length(idx); - idx_subsample = randsample(idx, min_occurrence); - idx_final = [idx_final; idx_subsample]; - idx_total = [idx_total; idx]; - - end - - idx_final=idx_total; - - behavior_final = dummyvar(categorical(behavior_labels(idx_final))); - context_final = dummyvar(categorical(context(idx_final)));%Same as above but in behavior labels - - %Behaviors - Foraging = behavior_final(:,1); - GroomGive = behavior_final(:,2); - GroomGet = behavior_final(:,3); - ThreatPartner = behavior_final(:,4); - ThreatSubject = behavior_final(:,5); - Travel = behavior_final(:,6); - Rest = behavior_final(:,7); - - %Context - NeighborF = context_final(:,1); - Alone = context_final(:,3); - - %Interaction - Foraging_alone = Foraging.*Alone; - ThreatPartner_alone = ThreatPartner.*Alone; - ThreatSubject_alone = ThreatSubject.*Alone; - Travel_alone = Travel.*Alone; - - Foraging_neighborF = Foraging.*NeighborF; - GroomGive_neighborF = GroomGive.*NeighborF; - GroomGet_neighborF = GroomGet.*NeighborF; - ThreatPartner_neighborF = ThreatPartner.*NeighborF; - ThreatSubject_neighborF = ThreatSubject.*NeighborF; - Travel_neighborF = Travel.*NeighborF; - - predictors_mat = [Foraging, GroomGive, GroomGet, ThreatPartner,... - ThreatSubject, Travel, NeighborF, Alone, ... - Foraging_alone,ThreatPartner_alone,ThreatSubject_alone,Travel_alone, ... - Foraging_neighborF,GroomGive_neighborF,GroomGet_neighborF, ... - ThreatPartner_neighborF, ThreatSubject_neighborF, Travel_neighborF]; - - - for unit = 1:size(Spike_count_raster,2) - - NeuralResponse = Spike_count_raster(idx_final,unit);%Only keep timepoints where the behaviors of interest occur in spiking data - - mdl =fitlm(predictors_mat,NeuralResponse); - rsqfull(unit)=mdl.Rsquared.Ordinary; - - for pred = 1:size(predictors_mat,2) - - reduced_model = predictors_mat; - reduced_model(:,pred)=[]; - - mdl =fitlm(reduced_model,NeuralResponse); - rsq_unique(unit, pred)=(rsqfull(unit)-mdl.Rsquared.Ordinary)/rsqfull(unit); - - end - - end %end of units - rsq_unique(rsq_unique==0)=nan; - - - %% Null data - - idx_final=[]; idx_total=[]; - for b=1:length(boi) - - idx= find(ismember(sim_behav,boi(b))); %find the indices of the behaviors considered - idx_total = [idx_total; idx]; - - end - - idx_final=idx_total; - - behavior_final = dummyvar(categorical(sim_behav(idx_final))); - context_final = dummyvar(categorical(sim_context(idx_final)));%Same as above but in behavior labels - - %Behaviors - Foraging = behavior_final(:,1); - GroomGive = behavior_final(:,2); - GroomGet = behavior_final(:,3); - ThreatPartner = behavior_final(:,4); - ThreatSubject = behavior_final(:,5); - Travel = behavior_final(:,6); - Rest = behavior_final(:,7); - - %Context - NeighborF = context_final(:,1); - Alone = context_final(:,3); - - %Interaction - Foraging_alone = Foraging.*Alone; - ThreatPartner_alone = ThreatPartner.*Alone; - ThreatSubject_alone = ThreatSubject.*Alone; - Travel_alone = Travel.*Alone; - - Foraging_neighborF = Foraging.*NeighborF; - GroomGive_neighborF = GroomGive.*NeighborF; - GroomGet_neighborF = GroomGet.*NeighborF; - ThreatPartner_neighborF = ThreatPartner.*NeighborF; - ThreatSubject_neighborF = ThreatSubject.*NeighborF; - Travel_neighborF = Travel.*NeighborF; - - predictors_mat = [Foraging, GroomGive, GroomGet, ThreatPartner,... - ThreatSubject, Travel, NeighborF, Alone, ... - Foraging_alone,ThreatPartner_alone,ThreatSubject_alone,Travel_alone, ... - Foraging_neighborF,GroomGive_neighborF,GroomGet_neighborF, ... - ThreatPartner_neighborF, ThreatSubject_neighborF, Travel_neighborF]; - - - for unit = 1:size(Spike_count_raster,2) - - NeuralResponse = Spike_count_raster(idx_final,unit);%Only keep timepoints where the behaviors of interest occur in spiking data - - mdl =fitlm(predictors_mat,NeuralResponse); - rsqfull_null(unit)=mdl.Rsquared.Ordinary; - - for pred = 1:size(predictors_mat,2) - - reduced_model = predictors_mat; - reduced_model(:,pred)=[]; - - mdl =fitlm(reduced_model,NeuralResponse); - rsq_unique_null(unit, pred)=(rsqfull_null(unit)-mdl.Rsquared.Ordinary)/rsqfull_null(unit); - - end - - end %end of units - rsq_unique_null(rsq_unique_null==0)=nan; - - - toc - - rsq_unique_mean= mean(rsq_unique); - nanstd(rsq_unique) - figure; bar(rsq_unique_mean) - ylabel('Proportion of full model') - xticks([1:18]) - xticklabels({'Foraging', 'GroomGive', 'GroomGet', 'ThreatPartner',... - 'ThreatSubject', 'Travel', 'NeighborF', 'Alone', ... - 'Foraging_alone','ThreatPartner_alone','ThreatSubject_alone','Travel_alone', ... - 'Foraging_neighborF','GroomGive_neighborF','GroomGet_neighborF', ... - 'ThreatPartner_neighborF', 'ThreatSubject_neighborF', 'Travel_neighborF'}) - title('Real fit') - - rsq_unique_mean_null= mean(rsq_unique_null); - nanstd(rsq_unique_null) - figure; bar(rsq_unique_mean_null) - ylabel('Proportion of full model') - xticks([1:18]) - xticklabels({'Foraging', 'GroomGive', 'GroomGet', 'ThreatPartner',... - 'ThreatSubject', 'Travel', 'NeighborF', 'Alone', ... - 'Foraging_alone','ThreatPartner_alone','ThreatSubject_alone','Travel_alone', ... - 'Foraging_neighborF','GroomGive_neighborF','GroomGet_neighborF', ... - 'ThreatPartner_neighborF', 'ThreatSubject_neighborF', 'Travel_neighborF'}) - title('Null fit') - - rsq_unique_adjusted = rsq_unique_mean - rsq_unique_mean_null; - figure; bar(rsq_unique_adjusted) - ylabel('Proportion of full model') - xticks([1:18]) - xticklabels({'Foraging', 'GroomGive', 'GroomGet', 'ThreatPartner',... - 'ThreatSubject', 'Travel', 'NeighborF', 'Alone', ... - 'Foraging_alone','ThreatPartner_alone','ThreatSubject_alone','Travel_alone', ... - 'Foraging_neighborF','GroomGive_neighborF','GroomGet_neighborF', ... - 'ThreatPartner_neighborF', 'ThreatSubject_neighborF', 'Travel_neighborF'}) - title('Adjusted fit') - -end %end of session - -cd("~/Desktop/") -save("rsq_context.mat", "Rsq_neighborID_mean","Rsq_paired_or_not_mean") - -cell2mat(Rsq_paired_or_not_mean) -cell2mat(Rsq_neighborID_mean) diff --git a/Varia/GLM/old_code/log_glm_socialContext_subsample.m b/Varia/GLM/old_code/log_glm_socialContext_subsample.m deleted file mode 100644 index eb03991..0000000 --- a/Varia/GLM/old_code/log_glm_socialContext_subsample.m +++ /dev/null @@ -1,173 +0,0 @@ -%Extract mean and STD behaviors and neurons - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1*temp_resolution;%set the smoothing window size (sigma) -agg_precedence =0; -num_iter=1000; -null=0; -plot_toggle=1; -warning('off','all') - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/Example_units']; - - %% Load data - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma, agg_precedence ); - end - - disp('Data Loaded') - clear ypred - - Spike_count_raster = zscore(Spike_rasters'); - - %Extract behavior labels - behavior_labels= cell2mat({labels{:,3}}');%Get behavior label from labels structure - context = cell2mat({labels{:,12}}'); context_categ={"female","male","alone"}; - paired_or_not = cell2mat({labels{:,13}}'); - - boi =[5,7,8,9,10,16,24]; - - %Compute freq of behavior for the session - behav_freq_table = tabulate(behavior_labels); - num_occurrence = behav_freq_table(boi,2); - min_occurrence = min(num_occurrence); - - tic - for iter=1:num_iter - - idx_final=[]; - for b=1:length(boi) - - idx= find(ismember(behavior_labels,boi(b))); %find the indices of the behaviors considered - num_samples(b)=length(idx); - idx_subsample = randsample(idx, min_occurrence); - idx_final = [idx_final; idx_subsample]; - - end - - - behavior_final = dummyvar(categorical(behavior_labels(idx_final))); - context_final = dummyvar(categorical(context(idx_final)));%Same as above but in behavior labels - - %Behaviors - Foraging = behavior_final(:,1); - GroomGive = behavior_final(:,2); - GroomGet = behavior_final(:,3); - ThreatPartner = behavior_final(:,4); - ThreatSubject = behavior_final(:,5); - Travel = behavior_final(:,6); - Rest = behavior_final(:,7); - - %Context - NeighborF = context_final(:,1); - Alone = context_final(:,3); - - %Interaction - Foraging_alone = Foraging.*Alone; - ThreatPartner_alone = ThreatPartner.*Alone; - ThreatSubject_alone = ThreatSubject.*Alone; - Travel_alone = Travel.*Alone; - - Foraging_neighborF = Foraging.*NeighborF; - GroomGive_neighborF = GroomGive.*NeighborF; - GroomGet_neighborF = GroomGet.*NeighborF; - ThreatPartner_neighborF = ThreatPartner.*NeighborF; - ThreatSubject_neighborF = ThreatSubject.*NeighborF; - Travel_neighborF = Travel.*NeighborF; - - predictors_mat = [Foraging, GroomGive, GroomGet, ThreatPartner,... - ThreatSubject, Travel, NeighborF, Alone, ... - Foraging_alone,ThreatPartner_alone,ThreatSubject_alone,Travel_alone, ... - Foraging_neighborF,GroomGive_neighborF,GroomGet_neighborF, ... - ThreatPartner_neighborF, ThreatSubject_neighborF, Travel_neighborF]; - - - for unit = 1:20%size(Spike_count_raster,2) - - NeuralResponse = Spike_count_raster(idx_final,unit);%Only keep timepoints where the behaviors of interest occur in spiking data - - mdl =fitlm(predictors_mat,NeuralResponse); - rsqfull(unit, iter)=mdl.Rsquared.Ordinary; - - for pred = 1:size(predictors_mat,2) - - reduced_model = predictors_mat; - reduced_model(:,pred)=[]; - - mdl =fitlm(reduced_model,NeuralResponse); - rsq_unique{iter}(unit, pred)=(rsqfull(unit, iter)-mdl.Rsquared.Ordinary)/rsqfull(unit, iter); - - end - - end %end of units - rsq_unique{iter}(rsq_unique{iter}==0)=nan; - - disp(['iter ' num2str(iter) ' done.']) - end %end of iterations - toc - - mean_across_iter = nanmean(cat(3,rsq_unique{:}),3); std_across_iter = nanstd(cat(3,rsq_unique{:}),[],3); - std_across_iter./mean_across_iter - - rsq_unique_mean=nanmean(mean_across_iter); - nanstd(cat(3,rsq_unique{:}),[],3) - figure; bar(rsq_unique_mean) - xticks([1:18]) - xticklabels({'Foraging', 'GroomGive', 'GroomGet', 'ThreatPartner',... - 'ThreatSubject', 'Travel', 'NeighborF', 'Alone', ... - 'Foraging_alone','ThreatPartner_alone','ThreatSubject_alone','Travel_alone', ... - 'Foraging_neighborF','GroomGive_neighborF','GroomGet_neighborF', ... - 'ThreatPartner_neighborF', 'ThreatSubject_neighborF', 'Travel_neighborF'}) - -end %end of session - -cd("~/Desktop/") -save("rsq_context.mat", "Rsq_neighborID_mean","Rsq_paired_or_not_mean") - -cell2mat(Rsq_paired_or_not_mean) -cell2mat(Rsq_neighborID_mean) diff --git a/Varia/GrangerCausality_PredictiveActivity/GC_develop.m b/Varia/GrangerCausality_PredictiveActivity/GC_develop.m deleted file mode 100644 index 4f9874c..0000000 --- a/Varia/GrangerCausality_PredictiveActivity/GC_develop.m +++ /dev/null @@ -1,23 +0,0 @@ -%% Running notes - -%2022-10-20 -%From first readings fairly straightforward to try this. Main issue is -%method only seems to work if data is stationary. There are transforms we -%can use to make data stationary but we can't apply these to our -%catagorical data. First use the built in tools to check if we have issues -%with stationarity. If we don't have issues give this a go to see what -%results we get. If we do have issues, see again if we can find some kind -%of forced markov model where the transition probabilities are a continuous -%function of the neural data. Though that does sound a hell of a lot like -%just redriving multinomial regression/if mnr didn't work then I'm not sure -%this will work. - -%% Load data. - -%% Test stainoarity. - -%% Transform data if needed - -%% Plot data, run gctest - -%% Further interpretation \ No newline at end of file diff --git a/Varia/Hidden Markov Model/.DS_Store b/Varia/Hidden Markov Model/.DS_Store deleted file mode 100644 index 5008ddf..0000000 Binary files a/Varia/Hidden Markov Model/.DS_Store and /dev/null differ diff --git a/Varia/Hidden Markov Model/calculateHMMfinal.m b/Varia/Hidden Markov Model/calculateHMMfinal.m deleted file mode 100644 index 896442c..0000000 --- a/Varia/Hidden Markov Model/calculateHMMfinal.m +++ /dev/null @@ -1,546 +0,0 @@ -function HMM = calculateHMM(spikes, dt, averageStateDuration, verbose, ... - version, randInit, nStatesFinal, nStatesVector, nRepetitions, ... - timeSmoothFactor, normalization, timeSmoothFactor2) -%calculateHMM Hidden Markov Model analysis -% From discretized spiking data (nNeurons x nTimeSteps 2D matrix), -% extract symbol = which neuron is spiking at each time step (random choice -% between multiple neurons if > 1 neuron spike simultaneously; if no neuron -% spikes, null symbol). Then, Hidden Markov Model analysis on this -% 1 x nTimeSteps 1D vector of symbols (= which neurons spike). -% Initial guess for HMM transition & emission matrix, then optimization. -% Input: -% - spikes: nNeurons x nTimeSteps 2D binary matrix, containing -% discretized spike data (did neuron N spike during time bin T, yes -% or no?) -% - dt: num scalar, width of time step / time bin -% - averageStateDuration: num scalar, initial estimation of state period -% duration (in milliseconds) -% - verbose: bool scalar, print program working info or not -% - version: char, what version of data transformation to use. -% 'single spikes' or 'k-means'. Only use 'single spikes' = procedure -% described above (symbol = identity of 1 neuron spiking at each time -% step). Inspired by Seidemann / O'Halloran & Cronin. -% 'k-means' = attempt to define symbol through N dimensional -% network frequency space, where symbol = closest centroid in -% to vector of network frequency. Inspired by Abeles / Gat / Tishby -% algorithm. Doesn't seem to work. -% - randInit: bool scalar, random initialization of transition & emission -% matrix or not (see detail in code). -% - nStatesFinal: num scalar, final number of states to choose -% (irrespective of selection criteria, see code below for more info). -% - nStatesVector: num vector, multiple HMMs each with a different -% number of states as defined by this vector. -% - nRepetitions: num scalar, number of times to repeat HMMs for each -% number of state. -% - timeSmoothFactor: num scalar. Only useful for k-means algorithm, -% specifies milliseconds over which to smooth spikes for frequency -% estimation. Might not be fully compatible with latest code version. -% - normalization: char. Only useful for k-means algorithm, specifies -% frequency normalization method (subtract, divide) between frequency -% estimated with timeSmoothFactor and timeSmoothFactor2. Might not be -% fully compatible with latest code version. -% - timeSmoothFactor2: num scalar. Only useful for k-means algorithm, -% specifies milliseconds over which to smooth spikes for 2nd longer -% frequency estimation. Might not be fully compatible with latest -% code version. -% Output: -% - HMM: struct scalar, contains many HMM information (the observables, -% the states, the probability of states, the transition / emission -% matrices, etc.) - -%% Handle arguments -assert(islogical(spikes) && ~isscalar(spikes), ... - '1st argument (spikes) must be a logical vector or matrix'); - -assert(isnumeric(dt) && ~isnan(dt) && ~isinf(dt) && isscalar(dt), ... - '2nd argument (dt) must be a valid numeric scalar.'); - -if ~exist('averageStateDuration', 'var') || isempty(averageStateDuration) - averageStateDuration = 300; -end -assert(isnumeric(averageStateDuration) && ~isnan(averageStateDuration) && ... - ~isinf(averageStateDuration) && isscalar(averageStateDuration), ... - '3rd argument (averageStateDuration) must be a valid numeric scalar.'); - -if ~exist('verbose', 'var') || isempty(verbose) - verbose = false; -end -assert(islogical(verbose) || ... - (isnumeric(verbose) && ~isnan(verbose) && ... - ~isinf(verbose) && isscalar(verbose) && ... - (verbose == 0 || verbose == 1)), ... - '4th argument (verbose) must be logical or numeric (1 or 0).'); - -if ~exist('version', 'var') || isempty(version) - version = 'single spikes'; -end -possibilities = {'single spikes'}; % {'single spikes', 'k-means'} -assert(ischar(version) && any(strcmp(version, possibilities)), ... - sprintf(['5th argument (version) must be one of the following ' ... - 'chars: ' repmat('''%s'', ', 1, length(possibilities)) '.'])); - -if ~exist('randInit', 'var') || isempty(randInit) - randInit = false; -end -assert(islogical(randInit) || ... - (isnumeric(randInit) && ~isnan(randInit) && ... - ~isinf(randInit) && isscalar(randInit) && ... - (randInit == 0 || randInit == 1)), ... - '6th argument (randInit) must be logical or numeric (1 or 0).'); - -if ~exist('nStatesFinal', 'var') || isempty(nStatesFinal) - nStatesFinal = 4; -end -assert(isnumeric(nStatesFinal) && ~isnan(nStatesFinal) && ... - ~isinf(nStatesFinal) && isscalar(nStatesFinal), ... - '7th argument (nStatesFinal) must be a valid numeric scalar.'); - -if ~exist('nStatesVector', 'var') || isempty(nStatesVector) - nStatesVector = nStatesFinal; -end -assert(isnumeric(nStatesVector) && all(~isnan(nStatesVector)) && ... - all(~isinf(nStatesVector)) && isvector(nStatesVector), ... - '8th argument (nStatesVector) must be a valid numeric vector or scalar.'); - -if ~exist('nRepetitions', 'var') || isempty(nRepetitions) - nRepetitions = 1; -end -assert(isnumeric(nRepetitions) && ~isnan(nRepetitions) && ... - ~isinf(nRepetitions) && isscalar(nRepetitions), ... - '9th argument (nRepetitions) must be a valid numeric scalar.'); - -if ~exist('timeSmoothFactor', 'var') || isempty(timeSmoothFactor) - timeSmoothFactor = 500; -end -assert(isnumeric(timeSmoothFactor) && ~isnan(timeSmoothFactor) && ... - ~isinf(timeSmoothFactor) && isscalar(timeSmoothFactor), ... - '10th argument (timeSmoothFactor) must be a valid numeric scalar.'); - -if ~exist('normalization', 'var') || isempty(normalization) - normalization = 'none'; -end -possibilities = {'none', 'subtract'}; -assert(ischar(normalization) && any(strcmp(normalization, possibilities)), ... - sprintf(['11th argument (normalization) must be one of the following ' ... - 'chars: ' repmat('''%s'', ', 1, length(possibilities)) '.'])); - -if ~exist('timeSmoothFactor2', 'var') || isempty(timeSmoothFactor2) - timeSmoothFactor2 = 60000; -end -assert(isnumeric(timeSmoothFactor2) && ~isnan(timeSmoothFactor2) && ... - ~isinf(timeSmoothFactor2) && isscalar(timeSmoothFactor2), ... - '12th argument (timeSmoothFactor2) must be a valid numeric scalar.'); - -%% Prepare observations temporal vector -if strcmp(version, 'single spikes') - %% Average number of spikes per time bin - nSpikes = 0:4; - nSpikesPerBin = NaN(1, length(nSpikes)); - - for s = 1:length(nSpikes) - n = nSpikes(s); - nSpikesPerBin(s) = mean(sum(spikes, 1) >= n & ... - sum(spikes, 1) < (n+1)); - end - pBinWithoutSpikes = nSpikesPerBin(nSpikes == 0); - - % --- Debugging plot --- - % figure; hold on; - % plot(nSpikes, nSpikesPerBin); - % plot(nSpikes, cumsum(nSpikesPerBin)); - - clear nSpikes nSpikesPerBin s n nSpikesPerBin - - %% Convert multi-variate spike trains to univariate spike idx - nTimeBins = size(spikes, 2); - nNeurons = size(spikes, 1); - nOutputs = nNeurons+1; - HMM.obs = ones(1, nTimeBins)*nOutputs; - for kt = 1:nTimeBins - if any(spikes(:, kt)) - spikeIdx = find(spikes(:, kt)); - if length(spikeIdx) == 1 - HMM.obs(kt) = spikeIdx; - else - HMM.obs(kt) = datasample(spikeIdx, 1); - end - end - end - - clear nTimeBins nNeurons kt spikeIdx -elseif strcmp(version, 'k-means') - ti = round(timeSmoothFactor/dt); - - % Bins -% spikes = spikes(:, 1:(round(end/ti)*ti)); -% rates = permute(sum(reshape(spikes, size(spikes, 1), ti, []), 2), ... -% [1 3 2]); - - %% Smooth - window = ones(1, ti)/ti; - rates = conv2NoPadding(spikes, window); - if strcmp(normalization, 'subtract') - ti2 = round(timeSmoothFactor2/dt); - window2 = ones(1, ti2)/ti2; - slow = conv2NoPadding(spikes, window2); - rates = rates - slow + mean(slow, 2); - % Other possibilities: high-pass filter, wavelet transform, - % fourier transform? - % - Wavelet transform gives same result as conv_fast - conv_slow, - % but has problems on edges. - % - High-pass filter is good sometimes, but crazy in other cases. - % Haven't tried fourier transform, but seems to have other - % problems (notably, edges). - % Thus, conv_fast - conv_slow is best. - end - - %% Verification code -% HMM = struct(); -% nNeurons = size(spikes, 1); -% rates2 = permute(sum(reshape(spikes(:, 1:floor(end/ti)*ti), ... -% size(spikes, 1), ti, []), 2), [1 3 2]); -% -% t = 0:timeSmoothFactor:1e3; -% f = @(tau) exp(-t/tau); -% options = optimoptions('fmincon', 'Display', 'off'); -% taus = NaN(nNeurons, 1); -% for n = 1:nNeurons -% temp = autocorr(rates2(n, :), 'NumLags', round(1e3/timeSmoothFactor)); -% error = @(tau) sqrt(sum((f(tau) - temp).^2)); -% taus(n) = fmincon(error, 100, [], [], [], [], [], [], [], ... -% options); -% end -% data = [std(rates, [], 2) ... -% mean(rates, 2) ... -% std(rates, [], 2) ./ mean(rates, 2) ... -% mean(abs(diff(rates, [], 2)), 2) ... -% taus]; -% data = median(data, 1); -% fprintf('%g ', data); -% fprintf('\n'); -% return; - - %% HMM - nOutputs = size(spikes, 1)*10; - [HMM.obs, clusters] = kmeans(rates', nOutputs, 'Replicates', 20); - HMM.obs = HMM.obs'; - - clear window rates spikes clusters -end - -% --- Debugging plot --- -% newSpikes = spikeIdxs; -% newSpikes(newSpikes == nOutputs) = NaN; -% [nAP, tAP] = find(spikes); -% figure; hold on; -% scatter(tAP, nAP, 6, 'r', 'filled'); -% scatter(1:nT, newSpikes, 6, 'k', 'filled'); - -%% Estimate transition & emission matrices -averageTransitionProb = 1/(averageStateDuration/dt); - -template = cell(length(nStatesVector), nRepetitions); -transitionMatriceCell = template; -emissionMatriceCell = template; -statesCell = template; -pStatesCell = template; -logProbSeqCell = NaN(length(nStatesVector), nRepetitions); -logLikCell = template; % My extension - -clear template - -for s = 1:length(nStatesVector) - nStates = nStatesVector(s); - fprintf('Number of states: %d. ', nStates); - for r = 1:nRepetitions - fprintf('Repetition = %d, ', r); - %% Transition matrix initialization: Si x Sj (Si --> Sj) - % Bias transition matrix towards staying in same state (diagonal), - % with average duration = averageStateDuration - diagonal = eye(nStates); - if randInit - randMat = 2 .* rand(nStates, nStates); % uniform [0 2] - else - randMat = ones(nStates, nStates); % all ones - end - transitionMatrixInit = ... - diagonal .* (1-averageTransitionProb) + ... Diagonal non-random - (1 - diagonal) .* ... - (averageTransitionProb / (nStates-1) .* randMat); - if ~verLessThan('matlab', '9.0') - transitionMatrixInit = transitionMatrixInit ./ ... - sum(transitionMatrixInit, 2); - else - transitionMatrixInit = transitionMatrixInit ./ ... - repmat(sum(transitionMatrixInit, 2), 1, nStates); - end - - %% Emission matrix initialization: S x O - % Biais emission matrix with high probability for no spike - emissionMatrixInit = NaN(nStates, nOutputs); - - if randInit - randMat = 2 .* rand(nStates, nOutputs); % uniform [0 2] - else - randMat = ones(nStates, nOutputs); % all ones - end - - if strcmp(version, 'single spikes') - emissionMatrixInit(:, end) = pBinWithoutSpikes; - randMat(:, end) = 1; % Spike absence non-random - emissionMatrixInit(:, 1:end-1) = ... - (1-pBinWithoutSpikes) / (nOutputs-1); - elseif strcmp(version, 'k-means') - emissionMatrixInit(:) = 1/nOutputs; - end - emissionMatrixInit = emissionMatrixInit .* randMat; - if ~verLessThan('matlab', '9.0') - emissionMatrixInit = emissionMatrixInit ./ ... - sum(emissionMatrixInit, 2); - else - emissionMatrixInit = emissionMatrixInit ./ ... - repmat(sum(emissionMatrixInit, 2), 1, nOutputs); - end - - %% Estimate transition & emission matrices - [transitionMatrix, emissionMatrix, loglik] = ... - hmmtrain(HMM.obs, transitionMatrixInit, emissionMatrixInit, ... - 'Verbose', verbose); % I added loglik - states = hmmviterbi(HMM.obs, transitionMatrix, emissionMatrix); - [pStates, logProbSeq] = ... - hmmdecode(HMM.obs, transitionMatrix, emissionMatrix); - - %% Compare values for different number of states - transitionMatriceCell{s, r} = transitionMatrix; - emissionMatriceCell{s, r} = emissionMatrix; - statesCell{s, r} = states; - pStatesCell{s, r} = pStates; - logProbSeqCell(s, r) = logProbSeq; - logLikCell{s, r} = loglik; % my addition - end - fprintf('\n'); -end - -clear s nStates r diagonal randMat transitionMatrixInit emissionMatrixInit -clear transitionMatrix emissionMatrix states pStates logProbSeq - -%% Choose best HMM for each number of states, then best number of states -% [AIC, BIC, p] = compareHMMNumberOfStates(nStatesVector, HMM.obs, ... -% logProbSeqCell, transitionMatriceCell, emissionMatriceCell, ... -% pStatesCell, statesCell, nOutputs, size(spikes, 2)); - -% Get best HMM (highest log-likelihood) for each number of states? -% Or just choose number of states? -% [~, repMax] = max(logProbSeqCell, [], 2); -% maxIdx = sub2ind(size(logProbSeqCell), 1:length(nStatesVector), repMax')'; -maxIdx = find(nStatesFinal == nStatesVector); - -transitionMatriceCell = transitionMatriceCell(maxIdx); -emissionMatriceCell = emissionMatriceCell(maxIdx); -statesCell = statesCell(maxIdx); -pStatesCell = pStatesCell(maxIdx); -logProbSeqCell = logProbSeqCell(maxIdx); -logLikCell = logLikCell(maxIdx); - -% Choose best number of states -stateIdx = nStatesVector == nStatesFinal; -HMM.transitionMatrix = transitionMatriceCell{stateIdx}; -HMM.emissionMatrix = emissionMatriceCell{stateIdx}; -HMM.states = statesCell{stateIdx}; -HMM.pStates = pStatesCell{stateIdx}; -HMM.logProbSeq = logProbSeqCell(stateIdx); -HMM.logLik = logLikCell{stateIdx}; - -clear repMax logProbSeqCell maxIdx nStatesVector transitionMatriceCell -clear emissionMatriceCell statesCell pStatesCell logProbSeqCell stateIdx - -%% Add null state -nullStateThreshold = 0.8; -existingStates = HMM.pStates > nullStateThreshold; -[~, HMM.newStates] = max(existingStates, [], 1); -nullState = ~any(existingStates, 1); -HMM.newStates(nullState) = 0; - -clear nullStateThreshold existingStates nullState - -%% Concatenate states when null state in between -paddedStates = [NaN HMM.newStates NaN]; -stateDiffs = diff(paddedStates); -newStateChangesIdx = find(stateDiffs); -newStateChangesIdx(end) = newStateChangesIdx(end)-1; -% stateDiffsAtChanges = stateDiffs(newStateChangesIdx); -newStateAtChanges = HMM.newStates(newStateChangesIdx); - -for s = 1:nStatesFinal - for t = 1:length(newStateAtChanges)-2 - if all(newStateAtChanges(t:t+2) == [s 0 s]) - HMM.newStates(newStateChangesIdx(t+1):newStateChangesIdx(t+2)-1) = s; - end - end -end - -clear paddedStates stateDiffs s t newStateChangesIdx newStateAtChanges - -%% Calculate state periods -% Prepare data -paddedStates = [NaN HMM.newStates NaN]; -stateDiffs = diff(paddedStates); -HMM.newStateChangesIdx = find(stateDiffs); -HMM.newStateChangesIdx(end) = HMM.newStateChangesIdx(end)-1; -HMM.newStateAtChanges = HMM.newStates(HMM.newStateChangesIdx); - -% Calculate all durations -allNewDurations = diff(HMM.newStateChangesIdx)*dt; - -clear paddedStates stateDiffs - -%% Calculate durations of states -% Calculate durations of all states -HMM.newDurations = allNewDurations(HMM.newStateAtChanges(1:end-1) > 0); -HMM.newAverageDuration = mean(HMM.newDurations); -HMM.newMedianDuration = median(HMM.newDurations); - -% Calculate durations for each state -HMM.newDurationsInState = cell(1, nStatesFinal); -HMM.newAverageDurationInState = cell(1, nStatesFinal); -HMM.newMedianDurationInState = cell(1, nStatesFinal); -for s = 1:nStatesFinal - stateNewDurations = allNewDurations(HMM.newStateAtChanges(1:end-1) == s); - HMM.newDurationsInState{s} = stateNewDurations; - HMM.newAverageDurationInState{s} = mean(HMM.newDurationsInState{s}); - HMM.newMedianDurationInState{s} = median(HMM.newDurationsInState{s}); -end - -% Calculate null durations -HMM.newDurationsInNull = allNewDurations(HMM.newStateAtChanges(1:end-1) == 0); -HMM.newAverageDurationInNull = mean(HMM.newDurationsInNull); -HMM.newMedianDurationInNull = median(HMM.newDurationsInNull); - -clear allNewDurations s stateNewDurations - -%% Calculate time spent in states -% Calculate time spent of all states -data = HMM.newDurations; -edges = (min(data)-dt/2) : dt : (max(data)+dt/2); -temp = reverseHist(data, edges); -HMM.newTimeSpent = temp{1}; -HMM.newAverageTimeSpent = mean(HMM.newTimeSpent); -HMM.newMedianTimeSpent = median(HMM.newTimeSpent); - -% Calculate time spent for each states -HMM.newTimeSpentInState = cell(1, nStatesFinal); -HMM.newAverageTimeSpentInState = cell(1, nStatesFinal); -HMM.newMedianTimeSpentInState = cell(1, nStatesFinal); -for s = 1:nStatesFinal - data = HMM.newDurationsInState{s}; - edges = (min(data)-dt/2) : dt : (max(data)+dt/2); - temp = reverseHist(data, edges); - HMM.newTimeSpentInState{s} = temp{1}; - HMM.newAverageTimeSpentInState{s} = mean(HMM.newTimeSpentInState{s}); - HMM.newMedianTimeSpentInState{s} = median(HMM.newTimeSpentInState{s}); -end - -% Calculate time spent of in null state -data = HMM.newDurationsInNull; -edges = (min(data)-dt/2) : dt : (max(data)+dt/2); -temp = reverseHist(data, edges); -HMM.newTimeSpentInNull = temp{1}; -HMM.newAverageTimeSpentInNull = mean(HMM.newTimeSpentInNull); -HMM.newMedianTimeSpentInNull = median(HMM.newTimeSpentInNull); - -clear data edges temp s - -%% Last calculations -HMM.averageStateDurations = dt ./ (1 - diag(HMM.transitionMatrix)); -% if ~verLessThan('matlab', '9.0') -% HMM.neuronStateProb = emissionMatrix ./ sum(emissionMatrix, 1); -% else -% HMM.neuronStateProb = emissionMatrix ./ ... -% repmat(sum(emissionMatrix, 1), nStatesFinal, 1); -% end -% HMM.neuronStateProb = HMM.neuronStateProb(:, 1:end-1); -% [~, HMM.neuronsInState] = max(HMM.neuronStateProb, [], 1); - -%% Storing the Viterbi-based results - -% Computing the original Markovian states -fprintf('\tcomputing state chnages...\n'); -paddedStates = [NaN HMM.states NaN]; -stateDiffs = diff(paddedStates); -HMM.stateChangesIdx = find(stateDiffs); -HMM.stateChangesIdx(end) = HMM.stateChangesIdx(end)-1; -HMM.stateAtChanges = HMM.states(HMM.stateChangesIdx); - -% Calculate all durations -allDurations = diff(HMM.stateChangesIdx)*dt; -clear paddedStates stateDiffs - -% Calculate durations of states -fprintf('\tcomputing durations...\n'); -% Calculate durations of all states -HMM.durations = allDurations(HMM.stateAtChanges(1:end-1) > 0); -HMM.averageDuration = mean(HMM.durations); -HMM.medianDuration = median(HMM.durations); - -% Calculate durations for each state -HMM.durationsInState = cell(1, nStatesFinal); -HMM.averageDurationInState = cell(1, nStatesFinal); -HMM.medianDurationInState = cell(1, nStatesFinal); -for s = 1:nStatesFinal - stateDurations = allDurations(HMM.stateAtChanges(1:end-1) == s); - HMM.durationsInState{s} = stateDurations; - HMM.averageDurationInState{s} = mean(HMM.durationsInState{s}); - HMM.medianDurationInState{s} = median(HMM.durationsInState{s}); -end - -clear allDurations s stateDurations - -% Calculate time spent in state -fprintf('\tcomputing time spent...\n'); -% Calculate time spent of all states -data = HMM.durations; -edges = (min(data)-dt/2) : dt : (max(data)+dt/2); -temp = reverseHist(data, edges); -HMM.timeSpent = temp{1}; -HMM.averageTimeSpent = mean(HMM.timeSpent); -HMM.medianTimeSpent = median(HMM.timeSpent); - -% Calculate time spent for each states -HMM.timeSpentInState = cell(1, nStatesFinal); -HMM.averageTimeSpentInState = cell(1, nStatesFinal); -HMM.medianTimeSpentInState = cell(1, nStatesFinal); -for s = 1:nStatesFinal - data = HMM.durationsInState{s}; - edges = (min(data)-dt/2) : dt : (max(data)+dt/2); - temp = reverseHist(data, edges); - HMM.timeSpentInState{s} = temp{1}; - HMM.averageTimeSpentInState{s} = mean(HMM.timeSpentInState{s}); - HMM.medianTimeSpentInState{s} = median(HMM.timeSpentInState{s}); -end - -clear data edges temp s - -%% Keep data -HMM.version = version; -HMM.dt = dt; -HMM.nStates = nStatesFinal; - -end - -% To re-calculate everything else quickly, most important variables: -% HMM.obs, HMM.transitionMatrix, HMM.emissionMatrix -% HMM.stateChangesIdx, HMM.stateAtChanges - -% Automatically detect number of states - % * Repetitions? - % * Without random initialization, multiple repetitions give exactly - % same transition / emission matrix / probability & chosen state - % sequence for N states. - % * With random initialization, probably different. - % * Increasing number of states? - % * Increases log-prob, partial structure kept. - % * transition times ~< 300ms. - % * To choose, BIC/AIC, cross-validation, (duration of states), - % Bayesian estimation of the parameters. No fit-all procedure. -% Smoothed probabilities vs real probabilities with threshold -% Test multiple random initializations \ No newline at end of file diff --git a/Varia/Hidden Markov Model/hmm_test.m b/Varia/Hidden Markov Model/hmm_test.m deleted file mode 100644 index 6180736..0000000 --- a/Varia/Hidden Markov Model/hmm_test.m +++ /dev/null @@ -1,137 +0,0 @@ - - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; -session_range_with_neighbor=11; - -%Set parameters -with_partner =0;% 0: no partner; 1: with partner; 2: with neighbor -temp = 1; temp_resolution = 500; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 50;%Number of SVM iterations - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -elseif with_partner ==2 - session_range = session_range_with_neighbor; - a_sessions = nan; h_sessions = 11; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -%Set path -filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) -savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - -chan = 1; -[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_hmm(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - -disp('Data Loaded') - -Spike_count_raster = zscore(Spike_rasters'); -behavior_labels = cell2mat({labels{:,3}}'); - -%remove rest epochs -idx_keep = find(behavior_labels~=length(behav_categ)); %1:length(behavior_labels);% -Spike_count_raster_final = Spike_count_raster(idx_keep,:); -behavior_labels_final = behavior_labels(idx_keep); - -%reset labels -unique_lbl = unique(behavior_labels_final); -labels_final = behavior_labels_final; -for lb = 1:length(unique_lbl) - labels_final(behavior_labels_final == unique_lbl(lb)) = lb; -end -[1:length(unique_lbl)'; unique_lbl(1:length(unique_lbl))'] - -%% HMM estimation - -%%%%%%%%%%%%% -%Chen Matlab toolbox -num_states = 4; -[model, llh] = hmmEm(labels_final',num_states); -figure; heatmap(model.A) %transition probabilities -figure; heatmap(model.E) %observation probability in each state -figure; plot(llh) - -z = hmmViterbi(labels_final',model); -figure; plot(z) -figure; hist(z) - -%%%%%%%%%%%%% -%Matlab toolbox -seq = labels_final'; -[TRANS,EMIS] = hmmtrain(seq, model.A, model.E,'Tolerance',1e-10, 'Maxiterations',1e5); -figure; heatmap(TRANS) -STATES = hmmviterbi(seq,TRANS,EMIS) -figure; hold on; plot(STATES); plot(labels_final) -count=hist(STATES); - -%%%%%%%%%%%%%% -%Decode -num_iter = 100; -min_occurrences = min(count); -state_labels_final = STATES; -for iter = 1:num_iter - - % clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... - % Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... - % channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %Balance number of trials per class - Labels = state_labels_final'; - Input_matrix = Spike_count_raster_final; - - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min_occurrences;%min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) -end -mean(hitrate) -mean(hitrate_shuffled) \ No newline at end of file diff --git a/Varia/Hidden Markov Model/hmm_test_v2.m b/Varia/Hidden Markov Model/hmm_test_v2.m deleted file mode 100644 index 28c5989..0000000 --- a/Varia/Hidden Markov Model/hmm_test_v2.m +++ /dev/null @@ -1,136 +0,0 @@ - - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16]; -session_range_with_partner=[1:3,11:13]; -session_range_with_neighbor=11; - -%Set parameters -with_partner =0;% 0: no partner; 1: with partner; 2: with neighbor -temp = 1; temp_resolution = 500; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=1;%Only consider isolated units. 0=all units; 1=only well isolated units -num_iter = 50;%Number of SVM iterations - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -elseif with_partner ==2 - session_range = session_range_with_neighbor; - a_sessions = nan; h_sessions = 11; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; -%Set path -filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) -savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - -chan = 1; -[Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_function_hmm(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); - -disp('Data Loaded') - -% - -%remove rest epochs -idx_keep = find(behavior_labels~=length(behav_categ)); %1:length(behavior_labels);% -Spike_count_raster_final = Spike_count_raster(idx_keep,:); -behavior_labels_final = behavior_labels(idx_keep); - -%reset labels -unique_lbl = unique(behavior_labels_final); -labels_final = behavior_labels_final; -for lb = 1:length(unique_lbl) - labels_final(behavior_labels_final == unique_lbl(lb)) = lb; -end -[1:length(unique_lbl)'; unique_lbl(1:length(unique_lbl))'] - -%% HMM estimation - -%%%%%%%%%%%%% -%Chen Matlab toolbox -num_states = 4; -[model, llh] = hmmEm(labels_final',num_states); -figure; heatmap(model.A) %transition probabilities -figure; heatmap(model.E) %observation probability in each state -figure; plot(llh) - -z = hmmViterbi(labels_final',model); -figure; plot(z) -figure; hist(z) - -%%%%%%%%%%%%% -%Matlab toolbox -seq = labels_final'; -[TRANS,EMIS] = hmmtrain(seq, model.A, model.E,'Tolerance',1e-10, 'Maxiterations',1e5); -figure; heatmap(TRANS) -STATES = hmmviterbi(seq,TRANS,EMIS) -figure; hold on; plot(STATES); plot(labels_final) -count=hist(STATES); - -%%%%%%%%%%%%%% -%Decode -num_iter = 100; -min_occurrences = min(count); -state_labels_final = STATES; -for iter = 1:num_iter - - % clearvars -except savePath behav_categ behavior_labels_final behavior_labels_shifted Spike_count_raster_final... - % Spike_count_raster_shifted num_iter iter hitrate hitrate_shuffled C C_shuffled temp_resolution... - % channel_flag filePath chan temp mean_hitrate sd_hitrate mean_hitrate_shuffled C_table behavs_eval behav is_mac min_occurrences - - %Balance number of trials per class - Labels = state_labels_final'; - Input_matrix = Spike_count_raster_final; - - uniqueLabels = unique(Labels); %IDentify unique labels (useful when not numbers) - NumOfClasses = length(uniqueLabels); % Total number of classes - numericLabels = 1:NumOfClasses; %Numeric name of labels - - labels_temp = Labels; - for i=1:NumOfClasses - idx = Labels == uniqueLabels(i); - labels_temp(idx) = numericLabels(i); - labels_id{i,1} = uniqueLabels(i); labels_id{i,2}=behav_categ{uniqueLabels(i)} ; - end - Labels = labels_temp; - - num_trials = hist(Labels,numericLabels); %number of trials in each class - minNumTrials = min_occurrences;%min(num_trials); %find the minimum one %CT change to have 30 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == numericLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - Input_matrix = Input_matrix(chosen_trials, :); - Labels = Labels(chosen_trials, :); - Labels_shuffled = Labels(randperm(length(Labels))); - %tabulate(Labels); - - % % %% Save variables - % save([filePath '\SVM_input.mat'],'Input_matrix', 'Labels','Labels_shuffled'); - - % Run svm - [hitrate(iter), C{iter}] = log_SVM_basic_function(Input_matrix, Labels, 5, 0, 0); - [hitrate_shuffled(iter), C_shuffled{iter}] = log_SVM_basic_function(Input_matrix, Labels_shuffled, 5, 0, 0); - - disp(['SVM run' num2str(iter) '/' num2str(num_iter)]) -end -mean(hitrate) -mean(hitrate_shuffled) \ No newline at end of file diff --git a/Varia/Hidden Markov Model/project_recap_2022_march.pptx b/Varia/Hidden Markov Model/project_recap_2022_march.pptx deleted file mode 100644 index f4d2f8a..0000000 Binary files a/Varia/Hidden Markov Model/project_recap_2022_march.pptx and /dev/null differ diff --git a/Varia/Manifolds/SetupandDev.m b/Varia/Manifolds/SetupandDev.m deleted file mode 100644 index 16278d3..0000000 --- a/Varia/Manifolds/SetupandDev.m +++ /dev/null @@ -1,5 +0,0 @@ -%% Running notes -%2022-09-13: -%To be returned to. Leaving as place holder while development for code is -%in repo for broader projects concerning dimensionality, manifolds, and -%data with temporal structure. \ No newline at end of file diff --git a/Varia/Motion energy/preprocess_ME.R b/Varia/Motion energy/preprocess_ME.R deleted file mode 100644 index 997820d..0000000 --- a/Varia/Motion energy/preprocess_ME.R +++ /dev/null @@ -1,89 +0,0 @@ -# Script to pull together ME data - -library(xlsx) - -#Load data -file = file.choose() # choose the formatted behavior file -log <- xlsx::read.xlsx(file, 1) -date = substr(file,nchar(file)-24, nchar(file)-15) -if (length(grep('Hooke',file))>0){monkey ="Hooke"}else{monkey="Amos"} -session=paste(monkey,date,sep="_") - -LeftVideo_file = file.choose() # choose the left video ME file -ME_left_file = read.delim(LeftVideo_file, header = TRUE, sep = " ") -ME_left = ME_left_file[,1] - -RightVideo_file = file.choose() # choose the right video ME file -ME_right_file = read.delim(RightVideo_file, header = TRUE, sep = " ") -ME_right = ME_right_file[,1] - -#First important note: a few frames difference between the two videos. -diff_vids = nrow(ME_right_file) - nrow(ME_left_file) -print(paste('Difference in frames between videos is:', abs(diff_vids), 'frames.')) -if (diff_vids <0){ - ME_right[(length(ME_right)+1):(length(ME_right)+abs(diff_vids))]=0 -} -if (diff_vids >0){ - ME_left[(length(ME_left)+1):(length(ME_left)+abs(diff_vids))]=0 -} - -MotionEnergy = data.frame(cbind(ME_left, ME_right)) - -# #Visualize ME -# plot(1:3000, ME_right[1:3000], type = "S", frame = FALSE, -# col = "red", xlab = "x", ylab = "y") -# lines(1:3000, ME_left[1:3000], col = "blue", type = "S", lty = 2) - -#Get indices for camera sync and stopped recording -idx_start_rec = which(log$Behavior=="Started recording") -idx_stop_rec = which(log$Behavior=="Stopped recording") -idx_camera_sync = which(log$Behavior=="Camera Sync") - -#Get recording length in sec -length_recording = log[idx_stop_rec,"Time"]/1000 -#Important note: the length of the recording session should not be more than 2sec different between the -#deuteron log and "unit rasters". That is because in the script "log_read_sorted_NEX_file_CombinedChannels.m" -#Unit_rasters is initialized by the line: Unit_rasters(unit,:) = zeros(1,round(length_recording*temp_resolution)+1); -#round and +1 can lead to up to 2sec difference with the length of recording from the log. -time_before_cameraSync = log[2,"Time"]/1000 -if (session == "Hooke_2021-08-02"){ - time_after_cameraSync = 0 #Camera ended after start of recording... -}else{ - time_after_cameraSync = log[idx_stop_rec,"Time"]/1000 - log[idx_camera_sync[2],"Time"]/1000 -} -#Special note for Hooke_2021-08-02: There was no end recording time due to a bug. - -#Estimate frame rate -length_video = (log[idx_camera_sync[2],"Time"] - log[idx_camera_sync[1],"Time"])/1000 -frame_rate = 29.973 -#For Amos_2021-07-29: 29.791 -# Hooke_2021-08-02: 29.97 -# Amos_2021-08-03: 29.973 -# Hooke_2021-08-05: 29.972 -expected_num_frames = frame_rate*length_video -diff_frames = expected_num_frames-length(ME_right) -print(paste('Difference in frames between real and expected is:', diff_frames, 'frames.')) - -#Pad ME with 0 in the extra recording spots -total_frames_needed = round(length_recording*frame_rate) -numFrames_before_cameraSync = round(time_before_cameraSync*frame_rate) -numFrames_after_cameraSync = round(time_after_cameraSync*frame_rate) -zero_pad_before = data.frame(matrix(data = 0, nrow = numFrames_before_cameraSync, ncol = 2)); names(zero_pad_before) = c("ME_left","ME_right") -zero_pad_after = data.frame(matrix(data = 0, nrow = numFrames_after_cameraSync, ncol = 2)); names(zero_pad_after) = c("ME_left","ME_right") - -ME_final = rbind(zero_pad_before, MotionEnergy, zero_pad_after) - -if (session=="Hooke_2021-08-02"){ - #Hooke_2021-08-02 has a longer video length than recording. To rectify this mismatch, - #remove the extra frames to match the rneural recording length. - idx_to_cut = round(frame_rate*abs(log[nrow(log),"Time"]/1000 - log[nrow(log)-1,"Time"]/1000)) - ME_final=ME_final[1:(nrow(ME_final)-idx_to_cut+1),] -} - -#Save ME file -mac =1 -if (mac == 1) { home = '~'} else -{home = 'C:/Users/GENERAL'} - -setwd(paste(home,'/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/', sep="" )) -write.csv(ME_final, paste(session,'/ME.csv',sep=""),row.names=F) diff --git a/Varia/Population trajectory/.DS_Store b/Varia/Population trajectory/.DS_Store deleted file mode 100644 index 5008ddf..0000000 Binary files a/Varia/Population trajectory/.DS_Store and /dev/null differ diff --git a/Varia/Population trajectory/gpfa_R37.m b/Varia/Population trajectory/gpfa_R37.m deleted file mode 100644 index 5d5a731..0000000 --- a/Varia/Population trajectory/gpfa_R37.m +++ /dev/null @@ -1,317 +0,0 @@ - -%% Load data - -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) -cd(filePath) - -session = filePath(end-9:end); -monkey = filePath(end-14:end-10); - -%behav_log = readtable('behavioral_log_session1.csv'); -behavior_log = readtable(['EVENTLOG_restructured_',monkey,session,'.csv']);% Behavioral data -behav_log = readtable('behavioral_log_session1.csv'); -behavior_log = readtable('behav_log_restructured_session1.csv');% Behavioral data -behavior_log{:,'start_time_round'}=round(behavior_log{:,'start_time'}); -behavior_log{:,'stop_time_round'}=round(behavior_log{:,'stop_time'}); -behavior_log{:,'duration_round'}=behavior_log{:,'stop_time_round'}-behavior_log{:,'start_time_round'}; - -load('Raster_per_second_Allchannels.mat') % Neural data -% load('SpikeData_timestamps.mat') - - -%% Plot SDF (intergrated signal at the second resolution) -% Check what the neural signal looks like - -figure; hold on -for neuron = 1:size(Unit_rasters,1) - plot(Unit_rasters(neuron,:)+ones(1,size(Unit_rasters,2))*300*neuron) -end -pause(1); close all - -%For now exclude unit 4 and 37 which have a weird activity -included_units = [1:3 5:36 38:59]; -Unit_rasters = Unit_rasters(included_units,:); - -%% Neural trajectory by wakefulness state - -%Get wakefulness events: -wakingup = round(behav_log{strcmp(behav_log{:,'Behavior'},'wakingup'),'Time'}/1000); %time in sec -awake = round(behav_log{strcmp(behav_log{:,'Behavior'},'awake'),'Time'}/1000); %time in sec -fullyawake = round(behav_log{strcmp(behav_log{:,'Behavior'},'fullyawake'),'Time'}/1000); %time in sec - -%Create input structure for Data High: -D =struct(); -D(1).type = 'traj'; -D(1).data = Unit_rasters; -D(1).epochStarts=[1 wakingup awake ];%fullyawake]; -D(1).epochColors=[1,0,0;0,1,0;0,0,1];%0,1,1]; - -cd('C:\Users\Camille Testard\Desktop\Grants_Fellowships\R37_Grant_renewal\DataHigh1.3') -DataHigh(D,'DimReduce') - -%% Create trajectory video - -%I manually extracted the trajectory to have total plotting control. -%Load 3D trajectory coordinates: -load('trajectory_allUnits.mat') - -%get new epoch starts -load('trajectory_allUnits_info.mat') -D.epochStarts = handles.D.epochStarts; close all -%set frame rate -window_size = 5; step = 1; -no_of_windows = max(1 + floor((size(p, 2) - window_size) / step), 0); -plotting_windows=buffer([1:size(p,2)], window_size, step); -plotting_windows(plotting_windows==0)=1; -plotting_windows(find(plotting_windows(:,end)==1),end)=max(max(plotting_windows)); -C=[0.8 0.8 0.8; 0.7 0 0.7;0.9 0.6 0.1]; - -%Get new times for behaviors -ratio = size(p,2)/size(Unit_rasters,2); -behavior_log.new_start_time = round(behavior_log.start_time*ratio); behavior_log.new_start_time(1)=1; -behavior_log.new_stop_time = round(behavior_log.stop_time*ratio); -behavior_log.new_duration=behavior_log.new_stop_time - behavior_log.new_start_time; -all_Colors = [1 0 0; 1 0 1; 0.8 1 0.6; 0 0 1; 0 1 1; 0.8 0.8 1; 0 0.6 0.4; 0.6 0.6 0.6; 1 0.5 0; 0.6 0.8 0]; - -for row_num = 1:length(behavior_log.new_start_time) - if strcmp(behavior_log.Behavior(row_num), 'Aggression') - behavior_log.Color(row_num) = {[1 0 0]}; - elseif strcmp(behavior_log.Behavior(row_num),'Drinking') - behavior_log.Color(row_num) = {[1 0 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Feeding') - behavior_log.Color(row_num) = {[0.8 1 0.6]}; - elseif strcmp(behavior_log.Behavior(row_num),'Grooming') - behavior_log.Color(row_num) = {[0 0 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Proximity') - behavior_log.Color(row_num) = {[0 1 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Resting') - behavior_log.Color(row_num) = {[0.8 0.8 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Self-directed behavior') - behavior_log.Color(row_num) = {[0 0.6 0.4]}; - elseif strcmp(behavior_log.Behavior(row_num),'Sleeping') - behavior_log.Color(row_num) = {[0.6 0.6 0.6]}; - elseif strcmp(behavior_log.Behavior(row_num),'Submission') - behavior_log.Color(row_num) = {[1 0.5 0]}; - else - behavior_log.Color(row_num) = {[0.6 0.8 0]}; - end -end - -% % figure; hold on; ylim([1 2]); xlim([0 455]) -% % set(gca,'Xticklabel',[]); set(gca, 'XTick', []); -% % set(gca,'Yticklabel',[]); set(gca, 'YTick', []); -% % set(gca,'Zticklabel',[]); set(gca, 'ZTick', []); -% % for b = 1:length(behavior_log.new_start_time) -% % rectangle('Position',[behavior_log.new_start_time(b),1,... -% % behavior_log.new_stop_time(b)-behavior_log.new_start_time(b),1], 'FaceColor',behavior_log.Color{b},... -% % 'EdgeColor',behavior_log.Color{b}) -% % end -% % %Just to make a legend... -% % for c=1:size(all_Colors,1) -% % scatter(0,380,1,all_Colors(c,:),'filled') -% % end -% % legend({'Aggression','Drinking','Feeding','Grooming','Proximity',... -% % 'Resting','Self-groooming','Sleeping','Submission','Walking'},'Location','East') -% % set(gcf, 'Position', [100, 100, 1000, 200]) - -vidfile = VideoWriter('TrajectoryMovie.mp4','MPEG-4'); -open(vidfile); -for k = 1:size(p,2)%size(plotting_windows,2) - - disp([num2str(k) '/' num2str(size(p,2))]) - figure; hold on; set(gcf, 'Position', [100, 100, 1000, 600]) - - ethogramplot = subplot(2,1,1); hold on; ylim([1 2]); xlim([0 372]) - set(gca,'Xticklabel',[]); set(gca, 'XTick', []); - set(gca,'Yticklabel',[]); set(gca, 'YTick', []); - set(gca,'Zticklabel',[]); set(gca, 'ZTick', []); - ethogramplot.Position = ethogramplot.Position + [0 0.08 0 -0.05]; - - behav2plot=find(behavior_log.new_start_time<=k); - - for i = 1:length(behav2plot) - b=behav2plot(i); - rectangle('Position',[behavior_log.new_start_time(b),1,... - k-behavior_log.new_start_time(b)+1,1], 'FaceColor',behavior_log.Color{b},... - 'EdgeColor',behavior_log.Color{b}) - end - %Just to make a legend... - xline(D.epochStarts(2),'--',{'WakingUp'},'LineWidth',2,'Color',C(2,:)); - xline(D.epochStarts(3),'--',{'Awake'},'LineWidth',2,'Color',C(3,:)); - for c=1:size(all_Colors,1) - scatter(0,380,0.1,all_Colors(c,:),'filled') - end - legend({'Waking up','Awake','Aggression','Drinking','Feeding','Grooming','Proximity',... - 'Resting','Self-groooming','Sleeping','Submission','Walking'},'Location','eastoutside') - t=title('Monkey Behavior','FontSize', 16); - set(t,'position',get(t,'position')-[130 0 0]) - - % Neural trajectory plot - trajplot=subplot(2,1,2); hold on; - trajplot.Position = trajplot.Position + [0 -0.1 0 0.3]; - xlim([min(p(1,:)), max(p(1,:))]); set(gca,'Xticklabel',[]) - ylim([min(p(2,:)), max(p(2,:))]); set(gca,'Yticklabel',[]) - zlim([min(p(3,:)), max(p(3,:))]); set(gca,'Zticklabel',[]) - view(40,30) - grid on - - if k<=D.epochStarts(2) - line(p(1,1:k), p(2,1:k),... - p(3,1:k), 'LineWidth', 2, 'Color', C(1,:)); - - legend('Sedated','Location','SouthEast') - end - if k>D.epochStarts(2) - line(p(1,1:D.epochStarts(2)), p(2,1:D.epochStarts(2)),... - p(3,1:D.epochStarts(2)), 'LineWidth', 2, 'Color', C(1,:)); - line(p(1,D.epochStarts(2):k), p(2,D.epochStarts(2):k),... - p(3,D.epochStarts(2):k), 'LineWidth', 2, 'Color', C(2,:)); - legend('Sedated', 'Waking up','Location','SouthEast') - end - if k>D.epochStarts(3) - line(p(1,D.epochStarts(3)+1:k), p(2,D.epochStarts(3)+1:k),... - p(3,D.epochStarts(3)+1:k), 'LineWidth', 2, 'Color', C(3,:)); - legend('Sedated', 'Waking up', 'Awake','Location','SouthEast') - end - t=title('Neural Trajectory','FontSize', 16); - set(t,'position',get(t,'position')-[650 0 800]) - - frame = getframe(gcf); - writeVideo(vidfile,frame); - - close all -end - -close(vidfile); - -%% Neural states during "awake" state. 1- Compare active behaviors states - -% Start with "long behaviors": -min_duration = 5;%in sec -max_duration = 100;%in sec % exclude long behaviors as they may not be "clean". I.e. they co-occur with a number of other behaviors. -idx = find(behavior_log{:,'duration_round'}>=min_duration &... - behavior_log{:,'duration_round'}<=max_duration &... - ~strcmp(behavior_log{:,'Behavior'},'Resting')); %exclude resting for now -new_log=behavior_log(idx,:); - -% Create structure with chunked neural data per event types. -time_around_epoch = 1;%in sec -event_window=min_duration;%in sec -B = struct(); -pseudo_trial = 1; -for behav = 1:size(new_log,1) - - total_time = new_log{behav,'start_time_round'}-time_around_epoch ... - :new_log{behav,'stop_time_round'}+time_around_epoch; - - divide=floor(length(total_time)./event_window); - mode=mod(length(total_time),event_window); - new_chunks = reshape(total_time(1:end-mode),divide,[]); - for ii = 1:divide - - B(pseudo_trial).data = Unit_rasters(:,new_chunks(ii,:)); - - B(pseudo_trial).condition = char(new_log{behav,'Behavior'}); - % if strcmp(new_log{behav,'Behavior'},'Resting') - % B(behav).condition = 'Non-Resting'; - % else - % B(behav).condition = 'Resting'; - % end - - B(pseudo_trial).epochStarts=1; - if strcmp(new_log{behav,'Behavior'},'Aggression') - B(pseudo_trial).epochColors=[1,0,0]; - elseif strcmp(new_log{behav,'Behavior'},'Grooming') - B(pseudo_trial).epochColors=[0,0,1]; - elseif strcmp(new_log{behav,'Behavior'},'Proximity') - B(pseudo_trial).epochColors=[0,0.75,1]; - elseif strcmp(new_log{behav,'Behavior'},'Self-directed behavior') - B(pseudo_trial).epochColors=[0,1,0]; - elseif strcmp(new_log{behav,'Behavior'},'Submission') - B(pseudo_trial).epochColors=[1,0.75,0]; - elseif strcmp(new_log{behav,'Behavior'},'Walking') - B(pseudo_trial).epochColors=[1,0,1]; - elseif strcmp(new_log{behav,'Behavior'},'Resting') - B(pseudo_trial).epochColors=[0,0.5,1];s - elseif strcmp(new_log{behav,'Behavior'},'Feeding') - B(pseudo_trial).epochColors=[0.5,0.5,0.5]; - elseif strcmp(new_log{behav,'Behavior'},'Drinking') - B(pseudo_trial).epochColors=[0.5,0,1]; - end - B(pseudo_trial).type='state'; - - pseudo_trial=pseudo_trial+1; - end -end - -% Average "chunked" trials (for better visualization): -table_B = struct2table(B); avg_B=struct(); -tabulate(table_B{:,'condition'}); -behav_list = unique(table_B{:,'condition'}); -num_events=4; avg_state=1; -for b = 1:length(behav_list) - - idx=find(ismember(table_B{:,'condition'},behav_list{b})); - divide=floor(length(idx)./num_events); - mode=mod(length(idx),num_events); - average_over = reshape(idx(1:end-mode),divide,[]); - - for avg = 1:size(average_over,1) - avg_B(avg_state).data = mean(cat(3,B(average_over(avg,1)).data,B(average_over(avg,2)).data,... - B(average_over(avg,3)).data,B(average_over(avg,4)).data),3); - avg_B(avg_state).condition = behav_list{b}; - avg_B(avg_state).epochStarts = 1; - avg_B(avg_state).epochColors = B(average_over(avg,1)).epochColors; - avg_B(avg_state).type='state'; - - avg_state=avg_state+1; - end -end - -% Call DataHigh -DataHigh(avg_B,'DimReduce') - -%% Scrap code -for row_num = 1:length(labels) - if strcmp(behavior_log.Behavior(row_num), 'Aggression') - behavior_log.Color(row_num) = {[1 0 0]}; - elseif strcmp(behavior_log.Behavior(row_num),'Approach') - behavior_log.Color(row_num) = {[1 0 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Drinking') - behavior_log.Color(row_num) = {[1 0 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Foraging') - behavior_log.Color(row_num) = {[1 0 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Groom Give') - behavior_log.Color(row_num) = {[1 0 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Groom Receive') - behavior_log.Color(row_num) = {[1 0 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'HIP') - behavior_log.Color(row_num) = {[1 0 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'HIS') - behavior_log.Color(row_num) = {[1 0 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Leave') - behavior_log.Color(row_num) = {[1 0 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Other monkeys vocalize') - behavior_log.Color(row_num) = {[1 0 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Pacing/Travel') - behavior_log.Color(row_num) = {[1 0 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Proximity') - behavior_log.Color(row_num) = {[0.8 1 0.6]}; - elseif strcmp(behavior_log.Behavior(row_num),'RR') - behavior_log.Color(row_num) = {[0 0 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'SP') - behavior_log.Color(row_num) = {[1 0 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'SS') - behavior_log.Color(row_num) = {[0 1 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'SP') - behavior_log.Color(row_num) = {[0.8 0.8 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Scratch') - behavior_log.Color(row_num) = {[0 0.6 0.4]}; - elseif strcmp(behavior_log.Behavior(row_num),'Self-groom') - behavior_log.Color(row_num) = {[0.6 0.6 0.6]}; - elseif strcmp(behavior_log.Behavior(row_num),'Vocalization') - behavior_log.Color(row_num) = {[1 0.5 0]}; - else - behavior_log.Color(row_num) = {[0.6 0.8 0]}; - end -end \ No newline at end of file diff --git a/Varia/Population trajectory/log_CreateTrajectoryVideo.m b/Varia/Population trajectory/log_CreateTrajectoryVideo.m deleted file mode 100644 index bfc41b8..0000000 --- a/Varia/Population trajectory/log_CreateTrajectoryVideo.m +++ /dev/null @@ -1,134 +0,0 @@ -%% Log_CreateTrajectoryVideo - -%I manually extracted the trajectory to have total plotting control. -%Load 3D trajectory coordinates: -load('trajectory_allUnits.mat') - -%get new epoch starts -load('trajectory_allUnits_info.mat') -D.epochStarts = handles.D.epochStarts; close all -%set frame rate -window_size = 5; step = 1; -no_of_windows = max(1 + floor((size(p, 2) - window_size) / step), 0); -plotting_windows=buffer([1:size(p,2)], window_size, step); -plotting_windows(plotting_windows==0)=1; -plotting_windows(find(plotting_windows(:,end)==1),end)=max(max(plotting_windows)); -C=[0.8 0.8 0.8; 0.7 0 0.7;0.9 0.6 0.1]; - -%Get new times for behaviors -ratio = size(p,2)/size(Unit_rasters,2); -behavior_log.new_start_time = round(behavior_log.start_time*ratio); behavior_log.new_start_time(1)=1; -behavior_log.new_stop_time = round(behavior_log.stop_time*ratio); -behavior_log.new_duration=behavior_log.new_stop_time - behavior_log.new_start_time; -all_Colors = [1 0 0; 1 0 1; 0.8 1 0.6; 0 0 1; 0 1 1; 0.8 0.8 1; 0 0.6 0.4; 0.6 0.6 0.6; 1 0.5 0; 0.6 0.8 0]; - -for row_num = 1:length(behavior_log.new_start_time) - if strcmp(behavior_log.Behavior(row_num), 'Aggression') - behavior_log.Color(row_num) = {[1 0 0]}; - elseif strcmp(behavior_log.Behavior(row_num),'Drinking') - behavior_log.Color(row_num) = {[1 0 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Feeding') - behavior_log.Color(row_num) = {[0.8 1 0.6]}; - elseif strcmp(behavior_log.Behavior(row_num),'Grooming') - behavior_log.Color(row_num) = {[0 0 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Proximity') - behavior_log.Color(row_num) = {[0 1 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Resting') - behavior_log.Color(row_num) = {[0.8 0.8 1]}; - elseif strcmp(behavior_log.Behavior(row_num),'Self-directed behavior') - behavior_log.Color(row_num) = {[0 0.6 0.4]}; - elseif strcmp(behavior_log.Behavior(row_num),'Sleeping') - behavior_log.Color(row_num) = {[0.6 0.6 0.6]}; - elseif strcmp(behavior_log.Behavior(row_num),'Submission') - behavior_log.Color(row_num) = {[1 0.5 0]}; - else - behavior_log.Color(row_num) = {[0.6 0.8 0]}; - end -end - -% % figure; hold on; ylim([1 2]); xlim([0 455]) -% % set(gca,'Xticklabel',[]); set(gca, 'XTick', []); -% % set(gca,'Yticklabel',[]); set(gca, 'YTick', []); -% % set(gca,'Zticklabel',[]); set(gca, 'ZTick', []); -% % for b = 1:length(behavior_log.new_start_time) -% % rectangle('Position',[behavior_log.new_start_time(b),1,... -% % behavior_log.new_stop_time(b)-behavior_log.new_start_time(b),1], 'FaceColor',behavior_log.Color{b},... -% % 'EdgeColor',behavior_log.Color{b}) -% % end -% % %Just to make a legend... -% % for c=1:size(all_Colors,1) -% % scatter(0,380,1,all_Colors(c,:),'filled') -% % end -% % legend({'Aggression','Drinking','Feeding','Grooming','Proximity',... -% % 'Resting','Self-groooming','Sleeping','Submission','Walking'},'Location','East') -% % set(gcf, 'Position', [100, 100, 1000, 200]) - -vidfile = VideoWriter('TrajectoryMovie.mp4','MPEG-4'); -open(vidfile); -for k = 1:size(p,2)%size(plotting_windows,2) - - disp([num2str(k) '/' num2str(size(p,2))]) - figure; hold on; set(gcf, 'Position', [100, 100, 1000, 600]) - - ethogramplot = subplot(2,1,1); hold on; ylim([1 2]); xlim([0 372]) - set(gca,'Xticklabel',[]); set(gca, 'XTick', []); - set(gca,'Yticklabel',[]); set(gca, 'YTick', []); - set(gca,'Zticklabel',[]); set(gca, 'ZTick', []); - ethogramplot.Position = ethogramplot.Position + [0 0.08 0 -0.05]; - - behav2plot=find(behavior_log.new_start_time<=k); - - for i = 1:length(behav2plot) - b=behav2plot(i); - rectangle('Position',[behavior_log.new_start_time(b),1,... - k-behavior_log.new_start_time(b)+1,1], 'FaceColor',behavior_log.Color{b},... - 'EdgeColor',behavior_log.Color{b}) - end - %Just to make a legend... - xline(D.epochStarts(2),'--',{'WakingUp'},'LineWidth',2,'Color',C(2,:)); - xline(D.epochStarts(3),'--',{'Awake'},'LineWidth',2,'Color',C(3,:)); - for c=1:size(all_Colors,1) - scatter(0,380,0.1,all_Colors(c,:),'filled') - end - legend({'Waking up','Awake','Aggression','Drinking','Feeding','Grooming','Proximity',... - 'Resting','Self-groooming','Sleeping','Submission','Walking'},'Location','eastoutside') - t=title('Monkey Behavior','FontSize', 16); - set(t,'position',get(t,'position')-[130 0 0]) - - % Neural trajectory plot - trajplot=subplot(2,1,2); hold on; - trajplot.Position = trajplot.Position + [0 -0.1 0 0.3]; - xlim([min(p(1,:)), max(p(1,:))]); set(gca,'Xticklabel',[]) - ylim([min(p(2,:)), max(p(2,:))]); set(gca,'Yticklabel',[]) - zlim([min(p(3,:)), max(p(3,:))]); set(gca,'Zticklabel',[]) - view(40,30) - grid on - - if k<=D.epochStarts(2) - line(p(1,1:k), p(2,1:k),... - p(3,1:k), 'LineWidth', 2, 'Color', C(1,:)); - - legend('Sedated','Location','SouthEast') - end - if k>D.epochStarts(2) - line(p(1,1:D.epochStarts(2)), p(2,1:D.epochStarts(2)),... - p(3,1:D.epochStarts(2)), 'LineWidth', 2, 'Color', C(1,:)); - line(p(1,D.epochStarts(2):k), p(2,D.epochStarts(2):k),... - p(3,D.epochStarts(2):k), 'LineWidth', 2, 'Color', C(2,:)); - legend('Sedated', 'Waking up','Location','SouthEast') - end - if k>D.epochStarts(3) - line(p(1,D.epochStarts(3)+1:k), p(2,D.epochStarts(3)+1:k),... - p(3,D.epochStarts(3)+1:k), 'LineWidth', 2, 'Color', C(3,:)); - legend('Sedated', 'Waking up', 'Awake','Location','SouthEast') - end - t=title('Neural Trajectory','FontSize', 16); - set(t,'position',get(t,'position')-[650 0 800]) - - frame = getframe(gcf); - writeVideo(vidfile,frame); - - close all -end - -close(vidfile); \ No newline at end of file diff --git a/Varia/Population trajectory/log_GenerateNeuralTrajectories.m b/Varia/Population trajectory/log_GenerateNeuralTrajectories.m deleted file mode 100644 index 1bbff76..0000000 --- a/Varia/Population trajectory/log_GenerateNeuralTrajectories.m +++ /dev/null @@ -1,99 +0,0 @@ -%% log_GenerateNeuralTrajectories -% This script generates neural trajectories, color-coded by behavior. -% CT 2021/11 - -%% Load data -is_mac = 0; - -if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') -end -filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) -cd(filePath) - -session = filePath(end-9:end); -monkey = filePath(end-14:end-10); - -%Load behavioral data -behavior_log = readtable(['EVENTLOG_restructured_',monkey,session,'.csv']);% Behavioral data -behavior_log{:,'start_time_round'}=round(behavior_log{:,'start_time'}); -behavior_log{:,'end_time_round'}=round(behavior_log{:,'end_time'}); -behavior_log{:,'duration_round'}=behavior_log{:,'end_time_round'}-behavior_log{:,'start_time_round'}; - -% %Load labels -% load('Labels_per_sec.mat') -% %Load neural data -% load(['Neural_data_' session '.mat']) % Neural data - -%Get data with specified temporal resolution and channels -temp_resolution = 1; channel_flag = "all"; -[Spike_rasters, labels, behav_categ]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag); - -%% Color code behaviors - -% %Set color code -% behav_categ_color = {[1,0,0]; [0.6350, 0.0780, 0.1840]; [0.3010, 0.7450, 0.9330];... %Aggression; Approach; Drinking -% [0, 0.5, 0]; [0, 0.75, 0.75]; [0, 0, 1];...%Foraging; Groom Give; Groom Receive -% [0.75, 0, 0.75]; [0.4940, 0.1840, 0.5560]; [1, 1, 0];...%HIP; HIS; Leave -% %[0, 1, 0]; [0.25, 0.25, 0.25]; [1, 1, 1];... %Other monkeys vocalize; Travel; Proximity %%%Set proximity to white for now -% [0, 1, 0]; [0.25, 0.25, 0.25]; [0.75, 0.75, 0];... %Other monkeys vocalize; Travel; Proximity -% [0.6831, 0.3651, 0.3651]; [0.7746, 0.6583, 0.5164]; [0.8563, 0.8563, 0.6325];...%RR; SP; SS -% [0.9290, 0.6940, 0.1250]; [0.8500, 0.3250, 0.0980]; [0, 0.2, 0]; %Scratch; Self-groom; Vocalization -% [1.00, 0.54, 0.00]; [0.3 0.3 0.3]};% Yawning; Rest - -%Set color code -behav_categ_color = {[0.7 0.7 0.7]; [0.7 0.7 0.7]; [0.7 0.7 0.7];... %Aggression; Approach; Drinking - [0.7 0.7 0.7]; [0, 0.75, 0.75]; [0, 0, 1];...%Foraging; Groom Give; Groom Receive - [0.7 0.7 0.7]; [0.7 0.7 0.7]; [0.7 0.7 0.7];...%HIP; HIS; Leave - [0.7 0.7 0.7]; [0.7 0.7 0.7]; [0.7 0.7 0.7];... %Other monkeys vocalize; Travel; Proximity - [0.7 0.7 0.7]; [0.7 0.7 0.7]; [0.7 0.7 0.7];...%RR; SP; SS - [0.7 0.7 0.7]; [0.7 0.7 0.7]; [0.7 0.7 0.7]; %Scratch; Self-groom; Vocalization - [0.7 0.7 0.7]; [0.7 0.7 0.7]};% Yawning; Rest - -behav_categ_color_label = {'Red','Dark Red','Light Blue','Dark Green','Turquoise','Dark Blue',... - 'Magenta','Dark Purple','Yellow','Green','Dark grey','Light Green','Dark Beige','Beige','Pink yellow',... - 'Dark Yellow','Orange','Very Dark Green','Dark Orange','Grey'}'; - -behav_categ = [behav_categ, behav_categ_color, behav_categ_color_label]; - -label_colors = cell2mat({behav_categ_color{[labels{:,3}]'}}');% Get a color for each second of the session -%label_colors(end,:) = []; - -%% Plot SDF (intergrated signal at the second resolution) -% % Check what the neural signal looks like -% -% figure; hold on -% for neuron = 1:100%size(Unit_rasters,1) -% plot(Unit_rasters(neuron,:)+ones(1,size(Unit_rasters,2))*300*neuron) -% end -% pause(1); close all - -% %For now exclude unit 4 and 37 which have a weird activity -% included_units = [1:3 5:36 38:59]; -% Unit_rasters = Unit_rasters(included_units,:); - -%% Only condiser a chunk of the session -interval = 45:900; -neural_data = Spike_rasters(:,interval); -% neural_data_std = scale(neural_data); -label_data = labels(interval,:); -color_data = label_colors(interval,:); -%hist([label_data{:,3}]') - - -%% Neural trajectory color-coded by behavior - -%Create input structure for Data High: -D =struct(); -D(1).type = 'traj'; -D(1).data = neural_data; -D(1).epochStarts = [find(abs(diff([label_data{:,3}]'))>0)+1]';%1:length(labels)-1;%[1, 1000]; -D(1).epochColors= color_data(D(1).epochStarts',:); %[1,0,0;0,1,0];% -%[D(1).epochStarts', D(1).epochColors] - -DataHigh(D,'DimReduce') - - - diff --git a/Varia/Population trajectory/log_centroidtraj.m b/Varia/Population trajectory/log_centroidtraj.m deleted file mode 100644 index d47e1e5..0000000 --- a/Varia/Population trajectory/log_centroidtraj.m +++ /dev/null @@ -1,410 +0,0 @@ -%% Running Notes -%As first pass analysis for trajectories/manifold idea, finding population -%average (centroid) for each behavior and using this to predict moment to -%moment behavioral state. - -%% Load in data and preprocess - -%Set path -is_mac = 0; is_camille = 1; - -if is_camille - if is_mac - cd('~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') - else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/') - end - filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - - if is_mac - cd('~/Dropbox (Penn)/Datalogger/Results/') - else - cd('C:/Users/GENERAL/Dropbox (Penn)/Datalogger/Results/') - end - savePath = uigetdir('', 'Please select the result directory'); - -else - - %"[RON ADD PATHS]" - - addpath('C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger\Behavior') - addpath('C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger\Neural preprocessing') - cd('C:\Users\ronwd\Dropbox\Ready to analyze output'); - - filePath = uigetdir('', 'Please select the experiment directory'); % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - - cd('C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_results') - - savePath = uigetdir('', 'Please select the results directory'); - - - - -end - -clearvars -except savePath filePath temp_resolution channel_flag is_mac - -%Set temporal resolution -temp = 1; temp_resolution = 1; -run_cvmnr = 1; %toggle running the cross validated multinomial regression - -%pre-choose number of features to use or use 85% variance threshold for PCA -choose_numcom = 1; man_num = 20; %update 2021-12-06 this doesn't seem to effect the trend of vlPFC being worse prediction wise than TEO for the centroid analysis. -randomize = 0; -for temp_resolution = [1, 2, 5, 10] %1sec, 500msec, 200msec, 100msec - %temp_resolution = [1/5, 1/2, 1, 5, 10] %5sec, 2sec, 1sec,500msec, 100msec - %1 for second resolution, 10 for 100msec resolution, 100 for 10msec resolution, 1000 for msec resolution. etc. - %0.1 for 10sec resolution, 1/5 for 5sec resolution - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; channel_flag = "all"; - for channel_flag = ["vlPFC", "TEO", "all"] - - channel = char(channel_flag);%for later saving - - %Get data with specified temporal resolution and channels - [Spike_rasters, labels, behav_categ, block_times, monkey]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac); - %filePath is the experimental data path - %Temp_resolution is the temporal resolution at which we would like to - %analyze the dat - %Channel_flag specifies with channels to include: only TEO array, only - %vlPFC array or all channels - disp('Data Loaded') - - Labels = cell2mat(labels(:,3)); %Get numerical labels based on prioritization - tabulate(Labels) - Data = Spike_rasters; %Get neural data - - Z_data = zscore(Data,[],2)'; - %% Sort and summarize data - - %Quick histogram of behaviors - - C = categorical(Labels,1:length(behav_categ),behav_categ); - figure() - histogram(C,behav_categ(1:end-1)) %removing "rest" since it dominates data - pause(1); close all - - - %Take advantage of numeric labels to get neural activity associated with - %each time a behavior was present. %Not sure the sorting really helps but - %makes it easier to check things. - - - LD_holding = [Labels Z_data];%[Labels Data_group];% %This keeps matrix for all behaviors, now first column is labels - - boi = [5,7:10,21]; %manually set behaviors of interest - not_boi = setdiff(1:length(behav_categ), boi); - - index_use = LD_holding(:,1)==boi; %creates numel(boi) vectors of logical indecies for every time point - index_use = sum(index_use,2)>0; %has ones know where one of the behaviors of interest happened - - LD_tog = LD_holding(index_use,:); - - %% Dim reduction, calculate centriod - % Only doing this to check linear embedding dimension and to aid visualization. - %2021-12-3 copying Camille's method to select behaviors to analyze instead - %of using whole data set at once - %Just set boi to different values if you want different numbers of - %behaviors - - [coeff, score,latent,~,explained] = pca(LD_tog(:,2:end), 'Centered', false); - - if choose_numcom - - num_component = man_num;% set desired number of components to use instead of using what explains 85% of the variance - - else - - num_component = find(cumsum(explained)>85,1,'first'); %use number of components that explains more than 85% of the variance - - end - - if num_component > 20 %want to use 10 or less, 20 is limit where nearest neighbor falls apart for high dim data - - disp(['number of components = ' num2str(num_component)]) - warning('Using more than 20 components in dim reduced space. Changing from L2 to L1 measure') - - end - - - %note, don't need to do reconstruction to see things in pca space, just use - %score variable - - - DR_data = score(:,1:num_component); - - - centriods = cell(length(behav_categ),1); - %radii = zeros(length(behav_categ),1); %drawing spheres at some point? - - var_sum = cumsum(explained); - figure; plot(1:10:length(var_sum),var_sum(1:10:end),'.k'); xlabel('PCs used'); ylabel('var explained'); - figure; - scatter3(DR_data(:,1), DR_data(:,2),DR_data(:,3),12); title('Data in PCA space'); - figure; hold on - color = hsv(length(boi)); - - for b = 1:length(boi) - - scatter3(DR_data(LD_tog(:,1)==boi(b),1), DR_data(LD_tog(:,1)==boi(b),2),DR_data(LD_tog(:,1)==boi(b),3),... - 12,color(b,:),'filled'); - - centriods{boi(b)} = mean(DR_data(LD_tog(:,1)==boi(b),:),1); - - end - - - %% Predict state from closest centriod - - %Try training and testing - %Try moving centriod closer - - dis_mat = nan(length(DR_data),length(boi)); %initialize distance matrix for distance between - - %As a check add noise which should tank performance - %Check passed...still surprised at how well this works. - - - use_noise = 0; %Note: check average distance and make sure to change noise parameters accordingly - - %Trying other noise implementation too where use it on DR_data directly; - %DR_data=DR_data_hold; - - noise_dr = 0; - - DR_data_hold = DR_data; - DR_data = DR_data+noise_dr*normrnd(3,1,size(DR_data)); - - for b=1:length(boi) - - if num_component > 20 %use L1 norm - - dis_mat(:,b) = vecnorm(DR_data'-centriods{boi(b)}',1)+use_noise*normrnd(200,100,1,length(dis_mat)); %works more easily if everything is column vector - - else %use L2 norm - - dis_mat(:,b) = vecnorm(DR_data'-centriods{boi(b)}',2)+use_noise*normrnd(200,100,1,length(dis_mat)); - - end - - - - end - - [~,cs] = min(dis_mat,[],2); %Find which centriod the activity was closer to - - preds = boi(cs); %Predict behavior that was that centriod - - %plot centroid results - close all; figure; set(gcf,'Position',[150 250 1700 800]) - plot(LD_tog(:,1), 'LineWidth',2) - hold on - plot(preds, 'LineWidth',2) - yticks([0:length(behav_categ)+1]); - ticklabs = behav_categ; ticklabs(not_boi)={' '}; - yticklabels({'',ticklabs{:},''}) - ylim([0 length(behav_categ)+1]) - legend('Real','Predicted State', 'FontSize',16) - ylabel('Behavior', 'FontSize',18) - xlabel('Time', 'FontSize',18) - %xlabel('index') - ax = gca; - ax.FontSize = 14; - per_cor(temp, chan) = sum(LD_tog(:,1)==preds')/length(preds)*100 - title(['Predicted based on neural data vs. Real behavioral state. Resolution: ' num2str(1000/temp_resolution) 'msec. Area: ' channel '. Accuracy: ' num2str(round(per_cor(temp, chan))) '%']) - - -% cd(savePath) -% saveas(gcf,[savePath '/Centroid_' num2str(1000/temp_resolution) 'msec_' channel '.png']) - pause(1); close all - - %% Predict behavioral state from neural data using multinomial regression - - -if run_cvmnr - - - behavs = categorical(LD_tog(:,1),boi,{behav_categ{boi}}); %create categorical array for use with mnrfit - if randomize %As it should be, get same result shuffling either way. - behavs = behavs(randperm(length(behavs))); %trying shuffling the data instead - % DR_data = DR_data(randperm(length(DR_data)),:); - end - - % Set up k-fold cross-validation - %Matlab has a built in k-fold function but it isn't clear if it interfaces - %with mnrfit, so just going to code my own. Going with 10 portions instead - %of 10 grabs - - folds = 10; - - sample_size = floor(length(LD_tog(:,1))/folds); - - sam_if = ones(folds,1)*sample_size; %samples in each fold - - - if mod(length(LD_tog(:,1)),folds)>0 %check if not divisible by 10 - disp('Samples do not divide evenly accross folds') - %if it does not divide evenly, add remainder randomly to one of the - %folds - ind = randsample(folds,1); - sam_if(ind) = sam_if(ind) + mod(length(LD_tog(:,1)),folds); - - end - - - %inds_groups = [1 cumsum(sam_if)']; %Doing the other implementation of shuffling all of the inds and grabbing the number of inds set above - - shuffledind = randperm(length(behavs)); - - ind_if = cell(folds,1); - - ind_if{1} = shuffledind(1:sam_if(1)); %set the first group out of the loop - - groups = cumsum(sam_if); - - if sum(sam_if)~=length(LD_tog(:,1)) - - error('Missing inds from shuffle') - - end - - - for i=2:folds - - ind_if{i} = shuffledind(groups(i-1)+1:groups(i)); - - end - - %Run multinomial regression - %2021-12-07 update: non-trival to increase the number of iterations mnrfit - %does...so will need to work around this or take the time to make something more custom...or switch to python. - - cv_per_cor = nan(folds,1); - cv_preds = cell(folds,1); - cv_behavs = cell(folds,1); - - for k = 1:folds - - disp(['iteration: ' num2str(k)]) - - states = behavs(ind_if{k},1); - - cv_behavs{k} = states; - - foldsidx = 1:folds; - - trainingidx = horzcat(ind_if{foldsidx~=k})'; %train on all indecies that aren't in the current fold - - [Betas,~,stats] = mnrfit(DR_data(trainingidx,:),behavs(trainingidx,1)); %note always get B is dim predictors+1 for the intercept term x length(boi)-1 as one behavior is selected as reference - - [pihat, ~,~] = mnrval(Betas,DR_data(ind_if{k},:),stats); %give probabilities for each behavior - - %take max of each predicted probability as the predicted behavioral state - %like above - - - [~,cs]= max(pihat,[],2); %Find which centriod the activity was closer to - - preds = boi(cs); %Predict behavior that was that centriod - - cv_preds{k} = preds; - - %preds = categorical(preds,boi,{Label_struct.behav_categ{boi}}); %use same categorical trick above so can do string compare - - - - % figure() %Have to wait for end to concatenate everything and unscramble - % plot(LD_tog(s_inds,1)) - % hold on - % plot(preds(s_inds)) - % title('Multinomial Regression Predictions') - - - per_cor_mnr = sum(LD_tog(ind_if{k},1)==preds')/length(preds)*100; %strcmp(behavs(ind_if{k}),preds) - - cv_per_cor(k) = per_cor_mnr; - - - - end - - per_cor_cvmnr(temp,chan) = mean(cv_per_cor) - -end - - %Think about how to make plot of CV tomorrow - - - - close all - - - clearvars -except randomize temp chan channel_flag temp_resolution per_cor savePath filePath boi choose_numcom man_num is_mac per_cor_cvmnr run_cvmnr - - chan = chan +1; - end - temp = temp+1; -end - -rowNames = ["1sec", "500msec", "200msec", "100msec"]; colNames = ["vlPFC","TEO","all"]; -%rowNames = ["1sec"]; colNames = ["vlPFC","TEO","all"]; -result_hitrate = array2table(per_cor_cvmnr,'RowNames',rowNames,'VariableNames',colNames) - -save([savePath '\MNRegression_results_allBehav.mat'], 'per_cor', 'per_cor_cvmnr', 'boi') -writetable(result_hitrate,[savePath '\MNRegression_results_allBehav.csv'],'WriteRowNames',true,'WriteVariableNames',true); - -%Plot centroid result -figure; hold on; set(gcf,'Position',[150 250 1000 500]) -cmap = cool(size(per_cor,1)); -for b = 1:size(per_cor,1) - y = per_cor(b,:); - x = 1:size(per_cor,2); - plot(x,y,'s','MarkerSize',15,... - 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) - %plot(x,y,'Color','k') -end -chance_level = 1/length(boi)*100; yline(chance_level,'--','Chance level', 'FontSize',16) -leg = legend("1sec","500msec","200msec","100msec","chance", 'Location','southwest'); -title(leg,'Window size') -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 100]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('%Accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Accuracy of behavioral states prediction based on neural data','FontSize', 20) -cd(savePath) -saveas(gcf,['Centroid_results_allBehav.png']) -close all - -%Plot multinomial regression results -%load([savePath '\MNRegression_results_allBehav.mat']) -figure; hold on; set(gcf,'Position',[150 250 1000 500]) -cmap = cool(size(per_cor,1)); -for b = 1:size(per_cor,1) - y = per_cor_cvmnr(b,:); - x = 1:size(per_cor,2); - plot(x,y,'s','MarkerSize',15,... - 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) - %plot(x,y,'Color','k') -end -chance_level = 1/length(boi)*100; yline(chance_level,'--','Chance level', 'FontSize',16) -leg = legend("1sec","500msec","200msec","100msec","chance", 'Location','southwest'); -title(leg,'Window size') -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 100]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('%Accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Accuracy of behavioral states prediction based on neural data','FontSize', 20) -cd(savePath) -saveas(gcf,['MNRegression_results_allBehav.png']) - -% -% is_still_ron = 1; -% -% if is_still_ron -% -% cd('C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger\Population trajectory') -% -% end \ No newline at end of file diff --git a/Varia/Population trajectory/log_get_trajectory_coordinates.m b/Varia/Population trajectory/log_get_trajectory_coordinates.m deleted file mode 100644 index e279852..0000000 --- a/Varia/Population trajectory/log_get_trajectory_coordinates.m +++ /dev/null @@ -1,9 +0,0 @@ -%get trajectory coordinates -%Find figure handles from data high Projection3D (GUI) -cd('C:\Users\Camille Testard\Desktop\Grants_Fellowships\R37_Grant_renewal\pilot_data\session2') -load('trajectory_data.mat'); close all - -%get new epoch starts -D.epochStarts = handles.D.epochStarts; -%Get trajectory coordinates -p = handles.orths * handles.D(1).data; close all diff --git a/Varia/Population trajectory/log_mnreg_subsample.m b/Varia/Population trajectory/log_mnreg_subsample.m deleted file mode 100644 index e0f0527..0000000 --- a/Varia/Population trajectory/log_mnreg_subsample.m +++ /dev/null @@ -1,261 +0,0 @@ -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1;%set the smoothing window size (sigma) -var_explained_threshold=90; -num_iter = 500; num_units = 100; -min_occurrences = 60; -behav=5;%[1,5,18,29]; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -dim=nan([length(session_range),2,length(behav),2,num_iter]); - -s=1; - -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/Dimensionality_results/']; - - chan = 1; - - %pre-choose number of features to use or use 85% variance threshold for PCA - choose_numcom = 1; man_num = 20; %update 2021-12-06 this doesn't seem to effect the trend of vlPFC being worse prediction wise than TEO for the centroid analysis. - randomize = 0; - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; channel_flag = "all"; - for channel_flag = ["vlPFC", "TEO", "all"] - - channel = char(channel_flag);%for later saving - - %Get data with specified temporal resolution and channels - [Spike_rasters, labels, behav_categ, block_times, monkey]= log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, is_mac); - %filePath is the experimental data path - %Temp_resolution is the temporal resolution at which we would like to - %analyze the dat - %Channel_flag specifies with channels to include: only TEO array, only - %vlPFC array or all channels - disp('Data Loaded') - - Labels = cell2mat(labels(:,3)); %Get numerical labels based on prioritization - tabulate(Labels) - Data = Spike_rasters; %Get neural data - - Z_data = zscore(Data,[],2)'; - %% Sort and summarize data - - %Quick histogram of behaviors - - C = categorical(Labels,1:length(behav_categ),behav_categ); - figure() - histogram(C,behav_categ(1:end-1)) %removing "rest" since it dominates data - pause(1); close all - - - %Take advantage of numeric labels to get neural activity associated with - %each time a behavior was present. %Not sure the sorting really helps but - %makes it easier to check things. - - - LD_holding = [Labels Z_data];%[Labels Data_group];% %This keeps matrix for all behaviors, now first column is labels - - boi = [5,7:10]; %[4:8, 17] %manually set behaviors of interest - not_boi = setdiff(1:length(behav_categ), boi); - - index_use = LD_holding(:,1)==boi; %creates numel(boi) vectors of logical indecies for every time point - index_use = sum(index_use,2)>0; %has ones know where one of the behaviors of interest happened - - LD_tog_temp = LD_holding(index_use,:); - - %% Run MNRegression over multiple iterations - num_iter = 100; - disp('Start running MNRegression...') - - for iter = 1:num_iter - - Labels = LD_tog_temp(:,1); - unqLabels = unique(Labels); NumOfClasses = length(unqLabels); - minNumTrials = 90;%min(num_trials); %find the minimum one %CT change to have 200 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Labels == unqLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - - LD_tog = LD_tog_temp(chosen_trials,:); - - % Run PCA - [coeff, score,latent,~,explained] = pca(LD_tog(:,2:end), 'Centered', false); - - if choose_numcom - num_component = man_num;% set desired number of components to use instead of using what explains 85% of the variance - else - num_component = find(cumsum(explained)>85,1,'first'); %use number of components that explains more than 85% of the variance - end - - DR_data = score(:,1:num_component); - - % Run k-fold cross-validation - - behavs = categorical(LD_tog(:,1),boi,{behav_categ{boi}}); %create categorical array for use with mnrfit - if randomize - behavs = behavs(randperm(length(behavs),length(behavs))); - end - - % Set up k-fold cross-validation - %Matlab has a built in k-fold function but it isn't clear if it interfaces - %with mnrfit, so just going to code my own. Going with 10 portions instead - %of 10 grabs - - folds = 5; - - sample_size = floor(length(LD_tog(:,1))/folds); - - sam_if = ones(folds,1)*sample_size; %samples in each fold - - - if mod(length(LD_tog(:,1)),folds)>0 %check if not divisible by folds - disp('Samples do not divide evenly accross folds') - %if it does not divide evenly, add remainder randomly to one of the - %folds - ind = randsample(folds,1); - sam_if(ind) = sam_if(ind) + mod(length(LD_tog(:,1)),folds); - - end - - - %inds_groups = [1 cumsum(sam_if)']; %Doing the other implementation of shuffling all of the inds and grabbing the number of inds set above - - shuffledind = randperm(length(behavs)); - ind_if = cell(folds,1); - ind_if{1} = shuffledind(1:sam_if(1)); %set the first group out of the loop - groups = cumsum(sam_if); - - if sum(sam_if)~=length(LD_tog(:,1)) - error('Missing inds from shuffle') - end - - for i=2:folds - ind_if{i} = shuffledind(groups(i-1)+1:groups(i)); - end - - %Run multinomial regression - %2021-12-07 update: non-trival to increase the number of iterations mnrfit - %does...so will need to work around this or take the time to make something more custom...or switch to python. - - cv_per_cor = nan(folds,1); - cv_preds = cell(folds,1); - cv_behavs = cell(folds,1); - - for k = 1:folds - - disp(['iteration: ' num2str(k)]) - states = behavs(ind_if{k},1); - cv_behavs{k} = states; - foldsidx = 1:folds; - trainingidx = horzcat(ind_if{foldsidx~=k})'; %train on all indices that aren't in the current fold - - [Betas,~,stats] = mnrfit(DR_data(trainingidx,:),behavs(trainingidx,1)); %note always get B is dim predictors+1 for the intercept term x length(boi)-1 as one behavior is selected as reference - - [pihat, ~,~] = mnrval(Betas,DR_data(ind_if{k},:),stats); %give probabilities for each behavior - - %take max of each predicted probability as the predicted behavioral state - %like above - - [~,cs]= max(pihat,[],2); %Find which centriod the activity was closer to - - preds = boi(cs); %Predict behavior that was that centriod - - cv_preds{k} = preds; - - %preds = categorical(preds,boi,{Label_struct.behav_categ{boi}}); %use same categorical trick above so can do string compare - per_cor_mnr = sum(LD_tog(ind_if{k},1)==preds')/length(preds)*100; %strcmp(behavs(ind_if{k}),preds) - - cv_per_cor(k) = per_cor_mnr; - - end - - per_cor_cvmnr(iter) = mean(cv_per_cor); - - - - disp(['MNReg run' num2str(iter) '/' num2str(num_iter)]) - end - - per_cor_cvmnr_mean(temp, chan) = mean(per_cor_cvmnr) - per_cor_cvmnr_sd(temp, chan) = std(per_cor_cvmnr) - - close all - - clearvars -except randomize temp chan channel_flag temp_resolution per_cor savePath filePath boi per_cor_cvmnr_sd choose_numcom man_num is_mac per_cor_cvmnr_mean - chan = chan +1; - end - temp = temp+1; -end - -rowNames = ["1sec", "500msec", "200msec", "100msec"]; colNames = ["vlPFC","TEO","all"]; -%rowNames = ["1sec"]; colNames = ["vlPFC","TEO","all"]; -result_hitrate = array2table(per_cor_cvmnr_mean,'RowNames',rowNames,'VariableNames',colNames) - -save([savePath '\MNRegression_results_allBehav.mat'], 'per_cor_cvmnr_mean', 'per_cor_cvmnr_sd', 'boi') -writetable(result_hitrate,[savePath '\MNRegression_results_allBehav.csv'],'WriteRowNames',true,'WriteVariableNames',true); - -%Plot multinomial regression results -%load([savePath '\MNRegression_results_allBehav.mat']) -figure; hold on; set(gcf,'Position',[150 250 1000 500]) -cmap = cool(size(per_cor_cvmnr_mean,1)); -for b = 1:size(per_cor_cvmnr_mean,1) - y = per_cor_cvmnr_mean(b,:); - std_dev = per_cor_cvmnr_sd(b,:); - errorbar(y,std_dev,'s','MarkerSize',10,... - 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) -end -chance_level = 1/length(boi)*100; yline(chance_level,'--','Chance level', 'FontSize',16) -leg = legend("1sec","500msec","200msec","100msec","chance", 'Location','southwest'); -title(leg,'Window size') -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 100]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('%Accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Accuracy of behavioral states prediction based on neural data','FontSize', 20) -cd(savePath) -saveas(gcf,['MNRegression_results_6Behav.png']) - -% -% is_still_ron = 1; -% -% if is_still_ron -% -% cd('C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger\Population trajectory') -% -% end \ No newline at end of file diff --git a/Varia/Population trajectory/log_mnreg_subsample_v2.m b/Varia/Population trajectory/log_mnreg_subsample_v2.m deleted file mode 100644 index b933fef..0000000 --- a/Varia/Population trajectory/log_mnreg_subsample_v2.m +++ /dev/null @@ -1,342 +0,0 @@ -%% Multinomial regressionn -% Get probability of a behavior based on neural data - -%Set session list -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); -session_range_no_partner=[1:6,11:13,15:16,18]; -session_range_with_partner=[1:6,11:13,15:16,18]; - -%Set parameters -with_partner =0; -temp = 1; temp_resolution = 1; -channel_flag = "all"; -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly= 0;%Only consider isolated units. 0=all units; 1=only well isolated units -smooth= 1; %smooth the data -sigma = 1;%set the smoothing window size (sigma) -num_iter = 500; - -%Select session range: -if with_partner ==1 - session_range = session_range_with_partner; - a_sessions = 1:3; h_sessions = 11:13; -else - session_range = session_range_no_partner; - a_sessions = 1:6; h_sessions = [11:13,15:16]; -end - -s=1; - -for s =session_range %1:length(sessions) - - %Set path - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/']; - - chan = 1; - - %Set channels: 'TEO', 'vlPFC' or 'all' - chan = 1; channel_flag = "vlPFC"; - - for channel_flag = ["vlPFC", "TEO", "all"] - - - channel = char(channel_flag);%for later saving - - %% Get data with specified temporal resolution and channels - %Get data with specified temporal resolution and channels - if with_partner ==1 - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= ... - log_GenerateDataToRes_function(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - else - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, ... - reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label, behavior_log, behav_categ_original]= ... - log_GenerateDataToRes_function_temp(filePath, temp_resolution, channel_flag, ... - is_mac, with_NC, isolatedOnly, smooth, sigma); - end - - disp('Data Loaded') - - %% Extract labels - - Labels = cell2mat(labels(:,3)); %Get numerical labels based on prioritization - - %Adjust labels - Labels(Labels==find(behav_categ=="Squeeze partner"))=find(behav_categ=="Threat to partner"); - Labels(Labels==find(behav_categ=="Squeeze Subject"))=find(behav_categ=="Threat to subject"); - Labels(Labels==find(behav_categ=="Proximity"))=length(behav_categ); %Make proximity equal to rest - - %Lump all aggressive interactions together - Labels(Labels==find(behav_categ=="Threat to partner"))=find(behav_categ=="Aggression"); - Labels(Labels==find(behav_categ=="Threat to subject"))=find(behav_categ=="Aggression"); - - %Lump all travel together - Labels(Labels==find(behav_categ=="Approach"))=find(behav_categ=="Travel"); - Labels(Labels==find(behav_categ=="Leave"))=find(behav_categ=="Travel"); - - tabulate(Labels) - Data = Spike_rasters; %Get neural data - - - Z_data = zscore(Data,[],2)'; - - - %% Sort and summarize data - - %Quick histogram of behaviors - - C = categorical(Labels,1:length(behav_categ),behav_categ); - figure() - histogram(C,behav_categ) %removing "rest" since it dominates data - pause(1); close all - - - %Take advantage of numeric labels to get neural activity associated with - %each time a behavior was present. %Not sure the sorting really helps but - %makes it easier to check things. - - - LD_holding = [Labels Z_data];%[Labels Data_group];% %This keeps matrix for all behaviors, now first column is labels - - boi = [5,7,24,29]; %[4:8, 17] %manually set behaviors of interest - not_boi = setdiff(1:length(behav_categ), boi); - - index_use = LD_holding(:,1)==boi; %creates numel(boi) vectors of logical indecies for every time point - index_use = sum(index_use,2)>0; %has ones know where one of the behaviors of interest happened - - LD_tog_temp = LD_holding(index_use,:); - - labels_temp = LD_tog_temp(:,1); - classes = unique(labels_temp); - for i = 1:length(classes) %for each class - idx = find(labels_temp == classes(i)); %find indexes of trials belonging to this class - labels_temp(idx)=i; - end - LD_tog_temp(:,1) = labels_temp; - -% % % % %%CAMILLE TEST -% % % % % Run PCA -% % % % -% % % % [coeff, score,latent,~,explained] = pca(LD_tog_temp(:,2:end), 'Centered', false); -% % % % num_component = find(cumsum(explained)>85,1,'first'); -% % % % figure; plot(cumsum(explained)) -% % % % -% % % % data = score(:,200); -% % % % [B,~,stats] = mnrfit(data,LD_tog_temp(:,1)); -% % % % pihat = mnrval(B,data,stats); -% % % % figure; hist(pihat(:,4)) -% % % % -% % % % behav = 4; -% % % % figure; hold on -% % % % plot(pihat) -% % % % labels_plot = nan(size(Labels)); labels_plot(Labels==behav)=1; -% % % % plot(labels_plot) - %%%%%%%%%%%%%%%%%%%% - - %% Run MNRegression over multiple iterations - num_iter = 100; - disp('Start running MNRegression...') - - for iter = 1:num_iter - - Lbls = LD_tog_temp(:,1); - data_details = tabulate(Lbls); - unqLabels = unique(Lbls); NumOfClasses = length(unqLabels); - minNumTrials = min(data_details(:,2)); %find the minimum one %CT change to have 200 of each class - chosen_trials = []; - for i = 1:NumOfClasses %for each class - idx = find(Lbls == unqLabels(i)); %find indexes of trials belonging to this class - rand_i = randsample(length(idx), minNumTrials); %Select a random n number of them - chosen_trials = [chosen_trials; idx(rand_i)]; %Put the selected trials in a matrix, ordered by class - end - - LD_tog = LD_tog_temp(chosen_trials,:); - - % Run PCA - [coeff, score,latent,~,explained] = pca(LD_tog_temp(:,2:end), 'Centered', false); - [~, score_allD,~,~,explained_allD] = pca(LD_holding(:,2:end), 'Centered', false); - - % if choose_numcom - % num_component = man_num;% set desired number of components to use instead of using what explains 85% of the variance - % else - num_component = find(cumsum(explained)>90,1,'first'); %use number of components that explains more than 85% of the variance - num_component_allD = find(cumsum(explained_allD)>90,1,'first'); - % end - - %DR_data = score(chosen_trials,1:num_component); - DR_data = LD_tog_temp(chosen_trials,2:end); - - %CT test code - [B,~,stats] = mnrfit(DR_data,LD_tog(:,1)); - %pihat = mnrval(B,score(:,1:num_component),stats); - pihat = mnrval(B,LD_tog_temp(:,2:end),stats); - pihat_allD = mnrval(B,score_allD(:,1:num_component),stats); - [~,cs]= max(pihat,[],2); %get max predictions - [~,cs_allD]= max(pihat_allD,[],2); - - %check the distribution is bimodal - behav = 7; - figure; hist(pihat(:,find(boi==behav))) - figure; hist(pihat_allD(:,find(boi==behav))) - - %Only considering behaviors of interest - figure; hold on - plot(pihat(:,find(boi==behav))) - pred_plot = nan(size(cs)); pred_plot(cs==find(boi==behav))=1; - plot(pred_plot, 'LineWidth',6) - labels_plot = nan(size(Lbls)); labels_plot(Lbls==find(boi==behav))=1.05; - plot(labels_plot, 'LineWidth',6) - ylim([0 1.1]) - - figure; hold on - for b=1:length(boi) - pred_plot = nan(size(cs)); pred_plot(cs==b)=b; - plot(pred_plot, 'LineWidth',3) - labels_plot = nan(size(Lbls)); labels_plot(Lbls==b)=b-0.1; - plot(labels_plot, 'LineWidth',3) - end - ylim([0 length(boi)+1]) - - %Plotting pihat before and after start of event - behav_times = zeros(size(Lbls)); behav_times(Lbls==find(boi==behav))=1; - behav_starts = find(diff(behav_times)>0)+1; - behav_ends = find(diff(behav_times)<0); - bout_length = behav_ends-behav_starts; - behav_starts_final = behav_starts(bout_length>=10); - -% %Check the ends and start fall at the right times -% figure; hold on -% plot(behav_times) -% for i = 1:length(behav_starts) -% xline(behav_starts(i), 'Color','r','LineWidth',2) -% xline(behav_ends(i), 'Color','g','LineWidth',2) -% end - - window = 5; - - pihat_around_behav = nan(length(behav_starts_final),window*2+1); - for i = 1:length(behav_starts_final) - pihat_around_behav(i,:) = pihat_allD(behav_starts(i)-window: behav_starts(i)+window,find(boi==behav)); - end - - figure; hold on - ylim([0 1.05]); xlim([0 window*2+1]); - xline(window+1, 'Color','r','LineWidth',3) - plot(mean(pihat_around_behav), 'LineWidth',3) - plot(pihat_around_behav', 'Color',[0.5 0.5 0.5]) - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - %Considering the whole session - - %Plot pihat, predicted times for behavior 'behav' and actual - %times. - figure; hold on - plot(pihat_allD(:,find(boi==behav))) - pred_plot = nan(size(cs_allD)); pred_plot(cs_allD==find(boi==behav))=1; - plot(pred_plot, 'LineWidth',6) - labels_plot = nan(size(LD_holding(:,1))); labels_plot(LD_holding(:,1)==behav)=1.05; - plot(labels_plot, 'LineWidth',6) - ylim([0 1.1]) - - %Plot predicted vs actual behaviors - figure; hold on - for b=1:length(boi) - pred_plot = nan(size(cs_allD)); pred_plot(cs_allD==b)=b; - plot(pred_plot, 'LineWidth',3) - labels_plot = nan(size(LD_holding(:,1))); labels_plot(LD_holding(:,1)==boi(b))=b-0.1; - plot(labels_plot, 'LineWidth',3) - end - ylim([0 length(boi)+1]) - - %Plotting pihat before and after start of event - idx_behav = find(~cellfun(@isempty,(strfind(table2cell(behavior_log(:,'Behavior')), behav_categ_original(behav))))); - idx_dur = find(table2array(behavior_log(:,'duration_round'))>3); - idx = intersect (idx_behav, idx_dur); - behav_starts = table2array(behavior_log(idx,"start_time_round")); %start times in msec - - window = 5; - - pihat_around_behav = nan(length(behav_starts),window*2+1); - for i = 1:length(behav_starts) - pihat_around_behav(i,:) = pihat_allD(behav_starts(i)-window: behav_starts(i)+window,find(boi==behav)); - end - - figure; hold on - ylim([0 1.05]); xlim([0 window*2+1]); - xline(window+1, 'Color','r','LineWidth',3) - plot(mean(pihat_around_behav), 'LineWidth',3) - plot(pihat_around_behav', 'Color',[0.5 0.5 0.5]) - - - -% % %preds = categorical(preds,boi,{Label_struct.behav_categ{boi}}); %use same categorical trick above so can do string compare -% % per_cor_mnr = sum(LD_tog(ind_if{k},1)==preds')/length(preds)*100; %strcmp(behavs(ind_if{k}),preds) -% % -% % cv_per_cor(k) = per_cor_mnr; -% % -% % per_cor_cvmnr(iter) = mean(cv_per_cor); - - - - disp(['MNReg run' num2str(iter) '/' num2str(num_iter)]) - end - - per_cor_cvmnr_mean(temp, chan) = mean(per_cor_cvmnr) - per_cor_cvmnr_sd(temp, chan) = std(per_cor_cvmnr) - - close all - - clearvars -except randomize temp chan channel_flag temp_resolution per_cor savePath filePath boi per_cor_cvmnr_sd choose_numcom man_num is_mac per_cor_cvmnr_mean - chan = chan +1; - end - temp = temp+1; -end - -rowNames = ["1sec", "500msec", "200msec", "100msec"]; colNames = ["vlPFC","TEO","all"]; -%rowNames = ["1sec"]; colNames = ["vlPFC","TEO","all"]; -result_hitrate = array2table(per_cor_cvmnr_mean,'RowNames',rowNames,'VariableNames',colNames) - -save([savePath '\MNRegression_results_allBehav.mat'], 'per_cor_cvmnr_mean', 'per_cor_cvmnr_sd', 'boi') -writetable(result_hitrate,[savePath '\MNRegression_results_allBehav.csv'],'WriteRowNames',true,'WriteVariableNames',true); - -%Plot multinomial regression results -%load([savePath '\MNRegression_results_allBehav.mat']) -figure; hold on; set(gcf,'Position',[150 250 1000 500]) -cmap = cool(size(per_cor_cvmnr_mean,1)); -for b = 1:size(per_cor_cvmnr_mean,1) - y = per_cor_cvmnr_mean(b,:); - std_dev = per_cor_cvmnr_sd(b,:); - errorbar(y,std_dev,'s','MarkerSize',10,... - 'MarkerEdgeColor',cmap(b,:),'MarkerFaceColor',cmap(b,:)) -end -chance_level = 1/length(boi)*100; yline(chance_level,'--','Chance level', 'FontSize',16) -leg = legend("1sec","500msec","200msec","100msec","chance", 'Location','southwest'); -title(leg,'Window size') -xticks([0.8 1 2 3 3.2]); xlim([0.8 3.2]); ylim([0 100]) -xticklabels({'','vlPFC','TEO','all',''}) -ax = gca; -ax.FontSize = 14; -ylabel('%Accuracy','FontSize', 18); xlabel('Brain area','FontSize', 18) -title('Accuracy of behavioral states prediction based on neural data','FontSize', 20) -cd(savePath) -saveas(gcf,['MNRegression_results_6Behav.png']) - -% -% is_still_ron = 1; -% -% if is_still_ron -% -% cd('C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger\Population trajectory') -% -% end \ No newline at end of file diff --git a/Varia/Population trajectory/rwd_centroidtraj.m b/Varia/Population trajectory/rwd_centroidtraj.m deleted file mode 100644 index 32d5860..0000000 --- a/Varia/Population trajectory/rwd_centroidtraj.m +++ /dev/null @@ -1,360 +0,0 @@ -%% Running Notes -%As first pass analysis for trajectories/manifold idea, finding population -%average (centroid) for each behavior and using this to predict moment to -%moment behavioral state. - -%May need to reduce the number of components used so that MLE has an easier -%time for the multinomial regression (i.e. manual number of components) - -%% Load in data and preprocess - - -is_ron = 1; - -if is_ron >0 - load_folder = 'C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_data\'; -else - load_folder = '~/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/'; -end - -session = 'Amos_2021-07-29'; - -%Pull need variables, push struct into separate variables in case need info -%later - -Label_struct = load([load_folder session '/Labels_per_sec.mat']); -Data_struct = load([load_folder session '/Neural_data.mat']); - -Labels = cell2mat(Label_struct.labels(:,3)); %Get numerical labels based on prioritization -Data = Data_struct.Unit_rasters; %Get neural data - -Z_data = zscore(Data,[],2)';%Data'; -%note: Z-score increases dim relative to not z-scoring as neurons that fire -%more can't dominate the overall signal. Not an issue but may be worth -%keeping in mind -%% Sort and summarize data - -%Quick histogram of behaviors - -C = categorical(Labels,1:20,Label_struct.behav_categ); -figure() -histogram(C,Label_struct.behav_categ(1:end-1)) %removing rest since it dominates data - - -%Take advantage of numeric labels to get neural activity associated with -%each time a behavior was present. %Not sure the sorting really helps but -%makes it easier to check things. - - -LD_holding = [Labels Z_data];%[Labels Data_group];% %This keeps matrix for all behaviors, now first column is labels - -boi = [1:17]; %manually set behaviors of interest - -index_use = LD_holding(:,1)==boi; %creates numel(boi) vectors of logical indecies for every time point -index_use = sum(index_use,2)>0; %has ones know where one of the behaviors of interest happened - -LD_tog = LD_holding(index_use,:); - -%undoing the sorting is not working as predicted...need to do a differen -%procedure it seems fix that here and then fix it in the main -%code...removing the sorting is now causing some kind of weird response and -%tanking performance. Not really clear on what is happening but it seems like -%some offset between the data and behaviors is being introduced as -%performance drops to the same level as when didn't sort LD_tog but s_inds -%was still defined. Going to try shuffling behaviors than sorting to make -%sure this isn't bs. - -%Sanity check only - -%LD_tog(:,1) = LD_tog(randperm(length(LD_tog)),1); %shuffle labels while sorting to see if the metric is false as there is now no relation between states and neurons - -%Look like metric is inflated by this since now it just need to get the -%number of point right rather than the actual timing. Removing all -%sorting. - - -%% Dim reduction, calculate centriod -% Only doing this to check linear embedding dimension and to aid visualization. -%2021-12-3 copying Camille's method to select behaviors to analyze instead -%of using whole data set at once -%Just set boi to different values if you want different numbers of -%behaviors - -man_num = 1; %user chooses number of components to use - -[coeff, score,latent,~,explained] = pca(LD_tog(:,2:end), 'Centered', true); - - -centriods = cell(length(Label_struct.behav_categ),1); -%radii = zeros(length(Label_struct.behav_categ),1); %drawing spheres at some point? - -var_sum = cumsum(explained); - figure; plot(var_sum(1:end),'.k'); xlabel('PCs used'); ylabel('var explained'); - -if man_num - - num_component = input('Select number of PCs to use: '); - -else - - num_component = find(cumsum(explained)>85,1,'first'); %use number of components that explains more than 85% of the variance - - - -end - -if num_component > 20 %want to use 10 or less, 20 is limit where nearest neighbor falls apart for high dim data - - disp(['number of components = ' num2str(num_component)]) - warning('Using more than 20 components in dim reduced space. Changing from L2 to L1 measure') - -end - -%note, don't need to do reconstruction to see things in pca space, just use -%score variable - -DR_data = score(:,1:num_component); - - -figure; -scatter3(DR_data(:,1), DR_data(:,2),DR_data(:,3),12); title('Data in PCA space'); -figure; hold on -color = hsv(length(boi)); - - - - - for b = 1:length(boi) - - scatter3(DR_data(LD_tog(:,1)==boi(b),1), DR_data(LD_tog(:,1)==boi(b),2),DR_data(LD_tog(:,1)==boi(b),3),... - 12,color(b,:),'filled'); - - centriods{boi(b)} = mean(DR_data(LD_tog(:,1)==boi(b),:),1); - - - - end - -% figure;hold on -% for b = 1:length(boi) -% -% scatter3(centriods{boi(b)}(1),centriods{boi(b)}(2),centriods{boi(b)}(3),12,color(b,:),'filled') -% -% end - - -%end - - -%% Predict state from closest centriod -%removing this for now and switching to the multinomial regression -% %Try training and testing -% %Try moving centriod closer -% -dis_mat = nan(length(DR_data),length(boi)); %initialize distance matrix for distance between - -%As a check add noise which should tank performance -%Check passed...still surprised at how well this works. - - -use_noise = 0; %Note: check average distance and make sure to change noise parameters accordingly - -%Trying other noise implementation too where use it on DR_data directly; -%DR_data=DR_data_hold; - -noise_dr = 0; - -DR_data_hold = DR_data; -DR_data = DR_data+noise_dr*normrnd(3,1,size(DR_data)); - -for b=1:length(boi) - - if num_component > 20 %use L1 norm - - dis_mat(:,b) = vecnorm(DR_data'-centriods{boi(b)}',1)+use_noise*normrnd(200,100,1,length(dis_mat)); %works more easily if everything is column vector - - else %use L2 norm - - dis_mat(:,b) = vecnorm(DR_data'-centriods{boi(b)}',2)+use_noise*normrnd(200,100,1,length(dis_mat)); - - end - - - -end - -[~,cs]= min(dis_mat,[],2); %Find which centriod the activity was closer to - -preds = boi(cs); %Predict behavior that was that centriod - - - -%plot step function -figure -plot(LD_tog(:,1), 'LineWidth',2) -hold on -plot(preds, 'LineWidth',2) -yticks([0:18]); -ticklabs = Label_struct.behav_categ; -yticklabels({'',ticklabs{boi},''}) -ylim([0 max(boi)+2]) -legend('Real','Predicted State', 'FontSize',16) -ylabel('Behavior', 'FontSize',18) -xlabel('Time', 'FontSize',18) -%xlabel('index') -ax = gca; -ax.FontSize = 14; -title('Predicted behavioral state based on neural data vs. Real behavioral state') - - -per_cor_centroid = sum(LD_tog(:,1)==preds')/length(preds)*100 - - -%% Trying DBSCAN for unsupervised clustering -%2021-12-06 update need to mess with paramters for this to work -%This requires a lot of massaging to work so I'm not sure it's worth using -%Update feel like this nees to be used on heavily reduced data (i.e. with -%UMAP or something) -% [idxs, corepts] = dbscan(DR_data,250,5); -% unique(idxs) -% gscatter(DR_data(:,1),DR_data(:,2),idx) -% - - -%% Trying multinomial regression instead of centriod analysis -%Note: should implement a k-fold cross validation loop for this for greater -%confidence in the results -%Further note: keep an eye for when MLE does and doesn't converge - -behavs = categorical(LD_tog(:,1),boi,{Label_struct.behav_categ{boi}}); %create categorical array for use with mnrfit - -%Matlab has a built in k-fold function but it isn't clear if it interfaces -%with mnrfit, so just going to code my own. Going with 10 portions instead -%of 10 grabs - -folds = 10; - -sample_size = floor(length(LD_tog(:,1))/folds); - -sam_if = ones(folds,1)*sample_size; %samples in each fold - - -if mod(length(LD_tog(:,1)),folds)>0 %check if not divisible by 10 - disp('Samples do not divide evenly accross folds') - %if it does not divide evenly, add remainder randomly to one of the - %folds - ind = randsample(folds,1); - sam_if(ind) = sam_if(ind) + mod(length(LD_tog(:,1)),folds); - -end - - -%inds_groups = [1 cumsum(sam_if)']; %Doing the other implementation of shuffling all of the inds and grabbing the number of inds set above - -shuffledind = randperm(length(behavs)); - -ind_if = cell(folds,1); - -ind_if{1} = shuffledind(1:sam_if(1)); %set the first group out of the loop - -groups = cumsum(sam_if); - - -if sum(sam_if)~=length(LD_tog(:,1)) - - error('Missing inds from shuffle') - -end - - -for i=2:folds - - ind_if{i} = shuffledind(groups(i-1)+1:groups(i)); - - -end - -%2021-12-07 update: non-trival to increase the number of iterations mnrfit -%does...so will need to work around this or take the time to make something more custom...or switch to python. - -cv_per_cor = nan(folds,1); -cv_preds = cell(folds,1); -cv_behavs = cell(folds,1); - -clear i %just incase messed up index somewhere - -for k = 1:folds - - disp(['iteration: ' num2str(k)]) - - states = behavs(ind_if{k},1); - - cv_behavs{k} = states; - - foldsidx = 1:folds; - - trainingidx = horzcat(ind_if{foldsidx~=k})'; %train on all indecies that aren't in the current fold - -[Betas,~,stats] = mnrfit(DR_data(trainingidx,:),behavs(trainingidx,1)); %note always get B is dim predictors+1 for the intercept term x length(boi)-1 as one behavior is selected as reference - -[pihat, ~,~] = mnrval(Betas,DR_data(ind_if{k},:),stats); %give probabilities for each behavior - -%take max of each predicted probability as the predicted behavioral state -%like above - - -[~,cs]= max(pihat,[],2); %Find which centriod the activity was closer to - -preds = boi(cs); %Predict behavior that was that centriod - -cv_preds{k} = preds; - -%preds = categorical(preds,boi,{Label_struct.behav_categ{boi}}); %use same categorical trick above so can do string compare - - - -% figure() %Have to wait for end to concatenate everything and unscramble -% plot(LD_tog(s_inds,1)) -% hold on -% plot(preds(s_inds)) -% title('Multinomial Regression Predictions') - - - -per_cor_mnr = sum(LD_tog(ind_if{k},1)==preds')/length(preds)*100; %strcmp(behavs(ind_if{k}),preds) - -cv_per_cor(k) = per_cor_mnr; - - - -end - -%% Plot CV results - -%% Run and plot without cv just for convenience for now - -behavs = categorical(LD_tog(:,1),boi,{Label_struct.behav_categ{boi}}); %create categorical array for use with mnrfit - -[Betas,dev,stats] = mnrfit(DR_data,behavs); %note always get B is dim predictors+1 for the intercept term x length(boi)-1 as one behavior is selected as reference - -[pihat, dlow,dhi] = mnrval(Betas,DR_data,stats); %give probabilities for each behavior - -%take max of each predicted probability as the predicted behavioral state -%like above - -[~,cs]= max(pihat,[],2); - -preds = boi(cs); %Predict behavior that was that centriod - - - -per_cor_nocv = sum(LD_tog(:,1)==preds')/length(preds)*100 - -figure() %Have to wait for end to concatenate everything and unscramble -plot(LD_tog(:,1)) -hold on -plot(preds) -title('Multinomial Regression Predictions') - -%% posterity code - diff --git a/Varia/Population trajectory/trajectory_allUnits.mat b/Varia/Population trajectory/trajectory_allUnits.mat deleted file mode 100644 index 5e9bb6a..0000000 Binary files a/Varia/Population trajectory/trajectory_allUnits.mat and /dev/null differ diff --git a/Varia/Population trajectory/trajectory_allUnits_info.mat b/Varia/Population trajectory/trajectory_allUnits_info.mat deleted file mode 100644 index 5add4d7..0000000 Binary files a/Varia/Population trajectory/trajectory_allUnits_info.mat and /dev/null differ diff --git a/Varia/SanityChecksToyExamples/GeometryXi_et_al_tests.m b/Varia/SanityChecksToyExamples/GeometryXi_et_al_tests.m deleted file mode 100644 index 1aaa1c1..0000000 --- a/Varia/SanityChecksToyExamples/GeometryXi_et_al_tests.m +++ /dev/null @@ -1,9 +0,0 @@ -%% Running Notes -% Script for exploring and developing analyses based on Xie et al 2022 in -% Science "Geometry of sequence working memory in macaque prefrontal cortex" -%https://pubmed.ncbi.nlm.nih.gov/35143322/ - - -%% Simulate Neurons - -%% \ No newline at end of file diff --git a/Varia/SanityChecksToyExamples/PoissionNeuronMLE.m b/Varia/SanityChecksToyExamples/PoissionNeuronMLE.m deleted file mode 100644 index 059f779..0000000 --- a/Varia/SanityChecksToyExamples/PoissionNeuronMLE.m +++ /dev/null @@ -1,152 +0,0 @@ -%% Example code for how to fit MLE models to poission neuron data - -%Running Notes: Will probably put this into SimNeuronChecks later, but -%separate for now for development. Also this is just slapped together, -%will come back for more interesting simulations later on. - -%% Set duration and other important parameters -%2022-03-02 NOTE: don't use all this yet - %Timing parameters - - t_res = .001; % Start with milisecond resolution - - T = 300; %Start with just 5 minutes of recording - - t = 0:t_res:T; - - num_samples = length(t); - - - %Behavior simulation parameters - - n_behaviors = 3; - - boi = 1:n_behaviors; - - unidis = 1; %For now simulate as if each behavior occurs with equal frequency to avoid having to subsample - - %Neuronal simulation parameters - - n_neurons = 5; - - as_process = 1; %Toggle whether to just simulate poisson neurons with a single lambda - %Or to stimulate a poission process where lambdas are drawn for each frame - %(think GLM). Process will take longer to simulate I think - - fullrand = 0; %Toggle whether have neurons that are tuned to different catagories or not. - - %Binning parameters - - %100ms, 500ms, 1s, 2s, 5s - - binning = [.1 .5 1 2 5]*1/t_res; %convert time to indices - - %% A quite crap simulations - %This doesn't work to illustrate the point I want yet... - %Need to change how tuning is implemented. - - %Very basic situation, each behavior last 1/3 of session - behav_vec = [ones(T/n_behaviors,1); ones(T/n_behaviors,1)*2; ones(T/n_behaviors,1)*3]; - - spikes = nan(length(behav_vec),n_neurons); - - chunk = T/n_behaviors; %just for convenience for simulations - - gt_tuning = zeros(n_behaviors,n_neurons); %ground truth tuning - - fr_base = 10; - tuning_bump = 5; %spike count bump to lambda (additive) - - for n = 1:n_neurons - - gt_tuning(randi(3),n) = 1; %Randomly tune neuron to one behavior - - for b = 1:n_behaviors - - lambda_temp = fr_base + tuning_bump*gt_tuning(b,n); - - gt_tuning(b,n) = lambda_temp; %save lambda value instead of 1 or 0 - - %just being lazy otherwise this would be indexed by when - %behavior occured - spikes(1+chunk*(b-1):chunk*b,n) = poissrnd(gt_tuning(b,n),chunk,1); - - end - - - end - - expected_baseline = mean(gt_tuning); %expect baseline below to be average of underlying lambdas - %lambda_hat_behavs below should be close to gt_tunning - - %% Actual method stuff - %Fit Poisson - %hat because we are getting it from the fit - - lambda_hat_baseline = nan(1,n_neurons); %Just doing whole session as baseline - lambda_hb_ci = nan(2,n_neurons); %confidence interval for above - lambda_hat_behav = nan(n_behaviors,n_neurons); %One for each behavior for each neuron - lambda_hbeh_ci = nan(2,n_behaviors,n_neurons); %confidence interval for above - for n = 1:n_neurons - - [lambda_hat_baseline(1,n), lambda_hb_ci(:,n)] = poissfit(spikes(:,n)); - - for b = 1:n_behaviors - [lambda_hat_behav(b,n), lambda_hbeh_ci(:,b,n)] = poissfit(spikes(1+chunk*(b-1):chunk*b,n)); - end - - end - - - %Manual wald test (pulled from my previous work) - - %Review logic behind (i.e. my manuscript) this but go from 95% ci to se using below - lambda_hb_se = diff(lambda_hb_ci)/2/1.96; - lambda_hbeh_se = diff(lambda_hbeh_ci)/2/1.96; %Get width of confidence interval, scale back to se - - %Wiki https://en.wikipedia.org/wiki/Wald_test for formula until I explain - %via manuscript. sqW is wald statistic on asym z - - sqW = nan(size(lambda_hat_behav)); - - %This almost certainly doesn't need to be for loops but I am a bit tired - %to make something cute with the matrix math - - for n = 1:n_neurons - for b = 1:n_behaviors - - sqW(b,n) = sqrt((lambda_hat_behav(b,n) - lambda_hat_baseline(1,n)).^2)./sqrt(lambda_hbeh_se(1,b,n).^2 + lambda_hb_se(1,n).^2); - - end - - end - - %Wald stat follows Z distribtution - need to check again the validity of - %abs...remember that is in the text somewhere...need to think on more but - %something something two tailed versus one tailed...removing for now to - %only consider excitatory and not surpressive effects. There has to be - %an obvious way to deal with this that I am not thinking of. - - p_values = 1 - normcdf(sqW,0,1); - - cutoff = 0.001; %set as desired - - - gt_tuning - lambda_hat_behav - - expected_baseline - lambda_hat_baseline - - - h = p_values < cutoff %return behaviors that are signficantly different from baseline - - figure - imagesc(abs(sqW)) - %set(gca,'YDir','normal') - xticks([1:n_neurons]) - xticklabels({'n1','n2','n3','n4','n5'}) - yticks([1:n_behaviors]) - yticklabels({'b1', 'b2', 'b3'}) - title('tuning per behavior') - \ No newline at end of file diff --git a/Varia/SanityChecksToyExamples/SimNeuronChecks.m b/Varia/SanityChecksToyExamples/SimNeuronChecks.m deleted file mode 100644 index 2ac8f25..0000000 --- a/Varia/SanityChecksToyExamples/SimNeuronChecks.m +++ /dev/null @@ -1,450 +0,0 @@ -%% Script containing basic checks for datalogger project -%making sure things work as anticipated. For now just simulate poisson -%neurons. Later on we can try to spruce things up as we have more -%information. - -%Note: for now skipping doing this at ms resolution as we don't do that in -%the og analyses and it takes forever. - -%% Set parameters to sweep and vars to collect results - -sweep = 0:.005:.1; %For now sweeping tunning of neurons, -%only see the effect at very low tunning levels so focusing on those in the -%sweep - -Sweep_Results.LinSep = cell(length(sweep),1); - -Sweep_Results.MNR = cell(length(sweep),1); - -for run = 1:length(sweep) - - %% Set duration and other important parameters - - %Timing parameters - - t_res = .001; % Start with milisecond resolution - - T = 300; %Start with just 5 minutes of recording - - t = 0:t_res:T; - - num_samples = length(t); - - - %Behavior simulation parameters - - n_behaviors = 3; - - boi = 1:n_behaviors; - - unidis = 1; %For now simulate as if each behavior occurs with equal frequency to avoid having to subsample - - %Neuronal simulation parameters - - n_neurons = 100; - - as_process = 1; %Toggle whether to just simulate poisson neurons with a single lambda - %Or to stimulate a poission process where lambdas are drawn for each frame - %(think GLM). Process will take longer to simulate I think - - fullrand = 0; %Toggle whether have neurons that are tuned to different catagories or not. - - %Binning parameters - - %100ms, 500ms, 1s, 2s, 5s - - binning = [.1 .5 1 2 5]*1/t_res; %convert time to indices - - - - %% "Simulate" behaviors - %For convenience just setting up categorical data on the same time scale as - %neural activity - - %Could also try doing this as an events matrix...thinking something like - %block mat of ones with zeros for other behaviors...probably going to need - %to do this one we do an uneven distribution of behaviors. - - - - if unidis - - holding = ones(num_samples + n_behaviors - mod(num_samples,n_behaviors),1); %Using reshape trick for things not dividing evenly - - holding = reshape(holding,length(holding)/n_behaviors,n_behaviors).*[1:n_behaviors]; - - holding = reshape(holding,length(holding)*n_behaviors,1); - - behaviors =holding(1:num_samples); %just trim off some behaviors for the last one to get down to correct size - - else %write code for changing distribution of behaviors - - end - - - - %% Simulate Bernoulli Neurons - - %Update 2022-01-27 have fully random neurons - %Need to build in changes in lambda associated with different behavioral - %categories - %Update 2022-02-01 need to make these binomial instead of poisson due to 1 - %ms resolution - - - - - spikes = nan(num_samples, n_neurons); %Note going again with convention of linear algebra of obs x var instead of var x obs - - - - if fullrand - - if as_process - - for neuron =1:n_neurons - - neuron - - ps = rand(num_samples,1); % pick random lambda between 1 and 10 for lambda for each sample - - spikes(:, neuron) = binornd(1,ps); %will generate one draw for each sample with that lambda - - end - - - % else - % - % ps = randi([1 10], n_neurons,1); %Get a random expected spike count for each neuron - % - % %don't think poisrnd takes vector inputs of lambda so just run a for - % %loop even if a bit slow - % - % - % - % for neuron = 1:n_neurons - % - % neuron - % - % spikes(:,neuron) = poissrnd(ps(neuron),num_samples); - % - % - % - % end - - - end - - - else %still need to code what to do if by behavioral catagory. - - if as_process - - tunning = randi(max(boi),n_neurons,1); %randomly assign a prefered behavior to each neuron. - - scale_tune = sweep(run); %trying additive for now - - for neuron = 1:n_neurons - - neuron - - ps = rand(num_samples,1); - - tunned_inds = behaviors == tunning(neuron); - - ps = ps + tunned_inds*scale_tune; - - ps(ps>1) = 1; %make sure anything greater than 1 is set just to 1 - - spikes(:, neuron) = binornd(1,ps); - - - - end - - else - - %To match with emperical data, consider setting a single p for - %each neuron and then adding tunning. I.e one p per neuron and - %then add tunning. See below point. - - %Also try varying level of tunning within a sweep. This may be - %implemented later - - end - - - - - - - - end - - - - - - - - %% Create vars for each of the time bins. - - - - sc_bins = cell(length(binning)+1,1); %cell array to store different resolution spiking - - bh_bins = sc_bins; - - bh_bins {1} = behaviors; - - sc_bins{1} = spikes; - - for bin = 1:length(binning) - - holding = nan(num_samples + binning(bin) - mod(num_samples,binning(bin)),1); - - sc_bins{bin+1} = nan(length(holding)/binning(bin),n_neurons); - - for neuron=1:n_neurons - - holding = nan(num_samples + binning(bin) - mod(num_samples,binning(bin)),1); - %create holding vector that will have nans for incomplete samples at - %given binning. Can reshape this vector and sum across to get summed - %spike count at each binning - - holding(1:num_samples) = spikes(:,neuron); - - holding = reshape(holding, binning(bin),length(holding)/binning(bin)); - - sc_bins{bin+1}(:,neuron) = sum(holding,1,'OmitNaN'); - - end - - %trying same trick with behaviors using unique function for duplicates - - holding = nan(num_samples + binning(bin) - mod(num_samples,binning(bin)),1); - - bh_bins{bin+1} = nan(length(holding)/binning(bin),1); - - holding(1:num_samples) = behaviors; - - holding = reshape(holding, binning(bin),length(holding)/binning(bin)); - - %I'm sure there is a more efficient way to do this but for now just - %need something that works. - - temp = cell(size(holding,2),1); - - for col=1:size(holding,2) - - temp{col} = unique(holding(:,col)); - - temp{col}(isnan(temp{col})) = []; %remove nans - - %Decide another time on how want to handle duplicates for now just - %going to give it to greatest element - - temp{col} = temp{col}(end); - - - end - - bh_bins{bin+1} = vertcat(temp{:}); - - - end - - %Set up check for tunning if desired. - - if ~fullrand - - sc_per_beh = nan(n_behaviors,n_neurons); - - ind_beh = zeros(num_samples,n_behaviors); - - % there's probably something more elegant but this works for now - - for b =1:n_behaviors - - ind_beh(:,b) = behaviors == boi(b); - - - sc_per_beh(b,:) = sum(spikes(behaviors == boi(b),:),1); - - - end - - %%setup code to plot subset of neurons later. This need to be made - %%pretty but for now works to make sure the code is functional - - % figure - % plot(sc_per_beh) - % xlabel('Behavior') - % xticks([1,2,3]) - % ylabel('Spikes fired') - % xlim([0 max(boi)+1]) - % - - - end - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - %% Checks for time window - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - %% 1) Linear Separability - - Results.SVMAcc = nan(length(sc_bins),1); %Make a structure that holds all of the results and parameters at the end - - - svm_params = templateSVM('Standardize',true, 'KernelFunction','linear'); %Don't think you need to set kernel to linear but doing it for clarity - - for bin = 2:length(sc_bins) - - if bin ==1 - - disp(['Running svm for original resolution (1ms)']) - - else - - disp(['Running svm for ' num2str(binning(bin-1)*t_res) ' second bin size']) - - end - - if unidis - - mdl = fitcecoc(sc_bins{bin},bh_bins{bin}, 'Learners', svm_params); - - CVmdl = crossval(mdl,'Kfold',5); %To match MNR implementation - - Results.SVMAcc(bin) = 1-kfoldLoss(CVmdl); - - else %set up code for subsampling if non-uniform distribution of behaviors based on Camille's sub-sampling procedure - - end - - end - - - - %% 2) Multinomial Regression - - %Update 2022-02-01: note not every cv fold is converging for this - %analysis. For now this is fine as this is a check result rather than - %something we are analyzing deeply. - - %Further note this is running into issue as we increase n_neurons so - %may have to change this to doing PCA on the data first and then - %running the MNR. Alternatively or in addition could put this in a try - %catch block... - - %For now just going to set up a try catch block and take it from there. - - Results.MNRAcc = nan(length(sc_bins),1); %Make a structure that holds all of the results and parameters at the end - - - - for bin = 2:length(sc_bins) - - if bin ==1 - - disp(['Running multinomial regression for original resolution (1ms)']) - - else - - disp(['Running multinomial regression for ' num2str(binning(bin-1)*t_res) ' second bin size']) - end - if unidis - - folds = 5; - - sample_size = floor(size(sc_bins{bin},1)/folds); - - sam_if = ones(folds,1)*sample_size; %samples in each fold - - - if mod(size(sc_bins{bin},1),folds)>0 %check if not divisible by folds - disp('Samples do not divide evenly accross folds') - %if it does not divide evenly, add remainder randomly to one of the - %folds - ind = randsample(folds,1); - sam_if(ind) = sam_if(ind) + mod(size(sc_bins{bin},1),folds); - - end - - shuffledind = randperm(length(bh_bins{bin})); - ind_if = cell(folds,1); - ind_if{1} = shuffledind(1:sam_if(1)); %set the first group out of the loop - groups = cumsum(sam_if); - - if sum(sam_if)~=size(sc_bins{bin}(:,1),1) %Need to make it size because sometimes have less bins than neurons - error('Missing inds from shuffle') - end - - for i=2:folds - ind_if{i} = shuffledind(groups(i-1)+1:groups(i)); - end - - per_cor_mnr = nan(folds,1); - for k = 1:folds - - disp(['iteration: ' num2str(k)]) - states = bh_bins{bin}(ind_if{k},1); - - foldsidx = 1:folds; - trainingidx = horzcat(ind_if{foldsidx~=k})'; %train on all indices that aren't in the current fold - try - [Betas,~,stats] = mnrfit(sc_bins{bin}(trainingidx,:),bh_bins{bin}(trainingidx,1)); %note always get B is dim predictors+1 for the intercept term x length(boi)-1 as one behavior is selected as reference - - [pihat, ~,~] = mnrval(Betas,sc_bins{bin}(ind_if{k},:),stats); %give probabilities for each behavior - - %take max of each predicted probability as the predicted behavioral state - %like above - - [~,cs]= max(pihat,[],2); % - - preds = boi(cs); %Predict behavior that was that centriod - - %preds = categorical(preds,boi,{Label_struct.behav_categ{boi}}); %use same categorical trick above so can do string compare - per_cor_mnr(k) = sum(bh_bins{bin}(ind_if{k},1)==preds')/length(preds)*100; %strcmp(behavs(ind_if{k}),preds) - - catch - - per_cor_mnr(k) = nan; - - end - - - end - - Results.MNRAcc(bin) = mean(per_cor_mnr); %for now if any fold fails (i.e. is set to nan), set whole bin to nan - - - else %set up code for subsampling if non-uniform distribution of behaviors based on Camille's sub-sampling procedure - - end - - end - - - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - %% Checks for dimensionality - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - %Already did this for Gaussians but haven't tried for poisson neurons - - %% collect sweeped parameter results - -Sweep_Results.LinSep{run} = Results.SVMAcc*100; %Just to put on same scale as MNR -Sweep_Results.MNR{run} = Results.MNRAcc; - -end - -%% Save - -%best if cd is the SanityCheckToyExamples folder - -save([cd '\' num2str(n_behaviors) 'B_' num2str(n_neurons) 'N_TuneSweep.mat'],'Sweep_Results', 'sweep', 'n_neurons', 'n_behaviors','binning','sc_bins','bh_bins') - -%% Generate Analgous plots to what Camille has - diff --git a/Varia/SanityChecksToyExamples/TuneSweep/3B_100N_TuneSweep.mat b/Varia/SanityChecksToyExamples/TuneSweep/3B_100N_TuneSweep.mat deleted file mode 100644 index 8b2c8e0..0000000 Binary files a/Varia/SanityChecksToyExamples/TuneSweep/3B_100N_TuneSweep.mat and /dev/null differ diff --git a/Varia/SanityChecksToyExamples/TuneSweep/3B_10N_TuneSweep.mat b/Varia/SanityChecksToyExamples/TuneSweep/3B_10N_TuneSweep.mat deleted file mode 100644 index c27135c..0000000 Binary files a/Varia/SanityChecksToyExamples/TuneSweep/3B_10N_TuneSweep.mat and /dev/null differ diff --git a/Varia/SanityChecksToyExamples/TuneSweep/3B_20N_TuneSweep.mat b/Varia/SanityChecksToyExamples/TuneSweep/3B_20N_TuneSweep.mat deleted file mode 100644 index c409479..0000000 Binary files a/Varia/SanityChecksToyExamples/TuneSweep/3B_20N_TuneSweep.mat and /dev/null differ diff --git a/Varia/SanityChecksToyExamples/TuneSweep/3B_30N_TuneSweep.mat b/Varia/SanityChecksToyExamples/TuneSweep/3B_30N_TuneSweep.mat deleted file mode 100644 index b6c51b3..0000000 Binary files a/Varia/SanityChecksToyExamples/TuneSweep/3B_30N_TuneSweep.mat and /dev/null differ diff --git a/Varia/SanityChecksToyExamples/TuneSweep/3B_40N_TuneSweep.mat b/Varia/SanityChecksToyExamples/TuneSweep/3B_40N_TuneSweep.mat deleted file mode 100644 index 420d065..0000000 Binary files a/Varia/SanityChecksToyExamples/TuneSweep/3B_40N_TuneSweep.mat and /dev/null differ diff --git a/Varia/SanityChecksToyExamples/TuneSweep/3B_50N_TuneSweep.mat b/Varia/SanityChecksToyExamples/TuneSweep/3B_50N_TuneSweep.mat deleted file mode 100644 index e20c4b3..0000000 Binary files a/Varia/SanityChecksToyExamples/TuneSweep/3B_50N_TuneSweep.mat and /dev/null differ diff --git a/Varia/SanityChecksToyExamples/TuneSweep/3B_60N_TuneSweep.mat b/Varia/SanityChecksToyExamples/TuneSweep/3B_60N_TuneSweep.mat deleted file mode 100644 index e7d35b5..0000000 Binary files a/Varia/SanityChecksToyExamples/TuneSweep/3B_60N_TuneSweep.mat and /dev/null differ diff --git a/Varia/SanityChecksToyExamples/hiD_sep.m b/Varia/SanityChecksToyExamples/hiD_sep.m deleted file mode 100644 index 45591a9..0000000 --- a/Varia/SanityChecksToyExamples/hiD_sep.m +++ /dev/null @@ -1,81 +0,0 @@ -%% Running Notes -%Developing set of tests for checking intuition about higher dimensional -%spaces and for developing potential null model checks for datalogger -%project as well as other high neuronal data, population level analyses. - - -%% Linear Separability test -%Curious if nearest neighbor failure (i.e. ratio between nearest and -%furthest converges to 1 as dim -> inf) has consequences for linear -%separability. - -%Fairly satisified with the fact that it doesn't unless there is some -%difference in means for random gaussian data - -dims = [20,50,100,500,1000]; - -cv_perform = zeros(length(dims),1); - -num_cat = 2; %note: as expected takes much longer if you increase categories and make the problem harder. - -dat_cat = cell(length(dims),num_cat);%just going to start with two categories - -labels_cat = cell(size(dat_cat)); - -dat_tog = cell(length(dims),1); - -samples = 25000; %may also want explore this as a function of sample size - -means = [0 0.001 .4]; %note have to manually set - -mdls = cell(1,length(dims)); - -%Essentially creating high-d spheres by making each category random pulls -%from a gaussian of dimensionality dims with identity correlation matrix. -%Keep variance tight, but move average to see separability - -for i = 1:length(dims) - - count = 0; - - for cat = 1:num_cat - - dat_cat{i,cat} = normrnd(means(cat),1, samples, dims(i)); %keep observation x source structure matlab prefers - - labels_cat{i,cat} = count*ones(size(dat_cat{i,cat},1),1); %create vectors of labels - - count = count+1; - - end - - dat_tog{i} = [vertcat(labels_cat{i,:}) vertcat(dat_cat{i,:})]; %Put everything together with labels as leading column - - %default learner for these models is an SVM so don't have to set it in - %the code. - - if num_cat<3 - - mdls{i} = fitclinear(dat_tog{i}(:,2:end),dat_tog{i}(:,1),'KFold',5); - - else - - mdls{i} = fitcecoc(dat_tog{i}(:,2:end),dat_tog{i}(:,1),'KFold',5); - - end - - cv_perform(i) = 100 - kfoldLoss(mdls{i})*100 %Loss as percent misclassified, so subtract to get percent correct - - -end - -figure() -plot(dims,cv_perform,'.k') -title(['Difference in means: ' num2str(means(1:num_cat))]) -ylabel('Classification performance') -xlabel('Dimensionality of data') -ylim([(100/num_cat)-5 , 105]) -xlim([min(dims)-5 max(dims)+5]) - - - - diff --git a/Varia/SanityChecksToyExamples/loadandplotting.m b/Varia/SanityChecksToyExamples/loadandplotting.m deleted file mode 100644 index 3bd9795..0000000 --- a/Varia/SanityChecksToyExamples/loadandplotting.m +++ /dev/null @@ -1,36 +0,0 @@ -%% Script to load in and plot results from various sanity checks - -%% Tunesweep for varied number of neurons - -%Will set this up for loading in all of the number of neurons sessions -%later For now setting up plotting and relying on manual loading - -labels = arrayfun(@num2str,binning*.001,'UniformOutput',false) - -labels{length(labels)+1} = 'Chance'; - -%LinSep results - -LinSep = horzcat(Sweep_Results.LinSep{:}); -LinSep(1,:) = []; %Delete the first column as just full of nans - -figure %make this better later, but fine to illustrate the points for now -plot(sweep,LinSep','.', 'MarkerSize',10) -hold on -plot(sweep,33*ones(length(sweep),1),'k--') -title(['Simulated Linear Decoder Performance for b = ' num2str(n_behaviors) ' and N =' num2str(n_neurons)]) -xlabel('Increase in probability of firing (tunning)') -ylabel('% Correct') -legend(labels) - -%Multinomial regression results -MNR = horzcat(Sweep_Results.MNR{:}); -MNR(1,:) = []; %Delete the first column as just full of nans -figure %make this better later, but fine to illustrate the points for now -plot(sweep,MNR','.', 'MarkerSize',10) -hold on -plot(sweep,33*ones(length(sweep),1),'k--') -title(['Simulated MNR Performance for b = ' num2str(n_behaviors) ' and N =' num2str(n_neurons)]) -xlabel('Increase in probability of firing (tunning)') -ylabel('% Correct') -legend(labels) diff --git a/Varia/Toy_examples/Loading_example_session_data.m b/Varia/Toy_examples/Loading_example_session_data.m deleted file mode 100644 index 7bcfb43..0000000 --- a/Varia/Toy_examples/Loading_example_session_data.m +++ /dev/null @@ -1,34 +0,0 @@ -%% Loading data from example session -% C. Testard August 2022 - -%Set path -is_mac = 1; -if is_mac - home = '~'; -else - home ='C:/Users/GENERAL'; -end -cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) -sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); s=1; - -%Set parameters -with_partner =1; %Include partner behavior data 1:yes; 0:no -temp = 1; temp_resolution = 1; -channel_flag = "all"; %"all", "vlPFC" or "TEO" -randomsample=0; %subsample neurons to match between brain areas -unq_behav=0; %If only consider epochs where only 1 behavior happens -with_NC =1;%0: NC is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0;%Only consider isolated units. 0=all units; 1=only well isolated units - - -%Set path -filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) -savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SVM_results/']; - - -%% Get data with specified temporal resolution and channels -%Note: Thescription of inpout and output arguments detailed in te -%function's documentation. -[Spike_rasters, SpikeData, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all, brain_label]= log_GenerateDataToRes_Example_function(filePath, temp_resolution, channel_flag, is_mac, with_NC, isolatedOnly); -disp('Data Loaded') - diff --git a/Varia/Toy_examples/Ron_DoSmth.asv b/Varia/Toy_examples/Ron_DoSmth.asv deleted file mode 100644 index 2375ca3..0000000 --- a/Varia/Toy_examples/Ron_DoSmth.asv +++ /dev/null @@ -1,293 +0,0 @@ -%% Ron_DoSmth -% Have fun! - - - -%Set parameters -is_mac = 0; %For loading the data -is_ron = 1; %For setting path -with_partner =1; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "T"; %Channels considered (sets brain region TEO vlPFC all -with_NC =1; %0: Noise cluster (NC) is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well-isolated units - -for s = 3 %For now focus on session Amos_2021-07-29 - - %Set path - if is_ron - - sessions = dir('C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_data'); sessions = sessions(3:end,:); - filePath = ['C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_data\' sessions(s).name]; - savePath = ['C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_results']; - - else - - - %Set session list - home = '~'; % set home directory - cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) - sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/partner_vs_subject']; - end - %% Load data - - %Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_Ron(filePath, temp_resolution, channel_flag, is_mac,is_ron, with_NC, isolatedOnly); - % Output: - % 1. Spike_rasters: n_neuron X time in seconds (if temp_resolution = 1) - % 2. labels: time X 11 - check the function description for - % details. Essentially subject behavior is column 3. - % 3. labels_partner: time X 11. Partner behavior is column 3. - % 4. behav_categ: string vector; behavioral category do the numbers in - % labels correspond to. - % 5. block_times: specifies the block times and ID - % 6. monkey: subjecet monkey ID - % 7. reciprocal_set: set of behaviors that are deeemed "reciprocal" - % 8. social_set: set of behaviors that are deeemed "social" - % 9. ME_final: Motion energy values - % 10. unit_count: number of units in the session - % 11. groom_labels_all: labels for grooming. Check function - % description for details - - session_length = size(Spike_rasters,2); % get session length - Spike_count_raster = Spike_rasters'; - - %Extract behavior labels for subject and partner - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - - % Set proximity as rest - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - -end - -%% Subdivide data for successive dimensionality tests -%Going to just run successive tests based on what was outlined in the pitch -%Can come back to clean this up to run more efficiently but for now just -%want to confirm for this session that general idea is right. - -%Going to start with using PCA for speed and clarity (i.e. everyone gets -%PCA) - -%Before doing anything else just get the inds for all the groups we want. - -groominds_give = groom_labels_all(:,1) == 7; -groominds_receive = groom_labels_all(:,1) == 8; - -groominds_all = groominds_give + groominds_receive; %Since these are mutually exclusive and (as far as I can tell) define all groom times. -groominds_all = groominds_all>0; %To change it back to logical indexing -groominds_postthreat = groom_labels_all(:,3) == 1; -groominds_reciprocal = groom_labels_all(:, 4) == 1; -groominds_initiated = groom_labels_all(:,5) == 1; - -groominds_self = behavior_labels_subject_init == 24; %24 is label for self groom - -%Use Spike_count_raster as it is in obs x vars format these algorithms are -%expecting - -%% Test 0.1: Basic checks - Dimensionality for grooming, the rest, and all -%So far this does not match what I was predicting. The deviation isn't -%massive, but it is still surprising. Doesn't entirely sink this train of -%thought, so will contibue with other test, but need to reflect on what -%this means. - -threshold = 50; - -[eigenvec, score, eigenval, ~, var_exp] = pca(Spike_count_raster); - -Results.all.eigenvec = eigenvec; -Results.all.score = score; -Results.all.eigenval = eigenval; -Results.all.var_exp = var_exp; - - -Data_grooming = Spike_count_raster(groominds_all,:); -Data_self = Spike_count_raster(groominds_self,:); -Data_groomgive = Spike_count_raster(groominds_give,:); -Data_groomrec = Spike_count_raster(groominds_receive,:); - - -[eigenvec, score, eigenval, ~, var_exp] = pca(Data_grooming); - -Results.groomall.eigenvec = eigenvec; -Results.groomall.score = score; -Results.groomall.eigenval = eigenval; -Results.groomall.var_exp = var_exp; - -[eigenvec, score, eigenval, ~, var_exp] = pca(Data_groomgive); - -Results.groomgive.eigenvec = eigenvec; -Results.groomgive.score = score; -Results.groomgive.eigenval = eigenval; -Results.groomgive.var_exp = var_exp; - -[eigenvec, score, eigenval, ~, var_exp] = pca(Data_groomrec); - -Results.groomrec.eigenvec = eigenvec; -Results.groomrec.score = score; -Results.groomrec.eigenval = eigenval; -Results.groomrec.var_exp = var_exp; - -[eigenvec, score, eigenval, ~, var_exp] = pca(Data_self); - -Results.therest.eigenvec = eigenvec; -Results.therest.score = score; -Results.therest.eigenval = eigenval; -Results.therest.var_exp = var_exp; - -%Get number of eigen values needed to cross threshold of variance explained - -Results.all.majdim = find(cumsum(Results.all.var_exp)>threshold,1,'first'); -Results.therest.majdim = find(cumsum(Results.therest.var_exp)>threshold,1,'first'); -Results.groomall.majdim = find(cumsum(Results.groomall.var_exp)>threshold,1,'first'); -Results.groomgive.majdim = find(cumsum(Results.groomgive.var_exp)>threshold,1,'first'); -Results.groomrec.majdim = find(cumsum(Results.groomrec.var_exp)>threshold,1,'first'); - -%sorted_axis = 1:length(var_exp);%changed mind don't like this./length(var_exp); %quick trick for setting axes for eigen plots -figure -plot(1:length(Results.all.var_exp),cumsum(Results.all.var_exp),'k') -title([channel_flag ' ' num2str(s)]) -ylabel('var explained') -xlabel('Eigenvectors used') -ylim([0,110]) -hold on -plot(1:length(Results.groomall.var_exp),cumsum(Results.groomall.var_exp),'b') -plot(1:length(Results.groomgive.var_exp),cumsum(Results.groomgive.var_exp),'b--') -plot(1:length(Results.groomrec.var_exp),cumsum(Results.groomrec.var_exp),'b.-') -plot(1:length(Results.therest.var_exp),cumsum(Results.therest.var_exp),'r') -plot(1:length(var_exp), threshold * ones(size(var_exp)),'k--') %put dashed black line for majority of variance -%number beside is number of PCs needed to cross threshold var explained -legend(['All: ' num2str(Results.all.majdim)],... - ['Grooming: ' num2str(Results.groomall.majdim)],... - ['Groom Give: ' num2str(Results.groomgive.majdim)],... - ['Groom Receive:' num2str(Results.groomrec.majdim)],... - ['Groom Self: ' num2str(Results.therest.majdim)],... - 'Threshold') - - - -%% Test 1.1 Compare dimensionality: post threat, initiated, reciprocal - -%Dimensionality is effectively the same across these conditions as well as -%compared to considering all grooming. Kind of interesting to see; need to -%think more on what it means - -Data_groompt = Spike_count_raster(groominds_postthreat,:); -Data_groomrecip = Spike_count_raster(groominds_reciprocal,:); -Data_groomini = Spike_count_raster(groominds_initiated,:); - -[eigenvec, score, eigenval, ~, var_exp] = pca(Data_groompt); - -Results.groompt.eigenvec = eigenvec; -Results.groompt.score = score; -Results.groompt.eigenval = eigenval; -Results.groompt.var_exp = var_exp; - -[eigenvec, score, eigenval, ~, var_exp] = pca(Data_groomrecip); - -Results.groomrecip.eigenvec = eigenvec; -Results.groomrecip.score = score; -Results.groomrecip.eigenval = eigenval; -Results.groomrecip.var_exp = var_exp; - -[eigenvec, score, eigenval, ~, var_exp] = pca(Data_groomini); - -Results.groomini.eigenvec = eigenvec; -Results.groomini.score = score; -Results.groomini.eigenval = eigenval; -Results.groomini.var_exp = var_exp; - - -Results.groompt.majdim = find(cumsum(Results.groompt.var_exp)>threshold,1,'first'); -Results.groomrecip.majdim = find(cumsum(Results.groomrecip.var_exp)>threshold,1,'first'); -Results.groomini.majdim = find(cumsum(Results.groomini.var_exp)>threshold,1,'first'); - - -figure -plot(1:length(Results.groomall.var_exp),cumsum(Results.groomall.var_exp),'b') -title([channel_flag ' ' num2str(s)]) -ylabel('var explained') -xlabel('Eigenvectors used') -hold on -plot(1:length(Results.groompt.var_exp),cumsum(Results.groompt.var_exp),'bx') -plot(1:length(Results.groomrecip.var_exp),cumsum(Results.groomrecip.var_exp),'b--') -plot(1:length(Results.groomini.var_exp),cumsum(Results.groomini.var_exp),'b.-') - -plot(sorted_axis, threshold * ones(size(sorted_axis)),'k--') %put dashed black line for majority of variance -%number beside is number of PCs needed to cross threshold var explained -legend(['Grooming: ' num2str(Results.groomall.majdim)],... - ['Groom Post Threat: ' num2str(Results.groompt.majdim)],... - ['Groom Reciprocal: ' num2str(Results.groomrecip.majdim)],... - ['Groom Initated: ' num2str(Results.groomini.majdim)],... - 'Threshold') - -%% Test 1.2 Grooming lies in different subspaces based on context: post threat, initiated, reciprocal -%First do simple test with average angle between PCs -%Next as there was more to that paper than I thought and my first tries are -%not exactly working as expected; implement the analyses from the below -%paper to see what that gets us and if some of their processing steps are -%necessary to see this structure. - -%Not eigenvectors come out of PCA in matlab already normalized to have unit -%vector norm (i.e. norm(eigenvec(:,1)) = 1 - -%Just use maximum number need to cross threshold of variance explained from above. -num_eigenvec = max([Results.groompt.majdim,Results.groomrecip.majdim, Results.groomini.majdim]); - - -collected_eigens = cell(3,1); - -collected_eigens{1} = Results.groompt.eigenvec(:,1:num_eigenvec); -collected_eigens{2} = Results.groomrecip.eigenvec(:,1:num_eigenvec); -collected_eigens{3} = Results.groomini.eigenvec(:,1:num_eigenvec); - -angles_between = cell(3,1); %n choose k with 3 is 3!/2! (3-2)! = 3 -avg_angle = nan(3,1); - -comparison = [1 2; 1 3; 2 3]; %better ways to do this but for now just manually put in all of the comparisons we want to do - -% for comp = 1:length(angles_between) -% -% angles_between{comp} = acos(dot(collected_eigens{comparison(comp,1)},collected_eigens{comparison(comp,2)})); -% -% avg_angle(comp) = rad2deg(mean(angles_between{comp})); -% -% -% end -% - some_labels = {'post-threat to reciprocal','post-threat to initiated','reciprocal to initated'}'; -% -% table(avg_angle,some_labels,'VariableNames',{'avg angle', 'comparison'}) - -%Now trying the principal angles definition that is used in the below paper -%and seems to be more standard in the field given some reading. Take the -%SVD of the product of the two subspaces - -angles_betweensvd = cell(3,1); -principal_angle = nan(size(angles_betweensvd)); - -for comp = 1:length(angles_between) - - [~,S,~]= svd(collected_eigens{comparison(comp,1)}' * collected_eigens{comparison(comp,2)}); %want to do SVD on PC x PC matrix, hence transpose - - angles_betweensvd{comp} = rad2deg(acos(diag(S))); %singular vlaues of this matrix relate to the angles - - principal_angle(comp) = angles_betweensvd{comp}(end); %svd in matlab does descending order apparently. Larget SV is 1st principal angle -end - -table(principal_angle,some_labels,'VariableNames',{'principal angle', 'comparison'}) - - -%% Test 1.3 sanity check on overlap between grooming and all other behaviors -%Using principal angle definition from above to make sure grooming subspace -%doesn't have tremendous overlap with subspace for all other behaviors - -%% Test(s) 2-? Replicating Geometry of sequence working memory in macaque prefrontal cortex analyses -%Our analyses don't exactly align due to the difference in our behavior vs -%their task, but going to try to more or fully implement what they did and see -%where it gets us. - - diff --git a/Varia/Toy_examples/Ron_DoSmth.m b/Varia/Toy_examples/Ron_DoSmth.m deleted file mode 100644 index 416f42e..0000000 --- a/Varia/Toy_examples/Ron_DoSmth.m +++ /dev/null @@ -1,293 +0,0 @@ -%% Ron_DoSmth -% Have fun! - - - -%Set parameters -is_mac = 0; %For loading the data -is_ron = 1; %For setting path -with_partner =1; -temp_resolution = 1; %Temporal resolution of firing rate. 1sec -channel_flag = "TEO"; %Channels considered (sets brain region TEO vlPFC all -with_NC =1; %0: Noise cluster (NC) is excluded; 1:NC is included; 2:ONLY noise cluster -isolatedOnly=0; %Only consider isolated units. 0=all units; 1=only well-isolated units - -for s = 20 %For now focus on session Amos_2021-07-29 - - %Set path - if is_ron - - sessions = dir('C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_data'); sessions = sessions(3:end,:); - filePath = ['C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_data\' sessions(s).name]; - savePath = ['C:\Users\ronwd\OneDrive\Documents\GitHub\Datalogger_results']; - - else - - - %Set session list - home = '~'; % set home directory - cd([home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/']) - sessions = dir('Ready to analyze output'); sessions = sessions(5:end,:); - filePath = [home '/Dropbox (Penn)/Datalogger/Deuteron_Data_Backup/Ready to analyze output/' sessions(s).name]; % Enter the path for the location of your Deuteron sorted neural .nex files (one per channel) - savePath = [home '/Dropbox (Penn)/Datalogger/Results/' sessions(s).name '/SingleUnit_results/partner_vs_subject']; - end - %% Load data - - %Get data with specified temporal resolution and channels - [Spike_rasters, labels, labels_partner, behav_categ, block_times, monkey, reciprocal_set, social_set, ME_final,unit_count, groom_labels_all]= log_GenerateDataToRes_Ron(filePath, temp_resolution, channel_flag, is_mac,is_ron, with_NC, isolatedOnly); - % Output: - % 1. Spike_rasters: n_neuron X time in seconds (if temp_resolution = 1) - % 2. labels: time X 11 - check the function description for - % details. Essentially subject behavior is column 3. - % 3. labels_partner: time X 11. Partner behavior is column 3. - % 4. behav_categ: string vector; behavioral category do the numbers in - % labels correspond to. - % 5. block_times: specifies the block times and ID - % 6. monkey: subjecet monkey ID - % 7. reciprocal_set: set of behaviors that are deeemed "reciprocal" - % 8. social_set: set of behaviors that are deeemed "social" - % 9. ME_final: Motion energy values - % 10. unit_count: number of units in the session - % 11. groom_labels_all: labels for grooming. Check function - % description for details - - session_length = size(Spike_rasters,2); % get session length - Spike_count_raster = Spike_rasters'; - - %Extract behavior labels for subject and partner - behavior_labels_subject_init = cell2mat({labels{:,3}}'); %Extract unique behavior info for subject - behavior_labels_partner_init = cell2mat({labels_partner{:,3}}'); %Extract unique behavior info for partner - - % Set proximity as rest - behavior_labels_subject_init(behavior_labels_subject_init==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - behavior_labels_partner_init(behavior_labels_partner_init==find(behav_categ=="Proximity"))=length(behav_categ); %exclude proximity for now (i.e. mark as "undefined"). - -end - -%% Subdivide data for successive dimensionality tests -%Going to just run successive tests based on what was outlined in the pitch -%Can come back to clean this up to run more efficiently but for now just -%want to confirm for this session that general idea is right. - -%Going to start with using PCA for speed and clarity (i.e. everyone gets -%PCA) - -%Before doing anything else just get the inds for all the groups we want. - -groominds_give = groom_labels_all(:,1) == 7; -groominds_receive = groom_labels_all(:,1) == 8; - -groominds_all = groominds_give + groominds_receive; %Since these are mutually exclusive and (as far as I can tell) define all groom times. -groominds_all = groominds_all>0; %To change it back to logical indexing -groominds_postthreat = groom_labels_all(:,3) == 1; -groominds_reciprocal = groom_labels_all(:, 4) == 1; -groominds_initiated = groom_labels_all(:,5) == 1; - -groominds_self = behavior_labels_subject_init == 24; %24 is label for self groom - -%Use Spike_count_raster as it is in obs x vars format these algorithms are -%expecting - -%% Test 0.1: Basic checks - Dimensionality for grooming, the rest, and all -%So far this does not match what I was predicting. The deviation isn't -%massive, but it is still surprising. Doesn't entirely sink this train of -%thought, so will contibue with other test, but need to reflect on what -%this means. - -threshold = 50; - -[eigenvec, score, eigenval, ~, var_exp] = pca(Spike_count_raster); - -Results.all.eigenvec = eigenvec; -Results.all.score = score; -Results.all.eigenval = eigenval; -Results.all.var_exp = var_exp; - - -Data_grooming = Spike_count_raster(groominds_all,:); -Data_self = Spike_count_raster(groominds_self,:); -Data_groomgive = Spike_count_raster(groominds_give,:); -Data_groomrec = Spike_count_raster(groominds_receive,:); - - -[eigenvec, score, eigenval, ~, var_exp] = pca(Data_grooming); - -Results.groomall.eigenvec = eigenvec; -Results.groomall.score = score; -Results.groomall.eigenval = eigenval; -Results.groomall.var_exp = var_exp; - -[eigenvec, score, eigenval, ~, var_exp] = pca(Data_groomgive); - -Results.groomgive.eigenvec = eigenvec; -Results.groomgive.score = score; -Results.groomgive.eigenval = eigenval; -Results.groomgive.var_exp = var_exp; - -[eigenvec, score, eigenval, ~, var_exp] = pca(Data_groomrec); - -Results.groomrec.eigenvec = eigenvec; -Results.groomrec.score = score; -Results.groomrec.eigenval = eigenval; -Results.groomrec.var_exp = var_exp; - -[eigenvec, score, eigenval, ~, var_exp] = pca(Data_self); - -Results.therest.eigenvec = eigenvec; -Results.therest.score = score; -Results.therest.eigenval = eigenval; -Results.therest.var_exp = var_exp; - -%Get number of eigen values needed to cross threshold of variance explained - -Results.all.majdim = find(cumsum(Results.all.var_exp)>threshold,1,'first'); -Results.therest.majdim = find(cumsum(Results.therest.var_exp)>threshold,1,'first'); -Results.groomall.majdim = find(cumsum(Results.groomall.var_exp)>threshold,1,'first'); -Results.groomgive.majdim = find(cumsum(Results.groomgive.var_exp)>threshold,1,'first'); -Results.groomrec.majdim = find(cumsum(Results.groomrec.var_exp)>threshold,1,'first'); - -%sorted_axis = 1:length(var_exp);%changed mind don't like this./length(var_exp); %quick trick for setting axes for eigen plots -figure -plot(1:length(Results.all.var_exp),cumsum(Results.all.var_exp),'k') -title([channel_flag ' ' num2str(s)]) -ylabel('var explained') -xlabel('Eigenvectors used') -ylim([0,110]) -hold on -plot(1:length(Results.groomall.var_exp),cumsum(Results.groomall.var_exp),'b') -plot(1:length(Results.groomgive.var_exp),cumsum(Results.groomgive.var_exp),'b--') -plot(1:length(Results.groomrec.var_exp),cumsum(Results.groomrec.var_exp),'b.-') -plot(1:length(Results.therest.var_exp),cumsum(Results.therest.var_exp),'r') -plot(1:length(var_exp), threshold * ones(size(var_exp)),'k--') %put dashed black line for majority of variance -%number beside is number of PCs needed to cross threshold var explained -legend(['All: ' num2str(Results.all.majdim)],... - ['Grooming: ' num2str(Results.groomall.majdim)],... - ['Groom Give: ' num2str(Results.groomgive.majdim)],... - ['Groom Receive:' num2str(Results.groomrec.majdim)],... - ['Groom Self: ' num2str(Results.therest.majdim)],... - 'Threshold') - - - -%% Test 1.1 Compare dimensionality: post threat, initiated, reciprocal - -%Dimensionality is effectively the same across these conditions as well as -%compared to considering all grooming. Kind of interesting to see; need to -%think more on what it means - -Data_groompt = Spike_count_raster(groominds_postthreat,:); -Data_groomrecip = Spike_count_raster(groominds_reciprocal,:); -Data_groomini = Spike_count_raster(groominds_initiated,:); - -[eigenvec, score, eigenval, ~, var_exp] = pca(Data_groompt); - -Results.groompt.eigenvec = eigenvec; -Results.groompt.score = score; -Results.groompt.eigenval = eigenval; -Results.groompt.var_exp = var_exp; - -[eigenvec, score, eigenval, ~, var_exp] = pca(Data_groomrecip); - -Results.groomrecip.eigenvec = eigenvec; -Results.groomrecip.score = score; -Results.groomrecip.eigenval = eigenval; -Results.groomrecip.var_exp = var_exp; - -[eigenvec, score, eigenval, ~, var_exp] = pca(Data_groomini); - -Results.groomini.eigenvec = eigenvec; -Results.groomini.score = score; -Results.groomini.eigenval = eigenval; -Results.groomini.var_exp = var_exp; - - -Results.groompt.majdim = find(cumsum(Results.groompt.var_exp)>threshold,1,'first'); -Results.groomrecip.majdim = find(cumsum(Results.groomrecip.var_exp)>threshold,1,'first'); -Results.groomini.majdim = find(cumsum(Results.groomini.var_exp)>threshold,1,'first'); - - -figure -plot(1:length(Results.groomall.var_exp),cumsum(Results.groomall.var_exp),'b') -title([channel_flag ' ' num2str(s)]) -ylabel('var explained') -xlabel('Eigenvectors used') -hold on -plot(1:length(Results.groompt.var_exp),cumsum(Results.groompt.var_exp),'bx') -plot(1:length(Results.groomrecip.var_exp),cumsum(Results.groomrecip.var_exp),'b--') -plot(1:length(Results.groomini.var_exp),cumsum(Results.groomini.var_exp),'b.-') - -plot(sorted_axis, threshold * ones(size(sorted_axis)),'k--') %put dashed black line for majority of variance -%number beside is number of PCs needed to cross threshold var explained -legend(['Grooming: ' num2str(Results.groomall.majdim)],... - ['Groom Post Threat: ' num2str(Results.groompt.majdim)],... - ['Groom Reciprocal: ' num2str(Results.groomrecip.majdim)],... - ['Groom Initated: ' num2str(Results.groomini.majdim)],... - 'Threshold') - -%% Test 1.2 Grooming lies in different subspaces based on context: post threat, initiated, reciprocal -%First do simple test with average angle between PCs -%Next as there was more to that paper than I thought and my first tries are -%not exactly working as expected; implement the analyses from the below -%paper to see what that gets us and if some of their processing steps are -%necessary to see this structure. - -%Not eigenvectors come out of PCA in matlab already normalized to have unit -%vector norm (i.e. norm(eigenvec(:,1)) = 1 - -%Just use maximum number need to cross threshold of variance explained from above. -num_eigenvec = max([Results.groompt.majdim,Results.groomrecip.majdim, Results.groomini.majdim]); - - -collected_eigens = cell(3,1); - -collected_eigens{1} = Results.groompt.eigenvec(:,1:num_eigenvec); -collected_eigens{2} = Results.groomrecip.eigenvec(:,1:num_eigenvec); -collected_eigens{3} = Results.groomini.eigenvec(:,1:num_eigenvec); - -angles_between = cell(3,1); %n choose k with 3 is 3!/2! (3-2)! = 3 -avg_angle = nan(3,1); - -comparison = [1 2; 1 3; 2 3]; %better ways to do this but for now just manually put in all of the comparisons we want to do - -% for comp = 1:length(angles_between) -% -% angles_between{comp} = acos(dot(collected_eigens{comparison(comp,1)},collected_eigens{comparison(comp,2)})); -% -% avg_angle(comp) = rad2deg(mean(angles_between{comp})); -% -% -% end -% - some_labels = {'post-threat to reciprocal','post-threat to initiated','reciprocal to initated'}'; -% -% table(avg_angle,some_labels,'VariableNames',{'avg angle', 'comparison'}) - -%Now trying the principal angles definition that is used in the below paper -%and seems to be more standard in the field given some reading. Take the -%SVD of the product of the two subspaces - -angles_betweensvd = cell(3,1); -principal_angle = nan(size(angles_betweensvd)); - -for comp = 1:length(angles_between) - - [~,S,~]= svd(collected_eigens{comparison(comp,1)}' * collected_eigens{comparison(comp,2)}); %want to do SVD on PC x PC matrix, hence transpose - - angles_betweensvd{comp} = rad2deg(acos(diag(S))); %singular vlaues of this matrix relate to the angles - - principal_angle(comp) = angles_betweensvd{comp}(end); %svd in matlab does descending order apparently. Larget SV is 1st principal angle -end - -table(principal_angle,some_labels,'VariableNames',{'principal angle', 'comparison'}) - - -%% Test 1.3 sanity check on overlap between grooming and all other behaviors -%Using principal angle definition from above to make sure grooming subspace -%doesn't have tremendous overlap with subspace for all other behaviors - -%% Test(s) 2-? Replicating Geometry of sequence working memory in macaque prefrontal cortex analyses -%Our analyses don't exactly align due to the difference in our behavior vs -%their task, but going to try to more or fully implement what they did and see -%where it gets us. - - diff --git a/Varia/bimodal_poisson.m b/Varia/bimodal_poisson.m deleted file mode 100644 index 5dfe2a2..0000000 --- a/Varia/bimodal_poisson.m +++ /dev/null @@ -1,45 +0,0 @@ -mu1 = 15; % average firing rate of subpolutation 1 -mu2 = 2; % average firing rate of subpopulation 2 -frac1 = 0.5; % average fraction of neurons in subpopulation 1 -gamma_shape = 5; % parameter controlling distribution of rates in each population (higher = more gaussian, lower (with minimum 1) = more exponential) - -n = 100; % num neurons -t = 2000; % num trials - - -figure; -subplot(1,2,1); hold on; -subplot(1,2,2); hold on; - -for run=1:10 - rates = zeros(1, n); - % Make some simulated neurons, each with a particular rate. Roughly frac1*n of them will have a - % rate close to mu1, and (1-frac1)*n of them will have a rate close to mu2. - rates1 = gamrnd(gamma_shape, mu1/gamma_shape, n, 1); - rates2 = gamrnd(gamma_shape, mu2/gamma_shape, n, 1); - is_population_1 = rand(n, 1) < frac1; - rates(is_population_1) = rates1(is_population_1); - rates(~is_population_1) = rates2(~is_population_1); - - subplot(1,2,1); hold on; - histogram(rates, 0:2:50); - - % Generate poisson "spiking" data for n neurons on t trials - spikes = poissrnd(repmat(rates, t, 1)); - - c = cov(spikes); - e = eig(c); - - subplot(1,2,2); hold on; - plot(flipud(e)); -end - -subplot(1,2,1); -xlabel('rate'); -title('(bimodal) histogram of rates'); - -subplot(1,2,2); -set(gca, 'yscale', 'log'); -xlabel('rank'); -ylabel('eigenvalue'); -title('eigenspectrum'); diff --git a/Varia/poissHMM_test.m b/Varia/poissHMM_test.m deleted file mode 100644 index ce622b0..0000000 --- a/Varia/poissHMM_test.m +++ /dev/null @@ -1,120 +0,0 @@ - - -temp_resolution =1000; - -%Set data path -cd(['~/Dropbox (Penn)/Churchland_analysis/Wireless8a/data/all_tasks/Natural']) -datapath = ['~/Dropbox (Penn)/Churchland_analysis/Wireless8a/data/all_tasks/Natural/']; - -Files=dir(); -all_sessions = {Files.name}; session_names = all_sessions(4:end); -s=1; - -%Load in files. Note files are large so this can take a few moments. -load([datapath session_names{s}]) -length_recording = size(Unit_rasters,2); - -channels = 1:length(fields(SpikeData)); -Chan_name = fieldnames(SpikeData); %Identify channel names -C = regexp(Chan_name,'\d*','Match'); -C_char = cellfun(@char, C{:}, 'UniformOutput', false); -Chan_num = str2num(C_char{1, 1}); - -%Separate channels by array -%IMPORTANT NOTE: the channel mapping is reversed for each monkey -monkey='Hooke'; -if strcmp(monkey,'Hooke') - - TEO_chan = [1,2,5,6,9,10,13,14,17,18,21,22,25,26,29,30,33,34,37,38,41,... - 42,45,46,49,50,53,54,57,58,61,62,65,66,69,70,73,74,77,78,81,82,85,86,... - 89,90,93,94,97,98,101,102,105,106,109,110,113,114,117,118,121,122,125,126]; - - vlPFC_chan = [3,4,7,8,11,12,15,16,19,20,23,24,27,28,31,32,35,36,39,40,... - 43,44,47,48,51,52,55,56,59,60,63,64,67,68,71,72,75,76,79,80,83,84,... - 87,88,91,92,95,96,99,100,103,104,107,108,111,112,115,116,119,120,123,124,127,128]; - -elseif strcmp(monkey,'Amos') - - vlPFC_chan = [1,2,5,6,9,10,13,14,17,18,21,22,25,26,29,30,33,34,37,38,41,... - 42,45,46,49,50,53,54,57,58,61,62,65,66,69,70,73,74,77,78,81,82,85,86,... - 89,90,93,94,97,98,101,102,105,106,109,110,113,114,117,118,121,122,125,126]; - - TEO_chan = [3,4,7,8,11,12,15,16,19,20,23,24,27,28,31,32,35,36,39,40,... - 43,44,47,48,51,52,55,56,59,60,63,64,67,68,71,72,75,76,79,80,83,84,... - 87,88,91,92,95,96,99,100,103,104,107,108,111,112,115,116,119,120,123,124,127,128]; - -end - -chan_idx_TEO = find(ismember(Chan_num,TEO_chan))'; -chan_idx_vlPFC = find(ismember(Chan_num,vlPFC_chan))'; - - -%Select channels -channel_flag="all"; -if strcmp(channel_flag,'TEO') - channels = chan_idx_TEO; -elseif strcmp(channel_flag,'vlPFC') - channels = chan_idx_vlPFC; -elseif strcmp(channel_flag,'all') - channels = 1:length(fields(SpikeData)); %all channels -end - -unit=1; -for i = channels %For all channels - if ~isempty(SpikeData.(Chan_name{i})) %If there are sorted units on this channel - for j = 1:length(SpikeData.(Chan_name{i})) %For all units - - Spike_rasters(unit,:) = zeros(1,round(length_recording*temp_resolution)); %Fill the line with zeros to initiate raster for that trial (IMPORTANT NOTE: removed +1) - ticks = round(SpikeData.(Chan_name{i}){j}*temp_resolution); - Spike_counts = hist(ticks, 1:round(length_recording*temp_resolution)); - Spike_rasters(unit, :) = Spike_counts; %Fill in spikes in the raster - % - % if ismember(Chan_num(i),TEO_chan) - % brain_label(unit) = "TEO"; - % else - % brain_label(unit) = "vlPFC"; - % end - - clear ticks Spike_counts - - disp(unit) - unit = unit+1; - end - end -end - -interval=200*temp_resolution:250*temp_resolution; -[bestPath,maxPathLogProb,PI,A,B,gamma] = poissHMM(Spike_rasters,10,temp_resolution,5); - -nPredictedStates=10; -neural_data=int64(Spike_rasters); -nNeurons=size(neural_data,1); - -for i=1:nPredictedStates - for j=1:nPredictedStates - if (i == j) - TRGUESS(i,j) = .99; - else - TRGUESS(i,j) = .01/(nPredictedStates-1); - end - end -end -EMITGUESS=rand(nPredictedStates,nNeurons+1); -[ESTTR,ESTEMIT] = hmmtrain(neural_data,TRGUESS,EMITGUESS); -STATES = hmmviterbi(symbols,ESTTR,ESTEMIT); -PSTATES = hmmdecode(symbols,ESTTR,ESTEMIT); -figure; -subplot(2,1,1) -plot(tvec,STATES); hold on; plot(tvec,stateSeqVec) -subplot(2,1,2) -plot(PSTATES') - - -num_states = 10; -TRANSG = -seq = activity_all; -[TRANS,EMIS] = hmmtrain(seq, model.A, model.E,'Tolerance',1e-10, 'Maxiterations',1e5); -figure; heatmap(TRANS) -STATES = hmmviterbi(seq,TRANS,EMIS) -figure; hold on; plot(STATES); plot(labels_final) -count=hist(STATES); \ No newline at end of file diff --git a/Varia/umap_in_R.R b/Varia/umap_in_R.R deleted file mode 100644 index 478eea5..0000000 --- a/Varia/umap_in_R.R +++ /dev/null @@ -1,33 +0,0 @@ -library(tidyverse) -library(ggplot2) -library(umap) -library(RColorBrewer) -library(R.matlab) - -#Load data: -file = file.choose() -data <- readMat(file) -neural.data<- as.data.frame(data$Spike.count.raster) -labels<- as.factor(data$behavior.labels) -hist(table(labels), freq=TRUE, xlab = levels(labels), ylab = "Frequencies") - -#Select behaviors of interest -behav = c(4:6,17) -idx = which(labels %in% behav) -neural.data.final = neural.data[idx,] -labels.final = labels[idx] - -set.seed(3) - -a = umap(neural.data.final)#, n_neighbors = 5, min_dist = 0.5) -a.umap = data.frame(as.data.frame(a$layout),labels.final) - -ggplot(a.umap,aes_string('V1','V2',color='labels.final')) + - geom_point(size=1.25) + - #scale_color_manual(values=region.colors) + - theme_classic(base_size=18) + - xlab('UMAP 1') + - ylab('UMAP 2') + - coord_fixed() + - theme(axis.ticks=element_blank(),axis.text=element_blank()) -ggsave(p,file='figures/data_visualization_dimension_reduction_umap_batched.pdf',useDingbats=FALSE) \ No newline at end of file diff --git a/Varia/w8a_example_code/SDF_script.m b/Varia/w8a_example_code/SDF_script.m deleted file mode 100644 index e83e7cf..0000000 --- a/Varia/w8a_example_code/SDF_script.m +++ /dev/null @@ -1,156 +0,0 @@ -%% w8a_SpikeDensityFunction_perTarget_batch -% This is a batch implementation of w8a_SpikeDensityFunction_perTarget -% This script produces spike density function plots that pool trials according to target identity (star or circle). -% The script produces the raster matrix for selected blocks and convolves the raster to produce the SDF. -% The SDF is aligned to three events of interest (Cue onset, Target onset, and Response) and concatenated into a single SDF. -% Created by Testard and Tremblay 02/2017 - - - %Identify hit trials - %Identify the trial numbers part of each block - Trials_block1 = find(BHV.BlockNumber == block(1)); - Trials_block2 = find(BHV.BlockNumber == block(2)); - %Identify the hit trials in each block - hit_block1 = hits(ismember(hits, Trials_block1)); - hit_block2 = hits(ismember(hits, Trials_block2)); - %Pool the hits from both blocks (Corrupts chronology of trials. Shouldn't matter for SDF) - - %Get the timestamp of cue and target onset in the neural data for each hit trial (Not in chronological order) - for i = 1:length(hits) - Cue_onset_neural(i) = words_per_trials(hits(i)).timestamps(words_per_trials(hits(i)).words == 12); - Targets_onset(i) = words_per_trials(hits(i)).timestamps(words_per_trials(hits(i)).words == 25); - Response(i) = words_per_trials(hits(i)).timestamps(words_per_trials(hits(i)).words == 40); - end - Cue_onset_neural = Cue_onset_neural'; - Targets_onset = Targets_onset'; - Response = Response'; - - %% Create matrix for rasters aligned to three events - %Define your analysis periods (in seconds) - Analysis_period_lenght = 5000; %msec - Cue_window_length = 3000; %msec - Target_window_length = 750; - Response_window_length = 1250; - - %Define the timestamps limits for the analysis period for each trial - Analysis_period_cue = [(Cue_onset_neural - 1.5) (Cue_onset_neural + 1.5)]; - Analysis_period_target = [(Targets_onset - .5) (Targets_onset + .25)]; - Analysis_period_response = [(Response - .25) (Response + 1)]; - - %Create structure with rasters per trials for all units - hWaitbar = waitbar(0, 'Creating Raster Matrix'); - unit=1; - for i = 1:length(fields(SpikeData)) %For all channels - waitbar(i/length(fields(SpikeData)), hWaitbar) - - for j = 2:length(SpikeData.(['Channel_' num2str(i)])) %For all units - - for k = 1:length(hits) %For all hit trials - - Unit_rasters{unit}(k,:) = zeros(1,Analysis_period_lenght); %Fill the line with zeros to initiate raster for that trial - temp_cue = find(SpikeData.(['Channel_' num2str(i)]){j} > Analysis_period_cue(k,1) & SpikeData.(['Channel_' num2str(i)]){j} < Analysis_period_cue(k,2)); %Find indicies of spikes during this analysis period - tick_cue = SpikeData.(['Channel_' num2str(i)]){j}(temp_cue) - Analysis_period_cue(k,1); %Align those spikes to cue onset -1500ms for that trial - tick_cue = ceil(tick_cue*1000); %Convert spikes timings (in raster time) to miliseconds - Unit_rasters{unit}(k, tick_cue) = 1; %Fill in spikes in the raster - temp_target = find(SpikeData.(['Channel_' num2str(i)]){j} > Analysis_period_target(k,1) & SpikeData.(['Channel_' num2str(i)]){j} < Analysis_period_target(k,2)); %Find indicies of spikes during this analysis period - tick_target = SpikeData.(['Channel_' num2str(i)]){j}(temp_target) - Analysis_period_target(k,1); %Align those spikes to target - 500ms for that trial - tick_target = ceil(tick_target*1000) + Cue_window_length; %Convert spikes timings (in raster time) to miliseconds, add the cue analysis period length so as to concatenate both matrices. - Unit_rasters{unit}(k, tick_target) = 1; %Fill in spikes in the raster - temp_response = find(SpikeData.(['Channel_' num2str(i)]){j} > Analysis_period_response(k,1) & SpikeData.(['Channel_' num2str(i)]){j} < Analysis_period_response(k,2)); %Find indicies of spikes during this analysis period - tick_response = SpikeData.(['Channel_' num2str(i)]){j}(temp_response) - Analysis_period_response(k,1); %Align those spikes to response - 250ms for that trial - tick_response = ceil(tick_response*1000) + Cue_window_length + Target_window_length; %Convert spikes timings (in raster time) to miliseconds, add the cue analysis period length so as to concatenate both matrices. - Unit_rasters{unit}(k, tick_response) = 1; %Fill in spikes in the raster - clear tick_cue temp_cue tick_target temp_target temp_response tick_response - end - Electrode_ID(unit) = i; - unit = unit+1; - end - - end - - close(hWaitbar) - - %% Compute SDF - %Define kernel - sigma = .045; %Define width of kernel (in sec) - edges = -3*sigma:.001:3*sigma; - kernel = normpdf(edges,0,sigma); %Use a gaussian function - kernel = kernel*.001; %Time 1/1000 so the total area under the gaussian is 1 - - % Compute Spike Density Function for all hit trials - hWaitbar = waitbar(0, 'Convolving rasters'); - for i = 1:length(Unit_rasters) % for all units - waitbar(i/length(Unit_rasters), hWaitbar) - for j = 1:size(Unit_rasters{1,i},1) % for all hit trials - - sdf = conv(Unit_rasters{1,i}(j,:),kernel); %Convolve the gaussian - center = ceil(length(edges)/2); - SDF{1,i}(j,:) = sdf(center:length(sdf)-(center-1)).*1000; %Substract the irrelevant edges due to convolution operation - clear sdf - - end - end - close(hWaitbar) - clear Unit_rasters - - - %Identify trials where the correct target was a star or a circle among the hit trials - star = strfind(BHV.TimingFileByCond, 'visual_star.m'); star_conditions = find(not(cellfun('isempty', star))); - circle = strfind(BHV.TimingFileByCond, 'visual_circle.m'); circle_conditions = find(not(cellfun('isempty', circle))); - star_hit_trials = find(ismember(BHV.ConditionNumber(hits), star_conditions)); % indices of star trials in the hit trials vector (hits) - circle_hit_trials = find(ismember(BHV.ConditionNumber(hits), circle_conditions)); - - % Compute Mean SDF for correct target star and correct target circle - for i = 1:length(SDF) %For all units - Mean_SDF{1,i} = mean(SDF{1,i}(star_hit_trials,:)); - Error_SDF{1,i} = std(SDF{1,i}(star_hit_trials,:))/(size(SDF{1,i}(star_hit_trials,:),1))^(1/2); - Mean_SDF{2,i} = mean(SDF{1,i}(circle_hit_trials,:)); - Error_SDF{2,i} = std(SDF{1,i}(circle_hit_trials,:))/(size(SDF{1,i}(circle_hit_trials,:),1))^(1/2); - end - - - %% Plotting - hWaitbar = waitbar(0, ['Creating SDF plots for each ' num2str(length(SDF)) ' unit']); - x = 1:length(Mean_SDF{1,1}); % Define x axis for plotting - C = {'g', 'b'}; % Define colors for SDF, green for star, blue for circle - - for unit = 1:length(SDF) % For all units - waitbar(unit/length(SDF), hWaitbar); - - for j = 1:size(Mean_SDF,1) % For all task conditions - - figure(unit); - hold on - - %Reference timings - line([1500 1500], [0 max(max(Mean_SDF{j,unit}))+max(max(Mean_SDF{j,unit}))/10], 'Color', 'g') % mark cue onset - line([2500 2500], [0 max(max(Mean_SDF{j,unit}))+max(max(Mean_SDF{j,unit}))/10], 'Color', 'g') % mark cue offset - line([3000 3000], [0 max(max(Mean_SDF{j,unit}))+max(max(Mean_SDF{j,unit}))/10], 'Color', 'k', 'LineStyle', '--') % mark delay axis break - line([3500 3500], [0 max(max(Mean_SDF{j,unit}))+max(max(Mean_SDF{j,unit}))/10], 'Color', 'r') % mark target onset - line([3750 3750], [0 max(max(Mean_SDF{j,unit}))+max(max(Mean_SDF{j,unit}))/10], 'Color', 'k', 'LineStyle', '--') % mark reaction time axis break - line([4000 4000], [0 max(max(Mean_SDF{j,unit}))+max(max(Mean_SDF{j,unit}))/10], 'Color', 'c') % mark response onset - - %Plot SDF - fill([x fliplr(x)], [Mean_SDF{j,unit}(1,:)+Error_SDF{j,unit}(1,:), fliplr(Mean_SDF{j,unit}(1,:)-Error_SDF{j,unit}(1,:))], C{j}) - plot(Mean_SDF{j,unit}(1,:), 'r') - - end - - title(['Star (green) and Circle (blue) hit trials, Neuron # ' num2str(unit) ' , Channel # ' num2str(Electrode_ID(unit))]) - xlabel('Time (msec)') - ylabel('Firing rate (Hz)') - ylim([-Inf Inf]) - xlim([500 Analysis_period_lenght-200]) - set(gca,'TickDir','out') % draw the tick marks on the outside - - pause(1) - close all - - end %End of loop over all units in this session - - close(hWaitbar) - - disp(['SDF plots done in ', num2str(toc), ' seconds for session ' session_name]) - - clearvars -except saving_folder Session PathName neural_dir correct_drift - diff --git a/Varia/w8a_example_code/raster_plot_script.m b/Varia/w8a_example_code/raster_plot_script.m deleted file mode 100644 index a539179..0000000 --- a/Varia/w8a_example_code/raster_plot_script.m +++ /dev/null @@ -1,97 +0,0 @@ - - - %% Plot the rasters aligned to Cue onset - close all - tic - - %Identify hit trials - hits = find(BHV.TrialError == 0); - - %Identify timing of relevant events (all timings are relative to cue onset on a trial by trial basis) - cue_ticks = 1500; %Identify time of significant cue onset on raster (as specified above) - for y = 1:length(hits) %identify other timings that are trial specific - targets_onset(y) = (words_per_trials(hits(y)).timestamps(words_per_trials(hits(y)).words == 25) - words_per_trials(hits(y)).timestamps(words_per_trials(hits(y)).words == 12))*1000 +1500; - target_selection(y) = (words_per_trials(hits(y)).timestamps(words_per_trials(hits(y)).words == 40)- words_per_trials(hits(y)).timestamps(words_per_trials(hits(y)).words == 12))*1000 +1500; - end - - %Identify trials where the correct target was a star or a circle among the hit trials - star = strfind(BHV.TimingFileByCond, 'visual_star.m'); star_conditions = find(not(cellfun('isempty', star))); - circle = strfind(BHV.TimingFileByCond, 'visual_circle.m'); circle_conditions = find(not(cellfun('isempty', circle))); - star_hit_trials = find(ismember(BHV.ConditionNumber(hits), star_conditions)); % indices of star trial among hit trials vector (hits) - circle_hit_trials = find(ismember(BHV.ConditionNumber(hits), circle_conditions)); - - %Find the indices of hit trials when targets onset time are organized in ascending order - [~,index_star] = sort(targets_onset(star_hit_trials)); - [~,index_circle] = sort(targets_onset(circle_hit_trials)); - - clear BHV - - hWaitbar = waitbar(0, ['Creating raster plots for each unit in ' session_name]); - - % Draw the raster plot figures for each neuron - for i = 1:length(Unit_rasters) %For all units - - waitbar(i/length(Unit_rasters), hWaitbar); - - figure - hold on - - subplot(2,1,1) %Star trials - for j = 1:length(star_hit_trials) %For all hit trials with star as a target - - line([cue_ticks cue_ticks], [j-.3 j+.3], 'Color', 'g', 'LineWidth', 1.5) %Cue onset - line([cue_ticks+1000 cue_ticks+1000], [j-.3 j+.3], 'Color', 'r', 'LineWidth', 1.5) %Cue Offset - line([targets_onset(star_hit_trials(index_star(j))) targets_onset(star_hit_trials(index_star(j)))], [j-.3 j+.3], 'Color', 'g', 'LineWidth', 1.5) - line([target_selection(star_hit_trials(index_star(j))) target_selection(star_hit_trials(index_star(j)))], [j-.3 j+.3], 'Color', 'r', 'LineWidth', 1.5) - - ticks = find(Unit_rasters{i}(star_hit_trials(index_star(j)),:)); %Find the index of this spike - hold on; scatter(ticks, ones(1,length(ticks))*j, .5, 'b', 'filled') - - end - title(['Star target, Neuron # ' num2str(i) ' , Channel # ' num2str(Electrode_ID(i))]) - xlabel('Bin # (1 msec)') - ylabel('Trial #') - ylim([0 length(star_hit_trials)+1]) - xlim([0 Analysis_period_lenght*1000]) - set(gca,'TickDir','out') % draw the tick marks on the outside - - subplot(2,1,2) %Circle trials - for j = 1:length(circle_hit_trials) %For all hit trials with circle as a target - - line([cue_ticks cue_ticks], [j-.3 j+.3], 'Color', 'g', 'LineWidth', 1.5) %Cue onset - line([cue_ticks+1000 cue_ticks+1000], [j-.3 j+.3], 'Color', 'r', 'LineWidth', 1.5) %Cue Offset - line([targets_onset(circle_hit_trials(index_circle(j))) targets_onset(circle_hit_trials(index_circle(j)))], [j-.3 j+.3], 'Color', 'g', 'LineWidth', 1.5) - line([target_selection(circle_hit_trials(index_circle(j))) target_selection(circle_hit_trials(index_circle(j)))], [j-.3 j+.3], 'Color', 'r', 'LineWidth', 1.5) - - ticks = find(Unit_rasters{i}(circle_hit_trials(index_circle(j)),:)); %Find the index of this spike - hold on; scatter(ticks, ones(1,length(ticks))*j, .5, 'b', 'filled') - - end - title(['Circle target, Neuron # ' num2str(i) ' , Channel # ' num2str(Electrode_ID(i))]) - xlabel('Bin # (1 msec)') - ylabel('Trial #') - ylim([0 length(circle_hit_trials)+1]) - xlim([0 Analysis_period_lenght*1000]) - set(gca,'TickDir','out') % draw the tick marks on the outside - - - % Saving the plots - obj = findobj('type', 'figure'); - - %Adjust paths - if ~exist([saving_folder '/' session_name '/Neuron_' num2str(i)], 'dir'); - mkdir([saving_folder '/' session_name '/Neuron_' num2str(i)]) - end - cd([saving_folder '/' session_name '/Neuron_' num2str(i)]) - saveas(obj, ['Neuron_' num2str(i) '_Raster_aligned_RT'], 'tif') - close all - - end %End of loop over all units in this session - - close(hWaitbar) - - disp(['Raster plots done in ', num2str(toc), ' seconds for session ' session_name]) - - clearvars -except saving_folder Session PathName neural_dir - -end %end of iteration across sessions diff --git a/Varia/w8a_example_code/unit_matrix_script.m b/Varia/w8a_example_code/unit_matrix_script.m deleted file mode 100644 index fa5d75c..0000000 --- a/Varia/w8a_example_code/unit_matrix_script.m +++ /dev/null @@ -1,49 +0,0 @@ -%Practice script for making unit matrix. -% Info to know: word 12 = cue onset - -%Load data - -%Define your analysis period after target onset (in seconds) -Analysis_period_lenght = 5.5; - -% Identify indices of hit trials -hits = find(BHV.TrialError == 0); - -%Define your event of interest to which neural activity is aligned -for i = 1:length(hits) %for all hit trials - Cue_onset_neural(i) = words_per_trials(hits(i)).timestamps(words_per_trials(hits(i)).words == 12); %Get the timestamp of the cue -end -Cue_onset_neural = Cue_onset_neural'; - -%Define the timestamps limits for the analysis period for each trial -Analysis_period = [Cue_onset_neural - 1.5 (Cue_onset_neural + 4)]; - -hWaitbar = waitbar(0, 'Creating raster matrix'); - -%Create structure with rasters per hit trials for all units -unit=1; -for i = 1:length(fields(SpikeData)) %For all channels - waitbar(i/length(fields(SpikeData)), hWaitbar); - - for j = 2:length(SpikeData.(['Channel_' num2str(i)])) %For all units, except the unsorted cluster - - for k = 1:length(Cue_onset_neural) %For all hit trials - - Unit_rasters{unit}(k,:) = zeros(1,Analysis_period_lenght*1000); %Fill the line with zeros to initiate raster for that trial - temp = find(SpikeData.(['Channel_' num2str(i)]){j} > Analysis_period(k,1) & SpikeData.(['Channel_' num2str(i)]){j} < Analysis_period(k,2)); %Find indicies of spikes during this analysis period - ticks = SpikeData.(['Channel_' num2str(i)]){j}(temp) - Analysis_period(k,1); %Align those spikes to cue onset - 1500 msec for that trial - ticks = ceil(ticks*1000); %Convert spikes timings (in raster time) to miliseconds - Unit_rasters{unit}(k, ticks) = 1; %Fill in spikes in the raster - clear ticks temp - end - Electrode_ID(unit) = i; - unit = unit+1; - end - -end - -close(hWaitbar) - -clearvars -except BHV words_per_trials Unit_rasters Electrode_ID Analysis_period_lenght saving_folder session_name Session PathName neural_dir - -disp(['Creation of the raster matrix done in ', num2str(toc), ' seconds.'])