f_PooledEnsDecoding.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. %decodes trial identity from spiking across bins for neurons pooled across sessions
  2. %also does this for only selective and non-selective neurons
  3. %need to run A, B, and D first
  4. clear all;
  5. load ('R_2R.mat');
  6. load ('RAW.mat');
  7. %NAc is 1, VP is 2
  8. region={'NA';'VP'};
  9. %decoding parameters
  10. NumNeurons=[10 25 50 100 150]; %matrix of how many neurons to use on each iteration
  11. repetitions=20; %how many times to run the analysis
  12. folds = 5; %number of times cross-validated:
  13. shuffs = 1; %number of shuffled models created
  14. %load parameters
  15. BinDura=R_2R.Param.BinDura;
  16. bins=R_2R.Param.bins;
  17. binint=R_2R.Param.binint;
  18. binstart=R_2R.Param.binstart;
  19. %Need to keep number of trials used in VP and NAc constant, so have to pick
  20. %minimum number across all sessions in both regions. Right now with
  21. %existing sessions it's 20 Ev1s and 20 Ev2s
  22. Ev1s=20;
  23. Ev2s=20;
  24. %find number of trials in each session
  25. for i=1:length(RAW)
  26. %events being compared
  27. Ev1=strcmp('RD1', RAW(i).Einfo(:,2));
  28. Ev2=strcmp('RD2', RAW(i).Einfo(:,2));
  29. Ev1perSess(i)=length(RAW(i).Erast{Ev1,1});
  30. Ev2perSess(i)=length(RAW(i).Erast{Ev2,1});
  31. end
  32. %setup variables
  33. PoolDec=[];
  34. NN = 0;
  35. for e=1:3 %different selections of neurons
  36. %pick which set of neurons: all, reward-specific, or non-reward specific
  37. if e==1 selection=R_2R.Ninfo; end %all neurons
  38. if e==2 selection=R_2R.RSinfo; end %reward-selective neurons
  39. if e==3 selection=R_2R.RNSinfo; end %reward-nonselective neurons
  40. for f=1:2 %region
  41. TotalNeurons = 0;
  42. %total number of neurons in all sessions and events per session
  43. for i=1:length(RAW)
  44. if strcmp(region(f),RAW(i).Type(1:2))
  45. TotalNeurons=TotalNeurons+size(RAW(i).Nrast,1);
  46. end
  47. end
  48. for l=1:bins
  49. DecodeSpikes=NaN(1,1);
  50. AllSpikes=NaN(1,1);
  51. NN = 0;
  52. for i=1:length(RAW) %loops through sessions
  53. if strcmp(region(f),RAW(i).Type(1:2)) && Ev1perSess(i)>=20 && Ev2perSess(i)>=20
  54. %events being compared
  55. Ev1=strcmp('RD1', RAW(i).Einfo(:,2));
  56. Ev2=strcmp('RD2', RAW(i).Einfo(:,2));
  57. for j= 1:size(RAW(i).Nrast,1) %Number of neurons per session
  58. if sum(strcmp(RAW(i).Ninfo(j,1),selection(:,1)) & strcmp(RAW(i).Ninfo(j,2),selection(:,2)))==1 %check if this neuron is on the selection list
  59. NN=NN+1; %neuron counter
  60. Ev1Spikes=NaN(1,1);
  61. Ev2Spikes=NaN(1,1);
  62. %get number of spikes for this neuron in this bin for all
  63. %Ev1 trials
  64. [PSR1,N1]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Ev1},[(BinDura(1)+(binstart - binint)+l*binint) (BinDura(2)+(binstart - binint)+l*binint)],{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
  65. for m=1:length(PSR1)
  66. Ev1Spikes(m,1)=sum(PSR1{1,m}>(binstart));
  67. end
  68. %pick which trials get used for decoding
  69. SetupTrials=cat(1,ones(Ev1s,1),zeros(length(PSR1)-Ev1s,1));
  70. Trials=(SetupTrials(randperm(length(SetupTrials)))==1);
  71. %put spikes from those trials in a matrix
  72. AllSpikes(1:Ev1s,NN)=Ev1Spikes(Trials,1);
  73. %get all the spikes from reward 1p2 trials
  74. [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Ev2},[(BinDura(1)+(binstart - binint)+l*binint) (BinDura(2)+(binstart - binint)+l*binint)],{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
  75. for n=1:length(PSR2)
  76. Ev2Spikes(n,1)=sum(PSR2{1,n}>(binstart));
  77. end
  78. %pick which trials get used for decoding
  79. SetupTrials2=cat(1,ones(Ev2s,1),zeros(length(PSR2)-Ev2s,1));
  80. Trials2=(SetupTrials2(randperm(length(SetupTrials2)))==1);
  81. %put spikes from those trials in a matrix
  82. AllSpikes((Ev1s+1):(Ev1s+Ev2s),NN)=Ev2Spikes(Trials2,1);
  83. end
  84. end %neurons
  85. end %checking if enough events in that session
  86. end %sessions
  87. TotalNeurons=NN;
  88. %do the decoding
  89. for v = 1:length(NumNeurons)
  90. if TotalNeurons>NumNeurons(v)
  91. for u=1:repetitions
  92. LowHz=zeros(1,NumNeurons(v)); %reset the lowHz identifier
  93. %pick which neurons to use
  94. SetupSel=cat(1,ones(NumNeurons(v),1),zeros(TotalNeurons-NumNeurons(v),1));
  95. Sel=(SetupSel(randperm(length(SetupSel)))==1);
  96. %setup the event identity matrix for decoding
  97. %DecodeRs=zeros(Ev1s+Ev2s,NumNeurons(v));
  98. DecodeRs(1:Ev1s,1)=1;
  99. %DecodeRs(1:Ev1s,end)=1;
  100. %DecodeRs((Ev1s+1):(Ev1s+Ev2s),2:end-1)=1;
  101. DecodeRs((Ev1s+1):(Ev1s+Ev2s),1)=0;
  102. %setup decode spike matrix
  103. DecodeSpikes=AllSpikes(:,Sel);
  104. %find neurons with too few spikes
  105. for t=1:NumNeurons(v)
  106. if sum(DecodeSpikes(1:Ev1s,t))<7 || sum(DecodeSpikes((1+Ev1s):(Ev1s+Ev2s),t))<7
  107. LowHz(1,t)=1;
  108. end
  109. end
  110. %remove neurons with too few spikes
  111. if sum(LowHz)>0
  112. Sel2=LowHz<1;
  113. DecodeSpikes=DecodeSpikes(:,Sel2);
  114. end
  115. %set up validation result matrices
  116. CVacc = NaN(folds,1);
  117. CVaccSh = NaN(folds,1);
  118. %normal model
  119. for r = 1:folds
  120. Partitions = cvpartition(DecodeRs,'KFold',folds);
  121. SVMModel = fitcdiscr(DecodeSpikes(Partitions.training(r),:),DecodeRs(Partitions.training(r)));
  122. prediction = predict(SVMModel,DecodeSpikes(Partitions.test(r),:));
  123. actual = DecodeRs(Partitions.test(r));
  124. correct = prediction - actual;
  125. CVacc(r) = sum(correct==0) / length(correct);
  126. end
  127. %shuffled model
  128. for q=1:shuffs
  129. DecodeRsSh=DecodeRs(randperm(length(DecodeRs)));
  130. PartitionsSh = cvpartition(DecodeRsSh,'KFold',folds);
  131. for s = 1:folds
  132. SVMModelSh = fitcdiscr(DecodeSpikes(PartitionsSh.training(s),:),DecodeRsSh(PartitionsSh.training(s)));
  133. predictionSh = predict(SVMModelSh,DecodeSpikes(PartitionsSh.test(s),:));
  134. actualSh = DecodeRsSh(PartitionsSh.test(s));
  135. correctSh = predictionSh - actualSh;
  136. CVaccSh(s) = sum(correctSh==0) / length(correctSh);
  137. end
  138. AccShuff(q,1) = nanmean(CVaccSh);
  139. end
  140. PoolDec{e,f}.True{v,1}(u,l) = nanmean(CVacc);
  141. PoolDec{e,f}.Shuff{v,1}(u,l) = nanmean(AccShuff);
  142. end %repetitions
  143. fprintf(['Selection #' num2str(e) ', Region #' num2str(f) ', Bin #' num2str(l) ', Condition #' num2str(v) '\n']);
  144. else
  145. PoolDec{e,f}.True{v,1} = NaN;
  146. PoolDec{e,f}.Shuff{v,1} = NaN;
  147. fprintf(['Selection #' num2str(e) ', Region #' num2str(f) ', Bin #' num2str(l) ', Condition #' num2str(v) '\n']);
  148. end %checking if there are enough neurons
  149. end %conditions (number of neurons)
  150. end %bins
  151. end %region
  152. end %selections
  153. save('PoolDec_2R.mat','PoolDec');
  154. R_2R.Param.NumNeurons=NumNeurons;
  155. save('R_2R.mat','R_2R');