Fig3.m 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. addpath(genpath('...\auxillary'));
  2. load('...\PVC\DataAll.mat')
  3. load('...\PVC\ZLowCa.mat')
  4. set(0,'DefaultFigureColormap',colormap(redblue(256)))
  5. SampleRate=30;
  6. StimDur=2;%second
  7. DelayDur=2;
  8. ResponseWind=2;
  9. NormBaseline=1;%second before stimOn 1s
  10. AfterStimDur=2;%second after stimOff 2s
  11. ButFiltBand=[5];
  12. StageName={'Naive','Exp','RevNaive','RevExp'};
  13. nTrainStage=length(StageName);% 'Naive','Exp','RevNaive','RevExp'
  14. BatchName={'Aud1','Vis1'};
  15. PeriodName={'Stim','Delay','Resp'};
  16. if nTrainStage==4
  17. TaskCode{1,1}=[...
  18. 1 1 12720;%Aud-Go 1
  19. 1 1 180; %Vis-Nogo 2
  20. 1 2 12720;%Aud-Go 3
  21. 1 2 180; %Vis-Nogo 4
  22. 2 3 12720;%Aud-Nogo 5
  23. 2 3 180; %Vis-Go 6
  24. 2 4 12720;%Aud-Nogo 7
  25. 2 4 180; %Vis-Go 8
  26. ];
  27. % batch 2
  28. TaskCode{1,2}=[...
  29. 2 1 12720;%Aud-NoGo 1
  30. 2 1 180; %Vis-Go 2
  31. 2 2 12720;%Aud-NoGo 3
  32. 2 2 180; %Vis-Go 4
  33. 1 3 12720;%Aud-Go 5
  34. 1 3 180; %Vis-NoGo 6
  35. 1 4 12720;%Aud-Go 7
  36. 1 4 180; %Vis-NoGo 8
  37. ];
  38. GoCode{1,1}=TaskCode{1,1}([1 3 6 8],:); %1
  39. GoCode{1,2}=TaskCode{1,2}([2 4 5 7],:); %2
  40. NoGoCode{1,1}=TaskCode{1,1}([2 4 5 7],:); %3
  41. NoGoCode{1,2}=TaskCode{1,2}([1 3 6 8],:); %4
  42. end
  43. for iBatch=1:2
  44. for iSub=1:size(DataAll.(BatchName{iBatch}),1)
  45. for iTrainStage=1:nTrainStage%FWD REV
  46. tic
  47. for iSession=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}))
  48. clear temp*
  49. temp=DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}){iSession};
  50. % pad nan before DLC vector to align rasberry pi cam
  51. tempLick=cat(1,nan(floor(abs(temp.CamStamp(1)-temp.RecStart)*SampleRate),1),temp.DLC_Lick);
  52. % find 2 consecutive frames with tongue deteected
  53. [~,LickIndDLC{1,iBatch}{iSub,iTrainStage}{iSession,1}]=...
  54. findpeaks(tempLick,'MinPeakHeight',.5,'MinPeakWidth',2);
  55. % time vector is different in ScanStamp and CamStamp in rasberry pi cam
  56. t{1,iBatch}{iSub,iTrainStage}{iSession,1}=temp.ScanStamp;
  57. tCam{1,iBatch}{iSub,iTrainStage}{iSession,1}=temp.CamStamp;
  58. StimOnT{1,iBatch}{iSub,iTrainStage}{iSession,1}=temp.StimStart;
  59. RewardOnT{1,iBatch}{iSub,iTrainStage}{iSession,1}=temp.RewardOn;
  60. StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=dsearchn(t{1,iBatch}{iSub,iTrainStage}{iSession,1},StimOnT{1,iBatch}{iSub,iTrainStage}{iSession,1});
  61. StimOnIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}=dsearchn(tCam{1,iBatch}{iSub,iTrainStage}{iSession,1},StimOnT{1,iBatch}{iSub,iTrainStage}{iSession,1});
  62. StimOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}+StimDur*SampleRate;
  63. StimOffIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}=StimOnIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}+StimDur*SampleRate;
  64. DelayOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=StimOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1};
  65. DelayOnIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}=StimOffIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1};
  66. DelayOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=DelayOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}+DelayDur*SampleRate;
  67. DelayOffIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}=DelayOnIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}+DelayDur*SampleRate;
  68. ResponseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=DelayOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1};
  69. ResponseOnIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}=DelayOffIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1};
  70. ResponseOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=ResponseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}+ResponseWind*SampleRate;
  71. ResponseOffIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}=ResponseOnIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}+ResponseWind*SampleRate;
  72. StimBaseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}-NormBaseline*SampleRate;
  73. RewardOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=dsearchn(t{1,iBatch}{iSub,iTrainStage}{iSession,1},RewardOnT{1,iBatch}{iSub,iTrainStage}{iSession,1});
  74. % electrical lick detector
  75. LickIndResp{1,iBatch}{iSub,iTrainStage}{iSession,1}=unique(dsearchn(t{1,iBatch}{iSub,iTrainStage}{iSession,1},temp.LickPulse));
  76. temp1=[ResponseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1} ResponseOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}];
  77. for iTrial=1:size(temp1,1)
  78. if any(LickIndResp{1,iBatch}{iSub,iTrainStage}{iSession,1}>=temp1(iTrial,1)&...
  79. LickIndResp{1,iBatch}{iSub,iTrainStage}{iSession,1}<=temp1(iTrial,2))
  80. IfLickResp{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial,1)=true;
  81. else
  82. IfLickResp{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial,1)=false;
  83. end
  84. end
  85. % DLC lick detector
  86. temp1=[DelayOnIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1} DelayOffIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}];
  87. for iTrial=1:size(temp1,1)
  88. if any(LickIndDLC{1,iBatch}{iSub,iTrainStage}{iSession,1}>=temp1(iTrial,1)&...
  89. LickIndDLC{1,iBatch}{iSub,iTrainStage}{iSession,1}<=temp1(iTrial,2))
  90. IfLickDelayDLC{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial,1)=true;
  91. else
  92. IfLickDelayDLC{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial,1)=false;
  93. end
  94. end
  95. tempIndHit=zeros(size(ZLowCa{1,iBatch}{iSub,iTrainStage}{iSession,1},1),1);
  96. for iTrial=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}){iSession}.StimStart)
  97. tempIndHit(StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  98. ResponseOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),1)=1;
  99. end
  100. ITIindex{1,iBatch}{iSub,iTrainStage}{iSession,1}=find(tempIndHit==0);
  101. % If TaskType == 1, that means that Aud-Go and Vis-Nogo.
  102. % If TaskType == 2, that means that Vis-Go and Aud-Nogo.
  103. StimTypeTemp{1,iBatch}{iSub,iTrainStage}{iSession,1}=...
  104. temp.Trial_Freq+temp.Trial_Grat;
  105. % AudTrial%12720 aud
  106. % VisTrial%180: vis
  107. % BlankTrial%720: vis&aud off blank
  108. for iTrial=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}){iSession}.StimStart)
  109. TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial,1)=...
  110. temp.TaskType;%TaskType
  111. TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial,2)=...
  112. iTrainStage;%TrainStage
  113. TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial,3)=...
  114. StimTypeTemp{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial);
  115. end
  116. % find Go trial in stim trial
  117. temp2=[ismember(TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1},...
  118. GoCode{1,iBatch}(iTrainStage,:),'rows') ...
  119. IfLickResp{1,iBatch}{iSub,iTrainStage}{iSession,1}];
  120. % Hit 1
  121. TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}...
  122. (ismember(temp2,[1 1],'rows'),1)=1;
  123. % Miss 2
  124. TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}...
  125. (ismember(temp2,[1 0],'rows'),1)=2;
  126. % find NoGo trial in stim trial
  127. temp2=[ismember(TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1},...
  128. NoGoCode{1,iBatch}(iTrainStage,:),'rows') ...
  129. IfLickResp{1,iBatch}{iSub,iTrainStage}{iSession,1}];
  130. % Correct Rejection in stim trial 3
  131. TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}...
  132. (ismember(temp2,[1 0],'rows'),1)=3;
  133. % False Alarm in stim trial 4
  134. TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}...
  135. (ismember(temp2,[1 1],'rows'),1)=4;
  136. % find Blank trial
  137. temp3=[TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1}(:,3)==720 ...
  138. IfLickResp{1,iBatch}{iSub,iTrainStage}{iSession,1}];
  139. % CR in blank trial 5
  140. TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}...
  141. (ismember(temp3,[1 0],'rows'),1)=5;
  142. % FA in blank trial 6
  143. TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}...
  144. (ismember(temp3,[1 1],'rows'),1)=6;
  145. % HIT rate
  146. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,1)=...
  147. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==1)/...
  148. (sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==1)+...
  149. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==2));
  150. % MISS rate
  151. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,2)=...
  152. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==2)/...
  153. (sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==1)+...
  154. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==2));
  155. % CR rate (Stim & Control trials)
  156. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,3)=...
  157. (sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==3)+...
  158. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==5))/...
  159. (sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==3)+...
  160. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==4)+...
  161. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==5)+...
  162. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==6));
  163. % FA rate
  164. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,4)=...
  165. (sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==4)+...
  166. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==6))/...
  167. (sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==3)+...
  168. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==4)+...
  169. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==5)+...
  170. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==6));
  171. % Blank CR rate
  172. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,5)=...
  173. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==5)/...
  174. (sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==5)+...
  175. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==6));
  176. % Blank FA rate
  177. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,6)=...
  178. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==6)/...
  179. (sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==5)+...
  180. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==6));
  181. % correcting dprime for HIT=1/0 or FA=1/0
  182. % 1/2N
  183. temp=1/(2*(sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==1)+...
  184. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==2)));
  185. temp1=1/(2*(sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==3)+...
  186. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==4)+...
  187. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==5)+...
  188. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==6)));
  189. if PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,1)==1
  190. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,1)=...
  191. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,1)-temp;
  192. end
  193. if PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,1)==0
  194. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,1)=...
  195. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,1)+temp;
  196. end
  197. if PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,4)==1
  198. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,4)=...
  199. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,4)-temp1;
  200. end
  201. if PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,4)==0
  202. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,4)=...
  203. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,4)+temp1;
  204. end
  205. [dprime{1,iBatch}{iSub,iTrainStage}{iSession,1},~]=...
  206. dprime_simple(PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,1),...
  207. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,4));
  208. clear temp*
  209. end
  210. toc
  211. end
  212. end
  213. end
  214. clear temp*
  215. %% plot all correlation matrices correlation matrix stim delay resp separate
  216. for iPeriod=1:2
  217. i=1;
  218. figure
  219. for iBatch=1:2
  220. for iSub=1:size(DataAll.(BatchName{iBatch}),1)
  221. for iTrainStage=1:nTrainStage
  222. for iSession=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}))
  223. % Go-NoGo-blank
  224. clear TrialTypeIdx
  225. % Go
  226. TrialTypeIdx(ismember(TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1},...
  227. GoCode{1,iBatch}(iTrainStage,:),'rows'),1)=1;
  228. % NoGo
  229. TrialTypeIdx(ismember(TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1},...
  230. NoGoCode{1,iBatch}(iTrainStage,:),'rows'),1)=2;
  231. % Blank
  232. TrialTypeIdx(TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1}(:,3)==720,1)=3;
  233. [~,TrialTypeInd]=sort(TrialTypeIdx);
  234. CorrBLInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=...
  235. StimBaseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(TrialTypeInd,1);
  236. CorrStimInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=...
  237. StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(TrialTypeInd,1);
  238. CorrDelayInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=...
  239. DelayOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(TrialTypeInd,1);
  240. CorrRespInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=...
  241. ResponseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(TrialTypeInd,1);
  242. CorrResponseWind=2;
  243. temp=ZLowCa{1,iBatch}{iSub,iTrainStage}{iSession,1};
  244. tempMat1=[];tempMat2=[];tempMat3=[];
  245. for iTrial=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}){iSession}.StimStart)
  246. f0=mean(temp(CorrBLInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  247. CorrStimInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:),1);
  248. %stim
  249. temp1=mean(temp(CorrStimInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  250. CorrStimInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial)+StimDur*SampleRate,:),1)-f0;
  251. %delay
  252. temp2=mean(temp(CorrDelayInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  253. CorrDelayInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial)+DelayDur*SampleRate,:),1)-f0;
  254. %resp
  255. temp3=mean(temp(CorrRespInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  256. CorrRespInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial)+CorrResponseWind*SampleRate,:),1)-f0;
  257. tempMat1=cat(1,tempMat1,temp1);
  258. tempMat2=cat(1,tempMat2,temp2);
  259. tempMat3=cat(1,tempMat3,temp3);
  260. end
  261. tempMat1=zscore(tempMat1')';
  262. tempMat2=zscore(tempMat2')';
  263. tempMat3=zscore(tempMat3')';
  264. CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,1}=...
  265. tempMat1*tempMat1'/(size(tempMat1,2)-1);%stim
  266. CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,2}=...
  267. tempMat2*tempMat2'/(size(tempMat2,2)-1);%delay
  268. CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,3}=...
  269. tempMat3*tempMat3'/(size(tempMat3,2)-1);%resp
  270. subplot(8,15,(i-1)*15+(iTrainStage-1)*4+iSession)
  271. % TimeAxis=[1:sum(TrialTypeIdx==1) 1:sum(TrialTypeIdx==2) 1:sum(TrialTypeIdx==3)];
  272. temp=sum(TrialTypeIdx==1)+sum(TrialTypeIdx==2);
  273. imagesc(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(...
  274. 1:temp,1:temp))
  275. % be careful of 1 more index number
  276. Lines(cumsum([sum(TrialTypeIdx==1)]),[],'k','-',2);
  277. Lines([],cumsum([sum(TrialTypeIdx==1)]),'k','-',2);
  278. % colorbar
  279. caxis([-.3 .6])
  280. xticks([1 temp])
  281. yticks([1 temp])
  282. colormap(bluewhitered(256))
  283. axis square
  284. % title([BatchName{iBatch},'-m',num2str(iSub),'-',StageName{iTrainStage},...
  285. % '-s',num2str(iSession),'d=',num2str(dprime{1,iBatch}{iSub,iTrainStage}{iSession,1})])
  286. clear temp*
  287. end
  288. end
  289. i=i+1;
  290. end
  291. end
  292. end
  293. suplabel(PeriodName{iPeriod},'x',[.5 .99 0 0]);%top x
  294. %% PV correlation Go/Nogo grouping
  295. for iPeriod=1:2% stim delay resp
  296. for iBatch=1:2
  297. for iSub=1:size(DataAll.(BatchName{iBatch}),1)
  298. for iTrainStage=1:nTrainStage
  299. for iSession=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}))
  300. clear TrialTypeIdx
  301. % Go
  302. TrialTypeIdx(ismember(TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1},...
  303. GoCode{1,iBatch}(iTrainStage,:),'rows'),1)=1;
  304. % NoGo
  305. TrialTypeIdx(ismember(TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1},...
  306. NoGoCode{1,iBatch}(iTrainStage,:),'rows'),1)=2;
  307. % Blank
  308. TrialTypeIdx(TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1}(:,3)==720,1)=3;
  309. temp1=1:sum(TrialTypeIdx==1);
  310. temp2=[1:sum(TrialTypeIdx==2)]+temp1(end);
  311. temp3=[1:sum(TrialTypeIdx==3)]+temp2(end);
  312. CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}...
  313. (logical(diag(ones(1,size(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod},1)),0)))=nan;
  314. % Go-Go corr
  315. DesignMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(1,1)=...
  316. nanmean(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp1,temp1),'all');
  317. DesignCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}{1,1}=...
  318. rmmissing(reshape(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp1,temp1),[],1));
  319. % DesignMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}{1,1}(1,2:3)=...
  320. % prctile(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp1,temp1),percentile,'all');
  321. % NoGo-NoGo corr
  322. DesignMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(2,2)=...
  323. nanmean(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp2,temp2),'all');
  324. DesignCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}{2,2}=...
  325. rmmissing(reshape(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp2,temp2),[],1));
  326. % DesignMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}{2,2}(1,2:3)=...
  327. % prctile(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp2,temp2),percentile,'all');
  328. % catch-catch corr
  329. DesignMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(3,3)=...
  330. nanmean(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp3,temp3),'all');
  331. DesignCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}{3,3}=...
  332. rmmissing(reshape(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp3,temp3),[],1));
  333. % DesignMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}{3,3}(1,2:3)=...
  334. % prctile(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp3,temp3),percentile,'all');
  335. % Go-NoGo corr
  336. DesignMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(2,1)=...
  337. nanmean(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp2,temp1),'all');
  338. DesignCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}{2,1}=...
  339. rmmissing(reshape(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp2,temp1),[],1));
  340. % DesignMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}{2,1}(1,2:3)=...
  341. % prctile(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp2,temp1),percentile,'all');
  342. % Go-catch corr
  343. DesignMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(3,1)=...
  344. nanmean(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp3,temp1),'all');
  345. DesignCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}{3,1}=...
  346. rmmissing(reshape(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp3,temp1),[],1));
  347. % DesignMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}{3,1}(1,2:3)=...
  348. % prctile(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp3,temp1),percentile,'all');
  349. % NoGo-catch corr
  350. DesignMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(3,2)=...
  351. nanmean(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp3,temp2),'all');
  352. DesignCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}{3,2}=...
  353. rmmissing(reshape(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp3,temp2),[],1));
  354. % DesignMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}{3,2}(1,2:3)=...
  355. % prctile(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp3,temp2),percentile,'all');
  356. clear temp*
  357. end
  358. end
  359. end
  360. end
  361. end
  362. clear tmep*
  363. for iPeriod=1:2%stim delay resp
  364. for iResult=1:2% Go-Go/NoGo-NoGo/catch-catch
  365. for iTrainStage=1:nTrainStage
  366. CatDesignCorr=[];CatDesignCorr1=[];
  367. for iBatch=1:2
  368. for iSub=1:size(DataAll.(BatchName{iBatch}),1)
  369. for iSession=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}))
  370. temp(iSession,1)=...
  371. DesignMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(iResult,iResult);
  372. end
  373. CatDesignCorr=cat(1,CatDesignCorr,mean(temp));
  374. CatDesignCorr1=cat(1,CatDesignCorr1,temp);
  375. clear temp*
  376. end
  377. end
  378. DesignCorrPerMouse(:,iTrainStage,iPeriod,iResult)=CatDesignCorr;
  379. DesignCorrPerMouseSessions{iTrainStage,iPeriod,iResult}=CatDesignCorr1;
  380. end
  381. end
  382. end
  383. % change iTrainStage to 1:2 for comparing Initial learning stage or 3:4 for
  384. % reversal learning stage
  385. % plot individual session in each training phase
  386. clear Cat* label
  387. ResultName={'Go','NoGo'};
  388. label=[];CatDesignCorrPerMouseSession=[];
  389. for iPeriod=1:2%stim delay resp
  390. for iResult=1:2% Go-Go/NoGo-NoGo/catch-catch
  391. for iTrainStage=3:4% change here
  392. CatDesignCorrPerMouseSession=cat(1,CatDesignCorrPerMouseSession,...
  393. DesignCorrPerMouseSessions{iTrainStage,iPeriod,iResult});
  394. label=cat(1,label,string(repmat([StageName{iTrainStage},'-',PeriodName{iPeriod},'-',ResultName{iResult}],...
  395. length(DesignCorrPerMouseSessions{iTrainStage,iPeriod,iResult}),1)));
  396. end
  397. end
  398. end
  399. figure
  400. [~,ViolinOutput]=violinplot(CatDesignCorrPerMouseSession,label,'ViolinColor',...
  401. repmat([hex2rgb('#D35400');hex2rgb('#2980B9')],4,1));
  402. xtickangle(45)
  403. % plot Aud or Vis Go first group
  404. close all
  405. BatchGroup=[12 12 11 11;13 13 12 12];% Aud Vis train sequence and bumber of session
  406. figure
  407. hold on
  408. bar(cellfun(@mean,ViolinOutput(:,1)),'w','BarWidth',.5,'LineWidth',2);
  409. errorbar([1:8],cellfun(@mean,ViolinOutput(:,1)),sem(ViolinOutput(:,1),0),'Color',[0 0 0],'LineStyle','none','LineWidth',2,'CapSize',10)
  410. for i=1:8
  411. switch ViolinOutput{i,2}(1:4)
  412. case 'Naiv'
  413. scatter(ones(length(ViolinOutput{i,1}(1:BatchGroup(1,1))),1)*i,...
  414. ViolinOutput{i,1}(1:BatchGroup(1,1)),'r','filled','jitter','on','jitterAmount',0.2);
  415. scatter(ones(length(ViolinOutput{i,1}(BatchGroup(2,1):end)),1)*i,...
  416. ViolinOutput{i,1}(BatchGroup(2,1):end),'g','filled','jitter','on','jitterAmount',0.2);
  417. case 'Exp-'
  418. scatter(ones(length(ViolinOutput{i,1}(1:BatchGroup(1,2))),1)*i,...
  419. ViolinOutput{i,1}(1:BatchGroup(1,2)),'r','filled','jitter','on','jitterAmount',0.2);
  420. scatter(ones(length(ViolinOutput{i,1}(BatchGroup(2,2):end)),1)*i,...
  421. ViolinOutput{i,1}(BatchGroup(2,2):end),'g','filled','jitter','on','jitterAmount',0.2);
  422. case 'RevN'
  423. scatter(ones(length(ViolinOutput{i,1}(1:BatchGroup(1,3))),1)*i,...
  424. ViolinOutput{i,1}(1:BatchGroup(1,3)),'r','filled','jitter','on','jitterAmount',0.2);
  425. scatter(ones(length(ViolinOutput{i,1}(BatchGroup(2,3):end)),1)*i,...
  426. ViolinOutput{i,1}(BatchGroup(2,3):end),'g','filled','jitter','on','jitterAmount',0.2);
  427. case 'RevE'
  428. scatter(ones(length(ViolinOutput{i,1}(1:BatchGroup(1,4))),1)*i,...
  429. ViolinOutput{i,1}(1:BatchGroup(1,4)),'r','filled','jitter','on','jitterAmount',0.2);
  430. scatter(ones(length(ViolinOutput{i,1}(BatchGroup(2,4):end)),1)*i,...
  431. ViolinOutput{i,1}(BatchGroup(2,4):end),'g','filled','jitter','on','jitterAmount',0.2);
  432. end
  433. end
  434. temp=gcf;
  435. temp.Parent.Children.CurrentAxes.XTick=[1:8];
  436. temp.Parent.Children.CurrentAxes.XTickLabel=ViolinOutput(:,2);
  437. ylim([0 .5])
  438. temp.Parent.Children.CurrentAxes.YTick=0:.1:.5;
  439. xtickangle(45)
  440. %% statistics
  441. clear TableDesignMeanCorrSession
  442. TableDesignMeanCorrSession=table('Size',[0 5],'VariableTypes',{'double','string','string','string','string'},...
  443. 'VariableNames',{'R','stage','period','design','ID'});
  444. % TableDesignCorrTrial=table('Size',[0 5],'VariableTypes',{'double','string','string','string','string'},...
  445. % 'VariableNames',{'R','stage','period','design','ID'});
  446. tempMouseID{1,1}=1:4;tempMouseID{1,2}=5:8;
  447. ResultName={'Go','NoGo'};
  448. % change iPeriod iResult iTrainStage for subgroup statistical analysis
  449. i=1;
  450. for iPeriod=2%stim delay resp
  451. for iResult=1% Go-Go/NoGo-NoGo/catch-catch
  452. for iTrainStage=1:2
  453. for iBatch=1:2
  454. for iSub=1:size(DataAll.(BatchName{iBatch}),1)
  455. for iSession=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}))
  456. % within session mean (2-3 sessions) as observation
  457. TableDesignMeanCorrSession(i,:)={...
  458. DesignMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(iResult,iResult)...
  459. ,StageName{iTrainStage},PeriodName{iPeriod},ResultName{iResult},...
  460. string(tempMouseID{1,iBatch}(iSub))};
  461. i=i+1;
  462. end
  463. end
  464. end
  465. end
  466. end
  467. end
  468. LMM=fitlme(TableDesignMeanCorrSession(:,[1 2 5]),'R~stage+(1|ID)')
  469. %% sort trial result Hit/FA
  470. %% sort trial result Hit/FA
  471. for iPeriod=1:2
  472. i=1;
  473. figure
  474. for iBatch=1:2
  475. for iSub=1:size(DataAll.(BatchName{iBatch}),1)
  476. for iTrainStage=1:nTrainStage%FWD REV
  477. for iSession=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}))
  478. temp=ZLowCa{1,iBatch}{iSub,iTrainStage}{iSession,1};
  479. tempMat1=[];tempMat2=[];tempMat3=[];
  480. for iTrial=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}){iSession}.StimStart)
  481. f0=mean(temp(StimBaseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  482. StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:),1);
  483. %stim
  484. temp1=mean(temp(StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  485. StimOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:))-f0;
  486. %delay
  487. temp2=mean(temp(DelayOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  488. DelayOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:))-f0;
  489. %resp
  490. temp3=mean(temp(ResponseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  491. ResponseOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:))-f0;
  492. tempMat1=cat(1,tempMat1,temp1);
  493. tempMat2=cat(1,tempMat2,temp2);
  494. tempMat3=cat(1,tempMat3,temp3);
  495. end
  496. % [Hit; FA]
  497. TrialResultIdx{1,1}=find(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==1);
  498. TrialResultIdx{1,2}=find(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==4);
  499. tempMat1=zscore([tempMat1(TrialResultIdx{1,1},:);tempMat1(TrialResultIdx{1,2},:)]')';
  500. tempMat2=zscore([tempMat2(TrialResultIdx{1,1},:);tempMat2(TrialResultIdx{1,2},:)]')';
  501. tempMat3=zscore([tempMat3(TrialResultIdx{1,1},:);tempMat3(TrialResultIdx{1,2},:)]')';
  502. CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,1}=...
  503. tempMat1*tempMat1'/(size(tempMat1,2)-1);%stim
  504. CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,2}=...
  505. tempMat2*tempMat2'/(size(tempMat2,2)-1);%delay
  506. CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,3}=...
  507. tempMat3*tempMat3'/(size(tempMat3,2)-1);%resp
  508. subplot(8,15,(i-1)*15+(iTrainStage-1)*4+iSession)
  509. imagesc(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod})
  510. % be careful of 1 more index number
  511. Lines(length(TrialResultIdx{1,1}),[],'k','',1.5);
  512. Lines([],length(TrialResultIdx{1,1}),'k','',1.5);
  513. axis square
  514. caxis([-.3 .6])
  515. xticks([1 sum(cellfun(@length,TrialResultIdx))])
  516. yticks([1 sum(cellfun(@length,TrialResultIdx))])
  517. colormap(bluewhitered(256))
  518. end
  519. end
  520. i=i+1;
  521. end
  522. end
  523. colorbar
  524. end
  525. % summary data
  526. TrialNumThr=6;
  527. for iBatch=1:2
  528. for iSub=1:size(DataAll.(BatchName{iBatch}),1)
  529. for iTrainStage=1:nTrainStage%FWD REV
  530. for iSession=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}))
  531. clear temp*
  532. temp=ZLowCa{1,iBatch}{iSub,iTrainStage}{iSession,1};
  533. tempMat1=[];tempMat2=[];tempMat3=[];
  534. for iTrial=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}){iSession}.StimStart)
  535. f0=mean(temp(StimBaseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  536. StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:),1);
  537. %stim
  538. temp1=mean(temp(StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  539. StimOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:))-f0;
  540. %delay
  541. temp2=mean(temp(DelayOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  542. DelayOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:))-f0;
  543. %resp
  544. temp3=mean(temp(ResponseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  545. ResponseOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:))-f0;
  546. tempMat1=cat(1,tempMat1,temp1);
  547. tempMat2=cat(1,tempMat2,temp2);
  548. tempMat3=cat(1,tempMat3,temp3);
  549. end
  550. % [Hit;FA]%CR
  551. TrialResultIdx{1,1}=find(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==1);
  552. TrialResultIdx{1,2}=find(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==4);
  553. % TrialResultIdx{1,3}=find(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==3);
  554. tempMat1=zscore([tempMat1(TrialResultIdx{1,1},:);tempMat1(TrialResultIdx{1,2},:)]')';%;tempMat1(TrialResultIdx{1,3},:)
  555. tempMat2=zscore([tempMat2(TrialResultIdx{1,1},:);tempMat2(TrialResultIdx{1,2},:)]')';%;tempMat2(TrialResultIdx{1,3},:)
  556. tempMat3=zscore([tempMat3(TrialResultIdx{1,1},:);tempMat3(TrialResultIdx{1,2},:)]')';%;tempMat3(TrialResultIdx{1,3},:)
  557. CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,1}=...
  558. tempMat1*tempMat1'/(size(tempMat1,2)-1);%stim
  559. CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,2}=...
  560. tempMat2*tempMat2'/(size(tempMat2,2)-1);%delay
  561. CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,3}=...
  562. tempMat3*tempMat3'/(size(tempMat3,2)-1);%resp
  563. ResultInd=cumsum([length(TrialResultIdx{1,1}) length(TrialResultIdx{1,2})]);% length(TrialResultIdx{1,2})
  564. temp1=[1:ResultInd(1)];
  565. temp2=[ResultInd(1):ResultInd(2)];
  566. for iPeriod=1:3 %stim delay resp
  567. % Hit corr
  568. ResultMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(1,1)=...
  569. mean(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp1,temp1),'all');
  570. % FA corr
  571. ResultMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(2,2)=...
  572. mean(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp2,temp2),'all');
  573. % CR corr
  574. % ResultMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(3,3)=...
  575. % mean(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp3,temp3),'all');
  576. % Avoid bias of small num of trial (<TrialNumThr will be discard)
  577. % If there is no trial being in Hit/FA/CR, diagonal "1" will be selected
  578. if length(temp1)<TrialNumThr
  579. ResultMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(1,1)=nan;
  580. end
  581. if length(temp2)<TrialNumThr
  582. ResultMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(2,2)=nan;
  583. end
  584. % if length(temp3)<TrialNumThr
  585. % ResultMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(3,3)=1;
  586. % end
  587. end
  588. end
  589. end
  590. end
  591. end
  592. clear Cat* temp* ResultCorrPerMouse ResultCorrPerMouseSessions
  593. for iPeriod=1:2%stim delay resp
  594. for iResult=1:2% Hit/FA
  595. for iTrainStage=1:nTrainStage
  596. CatResultCorr=[];CatResultCorr1=[];
  597. for iBatch=1:2
  598. for iSub=1:size(DataAll.(BatchName{iBatch}),1)
  599. i=1;
  600. for iSession=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}))
  601. if ~isnan(ResultMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(iResult,iResult))
  602. temp(i,1)=ResultMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(iResult,iResult);
  603. i=i+1;
  604. end
  605. end
  606. CatResultCorr=cat(1,CatResultCorr,nanmean(temp));
  607. CatResultCorr1=cat(1,CatResultCorr1,temp);
  608. clear temp
  609. end
  610. end
  611. ResultCorrPerMouse(:,iTrainStage,iPeriod,iResult)=CatResultCorr;
  612. ResultCorrPerMouseSessions{iTrainStage,iPeriod,iResult}=CatResultCorr1;
  613. end
  614. end
  615. end
  616. % statistics for individual session data in suppl.
  617. % change iTrainStage to plot Initial and Reversal training stage
  618. clear Cat* label
  619. ResultName={'Hit','FA'};
  620. label=[];CatResultCorrPerMouseSession=[];
  621. for iPeriod=1:2%stim delay resp
  622. for iResult=1:2% Hit-Hit/FA-FA
  623. for iTrainStage=1:2
  624. CatResultCorrPerMouseSession=cat(1,CatResultCorrPerMouseSession,...
  625. ResultCorrPerMouseSessions{iTrainStage,iPeriod,iResult});
  626. label=cat(1,label,string(repmat([StageName{iTrainStage},'-',PeriodName{iPeriod},'-',ResultName{iResult}],...
  627. length(ResultCorrPerMouseSessions{iTrainStage,iPeriod,iResult}),1)));
  628. end
  629. end
  630. end
  631. close all
  632. figure
  633. hold on
  634. bar(cellfun(@mean,ViolinOutput(:,1)),'w','BarWidth',.5,'LineWidth',2);
  635. errorbar([1:8],cellfun(@mean,ViolinOutput(:,1)),sem(ViolinOutput(:,1),0),'Color',[0 0 0],'LineStyle','none','LineWidth',2,'CapSize',0)
  636. for i=1:8
  637. scatter(ones(length(ViolinOutput{i,1}),1)*i,...
  638. ViolinOutput{i,1},'k','filled','jitter','on','jitterAmount',0.2);
  639. end
  640. temp=gcf;
  641. temp.Parent.Children.CurrentAxes.XTick=[1:8];
  642. temp.Parent.Children.CurrentAxes.XTickLabel=ViolinOutput(:,2);
  643. xtickangle(45)
  644. ylim([0 .5])
  645. temp.Parent.Children.CurrentAxes.YTick=0:.1:.5;
  646. clear TableResultMeanCorrSession
  647. TableResultMeanCorrSession=table('Size',[0 5],'VariableTypes',{'double','string','string','string','string'},...
  648. 'VariableNames',{'R','stage','period','design','ID'});
  649. tempMouseID{1,1}=1:4;tempMouseID{1,2}=5:8;
  650. ResultName={'Hit','FA'};
  651. % change iPeriod iResult iTrainStage for subgroup statistical analysis
  652. i=1;
  653. for iPeriod=2%stim delay resp
  654. for iResult=2% Hit-Hit/FA-FA
  655. for iTrainStage=1:2
  656. for iBatch=1:2
  657. for iSub=1:size(DataAll.(BatchName{iBatch}),1)
  658. for iSession=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}))
  659. % within session mean (2-3 sessions) as observation
  660. temp=ResultMeanCorr{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(iResult,iResult);
  661. if ~isnan(temp)
  662. TableResultMeanCorrSession(i,:)={...
  663. temp,StageName{iTrainStage},PeriodName{iPeriod},ResultName{iResult},...
  664. string(tempMouseID{1,iBatch}(iSub))};
  665. i=i+1;
  666. end
  667. end
  668. end
  669. end
  670. end
  671. end
  672. end
  673. LMM=fitlme(TableResultMeanCorrSession(:,[1 2 5]),'R~stage+(1|ID)')
  674. %% pool correlation for Hit trial for Lick +/-
  675. clear label* Cat* CorrMat
  676. HitLickMeanCorr=[];HitNoLickMeanCorr=[];labelLick=[];labelNoLick=[];
  677. TrialNumThr=6;
  678. figure
  679. i=1;
  680. for iBatch=1:2
  681. for iSub=1:size(DataAll.(BatchName{iBatch}),1)
  682. for iTrainStage=[1 2]%Exp Rev-Exp
  683. for iSession=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}))
  684. clear temp*
  685. temp=ZLowCa{1,iBatch}{iSub,iTrainStage}{iSession,1};
  686. tempMat1=[];tempMat2=[];tempMat3=[];
  687. for iTrial=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}){iSession}.StimStart)
  688. f0=mean(temp(StimBaseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  689. StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:),1);
  690. %stim
  691. temp1=mean(temp(StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  692. StimOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:))-f0;
  693. %delay
  694. temp2=mean(temp(DelayOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  695. DelayOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:))-f0;
  696. %resp
  697. temp3=mean(temp(ResponseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  698. ResponseOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:))-f0;
  699. tempMat1=cat(1,tempMat1,temp1);
  700. tempMat2=cat(1,tempMat2,temp2);
  701. tempMat3=cat(1,tempMat3,temp3);
  702. end
  703. % Hit
  704. temp1=[TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1} ...
  705. IfLickDelayDLC{1,iBatch}{iSub,iTrainStage}{iSession,1}];
  706. tempLickIndHit=find(ismember(temp1,[1 1],'rows'));
  707. tempNoLickIndHit=find(ismember(temp1,[1 0],'rows'));
  708. % include session with Lick and Nolick >6 trials
  709. if (length(tempLickIndHit)>TrialNumThr)&(length(tempNoLickIndHit)>TrialNumThr)
  710. % if true
  711. tempMat1=zscore([tempMat1(tempLickIndHit,:);tempMat1(tempNoLickIndHit,:)]')';
  712. tempMat2=zscore([tempMat2(tempLickIndHit,:);tempMat2(tempNoLickIndHit,:)]')';
  713. tempMat3=zscore([tempMat3(tempLickIndHit,:);tempMat3(tempNoLickIndHit,:)]')';
  714. CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,1}=...
  715. tempMat1*tempMat1'/(size(tempMat1,2)-1);%stim
  716. CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,2}=...
  717. tempMat2*tempMat2'/(size(tempMat2,2)-1);%delay
  718. CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,3}=...
  719. tempMat3*tempMat3'/(size(tempMat3,2)-1);%resp
  720. temp1=[1:length(tempLickIndHit)];
  721. temp2=[length(tempLickIndHit)+1:(length(tempNoLickIndHit)+length(tempLickIndHit))];
  722. for iPeriod=2 %stim delay resp
  723. % HitLick corr
  724. HitLickMeanCorr=cat(1,HitLickMeanCorr,...
  725. mean(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp1,temp1),'all'));
  726. % HitNoLick corr
  727. HitNoLickMeanCorr=cat(1,HitNoLickMeanCorr,...
  728. mean(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(temp2,temp2),'all'));
  729. labelLick=cat(1,labelLick,cellstr([StageName{iTrainStage}]));
  730. labelNoLick=cat(1,labelNoLick,cellstr([StageName{iTrainStage}]));
  731. end
  732. end
  733. % subplot(8,15,(i-1)*15+(iTrainStage-1)*4+iSession)
  734. % imagesc(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod})
  735. % Lines(length(tempLickIndHit)+.5,[],'k','',1.5);
  736. % Lines([],length(tempLickIndHit)+.5,'k','',1.5);
  737. % axis square
  738. % caxis([-.3 .6])
  739. % xticks([1 size(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod},1)])
  740. % yticks([1 size(CorrMat{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod},1)])
  741. % colormap(bluewhitered(256))
  742. end
  743. end
  744. i=i+1;
  745. end
  746. end
  747. g1=[labelLick;labelNoLick];
  748. g2=[repmat(cellstr('Lick'),length(labelLick),1);repmat(cellstr('NoLick'),length(labelNoLick),1)];
  749. anovan([HitLickMeanCorr;HitNoLickMeanCorr],{g1,g2})
  750. figure
  751. violinplot([HitLickMeanCorr;HitNoLickMeanCorr],append(g1,'-',g2));
  752. tempCorr=[HitLickMeanCorr;HitNoLickMeanCorr];
  753. [~,~,UniIdx]=unique(append(g1,'-',g2));
  754. [ranksum(tempCorr(UniIdx==2),tempCorr(UniIdx==1)),ranksum(tempCorr(UniIdx==4),tempCorr(UniIdx==3))]
  755. % Expert 2-nolick 1-lick
  756. [~,~,stats]=ranksum(tempCorr(UniIdx==2),tempCorr(UniIdx==1))
  757. % Naive 4-nolick 3-lick
  758. [~,~,stats]=ranksum(tempCorr(UniIdx==4),tempCorr(UniIdx==3))
  759. PlotValue={tempCorr(UniIdx==2),tempCorr(UniIdx==1),tempCorr(UniIdx==4),tempCorr(UniIdx==3)};
  760. PlotLabel={'Exp-NoLick','Exp-Lick','Naive-NoLick','Naive-Lick'};
  761. close all
  762. figure
  763. hold on
  764. bar(cellfun(@mean,PlotValue),'w','BarWidth',.5,'LineWidth',2);
  765. errorbar([1:4],cellfun(@mean,PlotValue),sem(PlotValue,0),'LineStyle','none','LineWidth',2,'CapSize',10)
  766. for i=[1 3]
  767. scatter(ones(length(PlotValue{1,i}),1)*i,PlotValue{1,i},'k','filled','jitter','on','jitterAmount',0.2);
  768. end
  769. for i=[2 4]
  770. scatter(ones(length(PlotValue{1,i}),1)*i,PlotValue{1,i},'k','filled','LineWidth',2,'jitter','on','jitterAmount',0.2);
  771. end
  772. temp=gcf;
  773. temp.Parent.Children.CurrentAxes.XTick=[1:4];
  774. temp.Parent.Children.CurrentAxes.XTickLabel=PlotLabel;
  775. ylim([0 .5])
  776. xtickangle(45)