GenerateTHDataForModeling.m 4.6 KB

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