h_PooledDecodeSucroseAlcohol.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. %decodes trial identity from spiking across bins for each individual neuron
  2. clear all
  3. load('RAWpsvpALL.mat')
  4. load('RAWvpppsALL.mat')
  5. load('RAWvppaALL.mat')
  6. load('RAWvpppaALL.mat')
  7. if ~exist('RPPSall'),load('RPPSall.mat'); end
  8. if ~exist('RPSall'),load('RPSall.mat'); end
  9. if ~exist('RvppaALL'),load('RvppaALL.mat'); end
  10. if ~exist('RvpppaALL'),load('RvpppaALL.mat'); end
  11. for PavInst=1; %DS is 1, Pav is 2
  12. if PavInst==1
  13. RAW=RAWvpppsALL;
  14. selection=RPPSall.Structure==10 & RPPSall.CSPlusRatio>=.5 & (RPPSall.CSPlusRatio./(RPPSall.CSPlusRatio+RPPSall.CSMinusRatio))>=.7;
  15. else if PavInst==2
  16. RAW=RAWpsvpALL;
  17. selection=RPSall.Structure==10 & RPSall.CSPlusRatio>=.5 & (RPSall.CSPlusRatio./(RPSall.CSPlusRatio+RPSall.CSMinusRatio))>=.7;
  18. else if PavInst==3
  19. RAW=RAWvpppaALL;
  20. selection=RvpppaALL.Structure==10 & RvpppaALL.CSPlusRatio>=.5 & (RvpppaALL.CSPlusRatio./(RvpppaALL.CSPlusRatio+RvpppaALL.CSMinusRatio))>=.7;
  21. else if PavInst==4
  22. RAW=RAWvppaALL;
  23. selection=RvppaALL.Structure==10 & RvppaALL.CSPlusRatio>=.5 & (RvppaALL.CSPlusRatio./(RvppaALL.CSPlusRatio+RvppaALL.CSMinusRatio))>=.7;
  24. end
  25. end
  26. end
  27. end
  28. TotalNeurons = 0;
  29. NumNeurons=[1 5 10 25 50 100]; %matrix of how many neurons to use on each iteration
  30. repetitions=20; %how many times to run the analysis
  31. folds = 5; %number of times cross-validated:
  32. Dura=[0 0.3]; %size of bin used for decoding:
  33. bins = 1; %number of bins
  34. binint = 0.3; %spacing of bins
  35. binstart = 0; %start time of first bin relative to event
  36. shuffs = 1; %number of shuffled models created
  37. for i=1:length(NumNeurons)
  38. PoolMdlAcc{i,1}=NaN(folds,bins);
  39. PoolMdlAccShuff{i,1}=NaN(folds,bins);
  40. end
  41. %total number of neurons in all sessions and events per session
  42. %for i=1:length(RAW)
  43. TotalNeurons=sum(selection);
  44. % Ev1perSess(i)=length(RAW(i).Erast{Ev1,1});
  45. % Ev2perSess(i)=length(RAW(i).Erast{Ev2,1});
  46. %end
  47. %figure out how many trials of each event can be used
  48. % Ev1s=min(Ev1perSess);
  49. % Ev2s=min(Ev2perSess);
  50. %Need to keep number of trials used in VP and NAc constant, so have to pick
  51. %minimum number across all sessions in both regions. Right now with
  52. %existing sessions it's 20 Ev1s and 20 Ev2s
  53. Ev1s=27;
  54. Ev2s=27;
  55. for v = 1:length(NumNeurons)
  56. for u=1:repetitions
  57. %pick which neurons to use
  58. SetupSel=cat(1,ones(NumNeurons(v),1),zeros(TotalNeurons-NumNeurons(v),1));
  59. Sel=(SetupSel(randperm(length(SetupSel)))==1);
  60. %setup the event identity matrix for decoding
  61. DecodeRs(1:Ev1s,1)=1;
  62. DecodeRs((Ev1s+1):(Ev1s+Ev2s),1)=2;
  63. for l=1:bins
  64. DecodeSpikes=NaN(1,1);
  65. AllSpikes=NaN(1,1);
  66. LowHz=zeros(1,NumNeurons(v));
  67. NN = 1;
  68. AllNN=1;
  69. for i=1:length(RAW) %loops through sessions
  70. for j= 1:size(RAW(i).Nrast,1) %Number of neurons per session
  71. if selection(AllNN)==1
  72. Ev1Spikes=NaN(1,1);
  73. Ev2Spikes=NaN(1,1);
  74. %events being compared
  75. if (PavInst==1 | PavInst==2)
  76. Ev1=strcmp('CSPlus1', RAW(i).Einfo(:,2));
  77. Ev2=strcmp('CSMinus1', RAW(i).Einfo(:,2));
  78. else
  79. Ev1=strcmp('CSPlus', RAW(i).Einfo(:,2));
  80. Ev2=strcmp('CSMinus', RAW(i).Einfo(:,2));
  81. end
  82. %get number of spikes for this neuron in this bin for all
  83. %Ev1 trials
  84. [PSR1,N1]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Ev1},[(Dura(1)+(binstart - binint)+l*binint) (Dura(2)+(binstart - binint)+l*binint)],{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
  85. for m=1:length(PSR1)
  86. Ev1Spikes(m,1)=sum(PSR1{1,m}>(binstart));
  87. end
  88. %pick which trials get used for decoding
  89. SetupTrials=cat(1,ones(Ev1s,1),zeros(length(PSR1)-Ev1s,1));
  90. Trials=(SetupTrials(randperm(length(SetupTrials)))==1);
  91. %put spikes from those trials in a matrix
  92. AllSpikes(Trials,NN)=Ev1Spikes(Trials,1);
  93. %get all the spikes from reward 1p2 trials
  94. [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Ev2},[(Dura(1)+(binstart - binint)+l*binint) (Dura(2)+(binstart - binint)+l*binint)],{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
  95. for n=1:length(PSR2)
  96. Ev2Spikes(n,1)=sum(PSR2{1,n}>(binstart));
  97. end
  98. %pick which trials get used for decoding
  99. SetupTrials2=cat(1,ones(Ev2s,1),zeros(length(PSR2)-Ev2s,1));
  100. Trials2=(SetupTrials2(randperm(length(SetupTrials2)))==1);
  101. %put spikes from those trials in a matrix
  102. AllSpikes((Ev1s+1):(Ev1s+Ev2s),NN)=Ev2Spikes(Trials2,1);
  103. NN=NN+1; %neuron counter
  104. AllNN=AllNN+1;
  105. else AllNN=AllNN+1;
  106. end
  107. end
  108. end
  109. %Setup spikes matrix for decoding
  110. DecodeSpikes=AllSpikes(:,Sel);
  111. %find neurons with too few spikes
  112. for t=1:NumNeurons(v)
  113. if sum(DecodeSpikes(1:Ev1s,t))<3 || sum(DecodeSpikes((1+Ev1s):(Ev1s+Ev2s),t))<3
  114. LowHz(1,t)=1;
  115. end
  116. end
  117. %remove neurons with too few spikes
  118. % if sum(LowHz)>0
  119. % Sel2=LowHz<1;
  120. % DecodeSpikes=DecodeSpikes(:,Sel2);
  121. % end
  122. %set up validation result matrices
  123. CVacc = NaN(folds,1);
  124. CVaccSh = NaN(folds,1);
  125. %normal model
  126. Partitions = cvpartition(DecodeRs,'KFold',folds);
  127. for r = 1:folds
  128. SVMModel = fitcdiscr(DecodeSpikes(Partitions.training(r),:),DecodeRs(Partitions.training(r)),'discrimType','diaglinear');
  129. [prediction,score] = predict(SVMModel,DecodeSpikes(Partitions.test(r),:));
  130. actual = DecodeRs(Partitions.test(r));
  131. correct = prediction - actual;
  132. CVacc(r) = sum(correct==0) / length(correct);
  133. end
  134. %shuffled model
  135. for q=1:shuffs
  136. DecodeRsSh=DecodeRs(randperm(length(DecodeRs)));
  137. PartitionsSh = cvpartition(DecodeRsSh,'KFold',folds);
  138. for s = 1:folds
  139. SVMModelSh = fitcdiscr(DecodeSpikes(PartitionsSh.training(s),:),DecodeRsSh(PartitionsSh.training(s)),'discrimType','diaglinear');
  140. [predictionSh, score] = predict(SVMModelSh,DecodeSpikes(PartitionsSh.test(s),:));
  141. actualSh = DecodeRsSh(PartitionsSh.test(s));
  142. correctSh = predictionSh - actualSh;
  143. CVaccSh(s) = sum(correctSh==0) / length(correctSh);
  144. end
  145. AccShuff(q,1) = nanmean(CVaccSh);
  146. end
  147. PoolMdlAcc{v,1}(u,l) = nanmean(CVacc);
  148. PoolMdlAccShuff{v,1}(u,l) = nanmean(AccShuff);
  149. fprintf(['Condition #' num2str(v) ', Rep #' num2str(u) ', Bin #' num2str(l) '\n']);
  150. end %bins
  151. end
  152. end
  153. if PavInst==1
  154. PoolMdlAccDSSucrose=PoolMdlAcc;
  155. PoolMdlAccDSSucroseShuff=PoolMdlAccShuff;
  156. save('PoolMdlAccDSSucrose.mat','PoolMdlAccDSSucrose')
  157. save('PoolMdlAccDSSucroseShuff.mat','PoolMdlAccDSSucroseShuff')
  158. else if PavInst==2
  159. PoolMdlAccPavSucrose=PoolMdlAcc;
  160. PoolMdlAccPavSucroseShuff=PoolMdlAccShuff;
  161. save('PoolMdlAccPavSucrose.mat','PoolMdlAccPavSucrose')
  162. save('PoolMdlAccPavSucroseShuff.mat','PoolMdlAccPavSucroseShuff')
  163. else if PavInst==3
  164. PoolMdlAccDSAlcohol=PoolMdlAcc;
  165. PoolMdlAccDSAlcoholShuff=PoolMdlAccShuff;
  166. save('PoolMdlAccDSAlcohol.mat','PoolMdlAccDSAlcohol')
  167. save('PoolMdlAccDSAlcoholShuff.mat','PoolMdlAccDSAlcoholShuff')
  168. else if PavInst==4
  169. PoolMdlAccPavAlcohol=PoolMdlAcc;
  170. PoolMdlAccPavAlcoholShuff=PoolMdlAccShuff;
  171. save('PoolMdlAccPavAlcohol.mat','PoolMdlAccPavAlcohol')
  172. save('PoolMdlAccPavAlcoholShuff.mat','PoolMdlAccPavAlcoholShuff')
  173. end
  174. end
  175. end
  176. end
  177. end