RewHist2R_calculator.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. %get parameters
  6. trialsback=10; %how many trials back to look
  7. Baseline=[-11 -1]; %Relative to cue
  8. RDWindow=[0.75 1.95];
  9. PEWindow=[-1.5 -0.5]; %relative to RD
  10. CueWindow=[0 0.75];
  11. %for looking across time
  12. bins=45;
  13. binstart=-1.5; %in seconds
  14. binsize=0.1; %in seconds
  15. %save variables
  16. RewHist2R.trialsback=trialsback;
  17. RewHist2R.Baseline=Baseline;
  18. RewHist2R.RDWindow=RDWindow;
  19. RewHist2R.PEWindow=PEWindow;
  20. RewHist2R.CueWindow=CueWindow;
  21. RewHist2R.bins=bins;
  22. RewHist2R.binstart=binstart;
  23. RewHist2R.binsize=binsize;
  24. %reset
  25. NN=0;
  26. EvMeanz=0;
  27. if runanalysis==1
  28. load ('RAWintBlocks.mat');
  29. RAW=RAWblocks;
  30. for i=1:length(RAW) %loops through sessions
  31. if strcmp('VP',RAW(i).Region(1:2)) && strcmp('I',RAW(i).Blocks) %only look at VP interspersed sessions
  32. %events
  33. EV1=strmatch('RD1P2',RAW(i).Einfo(:,2),'exact');
  34. EV2=strmatch('RD1P1',RAW(i).Einfo(:,2),'exact');
  35. EV3=strmatch('RD2P2',RAW(i).Einfo(:,2),'exact');
  36. EV4=strmatch('RD2P1',RAW(i).Einfo(:,2),'exact');
  37. RD=strmatch('RD',RAW(i).Einfo(:,2),'exact');
  38. Cue=strmatch('Cue',RAW(i).Einfo(:,2),'exact');
  39. R1=strmatch('R1withanylick',RAW(i).Einfo(:,2),'exact');
  40. R2=strmatch('R2withanylick',RAW(i).Einfo(:,2),'exact');
  41. % linear model for impact of previous rewards
  42. %reset
  43. X=[];
  44. Y=[];
  45. %set up the matrix with outcome identity for each session
  46. rewards1=cat(2,RAW(i).Erast{R1,1}(:,1),ones(length(RAW(i).Erast{R1,1}(:,1)),1));
  47. rewards2=cat(2,RAW(i).Erast{R2,1}(:,1),zeros(length(RAW(i).Erast{R2,1}(:,1)),1));
  48. rewards=cat(1,rewards1,rewards2);
  49. [rewards(:,1),ind]=sort(rewards(:,1));
  50. rewards(:,2)=rewards(ind,2);
  51. for k=trialsback+1:length(RAW(i).Erast{RD,1}(:,1))
  52. time=RAW(i).Erast{RD,1}(k,1);
  53. entry=find(rewards(:,1)==time);
  54. for m=1:trialsback+1
  55. X(k-trialsback,m)=rewards(entry+1-m,2);
  56. end
  57. end
  58. for j= 1:length(RAW(i).Nrast) %Number of neurons within sessions
  59. NN=NN+1;
  60. basespk=0;
  61. %get mean baseline firing for all reward trials
  62. [Bcell1,B1n]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{Cue},Baseline,{2});% makes trial by trial rasters for baseline
  63. for y= 1:B1n
  64. basespk(1,y)=sum(Bcell1{1,y}>Baseline(1));
  65. end
  66. Bhz=basespk/(Baseline(1,2)-Baseline(1,1));
  67. Bmean=nanmean(Bhz);
  68. Bstd=nanstd(Bhz);
  69. for k=1:bins
  70. Window=[binstart+(k-1)*binsize binstart+k*binsize];
  71. rewspk=[];
  72. %get trial by trial firing rate for all reward trials
  73. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
  74. for y= 1:EVn
  75. rewspk(y,1)=sum(EVcell{1,y}>Window(1));
  76. end
  77. Y=((rewspk(trialsback+1:end,1)/(Window(1,2)-Window(1,1)))-Bmean)/Bstd; %normalize the activity to baseline
  78. RewHist2R.BinHz{NN,k}=Y;
  79. end
  80. %get trial by trial firing rate for all reward trials based on fixed window
  81. rewspk=0;
  82. Window=RDWindow;
  83. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
  84. for y= 1:EVn
  85. rewspk(y,1)=sum(EVcell{1,y}>Window(1));
  86. end
  87. Y=((rewspk(trialsback+1:end,1)/(Window(1,2)-Window(1,1)))-Bmean)/Bstd; %z-score
  88. RewHist2R.RDHz{NN,1}=Y;
  89. %get trial by trial firing rate for all PE trials based on fixed window
  90. rewspk=0;
  91. Window=PEWindow;
  92. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),RAW(i).Erast{RD},Window,{2});% makes trial by trial rasters for event
  93. for y= 1:EVn
  94. rewspk(y,1)=sum(EVcell{1,y}>Window(1));
  95. end
  96. Y=((rewspk(trialsback+1:end,1)/(Window(1,2)-Window(1,1)))-Bmean)/Bstd; %z-score
  97. RewHist2R.PEHz{NN,1}=Y;
  98. %get trial by trial firing rate for all cue trials based on fixed window
  99. CueList=[];
  100. for k=1:length(RAW(i).Erast{RD})
  101. CueList(k,1)=RAW(i).Erast{Cue}(max(find(RAW(i).Erast{Cue}<RAW(i).Erast{RD}(k,1))),1);
  102. end
  103. Window=CueWindow;
  104. rewspk=0;
  105. [EVcell,EVn]=MakePSR04(RAW(i).Nrast(j),CueList,CueWindow,{2});% makes trial by trial rasters for event
  106. for y= 1:EVn
  107. rewspk(y,1)=sum(EVcell{1,y}>CueWindow(1));
  108. end
  109. Y=((rewspk(trialsback+1:end,1)/(CueWindow(1,2)-CueWindow(1,1)))-Bmean)/Bstd; %subtract baseline to keep it linear
  110. RewHist2R.CueHz{NN,1}=Y;
  111. %save the predictors
  112. RewHist2R.Predictors{NN,1}=X;
  113. fprintf('Neuron # %d\n',NN);
  114. end
  115. end
  116. end
  117. end
  118. save('RewHist2R.mat','RewHist2R');