Przeglądaj źródła

First Code Upload

All of the code used to analyze data in our manuscript
David J Ottenheimer 3 lat temu
rodzic
commit
c5c1d94784
100 zmienionych plików z 9528 dodań i 0 usunięć
  1. BIN
      MatlabScripts/.DS_Store
  2. 196 0
      MatlabScripts/1_InitialAnalysis/GenerateCuedDataForModeling.m
  3. 127 0
      MatlabScripts/1_InitialAnalysis/GenerateTHDataForModeling.m
  4. 161 0
      MatlabScripts/1_InitialAnalysis/GenerateintBlocksDataForModeling.m
  5. 174 0
      MatlabScripts/1_InitialAnalysis/PSTHsCued.m
  6. 187 0
      MatlabScripts/1_InitialAnalysis/PSTHsSucMal.m
  7. 175 0
      MatlabScripts/1_InitialAnalysis/PSTHsTH.m
  8. 149 0
      MatlabScripts/1_InitialAnalysis/RewHist2R_calculator.m
  9. 144 0
      MatlabScripts/1_InitialAnalysis/RewHistCued_calculator.m
  10. 132 0
      MatlabScripts/1_InitialAnalysis/RewHistTH_calculator.m
  11. 196 0
      MatlabScripts/1_InitialAnalysis/block_evolution.m
  12. 35 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_base.m
  13. 40 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_base_asymm.m
  14. 52 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_base_asymm_cue.m
  15. 47 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_base_cue.m
  16. 39 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_curr.m
  17. 49 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_curr_cue.m
  18. 14 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_mean.m
  19. 38 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_mean_cue.m
  20. 44 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_threeValue.m
  21. 49 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_threeValue_asymm.m
  22. 59 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_threeValue_asymm_cue.m
  23. 54 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_threeValue_cue.m
  24. 35 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_value/ott_RW_V_base.m
  25. 40 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_value/ott_RW_V_base_asymm.m
  26. 52 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_value/ott_RW_V_base_asymm_cue.m
  27. 47 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_value/ott_RW_V_base_cue.m
  28. 14 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_value/ott_RW_V_mean.m
  29. 38 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_value/ott_RW_V_mean_cue.m
  30. 39 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_value/ott_RW_V_prev.m
  31. 49 0
      MatlabScripts/2_Modeling/cue/fit_model/RWmodels_value/ott_RW_V_prev_cue.m
  32. 77 0
      MatlabScripts/2_Modeling/cue/fit_model/fit_RW_MLE.m
  33. 282 0
      MatlabScripts/2_Modeling/cue/fit_model/helper_RW_RPE.m
  34. 205 0
      MatlabScripts/2_Modeling/cue/fit_model/helper_RW_V.m
  35. 38 0
      MatlabScripts/2_Modeling/cue/scripts/enrichment_predcue_cueRD.m
  36. 32 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_V.m
  37. 34 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_adapt.m
  38. 32 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_base.m
  39. 37 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_base_asymm.m
  40. 37 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_base_asymm_flipped.m
  41. 32 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_base_flipped.m
  42. 39 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_base_rect.m
  43. 21 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_curr.m
  44. 21 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_curr_flipped.m
  45. 35 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_habit.m
  46. 40 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_habit_asymm.m
  47. 15 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_mean.m
  48. 32 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_value/ott_RW_V_base.m
  49. 37 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_value/ott_RW_V_base_asymm.m
  50. 15 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_value/ott_RW_V_mean.m
  51. 33 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_value/ott_RW_V_prev.m
  52. 45 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/fit_RW_MLE.m
  53. 279 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/helper_RW_RPE.m
  54. 119 0
      MatlabScripts/2_Modeling/intBlocks/fit_model/helper_RW_V.m
  55. 46 0
      MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/fit_gRW_MLE.m
  56. 32 0
      MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/gRWmodels_RPE/ott_gRW_RPE_base.m
  57. 37 0
      MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/gRWmodels_RPE/ott_gRW_RPE_base_asymm.m
  58. 20 0
      MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/gRWmodels_RPE/ott_gRW_RPE_curr.m
  59. 14 0
      MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/gRWmodels_RPE/ott_gRW_RPE_mean.m
  60. 32 0
      MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/gRWmodels_value/ott_gRW_V_base.m
  61. 37 0
      MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/gRWmodels_value/ott_gRW_V_base_asymm.m
  62. 14 0
      MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/gRWmodels_value/ott_gRW_V_mean.m
  63. 124 0
      MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/helper_gRW_RPE.m
  64. 119 0
      MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/helper_gRW_V.m
  65. 32 0
      MatlabScripts/2_Modeling/intBlocks/scripts/adaptationModel.m
  66. 43 0
      MatlabScripts/2_Modeling/intBlocks/scripts/overlap_cueV_rdRPE.m
  67. 234 0
      MatlabScripts/2_Modeling/intBlocks/scripts/permutationTest.m
  68. 282 0
      MatlabScripts/2_Modeling/intBlocks/scripts/recover_RW_MLE_sim.m
  69. 41 0
      MatlabScripts/2_Modeling/threeOutcomes/fit_model/RWmodels_RPE/ott_RW_RPE_base.m
  70. 46 0
      MatlabScripts/2_Modeling/threeOutcomes/fit_model/RWmodels_RPE/ott_RW_RPE_base_asymm.m
  71. 28 0
      MatlabScripts/2_Modeling/threeOutcomes/fit_model/RWmodels_RPE/ott_RW_RPE_curr.m
  72. 15 0
      MatlabScripts/2_Modeling/threeOutcomes/fit_model/RWmodels_RPE/ott_RW_RPE_mean.m
  73. 41 0
      MatlabScripts/2_Modeling/threeOutcomes/fit_model/RWmodels_value/ott_RW_V_base.m
  74. 46 0
      MatlabScripts/2_Modeling/threeOutcomes/fit_model/RWmodels_value/ott_RW_V_base_asymm.m
  75. 15 0
      MatlabScripts/2_Modeling/threeOutcomes/fit_model/RWmodels_value/ott_RW_V_mean.m
  76. 39 0
      MatlabScripts/2_Modeling/threeOutcomes/fit_model/RWmodels_value/ott_RW_V_prev.m
  77. 41 0
      MatlabScripts/2_Modeling/threeOutcomes/fit_model/fit_RW_MLE.m
  78. 124 0
      MatlabScripts/2_Modeling/threeOutcomes/fit_model/helper_RW_RPE.m
  79. 123 0
      MatlabScripts/2_Modeling/threeOutcomes/fit_model/helper_RW_V.m
  80. 45 0
      MatlabScripts/2_Modeling/threeOutcomes/scripts/nRPE_intVSthree.m
  81. BIN
      MatlabScripts/3_Figures/AdditionalFigures/.DS_Store
  82. 144 0
      MatlabScripts/3_Figures/AdditionalFigures/cue/plot_LLHperTrial.m
  83. 78 0
      MatlabScripts/3_Figures/AdditionalFigures/cue/plot_cueScatter.m
  84. 158 0
      MatlabScripts/3_Figures/AdditionalFigures/cue/plot_latentVariables.m
  85. 114 0
      MatlabScripts/3_Figures/AdditionalFigures/cue/plot_linearRegression.m
  86. 53 0
      MatlabScripts/3_Figures/AdditionalFigures/cue/plot_proportionOfNeurons.m
  87. 43 0
      MatlabScripts/3_Figures/AdditionalFigures/intBlocks/RPE_symmetry.m
  88. 305 0
      MatlabScripts/3_Figures/AdditionalFigures/intBlocks/VideoAnalysis_SucMal.m
  89. 125 0
      MatlabScripts/3_Figures/AdditionalFigures/intBlocks/plot_LLHperTrial.m
  90. 296 0
      MatlabScripts/3_Figures/AdditionalFigures/intBlocks/plot_latentVariables.m
  91. 217 0
      MatlabScripts/3_Figures/AdditionalFigures/intBlocks/plot_linearRegressions.m
  92. 155 0
      MatlabScripts/3_Figures/AdditionalFigures/intBlocks/plot_parameterEstimates.m
  93. 91 0
      MatlabScripts/3_Figures/AdditionalFigures/intBlocks/plot_simAdaptation.m
  94. 338 0
      MatlabScripts/3_Figures/AdditionalFigures/opto/OptoPEAnalysis.m
  95. 410 0
      MatlabScripts/3_Figures/AdditionalFigures/opto/VideoAnalysis_Arch.m
  96. 405 0
      MatlabScripts/3_Figures/AdditionalFigures/opto/VideoAnalysis_ChR2.m
  97. 396 0
      MatlabScripts/3_Figures/AdditionalFigures/opto/VideoAnalysis_ChR2Cue.m
  98. 331 0
      MatlabScripts/3_Figures/AdditionalFigures/threeOutcomes/VideoAnalysis_TH.m
  99. 187 0
      MatlabScripts/3_Figures/AdditionalFigures/threeOutcomes/plot_latentVariables.m
  100. 0 0
      MatlabScripts/3_Figures/AdditionalFigures/threeOutcomes/plot_linearRegressions.m

BIN
MatlabScripts/.DS_Store


+ 196 - 0
MatlabScripts/1_InitialAnalysis/GenerateCuedDataForModeling.m

@@ -0,0 +1,196 @@
+%run analysis?
+runanalysis=1;
+
+%which sessions go in
+%there's a logical later that highlights included neurons
+IncRats=[2 3 4 7 9 10];
+IncDays=8:20;
+
+%get parameters
+trialsback=10; %how many trials back to look
+Baseline=[-11 -1]; %Relative to cue
+RDWindow=[0.75 1.95];
+PEWindow=[-1.5 -0.5]; %relative to RD
+CueWindow=[0 0.3];
+
+%%
+
+%reset
+NN=0;
+EvMeanz=0;
+
+if runanalysis==1  
+    load ('RAWCued.mat');
+    RAW=RAWCued;
+    CD.Ninfo={};
+    for i=1:length(RAW) %loops through sessions 
+        if any(IncRats==RAW(i).Rat) && any(IncDays==RAW(i).Day) %only look at included sessions
+            
+            %store info about each neuron's channel
+            CD.Ninfo=cat(1,CD.Ninfo,RAW(i).Ninfo);
+            
+            %events
+            Cues=strmatch('Cues',RAW(i).Einfo(:,2),'exact');
+            Cue1=strmatch('Cue1',RAW(i).Einfo(:,2),'exact');
+            Cue2=strmatch('Cue2',RAW(i).Einfo(:,2),'exact');
+            Cue3=strmatch('Cue3',RAW(i).Einfo(:,2),'exact');
+            RDC=strmatch('RDC',RAW(i).Einfo(:,2),'exact');
+            RDA=strmatch('RDA',RAW(i).Einfo(:,2),'exact');
+            R1=strmatch('R1withanylick',RAW(i).Einfo(:,2),'exact');
+            R2=strmatch('R2withanylick',RAW(i).Einfo(:,2),'exact');
+
+            %% linear model for impact of previous rewards
+            %reset
+            Y=[];
+            AllTrials=[];
+            PrCue=[];
+            
+
+            %set up the matrix with outcome identity for each session
+            rewards1=cat(2,RAW(i).Erast{R1,1}(:,1),ones(length(RAW(i).Erast{R1,1}(:,1)),1));
+            rewards2=cat(2,RAW(i).Erast{R2,1}(:,1),zeros(length(RAW(i).Erast{R2,1}(:,1)),1));
+            rewards=cat(1,rewards1,rewards2);
+            [rewards(:,1),ind]=sort(rewards(:,1));
+            rewards(:,2)=rewards(ind,2);
+            
+            %put both conditions together
+            RDCkey=cat(2,RAW(i).Erast{RDC,1}(:,1),ones(length(RAW(i).Erast{RDC,1}(:,1)),1)); %1 if predictive trial
+            RDAkey=cat(2,RAW(i).Erast{RDA,1}(:,1),zeros(length(RAW(i).Erast{RDA,1}(:,1)),1)); %0 if not
+            RDall=cat(1,RDCkey,RDAkey);
+            [RDall(:,1),ind]=sort(RDall(:,1));
+            RDall(:,2)=RDall(ind,2);
+            
+            AllTrials(:,1)=rewards(:,2);
+            AllTrials(:,2)=0;
+            
+            
+            X=NaN(length(RDall),trialsback+1);
+            %create results key
+            for k=1:length(RDall(:,1))
+                time=RDall(k,1);
+                entry=find(round(rewards(:,1))==round(time));
+                for m=1:trialsback+1
+                    if entry+1-m>0
+                        X(k,m)=rewards(entry+1-m,2);
+                    end
+                end
+                
+                %add a 1 to next column if predicted sucrose, column after
+                %that if predicted maltodextrin
+                if RDall(k,2)==1
+                    if X(k,1)==1
+                        X(k,trialsback+2)=1;
+                        X(k,trialsback+3)=0;
+                        PrCue(k,1)=1;
+                        PrCue(k,2)=0;
+                    else
+                        X(k,trialsback+2)=0;
+                        X(k,trialsback+3)=1;
+                        PrCue(k,1)=0;
+                        PrCue(k,2)=1;
+                    end
+                else
+                    X(k,trialsback+2:trialsback+3)=0;
+                    PrCue(k,1:2)=0;
+                end
+                      
+                AllTrials(entry,2)=1;
+                
+            end
+            
+            %get cue for each trial
+            cue1s=RAW(i).Erast{Cue1,1}(:,1);
+            cue2s=RAW(i).Erast{Cue2,1}(:,1);
+            cue3s=RAW(i).Erast{Cue3,1}(:,1);
+            
+            PrCueAllTrials=zeros(length(rewards),3);
+            for j=1:length(rewards)
+                mostrecentcue(1)=max([cue1s(cue1s<rewards(j,1))' 0]);
+                mostrecentcue(2)=max([cue2s(cue2s<rewards(j,1))' 0]);
+                mostrecentcue(3)=max([cue3s(cue3s<rewards(j,1))' 0]);
+                [~,mostrecent]=max(mostrecentcue);
+                PrCueAllTrials(j,mostrecent)=1;
+            end
+            
+            
+             for j= 1:length(RAW(i).Nrast) %Number of neurons within sessions
+ 
+                 NN=NN+1;
+                basespk=0;
+                
+                %get mean baseline firing for all trials
+                [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Cues},Baseline,{2});% makes trial by trial rasters for baseline
+                for y= 1:B1n
+                    basespk(1,y)=sum(Bcell1{1,y}>Baseline(1));
+                end
+
+                Bhz=basespk/(Baseline(1,2)-Baseline(1,1));
+                Bmean=nanmean(Bhz);
+                Bstd=nanstd(Bhz);
+
+                %get trial by trial firing rate for all reward trials
+                rewspk=[];
+                [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RDall(:,1),RDWindow,{2});% makes trial by trial rasters for event
+                for y= 1:EVn
+                    rewspk(y,1)=sum(EVcell{1,y}>RDWindow(1));
+                end       
+                Y=(rewspk(1:end,1)/(RDWindow(1,2)-RDWindow(1,1)));%-Bmean)/Bstd; %normalize the activity to baseline
+                
+                
+                CD.RDHz{NN,1}=Y;
+                
+                %get trial by trial firing rate for all port entries
+                rewspk=[];
+                [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RDall(:,1),PEWindow,{2});% makes trial by trial rasters for event
+                for y= 1:EVn
+                    rewspk(y,1)=sum(EVcell{1,y}>PEWindow(1));
+                end       
+                Y=(rewspk(1:end,1)/(PEWindow(1,2)-PEWindow(1,1)));%-Bmean)/Bstd; %normalize the activity to baseline
+                
+                CD.PEHz{NN,1}=Y;
+              
+                
+                %get trial by trial firing rate for all cues
+                CueList=[];
+                rewspk=[];
+                
+                for k=1:length(RDall(:,1))
+                    CueList(k,1)=RAW(i).Erast{Cues}(max(find(RAW(i).Erast{Cues}<RDall(k,1))),1);
+                end
+                
+                [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),CueList,CueWindow,{2});% makes trial by trial rasters for event
+                for y= 1:EVn
+                    rewspk(y,1)=sum(EVcell{1,y}>CueWindow(1));
+                end       
+                Y=(rewspk(1:end,1)/(CueWindow(1,2)-CueWindow(1,1)));%-Bmean)/Bstd; %subtract baseline to keep it linear
+                CD.CueHz{NN,1}=Y;
+                
+                
+                
+                %save the predictors
+                CD.Predictors{NN,1}=X;
+                CD.AllTrials{NN,1}=AllTrials;
+                CD.PredCue{NN,1}=PrCue;
+                CD.PredCueAllTrials{NN,1}=PrCueAllTrials;
+                
+                CD.Day(NN,1)=RAW(i).Day;
+                CD.Rat(NN,1)=RAW(i).Rat;
+                
+                
+
+
+                fprintf('Neuron # %d\n',NN);
+            end
+        end
+
+    end
+end
+
+%% Other data
+
+%which neurons are plotted
+DOI = 11:20;
+ROI = [3 4 9 10];
+CD.IncludedNeurons=ChooseNs(CD,DOI,ROI);
+
+save('ModData_Cued.mat','CD');

+ 127 - 0
MatlabScripts/1_InitialAnalysis/GenerateTHDataForModeling.m

@@ -0,0 +1,127 @@
+load ('RAWTH.mat');
+RAW=RAWTH;
+runanalysis=1;
+
+%get parameters
+trialsback=10; %how many trials back to look
+Baseline=[-11 -1]; %For normalizing activity
+RDWindow=[0.75 1.95];
+PEWindow=[-1.5 -0.5]; %relative to RD
+CueWindow=[0 0.75];
+
+%reset
+NN=0;
+
+
+if runanalysis==1
+    for i=1:length(RAW) %loops through sessions
+        %events
+        RD=strmatch('RD',RAW(i).Einfo(:,2),'exact');
+        R1=strmatch('R1withanylick',RAW(i).Einfo(:,2),'exact');
+        R2=strmatch('R2withanylick',RAW(i).Einfo(:,2),'exact');
+        R3=strmatch('R3withanylick',RAW(i).Einfo(:,2),'exact');
+        Cue=strmatch('Cue',RAW(i).Einfo(:,2),'exact');
+        
+        %% linear model for impact of previous rewards
+        %reset
+        X=NaN(length(RAW(i).Erast{RD,1}(:,1)),trialsback+1);
+        Y=[];
+        AllTrials=[];
+        
+        %set up the matrix with outcome identity for each session
+        rewards1=cat(2,RAW(i).Erast{R1,1}(:,1),ones(length(RAW(i).Erast{R1,1}(:,1)),1));
+        rewards2=cat(2,RAW(i).Erast{R2,1}(:,1),zeros(length(RAW(i).Erast{R2,1}(:,1)),1));
+        rewards3=cat(2,RAW(i).Erast{R3,1}(:,1),2*ones(length(RAW(i).Erast{R3,1}(:,1)),1));
+        rewards=cat(1,rewards1,rewards2,rewards3);
+        [rewards(:,1),ind]=sort(rewards(:,1));
+        rewards(:,2)=rewards(ind,2);
+        
+        AllTrials(:,1)=rewards(:,2);
+        AllTrials(:,2)=0;
+        
+        for k=1:length(RAW(i).Erast{RD,1}(:,1))
+            time=RAW(i).Erast{RD,1}(k,1);
+            entry=find(round(rewards(:,1))==round(time));
+            for m=1:trialsback+1
+                if entry+1-m>0
+                    X(k,m)=rewards(entry+1-m,2);
+                end
+            end
+            
+            AllTrials(entry,2)=1;
+            
+        end
+        
+        
+        
+        for j= 1:length(RAW(i).Nrast) %Number of neurons within sessions
+            
+            NN=NN+1;
+            rewspk=0;
+            basespk=0;
+            
+            %get mean baseline firing for all reward trials
+            [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Cue},Baseline,{2});% makes trial by trial rasters for baseline
+            for y= 1:B1n
+                basespk(1,y)=sum(Bcell1{1,y}>Baseline(1));
+            end
+            
+            Bhz=basespk/(Baseline(1,2)-Baseline(1,1));
+            Bmean=nanmean(Bhz);
+            Bstd=nanstd(Bhz);
+            
+            %get trial by trial firing rate for all reward trials
+            Window=RDWindow;
+            [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
+            for y= 1:EVn
+                rewspk(y,1)=sum(EVcell{1,y}>Window(1));
+            end
+            Y=(rewspk(1:end,1)/(Window(1,2)-Window(1,1)));%-Bmean)/Bstd; %subtract baseline firing
+            TH.RDHz{NN,1}=Y;
+            
+            %get trial by trial firing rate for all PE trials based on fixed window
+            rewspk=0;
+            Window=PEWindow;
+            [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
+            for y= 1:EVn
+                rewspk(y,1)=sum(EVcell{1,y}>Window(1));
+            end
+            Y=(rewspk(1:end,1)/(Window(1,2)-Window(1,1)));%-Bmean)/Bstd; %z-score
+            TH.PEHz{NN,1}=Y;
+            
+            %get trial by trial firing rate for all cue trials based on fixed window
+            CueList=[];
+            for k=1:length(RAW(i).Erast{RD})
+                CueList(k,1)=RAW(i).Erast{Cue}(max(find(RAW(i).Erast{Cue}<RAW(i).Erast{RD}(k,1))),1);
+            end
+            
+            Window=CueWindow;
+            rewspk=0;
+            [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),CueList,CueWindow,{2});% makes trial by trial rasters for event
+            for y= 1:EVn
+                rewspk(y,1)=sum(EVcell{1,y}>CueWindow(1));
+            end
+            Y=(rewspk(1:end,1)/(CueWindow(1,2)-CueWindow(1,1)));%-Bmean)/Bstd; %subtract baseline to keep it linear
+            TH.CueHz{NN,1}=Y;
+            
+            %All Cues
+            AllCues = RAW(i).Erast{Cue};
+            rewspk=0;
+            [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),AllCues,CueWindow,{2});% makes trial by trial rasters for event
+            for y= 1:EVn
+                rewspk(y,1)=sum(EVcell{1,y}>CueWindow(1));
+            end
+            Y=(rewspk(1:end,1)/(CueWindow(1,2)-CueWindow(1,1)));%-Bmean)/Bstd; %subtract baseline to keep it linear
+            TH.CueHzAll{NN,1}=Y;
+            
+            
+            TH.Predictors{NN,1}=X;
+            TH.AllTrials{NN,1}=AllTrials;
+            
+            fprintf('Neuron # %d\n',NN);
+        end
+        
+    end
+end
+
+save('ModData_TH.mat','TH');

+ 161 - 0
MatlabScripts/1_InitialAnalysis/GenerateintBlocksDataForModeling.m

@@ -0,0 +1,161 @@
+load ('RAWintBlocks.mat');
+RAW=RAWblocks;
+runanalysis=1;
+
+%get parameters
+trialsback=10; %how many trials back to look
+Baseline=[-11 -1]; %For normalizing activity
+RDWindow=[0.75 1.95];
+PEWindow=[-1.5 -0.5]; %relative to RD
+CueWindow=[0 0.75];
+
+
+%reset
+NN=0;
+EvMeanz=0;
+
+Ninfo=[];
+Nneurons=0;
+%Finds the total number of neurons in 2R and marks them by region/session
+for i=1:length(RAW)
+    Ninfo=cat(1,Ninfo,RAW(i).Ninfo);
+    Nneurons=Nneurons+size(RAW(i).Nrast,1);
+end
+for i=1:Nneurons
+    Session=string(Ninfo(i,1));
+    Name=char(Session);
+    Region(i,1)=cellstr(Name(1:2)); %an array with each neurons region
+    Rat(i,1)=cellstr(Name(1:3)); %an array with each neuron's rat
+    Blocks(i,1)=cellstr(Name(4));
+    Blocks12(i,1)=cellstr(Name(5));
+end
+CS.Blocks=strcmp('B',Blocks); %neurons from blocks sessions are marked 1, int is 0
+CS.Blocks12=zeros(length(Blocks12),1); %start with all 0s
+CS.Blocks12(strcmp('1',Blocks12))=1; %neurons from sessions starting with sucrose are 1
+CS.Blocks12(strcmp('2',Blocks12))=2; %neurons from sessions starting with malt are 2
+
+
+if runanalysis==1  
+    for i=1:length(RAW) %loops through sessions
+        
+        %ADD IN STUFF
+        
+        %events
+        EV1=strmatch('RD1P2',RAW(i).Einfo(:,2),'exact');
+        EV2=strmatch('RD1P1',RAW(i).Einfo(:,2),'exact');
+        EV3=strmatch('RD2P2',RAW(i).Einfo(:,2),'exact');
+        EV4=strmatch('RD2P1',RAW(i).Einfo(:,2),'exact');
+        RD=strmatch('RD',RAW(i).Einfo(:,2),'exact');
+        R1=strmatch('R1withanylick',RAW(i).Einfo(:,2),'exact');
+        R2=strmatch('R2withanylick',RAW(i).Einfo(:,2),'exact');
+        R3=strmatch('R3withanylick',RAW(i).Einfo(:,2),'exact');
+        Cue=strmatch('Cue',RAW(i).Einfo(:,2),'exact');
+
+        %% linear model for impact of previous rewards
+        %reset
+        X=NaN(length(RAW(i).Erast{RD,1}(:,1)),trialsback+1);
+        Y=[];
+        AllTrials=[];
+
+        %set up the matrix with outcome identity for each session
+        rewards1=cat(2,RAW(i).Erast{R1,1}(:,1),ones(length(RAW(i).Erast{R1,1}(:,1)),1));
+        rewards2=cat(2,RAW(i).Erast{R2,1}(:,1),zeros(length(RAW(i).Erast{R2,1}(:,1)),1));
+        rewards=cat(1,rewards1,rewards2);
+        [rewards(:,1),ind]=sort(rewards(:,1));
+        rewards(:,2)=rewards(ind,2);
+
+        AllTrials(:,1)=rewards(:,2);
+        AllTrials(:,2)=0;
+
+        for k=1:length(RAW(i).Erast{RD,1}(:,1))
+            time=RAW(i).Erast{RD,1}(k,1);
+            entry=find(round(rewards(:,1))==round(time));
+            for m=1:trialsback+1
+                if entry+1-m>0
+                    X(k,m)=rewards(entry+1-m,2);
+                end
+            end
+
+            AllTrials(entry,2)=1;
+
+        end
+
+
+
+        for j= 1:length(RAW(i).Nrast) %Number of neurons within sessions
+
+            NN=NN+1;
+            rewspk=0;
+            basespk=0;
+
+            %get mean baseline firing for all reward trials
+            [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Cue},Baseline,{2});% makes trial by trial rasters for baseline
+            for y= 1:B1n
+                basespk(1,y)=sum(Bcell1{1,y}>Baseline(1));
+            end
+
+            Bhz=basespk/(Baseline(1,2)-Baseline(1,1));
+            Bmean=nanmean(Bhz);
+            Bstd=nanstd(Bhz);
+
+            %get trial by trial firing rate for all reward trials
+            Window=RDWindow;
+            [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
+            for y= 1:EVn
+                rewspk(y,1)=sum(EVcell{1,y}>Window(1));
+            end       
+            Y=(rewspk(1:end,1)/(Window(1,2)-Window(1,1)));%-Bmean)/Bstd; %subtract baseline firing
+            CS.RDHz{NN,1}=Y;
+
+            %get trial by trial firing rate for all PE trials based on fixed window
+            rewspk=0;
+            Window=PEWindow;
+            [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
+            for y= 1:EVn
+                rewspk(y,1)=sum(EVcell{1,y}>Window(1));
+            end       
+            Y=(rewspk(1:end,1)/(Window(1,2)-Window(1,1)));%-Bmean)/Bstd; %z-score
+            CS.PEHz{NN,1}=Y;               
+
+            %get trial by trial firing rate for all cue trials based on fixed window
+            CueList=[];
+            for k=1:length(RAW(i).Erast{RD})
+                CueList(k,1)=RAW(i).Erast{Cue}(max(find(RAW(i).Erast{Cue}<RAW(i).Erast{RD}(k,1))),1);
+            end
+
+            Window=CueWindow;
+            rewspk=0;
+            [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),CueList,CueWindow,{2});% makes trial by trial rasters for event
+            for y= 1:EVn
+                rewspk(y,1)=sum(EVcell{1,y}>CueWindow(1));
+            end       
+            Y=(rewspk(1:end,1)/(CueWindow(1,2)-CueWindow(1,1)));%-Bmean)/Bstd; %subtract baseline to keep it linear
+            CS.CueHz{NN,1}=Y;
+            
+            
+            %All Cues
+            AllCues = RAW(i).Erast{Cue};
+            rewspk=0;
+            [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),AllCues,CueWindow,{2});% makes trial by trial rasters for event
+            for y= 1:EVn
+                rewspk(y,1)=sum(EVcell{1,y}>CueWindow(1));
+            end       
+            Y=(rewspk(1:end,1)/(CueWindow(1,2)-CueWindow(1,1)));%-Bmean)/Bstd; %subtract baseline to keep it linear
+            CS.CueHzAll{NN,1}=Y;
+
+            CS.Predictors{NN,1}=X;
+            CS.AllTrials{NN,1}=AllTrials;
+
+            fprintf('Neuron # %d\n',NN);
+        end
+        
+
+    end
+end
+
+%% Other data
+
+CS.Rat=Rat;
+CS.Region=Region;
+
+save('ModData_intBlocks.mat','CS');

+ 174 - 0
MatlabScripts/1_InitialAnalysis/PSTHsCued.m

@@ -0,0 +1,174 @@
+%PSTHs and some simple analysis
+
+clear all; clc;
+global Dura Baseline Tm Tbase BSIZE Tbin
+tic
+if ~exist('RAWCued'), load ('RAWCued.mat'); end
+RAW=RAWCued;
+
+%Main settings
+SAVE_FLAG=1;
+BSIZE=0.01; %Do not change
+Dura=[-4 10]; Tm=Dura(1):BSIZE:Dura(2);
+%Baseline=[-22 0]; Tbase=Baseline(1):BSIZE:Baseline(2); %now defined line 98
+Tbin=-0.5:0.005:0.5; %window used to determine the optimal binsize
+Tbase=-11:BSIZE:-1; %for fixed baseline for z-score
+PStat=0.05; %for comparing pre versus post windows, or event A versus event B
+MinNumTrials=5;
+R=[];R.Ninfo={};NN=0;Nneurons=0;
+
+%for fixed bin size
+BinSize=0.01; %in seconds
+
+%which sessions included
+IncRats=[3 4 9 10];
+IncDays=11:20;
+
+%Smoothing PSTHs
+%smoothing filter
+smoothbins=25; %number of previous bins used to smooth
+halfnormal=makedist('HalfNormal','mu',0,'sigma',6.6); %std=3.98
+filterweights=pdf(halfnormal,[0:smoothbins]);
+
+%old smoothing
+% Smoothing=1; %0 for raw and 1 for smoothing
+% SmoothTYPE='lowess';
+% SmoothSPAN=7;
+% if Smoothing~=1, SmoothTYPE='NoSmoothing';SmoothSPAN=NaN; end
+
+% List of events to analyze and analysis windows EXTRACTED from excel file
+spreadsheet=importdata('Cued.xlsx');
+Erefnames=spreadsheet.textdata(3:11,1);
+prewin =  spreadsheet.data(1:9,1:2);
+postwin = spreadsheet.data(1:9,3:4);
+
+%%
+%Finds the total number of neurons accross all sessions
+for i=1:length(RAW)
+    if any(IncRats==RAW(i).Rat) && any(IncDays==RAW(i).Day) %only look at included sessions
+        R.Ninfo=cat(1,R.Ninfo,RAW(i).Ninfo);
+        Nneurons=Nneurons+size(RAW(i).Nrast,1);
+    end
+end
+
+R.Ninfo=cat(2, R.Ninfo, mat2cell([1:Nneurons]',ones(1,Nneurons),1));
+R.Erefnames= Erefnames;
+
+
+% preallocating
+R.Param.Tm=Tm;
+R.Param.Tbin=Tbin;
+R.Param.Dura=Dura;
+R.Param.Baseline=Baseline;
+R.Param.PStat=PStat;
+R.Param.MinNumTrials=MinNumTrials;
+R.Param.path=path;
+R.Param.prewin=prewin;
+R.Param.postwin=postwin;
+R.Param.SmoothTYPE='HalfNormal';
+R.Param.SmoothSPAN=std(halfnormal);
+
+for k=1:length(Erefnames)
+    R.Ev(k).PSTHraw(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
+    R.Ev(k).PSTHz(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
+    R.Ev(k).Meanraw(1:Nneurons,1)=NaN;
+    R.Ev(k).Meanz(1:Nneurons,1)=NaN;
+    R.Ev(k).BW(1:Nneurons,1)=NaN;
+    R.Ev(k).signrank(1:Nneurons,1)=NaN;
+    R.Ev(k).RespDir(1:Nneurons,1)=NaN;
+    R.Ev(k).NumberTrials(1:Nneurons,1)=NaN;
+end
+
+%% runs the main routine
+
+figure;
+
+for i=1:length(RAW) %loops through sessions
+    if any(IncRats==RAW(i).Rat) && any(IncDays==RAW(i).Day) %only look at included sessions
+        for j= 1:size(RAW(i).Nrast,1) %Number of neurons per session
+            NN=NN+1; %neuron counter
+            R.Day(NN,1)=RAW(i).Day;
+            R.Rat(NN,1)=RAW(i).Rat;
+
+            %use the same baseline for z-scoring every event
+            %baseline period is before every cue in the whole session
+            bltrlhz=[];
+            EvInd=strcmp('Cues',RAW(i).Einfo(:,2));
+            [PSRbl,N0]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},[-11 -1],{2});% makes collapsed rasters for baseline for use in normalizing
+            for l=1:length(PSRbl)
+                bltrlhz(l,1)=sum((PSRbl{l}<-1 & PSRbl{l}>-11)/10);
+            end
+            bmean=nanmean(bltrlhz(2:2:end)); %2:2:end is to match the number of trials in int/blocks sessions
+            bstd=nanstd(bltrlhz(2:2:end));
+
+            for k=1:length(Erefnames) %loops thorough the events
+                EvInd=strcmp(Erefnames(k),RAW(i).Einfo(:,2)); %find the event id number from RAW
+
+                if sum(EvInd)==0
+                    fprintf('HOWDY, CANT FIND EVENTS FOR ''%s''\n',Erefnames{k});
+                end
+
+                R.Ev(k).NumberTrials(NN,1)=length(RAW(i).Erast{EvInd});
+                if  ~isempty(EvInd) && R.Ev(k).NumberTrials(NN,1)>=MinNumTrials %avoid analyzing sessions where that do not have enough trials
+                    [PSR1,N1]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{1});% makes collpased rasters. PSR1 is a cell(neurons)
+                    [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
+                    [PSR3,~]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},prewin(k,:),{2});% makes trial by trial rasters for baseline for use in response detection
+                    if ~isempty(PSR1{1}) %to avoid errors, added on 12/28 2011
+                        %optimal bin size
+                        %[PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,1});%-----DP used here
+                        %fixed bin size
+                        [PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,0,BinSize});%fix bin used here
+
+                        %------------- Fills the R.Ev(k) fields --------------
+                        R.Ev(k).BW(NN,1)=BW1;
+
+                        %new smoothing
+                        for l=1:length(Tm)
+                           PTH1smooth(1,l)=sum(PTH1(1,l-min([l-1 smoothbins]):l).*fliplr(filterweights(1:min([l smoothbins+1]))))/sum(filterweights(1:min([l smoothbins+1])));                  
+                        end
+                        R.Ev(k).PSTHraw(NN,:)=PTH1smooth;
+
+                        %normalize already smoothed activity
+                        for l=1:length(PTH1smooth)
+                            R.Ev(k).PSTHz(NN,l)=(PTH1smooth(l)-bmean)/bstd;
+                        end
+
+                        %------------------ firing (in Hz) per trial in pre/post windows ------------------
+                        %used to make the between events comparisons and Response detection in a single window----
+                        ev(k).pre=NaN(size(RAW(i).Erast{EvInd},1),1);
+                        ev(k).post=NaN(size(RAW(i).Erast{EvInd},1),1);
+                        for m=1:size(RAW(i).Erast{EvInd},1) %loops through trials
+                            ev(k).pre(m)=sum(PSR3{m}<prewin(k,2) & PSR3{m}>prewin(k,1))/(prewin(k,2)-prewin(k,1)); %CHANGED FROM PSR2 to PSR0 here 10/24/17
+                            ev(k).post(m)=sum(PSR2{m}<postwin(k,2) & PSR2{m}>postwin(k,1))/(postwin(k,2)-postwin(k,1));
+                        end
+
+                        R.Ev(k).Meanraw(NN,1)=nanmean(ev(k).post);
+                        R.Ev(k).Meanz(NN,1)=(nanmean(ev(k).post)-bmean)/bstd;
+
+                        %---------------------------Response detection w/ SignRank on pre/post windows---------------------------
+
+
+                        R.Ev(k).signrank(NN,1)=signrank(ev(k).pre, ev(k).post); %Signrank used here because it is a dependant sample test
+                        if R.Ev(k).signrank(NN,1)<PStat
+                            R.Ev(k).RespDir(NN,1)=sign(mean(ev(k).post)-mean(ev(k).pre));
+                        else R.Ev(k).RespDir(NN,1)=0;
+                        end
+
+
+                    end %if ~isempty(PSR0{1}) || ~isempty(PSR1{1})
+                end %if EvInd=0 OR n(trials) < MinNumTrials fills with NaN
+            end %Events
+
+            fprintf('Neuron ID # %d\n',NN);
+        end %neurons: FOR j= 1:size(RAW(i).Nrast,1)
+    end
+end %sessions: FOR i=1:length(RAW)
+
+R_cued=R;
+
+if SAVE_FLAG
+    save('R_cued_all.mat','R_cued')
+end
+
+toc
+

+ 187 - 0
MatlabScripts/1_InitialAnalysis/PSTHsSucMal.m

@@ -0,0 +1,187 @@
+%PSTHs and some initial analysis
+%this is for sessions with two rewards (2R) -- suc and mal
+%both interspersed and blocked sessions are here
+
+clear all; clc;
+global Dura Baseline Tm Tbase BSIZE Tbin
+tic
+
+load ('RAWintBlocks.mat');
+RAW=RAWblocks;
+
+%Main settings
+SAVE_FLAG=1;
+BSIZE=0.01; %Do not change
+Dura=[-4 10]; Tm=Dura(1):BSIZE:Dura(2);
+%Baseline=[-22 0]; Tbase=Baseline(1):BSIZE:Baseline(2); %now defined line 98
+Tbin=-0.5:0.005:0.5; %window used to determine the optimal binsize
+Tbase=-11:BSIZE:-1; %for fixed baseline for z-score
+BaseZscore=[-11 -1]; %bin for calculating z-score, new way
+PStat=0.05; %for comparing pre versus post windows, or event A versus event B
+MinNumTrials=5; %how many trials of event there need to be to conduct analysis
+BinSize=0.01; %in seconds
+
+%Smoothing PSTHs
+%smoothing filter
+smoothbins=25; %number of previous bins used to smooth
+halfnormal=makedist('HalfNormal','mu',0,'sigma',6.6); %std=3.98
+filterweights=pdf(halfnormal,[0:smoothbins]);
+
+%start fresh
+R=[];R.Ninfo={};NN=0;Nneurons=0;
+
+% List of events to analyze and analysis windows EXTRACTED from excel file
+spreadsheet=importdata('SucMal.xlsx');
+Erefnames=spreadsheet.textdata(3:14,1);
+prewin =  spreadsheet.data(1:12,1:2);
+postwin = spreadsheet.data(1:12,3:4);
+
+%saves event names for reference later
+R.Erefnames=Erefnames;
+
+%Finds the total number of neurons in 2R and marks them by region/session
+for i=1:length(RAW)
+    R.Ninfo=cat(1,R.Ninfo,RAW(i).Ninfo);
+    Nneurons=Nneurons+size(RAW(i).Nrast,1);
+end
+for i=1:Nneurons
+    Session=string(R.Ninfo(i,1));
+    Name=char(Session);
+    R.Region(i,1)=cellstr(Name(1:2)); %an array with each neurons region
+    R.Rat(i,1)=cellstr(Name(1:3)); %an array with each neuron's rat
+    Blocks(i,1)=cellstr(Name(4));
+    Blocks12(i,1)=cellstr(Name(5));
+end
+R.Blocks=strcmp('B',Blocks); %neurons from blocks sessions are marked 1, int is 0
+R.Blocks12=zeros(length(Blocks12),1); %start with all 0s
+R.Blocks12(strcmp('1',Blocks12))=1; %neurons from sessions starting with sucrose are 1
+R.Blocks12(strcmp('2',Blocks12))=2; %neurons from sessions starting with malt are 2
+
+% preallocating
+R.Param.Tm=Tm;
+R.Param.Tbin=Tbin;
+R.Param.Dura=Dura;
+R.Param.Baseline=Baseline;
+R.Param.PStat=PStat;
+R.Param.MinNumTrials=MinNumTrials;
+R.Param.prewin=prewin;
+R.Param.postwin=postwin;
+R.Param.SmoothTYPE='HalfNormal';
+R.Param.SmoothSPAN=std(halfnormal);
+
+for k=1:length(Erefnames)
+    R.Ev(k).PSTHraw(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
+    R.Ev(k).PSTHz(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
+    R.Ev(k).Meanraw(1:Nneurons,1)=NaN;
+    R.Ev(k).Meanz(1:Nneurons,1)=NaN;
+    R.Ev(k).BW(1:Nneurons,1)=NaN;
+    R.Ev(k).signrank(1:Nneurons,1)=NaN;
+    R.Ev(k).RespDir(1:Nneurons,1)=NaN;
+    R.Ev(k).NumberTrials(1:Nneurons,1)=NaN;
+end
+
+% runs the main routine
+
+for i=1:length(RAW) %loops through sessions
+    for j= 1:size(RAW(i).Nrast,1) %Number of neurons per session
+        NN=NN+1; %neuron counter
+        
+        %use the same baseline for z-scoring every event
+        %baseline period is before every cue in the whole session
+        bltrlhz=[];
+        EvInd=strcmp('Cue',RAW(i).Einfo(:,2));
+        [PSRbl,N0]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},BaseZscore,{2});% makes collapsed rasters for baseline for use in normalizing
+        for l=1:length(PSRbl)
+            bltrlhz(l,1)=sum((PSRbl{l}<-1 & PSRbl{l}>-11)/10);
+        end
+        bmean=nanmean(bltrlhz);
+        bstd=nanstd(bltrlhz);   
+        
+        for k=1:length(Erefnames) %loops thorough the events
+            EvInd=strcmp(Erefnames(k),RAW(i).Einfo(:,2)); %find the event id number from RAW
+
+            if sum(EvInd)==0
+                fprintf('HOWDY, CANT FIND EVENTS FOR ''%s''\n',Erefnames{k});
+            end
+
+            R.Ev(k).NumberTrials(NN,1)=length(RAW(i).Erast{EvInd});
+            if  ~isempty(EvInd) && R.Ev(k).NumberTrials(NN,1)>=MinNumTrials %avoid analyzing sessions where that do not have enough trials
+                [PSR1,N1]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{1});% makes collpased rasters. PSR1 is a cell(neurons)
+                [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
+                [PSR3,~]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},prewin(k,:),{2});% makes trial by trial rasters for baseline for use in response detection
+                if ~isempty(PSR1{1}) %to avoid errors, added on 12/28 2011
+                    %Optimal bin size
+                    %[PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,1});%-----DP used here
+                    %Fixed bin size
+                    [PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,0,BinSize});%-----Fixed bin used here                   
+
+                    %------------- Fills the R.Ev(k) fields --------------
+                    R.Ev(k).BW(NN,1)=BW1;
+                    PTH1smooth=[];
+                    
+                    %new smoothing
+                    for l=1:length(Tm)
+                       PTH1smooth(1,l)=sum(PTH1(1,l-min([l-1 smoothbins]):l).*fliplr(filterweights(1:min([l smoothbins+1]))))/sum(filterweights(1:min([l smoothbins+1])));                  
+                    end
+                    R.Ev(k).PSTHraw(NN,:)=PTH1smooth;
+                    
+                    %normalize already smoothed activity
+                    for l=1:length(PTH1smooth)
+                        R.Ev(k).PSTHz(NN,l)=(PTH1smooth(l)-bmean)/bstd;
+                    end
+
+                    %------------------ firing (in Hz) per trial in pre/post windows ------------------
+                    %used to make the between events comparisons and Response detection in a single window----
+                    ev(k).pre=NaN(size(RAW(i).Erast{EvInd},1),1);
+                    ev(k).post=NaN(size(RAW(i).Erast{EvInd},1),1);
+                    for m=1:size(RAW(i).Erast{EvInd},1) %loops through trials
+                        ev(k).pre(m)=sum(PSR3{m}<prewin(k,2) & PSR3{m}>prewin(k,1))/(prewin(k,2)-prewin(k,1)); %CHANGED FROM PSR2 to PSR0 here 10/24/17
+                        ev(k).post(m)=sum(PSR2{m}<postwin(k,2) & PSR2{m}>postwin(k,1))/(postwin(k,2)-postwin(k,1));
+                    end
+
+                    R.Ev(k).Meanraw(NN,1)=nanmean(ev(k).post);
+                    R.Ev(k).Meanz(NN,1)=(nanmean(ev(k).post)-bmean)/bstd;
+                    
+                    %-------------------- signrank on event and direction----
+                    [R.Ev(k).signrank(NN,1),~]=signrank(ev(k).pre, ev(k).post); %Signrank used here because it is a dependant sample test
+                    if R.Ev(k).signrank(NN,1)<PStat
+                        R.Ev(k).RespDir(NN,1)=sign(mean(ev(k).post)-mean(ev(k).pre));
+                    else R.Ev(k).RespDir(NN,1)=0;
+                    end
+
+                end %if ~isempty(PSR0{1}) || ~isempty(PSR1{1})
+            end %if EvInd=0 OR n(trials) < MinNumTrials fills with NaN
+        end %Events
+
+
+        %----------------------- Check for difference in cue responding after sucrose and maltodextrin trials -----------------------         
+        CueP1=strcmp('CueP1', Erefnames);
+        CueP2=strcmp('CueP2', Erefnames);
+        [R.CueP12Stat(NN,1),~]=ranksum(ev(CueP1).post,ev(CueP2).post); %Ranksum test used becasue it is an independent sample test
+
+        
+        fprintf('Neuron #%d\n',NN);
+    end %neurons: FOR j= 1:size(RAW(i).Nrast,1)
+end %sessions: FOR i=1:length(RAW)
+
+if SAVE_FLAG
+    R_blocks=R;
+    save('R_intBlocks.mat','R_blocks')
+end
+
+toc
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 175 - 0
MatlabScripts/1_InitialAnalysis/PSTHsTH.m

@@ -0,0 +1,175 @@
+%PSTHs and some initial analysis
+%TH stands for three outcomes (suc/mal/wat)
+
+clear all; clc;
+global Dura Baseline Tm Tbase BSIZE Tbin
+tic
+
+load('RAWTH.mat');
+RAW=RAWTH;
+
+%Main settings
+SAVE_FLAG=1;
+BSIZE=0.01; %Do not change
+Dura=[-4 10]; Tm=Dura(1):BSIZE:Dura(2);
+%Baseline=[-22 0]; Tbase=Baseline(1):BSIZE:Baseline(2); %now defined line 98
+Tbin=-0.5:0.005:0.5; %window used to determine the optimal binsize
+Tbase=-11:BSIZE:-1; %for fixed baseline for z-score
+BaseZscore=[-11 -1]; %bin for calculating z-score, new way
+PStat=0.05; %for comparing pre versus post windows, or event A versus event B
+MinNumTrials=3; %how many trials of event there need to be to conduct analysis
+BinSize=0.01; %in seconds
+
+%Smoothing PSTHs
+%smoothing filter
+smoothbins=25; %number of previous bins used to smooth
+halfnormal=makedist('HalfNormal','mu',0,'sigma',6.6); %std=3.98
+filterweights=pdf(halfnormal,[0:smoothbins]);
+
+%start fresh
+R=[];R.Ninfo={};NN=0;Nneurons=0;
+
+% List of events to analyze and analysis windows EXTRACTED from excel file
+spreadsheet=importdata('TH.xlsx');
+Erefnames=spreadsheet.textdata(3:9,1);
+prewin =  spreadsheet.data(1:7,1:2);
+postwin = spreadsheet.data(1:7,3:4);
+
+%saves event names for reference later
+R.Erefnames=Erefnames;
+
+%Finds the total number of neurons in 2R and marks them by region/session
+for i=1:length(RAW)
+    R.Ninfo=cat(1,R.Ninfo,RAW(i).Ninfo);
+    Nneurons=Nneurons+size(RAW(i).Nrast,1);
+end
+for i=1:Nneurons
+    Session=string(R.Ninfo(i,1));
+    Name=char(Session);
+    R.Rat(i,1)=str2num(Name(3)); %an array with each neuron's rat
+end
+
+% preallocating
+R.Param.Tm=Tm;
+R.Param.Tbin=Tbin;
+R.Param.Dura=Dura;
+R.Param.Baseline=Baseline;
+R.Param.PStat=PStat;
+R.Param.MinNumTrials=MinNumTrials;
+R.Param.path=path;
+R.Param.prewin=prewin;
+R.Param.postwin=postwin;
+R.Param.SmoothTYPE='HalfNormal';
+R.Param.SmoothSPAN=std(halfnormal);
+
+for k=1:length(Erefnames)
+    R.Ev(k).PSTHraw(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
+    R.Ev(k).PSTHz(1:Nneurons,1:length(Tm))=NaN(Nneurons,length(Tm));
+    R.Ev(k).Meanraw(1:Nneurons,1)=NaN;
+    R.Ev(k).Meanz(1:Nneurons,1)=NaN;
+    R.Ev(k).BW(1:Nneurons,1)=NaN;
+    R.Ev(k).signrank(1:Nneurons,1)=NaN;
+    R.Ev(k).RespDir(1:Nneurons,1)=NaN;
+    R.Ev(k).NumberTrials(1:Nneurons,1)=NaN;
+end
+
+% runs the main routine
+
+for i=1:length(RAW) %loops through sessions
+    for j= 1:size(RAW(i).Nrast,1) %Number of neurons per session
+        NN=NN+1; %neuron counter
+        
+        %use the same baseline for z-scoring every event
+        %baseline period is before every cue in the whole session
+        bltrlhz=[];
+        EvInd=strcmp('Cue',RAW(i).Einfo(:,2));
+        [PSRbl,N0]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},BaseZscore,{2});% makes collapsed rasters for baseline for use in normalizing
+        for l=1:length(PSRbl)
+            bltrlhz(l,1)=sum((PSRbl{l}<-1 & PSRbl{l}>-11)/10);
+        end
+        bmean=nanmean(bltrlhz);
+        bstd=nanstd(bltrlhz);   
+        
+        for k=1:length(Erefnames) %loops thorough the events
+            EvInd=strcmp(Erefnames(k),RAW(i).Einfo(:,2)); %find the event id number from RAW
+
+            if sum(EvInd)==0
+                fprintf('HOWDY, CANT FIND EVENTS FOR ''%s''\n',Erefnames{k});
+            end
+
+            R.Ev(k).NumberTrials(NN,1)=length(RAW(i).Erast{EvInd});
+            if  ~isempty(EvInd) && R.Ev(k).NumberTrials(NN,1)>=MinNumTrials %avoid analyzing sessions where that do not have enough trials
+                [PSR1,N1]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{1});% makes collpased rasters. PSR1 is a cell(neurons)
+                [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},Dura,{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
+                [PSR3,~]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{EvInd},prewin(k,:),{2});% makes trial by trial rasters for baseline for use in response detection
+                if ~isempty(PSR1{1}) %to avoid errors, added on 12/28 2011
+                    %Optimal bin size
+                    %[PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,1});%-----DP used here
+                    %Fixed bin size
+                    [PTH1,BW1,~]=MakePTH07(PSR1,repmat(N1, size(RAW(i).Nrast{j},1),1),{2,0,BinSize});%-----Fixed bin used here                   
+
+                    %------------- Fills the R.Ev(k) fields --------------
+                    R.Ev(k).BW(NN,1)=BW1;
+                    PTH1smooth=[];
+                    
+                    %new smoothing
+                    for l=1:length(Tm)
+                       PTH1smooth(1,l)=sum(PTH1(1,l-min([l-1 smoothbins]):l).*fliplr(filterweights(1:min([l smoothbins+1]))))/sum(filterweights(1:min([l smoothbins+1])));                  
+                    end
+                    R.Ev(k).PSTHraw(NN,:)=PTH1smooth;
+                    
+                    %normalize already smoothed activity
+                    for l=1:length(PTH1smooth)
+                        R.Ev(k).PSTHz(NN,l)=(PTH1smooth(l)-bmean)/bstd;
+                    end
+
+                    %------------------ firing (in Hz) per trial in pre/post windows ------------------
+                    %used to make the between events comparisons and Response detection in a single window----
+                    ev(k).pre=NaN(size(RAW(i).Erast{EvInd},1),1);
+                    ev(k).post=NaN(size(RAW(i).Erast{EvInd},1),1);
+                    for m=1:size(RAW(i).Erast{EvInd},1) %loops through trials
+                        ev(k).pre(m)=sum(PSR3{m}<prewin(k,2) & PSR3{m}>prewin(k,1))/(prewin(k,2)-prewin(k,1)); %CHANGED FROM PSR2 to PSR0 here 10/24/17
+                        ev(k).post(m)=sum(PSR2{m}<postwin(k,2) & PSR2{m}>postwin(k,1))/(postwin(k,2)-postwin(k,1));
+                    end
+
+                    R.Ev(k).Meanraw(NN,1)=nanmean(ev(k).post);
+                    R.Ev(k).Meanz(NN,1)=(nanmean(ev(k).post)-bmean)/bstd;
+                    
+                    %-------------------- signrank on event and direction----
+                    [R.Ev(k).signrank(NN,1),~]=signrank(ev(k).pre, ev(k).post); %Signrank used here because it is a dependant sample test
+                    if R.Ev(k).signrank(NN,1)<PStat
+                        R.Ev(k).RespDir(NN,1)=sign(mean(ev(k).post)-mean(ev(k).pre));
+                    else R.Ev(k).RespDir(NN,1)=0;
+                    end
+
+                end %if ~isempty(PSR0{1}) || ~isempty(PSR1{1})
+            end %if EvInd=0 OR n(trials) < MinNumTrials fills with NaN
+        end %Events
+
+       
+        
+        fprintf('Neuron #%d\n',NN);
+    end %neurons: FOR j= 1:size(RAW(i).Nrast,1)
+end %sessions: FOR i=1:length(RAW)
+
+if SAVE_FLAG
+    R_TH=R;
+    save('R_TH.mat','R_TH')
+end
+
+toc
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 149 - 0
MatlabScripts/1_InitialAnalysis/RewHist2R_calculator.m

@@ -0,0 +1,149 @@
+%Looking at the average firing rate for a given window in each of 4
+%current/previous reward conditions
+
+%run linear model and stats? 1 is yes, 0 is no
+runanalysis=1;
+
+%get parameters
+trialsback=10; %how many trials back to look
+Baseline=[-11 -1]; %Relative to cue
+RDWindow=[0.75 1.95];
+PEWindow=[-1.5 -0.5]; %relative to RD
+CueWindow=[0 0.75];
+
+
+
+%for looking across time
+bins=45;
+binstart=-1.5; %in seconds
+binsize=0.1; %in seconds
+
+%save variables
+RewHist2R.trialsback=trialsback;
+RewHist2R.Baseline=Baseline;
+RewHist2R.RDWindow=RDWindow;
+RewHist2R.PEWindow=PEWindow;
+RewHist2R.CueWindow=CueWindow;
+RewHist2R.bins=bins;
+RewHist2R.binstart=binstart;
+RewHist2R.binsize=binsize;
+
+%reset
+NN=0;
+EvMeanz=0;
+
+if runanalysis==1  
+    load ('RAWintBlocks.mat');
+    RAW=RAWblocks;
+    for i=1:length(RAW) %loops through sessions
+        if strcmp('VP',RAW(i).Region(1:2)) && strcmp('I',RAW(i).Blocks) %only look at VP interspersed sessions
+            %events
+            EV1=strmatch('RD1P2',RAW(i).Einfo(:,2),'exact');
+            EV2=strmatch('RD1P1',RAW(i).Einfo(:,2),'exact');
+            EV3=strmatch('RD2P2',RAW(i).Einfo(:,2),'exact');
+            EV4=strmatch('RD2P1',RAW(i).Einfo(:,2),'exact');
+            RD=strmatch('RD',RAW(i).Einfo(:,2),'exact');
+            Cue=strmatch('Cue',RAW(i).Einfo(:,2),'exact');
+            R1=strmatch('R1withanylick',RAW(i).Einfo(:,2),'exact');
+            R2=strmatch('R2withanylick',RAW(i).Einfo(:,2),'exact');
+
+            % linear model for impact of previous rewards
+            %reset
+            X=[];
+            Y=[];
+
+            %set up the matrix with outcome identity for each session
+            rewards1=cat(2,RAW(i).Erast{R1,1}(:,1),ones(length(RAW(i).Erast{R1,1}(:,1)),1));
+            rewards2=cat(2,RAW(i).Erast{R2,1}(:,1),zeros(length(RAW(i).Erast{R2,1}(:,1)),1));
+            rewards=cat(1,rewards1,rewards2);
+            [rewards(:,1),ind]=sort(rewards(:,1));
+            rewards(:,2)=rewards(ind,2);
+
+            for k=trialsback+1:length(RAW(i).Erast{RD,1}(:,1))
+                time=RAW(i).Erast{RD,1}(k,1);
+                entry=find(rewards(:,1)==time);
+                for m=1:trialsback+1
+                    X(k-trialsback,m)=rewards(entry+1-m,2);
+                end
+            end
+
+            for j= 1:length(RAW(i).Nrast) %Number of neurons within sessions
+
+                NN=NN+1;
+                basespk=0;
+
+                %get mean baseline firing for all reward trials
+                [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Cue},Baseline,{2});% makes trial by trial rasters for baseline
+                for y= 1:B1n
+                    basespk(1,y)=sum(Bcell1{1,y}>Baseline(1));
+                end
+
+                Bhz=basespk/(Baseline(1,2)-Baseline(1,1));
+                Bmean=nanmean(Bhz);
+                Bstd=nanstd(Bhz);
+                
+                for k=1:bins
+                    
+                    Window=[binstart+(k-1)*binsize binstart+k*binsize];
+                    rewspk=[];
+                    
+                    %get trial by trial firing rate for all reward trials
+                    [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
+                    for y= 1:EVn
+                        rewspk(y,1)=sum(EVcell{1,y}>Window(1));
+                    end       
+                    Y=((rewspk(trialsback+1:end,1)/(Window(1,2)-Window(1,1)))-Bmean)/Bstd; %normalize the activity to baseline
+                                      
+                    RewHist2R.BinHz{NN,k}=Y;
+
+
+                end
+                
+                %get trial by trial firing rate for all reward trials based on fixed window
+                rewspk=0;
+                Window=RDWindow;
+                [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
+                for y= 1:EVn
+                    rewspk(y,1)=sum(EVcell{1,y}>Window(1));
+                end       
+                Y=((rewspk(trialsback+1:end,1)/(Window(1,2)-Window(1,1)))-Bmean)/Bstd; %z-score
+                RewHist2R.RDHz{NN,1}=Y;
+                
+                %get trial by trial firing rate for all PE trials based on fixed window
+                rewspk=0;
+                Window=PEWindow;
+                [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
+                for y= 1:EVn
+                    rewspk(y,1)=sum(EVcell{1,y}>Window(1));
+                end       
+                Y=((rewspk(trialsback+1:end,1)/(Window(1,2)-Window(1,1)))-Bmean)/Bstd; %z-score
+                RewHist2R.PEHz{NN,1}=Y;               
+                
+                %get trial by trial firing rate for all cue trials based on fixed window
+                CueList=[];
+                for k=1:length(RAW(i).Erast{RD})
+                    CueList(k,1)=RAW(i).Erast{Cue}(max(find(RAW(i).Erast{Cue}<RAW(i).Erast{RD}(k,1))),1);
+                end
+                
+                Window=CueWindow;
+                rewspk=0;
+                [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),CueList,CueWindow,{2});% makes trial by trial rasters for event
+                for y= 1:EVn
+                    rewspk(y,1)=sum(EVcell{1,y}>CueWindow(1));
+                end       
+                Y=((rewspk(trialsback+1:end,1)/(CueWindow(1,2)-CueWindow(1,1)))-Bmean)/Bstd; %subtract baseline to keep it linear
+                RewHist2R.CueHz{NN,1}=Y;
+                
+                %save the predictors
+                RewHist2R.Predictors{NN,1}=X;
+
+
+                fprintf('Neuron # %d\n',NN);
+            end
+        end
+
+    end
+
+end
+
+save('RewHist2R.mat','RewHist2R');

+ 144 - 0
MatlabScripts/1_InitialAnalysis/RewHistCued_calculator.m

@@ -0,0 +1,144 @@
+%Looking at the average firing rate for a given window in each of 4
+%current/previous reward conditions
+
+%run linear model and stats? 1 is yes, 0 is no
+runanalysis=1;
+
+%which sessions are we looking at
+IncRats=[3 4 9 10]; %needs to match 'Cued.m'
+IncDays=11:20; %needs to match 'Cued.m'
+
+%get parameters
+trialsback=10; %how many trials back to look
+Baseline=[-11 -1]; %Relative to cue
+RDWindow=[0.75 1.95];
+PEWindow=[-1.5 -0.5]; %relative to RD
+CueWindow=[0 0.75];
+
+%reset
+NN=0;
+EvMeanz=0;
+
+if runanalysis==1 
+    load('RAWCued.mat')
+    RAW=RAWCued;
+    for i=1:length(RAW) %loops through sessions
+        if any(IncRats==RAW(i).Rat) && any(IncDays==RAW(i).Day) %only look at included sessions
+            %events
+            Cues=strmatch('Cues',RAW(i).Einfo(:,2),'exact');
+            RDC=strmatch('RDC',RAW(i).Einfo(:,2),'exact');
+            RDA=strmatch('RDA',RAW(i).Einfo(:,2),'exact');
+            R1=strmatch('R1withanylick',RAW(i).Einfo(:,2),'exact');
+            R2=strmatch('R2withanylick',RAW(i).Einfo(:,2),'exact');
+
+            %% linear model for impact of previous rewards
+            %reset
+            X=[];
+            Y=[];
+
+            %set up the matrix with outcome identity for each session
+            rewards1=cat(2,RAW(i).Erast{R1,1}(:,1),ones(length(RAW(i).Erast{R1,1}(:,1)),1));
+            rewards2=cat(2,RAW(i).Erast{R2,1}(:,1),zeros(length(RAW(i).Erast{R2,1}(:,1)),1));
+            rewards=cat(1,rewards1,rewards2);
+            [rewards(:,1),ind]=sort(rewards(:,1));
+            rewards(:,2)=rewards(ind,2);
+            
+            %put both conditions together
+            RDCkey=cat(2,RAW(i).Erast{RDC,1}(:,1),ones(length(RAW(i).Erast{RDC,1}(:,1)),1)); %1 if predictive trial
+            RDAkey=cat(2,RAW(i).Erast{RDA,1}(:,1),zeros(length(RAW(i).Erast{RDA,1}(:,1)),1)); %0 if not
+            RDall=cat(1,RDCkey,RDAkey);
+            [RDall(:,1),ind]=sort(RDall(:,1));
+            RDall(:,2)=RDall(ind,2);
+            
+            %create results key
+            for k=trialsback+1:length(RDall(:,1))
+                time=RDall(k,1);
+                entry=find(rewards(:,1)==time);
+                for m=1:trialsback+1
+                    X(k-trialsback,m)=rewards(entry+1-m,2);
+                end
+                
+                %add a 1 to next column if predicted sucrose, column after
+                %that if predicted maltodextrin
+                if RDall(k,2)==1
+                    if X(k-trialsback,1)==1
+                        X(k-trialsback,trialsback+2)=1;
+                        X(k-trialsback,trialsback+3)=0;
+                    else
+                        X(k-trialsback,trialsback+2)=0;
+                        X(k-trialsback,trialsback+3)=1;
+                    end
+                else
+                    X(k-trialsback,trialsback+2:trialsback+3)=0;
+                end
+                %X(k-trialsback,trialsback+4)=(rewards(entry,1)-rewards(entry-1,1))/60; %time since last reward, in minutes
+                        
+            end
+            
+            for j= 1:length(RAW(i).Nrast) %Number of neurons within sessions
+
+                NN=NN+1;
+                basespk=0;
+                
+                %get mean baseline firing for all trials
+                [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Cues},Baseline,{2});% makes trial by trial rasters for baseline
+                for y= 1:B1n
+                    basespk(1,y)=sum(Bcell1{1,y}>Baseline(1));
+                end
+
+                Bhz=basespk/(Baseline(1,2)-Baseline(1,1));
+                Bmean=nanmean(Bhz);
+                Bstd=nanstd(Bhz);
+
+                %get trial by trial firing rate for all reward trials
+                rewspk=[];
+                [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RDall(:,1),RDWindow,{2});% makes trial by trial rasters for event
+                for y= 1:EVn
+                    rewspk(y,1)=sum(EVcell{1,y}>RDWindow(1));
+                end       
+                Y=((rewspk(trialsback+1:end,1)/(RDWindow(1,2)-RDWindow(1,1)))-Bmean)/Bstd; %normalize the activity to baseline
+                
+                
+                RewHistCued.RDHz{NN,1}=Y;
+                
+                %get trial by trial firing rate for all port entries
+                rewspk=[];
+                [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RDall(:,1),PEWindow,{2});% makes trial by trial rasters for event
+                for y= 1:EVn
+                    rewspk(y,1)=sum(EVcell{1,y}>PEWindow(1));
+                end       
+                Y=((rewspk(trialsback+1:end,1)/(PEWindow(1,2)-PEWindow(1,1)))-Bmean)/Bstd; %normalize the activity to baseline
+                
+                RewHistCued.PEHz{NN,1}=Y;
+              
+                
+                %get trial by trial firing rate for all cues
+                CueList=[];
+                rewspk=[];
+                
+                for k=1:length(RDall(:,1))
+                    CueList(k,1)=RAW(i).Erast{Cues}(max(find(RAW(i).Erast{Cues}<RDall(k,1))),1);
+                end
+                
+                [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),CueList,CueWindow,{2});% makes trial by trial rasters for event
+                for y= 1:EVn
+                    rewspk(y,1)=sum(EVcell{1,y}>CueWindow(1));
+                end       
+                Y=((rewspk(trialsback+1:end,1)/(CueWindow(1,2)-CueWindow(1,1)))-Bmean)/Bstd; %subtract baseline to keep it linear
+                RewHistCued.CueHz{NN,1}=Y;
+                
+                
+                
+                %save the predictors
+                RewHistCued.Predictors{NN,1}=X;
+
+
+                fprintf('Neuron # %d\n',NN);
+            end
+        end
+
+    end
+end
+
+save('RewHistCued.mat','RewHistCued');
+

+ 132 - 0
MatlabScripts/1_InitialAnalysis/RewHistTH_calculator.m

@@ -0,0 +1,132 @@
+%Looking at the average firing rate for a given window in each of 4
+%current/previous reward conditions
+MaltValue=0.75;
+
+load ('RAWTH.mat');
+RAW=RAWTH;
+
+%run linear model and stats? 1 is yes, 0 is no
+runanalysis=1;
+
+
+%get parameters
+trialsback=10; %how many trials back to look
+Baseline=[-11 -1]; %For normalizing activity
+RDWindow=[0.75 1.95];
+PEWindow=[-1.5 -0.5]; %relative to RD
+CueWindow=[0 0.75];
+
+%save variables
+RewHistTH.trialsback=trialsback;
+RewHistTH.Baseline=Baseline;
+RewHistTH.RDWindow=RDWindow;
+RewHistTH.PEWindow=PEWindow;
+RewHistTH.CueWindow=CueWindow;
+
+%reset
+NN=0;
+
+
+if runanalysis==1  
+    for i=1:length(RAW) %loops through sessions
+        %events
+        RD=strmatch('RD',RAW(i).Einfo(:,2),'exact');
+        R1=strmatch('R1withanylick',RAW(i).Einfo(:,2),'exact');
+        R2=strmatch('R2withanylick',RAW(i).Einfo(:,2),'exact');
+        R3=strmatch('R3withanylick',RAW(i).Einfo(:,2),'exact');
+        Cue=strmatch('Cue',RAW(i).Einfo(:,2),'exact');
+
+        %% linear model for impact of previous rewards
+        %reset
+        X=NaN(length(RAW(i).Erast{RD,1}(:,1)),trialsback+1);
+        Y=[];
+        AllTrials=[];
+
+        %set up the matrix with outcome identity for each session
+        rewards1=cat(2,RAW(i).Erast{R1,1}(:,1),ones(length(RAW(i).Erast{R1,1}(:,1)),1));
+        rewards2=cat(2,RAW(i).Erast{R2,1}(:,1),MaltValue*ones(length(RAW(i).Erast{R2,1}(:,1)),1));
+        rewards3=cat(2,RAW(i).Erast{R3,1}(:,1),zeros(length(RAW(i).Erast{R3,1}(:,1)),1));
+        rewards=cat(1,rewards1,rewards2,rewards3);
+        [rewards(:,1),ind]=sort(rewards(:,1));
+        rewards(:,2)=rewards(ind,2);
+
+        AllTrials(:,1)=rewards(:,2);
+        AllTrials(:,2)=0;
+
+        for k=1:length(RAW(i).Erast{RD,1}(:,1))
+            time=RAW(i).Erast{RD,1}(k,1);
+            entry=find(round(rewards(:,1))==round(time));
+            for m=1:trialsback+1
+                if entry+1-m>0
+                    X(k,m)=rewards(entry+1-m,2);
+                end
+            end
+
+            AllTrials(entry,2)=1;
+
+        end
+
+
+
+        for j= 1:length(RAW(i).Nrast) %Number of neurons within sessions
+
+            NN=NN+1;
+            rewspk=0;
+            basespk=0;
+
+            %get mean baseline firing for all reward trials
+            [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Cue},Baseline,{2});% makes trial by trial rasters for baseline
+            for y= 1:B1n
+                basespk(1,y)=sum(Bcell1{1,y}>Baseline(1));
+            end
+
+            Bhz=basespk/(Baseline(1,2)-Baseline(1,1));
+            Bmean=nanmean(Bhz);
+            Bstd=nanstd(Bhz);
+
+            %get trial by trial firing rate for all reward trials
+            Window=RDWindow;
+            [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
+            for y= 1:EVn
+                rewspk(y,1)=sum(EVcell{1,y}>Window(1));
+            end       
+            Y=((rewspk(1:end,1)/(Window(1,2)-Window(1,1)))-Bmean)/Bstd; %subtract baseline firing
+            RewHistTH.RDHz{NN,1}=Y(trialsback+1:end);
+
+            %get trial by trial firing rate for all PE trials based on fixed window
+            rewspk=0;
+            Window=PEWindow;
+            [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
+            for y= 1:EVn
+                rewspk(y,1)=sum(EVcell{1,y}>Window(1));
+            end       
+            Y=((rewspk(1:end,1)/(Window(1,2)-Window(1,1)))-Bmean)/Bstd; %z-score
+            RewHistTH.PEHz{NN,1}=Y(trialsback+1:end);               
+
+            %get trial by trial firing rate for all cue trials based on fixed window
+            CueList=[];
+            for k=1:length(RAW(i).Erast{RD})
+                CueList(k,1)=RAW(i).Erast{Cue}(max(find(RAW(i).Erast{Cue}<RAW(i).Erast{RD}(k,1))),1);
+            end
+
+            Window=CueWindow;
+            rewspk=0;
+            [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),CueList,CueWindow,{2});% makes trial by trial rasters for event
+            for y= 1:EVn
+                rewspk(y,1)=sum(EVcell{1,y}>CueWindow(1));
+            end       
+            Y=((rewspk(1:end,1)/(CueWindow(1,2)-CueWindow(1,1)))-Bmean)/Bstd; %subtract baseline to keep it linear
+            RewHistTH.CueHz{NN,1}=Y(trialsback+1:end);
+
+            RewHistTH.Predictors{NN,1}=X(trialsback+1:end,:);
+            RewHistTH.AllTrials{NN,1}=AllTrials;
+
+            fprintf('Neuron # %d\n',NN);
+        end
+    end
+
+
+end
+
+
+save('RewHistTH.mat','RewHistTH');

+ 196 - 0
MatlabScripts/1_InitialAnalysis/block_evolution.m

@@ -0,0 +1,196 @@
+%% Evolution Analysis
+%This script analyses the evolution of firing activity over the course of
+%the two blocks
+%% Clear and load
+% clearvars;
+% close all;
+load('RAWintBlocks.mat');
+RAW=RAWblocks;
+
+%% Generate spike rates per trial (Takes some time)
+
+
+events = {'Cue';'PECue';'RD'};
+event_intervals = [0. 0.5;
+                  -0.5 0.5;
+                   0.75 1.95];
+base_intervals = [-11. -1.;
+                  -11. -1.;
+                  -11. -1.]; %all relative to cue
+RD1 = strcmp('RD1',RAW(1).Einfo(:,2));
+RD2 = strcmp('RD2',RAW(1).Einfo(:,2));
+Cue = strcmp('Cue',RAW(1).Einfo(:,2));
+
+num_trials = 60;
+num_neurons = 0;
+for session = 1:length(RAW)
+    num_neurons = num_neurons + length(RAW(session).Nrast);
+end
+trial_info(num_neurons).blocks = [];
+
+for event = 1:length(events) 
+    counter = 1; 
+    neuron_spike_rate = zeros(num_neurons,num_trials);
+    baseline_spike_rate = zeros(num_neurons,num_trials);
+    for session = 1:length(RAW)
+        event_index = strcmp(events{event,1},RAW(session).Einfo(:,2));
+        event_onsets = RAW(session).Erast{event_index, 1};
+        baseline_onsets = RAW(session).Erast{Cue, 1};
+        if event == 2 % PECue
+            RD_onsets = RAW(session).Erast{strcmp('RD',RAW(session).Einfo(:,2)), 1};
+            index = zeros(length(event_onsets),1);
+            for i = 1:length(event_onsets)
+                index(i) = any(abs(RD_onsets-event_onsets(i))<2);
+            end
+            event_onsets = event_onsets(logical(index));
+        end
+        intervals = event_onsets + event_intervals(event,:);
+        baseline_intervals = baseline_onsets + base_intervals(event,:);
+        
+        for neuron = 1:length(RAW(session).Nrast)
+            spike_times = RAW(session).Nrast{neuron,1};
+            spike_count = NaN(num_trials,1);
+            baseline_count = NaN(num_trials,1);
+            for interval = 1:length(intervals)
+                spike_count(interval,1) = sum(spike_times>=intervals(interval,1) & ...
+                                              spike_times<=intervals(interval,2));
+
+            end
+            for interval = 1:length(baseline_intervals)
+                baseline_count(interval,1) = sum(spike_times>=baseline_intervals(interval,1) & ...
+                                 spike_times<=baseline_intervals(interval,2));
+            end
+            neuron_spike_rate(counter,:) = spike_count'./(event_intervals(event,2)-event_intervals(event,1));
+            baseline_spike_rate(counter,:) = baseline_count'./(base_intervals(event,2)-base_intervals(event,1));
+            name = RAW(session).Ninfo{neuron,1};
+            if event == 1 % On the first pass make the trial info struct
+                    if name(5) == 'n'
+                        trial_info(counter).blocks = 0;
+                    else
+                        trial_info(counter).blocks = str2double(name(5));
+                    end
+                    trial_info(counter).session = session;
+                    trial_info(counter).RD_suc = length(RAW(session).Erast{RD1});
+                    trial_info(counter).RD_mal = length(RAW(session).Erast{RD2});
+                    trial_info(counter).area = name(1:2);
+                    reward_times = sortrows([RAW(session).Erast{RD1},ones(length(RAW(session).Erast{RD1}),1);
+                                               RAW(session).Erast{RD2},zeros(length(RAW(session).Erast{RD2}),1)],1);
+                    trial_info(counter).current_reward = reward_times(:,2);
+                    % Find previous rewards for cues:
+                    cue_times = RAW(session).Erast{Cue}(2:end);
+                    cue_previous_reward = zeros(length(cue_times),1);
+                    for cue = 1:length(cue_times)
+                        [~,index] = max(reward_times(reward_times(:,1) < cue_times(cue),1)); %index of most recent reward consumed
+                        if isempty(index)
+                            cue_previous_reward(cue,1) = NaN; 
+                        else
+                            cue_previous_reward(cue,1) = reward_times(index,2); 
+                        end
+                    end
+                    trial_info(counter).cue_previous_reward = cue_previous_reward;
+
+            end
+            counter = counter + 1;
+        end
+    end
+    baseline_mean = nanmean(baseline_spike_rate,2);
+    baseline_std = nanstd(baseline_spike_rate,0,2);
+    events{event, 2} = (neuron_spike_rate-baseline_mean)./baseline_std; %z-score
+    events{event, 3} = (neuron_spike_rate-baseline_mean); %just baseline substracted for linear models
+    events{event, 4} = (neuron_spike_rate-baseline_mean)./(max(neuron_spike_rate,[],2)-min(neuron_spike_rate,[],2)); %baseline subtracted and normalized to total range
+
+end
+%% Define responsive logical vectors
+
+interspersed = [trial_info.blocks]' == 0;
+blocks1 = [trial_info.blocks]' == 1;
+blocks2 = [trial_info.blocks]' == 2;
+area_vectors = [strcmp({trial_info.area},'NA')',strcmp({trial_info.area},'VP')'];
+blocks_vectors = [[trial_info.blocks]'==0,[trial_info.blocks]'>0]; %Interspersed, blocks
+session_type = {interspersed,blocks1,blocks2};
+session_titles = {'Interspersed','Sucrose First','Malt First'};
+
+
+%% five samplings of 3 trials, equally space through each block of rewards
+
+fives = NaN(num_neurons,10);
+for event = 1:size(events,1)
+    for neuron = 1:num_neurons
+        trial_firing = events{event,2}(neuron,:); % Pull up the trial by trial firing
+        trial_firing = trial_firing(~isnan(trial_firing)); % Remove any NaN values
+        
+        first1 = 1;
+        last2 = length(trial_firing);
+        switch trial_info(neuron).blocks
+            case 0 % Interspersed
+                suc_trials = find(trial_info(neuron).current_reward == 1);
+                malt_trials = find(trial_info(neuron).current_reward == 0);
+                suc_first = suc_trials(1:3);
+                suc_quarter = ceil(length(suc_trials)/4);
+                suc_second = suc_trials(suc_quarter-1:suc_quarter+1);
+                suc_center = ceil(length(suc_trials)/2);
+                suc_mid = suc_trials(suc_center-1:suc_center+1);
+                suc_3rdquarter = ceil(3*length(suc_trials)/4);
+                suc_fourth = suc_trials(suc_3rdquarter-1:suc_3rdquarter+1);
+                suc_last = suc_trials(end-2:end);  
+                
+                malt_first = malt_trials(1:3);
+                malt_quarter = ceil(length(malt_trials)/4);
+                malt_second = malt_trials(malt_quarter-1:malt_quarter+1);
+                malt_center = ceil(length(malt_trials)/2);
+                malt_mid = malt_trials(malt_center-1:malt_center+1);
+                malt_3rdquarter = ceil(3*length(malt_trials)/4);
+                malt_fourth = malt_trials(malt_3rdquarter-1:malt_3rdquarter+1);
+                malt_last = malt_trials(end-2:end);  
+                
+                slices = [suc_first';malt_first';suc_second';malt_second';suc_mid';malt_mid';suc_fourth';malt_fourth';suc_last';malt_last'];
+            case 1 % Suc first
+                first2 = trial_info(neuron).RD_suc+1;
+                last1 = first2-1;
+                mid1 = ceil((last1-first1)/2)+first1;
+                mid2 = ceil((last2-first2)/2)+first2;
+                second1 = ceil((last1-first1)/4)+first1;
+                second2 = ceil((last2-first2)/4)+first2;
+                fourth1 = ceil(3*(last1-first1)/4)+first1;
+                fourth2 = ceil(3*(last2-first2)/4)+first2;
+                slices = [first1:first1+2;...
+                          second1-1:second1+1;...
+                          mid1-1:mid1+1;...
+                          fourth1-1:fourth1+1;...
+                          last1-2:last1;...
+                          first2:first2+2;...
+                          second2-1:second2+1;...
+                          mid2-1:mid2+1;...
+                          fourth2-1:fourth2+1;...
+                          last2-2:last2];
+            case 2 % Malt first
+                first2 = trial_info(neuron).RD_mal+1;
+                last1 = first2-1;
+                mid1 = ceil((last1-first1)/2)+first1;
+                mid2 = ceil((last2-first2)/2)+first2;
+                second1 = ceil((last1-first1)/4)+first1;
+                second2 = ceil((last2-first2)/4)+first2;
+                fourth1 = ceil(3*(last1-first1)/4)+first1;
+                fourth2 = ceil(3*(last2-first2)/4)+first2;
+                slices = [first1:first1+2;...
+                          second1-1:second1+1;...
+                          mid1-1:mid1+1;...
+                          fourth1-1:fourth1+1;...
+                          last1-2:last1;...
+                          first2:first2+2;...
+                          second2-1:second2+1;...
+                          mid2-1:mid2+1;...
+                          fourth2-1:fourth2+1;...
+                          last2-2:last2];
+        end
+
+        for slice = 1:size(slices,1)
+            fives(neuron,slice) = mean(trial_firing(slices(slice,:)));
+        end
+    end
+    events{event,5} = fives; %Just finished changing fives here
+end
+
+save('evolution_data.mat','events')
+save('evolution_info.mat','trial_info')
+disp('done')

+ 35 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_base.m

@@ -0,0 +1,35 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_base(startValues, spikeCounts, rewards, timeLocked)
+
+% cued experiment
+
+alphaLearn = startValues(1);
+slope = startValues(2);
+intercept = startValues(3);
+Vinit = 0.5;
+
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+end
+rateParam = exp(slope*RPE + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 40 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_base_asymm.m

@@ -0,0 +1,40 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_base_asymm(startValues, spikeCounts, rewards, timeLocked)
+
+% cued experiment
+
+alphaPPE = startValues(1);
+alphaNPE = startValues(2);
+slope = startValues(3);
+intercept = startValues(4);
+Vinit = alphaPPE / (alphaPPE + alphaNPE);
+
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    if RPE(t) >= 0
+        V(t + 1) = V(t) + alphaPPE*RPE(t);
+    else
+        V(t + 1) = V(t) + alphaNPE*RPE(t);
+    end
+end
+rateParam = exp(slope*RPE + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 52 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_base_asymm_cue.m

@@ -0,0 +1,52 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_base_asymm_cue(startValues, spikeCounts, rewards, timeLocked, cueInfo)
+
+% cued experiment
+
+alphaPPE = startValues(1);
+alphaNPE = startValues(2);
+slope = startValues(3);
+intercept = startValues(4);
+V_sucCue = startValues(5);
+V_malCue = startValues(6);
+Vinit = alphaPPE / (alphaPPE + alphaNPE);
+
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+rateParam = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    if RPE(t) >= 0
+        V(t + 1) = V(t) + alphaPPE*RPE(t);
+    else
+        V(t + 1) = V(t) + alphaNPE*RPE(t);
+    end
+    
+    if cueInfo(t, 1) == 1 % sucrose cue
+        rateParam(t) = exp(slope*RPE(t) + intercept + V_sucCue);
+    elseif cueInfo(t, 2) == 1 % malto cue
+        rateParam(t) = exp(slope*RPE(t) + intercept + V_malCue);
+    elseif cueInfo(t, 3) == 1 % nonpredictive cue
+        rateParam(t) = exp(slope*RPE(t) + intercept);
+    else
+        error('cueInfo is 0 for all columns\n');
+    end
+end
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 47 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_base_cue.m

@@ -0,0 +1,47 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_base_cue(startValues, spikeCounts, rewards, timeLocked, cueInfo)
+
+% cued experiment
+
+alphaLearn = startValues(1);
+slope = startValues(2);
+intercept = startValues(3);
+V_sucCue = startValues(4);
+V_malCue = startValues(5);
+Vinit = 0.5;
+
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+rateParam = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+    
+    if cueInfo(t, 1) == 1 % sucrose cue
+        rateParam(t) = exp(slope*RPE(t) + intercept + V_sucCue);
+    elseif cueInfo(t, 2) == 1 % malto cue
+        rateParam(t) = exp(slope*RPE(t) + intercept + V_malCue);
+    elseif cueInfo(t, 3) == 1 % nonpredictive cue
+        rateParam(t) = exp(slope*RPE(t) + intercept);
+    else
+        error('cueInfo is 0 for all columns\n');
+    end
+end
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 39 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_curr.m

@@ -0,0 +1,39 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_curr(startValues, spikeCounts, rewards, timeLocked)
+
+% cued experiment; weight determines how much V_sucrose and V_mal influence spike counts
+% cueInfo: N x 3 where the left - suc, mid - mal, right - none
+
+slope = startValues(1);
+intercept = startValues(2);
+
+alphaLearn = 0; % only current reward can be encoded
+Vinit = 0.5;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1); % RPE for trial-by-trial learning
+rateParam = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+end
+
+rateParam = exp(slope*RPE + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 49 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_curr_cue.m

@@ -0,0 +1,49 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_curr_cue(startValues, spikeCounts, rewards, timeLocked, cueInfo)
+
+% cued experiment; weight determines how much V_sucrose and V_mal influence spike counts
+% cueInfo: N x 3 where the left - suc, mid - mal, right - none
+
+slope = startValues(1);
+intercept = startValues(2);
+V_sucCue = startValues(3);
+V_malCue = startValues(4);
+
+alphaLearn = 0; % only current reward can be encoded
+Vinit = 0.5;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1); % RPE for trial-by-trial learning
+rateParam = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+    
+    if cueInfo(t, 1) == 1 % sucrose cue
+        rateParam(t) = exp(slope*RPE(t) + intercept + V_sucCue);
+    elseif cueInfo(t, 2) == 1 % malto cue
+        rateParam(t) = exp(slope*RPE(t) + intercept + V_malCue);
+    elseif cueInfo(t, 3) == 1 % nonpredictive cue
+        rateParam(t) = exp(slope*RPE(t) + intercept);
+    else
+        error('cueInfo is 0 for all columns\n');
+    end
+end
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 14 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_mean.m

@@ -0,0 +1,14 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_mean(startValues, spikeCounts, rewards, timeLocked)
+
+probSpike = poisspdf(spikeCounts, mean(spikeCounts)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = mean(spikeCounts);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end
+
+V = NaN;
+RPE = NaN;

+ 38 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_mean_cue.m

@@ -0,0 +1,38 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_mean_cue(startValues, spikeCounts, rewards, timeLocked, cueInfo)
+
+V_sucCue = startValues(1);
+V_malCue = startValues(2);
+
+trials = length(rewards);
+
+nonpred_trials = cueInfo(:, 3) == 1; % trials without predictive cues
+nonpred_trials = nonpred_trials(timeLocked); % subselect the ones with responses
+mean_nonpredSpikes = mean(spikeCounts(nonpred_trials)); % mean firing of non-predicted trials
+log_mean_nonpredSpikes = log(mean_nonpredSpikes); % log transform so exp(log...) = mean_nonpredSpikes
+
+rateParam = zeros(trials, 1);
+
+for t = 1:trials
+    if cueInfo(t, 1) == 1 % sucrose cue
+        rateParam(t) = exp(log_mean_nonpredSpikes + V_sucCue);
+    elseif cueInfo(t, 2) == 1 % malto cue
+        rateParam(t) = exp(log_mean_nonpredSpikes + V_malCue);
+    elseif cueInfo(t, 3) == 1 % nonpredictive cue
+        rateParam(t) = exp(log_mean_nonpredSpikes);
+    else
+        error('cueInfo is 0 for all columns\n');
+    end
+end
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end
+
+V = NaN;
+RPE = NaN;

+ 44 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_threeValue.m

@@ -0,0 +1,44 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_threeValue(startValues, spikeCounts, rewards, timeLocked, cueInfo)
+
+% cued experiment
+alphaLearn = startValues(1);
+slope = startValues(2);
+intercept = startValues(3);
+Vinit = 0.5;
+Vsuc = 1;
+Vmal = 0;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+RPEforObs = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+    if cueInfo(t, 1) == 1 % sucrose cue
+        RPEforObs(t) = rewards(t) - Vsuc;
+        V(t + 1) = V(t); % carry forward the value function for the nonpredictive cue
+    elseif cueInfo(t, 2) == 1 % malto cue
+        RPEforObs(t) = rewards(t) - Vmal;
+        V(t + 1) = V(t); % carry forward the value function
+    elseif cueInfo(t, 3) == 1 % nonpredictive cue
+        RPE(t) = rewards(t) - V(t);
+        V(t + 1) = V(t) + alphaLearn*RPE(t);
+        RPEforObs(t) = RPE(t);
+    end
+end
+rateParam = exp(slope*RPEforObs + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 49 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_threeValue_asymm.m

@@ -0,0 +1,49 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_threeValue_asymm(startValues, spikeCounts, rewards, timeLocked, cueInfo)
+
+% cued experiment
+alphaPPE = startValues(1);
+alphaNPE = startValues(2);
+slope = startValues(3);
+intercept = startValues(4);
+Vinit = alphaPPE / (alphaPPE + alphaNPE);
+Vsuc = 1;
+Vmal = 0;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+RPEforObs = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+    if cueInfo(t, 1) == 1 % sucrose cue
+        RPEforObs(t) = rewards(t) - Vsuc;
+        V(t + 1) = V(t); % carry forward the value function for the nonpredictive cue
+    elseif cueInfo(t, 2) == 1 % malto cue
+        RPEforObs(t) = rewards(t) - Vmal;
+        V(t + 1) = V(t); % carry forward the value function
+    elseif cueInfo(t, 3) == 1 % nonpredictive cue
+        RPE(t) = rewards(t) - V(t);
+        if RPE(t) >= 0
+            V(t + 1) = V(t) + alphaPPE*RPE(t);
+        else
+            V(t + 1) = V(t) + alphaNPE*RPE(t);
+        end
+        RPEforObs(t) = RPE(t);
+    end
+end
+rateParam = exp(slope*RPEforObs + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 59 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_threeValue_asymm_cue.m

@@ -0,0 +1,59 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_threeValue_asymm_cue(startValues, spikeCounts, rewards, timeLocked, cueInfo)
+
+% cued experiment
+
+alphaPPE = startValues(1);
+alphaNPE = startValues(2);
+slope = startValues(3);
+intercept = startValues(4);
+V_sucCue = startValues(5);
+V_malCue = startValues(6);
+Vinit = alphaPPE / (alphaPPE + alphaNPE);
+Vsuc = 1;
+Vmal = 0;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+RPEforObs = zeros(trials, 1);
+rateParam = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    if cueInfo(t, 1) == 1 % sucrose cue
+        RPEforObs(t) = rewards(t) - Vsuc;
+        V(t + 1) = V(t); % carry forward the value function for the nonpredictive cue
+        rateParam(t) = exp(slope*RPEforObs(t) + intercept + V_sucCue);
+    elseif cueInfo(t, 2) == 1 % malto cue
+        RPEforObs(t) = rewards(t) - Vmal;
+        V(t + 1) = V(t); % carry forward the value function
+        rateParam(t) = exp(slope*RPEforObs(t) + intercept + V_malCue);
+    elseif cueInfo(t, 3) == 1 % nonpredictive cue
+        RPE(t) = rewards(t) - V(t);
+        if RPE(t) >= 0
+            V(t + 1) = V(t) + alphaPPE*RPE(t);
+        else
+            V(t + 1) = V(t) + alphaNPE*RPE(t);
+        end
+        RPEforObs(t) = RPE(t);
+        rateParam(t) = exp(slope*RPEforObs(t) + intercept);
+    else
+        error('cueInfo is 0 for all columns\n');
+    end
+    
+end
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 54 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_RPE/ott_RW_RPE_threeValue_cue.m

@@ -0,0 +1,54 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_threeValue_cue(startValues, spikeCounts, rewards, timeLocked, cueInfo)
+
+% cued experiment
+
+alphaLearn = startValues(1);
+slope = startValues(2);
+intercept = startValues(3);
+V_sucCue = startValues(4);
+V_malCue = startValues(5);
+Vinit = 0.5;
+Vsuc = 1;
+Vmal = 0;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+RPEforObs = zeros(trials, 1);
+rateParam = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    if cueInfo(t, 1) == 1 % sucrose cue
+        RPEforObs(t) = rewards(t) - Vsuc;
+        V(t + 1) = V(t); % carry forward the value function for the nonpredictive cue
+        rateParam(t) = exp(slope*RPEforObs(t) + intercept + V_sucCue);
+    elseif cueInfo(t, 2) == 1 % malto cue
+        RPEforObs(t) = rewards(t) - Vmal;
+        V(t + 1) = V(t); % carry forward the value function
+        rateParam(t) = exp(slope*RPEforObs(t) + intercept + V_malCue);
+    elseif cueInfo(t, 3) == 1 % nonpredictive cue
+        RPE(t) = rewards(t) - V(t);
+        V(t + 1) = V(t) + alphaLearn*RPE(t);
+        RPEforObs(t) = RPE(t);
+        rateParam(t) = exp(slope*RPEforObs(t) + intercept);
+    else
+        error('cueInfo is 0 for all columns\n');
+    end
+    
+end
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 35 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_value/ott_RW_V_base.m

@@ -0,0 +1,35 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_V_base(startValues, spikeCounts, rewards, timeLocked)
+
+% cued experiment
+
+alphaLearn = startValues(1);
+slope = startValues(2);
+intercept = startValues(3);
+Vinit = 0.5;
+
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+end
+rateParam = exp(slope*V(1:trials) + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 40 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_value/ott_RW_V_base_asymm.m

@@ -0,0 +1,40 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_V_base_asymm(startValues, spikeCounts, rewards, timeLocked)
+
+% cued experiment
+
+alphaPPE = startValues(1);
+alphaNPE = startValues(2);
+slope = startValues(3);
+intercept = startValues(4);
+Vinit = alphaPPE / (alphaPPE + alphaNPE);
+
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    if RPE(t) >= 0
+        V(t + 1) = V(t) + alphaPPE*RPE(t);
+    else
+        V(t + 1) = V(t) + alphaNPE*RPE(t);
+    end
+end
+rateParam = exp(slope*V(1:trials) + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 52 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_value/ott_RW_V_base_asymm_cue.m

@@ -0,0 +1,52 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_V_base_asymm_cue(startValues, spikeCounts, rewards, timeLocked, cueInfo)
+
+% cued experiment
+
+alphaPPE = startValues(1);
+alphaNPE = startValues(2);
+slope = startValues(3);
+intercept = startValues(4);
+V_sucCue = startValues(5);
+V_malCue = startValues(6);
+Vinit = alphaPPE / (alphaPPE + alphaNPE);
+
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+rateParam = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    if RPE(t) >= 0
+        V(t + 1) = V(t) + alphaPPE*RPE(t);
+    else
+        V(t + 1) = V(t) + alphaNPE*RPE(t);
+    end
+    
+    if cueInfo(t, 1) == 1 % sucrose cue
+        rateParam(t) = exp(slope*V(t) + intercept + V_sucCue);
+    elseif cueInfo(t, 2) == 1 % malto cue
+        rateParam(t) = exp(slope*V(t) + intercept + V_malCue);
+    elseif cueInfo(t, 3) == 1 % nonpredictive cue
+        rateParam(t) = exp(slope*V(t) + intercept);
+    else
+        error('cueInfo is 0 for all columns\n');
+    end
+end
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 47 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_value/ott_RW_V_base_cue.m

@@ -0,0 +1,47 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_V_base_cue(startValues, spikeCounts, rewards, timeLocked, cueInfo)
+
+% cued experiment
+
+alphaLearn = startValues(1);
+slope = startValues(2);
+intercept = startValues(3);
+V_sucCue = startValues(4);
+V_malCue = startValues(5);
+Vinit = 0.5;
+
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+rateParam = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+    
+    if cueInfo(t, 1) == 1 % sucrose cue
+        rateParam(t) = exp(slope*V(t) + intercept + V_sucCue);
+    elseif cueInfo(t, 2) == 1 % malto cue
+        rateParam(t) = exp(slope*V(t) + intercept + V_malCue);
+    elseif cueInfo(t, 3) == 1 % nonpredictive cue
+        rateParam(t) = exp(slope*V(t) + intercept);
+    else
+        error('cueInfo is 0 for all columns\n');
+    end
+end
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 14 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_value/ott_RW_V_mean.m

@@ -0,0 +1,14 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_V_mean(startValues, spikeCounts, rewards, timeLocked)
+
+probSpike = poisspdf(spikeCounts, mean(spikeCounts)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = mean(spikeCounts);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end
+
+V = NaN;
+RPE = NaN;

+ 38 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_value/ott_RW_V_mean_cue.m

@@ -0,0 +1,38 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_V_mean_cue(startValues, spikeCounts, rewards, timeLocked, cueInfo)
+
+V_sucCue = startValues(1);
+V_malCue = startValues(2);
+
+trials = length(rewards);
+
+nonpred_trials = cueInfo(:, 3) == 1; % trials without predictive cues
+nonpred_trials = nonpred_trials(timeLocked); % subselect the ones with responses
+mean_nonpredSpikes = mean(spikeCounts(nonpred_trials)); % mean firing of non-predicted trials
+log_mean_nonpredSpikes = log(mean_nonpredSpikes); % log transform so exp(log...) = mean_nonpredSpikes
+
+rateParam = zeros(trials, 1);
+
+for t = 1:trials
+    if cueInfo(t, 1) == 1 % sucrose cue
+        rateParam(t) = exp(log_mean_nonpredSpikes + V_sucCue);
+    elseif cueInfo(t, 2) == 1 % malto cue
+        rateParam(t) = exp(log_mean_nonpredSpikes + V_malCue);
+    elseif cueInfo(t, 3) == 1 % nonpredictive cue
+        rateParam(t) = exp(log_mean_nonpredSpikes);
+    else
+        error('cueInfo is 0 for all columns\n');
+    end
+end
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end
+
+V = NaN;
+RPE = NaN;

+ 39 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_value/ott_RW_V_prev.m

@@ -0,0 +1,39 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_V_prev(startValues, spikeCounts, rewards, timeLocked)
+
+% cued experiment; weight determines how much V_sucrose and V_mal influence spike counts
+% cueInfo: N x 3 where the left - suc, mid - mal, right - none
+
+slope = startValues(1);
+intercept = startValues(2);
+
+alphaLearn = 1; % only current reward can be encoded
+Vinit = 0.5;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1); % RPE for trial-by-trial learning
+rateParam = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+end
+
+rateParam = exp(slope*V(1:trials) + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 49 - 0
MatlabScripts/2_Modeling/cue/fit_model/RWmodels_value/ott_RW_V_prev_cue.m

@@ -0,0 +1,49 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_V_prev_cue(startValues, spikeCounts, rewards, timeLocked, cueInfo)
+
+% cued experiment; weight determines how much V_sucrose and V_mal influence spike counts
+% cueInfo: N x 3 where the left - suc, mid - mal, right - none
+
+slope = startValues(1);
+intercept = startValues(2);
+V_sucCue = startValues(3);
+V_malCue = startValues(4);
+
+alphaLearn = 1; % only current reward can be encoded
+Vinit = 0.5;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1); % RPE for trial-by-trial learning
+rateParam = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+    
+    if cueInfo(t, 1) == 1 % sucrose cue
+        rateParam(t) = exp(slope*V(t) + intercept + V_sucCue);
+    elseif cueInfo(t, 2) == 1 % malto cue
+        rateParam(t) = exp(slope*V(t) + intercept + V_malCue);
+    elseif cueInfo(t, 3) == 1 % nonpredictive cue
+        rateParam(t) = exp(slope*V(t) + intercept);
+    else
+        error('cueInfo is 0 for all columns\n');
+    end
+end
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 77 - 0
MatlabScripts/2_Modeling/cue/fit_model/fit_RW_MLE.m

@@ -0,0 +1,77 @@
+clear; clc
+task = 'cue';
+dir_MLEfit_ott(task)
+ott_data = loadData_ott(task);
+
+nStart = 10;
+RNG_val = 1;
+
+% just the included neurons
+my_n = ott_data.IncludedNeurons;
+ott_data.Ninfo = {ott_data.Ninfo{my_n,1}; ott_data.Ninfo{my_n,2}}';
+ott_data.RDHz = ott_data.RDHz(my_n);
+ott_data.PEHz = ott_data.PEHz(my_n);
+ott_data.CueHz = ott_data.CueHz(my_n);
+ott_data.Predictors = ott_data.Predictors(my_n);
+ott_data.AllTrials = ott_data.AllTrials(my_n);
+ott_data.PredCue = ott_data.PredCue(my_n);
+ott_data.PredCueAllTrials = ott_data.PredCueAllTrials(my_n);
+ott_data.Day = ott_data.Day(my_n);
+ott_data.Rat = ott_data.Rat(my_n);
+ott_data.IncludedNeurons = ott_data.IncludedNeurons(my_n);
+%%
+clear os_temp os
+
+models_of_interest_RPE = {'base','base_asymm','base_cue','base_asymm_cue',...
+                          'threeValue','threeValue_asymm','threeValue_cue','threeValue_asymm_cue',...
+                          'curr','curr_cue','mean','mean_cue'};
+models_of_interest_V = {'base','base_asymm','base_cue','base_asymm_cue','mean','mean_cue'};
+
+% models_of_interest_RPE = {'base_asymm','base_asymm_cue',...
+%                           'threeValue_asymm','threeValue_asymm_cue'};
+% models_of_interest_V = {'base_asymm','base_asymm_cue'};
+
+all_fits = struct(); % initialize an empty structure 
+tic
+for ind = 1:length(ott_data.AllTrials) % for all neurons
+    fprintf('n %i of %i. Elapsed time is %0.2f min\n', ind, length(ott_data.AllTrials), toc/60)
+    
+    os_temp(ind).include = ott_data.IncludedNeurons(ind);
+    os_temp(ind).Ninfo = ott_data.Ninfo(ind, :);
+    os_temp(ind).day = ott_data.Day(ind);
+    os_temp(ind).rat = ott_data.Rat(ind);
+    os_temp(ind).spikeCount_RD = round(ott_data.RDHz{ind}*1.2); % RD period; 1.2s long
+    os_temp(ind).spikeCount_cue = round(ott_data.CueHz{ind}*0.75); % cue period; 0.75s long
+    os_temp(ind).spikeCount_PE = ott_data.PEHz{ind};
+    
+    os_temp(ind).rewards = ott_data.AllTrials{ind}(:, 1); % 0 mal, 1 suc
+    os_temp(ind).timeLocked = logical(ott_data.AllTrials{ind}(:, 2)); % trials fast enough to have time-locked responses
+    os_temp(ind).cueInfo = ott_data.PredCueAllTrials{ind}; % predictive cues logical mask [suc mal none]
+    
+    % spikeCount is a temporary field
+    % fit RD
+    os_temp(ind).spikeCount = os_temp(ind).spikeCount_RD;
+    ms = helper_RW_RPE(os_temp(ind), 'StartingPoints', nStart, 'RNG', RNG_val, 'ParticularModel', models_of_interest_RPE);
+    os_temp(ind).mod_RD = ms;
+    
+    % fit cue
+    os_temp(ind).spikeCount = os_temp(ind).spikeCount_cue;
+    ms = helper_RW_V(os_temp(ind), 'StartingPoints', nStart, 'RNG', RNG_val, 'ParticularModel', models_of_interest_V);
+    os_temp(ind).mod_cue = ms;
+    
+    % remove spikeCount to avoid future confusion
+    os(ind) = rmfield(os_temp(ind), 'spikeCount');
+end
+
+fprintf('Finished\n')
+save_MLEfit_ott(task, os);
+
+%%
+for n = 1:length(os)
+    os(n).mod_RD.base_asymm = os_tmp(n).mod_RD.base_asymm;
+    os(n).mod_RD.base_asymm_cue = os_tmp(n).mod_RD.base_asymm_cue;
+    os(n).mod_RD.threeValue_asymm = os_tmp(n).mod_RD.threeValue_asymm;
+    os(n).mod_RD.threeValue_asymm_cue = os_tmp(n).mod_RD.threeValue_asymm_cue;
+    os(n).mod_cue.base_asymm = os_tmp(n).mod_cue.base_asymm;
+    os(n).mod_cue.base_asymm_cue = os_tmp(n).mod_cue.base_asymm_cue;
+end

+ 282 - 0
MatlabScripts/2_Modeling/cue/fit_model/helper_RW_RPE.m

@@ -0,0 +1,282 @@
+function ms = helper_RW_RPE(os, varargin)
+% helper_RW_RPE    Fits neural models to test RW model predictions ~ RPE
+%   ms = helper_RW_RPE(os, varargin)
+%   INPUTS
+%       os: behavioral data structure
+%           .spikeCount: number of spikes in a particular time bin
+%           .rewards: 0 for maltodextrin, 1 for sucrose
+%           .timeLocked: 1 if the animal licked within 2s, 0 if the animal did not (logical)
+%               timeLocked will always be more than or equal to the number of spike trials
+%           .cueInfo: N x 3; left column 1 for suc cue, middle column for mal, right noninformative
+%       varargin
+%           StartingPoints: determines how many points to optimize from
+%           ParticularModel: cell array of strings of models to use
+%           RNG: random number generator seed (default = 1)
+%   OUTPUTS
+%       ms: model structure of fits
+
+p = inputParser;
+p.addParameter('StartingPoints', 1)
+p.addParameter('ParticularModel', []);
+p.addParameter('RNG', []);
+p.parse(varargin{:});
+
+if ~isempty(p.Results.RNG)
+    rng(p.Results.RNG)
+end
+              
+% Initialize models
+if isempty(p.Results.ParticularModel)
+    modelNames = {'base', 'base_cue', ...
+                  'curr', 'curr_cue', ...
+                  'mean', 'mean_cue'};
+else
+    modelNames = p.Results.ParticularModel;
+end
+
+
+% Set up optimization problem
+options = optimset('Algorithm', 'interior-point','ObjectiveLimit',...
+    -1.000000000e+300,'TolFun',1e-15, 'Display','off');
+
+% set boundary conditions
+alpha_range = [0 1];
+slope_range = [0 20]; % reward sensitivity
+intercept_range = [-20 20]; % baseline spiking
+vsuc_range = [-5 5]; % value of sucrose cue
+vmal_range = [-5 5]; % value of mal cue
+
+for currMod = modelNames
+    currMod = currMod{:};
+    
+    % initialize output variables
+    runs = p.Results.StartingPoints;
+    LH = zeros(runs, 1);
+    
+    if strcmp(currMod, 'base')
+        paramNames = {'alpha','slope','intercept'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  slope_range(2);  intercept_range(2);
+           -alpha_range(1); -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_base, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_base(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'base_asymm')
+        paramNames = {'alphaPPE','alphaNPE','slope','intercept'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  alpha_range(2);  slope_range(2);  intercept_range(2);
+           -alpha_range(1); -alpha_range(1); -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_base_asymm, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_base_asymm(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'base_cue')
+        paramNames = {'alpha','slope','intercept','vsuc','vmal'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(vsuc_range) + vsuc_range(1) ...
+                       rand(runs, 1)*diff(vmal_range) + vmal_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  slope_range(2);  intercept_range(2);  vsuc_range(2);  vmal_range(2);
+           -alpha_range(1); -slope_range(1); -intercept_range(1); -vsuc_range(1); -vmal_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_base_cue, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_base_cue(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+    elseif strcmp(currMod, 'base_asymm_cue')
+        paramNames = {'alphaPPE','alphaNPE','slope','intercept','vsuc','vmal'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(vsuc_range) + vsuc_range(1) ...
+                       rand(runs, 1)*diff(vmal_range) + vmal_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  alpha_range(2);  slope_range(2);  intercept_range(2);  vsuc_range(2);  vmal_range(2);
+           -alpha_range(1); -alpha_range(1); -slope_range(1); -intercept_range(1); -vsuc_range(1); -vmal_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_base_cue, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_base_cue(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+    elseif strcmp(currMod, 'threeValue')
+        paramNames = {'alpha','slope','intercept'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  slope_range(2);  intercept_range(2);
+           -alpha_range(1); -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_threeValue, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_threeValue(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+    elseif strcmp(currMod, 'threeValue_asymm')
+        paramNames = {'alpha','slope','intercept'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  alpha_range(2);  slope_range(2);  intercept_range(2);
+           -alpha_range(1); -alpha_range(1); -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_threeValue_asymm, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_threeValue_asymm(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+    elseif strcmp(currMod, 'threeValue_cue')
+        paramNames = {'alpha','slope','intercept','vsuc','vmal'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(vsuc_range) + vsuc_range(1) ...
+                       rand(runs, 1)*diff(vmal_range) + vmal_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  slope_range(2);  intercept_range(2);  vsuc_range(2);  vmal_range(2);
+           -alpha_range(1); -slope_range(1); -intercept_range(1); -vsuc_range(1); -vmal_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_threeValue_cue, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_threeValue_cue(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+    elseif strcmp(currMod, 'threeValue_asymm_cue')
+        paramNames = {'alphaPPE','alphaNPE','slope','intercept','vsuc','vmal'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(vsuc_range) + vsuc_range(1) ...
+                       rand(runs, 1)*diff(vmal_range) + vmal_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  alpha_range(2);  slope_range(2);  intercept_range(2);  vsuc_range(2);  vmal_range(2);
+           -alpha_range(1); -alpha_range(1); -slope_range(1); -intercept_range(1); -vsuc_range(1); -vmal_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_threeValue_asymm_cue, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_threeValue_asymm_cue(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+    elseif strcmp(currMod, 'curr')
+        paramNames = {'slope','intercept'};
+        startValues = [rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ slope_range(2);  intercept_range(2);
+           -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_curr, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_curr(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+   elseif strcmp(currMod, 'curr_cue')
+        paramNames = {'slope','intercept','vsuc','vmal'};
+        startValues = [rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(vsuc_range) + vsuc_range(1) ...
+                       rand(runs, 1)*diff(vmal_range) + vmal_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ slope_range(2);  intercept_range(2);  vsuc_range(2);  vmal_range(2);
+           -slope_range(1); -intercept_range(1); -vsuc_range(1); -vmal_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_curr_cue, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_curr_cue(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo); 
+    elseif strcmp(currMod, 'mean')
+        paramNames = {''};
+        numParam = 0;        
+        [LH, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_mean([], os.spikeCount);
+        bestFit = 1;
+        ms.(currMod).bestParams = [];
+    elseif strcmp(currMod, 'mean_cue')
+        paramNames = {'vsuc','vmal'};
+        startValues = [rand(runs, 1)*diff(vsuc_range) + vsuc_range(1) ...
+                       rand(runs, 1)*diff(vmal_range) + vmal_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ vsuc_range(2);  vmal_range(2);
+           -vsuc_range(1); -vmal_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_mean_cue, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_mean_cue(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+    else 
+        error('RW model: Model name not found')
+    end
+    ms.(currMod).paramNames = paramNames;
+    ms.(currMod).LH = -1 * LH(bestFit, :);
+    ms.(currMod).BIC = log(length(os.spikeCount))*numParam  - 2*ms.(currMod).LH;
+    ms.(currMod).AIC = 2*numParam - 2*ms.(currMod).LH;
+    ms.(currMod).AICc = ms.(currMod).AIC + (2*numParam^2 + 2*numParam)/(length(os.spikeCount) - numParam - 1);
+    
+%     ms.(currMod).CIvals = sqrt(diag(inv(hess)))'*1.96;
+end

+ 205 - 0
MatlabScripts/2_Modeling/cue/fit_model/helper_RW_V.m

@@ -0,0 +1,205 @@
+function ms = helper_RW_V(os, varargin)
+% helper_RW_V    Fits neural models to test RW model predictions ~ value
+%   ms = helper_RW_V(os, varargin)
+%   INPUTS
+%       os: behavioral data structure
+%           .spikeCount: number of spikes in a particular time bin
+%           .rewards: 0 for maltodextrin, 1 for sucrose
+%           .timeLocked: 1 if the animal licked within 2s, 0 if the animal did not (logical)
+%               timeLocked will always be more than or equal to the number of spike trials
+%           .cueInfo: N x 3; left column 1 for suc cue, middle column for mal, right noninformative
+%       varargin
+%           StartingPoints: determines how many points to optimize from
+%           ParticularModel: cell array of strings of models to use
+%           RNG: random number generator seed (default = 1)
+%   OUTPUTS
+%       ms: model structure of fits
+
+p = inputParser;
+p.addParameter('StartingPoints', 1)
+p.addParameter('ParticularModel', []);
+p.addParameter('RNG', []);
+p.parse(varargin{:});
+
+if ~isempty(p.Results.RNG)
+    rng(p.Results.RNG)
+end
+              
+% Initialize models
+if isempty(p.Results.ParticularModel)
+    modelNames = {'base', 'base_cue', ...
+                  'prev', 'prev_cue', ...
+                  'mean', 'mean_cue'};
+else
+    modelNames = p.Results.ParticularModel;
+end
+
+
+% Set up optimization problem
+options = optimset('Algorithm', 'interior-point','ObjectiveLimit',...
+    -1.000000000e+300,'TolFun',1e-15, 'Display','off');
+
+% set boundary conditions
+alpha_range = [0 1];
+slope_range = [0 20]; % reward sensitivity
+intercept_range = [-20 20]; % baseline spiking
+vsuc_range = [-5 5]; % value of sucrose cue
+vmal_range = [-5 5]; % value of mal cue
+
+for currMod = modelNames
+    currMod = currMod{:};
+    
+    % initialize output variables
+    runs = p.Results.StartingPoints;
+    LH = zeros(runs, 1);
+    
+    if strcmp(currMod, 'base')
+        paramNames = {'alpha','slope','intercept'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  slope_range(2);  intercept_range(2);
+           -alpha_range(1); -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_V_base, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_base(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'base_asymm')
+        paramNames = {'alphaPPE','alphaNPE','slope','intercept'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  alpha_range(2); slope_range(2);  intercept_range(2);
+           -alpha_range(1); -alpha_range(1); -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_V_base_asymm, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_base_asymm(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'base_cue')
+        paramNames = {'alpha','slope','intercept','vsuc','vmal'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(vsuc_range) + vsuc_range(1) ...
+                       rand(runs, 1)*diff(vmal_range) + vmal_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  slope_range(2);  intercept_range(2);  vsuc_range(2);  vmal_range(2); ...
+           -alpha_range(1); -slope_range(1); -intercept_range(1); -vsuc_range(1); -vmal_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_V_base_cue, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_base_cue(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+    elseif strcmp(currMod, 'base_asymm_cue')
+        paramNames = {'alphaPPE','alphaNPE','slope','intercept','vsuc','vmal'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(vsuc_range) + vsuc_range(1) ...
+                       rand(runs, 1)*diff(vmal_range) + vmal_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  alpha_range(2);  slope_range(2);  intercept_range(2);  vsuc_range(2);  vmal_range(2); ...
+           -alpha_range(1); -alpha_range(1); -slope_range(1); -intercept_range(1); -vsuc_range(1); -vmal_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_V_base_asymm_cue, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_base_asymm_cue(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+    elseif strcmp(currMod, 'prev')
+        paramNames = {'slope','intercept'};
+        startValues = [rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ slope_range(2);  intercept_range(2);
+           -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_V_prev, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_prev(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'prev_cue')
+        paramNames = {'slope','intercept','vsuc','vmal'};
+        startValues = [rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(vsuc_range) + vsuc_range(1) ...
+                       rand(runs, 1)*diff(vmal_range) + vmal_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ slope_range(2);  intercept_range(2);  vsuc_range(2);  vmal_range(2); ...
+           -slope_range(1); -intercept_range(1); -vsuc_range(1); -vmal_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_V_prev_cue, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_prev_cue(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+    elseif strcmp(currMod, 'mean')
+        paramNames = {''};
+        numParam = 0;        
+        [LH, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_mean([], os.spikeCount);
+        bestFit = 1;
+        ms.(currMod).bestParams = [];
+        hess = [];
+    elseif strcmp(currMod, 'mean_cue')
+        paramNames = {'vsuc','vmal'};
+        startValues = [rand(runs, 1)*diff(vsuc_range) + vsuc_range(1) ...
+                       rand(runs, 1)*diff(vmal_range) + vmal_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ vsuc_range(2);  vmal_range(2); ...
+           -vsuc_range(1); -vmal_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_V_mean_cue, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_mean_cue(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked, os.cueInfo);
+    else 
+        error('RW model: Model name not found')
+    end
+    ms.(currMod).paramNames = paramNames;
+    ms.(currMod).LH = -1 * LH(bestFit, :);
+    ms.(currMod).BIC = log(length(os.spikeCount))*numParam  - 2*ms.(currMod).LH;
+    ms.(currMod).AIC = 2*numParam - 2*ms.(currMod).LH;
+    ms.(currMod).AICc = ms.(currMod).AIC + (2*numParam^2 + 2*numParam)/(length(os.spikeCount) - numParam - 1);
+    
+%     ms.(currMod).CIvals = sqrt(diag(inv(hess)))'*1.96;
+end

+ 38 - 0
MatlabScripts/2_Modeling/cue/scripts/enrichment_predcue_cueRD.m

@@ -0,0 +1,38 @@
+clear; clc
+load(fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits', 'cue_MLEfits.mat'));
+
+myColors = importColors_bb;
+VP_color = myColors.bluishGreen;
+
+% get relevant behavior models
+modelCriterion = 'AIC';
+plotFlag = false;
+m_RPE = {'base','base_cue','curr','curr_cue','mean','mean_cue'};
+m_V = {'base','base_cue','mean','mean_cue'};
+
+timePeriod = 'RD';
+bm_RD = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,'plotModels_Flag',plotFlag,...
+                          'particularModel',m_RPE);
+
+timePeriod = 'cue';
+bm_cue = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,'plotModels_Flag',plotFlag,...
+                           'particularModel',m_V);
+                       
+%% enrichment among cue neurons
+predCue_RD = bm_RD.mask_base_cue | bm_RD.mask_curr_cue | bm_RD.mask_mean_cue;
+predCue_cue = bm_cue.mask_base_cue | bm_cue.mask_mean_cue;
+
+n_cueRD_cueCue = sum(predCue_cue(predCue_RD));
+n_notcueRD_cueCue = sum(predCue_cue(~predCue_RD));
+n_cueRD = sum(predCue_RD);
+n_notcueRD = sum(~predCue_RD);
+
+[~,p] = prop_test([n_cueRD_cueCue n_notcueRD_cueCue], [n_cueRD n_notcueRD]);
+% no enrichment
+
+fprintf('\n------\n')
+fprintf('%i of %i RD predictive-cue neurons (%0.2f%%) have predictive-cue response at cue\n', n_cueRD_cueCue, n_cueRD, n_cueRD_cueCue/n_cueRD*100)
+fprintf('%i of %i RD non-predictive-cue neurons (%0.2f%%) have predictive-cue response at cue\n', ...
+         n_notcueRD_cueCue, n_notcueRD, n_notcueRD_cueCue/n_notcueRD*100)
+fprintf('pValue: %0.2f\n',p)
+fprintf('------\n')

+ 32 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_V.m

@@ -0,0 +1,32 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_V(startValues, spikeCounts, rewards, timeLocked)
+
+alphaLearn = startValues(1);
+slope = startValues(2);
+intercept = startValues(3);
+Vinit = 0.5;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+end
+rateParam = exp(slope*V(1:end - 1) + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 34 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_adapt.m

@@ -0,0 +1,34 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE, int_vec] = ott_RW_RPE_adapt(startValues, spikeCounts, rewards, timeLocked)
+
+alphaLearn = startValues(1);
+slope = startValues(2);
+int_max = startValues(3);
+int_min = startValues(4);
+Vinit = 0.5;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+end
+int_vec = (int_min - int_max)*V(1:trials) + int_min; % intercept modulated by value
+rateParam = exp(slope*rewards + int_vec); % firing rate modulated by rewards
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+int_vec = int_vec(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 32 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_base.m

@@ -0,0 +1,32 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_base(startValues, spikeCounts, rewards, timeLocked)
+
+alphaLearn = startValues(1);
+slope = startValues(2);
+intercept = startValues(3);
+Vinit = 0.5;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+end
+rateParam = exp(slope*RPE + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 37 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_base_asymm.m

@@ -0,0 +1,37 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_base_asymm(startValues, spikeCounts, rewards, timeLocked)
+
+alphaPPE = startValues(1); % PPE learning rate
+alphaNPE = startValues(2); % NPE learning rate
+slope = startValues(3);
+intercept = startValues(4);
+Vinit = alphaPPE / (alphaPPE + alphaNPE);
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    if RPE(t) >= 0
+        V(t + 1) = V(t) + alphaPPE*RPE(t);
+    else
+        V(t + 1) = V(t) + alphaNPE*RPE(t);
+    end
+end
+rateParam = exp(slope*RPE + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 37 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_base_asymm_flipped.m

@@ -0,0 +1,37 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_base_asymm_flipped(startValues, spikeCounts, rewards, timeLocked)
+
+alphaPPE = startValues(1); % PPE learning rate
+alphaNPE = startValues(2); % NPE learning rate
+slope = startValues(3); % flipped
+intercept = startValues(4);
+Vinit = alphaPPE / (alphaPPE + alphaNPE);
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    if RPE(t) >= 0
+        V(t + 1) = V(t) + alphaPPE*RPE(t);
+    else
+        V(t + 1) = V(t) + alphaNPE*RPE(t);
+    end
+end
+rateParam = exp(slope*RPE + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 32 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_base_flipped.m

@@ -0,0 +1,32 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_base_flipped(startValues, spikeCounts, rewards, timeLocked)
+
+alphaLearn = startValues(1);
+slope = startValues(2); % slope flipped relative to positive neurons
+intercept = startValues(3);
+Vinit = 0.5;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+end
+rateParam = exp(slope*RPE + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 39 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_base_rect.m

@@ -0,0 +1,39 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_base_rect(startValues, spikeCounts, rewards, timeLocked)
+
+alphaLearn = startValues(1);
+RPErect = startValues(2); % if RPE is below this amount, fix it
+slope = startValues(3);
+intercept = startValues(4);
+Vinit = 0.5;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+RPE_forRateParam = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+    if RPE(t) < RPErect % if we need to rectify this RPE
+        RPE_forRateParam(t) = RPErect;
+    else
+        RPE_forRateParam(t) = RPE(t);
+    end
+end
+rateParam = exp(slope*RPE_forRateParam + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 21 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_curr.m

@@ -0,0 +1,21 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_curr(startValues, spikeCounts, rewards, timeLocked)
+% firing rate only correlates with current reward; no real learning happens
+
+slope = startValues(1);
+intercept = startValues(2);
+
+rateParam = exp(slope*rewards + intercept);
+rateParam(rateParam < 0) = 0.1;
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end
+
+V = NaN;
+RPE = NaN;

+ 21 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_curr_flipped.m

@@ -0,0 +1,21 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_curr_flipped(startValues, spikeCounts, rewards, timeLocked)
+% firing rate only correlates with current reward; no real learning happens
+
+slope = startValues(1); % maltodextrin-preferring
+intercept = startValues(2);
+
+rateParam = exp(slope*rewards + intercept);
+rateParam(rateParam < 0) = 0.1;
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end
+
+V = NaN;
+RPE = NaN;

+ 35 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_habit.m

@@ -0,0 +1,35 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE, slope_vec] = ott_RW_RPE_habit(startValues, spikeCounts, rewards, timeLocked)
+
+alphaLearn = startValues(1);
+slope_max = startValues(2); % slope positive
+slope_min = startValues(3);
+int = startValues(4); % intercept
+Vinit = 0.5;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+end
+slope_vec = (slope_min - slope_max)*V(1:trials) + slope_min; % slope modulated by value
+% right side (log_mean_spikes - slope_vec*0.5) constrains firing rate to mean firing rate at r = 0.5
+rateParam = exp(slope_vec.*rewards + int - slope_vec*0.5); % firing rate modulated by rewards
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+slope_vec = slope_vec(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 40 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_habit_asymm.m

@@ -0,0 +1,40 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE, slope_vec] = ott_RW_RPE_habit_asymm(startValues, spikeCounts, rewards, timeLocked)
+
+alphaPPE = startValues(1);
+alphaNPE = startValues(2);
+slope_max = startValues(3); % slope positive
+slope_min = startValues(4);
+int = startValues(5);
+Vinit = alphaPPE / (alphaPPE + alphaNPE);
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    if RPE(t) >= 0
+        V(t + 1) = V(t) + alphaPPE*RPE(t);
+    else
+        V(t + 1) = V(t) + alphaNPE*RPE(t);
+    end
+end
+slope_vec = (slope_min - slope_max)*V(1:trials) + slope_min; % slope modulated by value
+% right side (log_mean_spikes - slope_vec*0.5) constrains firing rate to mean firing rate at r = 0.5
+rateParam = exp(slope_vec.*rewards + int - slope_vec*0.5); % firing rate modulated by rewards
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+slope_vec = slope_vec(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 15 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_RPE/ott_RW_RPE_mean.m

@@ -0,0 +1,15 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_mean(startValues, spikeCounts, rewards, timeLocked)
+
+
+probSpike = poisspdf(spikeCounts, mean(spikeCounts)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = mean(spikeCounts);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end
+
+V = NaN;
+RPE = NaN;

+ 32 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_value/ott_RW_V_base.m

@@ -0,0 +1,32 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_V_base(startValues, spikeCounts, rewards, timeLocked)
+
+alphaLearn = startValues(1);
+slope = startValues(2);
+intercept = startValues(3);
+Vinit = 0.5;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+end
+rateParam = exp(slope*V(1:trials) + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 37 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_value/ott_RW_V_base_asymm.m

@@ -0,0 +1,37 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_V_base_asymm(startValues, spikeCounts, rewards, timeLocked)
+
+alphaPPE = startValues(1);
+alphaNPE = startValues(2);
+slope = startValues(3);
+intercept = startValues(4);
+Vinit = alphaPPE / (alphaPPE + alphaNPE);
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    if RPE(t) >= 0
+        V(t + 1) = V(t) + alphaPPE*RPE(t);
+    else
+        V(t + 1) = V(t) + alphaNPE*RPE(t);
+    end
+end
+rateParam = exp(slope*V(1:trials) + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 15 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_value/ott_RW_V_mean.m

@@ -0,0 +1,15 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_V_mean(startValues, spikeCounts, rewards, timeLocked)
+
+
+probSpike = poisspdf(spikeCounts, mean(spikeCounts)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = mean(spikeCounts);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end
+
+V = NaN;
+RPE = NaN;

+ 33 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/RWmodels_value/ott_RW_V_prev.m

@@ -0,0 +1,33 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_V_prev(startValues, spikeCounts, rewards, timeLocked)
+
+slope = startValues(1);
+intercept = startValues(2);
+
+alphaLearn = 1; % value just reflects previous trial outcome
+Vinit = 0.5;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+end
+rateParam = exp(slope*V(1:trials) + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 45 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/fit_RW_MLE.m

@@ -0,0 +1,45 @@
+clear; clc
+task = 'intBlocks';
+dir_MLEfit_ott(task)
+ott_data = loadData_ott(task);
+
+nStart = 10;
+RNG_val = 1;
+%%
+clear os_temp os
+
+models_of_interest_RPE = {'base','base_flipped','base_asymm','base_asymm_flipped','base_rect', ...
+                          'adapt','habit','habit_asymm','curr','curr_flipped','mean'};
+models_of_interest_V = {'base','base_asymm','mean'};
+all_fits = struct(); % initialize an empty structure 
+for ind = 1:length(ott_data.RDHz) % for all neurons
+    fprintf('n %i of %i\n', ind, length(ott_data.RDHz))
+    
+    os_temp(ind).Blocks = ott_data.Blocks(ind);
+    os_temp(ind).Blocks12 = ott_data.Blocks12(ind);
+    os_temp(ind).Region = ott_data.Region{ind};
+    
+    os_temp(ind).spikeCount_RD = round(ott_data.RDHz{ind}*1.2); % RD period; 1.2s long
+    os_temp(ind).spikeCount_cue = round(ott_data.CueHz{ind}*0.75); % cue period; 0.75s long
+    os_temp(ind).spikeCount_PE = ott_data.PEHz{ind}; % PE period; 1.0s long
+    
+    os_temp(ind).rewards = ott_data.AllTrials{ind}(:, 1); % 0 mal, 1 suc
+    os_temp(ind).timeLocked = logical(ott_data.AllTrials{ind}(:, 2)); % trials fast enough to have time-locked responses
+    
+    % spikeCount is a temporary field
+    % fit RD
+    os_temp(ind).spikeCount = os_temp(ind).spikeCount_RD;
+    ms = helper_RW_RPE(os_temp(ind), 'StartingPoints', nStart, 'RNG', RNG_val, 'ParticularModel', models_of_interest_RPE);
+    os_temp(ind).mod_RD = ms;
+    
+    % fit cue
+    os_temp(ind).spikeCount = os_temp(ind).spikeCount_cue;
+    ms = helper_RW_V(os_temp(ind), 'StartingPoints', nStart, 'RNG', RNG_val, 'ParticularModel', models_of_interest_V);
+    os_temp(ind).mod_cue = ms;
+    
+    % remove spikeCount to avoid future confusion
+    os(ind) = rmfield(os_temp(ind), 'spikeCount');
+end
+
+fprintf('Finished\n')
+save_MLEfit_ott(task, os);

+ 279 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/helper_RW_RPE.m

@@ -0,0 +1,279 @@
+function ms = helper_RW_RPE(os, varargin)
+% helper_RW_RPE    Fits neural models to test RW model predictions ~ RPE
+%   ms = helper_RW_RPE(os, varargin)
+%   INPUTS
+%       os: behavioral data structure
+%           .spikeCount: number of spikes in a particular time bin
+%           .rewards: 0 for maltodextrin, 1 for sucrose
+%           .timeLocked: 1 if the animal licked within 2s, 0 if the animal did not (logical)
+%               timeLocked will always be more than or equal to the number of spike trials
+%       varargin
+%           StartingPoints: determines how many points to optimize from
+%           ParticularModel: cell array of strings of models to use
+%           RNG: random number generator seed (default = 1)
+%   OUTPUTS
+%       ms: model structure of fits
+
+p = inputParser;
+p.addParameter('StartingPoints', 1)
+p.addParameter('ParticularModel', []);
+p.addParameter('RNG', []);
+p.parse(varargin{:});
+
+if ~isempty(p.Results.RNG)
+    rng(p.Results.RNG)
+end
+              
+% Initialize models
+if isempty(p.Results.ParticularModel)
+    modelNames = {'base', 'curr', 'mean'};
+    % possible model names
+        % V: value
+        % base: RPE
+        % base_flipped: RPE, sign-flipped
+        % base_asymm: RPE with asymmetric learning rates
+        % base_asymm_flipped: RPE with asymmetric learning rates, sign-flipped
+        % 
+else
+    modelNames = p.Results.ParticularModel;
+end
+
+
+% Set up optimization problem
+options = optimset('Algorithm', 'interior-point','ObjectiveLimit',...
+    -1.000000000e+300,'TolFun',1e-15, 'Display','off');
+
+% set boundary conditions
+alpha_range = [0 1];
+RPErect_range = [-1 1]; % rectify RPEs below this number
+slope_range = [0 20]; % reward sensitivity
+slope_flipped_range = [-20 0]; % reward sensitivity; flipped
+intercept_range = [-20 20]; % baseline spiking
+
+for currMod = modelNames
+    currMod = currMod{:};
+    
+    % initialize output variables
+    runs = p.Results.StartingPoints;
+    LH = zeros(runs, 1);
+    
+    if strcmp(currMod, 'V')
+        paramNames = {'alpha','slope','intercept'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  slope_range(2);  intercept_range(2);
+           -alpha_range(1); -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_V, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_V(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'base')
+        paramNames = {'alpha','slope','intercept'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  slope_range(2);  intercept_range(2);
+           -alpha_range(1); -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_base, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_base(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+     elseif strcmp(currMod, 'base_flipped')
+        paramNames = {'alpha','slope','intercept'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_flipped_range) + slope_flipped_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  slope_flipped_range(2);  intercept_range(2);
+           -alpha_range(1); -slope_flipped_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_base_flipped, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_base_flipped(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'base_asymm')
+        paramNames = {'alphaPPE','alphaNPE','slope','intercept'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ... % alphaPPE
+                       rand(runs, 1)*diff(alpha_range) + alpha_range(1) ... % alphaNPE
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  alpha_range(2);  slope_range(2);  intercept_range(2);
+           -alpha_range(1); -alpha_range(1); -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_base_asymm, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_base_asymm(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'base_asymm_flipped')
+        paramNames = {'alphaPPE','alphaNPE','slope','intercept'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ... % alphaPPE
+                       rand(runs, 1)*diff(alpha_range) + alpha_range(1) ... % alphaNPE
+                       rand(runs, 1)*diff(slope_flipped_range) + slope_flipped_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  alpha_range(2);  slope_flipped_range(2);  intercept_range(2);
+           -alpha_range(1); -alpha_range(1); -slope_flipped_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_base_asymm_flipped, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_base_asymm_flipped(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'base_rect')
+        paramNames = {'alpha','RPErect','slope','intercept'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(RPErect_range) + RPErect_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  RPErect_range(2);  slope_range(2);  intercept_range(2);
+           -alpha_range(1); -RPErect_range(1); -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_base_rect, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_base_rect(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'curr')
+        paramNames = {'slope','intercept'};
+        startValues = [rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ slope_range(2);  intercept_range(2);
+           -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_curr, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_curr(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+   elseif strcmp(currMod, 'curr_flipped')
+        paramNames = {'slope','intercept'};
+        startValues = [rand(runs, 1)*diff(slope_flipped_range) + slope_flipped_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ slope_flipped_range(2);  intercept_range(2);
+           -slope_flipped_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_curr_flipped, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_curr_flipped(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'mean')
+        paramNames = {''};
+        numParam = 0;        
+        [LH, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_mean([], os.spikeCount);
+        bestFit = 1;
+        ms.(currMod).bestParams = [];
+    % test out habit model
+    elseif strcmp(currMod, 'habit')
+        paramNames = {'alpha','slope_max','slope_min','int'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam); 0 -1 1 0]; % constrain slope_max to be less than slope_min
+        b=[ alpha_range(2);  slope_range(2);  slope_range(2);  intercept_range(2);
+           -alpha_range(1); -slope_range(1); -slope_range(1); -intercept_range(1); 0];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_habit, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs, ms.(currMod).slope_vec] = ...
+            ott_RW_RPE_habit(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'habit_asymm')
+        paramNames = {'alphaPPE','alphaNPE','slope_max','slope_min','int'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam); 0 0 -1 1 0]; % constrain slope_max to be less than slope_min
+        b=[ alpha_range(2);  alpha_range(2);  slope_range(2);  slope_range(2);  intercept_range(2);
+           -alpha_range(1); -alpha_range(1); -slope_range(1); -slope_range(1); -intercept_range(1); 0];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_habit_asymm, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs, ms.(currMod).slope_vec] = ...
+            ott_RW_RPE_habit_asymm(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'adapt')
+        paramNames = {'alpha','slope','int_max','int_min'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam); 0 0 -1 1]; % constrain int_max to be less than int_min
+        b=[ alpha_range(2);  slope_range(2);  intercept_range(2);  intercept_range(2);
+           -alpha_range(1); -slope_range(1); -intercept_range(1); -intercept_range(1); 0];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_adapt, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs, ms.(currMod).slope_vec] = ...
+            ott_RW_RPE_adapt(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    else 
+        error('RW model: Model name not found')
+    end
+    ms.(currMod).paramNames = paramNames;
+    ms.(currMod).LH = -1 * LH(bestFit, :);
+    ms.(currMod).BIC = log(length(os.spikeCount))*numParam  - 2*ms.(currMod).LH;
+    ms.(currMod).AIC = 2*numParam - 2*ms.(currMod).LH;
+    ms.(currMod).AICc = ms.(currMod).AIC + (2*numParam^2 + 2*numParam)/(length(os.spikeCount) - numParam - 1);
+end

+ 119 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model/helper_RW_V.m

@@ -0,0 +1,119 @@
+function ms = helper_RW_V(os, varargin)
+% helper_RW_V    Fits neural models to test RW model predictions ~ value
+%   ms = helper_RW_V(os, varargin)
+%   INPUTS
+%       os: behavioral data structure
+%           .spikeCount: number of spikes in a particular time bin
+%           .rewards: 0 for maltodextrin, 1 for sucrose
+%           .timeLocked: 1 if the animal licked within 2s, 0 if the animal did not (logical)
+%               timeLocked will always be more than or equal to the number of spike trials
+%       varargin
+%           StartingPoints: determines how many points to optimize from
+%           ParticularModel: cell array of strings of models to use
+%           RNG: random number generator seed (default = 1)
+%   OUTPUTS
+%       ms: model structure of fits
+
+p = inputParser;
+p.addParameter('StartingPoints', 1)
+p.addParameter('ParticularModel', []);
+p.addParameter('RNG', []);
+p.parse(varargin{:});
+
+if ~isempty(p.Results.RNG)
+    rng(p.Results.RNG)
+end
+
+% Initialize models
+if isempty(p.Results.ParticularModel)
+    modelNames = {'base','prev','mean'};
+else
+    modelNames = p.Results.ParticularModel;
+end
+              
+% Set up optimization problem
+options = optimset('Algorithm', 'interior-point','ObjectiveLimit',...
+    -1.000000000e+300,'TolFun',1e-15, 'Display','off');
+
+% set boundary conditions
+alpha_range = [0 1];
+slope_range = [0 20]; % reward sensitivity
+intercept_range = [-20 20]; % baseline spiking
+
+for currMod = modelNames
+    currMod = currMod{:};
+    
+    % initialize output variables
+    runs = p.Results.StartingPoints;
+    LH = zeros(runs, 1);
+    
+    if strcmp(currMod, 'base')
+        paramNames = {'alpha','slope','intercept'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  slope_range(2);  intercept_range(2);
+           -alpha_range(1); -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_V_base, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_base(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'base_asymm')
+        paramNames = {'alphaPPE','alphaNPE','slope','intercept'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  alpha_range(2);  slope_range(2);  intercept_range(2);
+           -alpha_range(1); -alpha_range(1); -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_V_base_asymm, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_base_asymm(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'prev')
+        paramNames = {'slope','intercept'};
+        startValues = [rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ slope_range(2);  intercept_range(2);
+           -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_V_prev, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_prev(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+   elseif strcmp(currMod, 'mean')
+        paramNames = {''};
+        numParam = 0;        
+        [LH, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_mean([], os.spikeCount);
+        bestFit = 1;
+        ms.(currMod).bestParams = [];
+    else 
+        error('RW model: Model name not found')
+    end
+    ms.(currMod).paramNames = paramNames;
+    ms.(currMod).LH = -1 * LH(bestFit, :);
+    ms.(currMod).BIC = log(length(os.spikeCount))*numParam  - 2*ms.(currMod).LH;
+    ms.(currMod).AIC = 2*numParam - 2*ms.(currMod).LH;
+    ms.(currMod).AICc = ms.(currMod).AIC + (2*numParam^2 + 2*numParam)/(length(os.spikeCount) - numParam - 1);
+end

+ 46 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/fit_gRW_MLE.m

@@ -0,0 +1,46 @@
+clear; clc
+task = 'intBlocks';
+ott_data = loadData_ott(task);
+
+nStart = 10;
+RNG_val = 1;
+my_root = fullfile(ottBari2020_root, 'MatlabScripts', '2_Modeling', 'intBlocks', 'fit_model_gauss');
+addpath(fullfile(my_root, 'gRWmodels_RPE'))
+addpath(fullfile(my_root, 'gRWmodels_value'))
+%%
+clear os_temp os
+
+models_of_interest_RPE = {'base','base_asymm','curr','mean'};
+% models_of_interest_V = {'base','base_asymm','mean'};
+all_fits = struct(); % initialize an empty structure 
+for ind = 1:length(ott_data.RDHz) % for all neurons
+    fprintf('n %i of %i\n', ind, length(ott_data.RDHz))
+    
+    os_temp(ind).Blocks = ott_data.Blocks(ind);
+    os_temp(ind).Blocks12 = ott_data.Blocks12(ind);
+    os_temp(ind).Region = ott_data.Region{ind};
+    
+    os_temp(ind).spikeRate_RD = zscore(round(ott_data.RDHz{ind}*1.2)); % RD period; 1.2s long
+    os_temp(ind).spikeRate_cue = zscore(round(ott_data.CueHz{ind}*0.75)); % cue period; 0.75s long
+    
+    os_temp(ind).rewards = ott_data.AllTrials{ind}(:, 1); % 0 mal, 1 suc
+    os_temp(ind).timeLocked = logical(ott_data.AllTrials{ind}(:, 2)); % trials fast enough to have time-locked responses
+    
+    % spikeRate is a temporary field
+    % fit RD
+    os_temp(ind).spikeRate = os_temp(ind).spikeRate_RD;
+    ms = helper_gRW_RPE(os_temp(ind), 'StartingPoints', nStart, 'RNG', RNG_val, 'ParticularModel', models_of_interest_RPE);
+    os_temp(ind).mod_RD = ms;
+    
+%     % fit cue
+%     os_temp(ind).spikeRate = os_temp(ind).spikeRate_cue;
+%     ms = helper_gRW_V(os_temp(ind), 'StartingPoints', nStart, 'RNG', RNG_val, 'ParticularModel', models_of_interest_V);
+%     os_temp(ind).mod_cue = ms;
+    
+    % remove spikeRate to avoid future confusion
+    os(ind) = rmfield(os_temp(ind), 'spikeRate');
+end
+
+fprintf('Finished\n')
+C:\Users\bilal\Dropbox\RPE Manuscript copy\Data\Modeling\ModelFits
+save(fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits', 'intBlocks_MLEfits_gauss.mat'),'os')

+ 32 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/gRWmodels_RPE/ott_gRW_RPE_base.m

@@ -0,0 +1,32 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_gRW_RPE_base(startValues, spikeRate, rewards, timeLocked)
+
+alphaLearn = startValues(1);
+slope = startValues(2);
+intercept = startValues(3);
+sigma = startValues(4);
+Vinit = 0.5;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+end
+rateParam = slope*RPE + intercept;
+probSpike = normpdf(spikeRate, rateParam(timeLocked), sigma); % normal distribution
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 37 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/gRWmodels_RPE/ott_gRW_RPE_base_asymm.m

@@ -0,0 +1,37 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_gRW_RPE_base_asymm(startValues, spikeRate, rewards, timeLocked)
+
+alphaPPE = startValues(1); % PPE learning rate
+alphaNPE = startValues(2); % NPE learning rate
+slope = startValues(3);
+intercept = startValues(4);
+sigma = startValues(5);
+Vinit = alphaPPE / (alphaPPE + alphaNPE);
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    if RPE(t) >= 0
+        V(t + 1) = V(t) + alphaPPE*RPE(t);
+    else
+        V(t + 1) = V(t) + alphaNPE*RPE(t);
+    end
+end
+rateParam = slope*RPE + intercept;
+probSpike = normpdf(spikeRate, rateParam(timeLocked), sigma); % normal distribution
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 20 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/gRWmodels_RPE/ott_gRW_RPE_curr.m

@@ -0,0 +1,20 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_gRW_RPE_curr(startValues, spikeRate, rewards, timeLocked)
+% firing rate only correlates with current reward; no real learning happens
+
+slope = startValues(1);
+intercept = startValues(2);
+sigma = startValues(3);
+
+rateParam = slope*rewards + intercept;
+probSpike = normpdf(spikeRate, rateParam(timeLocked), sigma); % normal distribution
+
+mean_predictedSpikes = rateParam(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end
+
+V = NaN;
+RPE = NaN;

+ 14 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/gRWmodels_RPE/ott_gRW_RPE_mean.m

@@ -0,0 +1,14 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_gRW_RPE_mean(startValues, spikeRate, rewards, timeLocked)
+
+probSpike = normpdf(spikeRate, mean(spikeRate), std(spikeRate));
+
+mean_predictedSpikes = mean(spikeRate);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end
+
+V = NaN;
+RPE = NaN;

+ 32 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/gRWmodels_value/ott_gRW_V_base.m

@@ -0,0 +1,32 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_gRW_V_base(startValues, spikeRate, rewards, timeLocked)
+
+alphaLearn = startValues(1);
+slope = startValues(2);
+intercept = startValues(3);
+sigma = startValues(4);
+Vinit = 0.5;
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+end
+rateParam = slope*V(1:trials) + intercept;
+probSpike = normpdf(spikeRate, rateParam(timeLocked), sigma); % normal distribution
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 37 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/gRWmodels_value/ott_gRW_V_base_asymm.m

@@ -0,0 +1,37 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_gRW_V_base_asymm(startValues, spikeRate, rewards, timeLocked)
+
+alphaPPE = startValues(1);
+alphaNPE = startValues(2);
+slope = startValues(3);
+intercept = startValues(4);
+sigma = startValues(5);
+Vinit = alphaPPE / (alphaPPE + alphaNPE);
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    if RPE(t) >= 0
+        V(t + 1) = V(t) + alphaPPE*RPE(t);
+    else
+        V(t + 1) = V(t) + alphaNPE*RPE(t);
+    end
+end
+rateParam = slope*V(1:trials) + intercept;
+probSpike = normpdf(spikeRate, rateParam(timeLocked), sigma); % normal distribution
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 14 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/gRWmodels_value/ott_gRW_V_mean.m

@@ -0,0 +1,14 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_gRW_V_mean(startValues, spikeRate, rewards, timeLocked)
+
+probSpike = normpdf(spikeRate, mean(spikeRate), std(spikeRate)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = mean(spikeRate);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end
+
+V = NaN;
+RPE = NaN;

+ 124 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/helper_gRW_RPE.m

@@ -0,0 +1,124 @@
+function ms = helper_gRW_RPE(os, varargin)
+% helper_RW_RPE    Fits neural models to test RW model predictions ~ RPE
+%   ms = helper_RW_RPE(os, varargin)
+%   INPUTS
+%       os: behavioral data structure
+%           .spikeRate: number of spikes in a particular time bin
+%           .rewards: 0 for maltodextrin, 1 for sucrose
+%           .timeLocked: 1 if the animal licked within 2s, 0 if the animal did not (logical)
+%               timeLocked will always be more than or equal to the number of spike trials
+%       varargin
+%           StartingPoints: determines how many points to optimize from
+%           ParticularModel: cell array of strings of models to use
+%           RNG: random number generator seed (default = 1)
+%   OUTPUTS
+%       ms: model structure of fits
+
+p = inputParser;
+p.addParameter('StartingPoints', 1)
+p.addParameter('ParticularModel', []);
+p.addParameter('RNG', []);
+p.parse(varargin{:});
+
+if ~isempty(p.Results.RNG)
+    rng(p.Results.RNG)
+end
+              
+% Initialize models
+if isempty(p.Results.ParticularModel)
+    modelNames = {'base_asym','base','curr','mean'};
+else
+    modelNames = p.Results.ParticularModel;
+end
+
+
+% Set up optimization problem
+options = optimset('Algorithm', 'interior-point','ObjectiveLimit',...
+    -1.000000000e+300,'TolFun',1e-15, 'Display','off');
+
+% set boundary conditions
+alpha_range = [0 1];
+slope_range = [0 20]; % reward sensitivity
+intercept_range = [-20 20]; % baseline spiking
+sigma_range = [0 2];
+
+for currMod = modelNames
+    currMod = currMod{:};
+    
+    % initialize output variables
+    runs = p.Results.StartingPoints;
+    LH = zeros(runs, 1);
+    
+    if strcmp(currMod, 'base')
+        paramNames = {'alpha','slope','intercept','sigma'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(sigma_range) + sigma_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  slope_range(2);  intercept_range(2);  sigma_range(2);
+           -alpha_range(1); -slope_range(1); -intercept_range(1); -sigma_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_gRW_RPE_base, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeRate, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_gRW_RPE_base(ms.(currMod).bestParams, os.spikeRate, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'base_asymm')
+        paramNames = {'alphaPPE','alphaNPE','slope','intercept','sigma'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ... % alphaPPE
+                       rand(runs, 1)*diff(alpha_range) + alpha_range(1) ... % alphaNPE
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(sigma_range) + sigma_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  alpha_range(2);  slope_range(2);  intercept_range(2);  sigma_range(2);
+           -alpha_range(1); -alpha_range(1); -slope_range(1); -intercept_range(1); -sigma_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_gRW_RPE_base_asymm, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeRate, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_gRW_RPE_base_asymm(ms.(currMod).bestParams, os.spikeRate, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'curr')
+        paramNames = {'slope','intercept','sigma'};
+        startValues = [rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(sigma_range) + sigma_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ slope_range(2);  intercept_range(2);  sigma_range(2);
+           -slope_range(1); -intercept_range(1); -sigma_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_gRW_RPE_curr, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeRate, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_gRW_RPE_curr(ms.(currMod).bestParams, os.spikeRate, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'mean')
+        paramNames = {''};
+        numParam = 0;        
+        [LH, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_gRW_RPE_mean([], os.spikeRate);
+        bestFit = 1;
+        ms.(currMod).bestParams = [];
+    else 
+        error('RW model: Model name not found')
+    end
+    ms.(currMod).paramNames = paramNames;
+    ms.(currMod).LH = -1 * LH(bestFit, :);
+    ms.(currMod).BIC = log(length(os.spikeRate))*numParam  - 2*ms.(currMod).LH;
+    ms.(currMod).AIC = 2*numParam - 2*ms.(currMod).LH;
+    ms.(currMod).AICc = ms.(currMod).AIC + (2*numParam^2 + 2*numParam)/(length(os.spikeRate) - numParam - 1);
+end

+ 119 - 0
MatlabScripts/2_Modeling/intBlocks/fit_model_gauss/helper_gRW_V.m

@@ -0,0 +1,119 @@
+function ms = helper_RW_V(os, varargin)
+% helper_RW_V    Fits neural models to test RW model predictions ~ value
+%   ms = helper_RW_V(os, varargin)
+%   INPUTS
+%       os: behavioral data structure
+%           .spikeCount: number of spikes in a particular time bin
+%           .rewards: 0 for maltodextrin, 1 for sucrose
+%           .timeLocked: 1 if the animal licked within 2s, 0 if the animal did not (logical)
+%               timeLocked will always be more than or equal to the number of spike trials
+%       varargin
+%           StartingPoints: determines how many points to optimize from
+%           ParticularModel: cell array of strings of models to use
+%           RNG: random number generator seed (default = 1)
+%   OUTPUTS
+%       ms: model structure of fits
+
+p = inputParser;
+p.addParameter('StartingPoints', 1)
+p.addParameter('ParticularModel', []);
+p.addParameter('RNG', []);
+p.parse(varargin{:});
+
+if ~isempty(p.Results.RNG)
+    rng(p.Results.RNG)
+end
+
+% Initialize models
+if isempty(p.Results.ParticularModel)
+    modelNames = {'base','prev','mean'};
+else
+    modelNames = p.Results.ParticularModel;
+end
+              
+% Set up optimization problem
+options = optimset('Algorithm', 'interior-point','ObjectiveLimit',...
+    -1.000000000e+300,'TolFun',1e-15, 'Display','off');
+
+% set boundary conditions
+alpha_range = [0 1];
+slope_range = [0 20]; % reward sensitivity
+intercept_range = [-20 20]; % baseline spiking
+
+for currMod = modelNames
+    currMod = currMod{:};
+    
+    % initialize output variables
+    runs = p.Results.StartingPoints;
+    LH = zeros(runs, 1);
+    
+    if strcmp(currMod, 'base')
+        paramNames = {'alpha','slope','intercept'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  slope_range(2);  intercept_range(2);
+           -alpha_range(1); -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_V_base, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_base(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'base_asymm')
+        paramNames = {'alphaPPE','alphaNPE','slope','intercept'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  alpha_range(2);  slope_range(2);  intercept_range(2);
+           -alpha_range(1); -alpha_range(1); -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_V_base_asymm, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_base_asymm(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'prev')
+        paramNames = {'slope','intercept'};
+        startValues = [rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ slope_range(2);  intercept_range(2);
+           -slope_range(1); -intercept_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_V_prev, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_prev(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+   elseif strcmp(currMod, 'mean')
+        paramNames = {''};
+        numParam = 0;        
+        [LH, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_mean([], os.spikeCount);
+        bestFit = 1;
+        ms.(currMod).bestParams = [];
+    else 
+        error('RW model: Model name not found')
+    end
+    ms.(currMod).paramNames = paramNames;
+    ms.(currMod).LH = -1 * LH(bestFit, :);
+    ms.(currMod).BIC = log(length(os.spikeCount))*numParam  - 2*ms.(currMod).LH;
+    ms.(currMod).AIC = 2*numParam - 2*ms.(currMod).LH;
+    ms.(currMod).AICc = ms.(currMod).AIC + (2*numParam^2 + 2*numParam)/(length(os.spikeCount) - numParam - 1);
+end

+ 32 - 0
MatlabScripts/2_Modeling/intBlocks/scripts/adaptationModel.m

@@ -0,0 +1,32 @@
+clear; clc
+load(fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits', 'intBlocks_MLEfits.mat'));
+int_task = [os.Blocks] == 0;
+os = os(int_task);
+VP_mask = contains({os.Region}, 'VP');
+os = os(VP_mask);
+
+%% get relevant behavior models
+modelCriterion = 'AIC';
+plotFlag = true;
+
+models_of_interest_RPE = {'base','adapt','habit','curr','mean'};
+
+timePeriod = 'RD';
+bm_RD = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,...
+                          'plotModels_Flag',plotFlag,...
+                          'particularModel',models_of_interest_RPE);
+
+
+%% pValues
+nTot = numel(bm_RD.mask_adapt);
+nBase = sum(bm_RD.mask_base);
+nAdapt = sum(bm_RD.mask_adapt);
+nHabit = sum(bm_RD.mask_habit);
+
+[~,pAdapt] = prop_test([nBase nAdapt],[nTot nTot]);
+[~,pHabit] = prop_test([nBase nHabit],[nTot nTot]);
+
+fprintf('\n------\n')
+fprintf('Base: %i\nAdapt: %i\nHabit: %i\nTotal: %i\n',nBase,nAdapt,nHabit,nTot);
+fprintf('pValue for adapt vs base: %0.2e\n', pAdapt);
+fprintf('pValue for habit vs base: %0.2e\n', pHabit);

+ 43 - 0
MatlabScripts/2_Modeling/intBlocks/scripts/overlap_cueV_rdRPE.m

@@ -0,0 +1,43 @@
+clear; clc
+load(fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits', 'intBlocks_MLEfits.mat'));
+int_task = [os.Blocks] == 0;
+os = os(int_task);
+VP_mask = contains({os.Region}, 'VP');
+os = os(VP_mask);
+myColors = importColors_bb;
+VP_color = myColors.bluishGreen;
+NAc_color = myColors.vermillion;
+
+%% get relevant behavior models
+modelCriterion = 'AIC';
+plotFlag = false;
+
+models_of_interest_RPE = {'base','curr','mean'};
+models_of_interest_V = {'base','mean'};
+
+timePeriod = 'RD';
+bm_RD = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,...
+                          'plotModels_Flag',plotFlag,...
+                          'particularModel',models_of_interest_RPE);
+
+timePeriod = 'cue';
+bm_cue = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,...
+                           'plotModels_Flag',plotFlag,...
+                           'particularModel',models_of_interest_V);
+                       
+%%
+mask_cueV = bm_cue.mask_base;
+mask_rdRPE = bm_RD.mask_base;
+
+n_cueV_rdRPE = sum(mask_cueV(mask_rdRPE));
+n_cueV_NOTrdRPE = sum(mask_cueV(~mask_rdRPE));
+
+n_rdRPE = sum(mask_rdRPE);
+n_NOTrdRPE = sum(~mask_rdRPE);
+
+[~,p] = prop_test([n_cueV_rdRPE n_cueV_NOTrdRPE], [n_rdRPE n_NOTrdRPE]);
+
+fprintf('\n------\n')
+fprintf('%i of %i RPE neurons (%0.2f%%) have cue-value response\n', n_cueV_rdRPE, n_rdRPE, n_cueV_rdRPE/n_rdRPE*100);
+fprintf('%i of %i non-RPE neurons (%0.2f%%) have cue-value responses \n', n_cueV_NOTrdRPE, n_NOTrdRPE, n_cueV_NOTrdRPE/n_NOTrdRPE*100);
+fprintf('pValue: %0.2e\n', p)

+ 234 - 0
MatlabScripts/2_Modeling/intBlocks/scripts/permutationTest.m

@@ -0,0 +1,234 @@
+clear; clc
+model_root = fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits');
+task = 'intBlocks';
+load(fullfile(model_root, [task '_MLEfits.mat']))
+os_int = os;
+clear os
+int_task = [os_int.Blocks] == 0;
+os_int = os_int(int_task);
+VP_mask = contains({os_int.Region}, 'VP');
+os_int = os_int(VP_mask); % only look at VP neurons
+
+task = 'threeOutcomes';
+load(fullfile(model_root, [task '_MLEfits.mat']))
+os_three = os;
+clear os
+
+task = 'cue';
+load(fullfile(model_root, [task '_MLEfits.mat']))
+os_cue = os;
+clear os
+
+%% 
+% figure out which sessions are unique
+timePeriod = 'RD'; % cue, RD
+switch timePeriod
+    case 'RD'
+        latentVar = 'RPEs';
+        spOfInt = 'spikeCount_RD';
+        mOfInt = 'mod_RD';
+        models_of_interest_int_three = {'base','curr','mean'};
+        models_of_interest_cue = {'base','base_cue','curr','curr_cue','mean','mean_cue'};
+    case 'cue'
+        latentVar = 'V';
+        spOfInt = 'spikeCount_cue';
+        mOfInt = 'mod_cue';
+        models_of_interest_int_three = {'base','mean'};
+        models_of_interest_cue = {'base','base_cue','mean','mean_cue'};
+end
+
+nTrials = [];
+session = 1;
+os_int(1).session = 1;
+nTrials = [nTrials length(os_int(1).(spOfInt))];
+for i = 2:length(os_int)
+    nTrials = [nTrials length(os_int(i).(spOfInt))];
+    if length(os_int(i).rewards) ~= length(os_int(i-1).rewards) % different number of trials
+        session = session + 1;
+    elseif ~all(os_int(i).rewards == os_int(i-1).rewards) % same trials but different rewards
+        session = session + 1;
+    end
+    os_int(i).session = session;
+end
+session = session + 1;
+os_three(1).session = session;
+nTrials = [nTrials length(os_three(1).(spOfInt))];
+for i = 2:length(os_three)
+    nTrials = [nTrials length(os_three(i).(spOfInt))];
+    if length(os_three(i).rewards) ~= length(os_three(i-1).rewards) % different number of trials
+        session = session + 1;
+    elseif ~all(os_three(i).rewards == os_three(i-1).rewards) % same trials but different rewards
+        session = session + 1;
+    end
+    os_three(i).session = session;
+end
+session = session + 1;
+os_cue(1).session = session;
+nTrials = [nTrials length(os_cue(1).(spOfInt))];
+for i = 2:length(os_cue)
+    nTrials = [nTrials length(os_cue(i).(spOfInt))];
+    if length(os_cue(i).rewards) ~= length(os_cue(i-1).rewards) % different number of trials
+        session = session + 1;
+    elseif ~all(os_cue(i).rewards == os_cue(i-1).rewards) % same trials but different rewards
+        session = session + 1;
+    end
+    os_cue(i).session = session;
+end
+
+modelCriterion = 'AIC';
+plotFlag = false;
+bm_int = select_RPEmods(os_int, timePeriod,'scoreToUse',modelCriterion,...
+                          'plotModels_Flag',plotFlag,...
+                          'particularModel',models_of_interest_int_three);
+bm_three = select_RPEmods(os_three, timePeriod,'scoreToUse',modelCriterion,...
+                          'plotModels_Flag',plotFlag,...
+                          'particularModel',models_of_interest_int_three);
+bm_cue = select_RPEmods(os_cue,timePeriod,'scoreToUse',modelCriterion,...
+                          'plotModels_Flag',plotFlag,...
+                          'particularModel',models_of_interest_cue);
+
+% generate permutation structure to make life easier
+permStruct = struct();
+nInd = 1;
+for n = 1:length(os_int)
+    session = ['s' int2str(os_int(n).session)];
+    neuron = ['n' int2str(nInd)];
+    if bm_int.mask_base(n) == 1 % if this is an RPE neuron
+        permStruct.(session).(neuron).task = 'int';
+        permStruct.(session).(neuron).spikes = os_int(n).(spOfInt);
+        permStruct.(session).(neuron).(latentVar) = os_int(n).(mOfInt).base.(latentVar);
+
+        nInd = nInd + 1;
+    end
+end
+for n = 1:length(os_three)
+    session = ['s' int2str(os_three(n).session)];
+    neuron = ['n' int2str(nInd)];
+    if bm_three.mask_base(n) == 1 % if this is an RPE neuron
+        permStruct.(session).(neuron).task = 'three';
+        permStruct.(session).(neuron).spikes = os_three(n).(spOfInt);
+        permStruct.(session).(neuron).(latentVar) = os_three(n).(mOfInt).base.(latentVar);
+
+        nInd = nInd + 1;
+    end
+end
+for n = 1:length(os_cue)
+    session = ['s' int2str(os_cue(n).session)];
+    neuron = ['n' int2str(nInd)];
+    if bm_cue.mask_base(n) == 1 % if this is an RPE neuron
+        permStruct.(session).(neuron).task = 'cue';
+        permStruct.(session).(neuron).spikes = os_cue(n).(spOfInt);
+        permStruct.(session).(neuron).(latentVar) = os_cue(n).(mOfInt).base.(latentVar);
+
+        nInd = nInd + 1;
+    elseif bm_cue.mask_base_cue(n) == 1 % if this is an RPE neuron w/ cue response
+        permStruct.(session).(neuron).task = 'cue';
+        permStruct.(session).(neuron).spikes = os_cue(n).(spOfInt);
+        permStruct.(session).(neuron).(latentVar) = os_cue(n).(mOfInt).base_cue.(latentVar);
+
+        nInd = nInd + 1;
+    end
+end
+
+%% generate distribution of RPEs to generate t-statistics
+all_s = fields(permStruct)';
+maxTrials = min(nTrials);
+for s_ind = 1:length(all_s)
+    s = all_s{s_ind};
+    fprintf('On session %s\n',s)
+    all_n = fields(permStruct.(s))';
+    % go through each neuron
+    for n = all_n
+        n = n{:};
+        
+        % generate RPEmat for other neurons; used for t-stat
+        latentMat = [];
+        s_exclude_list = all_s;
+        s_exclude_list(s_ind) = [];
+        for s_other = s_exclude_list
+            s_other = s_other{:};
+            
+            all_n_other = fields(permStruct.(s_other))';
+            rand_n_ind = randi(length(all_n_other)); % select a random neuron
+            latentMat = [latentMat; permStruct.(s_other).(all_n_other{rand_n_ind}).(latentVar)(1:maxTrials)'];
+        end
+        permStruct.(s).(n).([latentVar 'mat']) = latentMat;
+        
+        % generate a neuron-specific t-distribution
+        tDist = [];
+        for t_ind = 1:size(permStruct.(s).(n).([latentVar 'mat']), 1)
+            fr = normalize(permStruct.(s).(n).spikes(1:maxTrials));
+            tDist_lm = fitlm(fr, permStruct.(s).(n).([latentVar 'mat'])(t_ind,:));
+            tDist = [tDist tDist_lm.Coefficients.tStat(2)];
+        end
+        tReal_lm = fitlm(fr, permStruct.(s).(n).(latentVar)(1:maxTrials));
+        tReal = tReal_lm.Coefficients.tStat(2);
+        permStruct.(s).(n).tDist = tDist;
+        permStruct.(s).(n).tReal = tReal;
+    end
+end
+
+%% determine significant neurons
+pCutoff = 0.05;
+pCutoff = pCutoff * 100;
+all_sig_n = [];
+for s = all_s
+    s = s{:};
+    for n = fields(permStruct.(s))'
+        n = n{:};
+        
+        % determine empirical significance
+        threshold = prctile(permStruct.(s).(n).tDist, [pCutoff/2 100-pCutoff/2]);
+        if permStruct.(s).(n).tReal >= 0
+            if permStruct.(s).(n).tReal > threshold(2)
+                all_sig_n = [all_sig_n true];
+            else
+                all_sig_n = [all_sig_n false];
+            end
+        else
+            if permStruct.(s).(n).tReal < threshold(1)
+                all_sig_n = [all_sig_n true];
+            else
+                all_sig_n = [all_sig_n false];
+            end
+        end
+    end
+end
+
+fprintf('%i of %i neurons (%0.1f%%) are significant\n', ...
+        sum(all_sig_n), length(all_sig_n), 100*mean(all_sig_n))
+% 180 of 196 neurons (91.8%) are significant
+
+%% how correlated is the latent variable of one session with others
+all_corr = {};
+noise_corr = {};
+for s = all_s
+    s = s{:};
+    all_n = fields(permStruct.(s))';
+    
+    for n = all_n
+        n = n{:};
+        
+        v1 = permStruct.(s).(n).(latentVar)(1:maxTrials);
+        v2 = permStruct.(s).(n).([latentVar 'mat'])';
+        all_corr = [all_corr {corr(v1, v2)}];
+        
+        v1_noise = normrnd(0,1,size(v1));
+        v2_noise = normrnd(0,1,size(v2));
+        noise_corr = [noise_corr {corr(v1_noise, v2_noise)}];
+    end
+end
+
+real_median = cellfun(@(i) median(abs(i)), all_corr);
+noise_median = cellfun(@(i) median(abs(i)), noise_corr);
+
+real_CI = bootci(1e3, @median, real_median);
+noise_CI = bootci(1e3, @median, noise_median);
+
+fprintf('Real: median %0.3f (%0.3f - %0.3f)\n', median(real_median), real_CI(1), real_CI(2));
+fprintf('Noise: median %0.3f (%0.3f - %0.3f)\n', median(noise_median), noise_CI(1), noise_CI(2));
+
+% RPE neurons
+% Real: median 0.114 (0.109 - 0.118)
+% Noise: median 0.106 (0.102 - 0.109)
+

+ 282 - 0
MatlabScripts/2_Modeling/intBlocks/scripts/recover_RW_MLE_sim.m

@@ -0,0 +1,282 @@
+clear os_RPE_temp os_V_temp os_RPE os_V
+
+nStart = 10;
+
+nTrials = 55; % median number of trials
+nNeurons = 200;
+Vinit = 0.5;
+
+m_RPE = {'base_asymm','base','curr','mean'};
+m_V = {'base_asymm','base','mean'};
+
+
+for ind = 1:nNeurons % for all neurons
+    fprintf('n %i of %i\n', ind, nNeurons)
+    
+    V = NaN(nTrials + 1, 1);
+    RPE = NaN(nTrials, 1);
+    Vasymm = NaN(nTrials + 1, 1);
+    RPEasymm = NaN(nTrials, 1);
+    rwd = binornd(1, 0.5, nTrials, 1);
+    rwd_prev = [0; rwd(1:end - 1)];
+
+    alpha = rand; % 0 to 1
+    alphaPPE = rand; % 0 to 1
+    alphaNPE = rand; % 0 to 1
+    slope = rand*3 + 1; % 1 to 4
+    int = rand*10 - 5; % -5 to 5
+    
+    V(1) = Vinit;
+    Vasymm(1) = Vinit;
+    for t = 1:nTrials
+        RPE(t) = rwd(t) - V(t);
+        V(t + 1) = V(t) + alpha*RPE(t);
+        
+        RPEasymm(t) = rwd(t) - Vasymm(t);
+        if RPEasymm(t) >= 0
+            Vasymm(t + 1) = Vasymm(t) + alphaPPE*RPEasymm(t);
+        else
+            Vasymm(t + 1) = Vasymm(t) + alphaNPE*RPEasymm(t);
+        end
+    end
+    
+    % simulate RD neurons
+    sp_RPEasymm = poissrnd(exp(slope*RPEasymm + int));
+    sp_RPE = poissrnd(exp(slope*RPE + int));
+    sp_curr = poissrnd(exp(slope*rwd + int));
+    
+    % simulate cue neurons
+    sp_Vasymm = poissrnd(exp(slope*Vasymm(1:nTrials) + int));
+    sp_V = poissrnd(exp(slope*V(1:nTrials) + int));
+    sp_prev = poissrnd(exp(slope*rwd_prev + int));
+    
+    % simulate mean neurons (both for RD and cue)
+    sp_mean = poissrnd(exp(int), nTrials, 1);
+    
+    
+    % RD neurons
+    os_RPE_temp(ind).params.alpha = alpha;
+    os_RPE_temp(ind).params.alphaPPE = alphaPPE;
+    os_RPE_temp(ind).params.alphaNPE = alphaNPE;
+    os_RPE_temp(ind).params.slope = slope;
+    os_RPE_temp(ind).params.int = int;   
+    
+    os_RPE_temp(ind).rewards = rwd;
+    os_RPE_temp(ind).timeLocked = true(size(rwd));
+    os_RPE_temp(ind).spikeCount_RPEasymm = sp_RPEasymm;
+    os_RPE_temp(ind).spikeCount_RPE = sp_RPE;
+    os_RPE_temp(ind).spikeCount_curr = sp_curr;
+    os_RPE_temp(ind).spikeCount_mean = sp_mean;
+   
+    
+    % spikeCount is a temporary field
+    % fit RPE
+    os_RPE_temp(ind).spikeCount = os_RPE_temp(ind).spikeCount_RPEasymm;
+    ms = helper_RW_RPE(os_RPE_temp(ind), 'StartingPoints', nStart, 'particularModel', m_RPE);
+    os_RPE_temp(ind).mod_RPEasymm = ms;
+    
+    os_RPE_temp(ind).spikeCount = os_RPE_temp(ind).spikeCount_RPE;
+    ms = helper_RW_RPE(os_RPE_temp(ind), 'StartingPoints', nStart, 'particularModel', m_RPE);
+    os_RPE_temp(ind).mod_RPE = ms;
+    
+    os_RPE_temp(ind).spikeCount = os_RPE_temp(ind).spikeCount_curr;
+    ms = helper_RW_RPE(os_RPE_temp(ind), 'StartingPoints', nStart, 'particularModel', m_RPE);
+    os_RPE_temp(ind).mod_curr = ms;
+    
+    os_RPE_temp(ind).spikeCount = os_RPE_temp(ind).spikeCount_mean;
+    ms = helper_RW_RPE(os_RPE_temp(ind), 'StartingPoints', nStart, 'particularModel', m_RPE);
+    os_RPE_temp(ind).mod_mean = ms;
+    
+    % cue neurons
+    os_V_temp(ind).params.alpha = alpha;
+    os_V_temp(ind).params.alphaPPE = alphaPPE;
+    os_V_temp(ind).params.alphaNPE = alphaNPE;
+    os_V_temp(ind).params.slope = slope;
+    os_V_temp(ind).params.int = int;   
+    
+    os_V_temp(ind).rewards = rwd;
+    os_V_temp(ind).timeLocked = true(size(rwd));
+    os_V_temp(ind).spikeCount_Vasymm = sp_Vasymm;
+    os_V_temp(ind).spikeCount_V = sp_V;
+    os_V_temp(ind).spikeCount_prev = sp_prev;
+    os_V_temp(ind).spikeCount_mean = sp_mean;
+    % spikeCount is a temporary field
+    % fit V
+    os_V_temp(ind).spikeCount = os_V_temp(ind).spikeCount_Vasymm;
+    ms = helper_RW_V(os_V_temp(ind), 'StartingPoints', nStart, 'particularModel', m_V);
+    os_V_temp(ind).mod_Vasymm = ms;
+    
+    os_V_temp(ind).spikeCount = os_V_temp(ind).spikeCount_V;
+    ms = helper_RW_V(os_V_temp(ind), 'StartingPoints', nStart, 'particularModel', m_V);
+    os_V_temp(ind).mod_V = ms;
+    
+    os_V_temp(ind).spikeCount = os_V_temp(ind).spikeCount_prev;
+    ms = helper_RW_V(os_V_temp(ind), 'StartingPoints', nStart, 'particularModel', m_V);
+    os_V_temp(ind).mod_prev = ms;
+    
+    os_V_temp(ind).spikeCount = os_V_temp(ind).spikeCount_mean;
+    ms = helper_RW_V(os_V_temp(ind), 'StartingPoints', nStart, 'particularModel', m_V);
+    os_V_temp(ind).mod_mean = ms;
+    
+    % remove spikeCount to avoid future confusion
+    os_RPE(ind) = rmfield(os_RPE_temp(ind), 'spikeCount');
+    os_V(ind) = rmfield(os_V_temp(ind), 'spikeCount');
+end
+
+fprintf('Finished\n')
+save(fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits', ...
+              'intBlocks_MLEfits_simulated_offSim.mat'), ...
+    'os_RPE', 'os_V');
+
+%%
+load(fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits', ...
+              'intBlocks_MLEfits_simulated_offSim.mat'))
+scoreToUse = 'AIC';
+plotModels_Flag = true;
+m_RPE = {'base','curr','mean'};
+m_V = {'base','mean'};
+
+bm_RPE_aRPE = select_RPEmods(os_RPE, 'RPE', 'scoreToUse', scoreToUse, ...
+                                'plotModels_Flag', plotModels_Flag, 'particularModels', m_RPE);
+bm_RPE_acurr = select_RPEmods(os_RPE, 'curr', 'scoreToUse', scoreToUse, ...
+                                'plotModels_Flag', plotModels_Flag, 'particularModels', m_RPE);
+bm_RPE_aamean = select_RPEmods(os_RPE, 'mean', 'scoreToUse', scoreToUse, ...
+                                'plotModels_Flag', plotModels_Flag, 'particularModels', m_RPE);
+                    
+bm_V_aV = select_RPEmods(os_V, 'V', 'particularModels', m_V, 'scoreToUse', scoreToUse, ...
+                             'plotModels_Flag', plotModels_Flag);
+bm_V_amean = select_RPEmods(os_V, 'mean', 'particularModels', m_V, 'scoreToUse', scoreToUse, ...
+                             'plotModels_Flag', plotModels_Flag);
+                         
+myColors = importColors_bb;
+
+%%
+% RPE first
+model = 'V';
+switch model
+    case 'RPE'
+        m1 = bm_RPE_aRPE;
+        m2 = bm_RPE_acurr;
+        m3 = bm_RPE_aamean;
+        mod2 = 'curr';
+        nNeurons = length(os_RPE);
+    case 'V'
+        m1 = bm_V_aV;
+        m2 = bm_V_amean;
+        nNeurons = length(os_V);
+    otherwise
+        error('model not found')
+end
+
+switch model
+    case 'RPE'
+        aBase_rBase = sum(m1.mask_base)/nNeurons;
+        aBase_rMod2 = sum(m1.(['mask_' mod2]))/nNeurons;
+        aBase_rMean = sum(m1.mask_mean)/nNeurons;
+
+        aMod2_rBase = sum(m2.mask_base)/nNeurons;
+        aMod2_rMod2 = sum(m2.(['mask_' mod2]))/nNeurons;
+        aMod2_rMean = sum(m2.mask_mean)/nNeurons;
+
+        aMean_rBase = sum(m3.mask_base)/nNeurons;
+        aMean_rMod2 = sum(m3.(['mask_' mod2]))/nNeurons;
+        aMean_rMean = sum(m3.mask_mean)/nNeurons;
+
+        mat_for_hmap = [aBase_rBase      aBase_rMod2      aBase_rMean;
+                        aMod2_rBase      aMod2_rMod2      aMod2_rMean;
+                        aMean_rBase      aMean_rMod2      aMean_rMean];
+
+        h_heatmap = figure;
+        axisLabel = {'Base',mod2,'Mean'};
+    case 'V'
+
+        aBase_rBase = sum(m1.mask_base)/nNeurons;
+        aBase_rMean = sum(m1.mask_mean)/nNeurons;
+
+        aMod2_rBase = sum(m2.mask_base)/nNeurons;
+        aMod2_rMean = sum(m2.mask_mean)/nNeurons;
+
+        aMean_rBase = sum(m3.mask_base)/nNeurons;
+        aMean_rMean = sum(m3.mask_mean)/nNeurons;
+
+        mat_for_hmap = [aBase_rBase      aBase_rMean;
+                        aMean_rBase      aMean_rMean];
+
+        h_heatmap = figure;
+        axisLabel = {'Base',mod2,'Mean'};
+end
+cmap_toUse = cmap_customColors(64, 'whiteBlue');
+[hImage, hText, hTick] = heatmap_AD(mat_for_hmap, axisLabel, axisLabel, '%0.2f', ...
+    'Colormap', cmap_toUse, ...
+    'ShowAllTicks', true, ...
+    'UseFigureColormap', false, ...
+    'Colorbar', true, ...
+    'FontSize', 10, ...
+    'MinColorValue', 0, ...
+    'MaxColorValue', 1, ...
+    'GridLines', '-');
+xlabel('Recovered model')
+ylabel('True model')
+set(gca,'tickdir','out')
+title(model)
+
+username = getenv('USERNAME');
+
+%% recover parameters
+param_struct = struct();
+
+% RPE neurons
+n_RPE = os_RPE(bm_RPE_aRPE.mask_base);
+param_struct.RPE.alpha.actual = [];
+param_struct.RPE.alpha.recovered = [];
+param_struct.RPE.slope.actual = [];
+param_struct.RPE.slope.recovered = [];
+param_struct.RPE.int.actual = [];
+param_struct.RPE.int.recovered = [];
+
+for n = 1:length(n_RPE)
+    % alpha
+    param_struct.RPE.alpha.actual = [param_struct.RPE.alpha.actual ...
+                                             n_RPE(n).params.alpha];
+    param_struct.RPE.alpha.recovered = [param_struct.RPE.alpha.recovered ...
+                                                n_RPE(n).mod_RPE.base.bestParams(1)];
+    % slope
+    param_struct.RPE.slope.actual = [param_struct.RPE.slope.actual ...
+                                             n_RPE(n).params.slope];
+    param_struct.RPE.slope.recovered = [param_struct.RPE.slope.recovered ...
+                                                n_RPE(n).mod_RPE.base.bestParams(2)];
+    % slope
+    param_struct.RPE.int.actual = [param_struct.RPE.int.actual ...
+                                             n_RPE(n).params.int];
+    param_struct.RPE.int.recovered = [param_struct.RPE.int.recovered ...
+                                                n_RPE(n).mod_RPE.base.bestParams(3)];
+end
+
+% plot it
+binEdges = -1.1:0.2:1.1;
+
+h_paramRecovery = figure;
+h(1) = subplot(131); hold on
+rec_alpha = param_struct.RPE.alpha.actual - param_struct.RPE.alpha.recovered;
+histogram(rec_alpha, binEdges, 'EdgeColor','none','normalization','probability')
+xlabel('$\alpha$ (actual - recovered)','interpreter','latex')
+
+h(2) = subplot(132); hold on
+rec_slope = param_struct.RPE.slope.actual - param_struct.RPE.slope.recovered;
+histogram(rec_slope, binEdges, 'EdgeColor','none','normalization','probability')
+xlabel('slope (actual - recovered)','interpreter','latex')
+
+h(3) = subplot(133); hold on
+rec_int = param_struct.RPE.int.actual - param_struct.RPE.int.recovered;
+histogram(rec_int, binEdges, 'EdgeColor','none','normalization','probability')
+xlabel('intercept (actual - recovered)','interpreter','latex')
+
+for curr_h = h
+    subplot(curr_h)
+    ylim_range = get(curr_h, 'YLim');
+    plot([0 0],ylim_range,'--','Color', myColors.gray)
+    ylabel('Probability')
+    set(curr_h,'tickdir','out')
+end
+
+saveFigureIteration_ottBari2019(h_paramRecovery, saveLoc, 'recovery_paramBias','FigureSize','max')

+ 41 - 0
MatlabScripts/2_Modeling/threeOutcomes/fit_model/RWmodels_RPE/ott_RW_RPE_base.m

@@ -0,0 +1,41 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_base(startValues, spikeCounts, rewards, timeLocked)
+
+% reward is 0: mal, 1: suc, 2: water
+
+alphaLearn = startValues(1);
+slope = startValues(2);
+intercept = startValues(3);
+rho = startValues(4); % how valuable is maltodextrin on a water -> malto -> sucrose scale
+Vinit = (1 + 0 + rho)/3;
+
+water_ind = rewards == 2;
+mal_ind = rewards == 0;
+rewards(water_ind) = 0;
+rewards(mal_ind) = rho; % scale mal between 0 and 1
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+end
+rateParam = exp(slope*RPE + intercept);
+rateParam(rateParam < 0) = 0.1; % set rate param to zero if it goes below; might consider a better rule in the future
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 46 - 0
MatlabScripts/2_Modeling/threeOutcomes/fit_model/RWmodels_RPE/ott_RW_RPE_base_asymm.m

@@ -0,0 +1,46 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_base_asymm(startValues, spikeCounts, rewards, timeLocked)
+
+% reward is 0: mal, 1: suc, 2: water
+
+alphaPPE = startValues(1);
+alphaNPE = startValues(2);
+slope = startValues(3);
+intercept = startValues(4);
+rho = startValues(5); % how valuable is maltodextrin on a water -> malto -> sucrose scale
+Vinit = 2/3*(alphaPPE / (alphaPPE + alphaNPE)) + 1/3*rho;
+
+water_ind = rewards == 2;
+mal_ind = rewards == 0;
+rewards(water_ind) = 0;
+rewards(mal_ind) = rho; % scale mal between 0 and 1
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    if RPE(t) >= 0
+        V(t + 1) = V(t) + alphaPPE*RPE(t);
+    else
+        V(t + 1) = V(t) + alphaNPE*RPE(t);
+    end
+end
+rateParam = exp(slope*RPE + intercept);
+rateParam(rateParam < 0) = 0.1; % set rate param to zero if it goes below; might consider a better rule in the future
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 28 - 0
MatlabScripts/2_Modeling/threeOutcomes/fit_model/RWmodels_RPE/ott_RW_RPE_curr.m

@@ -0,0 +1,28 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_curr(startValues, spikeCounts, rewards, timeLocked)
+% firing rate only correlates with current reward; no real learning happens
+
+% reward is 0: mal, 1: suc, 2: water
+
+slope = startValues(1);
+intercept = startValues(2);
+rho = startValues(3); % how valuable is maltodextrin on a water -> malto -> sucrose scale
+
+water_ind = rewards == 2;
+mal_ind = rewards == 0;
+rewards(water_ind) = 0;
+rewards(mal_ind) = rho; % scale mal between 0 and 1
+
+rateParam = exp(slope*rewards + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end
+
+V = NaN;
+RPE = NaN;

+ 15 - 0
MatlabScripts/2_Modeling/threeOutcomes/fit_model/RWmodels_RPE/ott_RW_RPE_mean.m

@@ -0,0 +1,15 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_RPE_mean(startValues, spikeCounts, rewards, timeLocked)
+
+
+probSpike = poisspdf(spikeCounts, mean(spikeCounts)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = mean(spikeCounts);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end
+
+V = NaN;
+RPE = NaN;

+ 41 - 0
MatlabScripts/2_Modeling/threeOutcomes/fit_model/RWmodels_value/ott_RW_V_base.m

@@ -0,0 +1,41 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_V_base(startValues, spikeCounts, rewards, timeLocked)
+
+% reward is 0: mal, 1: suc, 2: water
+
+alphaLearn = startValues(1);
+slope = startValues(2);
+intercept = startValues(3);
+rho = startValues(4); % how valuable is maltodextrin on a water -> malto -> sucrose scale
+Vinit = (1 + 0 + rho)/3;
+
+water_ind = rewards == 2;
+mal_ind = rewards == 0;
+rewards(water_ind) = 0;
+rewards(mal_ind) = rho; % scale mal between 0 and 1
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+end
+rateParam = exp(slope*V(1:trials) + intercept);
+rateParam(rateParam < 0) = 0.1; % set rate param to zero if it goes below; might consider a better rule in the future
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 46 - 0
MatlabScripts/2_Modeling/threeOutcomes/fit_model/RWmodels_value/ott_RW_V_base_asymm.m

@@ -0,0 +1,46 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_V_base_asymm(startValues, spikeCounts, rewards, timeLocked)
+
+% reward is 0: mal, 1: suc, 2: water
+
+alphaPPE = startValues(1);
+alphaNPE = startValues(2);
+slope = startValues(3);
+intercept = startValues(4);
+rho = startValues(5); % how valuable is maltodextrin on a water -> malto -> sucrose scale
+Vinit = 2/3*(alphaPPE / (alphaPPE + alphaNPE)) + 1/3*rho;
+
+water_ind = rewards == 2;
+mal_ind = rewards == 0;
+rewards(water_ind) = 0;
+rewards(mal_ind) = rho; % scale mal between 0 and 1
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    if RPE(t) >= 0
+        V(t + 1) = V(t) + alphaPPE*RPE(t);
+    else
+        V(t + 1) = V(t) + alphaNPE*RPE(t);
+    end
+end
+rateParam = exp(slope*V(1:trials) + intercept);
+rateParam(rateParam < 0) = 0.1; % set rate param to zero if it goes below; might consider a better rule in the future
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 15 - 0
MatlabScripts/2_Modeling/threeOutcomes/fit_model/RWmodels_value/ott_RW_V_mean.m

@@ -0,0 +1,15 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_V_mean(startValues, spikeCounts, rewards, timeLocked)
+
+
+probSpike = poisspdf(spikeCounts, mean(spikeCounts)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = mean(spikeCounts);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end
+
+V = NaN;
+RPE = NaN;

+ 39 - 0
MatlabScripts/2_Modeling/threeOutcomes/fit_model/RWmodels_value/ott_RW_V_prev.m

@@ -0,0 +1,39 @@
+function [LH, probSpike, V, mean_predictedSpikes, RPE] = ott_RW_V_prev(startValues, spikeCounts, rewards, timeLocked)
+
+slope = startValues(1);
+intercept = startValues(2);
+rho = startValues(3); % how valuable is maltodextrin on a water -> malto -> sucrose scale
+
+alphaLearn = 1; % value just reflects previous trial outcome
+Vinit = 0.5;
+
+water_ind = rewards == 2;
+mal_ind = rewards == 0;
+rewards(water_ind) = 0;
+rewards(mal_ind) = rho; % scale mal between 0 and 1
+
+trials = length(rewards);
+V = zeros(trials + 1, 1);
+RPE = zeros(trials, 1);
+
+V(1) = Vinit;
+% Call learning rule
+for t = 1:trials
+
+    RPE(t) = rewards(t) - V(t);
+    V(t + 1) = V(t) + alphaLearn*RPE(t);
+end
+rateParam = exp(slope*V(1:trials) + intercept);
+
+probSpike = poisspdf(spikeCounts, rateParam(timeLocked)); % mask rateParam to exclude trials where the animal didn't lick fast enough
+
+mean_predictedSpikes = rateParam(timeLocked);
+V = V(1:trials);
+V = V(timeLocked);
+RPE = RPE(timeLocked);
+
+if any(isinf(log(probSpike)))
+    LH = 1e9;
+else
+    LH = -1 * sum(log(probSpike));
+end

+ 41 - 0
MatlabScripts/2_Modeling/threeOutcomes/fit_model/fit_RW_MLE.m

@@ -0,0 +1,41 @@
+clear; clc
+task = 'threeOutcomes';
+dir_MLEfit_ott(task)
+ott_data = loadData_ott(task);
+
+nStart = 10;
+RNG_val = 1;
+%%
+clear os_temp os
+
+models_of_interest_RPE = {'base','base_asymm','curr','mean'};
+models_of_interest_V = {'base','base_asymm','mean'};
+
+all_fits = struct(); % initialize an empty structure 
+for ind = 1:length(ott_data.RDHz) % for all neurons
+    fprintf('n %i of %i\n', ind, length(ott_data.RDHz))
+    
+    os_temp(ind).spikeCount_RD = round(ott_data.RDHz{ind}*1.2); % RD period; 1.2s long
+    os_temp(ind).spikeCount_cue = round(ott_data.CueHz{ind}*0.75);
+    os_temp(ind).spikeCount_PE = ott_data.PEHz{ind};
+    
+    os_temp(ind).rewards = ott_data.AllTrials{ind}(:, 1); % 0 mal, 1 suc
+    os_temp(ind).timeLocked = logical(ott_data.AllTrials{ind}(:, 2)); % trials fast enough to have time-locked responses
+    
+    % spikeCount is a temporary field
+    % fit RD
+    os_temp(ind).spikeCount = os_temp(ind).spikeCount_RD;
+    ms = helper_RW_RPE(os_temp(ind), 'StartingPoints', nStart, 'RNG', RNG_val, 'ParticularModel', models_of_interest_RPE);
+    os_temp(ind).mod_RD = ms;
+    
+    % fit cue
+    os_temp(ind).spikeCount = os_temp(ind).spikeCount_cue;
+    ms = helper_RW_V(os_temp(ind), 'StartingPoints', nStart, 'RNG', RNG_val, 'ParticularModel', models_of_interest_V);
+    os_temp(ind).mod_cue = ms;
+    
+    % remove spikeCount to avoid future confusion
+    os(ind) = rmfield(os_temp(ind), 'spikeCount');
+end
+
+fprintf('Finished\n')
+save_MLEfit_ott(task, os);

+ 124 - 0
MatlabScripts/2_Modeling/threeOutcomes/fit_model/helper_RW_RPE.m

@@ -0,0 +1,124 @@
+function ms = helper_RW_RPE(os, varargin)
+% helper_RW_RPE    Fits neural models to test RW model predictions ~ RPE
+%   ms = helper_RW_RPE(os, varargin)
+%   INPUTS
+%       os: behavioral data structure
+%           .spikeCount: number of spikes in a particular time bin
+%           .rewards: 0 for maltodextrin, 1 for sucrose
+%           .timeLocked: 1 if the animal licked within 2s, 0 if the animal did not (logical)
+%               timeLocked will always be more than or equal to the number of spike trials
+%       varargin
+%           StartingPoints: determines how many points to optimize from
+%           ParticularModel: cell array of strings of models to use
+%           RNG: random number generator seed (default = 1)
+%   OUTPUTS
+%       ms: model structure of fits
+
+p = inputParser;
+p.addParameter('StartingPoints', 1)
+p.addParameter('ParticularModel', []);
+p.addParameter('RNG', []);
+p.parse(varargin{:});
+
+if ~isempty(p.Results.RNG)
+    rng(p.Results.RNG)
+end
+              
+% Initialize models
+if isempty(p.Results.ParticularModel)
+    modelNames = {'base', 'curr', 'mean'};
+else
+    modelNames = p.Results.ParticularModel;
+end
+
+
+% Set up optimization problem
+options = optimset('Algorithm', 'interior-point','ObjectiveLimit',...
+    -1.000000000e+300,'TolFun',1e-15, 'Display','off');
+
+% set boundary conditions
+alpha_range = [0 1];
+slope_range = [0 20]; % reward sensitivity
+intercept_range = [-20 20]; % baseline spiking
+rho_range = [0 1];
+
+for currMod = modelNames
+    currMod = currMod{:};
+    
+    % initialize output variables
+    runs = p.Results.StartingPoints;
+    LH = zeros(runs, 1);
+    
+    if strcmp(currMod, 'base')
+        paramNames = {'alpha','slope','intercept','rho'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(rho_range) + rho_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  slope_range(2);  intercept_range(2);  rho_range(2);
+           -alpha_range(1); -slope_range(1); -intercept_range(1); -rho_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_base, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_base(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'base_asymm')
+        paramNames = {'alphaPPE','alphaNPE','slope','intercept','rho'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(rho_range) + rho_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  alpha_range(2);  slope_range(2);  intercept_range(2);  rho_range(2);
+           -alpha_range(1); -alpha_range(1); -slope_range(1); -intercept_range(1); -rho_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_base_asymm, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_base_asymm(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'curr')
+        paramNames = {'slope','intercept','rho'};
+        startValues = [rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(rho_range) + rho_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ slope_range(2);  intercept_range(2);  rho_range(2);
+           -slope_range(1); -intercept_range(1); -rho_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_RPE_curr, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_curr(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'mean')
+        paramNames = {''};
+        numParam = 0;        
+        [LH, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_RPE_mean([], os.spikeCount);
+        bestFit = 1;
+        ms.(currMod).bestParams = [];
+    else 
+        error('RW model: Model name not found')
+    end
+    ms.(currMod).paramNames = paramNames;
+    ms.(currMod).LH = -1 * LH(bestFit, :);
+    ms.(currMod).BIC = log(length(os.spikeCount))*numParam  - 2*ms.(currMod).LH;
+    ms.(currMod).AIC = 2*numParam - 2*ms.(currMod).LH;
+    ms.(currMod).AICc = ms.(currMod).AIC + (2*numParam^2 + 2*numParam)/(length(os.spikeCount) - numParam - 1);
+end

+ 123 - 0
MatlabScripts/2_Modeling/threeOutcomes/fit_model/helper_RW_V.m

@@ -0,0 +1,123 @@
+function ms = helper_RW_V(os, varargin)
+% helper_RW_V    Fits neural models to test RW model predictions ~ value
+%   ms = helper_RW_V(os, varargin)
+%   INPUTS
+%       os: behavioral data structure
+%           .spikeCount: number of spikes in a particular time bin
+%           .rewards: 0 for maltodextrin, 1 for sucrose
+%           .timeLocked: 1 if the animal licked within 2s, 0 if the animal did not (logical)
+%               timeLocked will always be more than or equal to the number of spike trials
+%       varargin
+%           StartingPoints: determines how many points to optimize from
+%           ParticularModel: cell array of strings of models to use
+%           RNG: random number generator seed (default = 1)
+%   OUTPUTS
+%       ms: model structure of fits
+
+p = inputParser;
+p.addParameter('StartingPoints', 1)
+p.addParameter('ParticularModel', []);
+p.addParameter('RNG', []);
+p.parse(varargin{:});
+
+if ~isempty(p.Results.RNG)
+    rng(p.Results.RNG)
+end
+
+% Initialize models
+if isempty(p.Results.ParticularModel)
+    modelNames = {'base','prev','mean'};
+else
+    modelNames = p.Results.ParticularModel;
+end
+              
+% Set up optimization problem
+options = optimset('Algorithm', 'interior-point','ObjectiveLimit',...
+    -1.000000000e+300,'TolFun',1e-15, 'Display','off');
+
+% set boundary conditions
+alpha_range = [0 1];
+slope_range = [0 20]; % reward sensitivity
+intercept_range = [-20 20]; % baseline spiking
+rho_range = [0 1];
+
+for currMod = modelNames
+    currMod = currMod{:};
+    
+    % initialize output variables
+    runs = p.Results.StartingPoints;
+    LH = zeros(runs, 1);
+    
+    if strcmp(currMod, 'base')
+        paramNames = {'alpha','slope','intercept','rho'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(rho_range) + rho_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  slope_range(2);  intercept_range(2);  rho_range(2);
+           -alpha_range(1); -slope_range(1); -intercept_range(1); -rho_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_V_base, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_base(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'base_asymm')
+        paramNames = {'alphaPPE','alphaNPE','slope','intercept','rho'};
+        startValues = [rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(alpha_range) + alpha_range(1) ...
+                       rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(rho_range) + rho_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ alpha_range(2);  alpha_range(2);  slope_range(2);  intercept_range(2);  rho_range(2);
+           -alpha_range(1); -alpha_range(1); -slope_range(1); -intercept_range(1); -rho_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_V_base_asymm, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_base_asymm(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+    elseif strcmp(currMod, 'prev')
+        paramNames = {'slope','intercept','rho'};
+        startValues = [rand(runs, 1)*diff(slope_range) + slope_range(1) ...
+                       rand(runs, 1)*diff(intercept_range) + intercept_range(1) ...
+                       rand(runs, 1)*diff(rho_range) + rho_range(1)];
+        numParam = size(startValues, 2);
+        allParams = zeros(runs, numParam);
+        A=[eye(numParam); -eye(numParam)];
+        b=[ slope_range(2);  intercept_range(2);  rho_range(2);
+           -slope_range(1); -intercept_range(1); -rho_range(1)];
+        parfor r = 1:runs
+            [allParams(r, :), LH(r, :)] = ...
+                fmincon(@ott_RW_V_prev, startValues(r, :), A, b, [], [], [], [], [], options, os.spikeCount, os.rewards, os.timeLocked);
+        end
+        [~, bestFit] = min(LH);
+        ms.(currMod).bestParams = allParams(bestFit, :);
+        [~, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_prev(ms.(currMod).bestParams, os.spikeCount, os.rewards, os.timeLocked);
+   elseif strcmp(currMod, 'mean')
+        paramNames = {''};
+        numParam = 0;        
+        [LH, ms.(currMod).probSpike, ms.(currMod).V, ms.(currMod).mean_predictedSpikes, ms.(currMod).RPEs] = ...
+            ott_RW_V_mean([], os.spikeCount);
+        bestFit = 1;
+        ms.(currMod).bestParams = [];
+    else 
+        error('RW model: Model name not found')
+    end
+    ms.(currMod).paramNames = paramNames;
+    ms.(currMod).LH = -1 * LH(bestFit, :);
+    ms.(currMod).BIC = log(length(os.spikeCount))*numParam  - 2*ms.(currMod).LH;
+    ms.(currMod).AIC = 2*numParam - 2*ms.(currMod).LH;
+    ms.(currMod).AICc = ms.(currMod).AIC + (2*numParam^2 + 2*numParam)/(length(os.spikeCount) - numParam - 1);
+end

+ 45 - 0
MatlabScripts/2_Modeling/threeOutcomes/scripts/nRPE_intVSthree.m

@@ -0,0 +1,45 @@
+clear; clc
+model_root = fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits');
+
+% interspersed task
+task = 'intBlocks';
+load(fullfile(model_root, [task '_MLEfits.mat']))
+int_task = [os.Blocks] == 0;
+os = os(int_task);
+VP_mask = contains({os.Region}, 'VP');
+os = os(VP_mask);
+
+os_int = os;
+clear os
+
+task = 'threeOutcomes';
+load(fullfile(model_root, [task '_MLEfits.mat']))
+os_three = os;
+clear os
+
+% get relevant behavior models
+modelCriterion = 'AIC';
+plotFlag = false;
+
+models_of_interest_RPE = {'base','curr','mean'};
+
+timePeriod = 'RD';
+bm_int_RD = select_RPEmods(os_int, timePeriod,'scoreToUse',modelCriterion,'plotModels_Flag',plotFlag, ...
+                               'particularModel', models_of_interest_RPE);
+bm_three_RD = select_RPEmods(os_three, timePeriod,'scoreToUse',modelCriterion,'plotModels_Flag',plotFlag, ...
+                               'particularModel', models_of_interest_RPE);
+
+%%
+nTot_int = numel(bm_int_RD.mask_base);
+nRPE_int = sum(bm_int_RD.mask_base);
+
+nTot_three = numel(bm_three_RD.mask_base);
+nRPE_three = sum(bm_three_RD.mask_base);
+
+[~,p] = prop_test([nRPE_int nRPE_three],[nTot_int nTot_three]);
+
+fprintf('\n------\n')
+fprintf('Int task: %i RPE of %i tot (%0.2f%%)\n',nRPE_int,nTot_int,nRPE_int/nTot_int*100);
+fprintf('Three task: %i RPE of %i tot (%0.2f%%)\n',nRPE_three,nTot_three,nRPE_three/nTot_three*100);
+fprintf('pValue: %0.2e\n', p)
+fprintf('------\n')

BIN
MatlabScripts/3_Figures/AdditionalFigures/.DS_Store


+ 144 - 0
MatlabScripts/3_Figures/AdditionalFigures/cue/plot_LLHperTrial.m

@@ -0,0 +1,144 @@
+clear; clc
+load(fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits', 'cue_MLEfits.mat'));
+
+myColors = importColors_bb;
+VP_color = myColors.bluishGreen;
+
+% get relevant behavior models
+modelCriterion = 'AIC';
+plotFlag = false;
+
+models_of_interest_RPE = {'base_cue','base','curr_cue','curr','mean_cue','mean'};
+models_of_interest_V = {'base_cue','base','mean_cue','mean'};
+
+timePeriod = 'RD';
+bm_RD = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,...
+                          'plotModels_Flag',plotFlag,...
+                          'particularModel',models_of_interest_RPE);
+
+timePeriod = 'cue';
+bm_cue = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,...
+                           'plotModels_Flag',plotFlag,...
+                           'particularModel',models_of_interest_V);
+
+%% likelihood per trial
+timePeriod = 'RD'; % RD, cue
+
+LH = struct();
+switch timePeriod
+    case 'RD'
+        mods = models_of_interest_RPE;
+        bm = bm_RD;
+        mOfInt = 'mod_RD';
+    case 'cue'
+        mods = models_of_interest_V;
+        bm = bm_cue;
+        mOfInt = 'mod_cue';
+end
+
+% generate a blank LH structure
+for m1 = mods
+    m1 = m1{:};
+    for m2 = mods
+        m2 = m2{:};
+        LH.(m1).(m2) = [];
+    end
+end
+
+% add for each neuron
+LH_cutoff = 10;
+for n = 1:length(os)
+    nTrials = numel(os(n).spikeCount_RD);
+    for m1 = mods
+        m1 = m1{:};
+        if bm.(['mask_' m1])(n) == 1 % if this is a neuron of interest
+            for m2 = mods
+                m2 = m2{:};
+                tmp_LH = -os(n).(mOfInt).(m2).LH/nTrials;
+                if tmp_LH < LH_cutoff
+                    LH.(m1).(m2) = [LH.(m1).(m2) tmp_LH];
+                else
+                    LH.(m1).(m2) = [LH.(m1).(m2) NaN];
+                end
+            end            
+        end
+    end
+end
+
+% fix names
+mods_forLabel = mods;
+mods_forLabel = strrep(mods_forLabel,'base_cue','RPE + Cue effect');
+mods_forLabel = strrep(mods_forLabel,'base','RPE');
+mods_forLabel = strrep(mods_forLabel,'curr_cue','Current outcome + Cue effect');
+mods_forLabel = strrep(mods_forLabel,'curr','Current outcome');
+mods_forLabel = strrep(mods_forLabel,'mean_cue','Unmodulated + Cue effect');
+mods_forLabel = strrep(mods_forLabel,'mean','Unmodulated');
+
+% plot it
+h_boxplot = figure;
+h_boxplot_rel = figure;
+n_plot = length(mods);
+for m1_ind = 1:length(mods)
+    m1 = mods{m1_ind};
+    
+    box_mat = [];
+    for m2_ind = 1:length(mods)
+        m2 = mods{m2_ind};
+        if ~isempty(LH.(m1).(m2))
+            box_mat(:,m2_ind) = LH.(m1).(m2);
+        end
+    end
+    
+    figure(h_boxplot)
+    set(h_boxplot, 'units', 'normalized', 'outerposition', [0 0 1 1]);
+    h_sp_LH(m1_ind) = subplot(1,n_plot,m1_ind); hold on
+    if size(box_mat, 1) > 5
+        boxplot(box_mat,'BoxStyle','filled','PlotStyle','traditional','symbol','')
+        set(gca,'tickdir','out','xtick',1:length(mods),'xticklabel',cellfun(@(i) [i ' model'],mods_forLabel,'UniformOutput',false),'xticklabelrotation',60)
+        ylabel('LH/trial')
+        title([mods_forLabel{m1_ind} ' neurons'],'interpreter','none')
+    end
+    
+    % plot relative to mean model
+    box_mat = [];
+    for m2_ind = 1:length(mods) - 1 % all except the mean model
+        m2 = mods{m2_ind};
+        if ~isempty(LH.(m1).(m2))
+            box_mat(:,m2_ind) = LH.(m1).(m2) - LH.(m1).mean;
+        end
+    end
+    figure(h_boxplot_rel)
+    set(h_boxplot_rel, 'units', 'normalized', 'outerposition', [0 0 1 1]);
+    h_sp_LHrel(m1_ind) = subplot(1,n_plot,m1_ind); hold on
+    if size(box_mat, 1) > 5
+        boxplot(box_mat,'BoxStyle','filled','PlotStyle','traditional','symbol','')
+        set(gca,'tickdir','out','xtick',1:length(mods)-1,'xticklabel',cellfun(@(i) [i ' model'],mods_forLabel(1:end-1),'UniformOutput',false),'xticklabelrotation',60)
+        ylabel('\DeltaLH/trial (relative to Unmodulated model)')
+        title([mods_forLabel{m1_ind} ' neurons'],'interpreter','none')
+        plot([0.5 5.5],[0 0],'k:')
+    end
+end
+
+ylim_LH = [-0.1 max([h_sp_LH.YLim])];
+ylim_LHrel = [min([h_sp_LHrel.YLim]) 0.1];
+figure(h_boxplot)
+for cp = h_sp_LH
+    subplot(cp)
+%     ylim(ylim_LH)
+    ylim([-0.1 5.5])
+end
+figure(h_boxplot_rel)
+for cp = h_sp_LHrel
+    subplot(cp)
+%     ylim(ylim_LHrel)
+    ylim([-0.9 0.1])
+end
+
+
+%%
+fprintf('RPE + Cue effect neurons [IQR]: %0.2f [%0.2f - %0.2f]\n', median(LH.base_cue.base_cue), prctile(LH.base_cue.base_cue, 25), prctile(LH.base_cue.base_cue, 75))
+fprintf('RPE neurons [IQR]: %0.2f [%0.2f - %0.2f]\n', median(LH.base.base), prctile(LH.base.base, 25), prctile(LH.base.base, 75))
+fprintf('Current outcome + Cue effect neurons [IQR]: %0.2f [%0.2f - %0.2f]\n', median(LH.curr_cue.curr_cue), prctile(LH.curr_cue.curr_cue, 25), prctile(LH.curr_cue.curr_cue, 75))
+fprintf('Current outcome neurons [IQR]: %0.2f [%0.2f - %0.2f]\n', median(LH.curr.curr), prctile(LH.curr.curr, 25), prctile(LH.curr.curr, 75))
+fprintf('Unmodulated neurons + Cue effect [IQR]: %0.2f [%0.2f - %0.2f]\n', median(LH.mean_cue.mean_cue), prctile(LH.mean_cue.mean_cue, 25), prctile(LH.mean_cue.mean_cue, 75))
+fprintf('Unmodulated neurons [IQR]: %0.2f [%0.2f - %0.2f]\n', median(LH.mean.mean), prctile(LH.mean.mean, 25), prctile(LH.mean.mean, 75))

+ 78 - 0
MatlabScripts/3_Figures/AdditionalFigures/cue/plot_cueScatter.m

@@ -0,0 +1,78 @@
+clear; clc
+
+load(fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits', 'cue_MLEfits.mat'));
+
+myColors = importColors_bb;
+VP_color = myColors.bluishGreen;
+
+% get relevant behavior models
+modelCriterion = 'AIC';
+plotFlag = true;
+m_RPE = {'base','base_cue','curr','curr_cue','mean','mean_cue'};
+m_V = {'base','base_cue','mean','mean_cue'};
+
+timePeriod = 'RD';
+bm_RD = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,'plotModels_Flag',plotFlag,...
+                          'particularModel',m_RPE);
+
+timePeriod = 'cue';
+bm_cue = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,'plotModels_Flag',plotFlag,...
+                           'particularModel',m_V);
+                       
+%%
+timePeriod = 'cue';
+switch timePeriod
+    case 'RD'
+        bm = bm_RD;
+        models_to_cycle = {'base_cue','curr_cue','mean_cue'};
+        signFlip = -1; % flip to keep consistent with cue-value
+    case 'cue'
+        bm = bm_cue;
+        models_to_cycle = {'base_cue','mean_cue'};
+        signFlip = 1;
+end
+valueStruct = struct();
+valueStruct.vsuc = [];
+valueStruct.vmal = [];
+for m = models_to_cycle
+    m = m{:};
+    mOfInt = bm.(['mask_' m]);
+    if any(mOfInt)
+        valueStruct.(['vsuc_' m]) = getParameters_ott(os(mOfInt),timePeriod,m,'vsuc');
+        valueStruct.(['vmal_' m]) = getParameters_ott(os(mOfInt),timePeriod,m,'vmal');
+        valueStruct.vsuc = [valueStruct.vsuc getParameters_ott(os(mOfInt),timePeriod,m,'vsuc')];
+        valueStruct.vmal = [valueStruct.vmal getParameters_ott(os(mOfInt),timePeriod,m,'vmal')];
+    else
+        valueStruct.(['vsuc_' m]) = [];
+        valueStruct.(['vmal_' m]) = [];
+    end
+end
+
+valueStruct.vsuc = signFlip*valueStruct.vsuc;
+valueStruct.vmal = signFlip*valueStruct.vmal;
+h_scatter = figure; hold on
+scatter(valueStruct.vsuc, valueStruct.vmal)
+plot([-1 1],[0 0],'Color',myColors.gray)
+plot([0 0],[-1 1],'Color',myColors.gray)
+xlim([-1 1])
+ylim([-1 1])
+xlabel('Sucrose value','interpreter','latex')
+ylabel('Maltodextrin value','interpreter','latex')
+title(timePeriod)
+set(gca,'tickdir','out','xtick',-1:0.5:1,'ytick',-1:0.5:1)
+
+nTot = numel(valueStruct.vsuc);
+q1 = sum(valueStruct.vsuc > 0 & valueStruct.vmal > 0)/nTot * 100;
+q2 = sum(valueStruct.vsuc < 0 & valueStruct.vmal > 0)/nTot * 100;
+q3 = sum(valueStruct.vsuc < 0 & valueStruct.vmal < 0)/nTot * 100;
+q4 = sum(valueStruct.vsuc > 0 & valueStruct.vmal < 0)/nTot * 100;
+strFormat = '%0.1f';
+text( 0.5,  0.5, [num2str(q1, strFormat) '%'])
+text(-0.5,  0.5, [num2str(q2, strFormat) '%'])
+text(-0.5, -0.5, [num2str(q3, strFormat) '%'])
+text( 0.5, -0.5, [num2str(q4, strFormat) '%'])
+
+p_prop = myBinomTest(sum(valueStruct.vsuc > 0 & valueStruct.vmal < 0), nTot, 0.25, 'two');
+fprintf('p_value for quadrant 4 is %0.1e\n', p_prop)
+
+axis square

+ 158 - 0
MatlabScripts/3_Figures/AdditionalFigures/cue/plot_latentVariables.m

@@ -0,0 +1,158 @@
+clear; clc
+load(fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits', 'cue_MLEfits.mat'));
+
+myColors = importColors_bb;
+VP_color = myColors.bluishGreen;
+
+% get relevant behavior models
+modelCriterion = 'AIC';
+plotFlag = false;
+m_RPE = {'base','base_cue','curr','curr_cue','mean','mean_cue'};
+m_V = {'base','base_cue','mean','mean_cue'};
+
+timePeriod = 'RD';
+bm_RD = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,'plotModels_Flag',plotFlag,...
+                          'particularModel',m_RPE);
+
+timePeriod = 'cue';
+bm_cue = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,'plotModels_Flag',plotFlag,...
+                           'particularModel',m_V);
+
+%%
+timePeriod = 'RD'; % RD, cue
+normalization = 'none'; % none, zscore, minmax
+
+mod_type = ['mod_' timePeriod];
+switch timePeriod
+    case 'RD'
+        latent_var = 'RPEs';
+        bm = bm_RD;
+    case 'cue'
+        latent_var = 'V';
+        bm = bm_cue;
+    otherwise
+        error('timePeriod not found')
+end
+
+nSim = 501; % odd
+
+% VP first
+all_latent_VP = [];
+norm_fr_real_VP = [];
+norm_fr_sim_VP = [];
+
+mean_real_VP = [];
+mean_pred_VP = [];
+corr_spike_count_VP = [];
+var_real_VP = [];
+var_pred_VP = [];
+trialComparison_pred_VP = [];
+for n = find(bm.mask_base)
+    sign_flip = sign(os(n).(mod_type).base.bestParams(2));
+    
+    real_spike_count = os(n).(['spikeCount_' timePeriod])';
+    
+    % generate nSim predicted spike counts
+    pred_spike_count = [];
+    for i = 1:nSim
+        pred_spike_count(i,:) = poissrnd(os(n).(mod_type).base.mean_predictedSpikes)';
+    end
+    tmp_corr = corr(real_spike_count', pred_spike_count');
+    
+    % get median correlation
+    corr_spike_count_VP = [corr_spike_count_VP median(tmp_corr)];
+    
+    % save that median neuron
+    median_ind = find(tmp_corr == median(tmp_corr), 1);
+        
+    % get latent variables for plotting
+    tmp_latent = os(n).(mod_type).base.(latent_var)';
+    % normalize
+    switch normalization
+        case 'none'
+        case 'zscore'
+            tmp_latent = normalize(tmp_latent);
+        case 'minmax'
+            norm_const = 1/max(abs(tmp_latent));
+            tmp_latent = norm_const*tmp_latent;
+    end
+    all_latent_VP = [all_latent_VP tmp_latent];
+    
+    % normalize real and predicted spike counts for tuning curves
+    norm_fr_real_VP = [norm_fr_real_VP sign_flip*normalize(real_spike_count)];
+    norm_fr_sim_VP = [norm_fr_sim_VP sign_flip*normalize(pred_spike_count(median_ind, :))];
+    
+    % mean spike counts
+    mean_real_VP = [mean_real_VP mean(real_spike_count)];
+    mean_pred_VP = [mean_pred_VP mean(pred_spike_count(median_ind, :))];
+    
+    % STD of spike counts; must use simulated spike counts here
+    var_real_VP = [var_real_VP var(real_spike_count)];
+    var_pred_VP = [var_pred_VP var(pred_spike_count(median_ind, :))];
+    
+    % save the median simulated spike count
+    trialComparison_pred_VP = [trialComparison_pred_VP {pred_spike_count(median_ind, :)}];
+end
+
+nBins = 11;
+latent_bins = prctile(all_latent_VP, linspace(0, 100, nBins));
+spike_bins_real_VP = arrayfun(@(i, j) norm_fr_real_VP(all_latent_VP >= i & all_latent_VP < j), latent_bins(1:end -1), ...
+    latent_bins(2:end), 'UniformOutput', false);
+spike_bins_sim_VP = arrayfun(@(i, j) norm_fr_sim_VP(all_latent_VP >= i & all_latent_VP < j), latent_bins(1:end -1), ...
+    latent_bins(2:end), 'UniformOutput', false);
+
+% figure
+x_latent_bins = latent_bins(1:end - 1) + diff(latent_bins)/2;
+scatterSize = 15;
+
+h_figure = figure; 
+h_lat_VP = subplot(221); hold on
+h_mean = subplot(222); hold on
+h_corr = subplot(223); hold on
+h_var = subplot(224); hold on
+
+subplot(h_lat_VP)
+t_lat_VP(1) = plotFilled(x_latent_bins, spike_bins_real_VP, VP_color, h_lat_VP);
+t_lat_VP(2) = plotFilled(x_latent_bins, spike_bins_sim_VP, myColors.blue_bright, h_lat_VP);
+
+subplot(h_mean)
+scatter(mean_real_VP, mean_pred_VP, scatterSize, 'filled', 'MarkerFaceColor', VP_color)
+maxVal = max([mean_real_VP mean_pred_VP]);
+plot([0 maxVal],[0 maxVal],'k--')
+xlabel('Real spikes (mean)')
+ylabel('Predicted spikes (mean)')
+
+subplot(h_corr)
+corr_bins = linspace(-1,1,40);
+histogram(corr_spike_count_VP, corr_bins, 'Normalization', 'Probability', 'EdgeColor', 'none', 'FaceColor', VP_color)
+xlabel('Correlation')
+ylabel('Probability')
+xlim([-0.05 1.05])
+
+subplot(h_var)
+scatter(var_real_VP, var_pred_VP, scatterSize, 'filled', 'MarkerFaceColor', VP_color)
+maxVal = max([var_real_VP var_pred_VP]);
+plot([0 maxVal],[0 maxVal],'k--')
+xlabel('Real spikes (variance)')
+ylabel('Predicted spikes (variance)')
+
+% clean it up
+legend(t_lat_VP, {'VP','Predicted'}, 'location', 'best')
+for cP = [h_lat_VP h_mean h_corr h_var]
+    subplot(cP)
+    set(cP,'tickdir','out')
+    if cP == h_lat_VP
+        xlabel(latent_var)
+        ylabel([timePeriod ' spikes (z-score)'])
+        if strcmp(latent_var, 'RPEs')
+%             xlim([-1 1])
+        else
+%             xlim([0 1])
+        end
+    elseif cP == h_mean
+        xlim([0 20]); ylim([0 20])
+    elseif cP == h_corr
+    elseif cP == h_var
+        xlim([0 60]); ylim([0 60])
+    end
+end

+ 114 - 0
MatlabScripts/3_Figures/AdditionalFigures/cue/plot_linearRegression.m

@@ -0,0 +1,114 @@
+clear; clc
+load(fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits', 'cue_MLEfits.mat'));
+
+myColors = importColors_bb;
+VP_color = myColors.bluishGreen;
+
+% get relevant behavior models
+modelCriterion = 'AIC';
+plotFlag = false;
+m_RPE = {'base','base_cue','curr','curr_cue','mean','mean_cue'};
+m_V = {'base','base_cue','mean','mean_cue'};
+
+timePeriod = 'RD';
+bm_RD = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,'plotModels_Flag',plotFlag,...
+                          'particularModel',m_RPE);
+
+timePeriod = 'cue';
+bm_cue = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,'plotModels_Flag',plotFlag,...
+                           'particularModel',m_V);
+
+%%
+timePeriod = 'RD';
+nanPad = 50;
+
+mod_type = ['mod_' timePeriod];
+switch timePeriod
+    case 'RD'
+        bm = bm_RD;
+        mod2 = 'curr';
+    case 'cue'
+        bm = bm_cue;
+        mod2 = 'prev';
+    otherwise
+        error('timePeriod not found')
+end
+
+% VP first
+rwd_base_VP = [];
+fr_base_VP = [];
+fr_base_pred_VP = [];
+
+rwd_mod2_VP = [];
+fr_mod2_VP = [];
+fr_mod2_pred_VP = [];
+
+rwd_mean_VP = [];
+fr_mean_VP = [];
+fr_mean_pred_VP = [];
+
+for n = 1:length(os)
+    spike_count = os(n).(['spikeCount_' timePeriod]);
+
+    rewards = os(n).rewards(os(n).timeLocked);
+    if bm.mask_base(n) == 1 % base neuron
+        sign_flip = sign(os(n).(mod_type).base.bestParams(2));
+        pred_spike_count = poissrnd(os(n).(mod_type).base.mean_predictedSpikes);
+        
+        rwd_base_VP = [rwd_base_VP; NaN(nanPad, 1); rewards];
+        fr_base_VP = [fr_base_VP; NaN(nanPad, 1); normalize(spike_count)];
+        fr_base_pred_VP = [fr_base_pred_VP; NaN(nanPad, 1); normalize(pred_spike_count)];
+    elseif bm.(['mask_' mod2])(n) == 1 || bm.(['mask_' mod2 '_cue'])(n) == 1 % curr/prev neuron
+        pred_spike_count = poissrnd(os(n).(mod_type).(mod2).mean_predictedSpikes);
+
+        rwd_mod2_VP = [rwd_mod2_VP; NaN(nanPad, 1); rewards];
+        fr_mod2_VP = [fr_mod2_VP; NaN(nanPad, 1); normalize(spike_count)];
+        fr_mod2_pred_VP = [fr_mod2_pred_VP; NaN(nanPad, 1); normalize(pred_spike_count)];
+    elseif bm.mask_mean(n) == 1 || bm.mask_mean_cue(n) == 1 % mean neuron
+        pred_spike_count = poissrnd(mean(spike_count), length(spike_count), 1);
+
+        rwd_mean_VP = [rwd_mean_VP; NaN(nanPad, 1); rewards];
+        fr_mean_VP = [fr_mean_VP; NaN(nanPad, 1); normalize(spike_count)];
+        fr_mean_pred_VP = [fr_mean_pred_VP; NaN(nanPad, 1); normalize(pred_spike_count)];
+    end
+end
+
+rwdHx_base_VP = [rwd_base_VP generateHistoryMatrix(rwd_base_VP, 10)];
+rwdHx_mod2_VP = [rwd_mod2_VP generateHistoryMatrix(rwd_mod2_VP, 10)];
+rwdHx_mean_VP = [rwd_mean_VP generateHistoryMatrix(rwd_mean_VP, 10)];
+base_mod_VP = fitlm(rwdHx_base_VP, fr_base_VP);
+mod2_mod_VP = fitlm(rwdHx_mod2_VP, fr_mod2_VP);
+mean_mod_VP = fitlm(rwdHx_mean_VP, fr_mean_VP);
+base_pred_mod_VP = fitlm(rwdHx_base_VP, fr_base_pred_VP);
+mod2_pred_mod_VP = fitlm(rwdHx_mod2_VP, fr_mod2_pred_VP);
+mean_pred_mod_VP = fitlm(rwdHx_mean_VP, fr_mean_pred_VP);
+
+
+% figure
+h_bg = figure; 
+h_VP = subplot(121); hold on
+h_VP_pred = subplot(122); hold on
+
+subplot(h_VP)
+title('VP')
+t_VP(1) = plotRegressionWithCI(base_mod_VP, 2:12, h_VP, 'XOffset', -1, 'Color', VP_color);
+t_VP(2) = plotRegressionWithCI(mod2_mod_VP, 2:12, h_VP, 'XOffset', -1, 'Color', myColors.darkGray);
+t_VP(3) = plotRegressionWithCI(mean_mod_VP, 2:12, h_VP, 'XOffset', -1, 'Color', myColors.lightGray);
+
+subplot(h_VP_pred)
+title('VP - simulated neurons')
+t_VP_sim(1) = plotRegressionWithCI(base_pred_mod_VP, 2:12, h_VP_pred, 'XOffset', -1, 'Color', VP_color);
+t_VP_sim(2) = plotRegressionWithCI(mod2_pred_mod_VP, 2:12, h_VP_pred, 'XOffset', -1, 'Color', myColors.darkGray);
+t_VP_sim(3) = plotRegressionWithCI(mean_pred_mod_VP, 2:12, h_VP_pred, 'XOffset', -1, 'Color', myColors.lightGray);
+
+for cP = [h_VP h_VP_pred]
+    subplot(cP)
+    set(cP,'tickdir','out')
+    plot([-0.5 10.5],[0 0],'k--')
+    xlim([-0.5 10.5])
+    ylim([-0.6 2.0])
+    xlabel('Reward n trials back')
+    ylabel('Coefficient ($\pm 95\%$ CI)', 'Interpreter', 'latex')
+end
+legend(t_VP, {'Base', mod2, 'Mean'})
+legend(t_VP_sim, {'Base', mod2, 'Mean'})

+ 53 - 0
MatlabScripts/3_Figures/AdditionalFigures/cue/plot_proportionOfNeurons.m

@@ -0,0 +1,53 @@
+clear; clc
+load(fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits', 'cue_MLEfits.mat'));
+
+myColors = importColors_bb;
+VP_color = myColors.bluishGreen;
+
+% get relevant behavior models
+modelCriterion = 'AIC';
+plotFlag = true;
+m_RPE = {'base','base_cue','curr','curr_cue','mean','mean_cue'};
+m_V = {'base','base_cue','mean','mean_cue'};
+
+timePeriod = 'RD';
+bm_RD = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,'plotModels_Flag',plotFlag,...
+                          'particularModel',m_RPE);
+
+timePeriod = 'cue';
+bm_cue = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,'plotModels_Flag',plotFlag,...
+                           'particularModel',m_V);
+                       
+%% RD proportion
+nTot = length(bm_RD.mask_base);
+n_noCue = [sum(bm_RD.mask_base) sum(bm_RD.mask_curr) sum(bm_RD.mask_mean)];
+n_cue = [sum(bm_RD.mask_base_cue) sum(bm_RD.mask_curr_cue) sum(bm_RD.mask_mean_cue)];
+
+n_noCue = n_noCue./nTot;
+n_cue = n_cue./nTot;
+
+
+h_RD = figure;
+bar([n_noCue; n_cue],'stacked')
+set(gca,'tickdir','out','xtick',1:2,'xticklabel',{'No cue effect','Cue effect'})
+legend('RPE','Curr','Mean','interpreter','none')
+ylabel('Proportion')
+title('RD')
+ylim([0 1])
+                            
+%% cue proportion
+nTot = length(bm_cue.mask_base);
+n_noCue = [sum(bm_cue.mask_base) sum(bm_cue.mask_mean)];
+n_cue = [sum(bm_cue.mask_base_cue) sum(bm_cue.mask_mean_cue)];
+
+n_noCue = n_noCue./nTot;
+n_cue = n_cue./nTot;
+
+
+h_cue = figure;
+bar([n_noCue; n_cue],'stacked')
+set(gca,'tickdir','out','xtick',1:2,'xticklabel',{'No cue effect','Cue effect'})
+legend('Value','Mean','interpreter','none')
+ylabel('Proportion')
+title('Cue')
+ylim([0 1])

+ 43 - 0
MatlabScripts/3_Figures/AdditionalFigures/intBlocks/RPE_symmetry.m

@@ -0,0 +1,43 @@
+Colors = load_colors();
+load('ModData_intBlocks.mat');
+load ('R_intBlocks.mat');
+load ('intBlocks_MLEfits.mat');
+bm_RD=select_RPEmods(os, 'RD', 'particularModels', {'mean','curr','base'},'plotmodels_Flag',false);
+    
+masks=cat(2,bm_RD.mask_base',bm_RD.mask_curr',bm_RD.mask_mean');
+region = strcmp(R_blocks.Region,'VP');
+task = R_blocks.Blocks==0;
+
+figure;
+
+%% activity plots
+ymax=4;
+alph=0.3;
+Sel=region&task&masks(:,1);
+
+neuronIDs = find(Sel);
+pos_error=[];
+neg_error=[];
+for neuron = 1:sum(Sel)
+    neuronID = neuronIDs(neuron);
+    
+    SSHz = mean(CS.RDHz{neuronID}(CS.Predictors{neuronID}(:,1)==1 & CS.Predictors{neuronID}(:,2)==1));
+    SMHz = mean(CS.RDHz{neuronID}(CS.Predictors{neuronID}(:,1)==1 & CS.Predictors{neuronID}(:,2)==0));
+    pos_error(neuron,1) = (SMHz - SSHz) / std(CS.RDHz{neuronID});
+    
+    MSHz = mean(CS.RDHz{neuronID}(CS.Predictors{neuronID}(:,1)==0 & CS.Predictors{neuronID}(:,2)==1));
+    MMHz = mean(CS.RDHz{neuronID}(CS.Predictors{neuronID}(:,1)==0 & CS.Predictors{neuronID}(:,2)==0));
+    neg_error(neuron,1) = (MSHz - MMHz) / std(CS.RDHz{neuronID});
+    
+end
+    
+subplot(1,1,1);
+hold on;
+scatter(pos_error,neg_error);
+plot([-1.5 1.5],[0 0],'color','k','linewidth',1);
+plot([0 0],[-1.5 1.5],'color','k','linewidth',1);
+xlabel('(suc after mal) - (suc after suc)');
+ylabel('(mal after suc) - (mal after mal)');
+title('Negative prediction error vs. positive prediction error');
+
+

+ 305 - 0
MatlabScripts/3_Figures/AdditionalFigures/intBlocks/VideoAnalysis_SucMal.m

@@ -0,0 +1,305 @@
+clear all;
+
+files={'42Sess2foreverDeepCut_resnet50_ThreeAug21shuffle1_1030000.h5';...
+        '42Sess3DeepCut_resnet50_ThreeAug21shuffle1_1030000.h5';...
+        '42Sess4DeepCut_resnet50_ThreeAug21shuffle1_1030000.h5';...
+        '42Sess4redoDeepCut_resnet50_ThreeAug21shuffle1_1030000.h5';...
+        '42Sess5DeepCut_resnet50_ThreeAug21shuffle1_1030000.h5';...
+        '51Sess1againDeepCut_resnet50_ThreeAug21shuffle1_1030000.h5';...
+        '51Sess1foreverDeepCut_resnet50_ThreeAug21shuffle1_1030000.h5';...
+        '51Sess2redoDeepCut_resnet50_ThreeAug21shuffle1_1030000.h5';...
+        '53Sess1againDeepCut_resnet50_ThreeAug21shuffle1_1030000.h5';...
+        '53Sess1redoDeepCut_resnet50_ThreeAug21shuffle1_1030000.h5';...
+        '54Sess1foreverDeepCut_resnet50_ThreeAug21shuffle1_1030000.h5';...
+        '54Sess1DeepCut_resnet50_ThreeAug21shuffle1_1030000.h5';...
+        '54Sess2DeepCut_resnet50_ThreeAug21shuffle1_1030000.h5'};
+    
+portcoord=[50 65;50 65;50 65;50 65;50 65;60 70;60 70;60 70;60 70;60 70;60 70;60 70;60 70];
+
+load('RAWintBlocks');
+load('ModData_intBlocks.mat');
+load ('intBlocks_MLEfits.mat');
+bm_RD=select_RPEmods(os, 'RD', 'particularModels', {'mean','curr','base'},'plotmodels_Flag',false);
+bm_cue=select_RPEmods(os, 'cue', 'particularModels', {'mean','curr','base'},'plotmodels_Flag',false);
+    
+for i=1:length(RAWblocks)
+    name=char(RAWblocks(i).Region);
+    region{i,1}=name(1:3);
+    name=char(RAWblocks(i).Blocks);
+    type{i,1}=name(1);
+end
+RAW=RAWblocks((strcmp('VP2',region) | strcmp('VP3',region) | strcmp('VP4',region) | strcmp('VP5',region)) & strcmp('I',type));
+
+included=(strcmp('VP2',CS.Rat) | strcmp('VP3',CS.Rat) | strcmp('VP4',CS.Rat) | strcmp('VP5',CS.Rat)) & CS.Blocks==0;
+%included=(strcmp('VP2',CS.Rat) | strcmp('VP3',CS.Rat) | strcmp('VP4',CS.Rat)) & CS.Blocks==0;
+Predictors=CS.Predictors(included);
+RDHz=CS.RDHz(included);
+CueHz=CS.CueHzAll(included);
+
+Colors = load_colors();
+[magma,inferno,plasma,viridis]=colormaps;
+masks=cat(2,bm_RD.mask_base(included)',bm_RD.mask_curr(included)',bm_RD.mask_mean(included)');
+cue_masks=cat(2,bm_cue.mask_base(included)',bm_cue.mask_mean(included)');
+
+
+%%
+NN=0;
+trlactivity={[];[];[]};
+trldistance={[];[];[]};
+all_distance=[];
+all_preds=[];
+
+for session=1:length(RAW)
+    %get coordinates from deeplabcut analysis
+    [timestamps,xcoordinates,ycoordinates]=AnalyzeDeepLabCut(files{session});
+    
+    %get event times
+    cue = strcmp('Cue',RAW(session).Einfo(:,2));
+    cuetimes = RAW(session).Erast{cue};
+    pe = strcmp('PE',RAW(session).Einfo(:,2));
+    petimes = RAW(session).Erast{pe};
+    rd = strcmp('RD',RAW(session).Einfo(:,2));
+    rdtimes = RAW(session).Erast{rd};
+    lick = strcmp('Licks',RAW(session).Einfo(:,2));
+    licks = RAW(session).Erast{lick};
+    
+    %find included trials
+    included_RDs=rdtimes<cuetimes(end);
+    included_cue_trials=[];
+    for trial=1:length(rdtimes)
+        if sum(cuetimes>rdtimes(trial))>0
+            included_cue_trials(trial,1)=find(cuetimes>rdtimes(trial),1,'first');
+        end
+    end
+    
+    %for each cue onset
+    xypoints={};
+    xtrial=[];
+    ytrial=[];
+    ITI_dfp=[];
+    
+    for trial=1:length(cuetimes)
+        
+        %coordinates at cue onset
+        if sum(timestamps>(cuetimes(trial)-0.15) & timestamps<(cuetimes(trial)+0.15))>0
+            xtrial(trial)=mean(xcoordinates((timestamps>cuetimes(trial)-0.15) & (timestamps<cuetimes(trial)+0.15)));
+            ytrial(trial)=mean(ycoordinates((timestamps>cuetimes(trial)-0.15) & (timestamps<cuetimes(trial)+0.15)));      
+        else
+            xtrial(trial)=NaN;
+            ytrial(trial)=NaN;
+        end
+        
+        %total distance traveled during ITI, and mean distance from port
+        binsize=0.2; %seconds
+
+        if sum(rdtimes<cuetimes(trial))>0 & sum(included_cue_trials==trial)>0
+            rd_time=max(rdtimes(rdtimes<cuetimes(trial)));
+            start_time=max(licks(licks>rd_time & licks<(rd_time+15)));
+            %bins=start_time:binsize:cuetimes(trial);
+            bins=start_time:binsize:cuetimes(trial)+binsize/2;
+            bn=0;
+            bin_t=[];
+            bin_x=[];
+            bin_y=[];
+            bin_dfp=[];
+            
+            %location for each bin
+            for bin=1:length(bins)-1
+                if sum(timestamps>bins(bin) & timestamps<bins(bin+1))>0
+                    bn=bn+1;
+                    bin_t(bn,1)=bins(bin)+binsize/2;
+                    bin_x(bn,1)=mean(xcoordinates(timestamps>bins(bin) & timestamps<bins(bin+1)));
+                    bin_y(bn,1)=mean(ycoordinates(timestamps>bins(bin) & timestamps<bins(bin+1)));
+                    bin_dfp(bn,1)=sqrt((bin_x(bn,1)-portcoord(session,1))^2+(bin_y(bn,1)-portcoord(session,2))^2);
+                end
+            end
+            
+            norm_time=(bin_t-bin_t(1))/(bin_t(end)-bin_t(1));
+            
+            xypoints{trial,1}=cat(2,bin_x,bin_y);
+            
+            ITI_dfp(trial)=trapz(bin_t,bin_dfp)/(bin_t(end)-bin_t(1)); %dfp = distance from port
+            
+           
+        else
+            ITI_dfp(trial)=NaN;
+            xypoints{trial,1}=NaN;
+        end
+    end
+    
+    %only look at included trials
+    distance_inc=ITI_dfp(included_cue_trials)';    
+   
+    %relate to neural activity
+    noi=[1:length(RAW(session).Nrast)]+NN;
+    noi_activity=RDHz(noi);
+    noi_activity_cue=CueHz(noi);
+
+    sessions=[5];
+    for neuron=1:length(noi)
+        NN=NN+1;
+        
+        %plot example traces
+        if neuron==1 & sum(sessions==session)>0
+             
+            %scatterplot of rat locations
+            xypoints=xypoints(included_cue_trials,:);
+            xtrial_inc=xtrial(included_cue_trials);
+            ytrial_inc=ytrial(included_cue_trials);
+            sucrosexy=cat(1,xypoints{Predictors{NN,1}(1:sum(included_RDs),1)==1});
+            maltodextrinxy=cat(1,xypoints{Predictors{NN,1}(1:sum(included_RDs),1)==0});
+            opacity=0.1;
+            dotsize=24;
+            
+            sucrosetrls=xypoints(Predictors{NN,1}(1:sum(included_RDs),1)==1);
+            maltodextrintrls=xypoints(Predictors{NN,1}(1:sum(included_RDs),1)==0);
+            
+            %sucrose traces
+            figure;
+            subplot(2,3,1);
+            hold on;
+            s1=scatter(sucrosexy(:,1),sucrosexy(:,2),dotsize,Colors('sucrose'),'filled');
+            s1.MarkerFaceAlpha = opacity;
+            sc=scatter(xtrial_inc(Predictors{NN,1}(1:sum(included_RDs),1)==1),ytrial_inc(Predictors{NN,1}(1:sum(included_RDs),1)==1),'k','x');
+
+            
+            axis([0 450 0 350]);
+            set(gca,'Ydir','reverse')
+            set(gca,'xtick',[]);
+            set(gca,'ytick',[]);
+            if session>5 plot([40 60],[25 70],'color','k'); end
+            if session<=5 plot([40 50],[25 65],'color','k'); end
+            legend(sc,'Location at cue onset');
+            text(5,10,'Reward Port');
+            title('Location post-sucrose');
+
+            
+            %maltodextrin traces
+            subplot(2,3,2);
+            hold on;
+            s2=scatter(maltodextrinxy(:,1),maltodextrinxy(:,2),dotsize,Colors('maltodextrin'),'filled');
+            s2.MarkerFaceAlpha = opacity;
+            scatter(xtrial_inc(Predictors{NN,1}(1:sum(included_RDs),1)==0),ytrial_inc(Predictors{NN,1}(1:sum(included_RDs),1)==0),[],'k','x')
+            
+            %plot individual trial traces
+%             for i=1:length(maltodextrintrls)
+%                 if maltodextrintrls{i,1}
+%                     plot(maltodextrintrls{i,1}(:,1),maltodextrintrls{i,1}(:,2),'color',Colors('maltodextrin'));
+%                 end
+%             end
+            
+            set(gca,'Ydir','reverse')
+            set(gca,'xtick',[]);
+            set(gca,'ytick',[]);
+            axis([0 450 0 350]);
+            if session>5 plot([40 60],[25 70],'color','k'); end
+            if session<=5 plot([40 50],[25 65],'color','k'); end
+            text(5,10,'Reward Port');
+            title('Location post-maltodextrin');
+
+          end
+
+
+        [distance_corr_cue(NN,1),distance_corr_cue(NN,2)]=corr(noi_activity_cue{neuron,1}(included_cue_trials,1),distance_inc,'rows','complete','type','spearman');
+        [distance_corr(NN,1),distance_corr(NN,2)]=corr(noi_activity{neuron,1}(included_RDs,1),distance_inc,'rows','complete','type','spearman');
+
+        
+        %shuffled controls
+        for i=1:1000
+            distance_shuff=distance_inc(randperm(length(distance_inc)));
+            dist_shuff_corr(NN,i)=corr(noi_activity{neuron,1}(included_RDs,1),distance_shuff,'rows','complete','type','spearman');
+            dist_shuff_corr_cue(NN,i)=corr(noi_activity_cue{neuron,1}(included_cue_trials,1),distance_shuff,'rows','complete','type','spearman');
+
+        end
+        
+        if neuron==1 %only do this once per session
+        
+            suc_distance(session,1)=nanmean(distance_inc(Predictors{NN,1}(1:sum(included_RDs),1)==1));
+            mal_distance(session,1)=nanmean(distance_inc(Predictors{NN,1}(1:sum(included_RDs),1)==0));
+            
+            distance_inc_norm = (distance_inc - nanmean(distance_inc))/nanstd(distance_inc);
+            
+            all_distance = cat(1,all_distance,distance_inc_norm);
+            all_preds = cat(1,all_preds,Predictors{NN,1}(1:sum(included_RDs),:));
+            
+        end
+        
+ 
+
+        
+        
+    end
+    
+    disp(['Session #' num2str(session)]);
+    
+end
+
+%% plotting distance from port graphs
+
+subplot(2,6,6);
+hold on;
+plot([1 2],[suc_distance mal_distance],'color',[0.6 0.6 0.6]);
+errorbar(1,nanmean(suc_distance),nanste(suc_distance,1),'color',Colors('sucrose'),'marker','o','linewidth',1.5);
+errorbar(2,nanmean(mal_distance),nanste(mal_distance,1),'color',Colors('maltodextrin'),'marker','o','linewidth',1.5);
+ylabel('Distance from port during ITI (pixels)');
+xticks([1 2]);
+xticklabels({'Post-suc','Post-mal'});
+xtickangle(45);
+axis([0.5 2.5 0 200]);
+
+%stats
+%signrank(suc_distance,mal_distance);
+
+colors{1,1}=Colors('rpe');
+colors{2,1}=Colors('current');
+colors{3,1}=Colors('mean');
+
+subplot(2,3,5);
+hold on;
+plots={};
+for mask=1:length(masks(1,:))
+    [cdf,x] = ecdf(distance_corr(masks(:,mask)));
+    plots{mask} = plot(x,cdf,'linewidth',1.5,'color',colors{mask,1});
+    plot([mean(distance_corr(masks(:,mask))) mean(distance_corr(masks(:,mask)))],[0 1],'color',colors{mask,1},'linewidth',1);
+end
+%histogram(distance_corr(masks(:,1)),-0.5:0.05:0.5,'normalization','probability','edgecolor','none','facecolor',Colors('rpe'));
+plot([0 0],[0 1],'color','k');
+plot([-0.5 0.5],[0.5 0.5],'color','k');
+axis([-.5 .5 0 1]);
+legend([plots{:}],'RPE','Current','Unmod.','location','southeast')
+xlabel('Spearman''s rho');
+ylabel('Cumulative fraction of neurons');
+title('Correlation between VP activity and ITI distance');
+text(-0.2,1,'*','color','k','fontsize',24);
+
+%stats tests
+%ranksum(distance_corr(masks(:,1)),distance_corr(masks(:,3)));
+%signrank(distance_corr(masks(:,2)),dist_shuff_corr(masks(:,2)));
+
+%% cue value
+colors{1,1}=Colors('rpe');
+colors{2,1}=Colors('mean');
+
+figure;
+
+subplot(1,1,1);
+hold on;
+plots={};
+for mask=1:length(cue_masks(1,:))
+    [cdf,x] = ecdf(distance_corr_cue(cue_masks(:,mask)));
+    plots{mask} = plot(x,cdf,'linewidth',1.5,'color',colors{mask,1});
+    plot([mean(distance_corr_cue(cue_masks(:,mask))) mean(distance_corr_cue(cue_masks(:,mask)))],[0 1],'color',colors{mask,1},'linewidth',1);
+end
+%histogram(distance_corr(masks(:,1)),-0.5:0.05:0.5,'normalization','probability','edgecolor','none','facecolor',Colors('rpe'));
+plot([0 0],[0 1],'color','k');
+plot([-0.5 0.5],[0.5 0.5],'color','k');
+axis([-.5 .5 0 1]);
+legend([plots{:}],'Value','Unmod.','location','northwest')
+xlabel('Spearman''s rho');
+ylabel('Cumulative fraction of neurons');
+title('Correlation between VP activity and ITI distance');
+text(-0.2,1,'*','color','k','fontsize',24);
+
+%stats tests
+%ranksum(distance_corr_cue(cue_masks(:,1)),distance_corr(cue_masks(:,2)));
+%signrank(distance_corr_cue(masks(:,1)),dist_shuff_corr_cue(masks(:,1)));

+ 125 - 0
MatlabScripts/3_Figures/AdditionalFigures/intBlocks/plot_LLHperTrial.m

@@ -0,0 +1,125 @@
+clear; clc
+
+load(fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits', 'intBlocks_MLEfits.mat'));
+int_task = [os.Blocks] == 0;
+os = os(int_task);
+VP_mask = contains({os.Region}, 'VP');
+os = os(VP_mask); % only VP neurons
+
+% get relevant behavior models
+modelCriterion = 'AIC';
+plotFlag = false;
+
+models_of_interest_RPE = {'base','curr','mean'};
+models_of_interest_V = {'base','mean'};
+
+timePeriod = 'RD';
+bm_RD = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,...
+                          'plotModels_Flag',plotFlag,...
+                          'particularModel',models_of_interest_RPE);
+
+timePeriod = 'cue';
+bm_cue = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,...
+                           'plotModels_Flag',plotFlag,...
+                           'particularModel',models_of_interest_V);
+
+%% likelihood per trial
+timePeriod = 'RD'; % RD, cue
+
+LH = struct();
+switch timePeriod
+    case 'RD'
+        mods = {'base','curr','mean'};
+        bm = bm_RD;
+        mOfInt = 'mod_RD';
+    case 'cue'
+        mods = {'base','mean'};
+        bm = bm_cue;
+        mOfInt = 'mod_cue';
+end
+
+% generate a blank LH structure
+for m1 = mods
+    m1 = m1{:};
+    for m2 = mods
+        m2 = m2{:};
+        LH.(m1).(m2) = [];
+    end
+end
+
+% add for each neuron
+LH_cutoff = 10;
+for n = 1:length(os)
+    nTrials = numel(os(n).spikeCount_RD);
+    for m1 = mods
+        m1 = m1{:};
+        if bm.(['mask_' m1])(n) == 1 % if this is a neuron of interest
+            for m2 = mods
+                m2 = m2{:};
+                tmp_LH = -os(n).(mOfInt).(m2).LH/nTrials;
+                if tmp_LH < LH_cutoff
+                    LH.(m1).(m2) = [LH.(m1).(m2) tmp_LH];
+                else
+                    LH.(m1).(m2) = [LH.(m1).(m2) NaN];
+                end
+            end            
+        end
+    end
+end
+
+% fix names
+mods_forLabel = mods;
+mods_forLabel = strrep(mods_forLabel,'base','RPE');
+mods_forLabel = strrep(mods_forLabel,'curr','Current outcome');
+mods_forLabel = strrep(mods_forLabel,'mean','Unmodulated');
+
+% plot it
+h_boxplot = figure;
+set(h_boxplot, 'units', 'normalized', 'outerposition', [0 0 1 1])
+n_plot = length(mods);
+for m1_ind = 1:length(mods)
+    m1 = mods{m1_ind};
+    
+    box_mat = [];
+    for m2_ind = 1:length(mods)
+        m2 = mods{m2_ind};
+        box_mat(:,m2_ind) = LH.(m1).(m2);
+    end
+    
+    h_sp_LH(m1_ind) = subplot(2,n_plot,m1_ind); hold on
+    boxplot(box_mat,'BoxStyle','filled','PlotStyle','traditional','symbol','')
+    set(gca,'tickdir','out','xtick',1:4,'xticklabel',cellfun(@(i) [i ' model'],mods_forLabel,'UniformOutput',false),'xticklabelrotation',60)
+    ylabel('LH/trial')
+    title([mods_forLabel{m1_ind} ' neurons'],'interpreter','none')
+    
+    % plot relative to mean model
+    box_mat = [];
+    for m2_ind = 1:length(mods) - 1 % all except the mean model
+        m2 = mods{m2_ind};
+        box_mat(:,m2_ind) = LH.(m1).(m2) - LH.(m1).mean;
+    end
+    h_sp_LHrel(m1_ind) = subplot(2,n_plot,m1_ind + n_plot); hold on
+    boxplot(box_mat,'BoxStyle','filled','PlotStyle','traditional','symbol','')
+    set(gca,'tickdir','out','xtick',1:3,'xticklabel',cellfun(@(i) [i ' model'],mods_forLabel(1:3),'UniformOutput',false),'xticklabelrotation',60)
+    ylabel('\DeltaLH/trial (relative to Unmodulated model)')
+    title([mods_forLabel{m1_ind} ' neurons'],'interpreter','none')
+    plot([0.5 2.5],[0 0],'k:')
+end
+
+pause(0.1)
+ylim_LH = [-0.1 max([h_sp_LH.YLim])];
+ylim_LHrel = [-1 0.1];
+for cp = h_sp_LH
+    subplot(cp)
+    ylim(ylim_LH)
+end
+for cp = h_sp_LHrel
+    subplot(cp)
+    ylim(ylim_LHrel)
+end
+
+
+%%
+fprintf('RPE neurons [IQR]: %0.2f [%0.2f - %0.2f]\n', median(LH.base.base), prctile(LH.base.base, 25), prctile(LH.base.base, 75))
+fprintf('Current outcome neurons [IQR]: %0.2f [%0.2f - %0.2f]\n', median(LH.curr.curr), prctile(LH.curr.curr, 25), prctile(LH.curr.curr, 75))
+fprintf('Unmodulated neurons [IQR]: %0.2f [%0.2f - %0.2f]\n', median(LH.mean.mean), prctile(LH.mean.mean, 25), prctile(LH.mean.mean, 75))

+ 296 - 0
MatlabScripts/3_Figures/AdditionalFigures/intBlocks/plot_latentVariables.m

@@ -0,0 +1,296 @@
+clear; clc
+load(fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits', 'intBlocks_MLEfits.mat'));
+int_task = [os.Blocks] == 0;
+os = os(int_task);
+VP_mask = contains({os.Region}, 'VP');
+myColors = importColors_bb;
+VP_color = myColors.bluishGreen;
+NAc_color = myColors.vermillion;
+%% get relevant behavior models
+modelCriterion = 'AIC';
+plotFlag = false;
+
+models_of_interest_RPE = {'base','curr','mean'};
+models_of_interest_V = {'base','mean'};
+
+timePeriod = 'RD';
+bm_RD = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,...
+                          'plotModels_Flag',plotFlag,...
+                          'particularModel',models_of_interest_RPE);
+
+timePeriod = 'cue';
+bm_cue = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,...
+                           'plotModels_Flag',plotFlag,...
+                           'particularModel',models_of_interest_V);
+
+%% real vs predicted tuning curves, correlations
+timePeriod = 'RD'; % RD or cue
+normalization = 'zscore'; % (latent variable) none, zscore, minmax, zscore_translate (so 0 = 0)
+
+mod_type = ['mod_' timePeriod];
+switch timePeriod
+    case 'RD'
+        latent_var = 'RPEs';
+        bm = bm_RD;
+    case 'cue'
+        latent_var = 'V';
+        bm = bm_cue;
+    otherwise
+        error('timePeriod not found')
+end
+
+nSim = 501; % must be odd
+% VP first
+all_latent_VP = [];
+norm_fr_real_VP = [];
+norm_fr_sim_VP = [];
+
+mean_real_VP = [];
+mean_pred_VP = [];
+corr_spike_count_VP = [];
+var_real_VP = [];
+var_pred_VP = [];
+trialComparison_pred_VP = [];
+for n = find(bm.mask_base & VP_mask)
+    sign_flip = sign(os(n).(mod_type).base.bestParams(2));
+    
+    real_spike_count = os(n).(['spikeCount_' timePeriod])';
+    
+    % generate nSim predicted spike counts
+    pred_spike_count = [];
+    for i = 1:nSim
+        pred_spike_count(i,:) = poissrnd(os(n).(mod_type).base.mean_predictedSpikes)';
+    end
+    tmp_corr = corr(real_spike_count', pred_spike_count');
+    
+    % get median correlation
+    corr_spike_count_VP = [corr_spike_count_VP median(tmp_corr)];
+    
+    % save that median neuron
+    median_ind = find(tmp_corr == nanmedian(tmp_corr), 1);
+        
+    % get latent variables for plotting
+    tmp_latent = os(n).(mod_type).base.(latent_var)';
+
+    % normalize
+    switch normalization
+        case 'none'
+        case 'zscore'
+            tmp_latent = normalize(tmp_latent);
+        case 'minmax'
+            norm_const = 1/max(abs(tmp_latent));
+            tmp_latent = norm_const*tmp_latent;
+        case 'zscore_translate'
+            tmp_latent = normalize(tmp_latent) + mean(tmp_latent)/std(tmp_latent);
+    end
+    all_latent_VP = [all_latent_VP tmp_latent];
+    
+    % normalize real and predicted spike counts for tuning curves
+    norm_fr_real_VP = [norm_fr_real_VP sign_flip*normalize(real_spike_count)];
+    norm_fr_sim_VP = [norm_fr_sim_VP sign_flip*normalize(pred_spike_count(median_ind, :))];
+    
+    if numel(norm_fr_real_VP) ~= numel(norm_fr_sim_VP)
+        fprintf('Returning\n')
+        return
+    end
+    
+    % mean spike counts
+    mean_real_VP = [mean_real_VP mean(real_spike_count)];
+    mean_pred_VP = [mean_pred_VP mean(pred_spike_count(median_ind, :))];
+    
+    % STD of spike counts; must use simulated spike counts here
+    var_real_VP = [var_real_VP var(real_spike_count)];
+    var_pred_VP = [var_pred_VP var(pred_spike_count(median_ind, :))];
+    
+    % save the median simulated spike count
+    trialComparison_pred_VP = [trialComparison_pred_VP {pred_spike_count(median_ind, :)}];
+end
+
+
+% NAc second
+all_latent_NAc = [];
+norm_fr_real_NAc = [];
+norm_fr_sim_NAc = [];
+
+mean_real_NAc = [];
+mean_pred_NAc = [];
+corr_spike_count_NAc = [];
+var_real_NAc = [];
+var_pred_NAc = [];
+trialComparison_pred_NAc = [];
+for n = find(bm.mask_base & ~VP_mask)
+    sign_flip = sign(os(n).(mod_type).base.bestParams(2));
+
+    real_spike_count = os(n).(['spikeCount_' timePeriod])';
+    
+    pred_spike_count = [];
+    for i = 1:nSim
+        pred_spike_count(i,:) = poissrnd(os(n).(mod_type).base.mean_predictedSpikes)';
+    end
+    tmp_corr = corr(real_spike_count', pred_spike_count');
+    
+    corr_spike_count_NAc = [corr_spike_count_NAc median(tmp_corr)];
+    median_ind = find(tmp_corr == median(tmp_corr), 1);
+        
+    % get latent variables for plotting
+    tmp_latent = os(n).(mod_type).base.(latent_var)';
+    % normalize
+    switch normalization
+        case 'none'
+        case 'zscore'
+            tmp_latent = normalize(tmp_latent);
+        case 'minmax'
+            norm_const = 1/max(abs(tmp_latent));
+            tmp_latent = norm_const*tmp_latent;
+    end
+    all_latent_NAc = [all_latent_NAc tmp_latent];
+    
+    norm_fr_real_NAc = [norm_fr_real_NAc sign_flip*normalize(real_spike_count)];
+    norm_fr_sim_NAc = [norm_fr_sim_NAc sign_flip*normalize(pred_spike_count(median_ind, :))];
+    
+    mean_real_NAc = [mean_real_NAc mean(real_spike_count)];
+    mean_pred_NAc = [mean_pred_NAc mean(pred_spike_count(median_ind, :))];
+    
+    var_real_NAc = [var_real_NAc var(real_spike_count)];
+    var_pred_NAc = [var_pred_NAc var(pred_spike_count(median_ind, :))];
+    
+    trialComparison_pred_NAc = [trialComparison_pred_NAc {pred_spike_count(median_ind, :)}];
+end
+
+nBins = 11;
+latent_bins = prctile([all_latent_VP all_latent_NAc], linspace(0, 100, nBins));
+spike_bins_real_VP = arrayfun(@(i, j) norm_fr_real_VP(all_latent_VP >= i & all_latent_VP < j), latent_bins(1:end -1), ...
+    latent_bins(2:end), 'UniformOutput', false);
+spike_bins_sim_VP = arrayfun(@(i, j) norm_fr_sim_VP(all_latent_VP >= i & all_latent_VP < j), latent_bins(1:end -1), ...
+    latent_bins(2:end), 'UniformOutput', false);
+
+spike_bins_real_NAc = arrayfun(@(i, j) norm_fr_real_NAc(all_latent_NAc >= i & all_latent_NAc < j), latent_bins(1:end -1), ...
+    latent_bins(2:end), 'UniformOutput', false);
+spike_bins_sim_NAc = arrayfun(@(i, j) norm_fr_sim_NAc(all_latent_NAc >= i & all_latent_NAc < j), latent_bins(1:end -1), ...
+    latent_bins(2:end), 'UniformOutput', false);
+
+
+x_latent_bins = latent_bins(1:end - 1) + diff(latent_bins)/2;
+scatterSize = 15;
+
+h_figure = figure; 
+h_lat_VP = subplot(2,3,1); hold on
+h_lat_NAc = subplot(2,3,2); hold on
+h_lat_both = subplot(233); hold on
+h_mean = subplot(234); hold on
+h_corr = subplot(235); hold on
+h_var = subplot(236); hold on
+
+subplot(h_lat_VP)
+t_lat_VP(1) = plotFilled(x_latent_bins, spike_bins_real_VP, VP_color, h_lat_VP);
+t_lat_VP(2) = plotFilled(x_latent_bins, spike_bins_sim_VP, myColors.blue_bright, h_lat_VP);
+
+subplot(h_lat_NAc)
+t_lat_NAc(1) = plotFilled(x_latent_bins, spike_bins_real_NAc, NAc_color, h_lat_NAc);
+t_lat_NAc(2) = plotFilled(x_latent_bins, spike_bins_sim_NAc, myColors.blue_bright, h_lat_NAc);
+
+subplot(h_lat_both)
+t_lat_both(1) = plotFilled(x_latent_bins, spike_bins_real_VP, VP_color, h_lat_VP);
+t_lat_both(2) = plotFilled(x_latent_bins, spike_bins_real_NAc, NAc_color, h_lat_NAc);
+
+subplot(h_mean)
+scatter(mean_real_VP, mean_pred_VP, scatterSize, 'filled', 'MarkerFaceColor', VP_color)
+scatter(mean_real_NAc, mean_pred_NAc, scatterSize, 'filled', 'MarkerFaceColor', NAc_color)
+maxVal = max([mean_real_VP mean_pred_VP mean_real_NAc mean_pred_NAc]);
+plot([0 maxVal],[0 maxVal],'k--')
+xlabel('Real spikes (mean)')
+ylabel('Predicted spikes (mean)')
+
+subplot(h_corr)
+corr_bins = linspace(-1,1,40);
+histogram(corr_spike_count_VP, corr_bins, 'Normalization', 'Probability', 'EdgeColor', 'none', 'FaceColor', VP_color)
+histogram(corr_spike_count_NAc, corr_bins, 'Normalization', 'Probability', 'EdgeColor', 'none', 'FaceColor', NAc_color)
+xlabel('Correlation')
+ylabel('Probability')
+xlim([-0.05 1.05])
+
+subplot(h_var)
+scatter(var_real_VP, var_pred_VP, scatterSize, 'filled', 'MarkerFaceColor', VP_color)
+scatter(var_real_NAc, var_pred_NAc, scatterSize, 'filled', 'MarkerFaceColor', NAc_color)
+maxVal = max([var_real_VP var_pred_VP var_real_NAc var_pred_NAc]);
+plot([0 maxVal],[0 maxVal],'k--')
+xlabel('Real spikes (variance)')
+ylabel('Predicted spikes (variance)')
+
+% clean it up
+legend(t_lat_VP, {'VP','Predicted'}, 'location', 'best')
+legend(t_lat_NAc, {'NAc','Predicted'}, 'location', 'best')
+legend(t_lat_both, {'VP','NAc'}, 'location', 'best')
+for cP = [h_lat_VP h_lat_NAc h_lat_both h_mean h_corr h_var]
+    subplot(cP)
+    set(cP,'tickdir','out')
+    if any(cP == [h_lat_VP h_lat_NAc h_lat_both])
+        xlabel(latent_var)
+        ylabel([timePeriod ' spikes (z-score)'])
+        if strcmp(latent_var, 'RPEs')
+%             xlim([-1 1])
+        else
+%             xlim([0 1])
+        end
+    elseif cP == h_mean
+        legend({'VP','NAc'}, 'location', 'best')
+        xlim([0 20]); ylim([0 20])
+    elseif cP == h_corr
+        legend({'VP','NAc'}, 'location', 'best')
+    elseif cP == h_var
+        legend({'VP','NAc'}, 'location', 'best')
+        xlim([0 60]); ylim([0 60])
+    end
+end
+
+
+%% plot neurons with particular cross correlations; go with median-correlation simulated spike count
+prtile_cutoff = 80;
+
+VP_neur = find(bm.mask_base & VP_mask);
+NAc_neur = find(bm.mask_base & ~VP_mask);
+[~, VP_neuron_ind] = min(abs(corr_spike_count_VP - prctile(corr_spike_count_VP, prtile_cutoff)));
+VP_neuron_corr = corr_spike_count_VP(VP_neuron_ind);
+[~, NAc_neuron_ind] = min(abs(corr_spike_count_NAc - prctile(corr_spike_count_NAc, prtile_cutoff)));
+NAc_neuron_corr = corr_spike_count_NAc(NAc_neuron_ind);
+os_VP = os(VP_neur);
+os_NAc = os(NAc_neur);
+
+os_VP_neur = os_VP(VP_neuron_ind);
+os_NAc_neur = os_NAc(NAc_neuron_ind);
+
+h_figure = figure;
+h_VP = subplot(211); hold on
+h_NAc = subplot(212); hold on
+
+subplot(h_VP)
+plot(os_VP_neur.spikeCount_RD, 'Color', VP_color, 'linewidth', 2)
+plot(trialComparison_pred_VP{VP_neuron_ind}, 'Color', myColors.blue_bright, 'linewidth', 2)
+title(sprintf('VP\nnind: %i, corr = %0.2f', VP_neuron_ind, VP_neuron_corr))
+legend('VP','Simulated')
+
+subplot(h_NAc)
+plot(os_NAc_neur.spikeCount_RD, 'Color', NAc_color, 'linewidth', 2)
+plot(trialComparison_pred_NAc{NAc_neuron_ind}, 'Color', myColors.blue_bright, 'linewidth', 2)
+title(sprintf('NAc\nnind: %i, corr = %0.2f', NAc_neuron_ind, NAc_neuron_corr))
+legend('NAc','Simulated')
+
+for cp = [h_VP h_NAc]
+    subplot(cp)
+    xlabel('Trials')
+    ylabel('Spike count')
+    set(cp,'tickdir','out')
+end
+
+
+%% stats for RMSE difference between real and predicted RPE vs spike count plots
+mse_VP = mean((norm_fr_real_VP - norm_fr_sim_VP).^2);
+mse_CI_VP = bootci(1e3, @mean, (norm_fr_real_VP - norm_fr_sim_VP).^2);
+
+mse_NAc = mean((norm_fr_real_NAc - norm_fr_sim_NAc).^2);
+mse_CI_NAc = bootci(1e3, @mean, (norm_fr_real_NAc - norm_fr_sim_NAc).^2);
+
+fprintf('\n---\n')
+fprintf('VP: %0.2f [%0.2f - %0.2f]\n', mse_VP, mse_CI_VP(1), mse_CI_VP(2))
+fprintf('NAc: %0.2f [%0.2f - %0.2f]\n', mse_NAc, mse_CI_NAc(1), mse_CI_NAc(2))
+fprintf('---\n\n')

+ 217 - 0
MatlabScripts/3_Figures/AdditionalFigures/intBlocks/plot_linearRegressions.m

@@ -0,0 +1,217 @@
+load(fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits', 'intBlocks_MLEfits.mat'));
+int_task = [os.Blocks] == 0;
+os = os(int_task);
+VP_mask = contains({os.Region}, 'VP');
+myColors = importColors_bb;
+VP_color = myColors.bluishGreen;
+NAc_color = myColors.vermillion;
+
+% get relevant behavior models
+modelCriterion = 'AIC';
+plotFlag = false;
+
+models_of_interest_RPE = {'base','curr','mean'};
+models_of_interest_V = {'base','mean'};
+
+timePeriod = 'RD';
+bm_RD = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,...
+                          'plotModels_Flag',plotFlag,...
+                          'particularModel',models_of_interest_RPE);
+
+timePeriod = 'cue';
+bm_cue = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,...
+                           'plotModels_Flag',plotFlag,...
+                           'particularModel',models_of_interest_V);
+
+%%
+timePeriod = 'RD'; % RD or cue
+nanPad = 50;
+
+mod_type = ['mod_' timePeriod];
+switch timePeriod
+    case 'RD'
+        latent_var = 'RPEs';
+        bm = bm_RD;
+        mod2 = 'curr';
+    case 'cue'
+        latent_var = 'V';
+        bm = bm_cue;
+    otherwise
+        error('timePeriod not found')
+end
+
+% VP first
+rwd_base_VP = [];
+fr_base_VP = [];
+fr_base_pred_VP = [];
+
+if strcmp(timePeriod,'RD')
+    rwd_mod2_VP = [];
+    fr_mod2_VP = [];
+    fr_mod2_pred_VP = [];
+end
+
+rwd_mean_VP = [];
+fr_mean_VP = [];
+fr_mean_pred_VP = [];
+
+for n = 1:length(os)
+    if VP_mask(n) == 1 % VP neuron
+        spike_count = os(n).(['spikeCount_' timePeriod]);
+        
+        rewards = os(n).rewards(os(n).timeLocked);
+        if bm.mask_base(n) == 1 % base neuron
+            sign_flip = sign(os(n).(mod_type).base.bestParams(2));
+            pred_spike_count = poissrnd(os(n).(mod_type).base.mean_predictedSpikes);
+
+            rwd_base_VP = [rwd_base_VP; NaN(nanPad, 1); rewards];
+            fr_base_VP = [fr_base_VP; NaN(nanPad, 1); sign_flip*normalize(spike_count)];
+            fr_base_pred_VP = [fr_base_pred_VP; NaN(nanPad, 1); sign_flip*normalize(pred_spike_count)];
+        elseif strcmp(timePeriod,'RD') && bm.(['mask_' mod2])(n) == 1 % curr/prev neuron
+            sign_flip = sign(os(n).(mod_type).(mod2).bestParams(1));
+            pred_spike_count = poissrnd(os(n).(mod_type).(mod2).mean_predictedSpikes);
+            
+            rwd_mod2_VP = [rwd_mod2_VP; NaN(nanPad, 1); rewards];
+            fr_mod2_VP = [fr_mod2_VP; NaN(nanPad, 1); sign_flip*normalize(spike_count)];
+            fr_mod2_pred_VP = [fr_mod2_pred_VP; NaN(nanPad, 1); sign_flip*normalize(pred_spike_count)];
+        elseif bm.mask_mean(n) == 1 % mean neuron
+            pred_spike_count = poissrnd(mean(spike_count), length(spike_count), 1);
+            
+            rwd_mean_VP = [rwd_mean_VP; NaN(nanPad, 1); rewards];
+            fr_mean_VP = [fr_mean_VP; NaN(nanPad, 1); normalize(spike_count)];
+            fr_mean_pred_VP = [fr_mean_pred_VP; NaN(nanPad, 1); normalize(pred_spike_count)];
+        end
+    end
+end
+
+rwdHx_base_VP = [rwd_base_VP generateHistoryMatrix(rwd_base_VP, 10)];
+rwdHx_mean_VP = [rwd_mean_VP generateHistoryMatrix(rwd_mean_VP, 10)];
+base_mod_VP = fitlm(rwdHx_base_VP, fr_base_VP);
+mean_mod_VP = fitlm(rwdHx_mean_VP, fr_mean_VP);
+base_pred_mod_VP = fitlm(rwdHx_base_VP, fr_base_pred_VP);
+mean_pred_mod_VP = fitlm(rwdHx_mean_VP, fr_mean_pred_VP);
+if strcmp(timePeriod,'RD')
+    rwdHx_mod2_VP = [rwd_mod2_VP generateHistoryMatrix(rwd_mod2_VP, 10)];
+    mod2_mod_VP = fitlm(rwdHx_mod2_VP, fr_mod2_VP);
+    mod2_pred_mod_VP = fitlm(rwdHx_mod2_VP, fr_mod2_pred_VP);
+end
+
+% NAc second
+rwd_base_NAc = [];
+fr_base_NAc = [];
+fr_base_pred_NAc = [];
+
+if strcmp(timePeriod,'RD')
+    rwd_mod2_NAc = [];
+    fr_mod2_NAc = [];
+    fr_mod2_pred_NAc = [];
+end
+
+rwd_mean_NAc = [];
+fr_mean_NAc = [];
+fr_mean_pred_NAc = [];
+
+for n = 1:length(os)
+    if VP_mask(n) == 0 % NAc neuron
+        spike_count = os(n).(['spikeCount_' timePeriod]);
+        rewards = os(n).rewards(os(n).timeLocked);
+        if bm.mask_base(n) == 1 % base neuron
+            sign_flip = sign(os(n).(mod_type).base.bestParams(2));
+            pred_spike_count = poissrnd(os(n).(mod_type).base.mean_predictedSpikes);
+
+            rwd_base_NAc = [rwd_base_NAc; NaN(nanPad, 1); rewards];
+            fr_base_NAc = [fr_base_NAc; NaN(nanPad, 1); sign_flip*normalize(spike_count)];
+            fr_base_pred_NAc = [fr_base_pred_NAc; NaN(nanPad, 1); sign_flip*normalize(pred_spike_count)];
+        elseif strcmp(timePeriod,'RD') && bm.(['mask_' mod2])(n) == 1 % curr/prev neuron
+            sign_flip = sign(os(n).(mod_type).(mod2).bestParams(1));
+            pred_spike_count = poissrnd(os(n).(mod_type).(mod2).mean_predictedSpikes);
+            
+            rwd_mod2_NAc = [rwd_mod2_NAc; NaN(nanPad, 1); rewards];
+            fr_mod2_NAc = [fr_mod2_NAc; NaN(nanPad, 1); sign_flip*normalize(spike_count)];
+            fr_mod2_pred_NAc = [fr_mod2_pred_NAc; NaN(nanPad, 1); sign_flip*normalize(pred_spike_count)];
+        elseif bm.mask_mean(n) == 1 % mean neuron
+            pred_spike_count = poissrnd(mean(spike_count), length(spike_count), 1);
+            
+            rwd_mean_NAc = [rwd_mean_NAc; NaN(nanPad, 1); rewards];
+            fr_mean_NAc = [fr_mean_NAc; NaN(nanPad, 1); normalize(spike_count)];
+            fr_mean_pred_NAc = [fr_mean_pred_NAc; NaN(nanPad, 1); normalize(pred_spike_count)];
+        end
+    end
+end
+
+rwdHx_base_NAc = [rwd_base_NAc generateHistoryMatrix(rwd_base_NAc, 10)];
+rwdHx_mean_NAc = [rwd_mean_NAc generateHistoryMatrix(rwd_mean_NAc, 10)];
+base_mod_NAc = fitlm(rwdHx_base_NAc, fr_base_NAc);
+mean_mod_NAc = fitlm(rwdHx_mean_NAc, fr_mean_NAc);
+base_pred_mod_NAc = fitlm(rwdHx_base_NAc, fr_base_pred_NAc);
+mean_pred_mod_NAc = fitlm(rwdHx_mean_NAc, fr_mean_pred_NAc);
+if strcmp(timePeriod,'RD')
+    rwdHx_mod2_NAc = [rwd_mod2_NAc generateHistoryMatrix(rwd_mod2_NAc, 10)];
+    mod2_mod_NAc = fitlm(rwdHx_mod2_NAc, fr_mod2_NAc);
+    mod2_pred_mod_NAc = fitlm(rwdHx_mod2_NAc, fr_mod2_pred_NAc);
+end
+
+
+% figure
+h_bg = figure; 
+h_VP = subplot(221); hold on
+h_VP_pred = subplot(222); hold on
+h_NAc = subplot(223); hold on
+h_NAc_pred = subplot(224); hold on
+
+subplot(h_VP)
+title('VP')
+t_VP(1) = plotRegressionWithCI(base_mod_VP, 2:12, h_VP, 'XOffset', -1, 'Color', VP_color);
+if strcmp(timePeriod,'RD')
+    t_VP(2) = plotRegressionWithCI(mod2_mod_VP, 2:12, h_VP, 'XOffset', -1, 'Color', myColors.darkGray);
+end
+t_VP(3) = plotRegressionWithCI(mean_mod_VP, 2:12, h_VP, 'XOffset', -1, 'Color', myColors.lightGray);
+
+subplot(h_NAc)
+title('NAc')
+t_NAc(1) = plotRegressionWithCI(base_mod_NAc, 2:12, h_NAc, 'XOffset', -1, 'Color', NAc_color);
+if strcmp(timePeriod,'RD')
+    t_NAc(2) = plotRegressionWithCI(mod2_mod_NAc, 2:12, h_NAc, 'XOffset', -1, 'Color', myColors.darkGray);
+end
+t_NAc(3) = plotRegressionWithCI(mean_mod_NAc, 2:12, h_NAc, 'XOffset', -1, 'Color', myColors.lightGray);
+
+subplot(h_VP_pred)
+title('VP - simulated neurons')
+t_VP_sim(1) = plotRegressionWithCI(base_pred_mod_VP, 2:12, h_VP_pred, 'XOffset', -1, 'Color', VP_color);
+if strcmp(timePeriod,'RD')
+    t_VP_sim(2) = plotRegressionWithCI(mod2_pred_mod_VP, 2:12, h_VP_pred, 'XOffset', -1, 'Color', myColors.darkGray);
+end
+t_VP_sim(3) = plotRegressionWithCI(mean_pred_mod_VP, 2:12, h_VP_pred, 'XOffset', -1, 'Color', myColors.lightGray);
+
+subplot(h_NAc_pred)
+title('NAc - simulated neurons')
+t_NAc_sim(1) = plotRegressionWithCI(base_pred_mod_NAc, 2:12, h_NAc_pred, 'XOffset', -1, 'Color', NAc_color);
+if strcmp(timePeriod,'RD')
+    t_NAc_sim(2) = plotRegressionWithCI(mod2_pred_mod_NAc, 2:12, h_NAc_pred, 'XOffset', -1, 'Color', myColors.darkGray);
+end
+t_NAc_sim(3) = plotRegressionWithCI(mean_pred_mod_NAc, 2:12, h_NAc_pred, 'XOffset', -1, 'Color', myColors.lightGray);
+
+for cP = [h_VP h_NAc h_VP_pred h_NAc_pred]
+    subplot(cP)
+    set(cP,'tickdir','out')
+    plot([-0.5 10.5],[0 0],'k--')
+    xlim([-0.5 10.5])
+    ylim([-0.6 1.1])
+    xlabel('Reward n trials back')
+    ylabel('Coefficient ($\pm 95\%$ CI)', 'Interpreter', 'latex')
+end
+if strcmp(timePeriod,'RD')
+    legend(t_VP, {'Base', mod2, 'Mean'})
+    legend(t_VP_sim, {'Base', mod2, 'Mean'})
+    legend(t_NAc, {'Base', mod2, 'Mean'})
+    legend(t_NAc_sim, {'Base', mod2, 'Mean'})
+else
+    t_VP(2) = [];
+    t_VP_sim(2) = [];
+    t_NAc(2) = [];
+    t_NAc_sim(2) = [];
+    legend(t_VP, {'Base', 'Mean'})
+    legend(t_VP_sim, {'Base', 'Mean'})
+    legend(t_NAc, {'Base', 'Mean'})
+    legend(t_NAc_sim, {'Base', 'Mean'})
+end

+ 155 - 0
MatlabScripts/3_Figures/AdditionalFigures/intBlocks/plot_parameterEstimates.m

@@ -0,0 +1,155 @@
+clear; clc
+load(fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits', 'intBlocks_MLEfits.mat'));
+int_task = [os.Blocks] == 0;
+os = os(int_task);
+VP_mask = contains({os.Region}, 'VP');
+myColors = importColors_bb;
+VP_color = myColors.bluishGreen;
+NAc_color = myColors.vermillion;
+
+%% get relevant behavior models
+modelCriterion = 'AIC';
+plotFlag = false;
+
+models_of_interest_RPE = {'base','curr','mean'};
+models_of_interest_V = {'base','mean'};
+
+timePeriod = 'RD';
+bm_RD = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,...
+                          'plotModels_Flag',plotFlag,...
+                          'particularModel',models_of_interest_RPE);
+
+timePeriod = 'cue';
+bm_cue = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,...
+                           'plotModels_Flag',plotFlag,...
+                           'particularModel',models_of_interest_V);
+                       
+                       
+%% fraction of neurons
+timePeriod = 'RD';
+switch timePeriod
+    case 'RD'
+        bm = bm_RD;
+        mToPlot = models_of_interest_RPE;
+    case 'cue'
+        bm = bm_cue;
+        mToPlot = models_of_interest_V;
+    otherwise
+        error('timePeriod not found')
+end
+s_VP = struct();
+s_NAc = struct();
+s_VP.n = sum(VP_mask);
+s_NAc.n = sum(~VP_mask);
+
+frac_VP = [];
+frac_NAc = [];
+for m = mToPlot
+    m = m{:};
+    s_VP.(m) = sum(bm.(['mask_' m]) & VP_mask);
+    s_NAc.(m) = sum(bm.(['mask_' m]) & ~VP_mask);
+    
+    frac_VP = [frac_VP s_VP.(m)/s_VP.n];
+    frac_NAc = [frac_NAc s_NAc.(m)/s_NAc.n];
+end
+
+h_frac = figure;
+bar([frac_VP; frac_NAc],'stacked')
+legend(mToPlot,'interpreter','none')
+set(gca,'tickdir','out','xtick',1:2,'xticklabel',{'VP','NAc'})
+ylabel('Proportion of neurons')
+title(['Activity at ' timePeriod])
+
+
+%% parameter estimates (asymm model)
+timePeriod = 'RD';
+model = 'base';
+
+switch model
+    case 'base_asymm'
+        alphaPPE = getParameters_ott(os,timePeriod,model,'alphaPPE');
+        alphaNPE = getParameters_ott(os,timePeriod,model,'alphaNPE');
+        asymmInd = alphaPPE ./ (alphaPPE + alphaNPE);
+    case 'base'
+        alpha = getParameters_ott(os,timePeriod,model,'alpha');
+end
+slope = getParameters_ott(os,timePeriod,model,'slope');
+int = getParameters_ott(os,timePeriod,model,'intercept');
+
+model_name = ['mask_' model];
+n_VP = VP_mask & bm_RD.(model_name);
+n_NAc = ~VP_mask & bm_RD.(model_name);
+
+h_parameters = figure;
+for i = 1:5
+    h_sp(i) = subplot(2,3,i); hold on
+end
+
+alpha_bins = linspace(0,1,20);
+switch model
+    case 'base_asymm'
+        i = 1;
+        % alphaPPE
+        subplot(h_sp(i))
+        histogram(alphaPPE(n_VP), alpha_bins, 'Normalization', 'probability', 'FaceColor', VP_color, 'EdgeColor', 'none')
+        histogram(alphaPPE(n_NAc), alpha_bins, 'Normalization', 'probability', 'FaceColor', NAc_color, 'EdgeColor', 'none')
+        set(h_sp(1),'tickdir','out')
+        xlabel('$\alpha_{PPE}$','Interpreter','latex')
+        ylabel('Probability')
+        legend('VP','NAc')
+        
+        i = 2;
+        % alphaNPE
+        subplot(h_sp(i))
+        histogram(alphaNPE(n_VP), alpha_bins, 'Normalization', 'probability', 'FaceColor', VP_color, 'EdgeColor', 'none')
+        histogram(alphaNPE(n_NAc), alpha_bins, 'Normalization', 'probability', 'FaceColor', NAc_color, 'EdgeColor', 'none')
+        set(h_sp(2),'tickdir','out')
+        xlabel('$\alpha_{NPE}$','Interpreter','latex')
+        ylabel('Probability')
+        legend('VP','NAc')
+        
+        % asymmetry
+        i = 3;
+        subplot(h_sp(i))
+        histogram(asymmInd(n_VP), alpha_bins, 'Normalization', 'probability', 'FaceColor', VP_color, 'EdgeColor', 'none')
+        histogram(asymmInd(n_NAc), alpha_bins, 'Normalization', 'probability', 'FaceColor', NAc_color, 'EdgeColor', 'none')
+        set(h_sp(2),'tickdir','out')
+        xlabel('Asymmetry $(\frac{\alpha_{PPE}}{\alpha_{PPE} + \alpha_{NPE}})$','Interpreter','latex')
+        ylabel('Probability')
+        legend('VP','NAc')
+    case 'base'
+        i = 1;
+        % alpha
+        subplot(h_sp(i))
+        histogram(alpha(n_VP), alpha_bins, 'Normalization', 'probability', 'FaceColor', VP_color, 'EdgeColor', 'none')
+        histogram(alpha(n_NAc), alpha_bins, 'Normalization', 'probability', 'FaceColor', NAc_color, 'EdgeColor', 'none')
+        set(h_sp(1),'tickdir','out')
+        xlabel('$\alpha$','Interpreter','latex')
+        ylabel('Probability')
+        legend('VP','NAc')
+        i = 3;
+end
+
+i = i + 1;
+% slope
+subplot(h_sp(i))
+slope_bins = floor(min([slope(n_VP) slope(n_NAc)])) - 1:0.5:ceil(max([slope(n_VP) slope(n_NAc)])) + 1;
+histogram(slope(n_VP), slope_bins, 'Normalization', 'probability', 'FaceColor', VP_color, 'EdgeColor', 'none')
+histogram(slope(n_NAc), slope_bins, 'Normalization', 'probability', 'FaceColor', NAc_color, 'EdgeColor', 'none')
+set(gca,'tickdir','out')
+xlabel('Slope','Interpreter','latex')
+ylabel('Probability')
+legend('VP','NAc')
+
+i = i + 1;
+% intercept
+subplot(h_sp(i))
+int_bins = floor(min([int(n_VP) int(n_NAc)])) - 1:0.5:ceil(max([int(n_VP) int(n_NAc)])) + 1;
+histogram(int(n_VP), int_bins, 'Normalization', 'probability', 'FaceColor', VP_color, 'EdgeColor', 'none')
+histogram(int(n_NAc), int_bins, 'Normalization', 'probability', 'FaceColor', NAc_color, 'EdgeColor', 'none')
+set(gca,'tickdir','out')
+xlabel('Intercept','Interpreter','latex')
+ylabel('Probability')
+legend('VP','NAc')
+
+sgtitle(strrep(['Model: ' model ' at ' timePeriod],'_',' '))

+ 91 - 0
MatlabScripts/3_Figures/AdditionalFigures/intBlocks/plot_simAdaptation.m

@@ -0,0 +1,91 @@
+clear; clc
+Vinit = 0.5;
+alpha = 0.7;
+blockLen = 5;
+tTotal = blockLen*2;
+nRepeat_max = 1e5;
+
+myColors = importColors_bb();
+c_suc = myColors.bluishGreen;
+c_mal = myColors.blue;
+
+h_sim = figure;
+h_random = subplot(131); hold on
+h_suc_first = subplot(132); hold on
+h_mal_first = subplot(133); hold on
+
+
+% random
+V = NaN(nRepeat_max, blockLen + 2);
+RPE = NaN(nRepeat_max, blockLen + 1);
+for nRepeat = 1:nRepeat_max
+    V(nRepeat,1) = Vinit;
+    for t = 1:blockLen + 1
+        r = binornd(1, 0.5);
+        RPE(nRepeat, t) = r - V(nRepeat, t);
+        V(nRepeat, t+1) = V(nRepeat, t) + alpha*RPE(nRepeat, t);
+    end
+end
+
+subplot(h_random)
+suc_trials = RPE > 0;
+mal_trials = RPE < 0;
+suc_RPE = RPE;
+mal_RPE = RPE;
+suc_RPE(~suc_trials) = NaN;
+mal_RPE(~mal_trials) = NaN;
+plot(1:5, nanmean(suc_RPE(:,2:end)), 'linewidth',2,'Color',myColors.orange)
+plot(1:5, nanmean(mal_RPE(:,2:end)), 'linewidth',2,'Color',myColors.reddishPurple)
+
+clear V RPE
+% sucrose first
+V(1) = Vinit;
+for t = 1:tTotal
+    if t <= blockLen
+        r = 1;
+    else
+        r = 0;
+    end
+    RPE(t) = r - V(t);
+    V(t+1) = V(t) + alpha*RPE(t);
+end
+
+subplot(h_suc_first); hold on
+plot(1:blockLen, RPE(1:blockLen), 'linewidth', 2, 'Color', c_suc)
+plot(blockLen+1:length(RPE), RPE(blockLen+1:end), 'linewidth', 2, 'Color', c_mal)
+
+clear V RPE
+% mal first
+V(1) = Vinit;
+for t = 1:tTotal
+    if t <= blockLen
+        r = 0;
+    else
+        r = 1;
+    end
+    RPE(t) = r - V(t);
+    V(t+1) = V(t) + alpha*RPE(t);
+end
+
+subplot(h_mal_first); hold on
+plot(1:blockLen, RPE(1:blockLen), 'linewidth', 2, 'Color', c_mal)
+plot(blockLen+1:length(RPE), RPE(blockLen+1:end), 'linewidth', 2, 'Color', c_suc)
+
+% clean up figure
+for cp = [h_random h_suc_first h_mal_first]
+    subplot(cp)
+    ylim([-1 1])
+    set(cp,'tickdir','out')
+    ylabel('RPE')
+    switch cp
+        case h_random
+            title('Random')
+            legend('Sucrose','Maltodextrin')
+        case h_suc_first
+            title('Blocked (sucrose first)')
+            legend('Sucrose','Maltodextrin')
+        case h_mal_first
+            title('Blocked (maltodextrin first)')
+    end
+    xlabel('Session progress')
+end

+ 338 - 0
MatlabScripts/3_Figures/AdditionalFigures/opto/OptoPEAnalysis.m

@@ -0,0 +1,338 @@
+clear all;
+
+
+%parameters
+window=[-5 20]; %seconds relative to reward delivery
+binsize=0.2; %in seconds
+bins=(window(2)-window(1))/binsize;
+xaxis=linspace(window(1)+binsize/2,window(1)+binsize/2+(bins-1)*binsize,bins);
+
+WOI=[0 10];
+boi=(WOI(1)-window(1))/binsize+1:(WOI(2)-window(1))/binsize; 
+
+address=['C:\Users\dottenh2\Dropbox\MATLAB\David\2R_blocks\ChR2PPBehavior']; %where are the running files located
+%address=['/Users/David/Dropbox/MATLAB/David/2R_blocks/ChR2PPBehavior'];
+files=dir([address,'\\*!*']);
+%files=dir([address,'//*!*']);
+
+for n=1:length(files)
+    filename=fullfile(files(n).folder,files(n).name);
+    file=fopen(filename);
+    L=textscan(file,'%s','Delimiter',':');
+    fclose('all');
+    x=str2num(files(n).name(30:31)); %x is rat #
+    
+    %find start of A, start of C, and end of C
+    Jstrt=find(strcmp('J',L{1,1})); %Cue
+    Kstrt=find(strcmp('K',L{1,1})); %Sucrose Delivery
+    Ostrt=find(strcmp('O',L{1,1})); %Port entry times
+    Pstrt=find(strcmp('P',L{1,1})); %Port durations
+    Qstrt=find(strcmp('Q',L{1,1})); %Laser stimulations
+    Xstrt=find(strcmp('X',L{1,1})); %List (marks end of laser stims)
+    
+    %Sucrose delivery times
+    sucrosedel=(L{1,1}(Kstrt+2:2:Ostrt-1));
+    sucrosedeltime=[];
+    for i=1:length(sucrosedel)
+        medtext=textscan(char(sucrosedel(i,1)),'%f','Delimiter',' ');
+        sdtime=medtext{1,1}(:,1);
+        sucrosedeltime=(cat(1,sucrosedeltime,sdtime(isnan(sdtime)==0)));
+    end
+    sucrosedeltime(sucrosedeltime==0)=[]; %delete 0s
+    
+    %Port entry times
+    pet=(L{1,1}(Ostrt+2:2:Pstrt-1));
+    petime=[];
+    for i=1:length(pet)
+        medtext=textscan(char(pet(i,1)),'%f','Delimiter',' ');
+        portentry=medtext{1,1}(:,1);
+        petime=(cat(1,petime,portentry(isnan(portentry)==0)));
+    end
+    removes=petime==0;
+    petime(petime==0)=[]; %delete 0s
+    
+    %Port entry durations
+    durationt=(L{1,1}(Pstrt+2:2:Qstrt-1));
+    durationtime=[];
+    for i=1:length(durationt)
+        medtext=textscan(char(durationt(i,1)),'%f','Delimiter',' ');
+        duration=medtext{1,1}(:,1);
+        durationtime=(cat(1,durationtime,duration(isnan(duration)==0)));
+    end
+    durationtime(removes)=[]; %delete 0s
+    
+    
+    %Laser stims
+    ST=(L{1,1}(Qstrt+2:2:Xstrt-1));
+    STtime=[];
+    for i=1:length(ST)
+        medtext=textscan(char(ST(i,1)),'%f','Delimiter',' ');
+        stime=medtext{1,1}(:,1);
+        STtime=(cat(1,STtime,stime(isnan(stime)==0)));
+    end
+    
+    STtime(STtime==0)=[]; %delete 0s
+    
+    stim_trials = [];
+    for trl = 1:length(STtime)
+        stim_trials(trl,1) =max(sucrosedeltime(sucrosedeltime<STtime(trl)));
+    end
+    
+    nost_trials = sucrosedeltime(ismember(sucrosedeltime,stim_trials)==0);
+    
+    
+    
+    %port occupancy matrix stim
+    stim_matrix=[];
+    for trl=1:length(stim_trials)
+        for bin=1:bins
+            reltime=window(1)+(bin-1)*binsize;
+            time=stim_trials(trl)+reltime;
+            
+            %check if this time point was during a port entry
+            if sum(petime<=time)
+                [entry_time,index] = max(petime(petime<=time));
+                stim_matrix(trl,bin) = time <= (entry_time + durationtime(index));
+            else
+                stim_matrix(trl,bin) = 0;
+            end
+        end
+    end
+    
+    %port occupancy matrix no stim
+    nost_matrix=[];
+    for trl=1:length(nost_trials)
+        for bin=1:bins
+            reltime=window(1)+(bin-1)*binsize;
+            time=nost_trials(trl)+reltime;
+            
+            %check if this time point was during a port entry
+            if sum(petime<=time)
+
+            [entry_time,index] = max(petime(petime<=time));
+            nost_matrix(trl,bin) = time <= (entry_time + durationtime(index));
+            else
+                nost_matrix(trl,bin) = 0;
+            end
+        end
+    end
+    
+    stim_PSTH(x,:) = mean(stim_matrix);
+    nost_PSTH(x,:) = mean(nost_matrix);
+
+    
+    
+    
+end
+
+%% plotting
+figure;
+
+yfp=[4 6 10 11 13 14 21]; %at least 30 trials, both fibers in general vicinity
+chr=[1 2 7 8 9 16 17 18 22 23 24]; %good placement, at least 30 trials
+
+colors{2}=[0 0.3 1];
+colors{1}=[0.4 0.4 0.4];
+groups{1,1}=yfp;
+groups{2,1}=chr;
+
+for group=1:2
+    subplot(2,2,2+group);
+    hold on;
+    psth1 = mean(nost_PSTH(groups{group,1},:));
+    up1 = psth1 + nanste(nost_PSTH(groups{group,1},:),1);
+    down1 = psth1 - nanste(nost_PSTH(groups{group,1},:),1);
+    
+    psth2 = mean(stim_PSTH(groups{group,1},:));
+    up2 = psth2 + nanste(stim_PSTH(groups{group,1},:),1);
+    down2 = psth2 - nanste(stim_PSTH(groups{group,1},:),1);
+    
+    patch([0 0 2 2],[0 1 1 0],[0.7 0.7 1],'edgecolor','none');
+    a=plot(xaxis,psth1,'Color',colors{1},'linewidth',1);
+    b=plot(xaxis,psth2,'Color',colors{2},'linewidth',1);
+
+    patch([xaxis,xaxis(end:-1:1)],[up1,down1(end:-1:1)],colors{1},'EdgeColor','none');alpha(0.5);
+    patch([xaxis,xaxis(end:-1:1)],[up2,down2(end:-1:1)],colors{2},'EdgeColor','none');alpha(0.5);  
+
+    ylabel('Fraction of trials in port');
+    xlabel('Seconds from reward delivery');
+    plot([0 0],[0 1],':','color','k','linewidth',1);
+    legend([a b],'No laser','Laser','location','northeast');
+    if group==1 title('GFP'); end
+    if group==2 title('ChR2'); end
+end
+
+
+%difference in time in port following 10s?
+boi = (xaxis>= 15) & (xaxis<= 20);
+nost_boi = mean(nost_PSTH(:,boi),2);
+stim_boi = mean(stim_PSTH(:,boi),2);
+p = signrank(nost_boi(chr),stim_boi(chr));
+%% inhibition
+
+clear all;
+
+
+%parameters
+window=[-5 20]; %seconds relative to reward delivery
+binsize=0.2; %in seconds
+bins=(window(2)-window(1))/binsize;
+xaxis=linspace(window(1)+binsize/2,window(1)+binsize/2+(bins-1)*binsize,bins);
+
+WOI=[0 10];
+boi=(WOI(1)-window(1))/binsize+1:(WOI(2)-window(1))/binsize; 
+
+address=['C:\Users\dottenh2\Dropbox\MATLAB\David\2R_blocks\OptoPPBehavior']; %where are the running files located
+%address=['/Users/David/Dropbox/MATLAB/David/2R_blocks/OptoPPBehavior'];
+files=dir([address,'\\*!*']);
+%files=dir([address,'//*!*']);
+
+for n=1:length(files)
+    filename=fullfile(files(n).folder,files(n).name);
+    file=fopen(filename);
+    L=textscan(file,'%s','Delimiter',':');
+    fclose('all');
+    x=str2num(files(n).name(30:31)); %x is rat #
+    
+    %find start of A, start of C, and end of C
+    Jstrt=find(strcmp('J',L{1,1})); %Cue
+    Kstrt=find(strcmp('K',L{1,1})); %Sucrose Delivery
+    Ostrt=find(strcmp('O',L{1,1})); %Port entry times
+    Pstrt=find(strcmp('P',L{1,1})); %Port durations
+    Qstrt=find(strcmp('Q',L{1,1})); %Laser stimulations
+    Xstrt=find(strcmp('X',L{1,1})); %List (marks end of laser stims)
+    
+    %Sucrose delivery times
+    sucrosedel=(L{1,1}(Kstrt+2:2:Ostrt-1));
+    sucrosedeltime=[];
+    for i=1:length(sucrosedel)
+        medtext=textscan(char(sucrosedel(i,1)),'%f','Delimiter',' ');
+        sdtime=medtext{1,1}(:,1);
+        sucrosedeltime=(cat(1,sucrosedeltime,sdtime(isnan(sdtime)==0)));
+    end
+    sucrosedeltime(sucrosedeltime==0)=[]; %delete 0s
+    
+    %Port entry times
+    pet=(L{1,1}(Ostrt+2:2:Pstrt-1));
+    petime=[];
+    for i=1:length(pet)
+        medtext=textscan(char(pet(i,1)),'%f','Delimiter',' ');
+        portentry=medtext{1,1}(:,1);
+        petime=(cat(1,petime,portentry(isnan(portentry)==0)));
+    end
+    removes=petime==0;
+    petime(petime==0)=[]; %delete 0s
+    
+    %Port entry durations
+    durationt=(L{1,1}(Pstrt+2:2:Qstrt-1));
+    durationtime=[];
+    for i=1:length(durationt)
+        medtext=textscan(char(durationt(i,1)),'%f','Delimiter',' ');
+        duration=medtext{1,1}(:,1);
+        durationtime=(cat(1,durationtime,duration(isnan(duration)==0)));
+    end
+    durationtime(removes)=[]; %delete 0s
+    
+    
+    %Laser stims
+    ST=(L{1,1}(Qstrt+2:2:Xstrt-1));
+    STtime=[];
+    for i=1:length(ST)
+        medtext=textscan(char(ST(i,1)),'%f','Delimiter',' ');
+        stime=medtext{1,1}(:,1);
+        STtime=(cat(1,STtime,stime(isnan(stime)==0)));
+    end
+    
+    STtime(STtime==0)=[]; %delete 0s
+    
+    stim_trials = [];
+    for trl = 1:length(STtime)
+        stim_trials(trl,1) =max(sucrosedeltime(sucrosedeltime<STtime(trl)));
+    end
+    
+    nost_trials = sucrosedeltime(ismember(sucrosedeltime,stim_trials)==0);
+    
+    
+    
+    %port occupancy matrix stim
+    stim_matrix=[];
+    for trl=1:length(stim_trials)
+        for bin=1:bins
+            reltime=window(1)+(bin-1)*binsize;
+            time=stim_trials(trl)+reltime;
+            
+            %check if this time point was during a port entry
+            if sum(petime<=time)
+                [entry_time,index] = max(petime(petime<=time));
+                stim_matrix(trl,bin) = time <= (entry_time + durationtime(index));
+            else
+                stim_matrix(trl,bin) = 0;
+            end
+        end
+    end
+    
+    %port occupancy matrix no stim
+    nost_matrix=[];
+    for trl=1:length(nost_trials)
+        for bin=1:bins
+            reltime=window(1)+(bin-1)*binsize;
+            time=nost_trials(trl)+reltime;
+            
+            %check if this time point was during a port entry
+            if sum(petime<=time)
+
+            [entry_time,index] = max(petime(petime<=time));
+            nost_matrix(trl,bin) = time <= (entry_time + durationtime(index));
+            else
+                nost_matrix(trl,bin) = 0;
+            end
+        end
+    end
+    
+    stim_PSTH(x,:) = mean(stim_matrix);
+    nost_PSTH(x,:) = mean(nost_matrix);
+
+    
+    
+    
+end
+
+%% plotting
+
+yfp=[1 2 6 9 10 15 23]; %at least 30 trials, both fibers in general vicinity
+%arc=[3 4 7 8 11 13 14 17 18 21 22];
+%arc=[3 4 11 13 18 22]; %latency effects
+arc=[3 4 8 11 13 18 22]; %good placement, at least 30 trials
+
+colors{2}=[0 0.6 0.4];
+colors{1}=[0.4 0.4 0.4];
+groups{1,1}=yfp;
+groups{2,1}=arc;
+
+for group=1:2
+    subplot(2,2,group);
+    hold on;
+    psth1 = mean(nost_PSTH(groups{group,1},:));
+    up1 = psth1 + nanste(nost_PSTH(groups{group,1},:),1);
+    down1 = psth1 - nanste(nost_PSTH(groups{group,1},:),1);
+    
+    psth2 = mean(stim_PSTH(groups{group,1},:));
+    up2 = psth2 + nanste(stim_PSTH(groups{group,1},:),1);
+    down2 = psth2 - nanste(stim_PSTH(groups{group,1},:),1);
+    
+    patch([0 0 5 5],[0 1 1 0],[0.7 1 0.7],'edgecolor','none');
+    a=plot(xaxis,psth1,'Color',colors{1},'linewidth',1);
+    b=plot(xaxis,psth2,'Color',colors{2},'linewidth',1);
+
+    patch([xaxis,xaxis(end:-1:1)],[up1,down1(end:-1:1)],colors{1},'EdgeColor','none');alpha(0.5);
+    patch([xaxis,xaxis(end:-1:1)],[up2,down2(end:-1:1)],colors{2},'EdgeColor','none');alpha(0.5);  
+
+    ylabel('Fraction of trials in port');
+    xlabel('Seconds from reward delivery');
+    plot([0 0],[0 1],':','color','k','linewidth',1);
+    legend([a b],'No laser','Laser','location','northeast');
+    if group==1 title('YFP'); end
+    if group==2 title('ArchT'); end
+
+end
+

+ 410 - 0
MatlabScripts/3_Figures/AdditionalFigures/opto/VideoAnalysis_Arch.m

@@ -0,0 +1,410 @@
+clear all;
+analyzedlcfiles=0;
+%% get data from DeepLabCut files
+
+if analyzedlcfiles
+    %original videos needed, available upon request. otherwise, use
+    %pre-generated struct below
+
+    ffmpeg_path='C:\FFmpeg\bin';
+    address=['']; %where are the running files located
+    files=dir([address,'\\*_*']);
+
+    videoaddress=[''];
+    videofiles=dir([videoaddress,'\\*Op*']);
+
+    for k=1:length(files)
+        filename=fullfile(files(k).folder,files(k).name);
+        rat=str2num(files(k).name(3:4)); %rat #
+        videofile=[videofiles(k).folder '\' videofiles(k).name];
+
+        %get timestamp data
+        [timestamps,xcoordinates,ycoordinates]=AnalyzeDeepLabCutPP(filename,videofile,ffmpeg_path);
+
+        VideoData{rat,1}=timestamps;
+        VideoData{rat,2}=xcoordinates;
+        VideoData{rat,3}=ycoordinates;
+
+    end
+
+    save('PPVideoData.mat','VideoData');
+
+else
+    load('PPVideoData.mat');
+
+end
+%%
+
+spreadsheet=importdata('PPVideoKey.xlsx');
+spreadsheetrat=spreadsheet.textdata(2:end,1);
+for i=1:length(spreadsheetrat)
+    sprat(i,1)=str2num(spreadsheetrat{i,1}(3:4));
+end
+
+lighton(sprat,1)=spreadsheet.data(:,1);
+portcoord(sprat,1)=spreadsheet.data(:,2);
+portcoord(sprat,2)=spreadsheet.data(:,3);
+cutoff(sprat,1)=spreadsheet.data(:,5);
+
+Colors = load_colors();
+trlactivity={[];[];[]};
+trldistance={[];[];[]};
+
+address=['/Data/ArchBehavior']; %where are the running files located
+files=dir([address,'//*!*']); %mac
+
+for k=1:length(files)
+    filename=fullfile(files(k).folder,files(k).name);
+    file=fopen(filename);
+    L=textscan(file,'%s','Delimiter',':');
+    fclose('all');
+    rat=str2num(files(k).name(end-1:end));
+
+    %find start of A, start of C, and end of C
+    Jstrt=find(strcmp('J',L{1,1})); %Cue
+    Kstrt=find(strcmp('K',L{1,1})); %Sucrose Delivery
+    Ostrt=find(strcmp('O',L{1,1})); %Port entry times
+    Pstrt=find(strcmp('P',L{1,1})); %Port durations
+    Qstrt=find(strcmp('Q',L{1,1})); %Laser stimulations
+    Xstrt=find(strcmp('X',L{1,1})); %List (marks end of laser stims)
+
+    %trial times
+    choicet=(L{1,1}(Jstrt+2:2:Kstrt-1));
+    choicetime=[];
+    for i=1:length(choicet)
+        medtext=textscan(char(choicet(i,1)),'%f','Delimiter',' ');
+        ctime=medtext{1,1}(:,1);
+        choicetime=(cat(1,choicetime,ctime(isnan(ctime)==0)));
+    end
+    choicetime(choicetime==0)=[]; %delete 0s
+
+
+    %Sucrose delivery times
+    sucrosedel=(L{1,1}(Kstrt+2:2:Ostrt-1));
+    sucrosedeltime=[];
+    for i=1:length(sucrosedel)
+        medtext=textscan(char(sucrosedel(i,1)),'%f','Delimiter',' ');
+        sdtime=medtext{1,1}(:,1);
+        sucrosedeltime=(cat(1,sucrosedeltime,sdtime(isnan(sdtime)==0)));
+    end
+    sucrosedeltime(sucrosedeltime==0)=[]; %delete 0s
+
+    %Port entry times
+    pet=(L{1,1}(Ostrt+2:2:Pstrt-1));
+    petime=[];
+    for i=1:length(pet)
+        medtext=textscan(char(pet(i,1)),'%f','Delimiter',' ');
+        portentry=medtext{1,1}(:,1);
+        petime=(cat(1,petime,portentry(isnan(portentry)==0)));
+    end
+    removes=petime==0;
+    petime(petime==0)=[]; %delete 0s
+
+    %Port entry durations
+    durationt=(L{1,1}(Pstrt+2:2:Qstrt-1));
+    durationtime=[];
+    for i=1:length(durationt)
+        medtext=textscan(char(durationt(i,1)),'%f','Delimiter',' ');
+        duration=medtext{1,1}(:,1);
+        durationtime=(cat(1,durationtime,duration(isnan(duration)==0)));
+    end
+    durationtime(removes)=[]; %delete 0s
+    
+    pexits=petime(1:length(durationtime))+durationtime;
+    
+    %Laser stims
+    ST=(L{1,1}(Qstrt+2:2:Xstrt-1));
+    STtime=[];
+    for i=1:length(ST)
+        medtext=textscan(char(ST(i,1)),'%f','Delimiter',' ');
+        stime=medtext{1,1}(:,1);
+        STtime=(cat(1,STtime,stime(isnan(stime)==0)));
+    end
+
+    STtime(STtime==0)=[]; %delete 0s    
+
+
+    %Sucrose or water?
+    latency=[];
+    for i=1:length(choicetime)
+        sucrose=find(sucrosedeltime(:,1) > choicetime(i,1),1);
+        sucrosetime=sucrosedeltime(sucrose);
+        if sucrosetime
+            latency(i,1)=sucrosetime-choicetime(i,1)-0.5; %delay is 0.5s
+        else
+            latency(i,1)=NaN;
+        end
+    end
+    latency(latency>10)=NaN;
+
+    completed=latency>0;
+
+    %stim trials
+    post_stimmed=zeros(length(choicetime),1);
+    for stim=1:length(STtime)
+        trialtime=min(choicetime(choicetime>STtime(stim)));
+        if ~isempty(trialtime)
+            post_stimmed(choicetime==trialtime)=1;
+        end
+    end
+
+    %no stim trials
+    postsuc=zeros(length(choicetime),1);
+    for suctrial=1:length(sucrosedeltime)
+        trialtime=min(choicetime(choicetime>sucrosedeltime(suctrial)));
+        if ~isempty(trialtime)
+            postsuc(choicetime==trialtime)=1;
+        end
+    end
+    nostim=postsuc==1 & post_stimmed==0;    
+    
+    
+    rdtimes=sucrosedeltime;
+    cuetimes=choicetime;
+    petimes=petime;
+    
+    included_cue_trials=[];
+    for trial=1:length(rdtimes)
+        if sum(cuetimes>rdtimes(trial))>0
+            included_cue_trials(trial,1)=find(cuetimes>rdtimes(trial),1,'first');
+        end
+    end
+    post_stim=post_stimmed(included_cue_trials);
+    
+    %get coordinates from deeplabcut analysis
+    timestamps=VideoData{rat,1}-lighton(rat,1); %adjust for when session started in video
+    xcoordinates=VideoData{rat,2};
+    ycoordinates=VideoData{rat,3};
+    
+    
+    %for each cue onset
+    xypoints={};
+    xtrial=[];
+    ytrial=[];
+    ITI_distance=[];
+    ITI_dfp=[];
+    licks_trl=[];
+    
+   
+    for trial=1:length(cuetimes)
+        if sum(timestamps>(cuetimes(trial)-0.25) & timestamps<(cuetimes(trial)+0.25))>0
+            xtrial(trial)=mean(xcoordinates((timestamps>cuetimes(trial)-0.25) & (timestamps<cuetimes(trial)+0.25)));
+            ytrial(trial)=mean(ycoordinates((timestamps>cuetimes(trial)-0.25) & (timestamps<cuetimes(trial)+0.25)));            
+          
+           
+           
+        elseif sum(timestamps>(cuetimes(trial)-1) & timestamps<(cuetimes(trial)+0.3))>0
+            xtrial(trial)=mean(xcoordinates((timestamps>cuetimes(trial)-1) & (timestamps<cuetimes(trial)+0.3)));
+            ytrial(trial)=mean(ycoordinates((timestamps>cuetimes(trial)-1) & (timestamps<cuetimes(trial)+0.3)));
+
+            
+        else
+            xtrial(trial)=NaN;
+            ytrial(trial)=NaN;
+        end
+        
+        %total distance traveled during ITI, and mean distance from port
+        binsize=0.2; %seconds
+
+        if sum(rdtimes<cuetimes(trial))>0 & postsuc(trial)==1
+            
+            rd_time=max(rdtimes(rdtimes<cuetimes(trial)));
+            pe_time=max(petimes(petimes<(rd_time+10)));
+            start_time=min([pexits(petimes==pe_time) rd_time+10]);
+            %bins=start_time:binsize:cuetimes(trial);
+            bins=start_time:binsize:cuetimes(trial)+binsize/2;
+            bn=0;
+            bin_t=[];
+            bin_x=[];
+            bin_y=[];
+            bin_dfp=[];
+            
+            %location for each bin
+            for bin=1:length(bins)-1
+                if sum(timestamps>bins(bin) & timestamps<bins(bin+1))>0
+                    bn=bn+1;
+                    bin_t(bn,1)=bins(bin)+binsize/2;
+                    bin_x(bn,1)=mean(xcoordinates(timestamps>bins(bin) & timestamps<bins(bin+1)));
+                    bin_y(bn,1)=mean(ycoordinates(timestamps>bins(bin) & timestamps<bins(bin+1)));
+                    bin_dfp(bn,1)=sqrt((bin_x(bn,1)-portcoord(rat,1))^2+(bin_y(bn,1)-portcoord(rat,2))^2);
+                end
+            end
+            
+            xypoints{trial,1}=cat(2,bin_x,bin_y);
+            
+%             subplot(5,5,rat);
+%             hold on;
+%             plot(bin_dfp);
+%             xypoints{trial,1}=cat(2,bin_x,bin_y);
+%             axis([0 25 0 500]);
+
+            ITI_dfp(trial)=trapz(bin_t,bin_dfp)/(bin_t(end)-bin_t(1)); %dfp = distance from port
+          
+            
+        else
+            ITI_dfp(trial)=NaN;
+            xypoints{trial,1}=NaN;
+        end
+    end
+   
+    
+    %plot example traces
+    if rat==18
+        
+        figure;
+
+        %scatterplot of rat locations
+        xypoints=xypoints(included_cue_trials,:);
+        xtrial_inc=xtrial(included_cue_trials);
+        ytrial_inc=ytrial(included_cue_trials);
+        sucrosexy=cat(1,xypoints{post_stim==0});
+        maltodextrinxy=cat(1,xypoints{post_stim==1});
+        opacity=0.1;
+        dotsize=24;
+
+        sucrosetrls=xypoints(post_stim==0);
+        maltodextrintrls=xypoints(post_stim==1);
+
+        %sucrose traces
+        subplot(1,4,1);
+        hold on;
+        s1=scatter(sucrosexy(:,1),sucrosexy(:,2),dotsize,[.4 .4 .4],'filled');
+        s1.MarkerFaceAlpha = opacity;
+        sc=scatter(xtrial_inc(post_stim==0),ytrial_inc(post_stim==0),'k','x');
+        %plot individual trial traces
+        %             for i=1:length(sucrosetrls)
+        %                 if sucrosetrls{i,1}
+        %                     plot(sucrosetrls{i,1}(:,1),sucrosetrls{i,1}(:,2),'color',Colors('sucrose'));
+        %                 end
+        %             end
+
+        axis([0 950 75 750]);
+        set(gca,'Ydir','reverse')
+        set(gca,'xtick',[]);
+        set(gca,'ytick',[]);
+        plot([950 portcoord(rat,1)],[450 portcoord(rat,2)],'color','k');
+        legend(sc,'Location at cue onset');
+        text(955,450,'Reward Port');
+        title('No laser');
+
+
+        %maltodextrin traces
+        subplot(1,4,2);
+        hold on;
+        s2=scatter(maltodextrinxy(:,1),maltodextrinxy(:,2),dotsize,[0 .6 .4],'filled');
+        s2.MarkerFaceAlpha = opacity;
+        scatter(xtrial_inc(post_stim==1),ytrial_inc(post_stim==1),[],'k','x')
+
+        %plot individual trial traces
+        %             for i=1:length(maltodextrintrls)
+        %                 if maltodextrintrls{i,1}
+        %                     plot(maltodextrintrls{i,1}(:,1),maltodextrintrls{i,1}(:,2),'color',Colors('maltodextrin'));
+        %                 end
+        %             end
+
+        set(gca,'Ydir','reverse')
+        set(gca,'xtick',[]);
+        set(gca,'ytick',[]);
+        axis([0 950 75 750]);
+        plot([950 portcoord(rat,1)],[450 portcoord(rat,2)],'color','k');
+        legend(sc,'Location at cue onset');
+        text(955,450,'Reward Port');
+        title('Laser');
+
+    end   
+  
+    
+    ITI_dfp_inc=ITI_dfp(included_cue_trials)';
+
+    
+    
+    mean_distance(rat,1)=nanmean(ITI_dfp_inc(post_stim==0));
+    mean_distance(rat,2)=nanmean(ITI_dfp_inc(post_stim==1));
+    
+
+
+    completed_trials(rat,1)=length(sucrosedeltime);
+
+    disp(['File #' num2str(k)]);
+end
+
+
+%% plotting measures
+
+%included rats
+yfp=[1 2 6 9 10 15 23];
+arc=[3 4 8 11 13 18 22];
+
+colors{2}=[0 0.6 0.4];
+colors{1}=[0.4 0.4 0.4];
+groups{1,1}=yfp;
+groups{2,1}=arc;
+% a={};
+% for group=1:2
+%     subplot(1,4,3);
+%     hold on
+%     
+%     a{group}=scatter(mean_distance(groups{group,1},1),mean_distance(groups{group,1},2),55,colors{group},'filled');
+%     title('Distance from port during ITI (pixels)');
+%     plot([0 500],[0 500],'color','k','linewidth',1);
+%     xlabel('No stim');
+%     ylabel('Stim');
+% end
+% legend([a{:}],'YFP','ARCH','location','southeast');
+% axis([100 350 100 350]);
+
+subplot(1,8,5);
+hold on;
+plot([1 2],mean_distance(yfp,:),'color',[0.4 0.4 0.4]);
+errorbar(1,nanmean(mean_distance(yfp,1)),nanste(mean_distance(yfp,1),1),'color',[0.4 0.4 0.4],'marker','o','linewidth',1.5);
+errorbar(2,nanmean(mean_distance(yfp,2)),nanste(mean_distance(yfp,2),1),'color',[0 0.6 0.4],'marker','o','linewidth',1.5);
+ylabel('Distance from port during ITI (pixels)');
+xticks([1 2]);
+xticklabels({'No laser','Laser'});
+xtickangle(45);
+axis([0.5 2.5 0 400]);
+title('YFP');
+
+pval=signrank(mean_distance(yfp,1),mean_distance(yfp,2));
+text(1.2,375,['p = ' num2str(round(pval,5))]);
+
+
+subplot(1,8,6);
+hold on;
+plot([1 2],mean_distance(arc,:),'color',[0.4 0.4 0.4]);
+errorbar(1,nanmean(mean_distance(arc,1)),nanste(mean_distance(arc,1),1),'color',[0.4 0.4 0.4],'marker','o','markerfacecolor',[0.4 0.4 0.4],'linewidth',1.5);
+errorbar(2,nanmean(mean_distance(arc,2)),nanste(mean_distance(arc,2),1),'color',[0 0.6 0.4],'marker','o','markerfacecolor',[0 0.6 0.4],'linewidth',1.5);
+ylabel('Distance from port during ITI (pixels)');
+xticks([1 2]);
+xticklabels({'No laser','Laser'});
+xtickangle(45);
+axis([0.5 2.5 0 400]);
+title('Arch');
+
+pval=signrank(mean_distance(arc,1),mean_distance(arc,2));
+text(1.2,375,['p = ' num2str(round(pval,5))]);
+
+included=zeros(24,1);
+included(arc)=1;
+included(yfp)=1;
+arclog=zeros(24,1);
+arclog(arc)=1;
+
+subplot(1,4,4);
+hold on;
+difference=(mean_distance(:,2)-mean_distance(:,1))./mean_distance(:,1);
+
+
+boxplot(difference(included==1),arclog(included==1),'colorgroup',[0 1],'colors',[colors{2};colors{2}],'boxstyle','filled');
+scatter(rand([length(yfp) 1])/4+1.2,difference(yfp),55,colors{2});
+scatter(rand([length(arc) 1])/4+2.2,difference(arc),55,colors{2},'filled');
+axis([0.7 2.74 -0.5 0.5]);
+plot([0 3],[0 0],'color','k');
+pval=ranksum(difference(yfp),difference(arc));
+text(1.2,0.4,['p = ' num2str(round(pval,5))]);
+ylabel('Fold-change over no laser');
+xticks([1.22 2.22]);
+xticklabels({'YFP','Arch'});
+
+
+dataforanova=[mean_distance(arc,1) mean_distance(arc,2);mean_distance(yfp,1) mean_distance(yfp,2)];

+ 405 - 0
MatlabScripts/3_Figures/AdditionalFigures/opto/VideoAnalysis_ChR2.m

@@ -0,0 +1,405 @@
+clear all;
+analyzedlcfiles=0;
+%% get data from DeepLabCut files
+
+%for getting data from DLC and timestamps from videos
+if analyzedlcfiles
+    %original videos needed, available upon request. otherwise, use
+    %pre-generated struct below
+    
+    ffmpeg_path='C:\FFmpeg\bin';
+    address=['']; %where are the running files located
+    files=dir([address,'\\*_*']);
+
+    videoaddress=[''];
+    videofiles=dir([videoaddress,'\\*Ch*']);
+
+    for k=1:length(files)
+        filename=fullfile(files(k).folder,files(k).name);
+        rat=str2num(files(k).name(6:7)); %rat #
+        videofile=[videofiles(k).folder '\' videofiles(k).name];
+
+        %get timestamp data
+        [timestamps,xcoordinates,ycoordinates]=AnalyzeDeepLabCutPP(filename,videofile,ffmpeg_path);
+
+        VideoData{rat,1}=timestamps;
+        VideoData{rat,2}=xcoordinates;
+        VideoData{rat,3}=ycoordinates;
+
+    end
+
+    save('ChVideoData.mat','VideoData');
+
+else
+    load('ChVideoData.mat');
+
+end
+%%
+
+%how long after reward delivery do we start looking at location?
+posttime=15; %10 is default
+
+spreadsheet=importdata('ChVideoKey.xlsx');
+spreadsheetrat=spreadsheet.textdata(2:end,1);
+for i=1:length(spreadsheetrat)
+    sprat(i,1)=str2num(spreadsheetrat{i,1}(3:4));
+end
+
+lighton(sprat,1)=spreadsheet.data(:,1);
+portcoord(sprat,1)=spreadsheet.data(:,2);
+portcoord(sprat,2)=spreadsheet.data(:,3);
+%cutoff(sprat,1)=spreadsheet.data(:,5);
+
+Colors = load_colors();
+trlactivity={[];[];[]};
+trldistance={[];[];[]};
+
+address=['/Data/ChR2Behavior'];
+files=dir([address,'//*!*']);
+
+for k=1:length(files)
+    filename=fullfile(files(k).folder,files(k).name);
+    file=fopen(filename);
+    L=textscan(file,'%s','Delimiter',':');
+    fclose('all');
+    rat=str2num(files(k).name(end-1:end));
+
+    if sum(rat==3 | rat==15)==0 %don't analyze rats without videos
+        %find start of A, start of C, and end of C
+        Jstrt=find(strcmp('J',L{1,1})); %Cue
+        Kstrt=find(strcmp('K',L{1,1})); %Sucrose Delivery
+        Ostrt=find(strcmp('O',L{1,1})); %Port entry times
+        Pstrt=find(strcmp('P',L{1,1})); %Port durations
+        Qstrt=find(strcmp('Q',L{1,1})); %Laser stimulations
+        Xstrt=find(strcmp('X',L{1,1})); %List (marks end of laser stims)
+
+        %trial times
+        choicet=(L{1,1}(Jstrt+2:2:Kstrt-1));
+        choicetime=[];
+        for i=1:length(choicet)
+            medtext=textscan(char(choicet(i,1)),'%f','Delimiter',' ');
+            ctime=medtext{1,1}(:,1);
+            choicetime=(cat(1,choicetime,ctime(isnan(ctime)==0)));
+        end
+        choicetime(choicetime==0)=[]; %delete 0s
+
+
+        %Sucrose delivery times
+        sucrosedel=(L{1,1}(Kstrt+2:2:Ostrt-1));
+        sucrosedeltime=[];
+        for i=1:length(sucrosedel)
+            medtext=textscan(char(sucrosedel(i,1)),'%f','Delimiter',' ');
+            sdtime=medtext{1,1}(:,1);
+            sucrosedeltime=(cat(1,sucrosedeltime,sdtime(isnan(sdtime)==0)));
+        end
+        sucrosedeltime(sucrosedeltime==0)=[]; %delete 0s
+
+        %Port entry times
+        pet=(L{1,1}(Ostrt+2:2:Pstrt-1));
+        petime=[];
+        for i=1:length(pet)
+            medtext=textscan(char(pet(i,1)),'%f','Delimiter',' ');
+            portentry=medtext{1,1}(:,1);
+            petime=(cat(1,petime,portentry(isnan(portentry)==0)));
+        end
+        removes=petime==0;
+        petime(petime==0)=[]; %delete 0s
+
+        %Port entry durations
+        durationt=(L{1,1}(Pstrt+2:2:Qstrt-1));
+        durationtime=[];
+        for i=1:length(durationt)
+            medtext=textscan(char(durationt(i,1)),'%f','Delimiter',' ');
+            duration=medtext{1,1}(:,1);
+            durationtime=(cat(1,durationtime,duration(isnan(duration)==0)));
+        end
+        durationtime(removes)=[]; %delete 0s
+
+        pexits=petime(1:length(durationtime))+durationtime;
+
+        %Laser stims
+        ST=(L{1,1}(Qstrt+2:2:Xstrt-1));
+        STtime=[];
+        for i=1:length(ST)
+            medtext=textscan(char(ST(i,1)),'%f','Delimiter',' ');
+            stime=medtext{1,1}(:,1);
+            STtime=(cat(1,STtime,stime(isnan(stime)==0)));
+        end
+
+        STtime(STtime==0)=[]; %delete 0s    
+
+
+        %Sucrose or water?
+        latency=[];
+        for i=1:length(choicetime)
+            sucrose=find(sucrosedeltime(:,1) > choicetime(i,1),1);
+            sucrosetime=sucrosedeltime(sucrose);
+            if sucrosetime
+                latency(i,1)=sucrosetime-choicetime(i,1)-0.5; %delay is 0.5s
+            else
+                latency(i,1)=NaN;
+            end
+        end
+        latency(latency>10)=NaN;
+
+        completed=latency>0;
+
+        %stim trials
+        post_stimmed=zeros(length(choicetime),1);
+        for stim=1:length(STtime)
+            trialtime=min(choicetime(choicetime>STtime(stim)));
+            if ~isempty(trialtime)
+                post_stimmed(choicetime==trialtime)=1;
+            end
+        end
+
+        %no stim trials
+        postsuc=zeros(length(choicetime),1);
+        for suctrial=1:length(sucrosedeltime)
+            trialtime=min(choicetime(choicetime>sucrosedeltime(suctrial)));
+            if ~isempty(trialtime)
+                postsuc(choicetime==trialtime)=1;
+            end
+        end
+        nostim=postsuc==1 & post_stimmed==0;    
+
+
+        rdtimes=sucrosedeltime;
+        cuetimes=choicetime;
+        petimes=petime;
+
+        included_cue_trials=[];
+        for trial=1:length(rdtimes)
+            if sum(cuetimes>rdtimes(trial))>0
+                included_cue_trials(trial,1)=find(cuetimes>rdtimes(trial),1,'first');
+            end
+        end
+        post_stim=post_stimmed(included_cue_trials);
+
+        %get coordinates from deeplabcut analysis
+        timestamps=VideoData{rat,1}-lighton(rat,1); %adjust for when session started in video
+        xcoordinates=VideoData{rat,2};
+        ycoordinates=VideoData{rat,3};
+
+
+        %for each cue onset
+        xypoints={};
+        xtrial=[];
+        ytrial=[];
+        ITI_distance=[];
+        ITI_dfp=[];
+        licks_trl=[];
+
+
+        for trial=1:length(cuetimes)
+            if sum(timestamps>(cuetimes(trial)-0.25) & timestamps<(cuetimes(trial)+0.25))>0
+                xtrial(trial)=mean(xcoordinates((timestamps>cuetimes(trial)-0.25) & (timestamps<cuetimes(trial)+0.25)));
+                ytrial(trial)=mean(ycoordinates((timestamps>cuetimes(trial)-0.25) & (timestamps<cuetimes(trial)+0.25)));            
+
+
+
+            elseif sum(timestamps>(cuetimes(trial)-1) & timestamps<(cuetimes(trial)+0.3))>0
+                xtrial(trial)=mean(xcoordinates((timestamps>cuetimes(trial)-1) & (timestamps<cuetimes(trial)+0.3)));
+                ytrial(trial)=mean(ycoordinates((timestamps>cuetimes(trial)-1) & (timestamps<cuetimes(trial)+0.3)));
+
+
+            else
+                xtrial(trial)=NaN;
+                ytrial(trial)=NaN;
+            end
+
+            %total distance traveled during ITI, and mean distance from port
+            binsize=0.2; %seconds
+
+            if sum(rdtimes<cuetimes(trial))>0 & postsuc(trial)==1
+
+                rd_time=max(rdtimes(rdtimes<cuetimes(trial)));
+                pe_time=max(petimes(petimes<(rd_time+posttime)));
+                start_time=min([pexits(petimes==pe_time) rd_time+posttime]);
+                %bins=start_time:binsize:cuetimes(trial);
+                bins=start_time:binsize:cuetimes(trial)+binsize/2;
+                bn=0;
+                bin_t=[];
+                bin_x=[];
+                bin_y=[];
+                bin_dfp=[];
+
+                %location for each bin
+                for bin=1:length(bins)-1
+                    if sum(timestamps>bins(bin) & timestamps<bins(bin+1))>0
+                        bn=bn+1;
+                        bin_t(bn,1)=bins(bin)+binsize/2;
+                        bin_x(bn,1)=mean(xcoordinates(timestamps>bins(bin) & timestamps<bins(bin+1)));
+                        bin_y(bn,1)=mean(ycoordinates(timestamps>bins(bin) & timestamps<bins(bin+1)));
+                        bin_dfp(bn,1)=sqrt((bin_x(bn,1)-portcoord(rat,1))^2+(bin_y(bn,1)-portcoord(rat,2))^2);
+                    end
+                end
+
+                xypoints{trial,1}=cat(2,bin_x,bin_y);
+                
+%                 subplot(5,5,rat);
+%                 hold on;
+%                 plot(bin_dfp);
+%                 xypoints{trial,1}=cat(2,bin_x,bin_y);
+%                 axis([0 25 0 500]);
+
+                ITI_dfp(trial)=trapz(bin_t,bin_dfp)/(bin_t(end)-bin_t(1)); %dfp = distance from port
+
+
+            else
+                ITI_dfp(trial)=NaN;
+                xypoints{trial,1}=NaN;
+            end
+        end
+
+
+        %plot example traces
+        if rat==8
+
+            figure;
+
+            %scatterplot of rat locations
+            xypoints=xypoints(included_cue_trials,:);
+            xtrial_inc=xtrial(included_cue_trials);
+            ytrial_inc=ytrial(included_cue_trials);
+            sucrosexy=cat(1,xypoints{post_stim==0});
+            maltodextrinxy=cat(1,xypoints{post_stim==1});
+            opacity=0.1;
+            dotsize=24;
+
+            sucrosetrls=xypoints(post_stim==0);
+            maltodextrintrls=xypoints(post_stim==1);
+
+            %sucrose traces
+            subplot(1,4,1);
+            hold on;
+            s1=scatter(sucrosexy(:,1),sucrosexy(:,2),dotsize,[.4 .4 .4],'filled');
+            s1.MarkerFaceAlpha = opacity;
+            sc=scatter(xtrial_inc(post_stim==0),ytrial_inc(post_stim==0),'k','x');
+            %plot individual trial traces
+            %             for i=1:length(sucrosetrls)
+            %                 if sucrosetrls{i,1}
+            %                     plot(sucrosetrls{i,1}(:,1),sucrosetrls{i,1}(:,2),'color',Colors('sucrose'));
+            %                 end
+            %             end
+
+            axis([0 1050 75 750]);
+            set(gca,'Ydir','reverse')
+            set(gca,'xtick',[]);
+            set(gca,'ytick',[]);
+            plot([950 portcoord(rat,1)],[450 portcoord(rat,2)],'color','k');
+            legend(sc,'Location at cue onset');
+            text(955,450,'Reward Port');
+            title('No laser');
+
+
+            %maltodextrin traces
+            subplot(1,4,2);
+            hold on;
+            s2=scatter(maltodextrinxy(:,1),maltodextrinxy(:,2),dotsize,[0 .3 1],'filled');
+            s2.MarkerFaceAlpha = opacity;
+            scatter(xtrial_inc(post_stim==1),ytrial_inc(post_stim==1),[],'k','x')
+ 
+            %plot individual trial traces
+            %             for i=1:length(maltodextrintrls)
+            %                 if maltodextrintrls{i,1}
+            %                     plot(maltodextrintrls{i,1}(:,1),maltodextrintrls{i,1}(:,2),'color',Colors('maltodextrin'));
+            %                 end
+            %             end
+
+            set(gca,'Ydir','reverse')
+            set(gca,'xtick',[]);
+            set(gca,'ytick',[]);
+            axis([0 1050 75 750]);
+            plot([950 portcoord(rat,1)],[450 portcoord(rat,2)],'color','k');
+            legend(sc,'Location at cue onset');
+            text(955,450,'Reward Port');
+            title('Laser');
+
+        end
+
+
+
+
+        ITI_dfp_inc=ITI_dfp(included_cue_trials)';
+
+
+
+        mean_distance(rat,1)=nanmean(ITI_dfp_inc(post_stim==0));
+        mean_distance(rat,2)=nanmean(ITI_dfp_inc(post_stim==1));
+
+
+
+        completed_trials(rat,1)=length(sucrosedeltime);
+    end
+
+    disp(['File #' num2str(k)]);
+end
+
+
+%% plotting measures
+
+%included rats
+yfp=[4 6 10 11 13 14 21];
+chr=[1 2 7 8 9 16 18 22 23 24];
+
+colors{2}=[0 0.2 1];
+colors{1}=[0.4 0.4 0.4];
+groups{1,1}=yfp;
+groups{2,1}=chr;
+
+subplot(1,8,5);
+hold on;
+plot([1 2],mean_distance(yfp,:),'color',[0.4 0.4 0.4]);
+errorbar(1,nanmean(mean_distance(yfp,1)),nanste(mean_distance(yfp,1),1),'color',[0.4 0.4 0.4],'marker','o','linewidth',1.5);
+errorbar(2,nanmean(mean_distance(yfp,2)),nanste(mean_distance(yfp,2),1),'color',[0 0.3 1],'marker','o','linewidth',1.5);
+ylabel('Distance from port during ITI (pixels)');
+xticks([1 2]);
+xticklabels({'No laser','Laser'});
+xtickangle(45);
+axis([0.5 2.5 0 400]);
+title('YFP');
+
+pval=signrank(mean_distance(yfp,1),mean_distance(yfp,2));
+text(1.2,375,['p = ' num2str(round(pval,5))]);
+
+
+subplot(1,8,6);
+hold on;
+plot([1 2],mean_distance(chr,:),'color',[0.4 0.4 0.4]);
+errorbar(1,nanmean(mean_distance(chr,1)),nanste(mean_distance(chr,1),1),'color',[0.4 0.4 0.4],'marker','o','markerfacecolor',[0.4 0.4 0.4],'linewidth',1.5);
+errorbar(2,nanmean(mean_distance(chr,2)),nanste(mean_distance(chr,2),1),'color',[0 0.3 1],'marker','o','markerfacecolor',[0 0.3 1],'linewidth',1.5);
+ylabel('Distance from port during ITI (pixels)');
+xticks([1 2]);
+xticklabels({'No laser','Laser'});
+xtickangle(45);
+axis([0.5 2.5 0 400]);
+title('ChR2');
+
+pval=signrank(mean_distance(chr,1),mean_distance(chr,2));
+text(1.2,375,['p = ' num2str(round(pval,5))]);
+
+included=zeros(24,1);
+included(chr)=1;
+included(yfp)=1;
+arclog=zeros(24,1);
+arclog(chr)=1;
+
+subplot(1,4,4);
+hold on;
+difference=(mean_distance(:,2)-mean_distance(:,1))./mean_distance(:,1);
+
+
+boxplot(difference(included==1),arclog(included==1),'colorgroup',[0 1],'colors',[colors{2};colors{2}],'boxstyle','filled');
+scatter(rand([length(yfp) 1])/4+1.2,difference(yfp),55,colors{2});
+scatter(rand([length(chr) 1])/4+2.2,difference(chr),55,colors{2},'filled');
+axis([0.7 2.74 -0.5 0.5]);
+plot([0 3],[0 0],'color','k');
+pval=ranksum(difference(yfp),difference(chr));
+text(1.2,0.4,['p = ' num2str(round(pval,5))]);
+ylabel('Fold-change over no laser');
+xticks([1.22 2.22]);
+xticklabels({'YFP','ChR2'});
+
+
+dataforanova=[mean_distance(chr,1) mean_distance(chr,2);mean_distance(yfp,1) mean_distance(yfp,2)];

+ 396 - 0
MatlabScripts/3_Figures/AdditionalFigures/opto/VideoAnalysis_ChR2Cue.m

@@ -0,0 +1,396 @@
+clear all;
+analyzedlcfiles=0;
+%% get data from DeepLabCut files
+
+if analyzedlcfiles
+
+    ffmpeg_path='C:\FFmpeg\bin';
+    address=['']; %where are the running files located
+    files=dir([address,'\\*_*']);
+
+    videoaddress=[''];
+    videofiles=dir([videoaddress,'\\*Ch*']);
+
+    for k=1:length(files)
+        filename=fullfile(files(k).folder,files(k).name);
+        rat=str2num(files(k).name(9:10)); %rat #
+        videofile=[videofiles(k).folder '\' videofiles(k).name];
+
+        %get timestamp data
+        [timestamps,xcoordinates,ycoordinates]=AnalyzeDeepLabCutPP(filename,videofile,ffmpeg_path);
+
+        VideoData{rat,1}=timestamps;
+        VideoData{rat,2}=xcoordinates;
+        VideoData{rat,3}=ycoordinates;
+
+    end
+
+    save('CueVideoData.mat','VideoData');
+
+else
+    load('CueVideoData.mat');
+
+end
+%%
+
+spreadsheet=importdata('CueVideoKey.xlsx');
+spreadsheetrat=spreadsheet.textdata(2:end,1);
+for i=1:length(spreadsheetrat)
+    sprat(i,1)=str2num(spreadsheetrat{i,1}(3:4));
+end
+
+lighton(sprat,1)=spreadsheet.data(:,1);
+portcoord(sprat,1)=spreadsheet.data(:,2);
+portcoord(sprat,2)=spreadsheet.data(:,3);
+%cutoff(sprat,1)=spreadsheet.data(:,5);
+
+Colors = load_colors();
+trlactivity={[];[];[]};
+trldistance={[];[];[]};
+
+address=['/Data/ChR2CueBehavior'];
+%files=dir([address,'\\*!*']);
+files=dir([address,'//*!*']); %mac
+
+for k=1:length(files)
+    filename=fullfile(files(k).folder,files(k).name);
+    file=fopen(filename);
+    L=textscan(file,'%s','Delimiter',':');
+    fclose('all');
+    rat=str2num(files(k).name(end-1:end));
+
+    if sum(rat==3 | rat==15)==0 %don't analyze rats without videos
+        %find start of A, start of C, and end of C
+        Jstrt=find(strcmp('J',L{1,1})); %Cue
+        Kstrt=find(strcmp('K',L{1,1})); %Sucrose Delivery
+        Ostrt=find(strcmp('O',L{1,1})); %Port entry times
+        Pstrt=find(strcmp('P',L{1,1})); %Port durations
+        Qstrt=find(strcmp('Q',L{1,1})); %Laser stimulations
+        Xstrt=find(strcmp('X',L{1,1})); %List (marks end of laser stims)
+
+        %trial times
+        choicet=(L{1,1}(Jstrt+2:2:Kstrt-1));
+        choicetime=[];
+        for i=1:length(choicet)
+            medtext=textscan(char(choicet(i,1)),'%f','Delimiter',' ');
+            ctime=medtext{1,1}(:,1);
+            choicetime=(cat(1,choicetime,ctime(isnan(ctime)==0)));
+        end
+        choicetime(choicetime==0)=[]; %delete 0s
+
+
+        %Sucrose delivery times
+        sucrosedel=(L{1,1}(Kstrt+2:2:Ostrt-1));
+        sucrosedeltime=[];
+        for i=1:length(sucrosedel)
+            medtext=textscan(char(sucrosedel(i,1)),'%f','Delimiter',' ');
+            sdtime=medtext{1,1}(:,1);
+            sucrosedeltime=(cat(1,sucrosedeltime,sdtime(isnan(sdtime)==0)));
+        end
+        sucrosedeltime(sucrosedeltime==0)=[]; %delete 0s
+
+        %Port entry times
+        pet=(L{1,1}(Ostrt+2:2:Pstrt-1));
+        petime=[];
+        for i=1:length(pet)
+            medtext=textscan(char(pet(i,1)),'%f','Delimiter',' ');
+            portentry=medtext{1,1}(:,1);
+            petime=(cat(1,petime,portentry(isnan(portentry)==0)));
+        end
+        removes=petime==0;
+        petime(petime==0)=[]; %delete 0s
+
+        %Port entry durations
+        durationt=(L{1,1}(Pstrt+2:2:Qstrt-1));
+        durationtime=[];
+        for i=1:length(durationt)
+            medtext=textscan(char(durationt(i,1)),'%f','Delimiter',' ');
+            duration=medtext{1,1}(:,1);
+            durationtime=(cat(1,durationtime,duration(isnan(duration)==0)));
+        end
+        durationtime(removes)=[]; %delete 0s
+
+        pexits=petime(1:length(durationtime))+durationtime;
+
+        %Laser stims
+        ST=(L{1,1}(Qstrt+2:2:Xstrt-1));
+        STtime=[];
+        for i=1:length(ST)
+            medtext=textscan(char(ST(i,1)),'%f','Delimiter',' ');
+            stime=medtext{1,1}(:,1);
+            STtime=(cat(1,STtime,stime(isnan(stime)==0)));
+        end
+
+        STtime(STtime==0)=[]; %delete 0s    
+
+
+        %Sucrose or water?
+        latency=[];
+        for i=1:length(choicetime)
+            sucrose=find(sucrosedeltime(:,1) > choicetime(i,1),1);
+            sucrosetime=sucrosedeltime(sucrose);
+            if sucrosetime
+                latency(i,1)=sucrosetime-choicetime(i,1)-0.5; %delay is 0.5s
+            else
+                latency(i,1)=NaN;
+            end
+        end
+        latency(latency>10)=NaN;
+
+        completed=latency>0;
+
+        %stim trials
+        post_stimmed=zeros(length(choicetime),1);
+        for stim=1:length(STtime)
+            trialtime=min(choicetime(choicetime>STtime(stim)));
+            if ~isempty(trialtime)
+                post_stimmed(choicetime==trialtime)=1;
+            end
+        end
+
+        %no stim trials
+        postsuc=zeros(length(choicetime),1);
+        for suctrial=1:length(sucrosedeltime)
+            trialtime=min(choicetime(choicetime>sucrosedeltime(suctrial)));
+            if ~isempty(trialtime)
+                postsuc(choicetime==trialtime)=1;
+            end
+        end
+        nostim=postsuc==1 & post_stimmed==0;    
+
+
+        rdtimes=sucrosedeltime;
+        cuetimes=choicetime;
+        petimes=petime;
+
+        included_cue_trials=[];
+        for trial=1:length(rdtimes)
+            if sum(cuetimes>rdtimes(trial))>0
+                included_cue_trials(trial,1)=find(cuetimes>rdtimes(trial),1,'first');
+            end
+        end
+        post_stim=post_stimmed(included_cue_trials);
+
+        %get coordinates from deeplabcut analysis
+        timestamps=VideoData{rat,1}-lighton(rat,1); %adjust for when session started in video
+        xcoordinates=VideoData{rat,2};
+        ycoordinates=VideoData{rat,3};
+
+
+        %for each cue onset
+        xypoints={};
+        xtrial=[];
+        ytrial=[];
+        ITI_distance=[];
+        ITI_dfp=[];
+        licks_trl=[];
+
+
+        for trial=1:length(cuetimes)
+            if sum(timestamps>(cuetimes(trial)-0.25) & timestamps<(cuetimes(trial)+0.25))>0
+                xtrial(trial)=mean(xcoordinates((timestamps>cuetimes(trial)-0.25) & (timestamps<cuetimes(trial)+0.25)));
+                ytrial(trial)=mean(ycoordinates((timestamps>cuetimes(trial)-0.25) & (timestamps<cuetimes(trial)+0.25)));            
+
+
+
+            elseif sum(timestamps>(cuetimes(trial)-1) & timestamps<(cuetimes(trial)+0.3))>0
+                xtrial(trial)=mean(xcoordinates((timestamps>cuetimes(trial)-1) & (timestamps<cuetimes(trial)+0.3)));
+                ytrial(trial)=mean(ycoordinates((timestamps>cuetimes(trial)-1) & (timestamps<cuetimes(trial)+0.3)));
+
+
+            else
+                xtrial(trial)=NaN;
+                ytrial(trial)=NaN;
+            end
+
+            %total distance traveled during ITI, and mean distance from port
+            binsize=0.2; %seconds
+
+            if sum(rdtimes<cuetimes(trial))>0 & postsuc(trial)==1
+
+                rd_time=max(rdtimes(rdtimes<cuetimes(trial)));
+                pe_time=max(petimes(petimes<(rd_time+10)));
+                start_time=min([pexits(petimes==pe_time) rd_time+10]);
+                %bins=start_time:binsize:cuetimes(trial);
+                bins=start_time:binsize:cuetimes(trial)+binsize/2;
+                bn=0;
+                bin_t=[];
+                bin_x=[];
+                bin_y=[];
+                bin_dfp=[];
+
+                %location for each bin
+                for bin=1:length(bins)-1
+                    if sum(timestamps>bins(bin) & timestamps<bins(bin+1))>0
+                        bn=bn+1;
+                        bin_t(bn,1)=bins(bin)+binsize/2;
+                        bin_x(bn,1)=mean(xcoordinates(timestamps>bins(bin) & timestamps<bins(bin+1)));
+                        bin_y(bn,1)=mean(ycoordinates(timestamps>bins(bin) & timestamps<bins(bin+1)));
+                        bin_dfp(bn,1)=sqrt((bin_x(bn,1)-portcoord(rat,1))^2+(bin_y(bn,1)-portcoord(rat,2))^2);
+                    end
+                end
+                
+                xypoints{trial,1}=cat(2,bin_x,bin_y);
+%                 subplot(5,5,rat);
+%                 hold on;
+%                 plot(bin_dfp);
+%                 xypoints{trial,1}=cat(2,bin_x,bin_y);
+%                 axis([0 25 0 500]);
+
+                ITI_dfp(trial)=trapz(bin_t,bin_dfp)/(bin_t(end)-bin_t(1)); %dfp = distance from port
+
+
+            else
+                ITI_dfp(trial)=NaN;
+                xypoints{trial,1}=NaN;
+            end
+        end
+
+        
+
+        %plot example traces
+        if rat==17
+
+            figure;
+
+            %scatterplot of rat locations
+            xypoints=xypoints(included_cue_trials,:);
+            xtrial_inc=xtrial(included_cue_trials);
+            ytrial_inc=ytrial(included_cue_trials);
+            sucrosexy=cat(1,xypoints{post_stim==0});
+            maltodextrinxy=cat(1,xypoints{post_stim==1});
+            opacity=0.1;
+            dotsize=24;
+
+            sucrosetrls=xypoints(post_stim==0);
+            maltodextrintrls=xypoints(post_stim==1);
+
+            %sucrose traces
+            subplot(1,4,1);
+            hold on;
+            s1=scatter(sucrosexy(:,1),sucrosexy(:,2),dotsize,[.4 .4 .4],'filled');
+            s1.MarkerFaceAlpha = opacity;
+            sc=scatter(xtrial_inc(post_stim==0),ytrial_inc(post_stim==0),'k','x');
+
+
+            axis([0 950 75 750]);
+            set(gca,'Ydir','reverse')
+            set(gca,'xtick',[]);
+            set(gca,'ytick',[]);
+            plot([950 portcoord(rat,1)],[450 portcoord(rat,2)],'color','k');
+            legend(sc,'Location at cue onset');
+            text(955,450,'Reward Port');
+            title('No laser');
+
+
+            %maltodextrin traces
+            subplot(1,4,2);
+            hold on;
+            s2=scatter(maltodextrinxy(:,1),maltodextrinxy(:,2),dotsize,[0 .3 1],'filled');
+            s2.MarkerFaceAlpha = opacity;
+            scatter(xtrial_inc(post_stim==1),ytrial_inc(post_stim==1),[],'k','x')
+
+            %plot individual trial traces
+            %             for i=1:length(maltodextrintrls)
+            %                 if maltodextrintrls{i,1}
+            %                     plot(maltodextrintrls{i,1}(:,1),maltodextrintrls{i,1}(:,2),'color',Colors('maltodextrin'));
+            %                 end
+            %             end
+
+            set(gca,'Ydir','reverse')
+            set(gca,'xtick',[]);
+            set(gca,'ytick',[]);
+            axis([0 950 75 750]);
+            plot([950 portcoord(rat,1)],[450 portcoord(rat,2)],'color','k');
+            legend(sc,'Location at cue onset');
+            text(955,450,'Reward Port');
+            title('Laser');
+
+        end
+
+
+
+
+        ITI_dfp_inc=ITI_dfp(included_cue_trials)';
+
+
+
+        mean_distance(rat,1)=nanmean(ITI_dfp_inc(post_stim==0));
+        mean_distance(rat,2)=nanmean(ITI_dfp_inc(post_stim==1));
+
+
+
+        completed_trials(rat,1)=length(sucrosedeltime);
+    end
+
+    disp(['File #' num2str(k)]);
+end
+
+
+%% plotting measures
+
+%included rats
+yfp=[4 6 10 11 13 14 21];
+chr=[1 2 8 9 16 17 18 22 23 24];
+
+colors{2}=[0 0.3 1];
+colors{1}=[0.4 0.4 0.4];
+groups{1,1}=yfp;
+groups{2,1}=chr;
+
+
+subplot(1,8,5);
+hold on;
+plot([1 2],mean_distance(yfp,:),'color',[0.4 0.4 0.4]);
+errorbar(1,nanmean(mean_distance(yfp,1)),nanste(mean_distance(yfp,1),1),'color',[0.4 0.4 0.4],'marker','o','linewidth',1.5);
+errorbar(2,nanmean(mean_distance(yfp,2)),nanste(mean_distance(yfp,2),1),'color',[0 0.3 1],'marker','o','linewidth',1.5);
+ylabel('Distance from port during ITI (pixels)');
+xticks([1 2]);
+xticklabels({'No laser','Laser'});
+xtickangle(45);
+axis([0.5 2.5 0 400]);
+title('YFP');
+
+pval=signrank(mean_distance(yfp,1),mean_distance(yfp,2));
+text(1.2,375,['p = ' num2str(round(pval,2,'significant'))]);
+
+
+subplot(1,8,6);
+hold on;
+plot([1 2],mean_distance(chr,:),'color',[0.4 0.4 0.4]);
+errorbar(1,nanmean(mean_distance(chr,1)),nanste(mean_distance(chr,1),1),'color',[0.4 0.4 0.4],'marker','o','markerfacecolor',[0.4 0.4 0.4],'linewidth',1.5);
+errorbar(2,nanmean(mean_distance(chr,2)),nanste(mean_distance(chr,2),1),'color',[0 0.3 1],'marker','o','markerfacecolor',[0 0.3 1],'linewidth',1.5);
+ylabel('Distance from port during ITI (pixels)');
+xticks([1 2]);
+xticklabels({'No laser','Laser'});
+xtickangle(45);
+axis([0.5 2.5 0 400]);
+title('ChR2');
+
+pval=signrank(mean_distance(chr,1),mean_distance(chr,2));
+text(1.2,375,['p = ' num2str(round(pval,2,'significant'))]);
+
+included=zeros(24,1);
+included(chr)=1;
+included(yfp)=1;
+arclog=zeros(24,1);
+arclog(chr)=1;
+
+subplot(1,4,4);
+hold on;
+difference=(mean_distance(:,2)-mean_distance(:,1))./mean_distance(:,1);
+
+
+boxplot(difference(included==1),arclog(included==1),'colorgroup',[0 1],'colors',[colors{2};colors{2}],'boxstyle','filled');
+scatter(rand([length(yfp) 1])/4+1.2,difference(yfp),55,colors{2});
+scatter(rand([length(chr) 1])/4+2.2,difference(chr),55,colors{2},'filled');
+axis([0.7 2.74 -0.5 0.5]);
+plot([0 3],[0 0],'color','k');
+pval=ranksum(difference(yfp),difference(chr));
+text(1.2,0.4,['p = ' num2str(round(pval,2,'significant'))]);
+ylabel('Fold-change over no laser');
+xticks([1.22 2.22]);
+xticklabels({'YFP','ChR2'});
+
+
+dataforanova=[mean_distance(chr,1) mean_distance(chr,2);mean_distance(yfp,1) mean_distance(yfp,2)];

+ 331 - 0
MatlabScripts/3_Figures/AdditionalFigures/threeOutcomes/VideoAnalysis_TH.m

@@ -0,0 +1,331 @@
+clear all;
+
+files={'Three1_42DeepCut_resnet50_ThreeAug21shuffle1_994000.h5';...
+        'Three2_42DeepCut_resnet50_ThreeAug21shuffle1_994000.h5';...
+        'Three3_51DeepCut_resnet50_ThreeAug21shuffle1_994000.h5';...
+        'Three4_54DeepCut_resnet50_ThreeAug21shuffle1_994000.h5'};
+    
+portcoord=[60 70;60 70;60 70;60 70];
+
+load('RAWTH'); RAW=RAWTH;
+load('ModData_TH.mat');
+load ('threeOutcomes_MLEfits.mat');
+bm_RD=select_RPEmods(os, 'RD', 'particularModels', {'mean','curr','base'},'plotmodels_Flag',false);
+bm_cue=select_RPEmods(os, 'cue', 'particularModels', {'mean','base'},'plotmodels_Flag',false);
+ 
+included=true(length(TH.RDHz),1); %all neurons
+
+Predictors=TH.Predictors(included);
+RDHz=TH.RDHz(included);
+CueHz=TH.CueHzAll(included);
+
+Colors = load_colors();
+[magma,inferno,plasma,viridis]=colormaps;
+masks=cat(2,bm_RD.mask_base(included)',bm_RD.mask_curr(included)',bm_RD.mask_mean(included)');
+cue_masks=cat(2,bm_cue.mask_base(included)',bm_cue.mask_mean(included)');
+
+for session=1:length(RAW)
+    %get coordinates from deeplabcut analysis
+    [DLC.timestamps{session,1},DLC.xcoordinates{session,1},DLC.ycoordinates{session,1}]=AnalyzeDeepLabCut(files{session});
+end
+
+
+%%
+NN=0;
+trlactivity={[];[];[]};
+trldistance={[];[];[]};
+all_distance=[];
+all_preds=[];
+
+for session=1:length(RAW)
+    %get coordinates from deeplabcut analysis
+    [timestamps,xcoordinates,ycoordinates]=AnalyzeDeepLabCut(files{session});
+    
+    %get event times
+    cue = strcmp('Cue',RAW(session).Einfo(:,2));
+    cuetimes = RAW(session).Erast{cue};
+    pe = strcmp('PE',RAW(session).Einfo(:,2));
+    petimes = RAW(session).Erast{pe};
+    rd = strcmp('RD',RAW(session).Einfo(:,2));
+    rdtimes = RAW(session).Erast{rd};
+    lick = strcmp('Licks',RAW(session).Einfo(:,2));
+    licks = RAW(session).Erast{lick};
+    
+    %find included trials
+    included_RDs=rdtimes<cuetimes(end);
+    included_cue_trials=[];
+    for trial=1:length(rdtimes)
+        if sum(cuetimes>rdtimes(trial))>0
+            included_cue_trials(trial,1)=find(cuetimes>rdtimes(trial),1,'first');
+        end
+    end
+    
+    %for each cue onset
+    xypoints={};
+    xtrial=[];
+    ytrial=[];
+    ITI_dfp=[];
+    
+    for trial=1:length(cuetimes)
+        
+        %coordinates at cue onset
+        if sum(timestamps>(cuetimes(trial)-0.15) & timestamps<(cuetimes(trial)+0.15))>0
+            xtrial(trial)=mean(xcoordinates((timestamps>cuetimes(trial)-0.15) & (timestamps<cuetimes(trial)+0.15)));
+            ytrial(trial)=mean(ycoordinates((timestamps>cuetimes(trial)-0.15) & (timestamps<cuetimes(trial)+0.15)));      
+        else
+            xtrial(trial)=NaN;
+            ytrial(trial)=NaN;
+        end
+        
+        %total distance traveled during ITI, and mean distance from port
+        binsize=0.2; %seconds
+
+        if sum(rdtimes<cuetimes(trial))>0 & sum(included_cue_trials==trial)>0
+            rd_time=max(rdtimes(rdtimes<cuetimes(trial)));
+            if licks>rd_time & licks<(rd_time+15)
+                start_time=max(licks(licks>rd_time & licks<(rd_time+15)));
+            else
+                start_time=rd_time+5;
+            end
+            %bins=start_time:binsize:cuetimes(trial);
+            bins=start_time:binsize:cuetimes(trial)+binsize/2;
+            bn=0;
+            bin_t=[];
+            bin_x=[];
+            bin_y=[];
+            bin_dfp=[];
+            
+            %location for each bin
+            for bin=1:length(bins)-1
+                if sum(timestamps>bins(bin) & timestamps<bins(bin+1))>0
+                    bn=bn+1;
+                    bin_t(bn,1)=bins(bin)+binsize/2;
+                    bin_x(bn,1)=mean(xcoordinates(timestamps>bins(bin) & timestamps<bins(bin+1)));
+                    bin_y(bn,1)=mean(ycoordinates(timestamps>bins(bin) & timestamps<bins(bin+1)));
+                    bin_dfp(bn,1)=sqrt((bin_x(bn,1)-portcoord(session,1))^2+(bin_y(bn,1)-portcoord(session,2))^2);
+                end
+            end
+            
+            norm_time=(bin_t-bin_t(1))/(bin_t(end)-bin_t(1));
+            
+            xypoints{trial,1}=cat(2,bin_x,bin_y);
+            
+            ITI_dfp(trial)=trapz(bin_t,bin_dfp)/(bin_t(end)-bin_t(1)); %dfp = distance from port
+            
+           
+        else
+            ITI_dfp(trial)=NaN;
+            xypoints{trial,1}=NaN;
+        end
+    end
+    
+    %only look at included trials
+    distance_inc=ITI_dfp(included_cue_trials)';    
+   
+    %relate to neural activity
+    noi=[1:length(RAW(session).Nrast)]+NN;
+    noi_activity=RDHz(noi);
+    noi_activity_cue=CueHz(noi);
+
+    sessions=[3];
+    for neuron=1:length(noi)
+        NN=NN+1;
+        
+        %plot example traces
+        if neuron==1 & sum(sessions==session)>0
+             
+            %scatterplot of rat locations
+            xypoints=xypoints(included_cue_trials,:);
+            xtrial_inc=xtrial(included_cue_trials);
+            ytrial_inc=ytrial(included_cue_trials);
+            sucrosexy=cat(1,xypoints{Predictors{NN,1}(1:sum(included_RDs),1)==1});
+            maltodextrinxy=cat(1,xypoints{Predictors{NN,1}(1:sum(included_RDs),1)==0});
+            waterxy=cat(1,xypoints{Predictors{NN,1}(1:sum(included_RDs),1)==2});
+
+            opacity=0.1;
+            dotsize=24;
+            
+            sucrosetrls=xypoints(Predictors{NN,1}(1:sum(included_RDs),1)==1);
+            maltodextrintrls=xypoints(Predictors{NN,1}(1:sum(included_RDs),1)==0);
+            watertrls=xypoints(Predictors{NN,1}(1:sum(included_RDs),1)==2);
+            
+            %sucrose traces
+            figure;
+            subplot(2,4,1);
+            hold on;
+            s1=scatter(sucrosexy(:,1),sucrosexy(:,2),dotsize,Colors('sucrose'),'filled');
+            s1.MarkerFaceAlpha = opacity;
+            sc=scatter(xtrial_inc(Predictors{NN,1}(1:sum(included_RDs),1)==1),ytrial_inc(Predictors{NN,1}(1:sum(included_RDs),1)==1),'k','x');
+            %plot individual trial traces
+%             for i=1:length(sucrosetrls)
+%                 if sucrosetrls{i,1}
+%                     plot(sucrosetrls{i,1}(:,1),sucrosetrls{i,1}(:,2),'color',Colors('sucrose'));
+%                 end
+%             end
+            
+            axis([0 450 0 350]);
+            set(gca,'Ydir','reverse')
+            set(gca,'xtick',[]);
+            set(gca,'ytick',[]);
+            if session>5 plot([40 60],[25 70],'color','k'); end
+            if session<=5 plot([40 50],[25 65],'color','k'); end
+            legend(sc,'Location at cue onset');
+            text(5,10,'Reward Port');
+            title('Location post-sucrose');
+
+            
+            %maltodextrin traces
+            subplot(2,4,2);
+            hold on;
+            s2=scatter(maltodextrinxy(:,1),maltodextrinxy(:,2),dotsize,Colors('maltodextrin'),'filled');
+            s2.MarkerFaceAlpha = opacity;
+            scatter(xtrial_inc(Predictors{NN,1}(1:sum(included_RDs),1)==0),ytrial_inc(Predictors{NN,1}(1:sum(included_RDs),1)==0),[],'k','x')
+            
+            %plot individual trial traces
+%             for i=1:length(maltodextrintrls)
+%                 if maltodextrintrls{i,1}
+%                     plot(maltodextrintrls{i,1}(:,1),maltodextrintrls{i,1}(:,2),'color',Colors('maltodextrin'));
+%                 end
+%             end
+            
+            set(gca,'Ydir','reverse')
+            set(gca,'xtick',[]);
+            set(gca,'ytick',[]);
+            axis([0 450 0 350]);
+            if session>5 plot([40 60],[25 70],'color','k'); end
+            if session<=5 plot([40 50],[25 65],'color','k'); end
+            text(5,10,'Reward Port');
+            title('Location post-maltodextrin');
+
+            %water traces
+            subplot(2,4,3);
+            hold on;
+            s2=scatter(waterxy(:,1),waterxy(:,2),dotsize,Colors('water'),'filled');
+            s2.MarkerFaceAlpha = opacity;
+            scatter(xtrial_inc(Predictors{NN,1}(1:sum(included_RDs),1)==0),ytrial_inc(Predictors{NN,1}(1:sum(included_RDs),1)==0),[],'k','x')
+            
+            %plot individual trial traces
+%             for i=1:length(maltodextrintrls)
+%                 if maltodextrintrls{i,1}
+%                     plot(maltodextrintrls{i,1}(:,1),maltodextrintrls{i,1}(:,2),'color',Colors('maltodextrin'));
+%                 end
+%             end
+            
+            set(gca,'Ydir','reverse')
+            set(gca,'xtick',[]);
+            set(gca,'ytick',[]);
+            axis([0 450 0 350]);
+            if session>5 plot([40 60],[25 70],'color','k'); end
+            if session<=5 plot([40 50],[25 65],'color','k'); end
+            text(5,10,'Reward Port');
+            title('Location post-water');            
+          end
+
+
+        [distance_corr_cue(NN,1),distance_corr_cue(NN,2)]=corr(noi_activity_cue{neuron,1}(included_cue_trials,1),distance_inc,'rows','complete','type','spearman');
+        [distance_corr(NN,1),distance_corr(NN,2)]=corr(noi_activity{neuron,1}(included_RDs,1),distance_inc,'rows','complete','type','spearman');
+
+        
+        %shuffled controls
+        for i=1:1000
+            distance_shuff=distance_inc(randperm(length(distance_inc)));
+            dist_shuff_corr(NN,i)=corr(noi_activity{neuron,1}(included_RDs,1),distance_shuff,'rows','complete','type','spearman');
+            dist_shuff_corr_cue(NN,i)=corr(noi_activity_cue{neuron,1}(included_cue_trials,1),distance_shuff,'rows','complete','type','spearman');
+
+        end
+        
+        if neuron==1 %only do this once per session
+        
+            suc_distance(session,1)=nanmean(distance_inc(Predictors{NN,1}(1:sum(included_RDs),1)==1));
+            mal_distance(session,1)=nanmean(distance_inc(Predictors{NN,1}(1:sum(included_RDs),1)==0));
+            wat_distance(session,1)=nanmean(distance_inc(Predictors{NN,1}(1:sum(included_RDs),1)==2));
+            
+            distance_inc_norm = (distance_inc - nanmean(distance_inc))/nanstd(distance_inc);
+            
+            all_distance = cat(1,all_distance,distance_inc_norm);
+            all_preds = cat(1,all_preds,Predictors{NN,1}(1:sum(included_RDs),:));
+            
+        end
+        
+ 
+
+        
+        
+    end
+    
+    disp(['Session #' num2str(session)]);
+    
+end
+
+%% plotting distance from port graphs
+
+subplot(2,6,6);
+hold on;
+plot([1:3],[suc_distance mal_distance wat_distance],'color',[0.6 0.6 0.6]);
+errorbar(1,nanmean(suc_distance),nanste(suc_distance,1),'color',Colors('sucrose'),'marker','o','linewidth',1.5);
+errorbar(2,nanmean(mal_distance),nanste(mal_distance,1),'color',Colors('maltodextrin'),'marker','o','linewidth',1.5);
+errorbar(3,nanmean(wat_distance),nanste(wat_distance,1),'color',Colors('water'),'marker','o','linewidth',1.5);
+ylabel('Distance from port during ITI (pixels)');
+xticks([1:3]);
+xticklabels({'Post-suc','Post-mal','Post-wat'});
+xtickangle(45);
+axis([0.5 3.5 0 200]);
+
+%stats
+%signrank(suc_distance,wat_distance);
+%anova1([suc_distance mal_distance wat_distance]);
+[r,p] = corr(cat(1,suc_distance,mal_distance,wat_distance),cat(1,3*ones(4,1),2*ones(4,1),ones(4,1)),'type','spearman');
+
+colors{1,1}=Colors('rpe');
+colors{2,1}=Colors('current');
+colors{3,1}=Colors('mean');
+
+subplot(2,4,5);
+hold on;
+plots={};
+for mask=1:length(masks(1,:))
+    [cdf,x] = ecdf(distance_corr(masks(:,mask)));
+    plots{mask} = plot(x,cdf,'linewidth',1.5,'color',colors{mask,1});
+    plot([nanmean(distance_corr(masks(:,mask))) nanmean(distance_corr(masks(:,mask)))],[0 1],'color',colors{mask,1},'linewidth',1);
+end
+%histogram(distance_corr(masks(:,1)),-0.5:0.05:0.5,'normalization','probability','edgecolor','none','facecolor',Colors('rpe'));
+plot([0 0],[0 1],'color','k');
+plot([-0.5 0.5],[0.5 0.5],'color','k');
+axis([-.5 .5 0 1]);
+legend([plots{:}],'RPE','Current','Unmod.','location','southeast')
+xlabel('Spearman''s rho');
+ylabel('Cumulative fraction of neurons');
+title('Correlation between VP RD activity and ITI distance');
+text(-0.2,1,'*','color','k','fontsize',24);
+
+%stats tests
+%ranksum(distance_corr(masks(:,1)),distance_corr(masks(:,3)));
+%signrank(distance_corr(masks(:,2)),dist_shuff_corr(masks(:,2)));
+
+%% cue value
+colors{1,1}=Colors('rpe');
+colors{2,1}=Colors('mean');
+
+
+subplot(2,4,8);
+hold on;
+plots={};
+for mask=1:length(cue_masks(1,:))
+    [cdf,x] = ecdf(distance_corr_cue(cue_masks(:,mask)));
+    %[cdf,x] = ecdf(dist_shuff_corr_cue(cue_masks(:,mask)));
+    plots{mask} = plot(x,cdf,'linewidth',1.5,'color',colors{mask,1});
+    plot([mean(distance_corr_cue(cue_masks(:,mask))) mean(distance_corr_cue(cue_masks(:,mask)))],[0 1],'color',colors{mask,1},'linewidth',1);
+end
+%histogram(distance_corr(masks(:,1)),-0.5:0.05:0.5,'normalization','probability','edgecolor','none','facecolor',Colors('rpe'));
+plot([0 0],[0 1],'color','k');
+plot([-0.5 0.5],[0.5 0.5],'color','k');
+axis([-.5 .5 0 1]);
+legend([plots{:}],'Value','Unmod.','location','northwest')
+xlabel('Spearman''s rho');
+ylabel('Cumulative fraction of neurons');
+title('Correlation between VP cue activity and ITI distance');
+text(-0.2,1,'*','color','k','fontsize',24);
+
+%stats tests
+%ranksum(distance_corr_cue(cue_masks(:,1)),distance_corr_cue(cue_masks(:,2)));
+%signrank(distance_corr_cue(cue_masks(:,1)),dist_shuff_corr_cue(cue_masks(:,1)));

+ 187 - 0
MatlabScripts/3_Figures/AdditionalFigures/threeOutcomes/plot_latentVariables.m

@@ -0,0 +1,187 @@
+clear; clc
+
+load(fullfile(ottBari2020_root, 'Data', 'Modeling', 'ModelFits', 'threeOutcomes_MLEfits.mat'));
+myColors = importColors_bb;
+VP_color = myColors.bluishGreen;
+
+% get relevant behavior models
+modelCriterion = 'AIC';
+plotFlag = false;
+
+models_of_interest_RPE = {'base','curr','mean'};
+models_of_interest_V = {'base','mean'};
+
+timePeriod = 'RD';
+bm_RD = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,'plotModels_Flag',plotFlag, ...
+                          'particularModel', models_of_interest_RPE);
+
+timePeriod = 'cue';
+bm_cue = select_RPEmods(os, timePeriod,'scoreToUse',modelCriterion,'plotModels_Flag',plotFlag, ...
+                           'particularModels', models_of_interest_V);
+
+%%
+timePeriod = 'RD'; % RD, cue
+normalization = 'zscore'; % none, zscore, minmax
+
+mod_type = ['mod_' timePeriod];
+switch timePeriod
+    case 'RD'
+        latent_var = 'RPEs';
+        bm = bm_RD;
+    case 'cue'
+        latent_var = 'V';
+        bm = bm_cue;
+    otherwise
+        error('timePeriod not found')
+end
+
+nSim = 501; % must be odd
+
+% VP first
+all_latent_VP = [];
+norm_fr_real_VP = [];
+norm_fr_sim_VP = [];
+
+mean_real_VP = [];
+mean_pred_VP = [];
+corr_spike_count_VP = [];
+var_real_VP = [];
+var_pred_VP = [];
+trialComparison_pred_VP = [];
+for n = find(bm.mask_base)
+    sign_flip = sign(os(n).(mod_type).base.bestParams(2));
+    
+    real_spike_count = os(n).(['spikeCount_' timePeriod])';
+    
+    % generate nSim predicted spike counts
+    pred_spike_count = [];
+    for i = 1:nSim
+        pred_spike_count(i,:) = poissrnd(os(n).(mod_type).base.mean_predictedSpikes)';
+    end
+    tmp_corr = corr(real_spike_count', pred_spike_count');
+    
+    % get median correlation
+    corr_spike_count_VP = [corr_spike_count_VP median(tmp_corr)];
+    
+    % save that median neuron
+    median_ind = find(tmp_corr == median(tmp_corr), 1);
+        
+    % get latent variables for plotting
+    tmp_latent = os(n).(mod_type).base.(latent_var)';
+    % normalize
+    switch normalization
+        case 'none'
+        case 'zscore'
+            tmp_latent = normalize(tmp_latent);
+        case 'minmax'
+            norm_const = 1/max(abs(tmp_latent));
+            tmp_latent = norm_const*tmp_latent;
+    end
+    all_latent_VP = [all_latent_VP tmp_latent];      
+    
+    % normalize real and predicted spike counts for tuning curves
+    norm_fr_real_VP = [norm_fr_real_VP sign_flip*normalize(real_spike_count)];
+    norm_fr_sim_VP = [norm_fr_sim_VP sign_flip*normalize(pred_spike_count(median_ind, :))];
+    
+    % mean spike counts
+    mean_real_VP = [mean_real_VP mean(real_spike_count)];
+    mean_pred_VP = [mean_pred_VP mean(pred_spike_count(median_ind, :))];
+    
+    % STD of spike counts; must use simulated spike counts here
+    var_real_VP = [var_real_VP var(real_spike_count)];
+    var_pred_VP = [var_pred_VP var(pred_spike_count(median_ind, :))];
+    
+    % save the median simulated spike count
+    trialComparison_pred_VP = [trialComparison_pred_VP {pred_spike_count(median_ind, :)}];
+end
+
+
+nBins = 11;
+latent_bins = prctile(all_latent_VP, linspace(0, 100, nBins));
+spike_bins_real_VP = arrayfun(@(i, j) norm_fr_real_VP(all_latent_VP >= i & all_latent_VP < j), latent_bins(1:end -1), ...
+    latent_bins(2:end), 'UniformOutput', false);
+spike_bins_sim_VP = arrayfun(@(i, j) norm_fr_sim_VP(all_latent_VP >= i & all_latent_VP < j), latent_bins(1:end -1), ...
+    latent_bins(2:end), 'UniformOutput', false);
+
+% figure
+x_latent_bins = latent_bins(1:end - 1) + diff(latent_bins)/2;
+scatterSize = 15;
+
+h_figure = figure; 
+h_lat_VP = subplot(221); hold on
+h_mean = subplot(222); hold on
+h_corr = subplot(223); hold on
+h_var = subplot(224); hold on
+
+subplot(h_lat_VP)
+t_lat_VP(1) = plotFilled(x_latent_bins, spike_bins_real_VP, VP_color, h_lat_VP);
+t_lat_VP(2) = plotFilled(x_latent_bins, spike_bins_sim_VP, myColors.blue_bright, h_lat_VP);
+
+subplot(h_mean)
+scatter(mean_real_VP, mean_pred_VP, scatterSize, 'filled', 'MarkerFaceColor', VP_color)
+maxVal = max([mean_real_VP mean_pred_VP]);
+plot([0 maxVal],[0 maxVal],'k--')
+xlabel('Real spikes (mean)')
+ylabel('Predicted spikes (mean)')
+
+subplot(h_corr)
+corr_bins = linspace(-1,1,40);
+histogram(corr_spike_count_VP, corr_bins, 'Normalization', 'Probability', 'EdgeColor', 'none', 'FaceColor', VP_color)
+xlabel('Correlation')
+ylabel('Probability')
+xlim([-0.05 1.05])
+
+subplot(h_var)
+scatter(var_real_VP, var_pred_VP, scatterSize, 'filled', 'MarkerFaceColor', VP_color)
+maxVal = max([var_real_VP var_pred_VP]);
+plot([0 maxVal],[0 maxVal],'k--')
+xlabel('Real spikes (variance)')
+ylabel('Predicted spikes (variance)')
+
+% clean it up
+legend(t_lat_VP, {'VP','Predicted'}, 'location', 'best')
+for cP = [h_lat_VP h_mean h_corr h_var]
+    subplot(cP)
+    set(cP,'tickdir','out')
+    if cP == h_lat_VP
+        xlabel(latent_var)
+        ylabel([timePeriod ' spikes (z-score)'])
+        if strcmp(latent_var, 'RPEs')
+%             xlim([-1 1])
+        else
+%             xlim([0 1])
+        end
+    elseif cP == h_mean
+        xlim([0 20]); ylim([0 20])
+    elseif cP == h_corr
+    elseif cP == h_var
+        xlim([0 60]); ylim([0 60])
+    end
+end
+
+
+%% plot neurons with particular cross correlations; go with median-correlation simulated spike count
+prtile_cutoff = 80;
+
+VP_neur = find(bm.mask_base);
+[~, VP_neuron_ind] = min(abs(corr_spike_count_VP - prctile(corr_spike_count_VP, prtile_cutoff)));
+VP_neuron_corr = corr_spike_count_VP(VP_neuron_ind);
+os_VP = os(VP_neur);
+
+os_VP_neur = os_VP(VP_neuron_ind);
+
+h_figure = figure;
+h_VP = subplot(1,1,1); hold on
+
+subplot(h_VP)
+plot(os_VP_neur.spikeCount_RD, 'Color', VP_color, 'linewidth', 2)
+plot(trialComparison_pred_VP{VP_neuron_ind}, 'Color', myColors.blue_bright, 'linewidth', 2)
+title(sprintf('VP\nnind: %i, corr = %0.2f', VP_neuron_ind, VP_neuron_corr))
+
+
+for cp = h_VP
+    subplot(cp)
+    xlabel('Trials')
+    ylabel('Spike count')
+    set(cp,'tickdir','out')
+end

+ 0 - 0
MatlabScripts/3_Figures/AdditionalFigures/threeOutcomes/plot_linearRegressions.m


Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików