H_generateFigure6AB_Decoding1.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. %% generate figure 6A and B. Decoding brain region ID without PCA
  2. % code from David Ottenheimer 04/19/2018
  3. %decode region
  4. %first I'll try just classifying individual neurons as DMS or DLS
  5. %if ~exist('PSTHzDecode') %if you haven't already collected all the psth data for the decoding
  6. %get the data
  7. clear all
  8. close all
  9. load('Rearlytraining_light.mat')
  10. Xaxis1=[-0.25 0.25];
  11. Ishow=find(Tm>=Xaxis1(1) & Tm<=Xaxis1(2));
  12. for i=4:13 %loop thru ACQ session
  13. DLSselection=Ses(i).Coord(:,4)==10 & Ses(i).Celltype(:,1)==1;
  14. DMSselection=Ses(i).Coord(:,4)==20 & Ses(i).Celltype(:,1)==1;
  15. DLSconcat_ACQ=[];DMSconcat_ACQ=[];
  16. DLSconcat_ACQ = cat(2,DLSconcat_ACQ,Ses(i).Ev(7).Meanz(DLSselection,1));
  17. DMSconcat_ACQ = cat(2,DMSconcat_ACQ,Ses(i).Ev(7).Meanz(DMSselection,1));
  18. for j=1:5
  19. DLSconcat_ACQ = cat(2,DLSconcat_ACQ,Ses(i).Ev(j).MeanzPRE(DLSselection,1));
  20. DLSconcat_ACQ = cat(2,DLSconcat_ACQ,Ses(i).Ev(j).Meanz(DLSselection,1));
  21. DMSconcat_ACQ = cat(2,DMSconcat_ACQ,Ses(i).Ev(j).MeanzPRE(DMSselection,1));
  22. DMSconcat_ACQ = cat(2,DMSconcat_ACQ,Ses(i).Ev(j).Meanz(DMSselection,1));
  23. end
  24. DLSconcat_ACQ = cat(2,DLSconcat_ACQ,Ses(i).Ev(6).MeanzPRE(DLSselection,1));
  25. DMSconcat_ACQ = cat(2,DMSconcat_ACQ,Ses(i).Ev(6).MeanzPRE(DMSselection,1));
  26. PSTHzDecodeACQ(i-3).DLS=DLSconcat_ACQ(~isnan(mean(DLSconcat_ACQ,2)),:);
  27. PSTHzDecodeACQ(i-3).DMS=DMSconcat_ACQ(~isnan(mean(DMSconcat_ACQ,2)),:);
  28. end
  29. clear Ses
  30. load('Rextendedtraining_light.mat');
  31. load('Celltype_extendedTraining.mat');
  32. DLSselection=Coord(:,4)==10 & Celltype(:,1)==1;
  33. DMSselection=Coord(:,4)==20 & Celltype(:,1)==1;
  34. DLSconcat_OT = Ev(7).Meanz(DLSselection,1);
  35. DMSconcat_OT = Ev(7).Meanz(DMSselection,1);
  36. for i=1:5
  37. DLSconcat_OT = cat(2,DLSconcat_OT,Ev(i).MeanzPRE(DLSselection,1));
  38. DLSconcat_OT = cat(2,DLSconcat_OT,Ev(i).Meanz(DLSselection,1));
  39. DMSconcat_OT = cat(2,DMSconcat_OT,Ev(i).MeanzPRE(DMSselection,1));
  40. DMSconcat_OT = cat(2,DMSconcat_OT,Ev(i).Meanz(DMSselection,1));
  41. end
  42. DLSconcat_OT = cat(2,DLSconcat_OT,Ev(6).MeanzPRE(DLSselection,1));
  43. DMSconcat_OT = cat(2,DMSconcat_OT,Ev(6).MeanzPRE(DMSselection,1));
  44. MeanzDecodeOT.DLS=DLSconcat_OT;
  45. MeanzDecodeOT.DMS=DMSconcat_OT;
  46. save('MeanzDecodeACQ','MeanzDecodeOT')
  47. %% ACQ PCcomponents Comparison sessions.
  48. %PCA first
  49. %first do PCA on an equal number of neurons in each region
  50. nbsession=0;
  51. for k=1:10
  52. clear rep
  53. countRep=0;
  54. PSTHzDecode.DLS=PSTHzDecodeACQ(k).DLS;
  55. PSTHzDecode.DMS=PSTHzDecodeACQ(k).DMS;
  56. if length(PSTHzDecode.DLS(:,1))<length(PSTHzDecode.DMS(:,1))
  57. PCAneurons=length(PSTHzDecode.DLS(:,1));
  58. else
  59. PCAneurons=length(PSTHzDecode.DMS(:,1));
  60. end
  61. for l=1:50 % loop added to account for selection bias in VSel and NSel
  62. %pick which neurons to use
  63. SetupDMSel=cat(1,ones(PCAneurons,1),zeros(length(PSTHzDecode.DMS(:,1))-PCAneurons,1));
  64. VSel=(SetupDMSel(randperm(length(SetupDMSel)))==1);
  65. %pick which neurons to use
  66. SetupDLSel=cat(1,ones(PCAneurons,1),zeros(length(PSTHzDecode.DLS(:,1))-PCAneurons,1));
  67. NSel=(SetupDLSel(randperm(length(SetupDLSel)))==1);
  68. rep(l).concat=cat(1,PSTHzDecode.DLS(NSel,:),PSTHzDecode.DMS(VSel,:));
  69. end
  70. %% Decoding
  71. clear Partitions SVMModel prediction actual correct DecodeRegion
  72. folds = 10;%PCAneurons; %number of times cross-validated
  73. shuffs = 1; %number of times shuffled
  74. DiscrimType= 'linear';
  75. %setup the event identity matrix for decoding
  76. DecodeRegion(1:PCAneurons,1)=1;
  77. DecodeRegion((PCAneurons+1):(PCAneurons*2),1)=2;
  78. for l=1:50
  79. countRep=countRep+1;
  80. clear CVacc CVaccSh
  81. %Setup spikes matrix for decoding
  82. DecodeMeanZ=rep(l).concat;
  83. %normal model
  84. for r = 1:folds
  85. Partitions = cvpartition(DecodeRegion,'KFold',folds);
  86. SVMModel = fitcdiscr(DecodeMeanZ(Partitions.training(r),:),DecodeRegion(Partitions.training(r)),'DiscrimType',DiscrimType);
  87. prediction = predict(SVMModel,DecodeMeanZ(Partitions.test(r),:));
  88. actual = DecodeRegion(Partitions.test(r));
  89. correct = prediction - actual;
  90. CVacc(r,1) = sum(correct==0) / length(correct);
  91. end
  92. %shuffled model
  93. for q=1:shuffs
  94. DecodeRsSh=DecodeRegion(randperm(length(DecodeRegion)));
  95. PartitionsSh = cvpartition(DecodeRsSh,'KFold',folds);
  96. for s = 1:folds
  97. SVMModelSh = fitcdiscr(DecodeMeanZ(PartitionsSh.training(s),:),DecodeRsSh(PartitionsSh.training(s)),'DiscrimType',DiscrimType);
  98. predictionSh = predict(SVMModelSh,DecodeMeanZ(PartitionsSh.test(s),:));
  99. actualSh = DecodeRsSh(PartitionsSh.test(s));
  100. correctSh = predictionSh - actualSh;
  101. CVaccSh(s,1) = sum(correctSh==0) / length(correctSh);
  102. end
  103. AccShuff(q,1) = nanmean(CVaccSh);
  104. end
  105. PCADecodeAccAA{k,1}(countRep,1)=nanmean(CVacc);
  106. PCADecodeAccShAA{k,1}(countRep,1)=nanmean(AccShuff);
  107. fprintf(['RepSelection #' num2str(countRep) '\n']);
  108. end
  109. end
  110. %% plotting
  111. %colors
  112. inh=[0 0 0.6];
  113. exc=[0.8 0 0];
  114. NumSession=[1 2 3 4 5 6 7 8 9 10];
  115. for i=1:10
  116. subplot(2,8,[1 2 3 4]);
  117. hold on;
  118. errorbar(NumSession(i),nanmean(PCADecodeAccAA{i,1}),nanstd(PCADecodeAccAA{i,1}),'o','color',exc);
  119. errorbar(NumSession(i),nanmean(PCADecodeAccShAA{i,1}),nanstd(PCADecodeAccShAA{i,1}),'o','color',inh);
  120. end
  121. axis([0 10+1 0.35 0.85]);
  122. title('Decoding of region across session');
  123. xlabel('Sessions');
  124. ylabel('Accuracy');
  125. legend({'True','Shuffled'},'location','northwest');
  126. %% Overtraining as a function of PC and number of neurons included
  127. for k=1:5
  128. countRep=0;
  129. clear rep
  130. PSTHzDecode.DLS=MeanzDecodeOT.DLS;
  131. PSTHzDecode.DMS=MeanzDecodeOT.DMS;
  132. nbneurons=[30 60 100 200 300];
  133. PCAneurons=nbneurons(k);
  134. for l=1:50 % loop added to account for selection bias in VSel and NSel
  135. SetupDMSel=cat(1,ones(PCAneurons,1),zeros(length(PSTHzDecode.DMS(:,1))-PCAneurons,1));
  136. VSel=(SetupDMSel(randperm(length(SetupDMSel)))==1);
  137. %pick which neurons to use
  138. SetupDLSel=cat(1,ones(PCAneurons,1),zeros(length(PSTHzDecode.DLS(:,1))-PCAneurons,1));
  139. NSel=(SetupDLSel(randperm(length(SetupDLSel)))==1);
  140. rep(l).concatOT=cat(1,PSTHzDecode.DLS(NSel,:),PSTHzDecode.DMS(VSel,:));
  141. end
  142. %% Decoding
  143. clear Partitions SVMModel prediction actual correct DecodeRegion
  144. folds = 10;%PCAneurons; %number of times cross-validated
  145. shuffs = 1; %number of times shuffled
  146. repetitions=20; %how many times to run the analysis
  147. DiscrimType= 'linear';
  148. %setup the event identity matrix for decoding
  149. DecodeRegion(1:PCAneurons,1)=1;
  150. DecodeRegion((PCAneurons+1):(PCAneurons*2),1)=2;
  151. for l=1:50
  152. countRep=countRep+1;
  153. clear CVacc CVaccSh
  154. %Setup spikes matrix for decoding
  155. DecodeMeanZ=rep(l).concatOT;
  156. %normal model
  157. for r = 1:folds
  158. Partitions = cvpartition(DecodeRegion,'KFold',folds);
  159. SVMModel = fitcdiscr(DecodeMeanZ(Partitions.training(r),:),DecodeRegion(Partitions.training(r)),'DiscrimType',DiscrimType);
  160. prediction = predict(SVMModel,DecodeMeanZ(Partitions.test(r),:));
  161. actual = DecodeRegion(Partitions.test(r));
  162. correct = prediction - actual;
  163. CVacc(r,1) = sum(correct==0) / length(correct);
  164. end
  165. %shuffled model
  166. for q=1:shuffs
  167. DecodeRsSh=DecodeRegion(randperm(length(DecodeRegion)));
  168. PartitionsSh = cvpartition(DecodeRsSh,'KFold',folds);
  169. for s = 1:folds
  170. SVMModelSh = fitcdiscr(DecodeMeanZ(PartitionsSh.training(s),:),DecodeRsSh(PartitionsSh.training(s)),'DiscrimType',DiscrimType);
  171. predictionSh = predict(SVMModelSh,DecodeMeanZ(PartitionsSh.test(s),:));
  172. actualSh = DecodeRsSh(PartitionsSh.test(s));
  173. correctSh = predictionSh - actualSh;
  174. CVaccSh(s,1) = sum(correctSh==0) / length(correctSh);
  175. end
  176. AccShuff(q,1) = nanmean(CVaccSh);
  177. end
  178. PCADecodeAccOT{1,k}(countRep,1)=nanmean(CVacc);
  179. PCADecodeAccShOT{1,k}(countRep,1)=nanmean(AccShuff);
  180. fprintf(['Rep #' num2str(countRep) '\n']);
  181. end
  182. end
  183. %% plotting
  184. %colors
  185. inh=[0 0 0.6];
  186. exc=[0.8 0 0];
  187. Shuffle=[];
  188. for k=1:5
  189. subplot(2,8,[5 6 7]);
  190. hold on;
  191. errorbar(nbneurons(k),nanmean(PCADecodeAccOT{1,k}),nanstd(PCADecodeAccOT{1,k}),'o','color',exc(1,:));
  192. errorbar(nbneurons(k),nanmean(PCADecodeAccShOT{1,k}),nanstd(PCADecodeAccShOT{1,k}),'o','color',inh(1,:));
  193. end
  194. axis([0.5 300 0.35 0.85]);
  195. title('Ext training: Decoding DMS vs DLS');
  196. xlabel('Ext Train');
  197. ylabel('Accuracy');
  198. %save('Decode_withoutPCA.mat','PCADecodeAccAA','PCADecodeAccOT','PCADecodeAccShAA','PCADecodeAccShOT');
  199. %% stat
  200. for i=1:10
  201. tableAcc(:,i)=cat(1,PCADecodeAccAA{i,1},PCADecodeAccShAA{i,1});
  202. Between_factor=cat(1,zeros(50,1),ones(50,1));
  203. [p,tbl]=anova1(tableAcc(:,i),Between_factor,'off');
  204. p_value(i)=p;
  205. stat(i).t=tbl;
  206. end
  207. for i=1:5
  208. tableAccOT(:,i)=cat(1,PCADecodeAccOT{1,i},PCADecodeAccShOT{1,i});
  209. [p,tbl]=anova1(tableAccOT(:,i),Between_factor,'off');
  210. pOT_value(i)=p;
  211. statOT(i).t=tbl;
  212. end
  213. tableAccACQ_OT=cat(1,PCADecodeAccAA{10,1},PCADecodeAccOT{1,1});
  214. [p_ACQ_OT,tbl_ACQ_OT]=anova1(tableAccACQ_OT,Between_factor,'off');
  215. %%
  216. for i=1:length(PCADecodeAccAA)
  217. allsamples=cat(1,PCADecodeAccAA{i,1},PCADecodeAccShAA{i,1});
  218. for p=1:10000
  219. shuffsamples=allsamples(randperm(length(allsamples)));
  220. shuffdiff(p,1)=abs(mean(shuffsamples(1:length(allsamples)/2))-mean(shuffsamples(length(allsamples)/2+1:end)));
  221. end
  222. diff=abs(nanmean(PCADecodeAccAA{i,1})-nanmean(PCADecodeAccShAA{i,1}));
  223. trainingpval(i,1)=(sum(shuffdiff>diff)+1)/(length(shuffdiff)+1);
  224. end
  225. %% overtraining
  226. for i=1:length(PCADecodeAccOT)
  227. allsamples=cat(1,PCADecodeAccOT{1,i},PCADecodeAccShOT{1,i});
  228. for p=1:10000
  229. shuffsamples=allsamples(randperm(length(allsamples)));
  230. shuffdiff(p,1)=abs(mean(shuffsamples(1:length(allsamples)/2))-mean(shuffsamples(length(allsamples)/2+1:end)));
  231. end
  232. diff=abs(nanmean(PCADecodeAccOT{1,i})-nanmean(PCADecodeAccShOT{1,i}));
  233. OTpval(1,i)=(sum(shuffdiff>diff)+1)/(length(shuffdiff)+1);
  234. end