RewHistTH_calculator.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. %Looking at the average firing rate for a given window in each of 4
  2. %current/previous reward conditions
  3. MaltValue=0.75;
  4. load ('RAWTH.mat');
  5. RAW=RAWTH;
  6. %run linear model and stats? 1 is yes, 0 is no
  7. runanalysis=1;
  8. %get parameters
  9. trialsback=10; %how many trials back to look
  10. Baseline=[-11 -1]; %For normalizing activity
  11. RDWindow=[0.75 1.95];
  12. PEWindow=[-1.5 -0.5]; %relative to RD
  13. CueWindow=[0 0.75];
  14. %save variables
  15. RewHistTH.trialsback=trialsback;
  16. RewHistTH.Baseline=Baseline;
  17. RewHistTH.RDWindow=RDWindow;
  18. RewHistTH.PEWindow=PEWindow;
  19. RewHistTH.CueWindow=CueWindow;
  20. %reset
  21. NN=0;
  22. if runanalysis==1
  23. for i=1:length(RAW) %loops through sessions
  24. %events
  25. RD=strmatch('RD',RAW(i).Einfo(:,2),'exact');
  26. R1=strmatch('R1withanylick',RAW(i).Einfo(:,2),'exact');
  27. R2=strmatch('R2withanylick',RAW(i).Einfo(:,2),'exact');
  28. R3=strmatch('R3withanylick',RAW(i).Einfo(:,2),'exact');
  29. Cue=strmatch('Cue',RAW(i).Einfo(:,2),'exact');
  30. %% linear model for impact of previous rewards
  31. %reset
  32. X=NaN(length(RAW(i).Erast{RD,1}(:,1)),trialsback+1);
  33. Y=[];
  34. AllTrials=[];
  35. %set up the matrix with outcome identity for each session
  36. rewards1=cat(2,RAW(i).Erast{R1,1}(:,1),ones(length(RAW(i).Erast{R1,1}(:,1)),1));
  37. rewards2=cat(2,RAW(i).Erast{R2,1}(:,1),MaltValue*ones(length(RAW(i).Erast{R2,1}(:,1)),1));
  38. rewards3=cat(2,RAW(i).Erast{R3,1}(:,1),zeros(length(RAW(i).Erast{R3,1}(:,1)),1));
  39. rewards=cat(1,rewards1,rewards2,rewards3);
  40. [rewards(:,1),ind]=sort(rewards(:,1));
  41. rewards(:,2)=rewards(ind,2);
  42. AllTrials(:,1)=rewards(:,2);
  43. AllTrials(:,2)=0;
  44. for k=1:length(RAW(i).Erast{RD,1}(:,1))
  45. time=RAW(i).Erast{RD,1}(k,1);
  46. entry=find(round(rewards(:,1))==round(time));
  47. for m=1:trialsback+1
  48. if entry+1-m>0
  49. X(k,m)=rewards(entry+1-m,2);
  50. end
  51. end
  52. AllTrials(entry,2)=1;
  53. end
  54. for j= 1:length(RAW(i).Nrast) %Number of neurons within sessions
  55. NN=NN+1;
  56. rewspk=0;
  57. basespk=0;
  58. %get mean baseline firing for all reward trials
  59. [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Cue},Baseline,{2});% makes trial by trial rasters for baseline
  60. for y= 1:B1n
  61. basespk(1,y)=sum(Bcell1{1,y}>Baseline(1));
  62. end
  63. Bhz=basespk/(Baseline(1,2)-Baseline(1,1));
  64. Bmean=nanmean(Bhz);
  65. Bstd=nanstd(Bhz);
  66. %get trial by trial firing rate for all reward trials
  67. Window=RDWindow;
  68. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
  69. for y= 1:EVn
  70. rewspk(y,1)=sum(EVcell{1,y}>Window(1));
  71. end
  72. Y=((rewspk(1:end,1)/(Window(1,2)-Window(1,1)))-Bmean)/Bstd; %subtract baseline firing
  73. RewHistTH.RDHz{NN,1}=Y(trialsback+1:end);
  74. %get trial by trial firing rate for all PE trials based on fixed window
  75. rewspk=0;
  76. Window=PEWindow;
  77. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
  78. for y= 1:EVn
  79. rewspk(y,1)=sum(EVcell{1,y}>Window(1));
  80. end
  81. Y=((rewspk(1:end,1)/(Window(1,2)-Window(1,1)))-Bmean)/Bstd; %z-score
  82. RewHistTH.PEHz{NN,1}=Y(trialsback+1:end);
  83. %get trial by trial firing rate for all cue trials based on fixed window
  84. CueList=[];
  85. for k=1:length(RAW(i).Erast{RD})
  86. CueList(k,1)=RAW(i).Erast{Cue}(max(find(RAW(i).Erast{Cue}<RAW(i).Erast{RD}(k,1))),1);
  87. end
  88. Window=CueWindow;
  89. rewspk=0;
  90. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),CueList,CueWindow,{2});% makes trial by trial rasters for event
  91. for y= 1:EVn
  92. rewspk(y,1)=sum(EVcell{1,y}>CueWindow(1));
  93. end
  94. Y=((rewspk(1:end,1)/(CueWindow(1,2)-CueWindow(1,1)))-Bmean)/Bstd; %subtract baseline to keep it linear
  95. RewHistTH.CueHz{NN,1}=Y(trialsback+1:end);
  96. RewHistTH.Predictors{NN,1}=X(trialsback+1:end,:);
  97. RewHistTH.AllTrials{NN,1}=AllTrials;
  98. fprintf('Neuron # %d\n',NN);
  99. end
  100. end
  101. end
  102. save('RewHistTH.mat','RewHistTH');