g_SingleUnitDecodeSucroseAlcohol.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. %decodes trial identity from spiking across bins for each individual neuron
  2. clear all
  3. load('RAWpsvpALL.mat')
  4. load('RAWvpppsALL.mat')
  5. load('RAWvppaALL.mat')
  6. load('RAWvpppaALL.mat')
  7. for PavInst=1:4; %DS is 1, Pav is 2
  8. if PavInst==1
  9. RAW=RAWvpppsALL;
  10. else if PavInst==2
  11. RAW=RAWpsvpALL;
  12. else if PavInst==3
  13. RAW=RAWvpppaALL;
  14. else if PavInst==4
  15. RAW=RAWvppaALL;
  16. end
  17. end
  18. end
  19. end
  20. folds = 5; %number of times cross-validated:
  21. Dura=[0 0.3]; %size of bin used for decoding:
  22. bins = 13; %number of bins
  23. binint = 0.3; %spacing of bins
  24. binstart = -.9; %start time of first bin relative to event
  25. shuffs = 1; %number of shuffled models created
  26. trials = 30; %max number of trials to use
  27. NN = 0;
  28. %events being compared
  29. % UnitMdlAcc=NaN(100,bins);
  30. % UnitMdlAccShuff=NaN(100,bins);
  31. for i=1:length(RAW) %loops through sessions
  32. if (PavInst==1 | PavInst==2)
  33. RD1=strcmp('CSPlus1', RAW(i).Einfo(:,2));
  34. RD2=strcmp('CSMinus1', RAW(i).Einfo(:,2));
  35. else
  36. RD1=strcmp('CSPlus', RAW(i).Einfo(:,2));
  37. RD2=strcmp('CSMinus', RAW(i).Einfo(:,2));
  38. end
  39. for j= 1:size(RAW(i).Nrast,1) %Number of neurons per session
  40. NN=NN+1; %neuron counter
  41. for l=1:bins
  42. LowHz=zeros(1,1);
  43. DecodeSpikes=NaN(1,1);
  44. DecodeRs=NaN(1,1);
  45. [PSR1,N1]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD1},[(Dura(1)+(binstart - binint)+l*binint) (Dura(2)+(binstart - binint)+l*binint)],{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
  46. %only do at most the number of trials listed above
  47. if N1>trials
  48. trials1=trials;
  49. else
  50. trials1=N1;
  51. end
  52. for m=1:trials1
  53. DecodeSpikes(m,1)=sum(PSR1{1,m}>(binstart));
  54. DecodeRs(m,1)=1;
  55. end
  56. %get all the spikes from reward 1p2 trials
  57. [PSR2,N2]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD2},[(Dura(1)+(binstart - binint)+l*binint) (Dura(2)+(binstart - binint)+l*binint)],{2});% makes trial by trial rasters. PSR1 is a cell(neurons, trials)
  58. %only do at most the number of trials listed above
  59. if N2>trials
  60. trials2=trials;
  61. else
  62. trials2=N2;
  63. end
  64. for n=1:trials2
  65. DecodeSpikes(n+m,1)=sum(PSR2{1,n}>(binstart));
  66. DecodeRs(n+m,1)=2;
  67. end
  68. if sum(DecodeSpikes(1:trials1,1))<7 || sum(DecodeSpikes((1+trials1):(trials1+trials2),1))<7 %prevents errors with LDA on conditions with no variance
  69. LowHz(1,1)=1;
  70. end
  71. %if one neuron or more has too few spikes, so LDA has error because not
  72. %enough variance, this removes those neurons from the analysis
  73. if sum(LowHz)==0
  74. %creating models
  75. CVacc = NaN(folds,1);
  76. CVaccSh = NaN(folds,1);
  77. %normal model
  78. for r = 1:folds
  79. Partitions = cvpartition(DecodeRs,'KFold',folds);
  80. LDAModel = fitcdiscr(DecodeSpikes(Partitions.training(r),:),DecodeRs(Partitions.training(r)));
  81. prediction = predict(LDAModel,DecodeSpikes(Partitions.test(r),:));
  82. actual = DecodeRs(Partitions.test(r));
  83. correct = prediction - actual;
  84. CVacc(r) = sum(correct==0) / length(correct);
  85. end
  86. %shuffled model
  87. for q=1:shuffs
  88. DecodeRsSh=DecodeRs(randperm(length(DecodeRs)));
  89. PartitionsSh = cvpartition(DecodeRsSh,'KFold',folds);
  90. for s = 1:folds
  91. LDAModelSh = fitcdiscr(DecodeSpikes(PartitionsSh.training(s),:),DecodeRsSh(PartitionsSh.training(s)));
  92. predictionSh = predict(LDAModelSh,DecodeSpikes(PartitionsSh.test(s),:));
  93. actualSh = DecodeRsSh(PartitionsSh.test(s));
  94. correctSh = predictionSh - actualSh;
  95. CVaccSh(s) = sum(correctSh==0) / length(correctSh);
  96. end
  97. AccShuff(q,1) = nanmean(CVaccSh);
  98. end
  99. UnitMdlAcc(NN,l) = nanmean(CVacc);
  100. UnitMdlAccShuff(NN,l) = nanmean(AccShuff);
  101. else
  102. UnitMdlAcc(NN,l) = NaN;
  103. UnitMdlAccShuff(NN,l) = NaN;
  104. end
  105. end
  106. fprintf('Neuron ID # %d\n',NN);
  107. end %neurons: FOR j= 1:size(RAW(i).Nrast,1)
  108. end %sessions: FOR i=1:length(RAW)
  109. if PavInst==1
  110. UnitMdlAccDSSucrose=UnitMdlAcc;
  111. UnitMdlAccDSShuffSucrose=UnitMdlAccShuff;
  112. save('UnitMdlDSSucrose.mat','UnitMdlAccDSSucrose')
  113. save('UnitMdlDSShuffSucrose.mat','UnitMdlAccDSShuffSucrose')
  114. else if PavInst==2
  115. UnitMdlAccPavSucrose=UnitMdlAcc;
  116. UnitMdlAccPavShuffSucrose=UnitMdlAccShuff;
  117. save('UnitMdlPavSucrose.mat','UnitMdlAccPavSucrose')
  118. save('UnitMdlPavShuffSucrose.mat','UnitMdlAccPavShuffSucrose')
  119. else if PavInst==3
  120. UnitMdlAccDSAlcohol=UnitMdlAcc;
  121. UnitMdlAccDSShuffAlcohol=UnitMdlAccShuff;
  122. save('UnitMdlDSAlcohol.mat','UnitMdlAccDSAlcohol')
  123. save('UnitMdlDSShuffAlcohol.mat','UnitMdlAccDSShuffAlcohol')
  124. else if PavInst==4
  125. UnitMdlAccPavAlcohol=UnitMdlAcc;
  126. UnitMdlAccPavShuffAlcohol=UnitMdlAccShuff;
  127. save('UnitMdlPavAlcohol.mat','UnitMdlAccPavAlcohol')
  128. save('UnitMdlPavShuffAlcohol.mat','UnitMdlAccPavShuffAlcohol')
  129. end
  130. end
  131. end
  132. end
  133. end