Fig4.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. addpath(genpath('...\auxillary'));
  2. load('...\network\DataAll.mat')
  3. load('...\network\ZLowCa.mat')
  4. SampleRate=30;
  5. StimDur=2;%second
  6. DelayDur=2;
  7. ResponseWind=2;
  8. NormBaseline=1;%second before stimOn 1s
  9. AfterStimDur=2;%second after stimOff 2s
  10. StageName={'Naive','Exp','RevNaive','RevExp'};
  11. nTrainStage=length(StageName);% 'Naive','Exp','RevNaive','RevExp'
  12. BatchName={'Aud1','Vis1'};
  13. PeriodName={'Stim','Delay','Resp'};
  14. if nTrainStage==4
  15. TaskCode{1,1}=[...
  16. 1 1 12720;%Aud-Go 1
  17. 1 1 180; %Vis-Nogo 2
  18. 1 2 12720;%Aud-Go 3
  19. 1 2 180; %Vis-Nogo 4
  20. 2 3 12720;%Aud-Nogo 5
  21. 2 3 180; %Vis-Go 6
  22. 2 4 12720;%Aud-Nogo 7
  23. 2 4 180; %Vis-Go 8
  24. ];
  25. % batch 2
  26. TaskCode{1,2}=[...
  27. 2 1 12720;%Aud-NoGo 1
  28. 2 1 180; %Vis-Go 2
  29. 2 2 12720;%Aud-NoGo 3
  30. 2 2 180; %Vis-Go 4
  31. 1 3 12720;%Aud-Go 5
  32. 1 3 180; %Vis-NoGo 6
  33. 1 4 12720;%Aud-Go 7
  34. 1 4 180; %Vis-NoGo 8
  35. ];
  36. GoCode{1,1}=TaskCode{1,1}([1 3 6 8],:); %1
  37. GoCode{1,2}=TaskCode{1,2}([2 4 5 7],:); %2
  38. NoGoCode{1,1}=TaskCode{1,1}([2 4 5 7],:); %3
  39. NoGoCode{1,2}=TaskCode{1,2}([1 3 6 8],:); %4
  40. end
  41. for iBatch=1:2
  42. for iSub=1:size(DataAll.(BatchName{iBatch}),1)
  43. for iTrainStage=1:nTrainStage%FWD REV
  44. tic
  45. for iSession=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}))
  46. clear temp*
  47. temp=DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}){iSession};
  48. % pad nan before DLC vector to align rasberry pi cam
  49. tempLick=cat(1,nan(floor(abs(temp.CamStamp(1)-temp.RecStart)*SampleRate),1),temp.DLC_Lick);
  50. % find 2 consecutive frames with tongue deteected
  51. [~,LickIndDLC{1,iBatch}{iSub,iTrainStage}{iSession,1}]=...
  52. findpeaks(tempLick,'MinPeakHeight',.5,'MinPeakWidth',2);
  53. % time vector is different in ScanStamp and CamStamp in rasberry pi cam
  54. t{1,iBatch}{iSub,iTrainStage}{iSession,1}=temp.ScanStamp;
  55. tCam{1,iBatch}{iSub,iTrainStage}{iSession,1}=temp.CamStamp;
  56. StimOnT{1,iBatch}{iSub,iTrainStage}{iSession,1}=temp.StimStart;
  57. RewardOnT{1,iBatch}{iSub,iTrainStage}{iSession,1}=temp.RewardOn;
  58. StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=dsearchn(t{1,iBatch}{iSub,iTrainStage}{iSession,1},StimOnT{1,iBatch}{iSub,iTrainStage}{iSession,1});
  59. StimOnIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}=dsearchn(tCam{1,iBatch}{iSub,iTrainStage}{iSession,1},StimOnT{1,iBatch}{iSub,iTrainStage}{iSession,1});
  60. StimOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}+StimDur*SampleRate;
  61. StimOffIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}=StimOnIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}+StimDur*SampleRate;
  62. DelayOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=StimOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1};
  63. DelayOnIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}=StimOffIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1};
  64. DelayOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=DelayOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}+DelayDur*SampleRate;
  65. DelayOffIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}=DelayOnIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}+DelayDur*SampleRate;
  66. ResponseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=DelayOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1};
  67. ResponseOnIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}=DelayOffIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1};
  68. ResponseOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=ResponseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}+ResponseWind*SampleRate;
  69. ResponseOffIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}=ResponseOnIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}+ResponseWind*SampleRate;
  70. StimBaseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}-NormBaseline*SampleRate;
  71. RewardOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=dsearchn(t{1,iBatch}{iSub,iTrainStage}{iSession,1},RewardOnT{1,iBatch}{iSub,iTrainStage}{iSession,1});
  72. % electrical lick detector
  73. LickIndResp{1,iBatch}{iSub,iTrainStage}{iSession,1}=unique(dsearchn(t{1,iBatch}{iSub,iTrainStage}{iSession,1},temp.LickPulse));
  74. temp1=[ResponseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1} ResponseOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}];
  75. for iTrial=1:size(temp1,1)
  76. if any(LickIndResp{1,iBatch}{iSub,iTrainStage}{iSession,1}>=temp1(iTrial,1)&...
  77. LickIndResp{1,iBatch}{iSub,iTrainStage}{iSession,1}<=temp1(iTrial,2))
  78. IfLickResp{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial,1)=true;
  79. else
  80. IfLickResp{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial,1)=false;
  81. end
  82. end
  83. % DLC lick detector
  84. temp1=[DelayOnIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1} DelayOffIndCam{1,iBatch}{iSub,iTrainStage}{iSession,1}];
  85. for iTrial=1:size(temp1,1)
  86. if any(LickIndDLC{1,iBatch}{iSub,iTrainStage}{iSession,1}>=temp1(iTrial,1)&...
  87. LickIndDLC{1,iBatch}{iSub,iTrainStage}{iSession,1}<=temp1(iTrial,2))
  88. IfLickDelayDLC{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial,1)=true;
  89. else
  90. IfLickDelayDLC{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial,1)=false;
  91. end
  92. end
  93. tempIndHit=zeros(size(ZLowCa{1,iBatch}{iSub,iTrainStage}{iSession,1},1),1);
  94. for iTrial=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}){iSession}.StimStart)
  95. tempIndHit(StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  96. ResponseOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),1)=1;
  97. end
  98. ITIindex{1,iBatch}{iSub,iTrainStage}{iSession,1}=find(tempIndHit==0);
  99. % If TaskType == 1, that means that Aud-Go and Vis-Nogo.
  100. % If TaskType == 2, that means that Vis-Go and Aud-Nogo.
  101. StimTypeTemp{1,iBatch}{iSub,iTrainStage}{iSession,1}=...
  102. temp.Trial_Freq+temp.Trial_Grat;
  103. % AudTrial%12720 aud
  104. % VisTrial%180: vis
  105. % BlankTrial%720: vis&aud off blank
  106. for iTrial=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}){iSession}.StimStart)
  107. TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial,1)=...
  108. temp.TaskType;%TaskType
  109. TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial,2)=...
  110. iTrainStage;%TrainStage
  111. TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial,3)=...
  112. StimTypeTemp{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial);
  113. end
  114. % find Go trial in stim trial
  115. temp2=[ismember(TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1},...
  116. GoCode{1,iBatch}(iTrainStage,:),'rows') ...
  117. IfLickResp{1,iBatch}{iSub,iTrainStage}{iSession,1}];
  118. % Hit 1
  119. TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}...
  120. (ismember(temp2,[1 1],'rows'),1)=1;
  121. % Miss 2
  122. TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}...
  123. (ismember(temp2,[1 0],'rows'),1)=2;
  124. % find NoGo trial in stim trial
  125. temp2=[ismember(TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1},...
  126. NoGoCode{1,iBatch}(iTrainStage,:),'rows') ...
  127. IfLickResp{1,iBatch}{iSub,iTrainStage}{iSession,1}];
  128. % Correct Rejection in stim trial 3
  129. TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}...
  130. (ismember(temp2,[1 0],'rows'),1)=3;
  131. % False Alarm in stim trial 4
  132. TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}...
  133. (ismember(temp2,[1 1],'rows'),1)=4;
  134. % find Blank trial
  135. temp3=[TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1}(:,3)==720 ...
  136. IfLickResp{1,iBatch}{iSub,iTrainStage}{iSession,1}];
  137. % CR in blank trial 5
  138. TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}...
  139. (ismember(temp3,[1 0],'rows'),1)=5;
  140. % FA in blank trial 6
  141. TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}...
  142. (ismember(temp3,[1 1],'rows'),1)=6;
  143. % HIT rate
  144. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,1)=...
  145. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==1)/...
  146. (sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==1)+...
  147. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==2));
  148. % MISS rate
  149. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,2)=...
  150. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==2)/...
  151. (sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==1)+...
  152. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==2));
  153. % CR rate (Stim & Control trials)
  154. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,3)=...
  155. (sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==3)+...
  156. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==5))/...
  157. (sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==3)+...
  158. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==4)+...
  159. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==5)+...
  160. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==6));
  161. % FA rate
  162. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,4)=...
  163. (sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==4)+...
  164. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==6))/...
  165. (sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==3)+...
  166. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==4)+...
  167. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==5)+...
  168. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==6));
  169. % Blank CR rate
  170. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,5)=...
  171. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==5)/...
  172. (sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==5)+...
  173. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==6));
  174. % Blank FA rate
  175. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,6)=...
  176. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==6)/...
  177. (sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==5)+...
  178. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==6));
  179. % correcting dprime for HIT=1/0 or FA=1/0
  180. % 1/2N
  181. temp=1/(2*(sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==1)+...
  182. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==2)));
  183. temp1=1/(2*(sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==3)+...
  184. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==4)+...
  185. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==5)+...
  186. sum(TrialResult{1,iBatch}{iSub,iTrainStage}{iSession,1}==6)));
  187. if PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,1)==1
  188. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,1)=...
  189. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,1)-temp;
  190. end
  191. if PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,1)==0
  192. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,1)=...
  193. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,1)+temp;
  194. end
  195. if PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,4)==1
  196. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,4)=...
  197. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,4)-temp1;
  198. end
  199. if PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,4)==0
  200. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,4)=...
  201. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,4)+temp1;
  202. end
  203. [dprime{1,iBatch}{iSub,iTrainStage}{iSession,1},~]=...
  204. dprime_simple(PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,1),...
  205. PerformanceAll{1,iBatch}{iSub,iTrainStage}{iSession,1}(1,4));
  206. clear temp*
  207. end
  208. toc
  209. end
  210. end
  211. end
  212. clear temp*
  213. %% adjacency matrix (concatenated trials)
  214. clear AdjMatAll temp*
  215. for iBatch=1:2
  216. for iSub=1:size(DataAll.(BatchName{iBatch}),1)
  217. for iTrainStage=1:nTrainStage% INI EXP REV
  218. for iSession=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}))
  219. % Go
  220. TrialTypeIdxTemp(ismember(TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1},...
  221. GoCode{1,iBatch}(iTrainStage,:),'rows'),1)=1;
  222. % NoGo
  223. TrialTypeIdxTemp(ismember(TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1},...
  224. NoGoCode{1,iBatch}(iTrainStage,:),'rows'),1)=2;
  225. % Blank
  226. TrialTypeIdxTemp(TrialType{1,iBatch}{iSub,iTrainStage}{iSession,1}(:,3)==720,1)=3;
  227. [~,TrialTypeIndTemp]=sort(TrialTypeIdxTemp);
  228. % temp=zscore(RawSpk{1,iBatch}{iSub,iTrainStage}{iSession,1});
  229. temp=ZLowCa{1,iBatch}{iSub,iTrainStage}{iSession,1};
  230. for iTrial=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}){iSession}.StimStart)
  231. tempBL=mean(temp(StimBaseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  232. StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:),1);
  233. tempStim(:,:,iTrial)=temp(StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  234. StimOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:)-tempBL;
  235. tempDelay(:,:,iTrial)=temp(DelayOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  236. DelayOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:)-tempBL;
  237. tempResp(:,:,iTrial)=temp(ResponseOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  238. ResponseOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:)-tempBL;
  239. tempStimDelay(:,:,iTrial)=temp(StimOnInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial):...
  240. DelayOffInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(iTrial),:)-tempBL;
  241. end
  242. TrialTypeInd{1,iBatch}{iSub,iTrainStage}{iSession,1}=1:sum(TrialTypeIdxTemp==1);
  243. TrialTypeInd{1,iBatch}{iSub,iTrainStage}{iSession,2}=[1:sum(TrialTypeIdxTemp==2)]+...
  244. TrialTypeInd{1,iBatch}{iSub,iTrainStage}{iSession,1}(end);
  245. TrialTypeInd{1,iBatch}{iSub,iTrainStage}{iSession,3}=[1:sum(TrialTypeIdxTemp==3)]+...
  246. TrialTypeInd{1,iBatch}{iSub,iTrainStage}{iSession,2}(end);
  247. % concatenate all trials time series
  248. for iType=1:3%Go/NoGo/catch
  249. tempStim1=tempStim(:,:,TrialTypeIndTemp(...
  250. TrialTypeInd{1,iBatch}{iSub,iTrainStage}{iSession,iType}));
  251. tempDelay1=tempDelay(:,:,TrialTypeIndTemp(...
  252. TrialTypeInd{1,iBatch}{iSub,iTrainStage}{iSession,iType}));
  253. tempResp1=tempResp(:,:,TrialTypeIndTemp(...
  254. TrialTypeInd{1,iBatch}{iSub,iTrainStage}{iSession,iType}));
  255. tempStimDelay1=tempStimDelay(:,:,TrialTypeIndTemp(...
  256. TrialTypeInd{1,iBatch}{iSub,iTrainStage}{iSession,iType}));
  257. tempStim1=zscore(reshape(permute(tempStim1,[1 3 2]),size(tempStim1,1)*size(tempStim1,3),size(tempStim1,2)));
  258. tempDelay1=zscore(reshape(permute(tempDelay1,[1 3 2]),size(tempDelay1,1)*size(tempDelay1,3),size(tempDelay1,2)));
  259. tempResp1=zscore(reshape(permute(tempResp1,[1 3 2]),size(tempResp1,1)*size(tempResp1,3),size(tempResp1,2)));
  260. tempStimDelay1=zscore(reshape(permute(tempStimDelay1,[1 3 2]),size(tempStimDelay1,1)*size(tempStimDelay1,3),size(tempStimDelay1,2)));
  261. tempN=size(tempStim1,1)-1;
  262. AdjMatStim(:,:,iType)=tempStim1'*tempStim1/tempN;
  263. AdjMatDelay(:,:,iType)=tempDelay1'*tempDelay1/tempN;
  264. AdjMatResp(:,:,iType)=tempResp1'*tempResp1/tempN;
  265. AdjMatStimDelay(:,:,iType)=tempStimDelay1'*tempStimDelay1/tempN;
  266. end
  267. AdjMatAll{1,iBatch}{iSub,iTrainStage}{iSession,1}=AdjMatStim;
  268. AdjMatAll{1,iBatch}{iSub,iTrainStage}{iSession,2}=AdjMatDelay;
  269. AdjMatAll{1,iBatch}{iSub,iTrainStage}{iSession,3}=AdjMatResp;
  270. AdjMatAll{1,iBatch}{iSub,iTrainStage}{iSession,4}=AdjMatStimDelay;
  271. clear temp* AdjMatStim AdjMatDelay AdjMatResp AdjMatStimDelay TrialTypeIdxTemp
  272. end
  273. end
  274. end
  275. end
  276. %% clustering coef, Geodesic path length,co-firing strength
  277. % contatenate all trials to one time series
  278. SignFactor=[1 -1];% split corr mat to positive R and negative R
  279. for iBatch=1:2
  280. for iSub=1:size(DataAll.(BatchName{iBatch}),1)
  281. tic
  282. for iTrainStage=1:nTrainStage% INI EXP REV
  283. for iSession=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}))
  284. for iPeriod=1:4 %BL Stim Delay Resp StimDelay(concaternated time series from stim to delay)
  285. for iType=1:3%Go/NoGo/catch
  286. for iSign=1:2
  287. % not fixed with signed weight CluCoef
  288. % clustering coef
  289. temp=SignFactor(iSign)*AdjMatAll{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(:,:,iType);
  290. temp(logical(diag(ones(1,size(temp,1)),0)))=0;
  291. temp(temp<0)=0;
  292. tempMax=max(temp,[],'all');% clu coef normalisation
  293. % exclude negative weight
  294. AdjMatAllNorm=temp/tempMax;
  295. CluCoef{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(:,iType,iSign)=...
  296. clustering_coef_wu(AdjMatAllNorm);
  297. % co-firing strength (Hubness)
  298. temp=SignFactor(iSign)*AdjMatAll{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(:,:,iType);
  299. temp(find(temp<0))=0;
  300. Weight2All{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(:,iType,iSign)=...
  301. mean(temp,2);
  302. % Geodesic path length
  303. temp=SignFactor(iSign)*AdjMatAll{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(:,:,iType);
  304. temp1=1./temp;
  305. temp1(temp1<0)=inf;
  306. temp1(logical(diag(ones(1,size(temp,1)),0)))=0;
  307. temp2=distance_wei(temp1);
  308. temp3=temp2(find(triu(temp2)~=0));
  309. PathLength{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(:,iType,iSign)=...
  310. temp3;
  311. clear temp*
  312. end
  313. end
  314. end
  315. end
  316. end
  317. toc
  318. end
  319. end
  320. clear temp* AdjMatAllNorm CluCoefPerMouse Weight2AllPerMouse PathLengthPerMouse identifiers identifiers1
  321. TypeNameTemp={'G','N','C'};
  322. PeriodNameTemp={'S','D','R','SD'};
  323. StageNameTemp={'1','2','3','4'};
  324. RValueName={'p','n'};
  325. for iBatch=1:2
  326. for iSub=1:size(DataAll.(BatchName{iBatch}),1)
  327. for iTrainStage=1:4% INI EXP REV-naive REV-expert
  328. tempClu=[];tempWeight=[];tempPath=[];tempLabel=[];tempLabel1=[];
  329. for iPeriod=1 %Stim Delay Resp
  330. for iType=[2 1]%:2%Go/NoGo/catch
  331. for iSign=1% +R/-R
  332. for iSession=1:length(DataAll.(BatchName{iBatch})(iSub).BHV.(StageName{iTrainStage}))
  333. label=[StageNameTemp{iTrainStage},'-',PeriodNameTemp{iPeriod},'-',...
  334. TypeNameTemp{iType},'-',RValueName{iSign}];
  335. temp=CluCoef{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(:,iType,iSign);
  336. tempClu=cat(1,tempClu,temp);
  337. temp1=Weight2All{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(:,iType,iSign);
  338. tempWeight=cat(1,tempWeight,temp1);
  339. tempLabel=cat(1,tempLabel,string(repmat(label,length(temp),1)));
  340. temp2=PathLength{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(:,iType,iSign);
  341. tempPath=cat(1,tempPath,temp2);
  342. tempLabel1=cat(1,tempLabel1,string(repmat(label,length(temp2),1)));
  343. end
  344. end
  345. end
  346. end
  347. CluCoefPerMouse{1,iBatch}{iSub,iTrainStage}=tempClu;
  348. Weight2AllPerMouse{1,iBatch}{iSub,iTrainStage}=tempWeight;
  349. PathLengthPerMouse{1,iBatch}{iSub,iTrainStage}=tempPath;
  350. identifiers{1,iBatch}{iSub,iTrainStage}=tempLabel;
  351. identifiers1{1,iBatch}{iSub,iTrainStage}=tempLabel1;
  352. clear temp*
  353. end
  354. end
  355. end
  356. clear temp* Group* label*
  357. GroupClu=[];GroupWeight=[];GroupPath=[];GroupLabel=[];GroupLabel1=[];GroupLabel2=[];
  358. for iBatch=1:2
  359. for iSub=1:size(DataAll.(BatchName{iBatch}),1)
  360. % pool mice
  361. tempDim=numel(CluCoefPerMouse{1,iBatch});
  362. data=cell2mat(reshape(CluCoefPerMouse{1,iBatch},tempDim,1));
  363. data1=cell2mat(reshape(Weight2AllPerMouse{1,iBatch},tempDim,1));
  364. data2=cell2mat(reshape(PathLengthPerMouse{1,iBatch},tempDim,1));
  365. temp=reshape(identifiers{1,iBatch},tempDim,1);
  366. temp1=reshape(identifiers1{1,iBatch},tempDim,1);
  367. label=cellstr(cat(1,temp{:}));
  368. label1=cellstr(cat(1,temp{:}));
  369. label2=cellstr(cat(1,temp1{:}));
  370. GroupClu=cat(1,GroupClu,data);
  371. GroupWeight=cat(1,GroupWeight,data1);
  372. GroupPath=cat(1,GroupPath,data2);
  373. GroupLabel=cat(1,GroupLabel,label);
  374. GroupLabel1=cat(1,GroupLabel1,label1);
  375. GroupLabel2=cat(1,GroupLabel2,label2);
  376. end
  377. end
  378. % labels
  379. %1-2-3-4:INI EXP REV-naive REV-expert
  380. %S-D:Stimulus period, Delay period
  381. %G-N:Go-Nogo trial type
  382. %p-n:positive-R, negative-R
  383. %example
  384. %1-S-N-p: Naive-Stimulus-Nogo-positiveR
  385. %4-D-G-n: REV-expert-Delay-Go-negativeR
  386. % only check fig1
  387. % plot path length
  388. close all
  389. figure;
  390. FscatJit2(GroupLabel2,GroupPath,[],'N',1);
  391. suplabel('Path','x',[.5 1 0 0]);%top x
  392. % plot Hubness
  393. close all
  394. figure
  395. FscatJit2(GroupLabel1,GroupWeight,[],'N',5);
  396. suplabel('Hubness','x',[.5 .99 0 0]);%top x
  397. close all
  398. figure
  399. FscatJit2(GroupLabel,GroupClu,[],'N',5);
  400. suplabel('CluCoef','x',[.5 .99 0 0]);%top x
  401. xtickangle(45)
  402. % circular plot
  403. tempSession=[1 3 1 2];
  404. iBatch=1;iSub=3;iPeriod=2;
  405. for iType=1:2 % Go,Nogo,Catch
  406. figure
  407. i=1;
  408. for iTrainStage=1:nTrainStage
  409. iSession=tempSession(iTrainStage);
  410. temp=AdjMatAll{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(:,:,iType);
  411. subplot(2,4,i)
  412. imagesc(temp);
  413. title(['C',num2str(round(mean(CluCoef{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(:,iType)),2))...
  414. ' L',num2str(round(mean(PathLength{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(:,iType)),2))...
  415. ' H',num2str(round(mean(Weight2All{1,iBatch}{iSub,iTrainStage}{iSession,iPeriod}(:,iType)),3))])
  416. axis square
  417. caxis([0 .3])
  418. subplot(2,4,i+4)
  419. temp(find(temp<0.3|temp>.99))=0;
  420. G=graph(temp,'omitselfloops','upper');
  421. plot(G,'Layout','circle','EdgeColor','r','NodeLabel',{},...
  422. 'LineWidth',5*G.Edges.Weight);%,'WeightEffect','inverse'
  423. title(["d'=",num2str(dprime{1,iBatch}{iSub,iTrainStage}{iSession,1})])
  424. axis square
  425. i=i+1;
  426. end
  427. end