H_generateFigure7B_Decoding2.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. %% Generate figure 7B and figure 7- supplement figure 1
  2. % decoding using PCA; Up left, compare 3 early training sessions across PCs
  3. % Up right, compare different ensemble size of extended training across PCs
  4. % code from David Ottenheimer 04/19/2018
  5. clear all
  6. close all
  7. load('Rearlytraining_light.mat')
  8. Xaxis1=[-0.25 0.25];
  9. Ishow=find(Tm>=Xaxis1(1) & Tm<=Xaxis1(2));
  10. for i=4:13 %loop thru ACQ session
  11. DLSselection=Ses(i).Coord(:,4)==10 & Ses(i).Celltype(:,1)==1;
  12. DMSselection=Ses(i).Coord(:,4)==20 & Ses(i).Celltype(:,1)==1;
  13. DLSconcat_ACQ=[];DMSconcat_ACQ=[];
  14. DLSconcat_ACQ = cat(2,DLSconcat_ACQ,Ses(i).Ev(7).PSTHz(DLSselection,Ishow));
  15. DMSconcat_ACQ = cat(2,DMSconcat_ACQ,Ses(i).Ev(7).PSTHz(DMSselection,Ishow));
  16. for j=1:6
  17. DLSconcat_ACQ = cat(2,DLSconcat_ACQ,Ses(i).Ev(j).PSTHz(DLSselection,Ishow));
  18. DMSconcat_ACQ = cat(2,DMSconcat_ACQ,Ses(i).Ev(j).PSTHz(DMSselection,Ishow));
  19. end
  20. PSTHzDecodeACQ(i-3).DLS=DLSconcat_ACQ(~isnan(mean(DLSconcat_ACQ,2)),:);
  21. PSTHzDecodeACQ(i-3).DMS=DMSconcat_ACQ(~isnan(mean(DMSconcat_ACQ,2)),:);
  22. end
  23. clear Ses
  24. load('Rextendedtraining_light.mat');
  25. load('Celltype_extendedtraining.mat');
  26. DLSselection=Coord(:,4)==10 & Celltype(:,1)==1;
  27. DMSselection=Coord(:,4)==20 & Celltype(:,1)==1;
  28. DLSconcat_OT = Ev(7).PSTHz(DLSselection,Ishow);
  29. DMSconcat_OT = Ev(7).PSTHz(DMSselection,Ishow);
  30. for i=1:6
  31. DLSconcat_OT = cat(2,DLSconcat_OT,Ev(i).PSTHz(DLSselection,Ishow));
  32. DMSconcat_OT = cat(2,DMSconcat_OT,Ev(i).PSTHz(DMSselection,Ishow));
  33. end
  34. PSTHzDecodeOT.DLS=DLSconcat_OT;
  35. PSTHzDecodeOT.DMS=DMSconcat_OT;
  36. save('PSTHzDecodeACQ','PSTHzDecodeOT')
  37. %% ACQ PCcomponents Comparison sessions.
  38. %PCA first
  39. %first do PCA on an equal number of neurons in each region
  40. nbsession=0;
  41. for k=1:3:10
  42. countRep=0;
  43. nbsession=nbsession+1;
  44. PSTHzDecode.DLS=PSTHzDecodeACQ(k).DLS;
  45. PSTHzDecode.DMS=PSTHzDecodeACQ(k).DMS;
  46. if length(PSTHzDecode.DLS(:,1))<length(PSTHzDecode.DMS(:,1))
  47. PCAneurons=length(PSTHzDecode.DLS(:,1));
  48. else
  49. PCAneurons=length(PSTHzDecode.DMS(:,1));
  50. end
  51. for l=1:50 % loop added to account for selection bias in VSel and NSel
  52. %pick which neurons to use
  53. SetupDMSel=cat(1,ones(PCAneurons,1),zeros(length(PSTHzDecode.DMS(:,1))-PCAneurons,1));
  54. VSel=(SetupDMSel(randperm(length(SetupDMSel)))==1);
  55. %pick which neurons to use
  56. SetupDLSel=cat(1,ones(PCAneurons,1),zeros(length(PSTHzDecode.DLS(:,1))-PCAneurons,1));
  57. NSel=(SetupDLSel(randperm(length(SetupDLSel)))==1);
  58. PCAneuronsACQ(nbsession,1)=PCAneurons;
  59. concat=cat(1,PSTHzDecode.DLS(NSel,:),PSTHzDecode.DMS(VSel,:))';
  60. [coeff,score,~,~,explained] = pca(concat);
  61. % Decoding
  62. clear Partitions SVMModel prediction actual correct
  63. folds = 10;%PCAneurons; %number of times cross-validated
  64. shuffs = 1; %number of times shuffled
  65. NumCoeffs = [1 2 3 4 5 6 7 8 9 10]; %number of PCs used in decoding
  66. repetitions=20; %how many times to run the analysis
  67. DiscrimType= 'linear';
  68. %setup the event identity matrix for decoding
  69. DecodeRegion(1:PCAneurons,1)=1;
  70. DecodeRegion((PCAneurons+1):(PCAneurons*2),1)=2;
  71. countRep=countRep+1;
  72. for i=1:length(NumCoeffs)
  73. clear CVacc CVaccSh
  74. %Setup spikes matrix for decoding
  75. DecodePCs=coeff(:,1:NumCoeffs(i));
  76. %ADD IN PCA HERE???
  77. %normal model
  78. for r = 1:folds
  79. Partitions = cvpartition(DecodeRegion,'KFold',folds);
  80. SVMModel = fitcdiscr(DecodePCs(Partitions.training(r),:),DecodeRegion(Partitions.training(r)),'DiscrimType',DiscrimType);
  81. prediction = predict(SVMModel,DecodePCs(Partitions.test(r),:));
  82. actual = DecodeRegion(Partitions.test(r));
  83. correct = prediction - actual;
  84. CVacc(r,1) = sum(correct==0) / length(correct);
  85. end
  86. %shuffled model
  87. for q=1:shuffs
  88. DecodeRsSh=DecodeRegion(randperm(length(DecodeRegion)));
  89. PartitionsSh = cvpartition(DecodeRsSh,'KFold',folds);
  90. for s = 1:folds
  91. SVMModelSh = fitcdiscr(DecodePCs(PartitionsSh.training(s),:),DecodeRsSh(PartitionsSh.training(s)),'DiscrimType',DiscrimType);
  92. predictionSh = predict(SVMModelSh,DecodePCs(PartitionsSh.test(s),:));
  93. actualSh = DecodeRsSh(PartitionsSh.test(s));
  94. correctSh = predictionSh - actualSh;
  95. CVaccSh(s,1) = sum(correctSh==0) / length(correctSh);
  96. end
  97. AccShuff(q,1) = nanmean(CVaccSh);
  98. end
  99. PCADecodeAccACQ{i,nbsession}(countRep,1)=nanmean(CVacc);
  100. PCADecodeAccShACQ{i,nbsession}(countRep,1)=nanmean(AccShuff);
  101. end
  102. fprintf(['RepACQ #' num2str(countRep) '\n']);
  103. end
  104. end
  105. %plotting
  106. %colors
  107. inh=[0 0.3333 1; 0 0.5 1; 0 0.67 1; 0 0.8353 1];
  108. exc=[1 0.1647 0; 0.9020 0.4510 0; 0.9020 0.6 0; 1 0.8353 0];
  109. %Y = prctile(X,p)
  110. for i=1:length(NumCoeffs)
  111. for k=1:4
  112. subplot(2,2,1);
  113. hold on;
  114. errorbar(NumCoeffs(i),nanmean(PCADecodeAccACQ{i,k}),nanstd(PCADecodeAccACQ{i,k}),'o','color',exc(k,:));
  115. errorbar(NumCoeffs(i),nanmean(PCADecodeAccShACQ{i,4}),nanstd(PCADecodeAccShACQ{i,4}),'o','color',inh(1,:));
  116. end
  117. end
  118. axis([0 NumCoeffs(end)+1 0.30 0.85]);
  119. title('Early training: Decoding DMS vs DLS');
  120. xlabel('Number of PCs');
  121. ylabel('Accuracy');
  122. %% Overtraining as a function of PC and number of neurons included
  123. for k=1:5
  124. countRep=0;
  125. PSTHzDecode.DLS=PSTHzDecodeOT.DLS;
  126. PSTHzDecode.DMS=PSTHzDecodeOT.DMS;
  127. nbneurons=[30 60 100 200 300];
  128. PCAneurons=nbneurons(k);
  129. for l=1:50 % loop added to account for selection bias in VSel and NSel
  130. %pick which neurons to use
  131. SetupDMSel=cat(1,ones(PCAneurons,1),zeros(length(PSTHzDecode.DMS(:,1))-PCAneurons,1));
  132. VSel=(SetupDMSel(randperm(length(SetupDMSel)))==1);
  133. %pick which neurons to use
  134. SetupDLSel=cat(1,ones(PCAneurons,1),zeros(length(PSTHzDecode.DLS(:,1))-PCAneurons,1));
  135. NSel=(SetupDLSel(randperm(length(SetupDLSel)))==1);
  136. concat=cat(1,PSTHzDecode.DLS(NSel,:),PSTHzDecode.DMS(VSel,:))';
  137. [coeff,score,~,~,explained] = pca(concat);
  138. %% Decoding
  139. clear Partitions SVMModel prediction actual correct DecodeRegion
  140. folds = 10;%PCAneurons; %number of times cross-validated
  141. shuffs = 1; %number of times shuffled
  142. NumCoeffs = [1 2 3 4 5 6 7 8 9 10]; %number of PCs used in decoding
  143. DiscrimType= 'linear';
  144. %setup the event identity matrix for decoding
  145. DecodeRegion(1:PCAneurons,1)=1;
  146. DecodeRegion((PCAneurons+1):(PCAneurons*2),1)=2;
  147. countRep=countRep+1;
  148. for i=1:length(NumCoeffs)
  149. clear CVacc CVaccSh
  150. %Setup spikes matrix for decoding
  151. DecodePCs=coeff(:,1:NumCoeffs(i));
  152. %ADD IN PCA HERE???
  153. %normal model
  154. for r = 1:folds
  155. Partitions = cvpartition(DecodeRegion,'KFold',folds);
  156. SVMModel = fitcdiscr(DecodePCs(Partitions.training(r),:),DecodeRegion(Partitions.training(r)),'DiscrimType',DiscrimType);
  157. prediction = predict(SVMModel,DecodePCs(Partitions.test(r),:));
  158. actual = DecodeRegion(Partitions.test(r));
  159. correct = prediction - actual;
  160. CVacc(r,1) = sum(correct==0) / length(correct);
  161. end
  162. %shuffled model
  163. for q=1:shuffs
  164. DecodeRsSh=DecodeRegion(randperm(length(DecodeRegion)));
  165. PartitionsSh = cvpartition(DecodeRsSh,'KFold',folds);
  166. for s = 1:folds
  167. SVMModelSh = fitcdiscr(DecodePCs(PartitionsSh.training(s),:),DecodeRsSh(PartitionsSh.training(s)),'DiscrimType',DiscrimType);
  168. predictionSh = predict(SVMModelSh,DecodePCs(PartitionsSh.test(s),:));
  169. actualSh = DecodeRsSh(PartitionsSh.test(s));
  170. correctSh = predictionSh - actualSh;
  171. CVaccSh(s,1) = sum(correctSh==0) / length(correctSh);
  172. end
  173. AccShuff(q,1) = nanmean(CVaccSh);
  174. end
  175. PCADecodeAccOT{i,k}(countRep,1)=nanmean(CVacc);
  176. PCADecodeAccShOT{i,k}(countRep,1)=nanmean(AccShuff);
  177. end
  178. fprintf(['RepOT #' num2str(countRep) '\n']);
  179. end
  180. end
  181. %% plotting
  182. %colors
  183. inh=[0 0 0.6; 0 0.3333 1; 0 0.5 1; 0 0.67 1; 0 0.8353 1];
  184. exc=[0.8 0 0; 1 0.1647 0; 0.9020 0.4510 0; 0.9020 0.6 0; 1 0.8353 0];
  185. for i=1:length(NumCoeffs)
  186. for k=1:5
  187. subplot(2,2,2);
  188. hold on;
  189. errorbar(NumCoeffs(i),nanmean(PCADecodeAccOT{i,k}),nanstd(PCADecodeAccOT{i,k}),'o','color',exc(k,:));
  190. errorbar(NumCoeffs(i),nanmean(PCADecodeAccShOT{i,5}),nanstd(PCADecodeAccShOT{i,5}),'o','color',inh(1,:));
  191. end
  192. end
  193. axis([0 NumCoeffs(end)+1 0.30 0.85]);
  194. title('Ext Training: Decoding DMS vs DLS');
  195. xlabel('Number of PCs');
  196. ylabel('Accuracy');
  197. %% Decoding DMS vs DLS across session, based on weight of 3 first PCs
  198. %PCA first
  199. for i=1:10
  200. countRep=0;
  201. PSTHzDecode.DLS=PSTHzDecodeACQ(i).DLS;
  202. PSTHzDecode.DMS=PSTHzDecodeACQ(i).DMS;
  203. if length(PSTHzDecode.DLS(:,1))<length(PSTHzDecode.DMS(:,1))
  204. PCAneurons=length(PSTHzDecode.DLS(:,1));
  205. else
  206. PCAneurons=length(PSTHzDecode.DMS(:,1));
  207. end
  208. for l=1:50
  209. SetupDMSel=cat(1,ones(PCAneurons,1),zeros(length(PSTHzDecode.DMS(:,1))-PCAneurons,1));
  210. VSel=(SetupDMSel(randperm(length(SetupDMSel)))==1);
  211. %pick which neurons to use
  212. SetupDLSel=cat(1,ones(PCAneurons,1),zeros(length(PSTHzDecode.DLS(:,1))-PCAneurons,1));
  213. NSel=(SetupDLSel(randperm(length(SetupDLSel)))==1);
  214. concat=cat(1,PSTHzDecode.DLS(NSel,:),PSTHzDecode.DMS(VSel,:))';
  215. [coeff,score,~,~,explained] = pca(concat);
  216. %% Decoding
  217. clear Partitions SVMModel prediction actual correct DecodeRegion
  218. folds = 10;%PCAneurons; %number of times cross-validated
  219. shuffs = 1; %number of times shuffled
  220. NumCoeffs = 3; %number of PCs used in decoding
  221. DiscrimType= 'linear';
  222. %setup the event identity matrix for decoding
  223. DecodeRegion(1:PCAneurons,1)=1;
  224. DecodeRegion((PCAneurons+1):(PCAneurons*2),1)=2;
  225. countRep=countRep+1;
  226. clear CVacc CVaccSh
  227. %Setup spikes matrix for decoding
  228. DecodePCs=coeff(:,1:NumCoeffs);
  229. %ADD IN PCA HERE???
  230. %normal model
  231. for r = 1:folds
  232. Partitions = cvpartition(DecodeRegion,'KFold',folds);
  233. SVMModel = fitcdiscr(DecodePCs(Partitions.training(r),:),DecodeRegion(Partitions.training(r)),'DiscrimType',DiscrimType);
  234. prediction = predict(SVMModel,DecodePCs(Partitions.test(r),:));
  235. actual = DecodeRegion(Partitions.test(r));
  236. correct = prediction - actual;
  237. CVacc(r,1) = sum(correct==0) / length(correct);
  238. end
  239. %shuffled model
  240. for q=1:shuffs
  241. DecodeRsSh=DecodeRegion(randperm(length(DecodeRegion)));
  242. PartitionsSh = cvpartition(DecodeRsSh,'KFold',folds);
  243. for s = 1:folds
  244. SVMModelSh = fitcdiscr(DecodePCs(PartitionsSh.training(s),:),DecodeRsSh(PartitionsSh.training(s)),'DiscrimType',DiscrimType);
  245. predictionSh = predict(SVMModelSh,DecodePCs(PartitionsSh.test(s),:));
  246. actualSh = DecodeRsSh(PartitionsSh.test(s));
  247. correctSh = predictionSh - actualSh;
  248. CVaccSh(s,1) = sum(correctSh==0) / length(correctSh);
  249. end
  250. AccShuff(q,1) = nanmean(CVaccSh);
  251. end
  252. PCADecodeAccAA{i,1}(countRep,1)=nanmean(CVacc);
  253. PCADecodeAccShAA{i,1}(countRep,1)=nanmean(AccShuff);
  254. fprintf(['RepACQses #' num2str(countRep) '\n']);
  255. end
  256. end
  257. %% plotting
  258. %colors
  259. inh=[0 0 0.6; 0 0.3333 1; 0 0.5 1; 0 0.67 1; 0 0.8353 1];
  260. exc=[0.8 0 0; 1 0.1647 0; 0.9020 0.4510 0; 0.9020 0.6 0; 1 0.8353 0];
  261. NumSession=[1 2 3 4 5 6 7 8 9 10];
  262. for i=1:10
  263. subplot(2,2,3);
  264. hold on;
  265. errorbar(NumSession(i),nanmean(PCADecodeAccAA{i,1}),nanstd(PCADecodeAccAA{i,1}),'o','color',exc(1,:));
  266. errorbar(NumSession(i),nanmean(PCADecodeAccShAA{i,1}),nanstd(PCADecodeAccShAA{i,1}),'o','color',inh(1,:));
  267. end
  268. axis([0 10+1 0.35 0.85]);
  269. title('Decoding of region across session');
  270. xlabel('Sessions');
  271. ylabel('Accuracy');
  272. for k=1:5
  273. subplot(2,2,4);
  274. hold on;
  275. errorbar(nbneurons(k),nanmean(PCADecodeAccOT{3,k}),nanstd(PCADecodeAccOT{3,k}),'o','color',exc(1,:));
  276. errorbar(nbneurons(k),nanmean(PCADecodeAccShOT{3,k}),nanstd(PCADecodeAccShOT{3,k}),'o','color',inh(1,:));
  277. end
  278. axis([0 300 0.35 0.85]);
  279. xlabel('OT');
  280. ylabel('Accuracy');
  281. save('PCADecode.mat','PCADecodeAccAA','PCADecodeAccACQ','PCADecodeAccOT','PCADecodeAccShAA','PCADecodeAccShACQ','PCADecodeAccShOT');
  282. %% stat
  283. for i=1:10
  284. tableAcc(:,i)=cat(1,PCADecodeAccAA{i,1},PCADecodeAccShAA{i,1});
  285. Between_factor=cat(1,zeros(50,1),ones(50,1));
  286. [p,tbl]=anova1(tableAcc(:,i),Between_factor,'off');
  287. p_value(i)=p;
  288. stat(i).t=tbl;
  289. end
  290. for i=1:5
  291. tableAccOT(:,i)=cat(1,PCADecodeAccOT{3,i},PCADecodeAccShOT{3,i});
  292. [p,tbl]=anova1(tableAccOT(:,i),Between_factor,'off');
  293. pOT_value(i)=p;
  294. statOT(i).t=tbl;
  295. end
  296. tableAccACQ_OT=cat(1,PCADecodeAccAA{10,1},PCADecodeAccOT{3,1});
  297. [p_ACQ_OT,tbl_ACQ_OT]=anova1(tableAccACQ_OT,Between_factor,'off');
  298. %% Permutation test
  299. for i=1:length(PCADecodeAccAA)
  300. allsamples=cat(1,PCADecodeAccAA{i,1},PCADecodeAccShAA{i,1});
  301. for p=1:10000
  302. shuffsamples=allsamples(randperm(length(allsamples)));
  303. shuffdiff(p,1)=abs(mean(shuffsamples(1:length(allsamples)/2))-mean(shuffsamples(length(allsamples)/2+1:end)));
  304. end
  305. diff=abs(nanmean(PCADecodeAccAA{i,1})-nanmean(PCADecodeAccShAA{i,1}));
  306. trainingpval(i,1)=(sum(shuffdiff>diff)+1)/(length(shuffdiff)+1);
  307. end
  308. %% overtraining
  309. figure;
  310. for i=1:size(PCADecodeAccOT,2)
  311. allsamples=cat(1,PCADecodeAccOT{3,i},PCADecodeAccShOT{3,i});
  312. for p=1:10000
  313. shuffsamples=allsamples(randperm(length(allsamples)));
  314. shuffdiff(p,1)=abs(mean(shuffsamples(1:length(allsamples)/2))-mean(shuffsamples(length(allsamples)/2+1:end)));
  315. end
  316. diff=abs(nanmean(PCADecodeAccOT{1,i})-nanmean(PCADecodeAccShOT{1,i}));
  317. OTpval(1,i)=(sum(shuffdiff>diff)+1)/(length(shuffdiff)+1);
  318. subplot(5,1,i)
  319. hist(shuffdiff);
  320. hold on;
  321. plot([diff diff],[0 4000]);
  322. end