e_SingleUnitDecoding.m 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. %decodes trial identity from spiking across bins for each individual neuron
  2. clear all;
  3. load ('R_2R.mat');
  4. load ('RAW.mat');
  5. folds = 5; %number of times cross-validated:
  6. shuffs = 1; %number of shuffled models created
  7. %load parameters
  8. BinDura=R_2R.Param.BinDura;
  9. bins=R_2R.Param.bins;
  10. binint=R_2R.Param.binint;
  11. binstart=R_2R.Param.binstart;
  12. %setup variables
  13. UnitDec=[];
  14. NN = 0;
  15. for i=1:length(RAW) %loops through sessions
  16. if strcmp('NA',RAW(i).Type(1:2)) | strcmp('VP',RAW(i).Type(1:2)) %check if it's a suc vs mal session
  17. %events being compared
  18. RD1=strcmp('RD1', RAW(i).Einfo(:,2));
  19. RD2=strcmp('RD2', RAW(i).Einfo(:,2));
  20. for j= 1:size(RAW(i).Nrast,1) %Number of neurons per session
  21. NN=NN+1; %neuron counter
  22. for l=1:bins
  23. LowHz=zeros(1,1);
  24. DecodeSpikes=NaN(1,1);
  25. DecodeRs=NaN(1,1);
  26. [PSR1,N1]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD1},[(BinDura(1)+(binstart - binint)+l*binint) (BinDura(2)+(binstart - binint)+l*binint)],{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
  27. for m=1:length(PSR1)
  28. DecodeSpikes(m,1)=sum(PSR1{1,m}>(binstart));
  29. DecodeRs(m,1)=1;
  30. end
  31. %get all the spikes from reward 1p2 trials
  32. [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD2},[(BinDura(1)+(binstart - binint)+l*binint) (BinDura(2)+(binstart - binint)+l*binint)],{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
  33. for n=1:length(PSR2)
  34. DecodeSpikes(n+m,1)=sum(PSR2{1,n}>(binstart));
  35. DecodeRs(n+m,1)=2;
  36. end
  37. if sum(DecodeSpikes(1:N1,1))<7 || sum(DecodeSpikes((1+N1):(N1+N2),1))<7 %prevents errors with LDA on conditions with no variance
  38. LowHz(1,1)=1;
  39. end
  40. %if one neuron or more has too few spikes, so LDA has error because not
  41. %enough variance, this removes those neurons from the analysis
  42. if sum(LowHz)==0
  43. %creating models
  44. CVacc = NaN(folds,1);
  45. CVaccSh = NaN(folds,1);
  46. %normal model
  47. Partitions = cvpartition(DecodeRs,'KFold',folds);
  48. for r = 1:folds
  49. LDAModel = fitcdiscr(DecodeSpikes(Partitions.training(r),:),DecodeRs(Partitions.training(r)));
  50. prediction = predict(LDAModel,DecodeSpikes(Partitions.test(r),:));
  51. actual = DecodeRs(Partitions.test(r));
  52. correct = prediction - actual;
  53. CVacc(r) = sum(correct==0) / length(correct);
  54. end
  55. %shuffled model
  56. for q=1:shuffs
  57. DecodeRsSh=DecodeRs(randperm(length(DecodeRs)));
  58. PartitionsSh = cvpartition(DecodeRsSh,'KFold',folds);
  59. for s = 1:folds
  60. LDAModelSh = fitcdiscr(DecodeSpikes(PartitionsSh.training(s),:),DecodeRsSh(PartitionsSh.training(s)));
  61. predictionSh = predict(LDAModelSh,DecodeSpikes(PartitionsSh.test(s),:));
  62. actualSh = DecodeRsSh(PartitionsSh.test(s));
  63. correctSh = predictionSh - actualSh;
  64. CVaccSh(s) = sum(correctSh==0) / length(correctSh);
  65. end
  66. AccShuff(q,1) = nanmean(CVaccSh);
  67. end
  68. UnitDec.True(NN,l) = nanmean(CVacc);
  69. UnitDec.Shuff(NN,l) = nanmean(AccShuff);
  70. else
  71. UnitDec.True(NN,l) = NaN;
  72. UnitDec.Shuff(NN,l) = NaN;
  73. end
  74. end
  75. fprintf('Neuron ID # %d\n',NN);
  76. end %neurons: FOR j= 1:size(RAW(i).Nrast,1)
  77. end %checking if the right session type
  78. end %sessions: FOR i=1:length(RAW)
  79. save('UnitDec_2R.mat','UnitDec');