RewHistCued_calculator.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. %Looking at the average firing rate for a given window in each of 4
  2. %current/previous reward conditions
  3. %run linear model and stats? 1 is yes, 0 is no
  4. runanalysis=1;
  5. %which sessions are we looking at
  6. IncRats=[3 4 9 10]; %needs to match 'Cued.m'
  7. IncDays=11:20; %needs to match 'Cued.m'
  8. %get parameters
  9. trialsback=10; %how many trials back to look
  10. Baseline=[-11 -1]; %Relative to cue
  11. RDWindow=[0.75 1.95];
  12. PEWindow=[-1.5 -0.5]; %relative to RD
  13. CueWindow=[0 0.75];
  14. %reset
  15. NN=0;
  16. EvMeanz=0;
  17. if runanalysis==1
  18. load('RAWCued.mat')
  19. RAW=RAWCued;
  20. for i=1:length(RAW) %loops through sessions
  21. if any(IncRats==RAW(i).Rat) && any(IncDays==RAW(i).Day) %only look at included sessions
  22. %events
  23. Cues=strmatch('Cues',RAW(i).Einfo(:,2),'exact');
  24. RDC=strmatch('RDC',RAW(i).Einfo(:,2),'exact');
  25. RDA=strmatch('RDA',RAW(i).Einfo(:,2),'exact');
  26. R1=strmatch('R1withanylick',RAW(i).Einfo(:,2),'exact');
  27. R2=strmatch('R2withanylick',RAW(i).Einfo(:,2),'exact');
  28. %% linear model for impact of previous rewards
  29. %reset
  30. X=[];
  31. Y=[];
  32. %set up the matrix with outcome identity for each session
  33. rewards1=cat(2,RAW(i).Erast{R1,1}(:,1),ones(length(RAW(i).Erast{R1,1}(:,1)),1));
  34. rewards2=cat(2,RAW(i).Erast{R2,1}(:,1),zeros(length(RAW(i).Erast{R2,1}(:,1)),1));
  35. rewards=cat(1,rewards1,rewards2);
  36. [rewards(:,1),ind]=sort(rewards(:,1));
  37. rewards(:,2)=rewards(ind,2);
  38. %put both conditions together
  39. RDCkey=cat(2,RAW(i).Erast{RDC,1}(:,1),ones(length(RAW(i).Erast{RDC,1}(:,1)),1)); %1 if predictive trial
  40. RDAkey=cat(2,RAW(i).Erast{RDA,1}(:,1),zeros(length(RAW(i).Erast{RDA,1}(:,1)),1)); %0 if not
  41. RDall=cat(1,RDCkey,RDAkey);
  42. [RDall(:,1),ind]=sort(RDall(:,1));
  43. RDall(:,2)=RDall(ind,2);
  44. %create results key
  45. for k=trialsback+1:length(RDall(:,1))
  46. time=RDall(k,1);
  47. entry=find(rewards(:,1)==time);
  48. for m=1:trialsback+1
  49. X(k-trialsback,m)=rewards(entry+1-m,2);
  50. end
  51. %add a 1 to next column if predicted sucrose, column after
  52. %that if predicted maltodextrin
  53. if RDall(k,2)==1
  54. if X(k-trialsback,1)==1
  55. X(k-trialsback,trialsback+2)=1;
  56. X(k-trialsback,trialsback+3)=0;
  57. else
  58. X(k-trialsback,trialsback+2)=0;
  59. X(k-trialsback,trialsback+3)=1;
  60. end
  61. else
  62. X(k-trialsback,trialsback+2:trialsback+3)=0;
  63. end
  64. %X(k-trialsback,trialsback+4)=(rewards(entry,1)-rewards(entry-1,1))/60; %time since last reward, in minutes
  65. end
  66. for j= 1:length(RAW(i).Nrast) %Number of neurons within sessions
  67. NN=NN+1;
  68. basespk=0;
  69. %get mean baseline firing for all trials
  70. [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Cues},Baseline,{2});% makes trial by trial rasters for baseline
  71. for y= 1:B1n
  72. basespk(1,y)=sum(Bcell1{1,y}>Baseline(1));
  73. end
  74. Bhz=basespk/(Baseline(1,2)-Baseline(1,1));
  75. Bmean=nanmean(Bhz);
  76. Bstd=nanstd(Bhz);
  77. %get trial by trial firing rate for all reward trials
  78. rewspk=[];
  79. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RDall(:,1),RDWindow,{2});% makes trial by trial rasters for event
  80. for y= 1:EVn
  81. rewspk(y,1)=sum(EVcell{1,y}>RDWindow(1));
  82. end
  83. Y=((rewspk(trialsback+1:end,1)/(RDWindow(1,2)-RDWindow(1,1)))-Bmean)/Bstd; %normalize the activity to baseline
  84. RewHistCued.RDHz{NN,1}=Y;
  85. %get trial by trial firing rate for all port entries
  86. rewspk=[];
  87. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RDall(:,1),PEWindow,{2});% makes trial by trial rasters for event
  88. for y= 1:EVn
  89. rewspk(y,1)=sum(EVcell{1,y}>PEWindow(1));
  90. end
  91. Y=((rewspk(trialsback+1:end,1)/(PEWindow(1,2)-PEWindow(1,1)))-Bmean)/Bstd; %normalize the activity to baseline
  92. RewHistCued.PEHz{NN,1}=Y;
  93. %get trial by trial firing rate for all cues
  94. CueList=[];
  95. rewspk=[];
  96. for k=1:length(RDall(:,1))
  97. CueList(k,1)=RAW(i).Erast{Cues}(max(find(RAW(i).Erast{Cues}<RDall(k,1))),1);
  98. end
  99. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),CueList,CueWindow,{2});% makes trial by trial rasters for event
  100. for y= 1:EVn
  101. rewspk(y,1)=sum(EVcell{1,y}>CueWindow(1));
  102. end
  103. Y=((rewspk(trialsback+1:end,1)/(CueWindow(1,2)-CueWindow(1,1)))-Bmean)/Bstd; %subtract baseline to keep it linear
  104. RewHistCued.CueHz{NN,1}=Y;
  105. %save the predictors
  106. RewHistCued.Predictors{NN,1}=X;
  107. fprintf('Neuron # %d\n',NN);
  108. end
  109. end
  110. end
  111. end
  112. save('RewHistCued.mat','RewHistCued');