f_PooledEnsDecoding.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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=50; %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. %if you change this to e=1:3, you can also look at nonselective neurons
  36. for e=1:2 %different selections of neurons
  37. %pick which set of neurons: all, reward-specific, or non-reward specific
  38. if e==1 selection=R_2R.Ninfo; end %all neurons
  39. if e==2 selection=R_2R.RSinfo; end %reward-selective neurons
  40. if e==3 selection=R_2R.RNSinfo; end %reward-nonselective neurons
  41. for f=1:2 %region
  42. TotalNeurons = 0;
  43. %total number of neurons in all sessions and events per session
  44. for i=1:length(RAW)
  45. if strcmp(region(f),RAW(i).Type(1:2))
  46. TotalNeurons=TotalNeurons+size(RAW(i).Nrast,1);
  47. end
  48. end
  49. for l=1:bins
  50. DecodeSpikes=NaN(1,1);
  51. AllSpikes=NaN(1,1);
  52. NN = 0;
  53. for i=1:length(RAW) %loops through sessions
  54. if strcmp(region(f),RAW(i).Type(1:2)) && Ev1perSess(i)>=20 && Ev2perSess(i)>=20
  55. %events being compared
  56. Ev1=strcmp('RD1', RAW(i).Einfo(:,2));
  57. Ev2=strcmp('RD2', RAW(i).Einfo(:,2));
  58. for j= 1:size(RAW(i).Nrast,1) %Number of neurons per session
  59. 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
  60. NN=NN+1; %neuron counter
  61. Ev1Spikes=NaN(1,1);
  62. Ev2Spikes=NaN(1,1);
  63. %get number of spikes for this neuron in this bin for all
  64. %Ev1 trials
  65. [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)
  66. for m=1:length(PSR1)
  67. Ev1Spikes(m,1)=sum(PSR1{1,m}>(binstart));
  68. end
  69. %pick which trials get used for decoding
  70. SetupTrials=cat(1,ones(Ev1s,1),zeros(length(PSR1)-Ev1s,1));
  71. Trials=(SetupTrials(randperm(length(SetupTrials)))==1);
  72. %put spikes from those trials in a matrix
  73. AllSpikes(1:Ev1s,NN)=Ev1Spikes(Trials,1);
  74. %get all the spikes from reward 1p2 trials
  75. [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)
  76. for n=1:length(PSR2)
  77. Ev2Spikes(n,1)=sum(PSR2{1,n}>(binstart));
  78. end
  79. %pick which trials get used for decoding
  80. SetupTrials2=cat(1,ones(Ev2s,1),zeros(length(PSR2)-Ev2s,1));
  81. Trials2=(SetupTrials2(randperm(length(SetupTrials2)))==1);
  82. %put spikes from those trials in a matrix
  83. AllSpikes((Ev1s+1):(Ev1s+Ev2s),NN)=Ev2Spikes(Trials2,1);
  84. end
  85. end %neurons
  86. end %checking if enough events in that session
  87. end %sessions
  88. TotalNeurons=NN;
  89. %do the decoding
  90. for v = 1:length(NumNeurons)
  91. if TotalNeurons>NumNeurons(v)
  92. for u=1:repetitions
  93. LowHz=zeros(1,NumNeurons(v)); %reset the lowHz identifier
  94. %pick which neurons to use
  95. SetupSel=cat(1,ones(NumNeurons(v),1),zeros(TotalNeurons-NumNeurons(v),1));
  96. Sel=(SetupSel(randperm(length(SetupSel)))==1);
  97. %setup the event identity matrix for decoding
  98. %DecodeRs=zeros(Ev1s+Ev2s,NumNeurons(v));
  99. DecodeRs(1:Ev1s,1)=1;
  100. %DecodeRs(1:Ev1s,end)=1;
  101. %DecodeRs((Ev1s+1):(Ev1s+Ev2s),2:end-1)=1;
  102. DecodeRs((Ev1s+1):(Ev1s+Ev2s),1)=0;
  103. %setup decode spike matrix
  104. DecodeSpikes=AllSpikes(:,Sel);
  105. %find neurons with too few spikes
  106. for t=1:NumNeurons(v)
  107. if sum(DecodeSpikes(1:Ev1s,t))<7 || sum(DecodeSpikes((1+Ev1s):(Ev1s+Ev2s),t))<7
  108. LowHz(1,t)=1;
  109. end
  110. end
  111. %remove neurons with too few spikes
  112. if sum(LowHz)>0
  113. Sel2=LowHz<1;
  114. DecodeSpikes=DecodeSpikes(:,Sel2);
  115. end
  116. %set up validation result matrices
  117. CVacc = NaN(folds,1);
  118. CVaccSh = NaN(folds,1);
  119. %normal model
  120. Partitions = cvpartition(DecodeRs,'KFold',folds);
  121. for r = 1:folds
  122. SVMModel = fitcdiscr(DecodeSpikes(Partitions.training(r),:),DecodeRs(Partitions.training(r)));
  123. prediction = predict(SVMModel,DecodeSpikes(Partitions.test(r),:));
  124. actual = DecodeRs(Partitions.test(r));
  125. correct = prediction - actual;
  126. CVacc(r) = sum(correct==0) / length(correct);
  127. end
  128. %shuffled model
  129. for q=1:shuffs
  130. DecodeRsSh=DecodeRs(randperm(length(DecodeRs)));
  131. PartitionsSh = cvpartition(DecodeRsSh,'KFold',folds);
  132. for s = 1:folds
  133. SVMModelSh = fitcdiscr(DecodeSpikes(PartitionsSh.training(s),:),DecodeRsSh(PartitionsSh.training(s)));
  134. predictionSh = predict(SVMModelSh,DecodeSpikes(PartitionsSh.test(s),:));
  135. actualSh = DecodeRsSh(PartitionsSh.test(s));
  136. correctSh = predictionSh - actualSh;
  137. CVaccSh(s) = sum(correctSh==0) / length(correctSh);
  138. end
  139. AccShuff(q,1) = nanmean(CVaccSh);
  140. end
  141. PoolDec{e,f}.True{v,1}(u,l) = nanmean(CVacc);
  142. PoolDec{e,f}.Shuff{v,1}(u,l) = nanmean(AccShuff);
  143. end %repetitions
  144. fprintf(['Selection #' num2str(e) ', Region #' num2str(f) ', Bin #' num2str(l) ', Condition #' num2str(v) '\n']);
  145. else
  146. PoolDec{e,f}.True{v,1} = NaN;
  147. PoolDec{e,f}.Shuff{v,1} = NaN;
  148. fprintf(['Selection #' num2str(e) ', Region #' num2str(f) ', Bin #' num2str(l) ', Condition #' num2str(v) '\n']);
  149. end %checking if there are enough neurons
  150. end %conditions (number of neurons)
  151. end %bins
  152. end %region
  153. end %selections
  154. save('PoolDec_2R.mat','PoolDec');
  155. R_2R.Param.NumNeurons=NumNeurons;
  156. save('R_2R.mat','R_2R');