lsdPaperNotes.m 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429
  1. %% Collate raw data: LSD vs. Saline; acute effects from that days' baseline
  2. [data,samp,files] = collateData(['G:\Greenlab\data\lsd\processed\',...
  3. 'imagAcute\'],{'lsd';'sal'},{'pow','coh'},'trl','');
  4. % For each animal, average that day's baseline and use to normalize post
  5. allData = [data{1,1};data{1,2}];
  6. mBase = cellfun(@(x) mean(x,1),allData(:,1),'UniformOutput',0);
  7. for ii = 1:numel(mBase)
  8. dif{ii} = allData{ii,2}-repmat(mBase{ii},size(allData{ii,2},1),1);
  9. end
  10. allPostLSD = dif(1:numel(files{1,1}));
  11. allPostSal = dif(numel(files{1,1})+1:end);
  12. minSamp = min(cellfun(@(x) size(x,1),dif));
  13. % save('G:\GreenLab\data\lsd\normAcuteImagRest.mat','allPostLSD','allPostSal','minSamp',...
  14. % 'data','files')
  15. %% Get rest times
  16. files1 = fileSearch('G:\Greenlab\data\lsd\restProcessed\','_Sal_');
  17. files2 = fileSearch('G:\Greenlab\data\lsd\restProcessed\','_LSD_');
  18. files = [files1,files2];
  19. time = linspace(0,1,1000);
  20. rests = zeros(size(files,2),1000);
  21. restsPost = nan(size(files,2),5500);
  22. for ii = 1:size(files,2)
  23. cd 'G:\Greenlab\data\lsd\restProcessed\'
  24. load(files{ii},'hist')
  25. totalTime(ii) = hist.eventTs.t{1}(end);
  26. starts{ii} = hist.eventTs.t{4};
  27. stops{ii} = hist.eventTs.t{5};
  28. startRel{ii} = round(starts{ii}./totalTime(ii),3);
  29. stopRel{ii} = round(stops{ii}./totalTime(ii),3);
  30. restsPost(ii,1:ceil(totalTime(ii)-inj(ii))) = 0;
  31. for jj = 1:size(startRel{ii})
  32. rests(ii,nearest_idx3(startRel{ii}(jj),time):...
  33. nearest_idx3(stopRel{ii}(jj),time)) = 1;
  34. load(['G:\GreenLab\data\lsd\processed\imagAcute\',...
  35. files{ii}(1:end-9),'_pre_vs_post.mat'],'hist')
  36. inj(ii) = hist.eventTs.t{3};
  37. if starts{ii}(jj) >= inj(ii)
  38. restsPost(ii,ceil(starts{ii}(jj)-inj(ii)):...
  39. ceil(stops{ii}(jj)-inj(ii))) = 1;
  40. end
  41. end
  42. end
  43. %%
  44. salInd = contains(files,'_Sal_');
  45. lsdInd = contains(files,'_LSD_');
  46. figure
  47. h = pcolor(padarray([restsPost(salInd,:);...
  48. nan(1,5500);...
  49. mean(restsPost(salInd,:));...
  50. nan(1,5500);...
  51. mean(restsPost(lsdInd,:));...
  52. nan(1,5500);...
  53. restsPost(lsdInd,:)],...
  54. [1 1],NaN,'post'));
  55. set(h,'EdgeColor','none')
  56. %%
  57. figure
  58. plot(smooth(mean(restsPost(salInd,:)),120),'b')
  59. hold on
  60. plot(smooth(mean(restsPost(lsdInd,:)),120),'r')
  61. set(gca,'xtick',0:600:5000,'xticklabel',[0:600:5500]./60,'ytick',0:0.25:1)
  62. xlim([0 5000])
  63. xlabel('mins post injection')
  64. ylabel('% of animals resting')
  65. legend({'SAL','LSD'})
  66. box off
  67. %%
  68. figure
  69. imagesc([rests(salInd,:);rests(lsdInd,:)]);
  70. hold on
  71. for ii = 1:size(inj,2)
  72. plot([nearest_idx3(inj(ii),time) nearest_idx3(inj(ii),time)],...
  73. [ii-.5 ii+.5],'r')
  74. end
  75. %% Collate data and split into pre and post injection
  76. % [data,samp,files] = collateData('G:\Greenlab\data\lsd\restProcessed\',...
  77. % {'_lsd';'_sal'},{'pow','coh'},'trl','');
  78. [data,samp,files] = collateData('G:\GreenLab\data\lsd\processed\imagAcute\',...
  79. {'_lsd';'_sal'},{'pow','coh'},'trl','');
  80. %%
  81. for c = 1:2
  82. for ii = 1:numel(files{c})
  83. % load(['G:\GreenLab\data\lsd\processed\imagAcute\',...
  84. % files{c}{ii}(1:end-9),'_pre_vs_post.mat'],'hist')
  85. load(['G:\GreenLab\data\lsd\processed\imagAcute\',...
  86. files{c}{ii}],'hist')
  87. pre{c,ii} = data{c}{ii}(samp{c}{ii}(:,1)<hist.eventTs.t{2}*2000,:);
  88. post{c,ii} = data{c}{ii}(samp{c}{ii}(:,1)>hist.eventTs.t{3}*2000,:);
  89. end
  90. end
  91. %% Only use data from the last 30 minutes
  92. c = 1;
  93. for ii = 1:2
  94. for jj = 1:size(data{1,ii},1)
  95. allData30(c,:) = [data{1,ii}(jj,1),...
  96. data{1,ii}{jj,2}(nearest_idx3(samp{1,ii}{jj,2}(end,1)...
  97. -30*60*400,samp{1,ii}{jj,2}(:,1)):end,:)];
  98. c = c+1;
  99. end
  100. end
  101. mBase = cellfun(@(x) mean(x,1),allData30(:,1),'UniformOutput',0);
  102. sBase = cellfun(@(x) std(x,[],1),allData30(:,1),'UniformOutput',0);
  103. for ii = 1:numel(mBase)
  104. dif30{ii} = allData30{ii,2}-repmat(mBase{ii},size(allData30{ii,2},1),1);
  105. dif30z{ii} = (allData30{ii,2}-...
  106. repmat(mBase{ii},size(allData30{ii,2},1),1))./...
  107. repmat(sBase{ii},size(allData30{ii,2},1),1);
  108. end
  109. allPostLSD30 = dif30(1:numel(files{1,1}));
  110. allPostSal30 = dif30(numel(files{1,1})+1:end);
  111. allPostLSD30z = dif30z(1:numel(files{1,1}));
  112. allPostSal30z = dif30z(numel(files{1,1})+1:end);
  113. minSamp30 = min(cellfun(@(x) size(x,1),dif30));
  114. % save('G:\GreenLab\data\lsd\normAcuteImag30.mat','allPostLSD30',...
  115. % 'allPostSal30','minSamp30','data','files','allData30',...
  116. % 'allPostSal30z','allPostLSD30z')
  117. %% Compare rest times between groups on injection day and post day
  118. salFiles = fileSearch('G:\GreenLab\data\lsd\scoredMat\','_Sal');
  119. salPostFiles = fileSearch('G:\GreenLab\data\lsd\scoredMat\','_PostSal');
  120. lsdFiles = fileSearch('G:\GreenLab\data\lsd\scoredMat\','_LSD');
  121. lsdPostFiles = fileSearch('G:\GreenLab\data\lsd\scoredMat\','_PostLSD');
  122. for ii = 1:numel(salFiles)
  123. load(salFiles{ii},'eventTs')
  124. inds = eventInd(eventTs,{'rest'});
  125. salRest(ii) = sum(eventTs.t{inds(2)}-eventTs.t{inds(1)});
  126. salRest30(ii) = sum(eventTs.t{inds(2)}(eventTs.t{inds(2)}>...
  127. eventTs.t{1}(end)-30*60)-eventTs.t{inds(1)}(eventTs.t{inds(2)}>...
  128. eventTs.t{1}(end)-30*60));
  129. end
  130. for ii = 1:numel(salPostFiles)
  131. load(salPostFiles{ii},'eventTs')
  132. inds = eventInd(eventTs,{'rest'});
  133. salPostRest(ii) = sum(eventTs.t{inds(2)}-eventTs.t{inds(1)});
  134. salPostRest30(ii) = sum(eventTs.t{inds(2)}(eventTs.t{inds(2)}>...
  135. eventTs.t{1}(end)-30*60)-eventTs.t{inds(1)}(eventTs.t{inds(2)}>...
  136. eventTs.t{1}(end)-30*60));
  137. end
  138. for ii = 1:numel(lsdFiles)
  139. load(lsdFiles{ii},'eventTs')
  140. inds = eventInd(eventTs,{'rest'});
  141. lsdRest(ii) = sum(eventTs.t{inds(2)}-eventTs.t{inds(1)});
  142. lsdRest30(ii) = sum(eventTs.t{inds(2)}(eventTs.t{inds(2)}>...
  143. eventTs.t{1}(end)-30*60)-eventTs.t{inds(1)}(eventTs.t{inds(2)}>...
  144. eventTs.t{1}(end)-30*60));
  145. end
  146. for ii = 1:numel(lsdPostFiles)
  147. load(lsdPostFiles{ii},'eventTs')
  148. inds = eventInd(eventTs,{'rest'});
  149. lsdPostRest(ii) = sum(eventTs.t{inds(2)}-eventTs.t{inds(1)});
  150. lsdPostRest30(ii) = sum(eventTs.t{inds(2)}(eventTs.t{inds(2)}>...
  151. eventTs.t{1}(end)-30*60)-eventTs.t{inds(1)}(eventTs.t{inds(2)}>...
  152. eventTs.t{1}(end)-30*60));
  153. end
  154. figure
  155. subplot(1,2,1)
  156. plotSpread({salRest,lsdRest,salPostRest,lsdPostRest},...
  157. 'DistributionMarkers','o');
  158. ylabel('time (sec)')
  159. set(gca,'xticklabel',{'sal acute','lsd acute','sal 24 hours',...
  160. 'lsd 24 hours'})
  161. title('all data')
  162. subplot(1,2,2)
  163. plotSpread({salRest30,lsdRest30,salPostRest30,lsdPostRest30},...
  164. 'DistributionMarkers','o');
  165. ylabel('time (sec)')
  166. set(gca,'xticklabel',{'sal acute','lsd acute','sal 24 hours',...
  167. 'lsd 24 hours'})
  168. title('last 30 minutes')
  169. %% power and coherence changes in LSD relative to SAL
  170. salFiles = fileSearch('G:\GreenLab\data\lsd\processed\imagAcute\','Sal');
  171. [salPow,salCoh] = deal(cell(numel(salFiles),2));
  172. for ii = 1:numel(salFiles)
  173. load(salFiles{ii},'psdTrls','coh','trls','LFPTs')
  174. inds30 = nearest_idx3(nearest_idx3(LFPTs.tvec(end)-30*60,...
  175. LFPTs.tvec),trls{1,2}.sampleinfo(:,1));
  176. salPow{ii,1} = psdTrls{1}.Pow;
  177. salPow{ii,2} = psdTrls{2}.Pow;
  178. salPow{ii,3} = psdTrls{2}.Pow(:,:,inds30:end);
  179. [b,c,t] = size(psdTrls{1}.bandPow);
  180. salBand{ii,1} = reshape(psdTrls{1}.bandPow,b*c,t)';
  181. [b,c,t] = size(psdTrls{2}.bandPow);
  182. salBand{ii,2} = reshape(psdTrls{2}.bandPow,b*c,t)';
  183. salCoh{ii,1} = coh{1}.Cxy;
  184. salCoh{ii,2} = coh{2}.Cxy;
  185. salCoh{ii,3} = coh{2}.Cxy(:,:,inds30:end);
  186. end
  187. lsdFiles = fileSearch('G:\GreenLab\data\lsd\processed\imagAcute\','LSD');
  188. [lsdPow,lsdCoh] = deal(cell(numel(lsdFiles),2));
  189. for ii = 1:numel(lsdFiles)
  190. load(lsdFiles{ii},'psdTrls','coh','trls','LFPTs')
  191. inds30 = nearest_idx3(nearest_idx3(LFPTs.tvec(end)-30*60,...
  192. LFPTs.tvec),trls{1,2}.sampleinfo(:,1));
  193. lsdPow{ii,1} = psdTrls{1}.Pow;
  194. lsdPow{ii,2} = psdTrls{2}.Pow;
  195. lsdPow{ii,3} = psdTrls{2}.Pow(:,:,inds30);
  196. [b,c,t] = size(psdTrls{1}.bandPow);
  197. lsdBand{ii,1} = reshape(psdTrls{1}.bandPow,b*c,t)';
  198. [b,c,t] = size(psdTrls{2}.bandPow);
  199. lsdBand{ii,2} = reshape(psdTrls{2}.bandPow,b*c,t)';
  200. lsdCoh{ii,1} = coh{1}.Cxy;
  201. lsdCoh{ii,2} = coh{2}.Cxy;
  202. lsdCoh{ii,3} = coh{2}.Cxy(:,:,inds30:end);
  203. end
  204. %% double check band power
  205. bInd = [1 4;5 10;11 14;15 30;45 65;70 90];
  206. for ii = 1:size(salPow,1)
  207. for jj = 1:size(salPow,2)
  208. these = [];
  209. for k = 1:size(bInd,1)
  210. data = squeeze(trapz(salPow{ii,jj}(:,bInd(k,1):bInd(k,2),:),2));
  211. these(k,:,:) = data;
  212. end
  213. [b,c,t] = size(these);
  214. salBandChk{ii,jj} = reshape(these,b*c,t)';
  215. if any(salBandChk{ii,jj}~=salBand{ii,jj})
  216. disp('fuck')
  217. end
  218. end
  219. end
  220. for ii = 1:size(lsdPow,1)
  221. for jj = 1:size(lsdPow,2)
  222. these = [];
  223. for k = 1:size(bInd,1)
  224. data = squeeze(trapz(lsdPow{ii,jj}(:,bInd(k,1):bInd(k,2),:),2));
  225. these(k,:,:) = data;
  226. end
  227. [b,c,t] = size(these);
  228. lsdBandChk{ii,jj} = reshape(these,b*c,t)';
  229. if any(lsdBandChk{ii,jj}~=lsdBand{ii,jj})
  230. disp('fuck')
  231. end
  232. end
  233. end
  234. %% plot pre post lsd and sal - power
  235. allLSDpre = cat(3,lsdPow{:,1});
  236. LSDpost30 = cat(3,lsdPow{:,3});
  237. allLSDpost = cat(3,lsdPow{:,2});
  238. mLSDpre = mean(allLSDpre,3);
  239. sLSDpre = std(allLSDpre,[],3);
  240. sLSDpost = std(allLSDpost,[],3);
  241. mLSDpost = mean(allLSDpost,3);
  242. mLSDpost30 = mean(LSDpost30,3);
  243. sLSDpost30 = std(LSDpost30,[],3);
  244. lsdDiff = mLSDpost-mLSDpre;
  245. lsdDiffS = (abs(std(allLSDpost,[],3)+std(allLSDpre,[],3)-2*...
  246. std(cat(3,allLSDpre,allLSDpost),[],3))).^0.5;
  247. allSALpre = cat(3,salPow{:,1});
  248. allSALpost = cat(3,salPow{:,2});
  249. SALpost30 = cat(3,salPow{:,3});
  250. mSALpre = mean(allSALpre,3);
  251. mSALpost = mean(allSALpost,3);
  252. mSALpost30 = mean(SALpost30,3);
  253. sSALpre = std(allSALpre,[],3);
  254. sSALpost = std(allSALpost,[],3);
  255. sSALpost30 = std(SALpost30,[],3);
  256. salDiff = mSALpost-mSALpre;
  257. salDiffS = (abs(std(allSALpost,[],3)+std(allSALpre,[],3)-2*...
  258. std(cat(3,allSALpre,allSALpost),[],3))).^0.5;
  259. x = (abs(std(cat(3,allSALpost,allLSDpost),[],3)+std(cat(3,allSALpost,allLSDpost),[],3)-2*...
  260. std(cat(3,allLSDpre,allLSDpost,allSALpre,allSALpost),[],3))).^0.5;
  261. lsdSalDiff = lsdDiff-salDiff;
  262. lsdSalDiffS = (abs(lsdDiffS+salDiffS-2*(x))).^0.5;
  263. sites = {'ILL','CA1L','PLL','NAcL','PLR','CA1R','ILR','NAcR'};
  264. %%
  265. figure
  266. for ii = 1:8
  267. subplot(4,4,ii)
  268. hold on
  269. shadedErrorBar(1:100,lsdDiff(ii,:),lsdDiffS(ii,:),'r',1)
  270. shadedErrorBar(1:100,salDiff(ii,:),salDiffS(ii,:),'b',1)
  271. % shadedErrorBar(1:100,lsdSalDiff(ii,:),lsdSalDiffS(ii,:),'k',1)
  272. plot([1 1],[-6.5 2],':k')
  273. plot([4 4],[-6.5 2],':k')
  274. plot([5 5],[-6.5 2],':k')
  275. plot([10 10],[-6.5 2],':k')
  276. plot([11 11],[-6.5 2],':k')
  277. plot([14 14],[-6.5 2],':k')
  278. plot([15 15],[-6.5 2],':k')
  279. plot([30 30],[-6.5 2],':k')
  280. plot([45 45],[-6.5 2],':k')
  281. plot([65 65],[-6.5 2],':k')
  282. plot([70 70],[-6.5 2],':k')
  283. plot([90 90],[-6.5 2],':k')
  284. plot([0 100],[0 0],'--k')
  285. set(gca,'xtick',0:10:100)
  286. xlabel('frequency (Hz)')
  287. ylabel('a.u.')
  288. ylim([-6.6 1])
  289. title(sites{ii})
  290. end
  291. for ii = 1:8
  292. subplot(4,4,8+ii)
  293. hold on
  294. shadedErrorBar(1:100,lsdSalDiff(ii,:),lsdSalDiffS(ii,:),'k',1)
  295. set(gca,'xtick',0:10:100)
  296. ylim([-5 1])
  297. plot([0 100],[0 0],'--k')
  298. end
  299. %%
  300. figure
  301. for ii = 1:8
  302. subplot(4,2,ii)
  303. hold on
  304. shadedErrorBar(1:100,mLSDpre(ii,:),sLSDpre(ii,:),'k',1)
  305. shadedErrorBar(1:100,mLSDpost(ii,:),sLSDpost(ii,:),'r',1)
  306. plot([1 1],[-80 -20],':k')
  307. plot([4 4],[-80 -20],':k')
  308. plot([5 5],[-80 -20],':k')
  309. plot([10 10],[-80 -20],':k')
  310. plot([11 11],[-80 -20],':k')
  311. plot([14 14],[-80 -20],':k')
  312. plot([15 15],[-80 -20],':k')
  313. plot([30 30],[-80 -20],':k')
  314. plot([45 45],[-80 -20],':k')
  315. plot([65 65],[-80 -20],':k')
  316. plot([70 70],[-80 -20],':k')
  317. plot([90 90],[-80 -20],':k')
  318. set(gca,'xtick',0:10:100)
  319. xlabel('frequency (Hz)')
  320. ylabel('a.u.')
  321. title(sites{ii})
  322. end
  323. figure
  324. for ii = 1:8
  325. subplot(4,2,ii)
  326. hold on
  327. shadedErrorBar(1:100,mSALpre(ii,:),sSALpre(ii,:),'k',1)
  328. shadedErrorBar(1:100,mSALpost(ii,:),sSALpost(ii,:),'b:',1)
  329. plot([1 1],[-80 -20],':k')
  330. plot([4 4],[-80 -20],':k')
  331. plot([5 5],[-80 -20],':k')
  332. plot([10 10],[-80 -20],':k')
  333. plot([11 11],[-80 -20],':k')
  334. plot([14 14],[-80 -20],':k')
  335. plot([15 15],[-80 -20],':k')
  336. plot([30 30],[-80 -20],':k')
  337. plot([45 45],[-80 -20],':k')
  338. plot([65 65],[-80 -20],':k')
  339. plot([70 70],[-80 -20],':k')
  340. plot([90 90],[-80 -20],':k')
  341. set(gca,'xtick',0:10:100)
  342. xlabel('frequency (Hz)')
  343. ylabel('a.u.')
  344. title(sites{ii})
  345. end
  346. %%
  347. figure
  348. for ii = 1:8
  349. subplot(4,2,ii)
  350. hold on
  351. shadedErrorBar(1:100,mLSDpre(ii,:),sLSDpre(ii,:),'k',1)
  352. shadedErrorBar(1:100,mLSDpost30(ii,:),sLSDpost30(ii,:),'r',1)
  353. plot([1 1],[-80 -20],':k')
  354. plot([4 4],[-80 -20],':k')
  355. plot([5 5],[-80 -20],':k')
  356. plot([10 10],[-80 -20],':k')
  357. plot([11 11],[-80 -20],':k')
  358. plot([14 14],[-80 -20],':k')
  359. plot([15 15],[-80 -20],':k')
  360. plot([30 30],[-80 -20],':k')
  361. plot([45 45],[-80 -20],':k')
  362. plot([65 65],[-80 -20],':k')
  363. plot([70 70],[-80 -20],':k')
  364. plot([90 90],[-80 -20],':k')
  365. set(gca,'xtick',0:10:100)
  366. xlabel('frequency (Hz)')
  367. ylabel('a.u.')
  368. title(sites{ii})
  369. end
  370. figure
  371. for ii = 1:8
  372. subplot(4,2,ii)
  373. hold on
  374. shadedErrorBar(1:100,mSALpre(ii,:),sSALpre(ii,:),'k',1)
  375. shadedErrorBar(1:100,mSALpost30(ii,:),sSALpost30(ii,:),'b:',1)
  376. plot([1 1],[-80 -20],':k')
  377. plot([4 4],[-80 -20],':k')
  378. plot([5 5],[-80 -20],':k')
  379. plot([10 10],[-80 -20],':k')
  380. plot([11 11],[-80 -20],':k')
  381. plot([14 14],[-80 -20],':k')
  382. plot([15 15],[-80 -20],':k')
  383. plot([30 30],[-80 -20],':k')
  384. plot([45 45],[-80 -20],':k')
  385. plot([65 65],[-80 -20],':k')
  386. plot([70 70],[-80 -20],':k')
  387. plot([90 90],[-80 -20],':k')
  388. set(gca,'xtick',0:10:100)
  389. xlabel('frequency (Hz)')
  390. ylabel('a.u.')
  391. title(sites{ii})
  392. end
  393. %% coh - coherogram differences
  394. allLSDpre = cat(3,lsdCoh{:,1});
  395. allLSDpost = cat(3,lsdCoh{:,2});
  396. LSDpost30 = cat(3,lsdCoh{:,3});
  397. % allLSDpre = cellfun(@(x) mean(x,3),lsdCoh(:,1),'UniformOutput',0);
  398. % allLSDpre = cat(3,allLSDpre{:});
  399. %
  400. % allLSDpost = cellfun(@(x) mean(x,3),lsdCoh(:,2),'UniformOutput',0);
  401. % allLSDpost = cat(3,allLSDpost{:});
  402. mLSDpre = mean(allLSDpre,3);
  403. mLSDpost = mean(allLSDpost,3);
  404. mLSDpost30 = mean(LSDpost30,3);
  405. sLSDpost30 = std(LSDpost30,[],3);
  406. sLSDpre = std(allLSDpre,[],3);
  407. lsdDiff = mLSDpost-mLSDpre;
  408. % Linear propogation of error
  409. lsdDiffS = (abs(std(allLSDpost,[],3)+std(allLSDpre,[],3)-2*...
  410. std(cat(3,allLSDpre,allLSDpost),[],3))).^0.5;
  411. allSALpre = cat(3,salCoh{:,1});
  412. allSALpost = cat(3,salCoh{:,2});
  413. SALpost30 = cat(3,salCoh{:,3});
  414. % allSALpre = cellfun(@(x) mean(x,3),salCoh(:,1),'UniformOutput',0);
  415. % allSALpre = cat(3,allSALpre{:});
  416. %
  417. % allSALpost = cellfun(@(x) mean(x,3),salCoh(:,2),'UniformOutput',0);
  418. % allSALpost = cat(3,allSALpost{:});
  419. mSALpre = mean(allSALpre,3);
  420. mSALpost = mean(allSALpost,3);
  421. mSALpost30 = mean(SALpost30,3);
  422. sSALpost30 = std(SALpost30,[],3);
  423. sSALpre = std(allSALpre,[],3);
  424. salDiff = mSALpost-mSALpre;
  425. % Linear propogation of error
  426. salDiffS = (abs(std(allSALpost,[],3)+std(allSALpre,[],3)-2*...
  427. std(cat(3,allSALpre,allSALpost),[],3))).^0.5;
  428. x = (abs(std(cat(3,allSALpost,allLSDpost),[],3)+std(cat(3,allSALpost,allLSDpost),[],3)-2*...
  429. std(cat(3,allLSDpre,allLSDpost,allSALpre,allSALpost),[],3))).^0.5;
  430. lsdSalDiff = lsdDiff-salDiff;
  431. lsdSalDiffS = (abs(lsdDiffS+salDiffS-2*(x))).^0.5;
  432. sites = {'ILL','CA1L','PLL','NAcL','PLR','CA1R','ILR','NAcR'};
  433. cmbs = nchoosek(1:8,2);
  434. cmbs = cmbs([3,6,7,21,22,28],:);
  435. %%
  436. figure
  437. for ii = 1:size(cmbs,1)
  438. subplot(3,2,ii)
  439. hold on
  440. shadedErrorBar(1:100,decimate(lsdDiff(ii,:),5),...
  441. decimate(lsdDiffS(ii,:),5),'r',1)
  442. shadedErrorBar(1:100,decimate(salDiff(ii,:),5),...
  443. decimate(salDiffS(ii,:),5),'b',1)
  444. shadedErrorBar(1:100,decimate(lsdSalDiff(ii,:),5),...
  445. decimate(lsdSalDiffS(ii,:),5),'k',1)
  446. plot([1 1],[-1 1],':k')
  447. plot([4 4],[-1 1],':k')
  448. plot([5 5],[-1 1],':k')
  449. plot([10 10],[-1 1],':k')
  450. plot([11 11],[-1 1],':k')
  451. plot([14 14],[-1 1],':k')
  452. plot([15 15],[-1 1],':k')
  453. plot([30 30],[-1 1],':k')
  454. plot([45 45],[-1 1],':k')
  455. plot([65 65],[-1 1],':k')
  456. plot([70 70],[-1 1],':k')
  457. plot([90 90],[-1 1],':k')
  458. plot([0 100],[0 0],'--k')
  459. set(gca,'xtick',0:10:100)
  460. ylim([-0.3 0.3])
  461. xlabel('frequency (Hz)')
  462. ylabel('a.u.')
  463. title([sites{cmbs(ii,1)},'-',sites{cmbs(ii,2)}])
  464. end
  465. %%
  466. figure
  467. for ii = 1:size(cmbs,1)
  468. subplot(3,2,ii)
  469. hold on
  470. shadedErrorBar(1:100,decimate(mLSDpost30(ii,:),5),...
  471. decimate(sLSDpost30(ii,:),5),'r',1)
  472. shadedErrorBar(1:100,decimate(mSALpost30(ii,:),5),...
  473. decimate(sSALpost30(ii,:),5),'b',1)
  474. plot([1 1],[-1 1],':k')
  475. plot([4 4],[-1 1],':k')
  476. plot([5 5],[-1 1],':k')
  477. plot([10 10],[-1 1],':k')
  478. plot([11 11],[-1 1],':k')
  479. plot([14 14],[-1 1],':k')
  480. plot([15 15],[-1 1],':k')
  481. plot([30 30],[-1 1],':k')
  482. plot([45 45],[-1 1],':k')
  483. plot([65 65],[-1 1],':k')
  484. plot([70 70],[-1 1],':k')
  485. plot([90 90],[-1 1],':k')
  486. plot([0 100],[0 0],'--k')
  487. set(gca,'xtick',0:10:100)
  488. ylim([-0.3 0.3])
  489. xlabel('frequency (Hz)')
  490. ylabel('a.u.')
  491. title([sites{cmbs(ii,1)},'-',sites{cmbs(ii,2)}])
  492. end
  493. %%
  494. figure
  495. for ii = 1:size(cmbs,1)
  496. subplot(3,2,ii)
  497. hold on
  498. % shadedErrorBar(1:100,decimate(mLSDpost30(ii,:),5),...
  499. % decimate(sLSDpost30(ii,:),5),'r',1)
  500. % shadedErrorBar(1:100,decimate(mLSDpre(ii,:),5),...
  501. % decimate(sLSDpre(ii,:),5),'k',1)
  502. plot(1:100,decimate(mLSDpost30(ii,:),5),'r')
  503. plot(1:100,decimate(mLSDpre(ii,:),5),'k')
  504. plot([1 1],[-1 1],':k')
  505. plot([4 4],[-1 1],':k')
  506. plot([5 5],[-1 1],':k')
  507. plot([10 10],[-1 1],':k')
  508. plot([11 11],[-1 1],':k')
  509. plot([14 14],[-1 1],':k')
  510. plot([15 15],[-1 1],':k')
  511. plot([30 30],[-1 1],':k')
  512. plot([45 45],[-1 1],':k')
  513. plot([65 65],[-1 1],':k')
  514. plot([70 70],[-1 1],':k')
  515. plot([90 90],[-1 1],':k')
  516. plot([0 100],[0 0],'--k')
  517. set(gca,'xtick',0:10:100)
  518. ylim([-0.2 0.2])
  519. xlabel('frequency (Hz)')
  520. ylabel('a.u.')
  521. title([sites{cmbs(ii,1)},'-',sites{cmbs(ii,2)}])
  522. end
  523. %%
  524. figure
  525. for ii = 1:size(cmbs,1)
  526. subplot(3,2,ii)
  527. hold on
  528. % shadedErrorBar(1:100,decimate(mSALpost30(ii,:),5),...
  529. % decimate(sLSDpost30(ii,:),5),'b',1)
  530. % shadedErrorBar(1:100,decimate(mSALpre(ii,:),5),...
  531. % decimate(sLSDpre(ii,:),5),'k',1)
  532. plot(1:100,decimate(mSALpost30(ii,:),5),'b')
  533. plot(1:100,decimate(mSALpre(ii,:),5),'k')
  534. plot([1 1],[-1 1],':k')
  535. plot([4 4],[-1 1],':k')
  536. plot([5 5],[-1 1],':k')
  537. plot([10 10],[-1 1],':k')
  538. plot([11 11],[-1 1],':k')
  539. plot([14 14],[-1 1],':k')
  540. plot([15 15],[-1 1],':k')
  541. plot([30 30],[-1 1],':k')
  542. plot([45 45],[-1 1],':k')
  543. plot([65 65],[-1 1],':k')
  544. plot([70 70],[-1 1],':k')
  545. plot([90 90],[-1 1],':k')
  546. plot([0 100],[0 0],'--k')
  547. set(gca,'xtick',0:10:100)
  548. ylim([-0.2 0.2])
  549. xlabel('frequency (Hz)')
  550. ylabel('a.u.')
  551. title([sites{cmbs(ii,1)},'-',sites{cmbs(ii,2)}])
  552. end
  553. %% coh - ecdfs
  554. [data,samp,files] = collateData(['G:\Greenlab\data\lsd\processed\',...
  555. 'imagAcute\'],{'lsd';'sal'},{'pow','coh'},'trl','');
  556. subInds = [1:6,37:42,19:24,43:48,79:84,61:66,85:90,169:174,211:216,175:180];
  557. feat = names({'ILL','CA1L','PLL','NAcL','PLR','CA1R','ILR','NAcR'},...
  558. {'d','t','a','b','lg','hg'});
  559. feat = feat(subInds);
  560. for ii = 1:size(data{1},1)
  561. lsdDiff{ii} = mean(data{1}{ii,1},1)-data{1}{ii,2};
  562. end
  563. allLSDdiff = cat(1,lsdDiff{:});
  564. allLSDdiff = allLSDdiff(:,subInds);
  565. for ii = 1:size(data{2},1)
  566. salDiff{ii} = mean(data{2}{ii,1},1)-data{2}{ii,2};
  567. end
  568. allSALdiff = cat(1,salDiff{:});
  569. allSALdiff = allSALdiff(:,subInds);
  570. figure
  571. for ii = 1:36
  572. subplot(6,6,ii)
  573. % plotSpread({allSALdiff(:,24+ii),allLSDdiff(:,24+ii)},...
  574. % 'distributionColor',{'b','r'})
  575. ecdf(allSALdiff(:,24+ii))
  576. hold on
  577. ecdf(allLSDdiff(:,24+ii))
  578. xlim([-0.5 0.5])
  579. title(feat{24+ii})
  580. end
  581. %% LSD vs. Base and SAL vs. Base - Log LOO
  582. % load('G:\GreenLab\data\lsd\normAcuteImag30.mat')
  583. subInds = [1:6,37:42,19:24,43:48,79:84,61:66,85:90,169:174,211:216,175:180];
  584. minSamp30 = min(min(cellfun(@(x) size(x,1),allData30)));
  585. % LSD vs. base - LOO
  586. for ii = 1:6
  587. testX = cat(1,allData30{ii,1}(randperm(size(allData30{ii,1},1),...
  588. minSamp30),subInds),...
  589. allData30{ii,2}(randperm(size(allData30{ii,2},1),minSamp30),...
  590. subInds));
  591. testY = cat(1,zeros(minSamp30,1),ones(minSamp30,1));
  592. otherInd = logicFind(1,~ismember(1:6,ii),'==');
  593. trainX = [];
  594. trainY = [];
  595. for jj = otherInd
  596. trainX = [trainX;cat(1,...
  597. allData30{jj,1}(randperm(size(allData30{jj,1},1),...
  598. minSamp30),subInds),...
  599. allData30{jj,2}(randperm(size(allData30{jj,2},1),...
  600. minSamp30),subInds))];
  601. trainY = [trainY;cat(1,zeros(minSamp30,1),ones(minSamp30,1))];
  602. end
  603. mdl = fitglm(zscore(trainX),trainY,'Distribution','binomial');
  604. prob = predict(mdl,zscore(testX));
  605. [~,~,~,aLSD(ii)] = perfcurve(testY,prob,1);
  606. end
  607. % LSD vs. base - 80:20
  608. for ii = 1:100
  609. for jj = 1:6
  610. thisData{jj,1} = allData30{jj,1}(randperm(size(allData30{jj,1},1),minSamp30),subInds);
  611. thisData{jj,2} = allData30{jj,2}(randperm(size(allData30{jj,2},1),minSamp30),subInds);
  612. end
  613. thisX = cat(1,thisData{:,1},thisData{:,2});
  614. thisY = cat(1,zeros(minSamp30*6,1),ones(minSamp30*6,1));
  615. [trainX,trainY,testX,testY] = trainTest(thisX,thisY,0.2);
  616. mdl = fitglm(zscore(trainX),trainY,'Distribution','binomial');
  617. prob = predict(mdl,zscore(testX));
  618. [~,~,~,aLSD80(ii)] = perfcurve(testY,prob,1);
  619. end
  620. % permuted LSD vs. base
  621. for ii = 1:6
  622. testX = cat(1,allData30{ii,1}(randperm(size(allData30{ii,1},1),...
  623. minSamp30),subInds),...
  624. allData30{ii,2}(randperm(size(allData30{ii,2},1),minSamp30),...
  625. subInds));
  626. testY = cat(1,zeros(minSamp30,1),ones(minSamp30,1));
  627. otherInd = logicFind(1,~ismember(1:6,ii),'==');
  628. otherInd1 = nchoosek(otherInd,3);
  629. for jj = 1:size(otherInd1,1)
  630. otherInd2(jj,:) = otherInd(~ismember(otherInd,otherInd1(jj,:)));
  631. end
  632. for jj = 1:size(otherInd1,1)
  633. trainX = [];
  634. trainY = [];
  635. for k = 1:3
  636. trainX = [trainX;allData30{otherInd1(jj,k),2}(randperm(size(allData30{otherInd1(jj,k),2},1),minSamp30),subInds);allData30{otherInd1(jj,k),1}(randperm(size(allData30{otherInd1(jj,k),1},1),minSamp30),subInds)];
  637. trainY = [trainY;ones(minSamp30,1);zeros(minSamp30,1)];
  638. end
  639. for k = 1:2
  640. trainX = [trainX;allData30{otherInd1(jj,k),2}(randperm(size(allData30{otherInd1(jj,k),2},1),minSamp30),subInds);allData30{otherInd1(jj,k),1}(randperm(size(allData30{otherInd1(jj,k),1},1),minSamp30),subInds)];
  641. trainY = [trainY;zeros(minSamp30,1);ones(minSamp30,1)];
  642. end
  643. mdl = fitglm(zscore(trainX),trainY,'Distribution','binomial');
  644. prob = predict(mdl,zscore(testX));
  645. [~,~,~,aLSDp(ii,jj,1)] = perfcurve(testY,prob,1);
  646. trainX = [];
  647. trainY = [];
  648. for k = 1:3
  649. trainX = [trainX;allData30{otherInd1(jj,k),2}(randperm(size(allData30{otherInd1(jj,k),2},1),minSamp30),subInds);allData30{otherInd1(jj,k),1}(randperm(size(allData30{otherInd1(jj,k),1},1),minSamp30),subInds)];
  650. trainY = [trainY;zeros(minSamp30,1);ones(minSamp30,1)];
  651. end
  652. for k = 1:2
  653. trainX = [trainX;allData30{otherInd1(jj,k),2}(randperm(size(allData30{otherInd1(jj,k),2},1),minSamp30),subInds);allData30{otherInd1(jj,k),1}(randperm(size(allData30{otherInd1(jj,k),1},1),minSamp30),subInds)];
  654. trainY = [trainY;ones(minSamp30,1);zeros(minSamp30,1)];
  655. end
  656. mdl = fitglm(zscore(trainX),trainY,'Distribution','binomial');
  657. prob = predict(mdl,zscore(testX));
  658. [~,~,~,aLSDp(ii,jj,2)] = perfcurve(testY,prob,1);
  659. end
  660. end
  661. % sal vs. base
  662. c = 1;
  663. for ii = 7:11
  664. testX = cat(1,allData30{ii,1}(randperm(size(allData30{ii,1},1),...
  665. minSamp30),subInds),...
  666. allData30{ii,2}(randperm(size(allData30{ii,2},1),minSamp30),subInds));
  667. testY = cat(1,zeros(minSamp30,1),ones(minSamp30,1));
  668. otherInd = logicFind(1,~ismember(7:11,ii),'==')+6;
  669. trainX = [];
  670. trainY = [];
  671. for jj = otherInd
  672. trainX = [trainX;cat(1,...
  673. allData30{jj,1}(randperm(size(allData30{jj,1},1),...
  674. minSamp30),subInds),...
  675. allData30{jj,2}(randperm(size(allData30{jj,2},1),...
  676. minSamp30),subInds))];
  677. trainY = [trainY;cat(1,zeros(minSamp30,1),ones(minSamp30,1))];
  678. end
  679. mdl = fitglm(zscore(trainX),trainY,'Distribution','binomial');
  680. prob = predict(mdl,zscore(testX));
  681. [~,~,~,aSAL(c)] = perfcurve(testY,prob,1);
  682. c = c+1;
  683. end
  684. % permuted sal vs. base
  685. for ii = 7:11
  686. testX = cat(1,allData30{ii,1}(randperm(size(allData30{ii,1},1),...
  687. minSamp30),subInds),...
  688. allData30{ii,2}(randperm(size(allData30{ii,2},1),minSamp30),...
  689. subInds));
  690. testY = cat(1,zeros(minSamp30,1),ones(minSamp30,1));
  691. otherInd = logicFind(1,~ismember(7:11,ii),'==')+6;
  692. otherInd1 = nchoosek(otherInd,2);
  693. for jj = 1:size(otherInd1,1)
  694. otherInd2(jj,:) = otherInd(~ismember(otherInd,otherInd1(jj,:)));
  695. end
  696. for jj = 1:size(otherInd1,1)
  697. trainX = [];
  698. trainY = [];
  699. for k = 1:2
  700. trainX = [trainX;allData30{otherInd1(jj,k),2}(randperm(size(allData30{otherInd1(jj,k),2},1),minSamp30),subInds);allData30{otherInd1(jj,k),1}(randperm(size(allData30{otherInd1(jj,k),1},1),minSamp30),subInds)];
  701. trainY = [trainY;zeros(minSamp30,1);ones(minSamp30,1)];
  702. end
  703. for k = 1:2
  704. trainX = [trainX;allData30{otherInd1(jj,k),2}(randperm(size(allData30{otherInd1(jj,k),2},1),minSamp30),subInds);allData30{otherInd1(jj,k),1}(randperm(size(allData30{otherInd1(jj,k),1},1),minSamp30),subInds)];
  705. trainY = [trainY;ones(minSamp30,1);zeros(minSamp30,1)];
  706. end
  707. mdl = fitglm(zscore(trainX),trainY,'Distribution','binomial');
  708. prob = predict(mdl,zscore(testX));
  709. [~,~,~,aSALp(ii,jj,1)] = perfcurve(testY,prob,1);
  710. trainX = [];
  711. trainY = [];
  712. for k = 1:2
  713. trainX = [trainX;allData30{otherInd1(jj,k),2}(randperm(size(allData30{otherInd1(jj,k),2},1),minSamp30),subInds);allData30{otherInd1(jj,k),1}(randperm(size(allData30{otherInd1(jj,k),1},1),minSamp30),subInds)];
  714. trainY = [trainY;ones(minSamp30,1);zeros(minSamp30,1)];
  715. end
  716. for k = 1:2
  717. trainX = [trainX;allData30{otherInd1(jj,k),2}(randperm(size(allData30{otherInd1(jj,k),2},1),minSamp30),subInds);allData30{otherInd1(jj,k),1}(randperm(size(allData30{otherInd1(jj,k),1},1),minSamp30),subInds)];
  718. trainY = [trainY;zeros(minSamp30,1);ones(minSamp30,1)];
  719. end
  720. mdl = fitglm(zscore(trainX),trainY,'Distribution','binomial');
  721. prob = predict(mdl,zscore(testX));
  722. [~,~,~,aSALp(ii,jj,2)] = perfcurve(testY,prob,1);
  723. end
  724. end
  725. (sum(aLSDp>mean(aLSD),[1,2,3])+1)/(numel(aLSDp)+1)
  726. % save('G:\GreenLab\data\lsd\LSDvBase_SALvBase_acute.mat','aLSD','aSAL','aLSDp','aSALp')
  727. %% Figures
  728. figure
  729. subplot(3,1,1)
  730. hold on
  731. [f,xi,bw] = ksdensity(aLSD);
  732. fill(xi,f*bw,'w')
  733. plotSpread(aLSD','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  734. plot([mean(aLSD) mean(aLSD)],[0 0.25],'-')
  735. yticks(0:0.05:0.25)
  736. xlim([0 1])
  737. ylim([0 0.25])
  738. subplot(3,1,2)
  739. hold on
  740. [f,xi,bw] = ksdensity(aSAL);
  741. fill(xi,f*bw,'w')
  742. plotSpread(aSAL','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  743. plot([mean(aSAL) mean(aSAL)],[0 0.35],'-')
  744. yticks(0:0.05:0.35)
  745. xlim([0 1])
  746. ylim([0 0.35])
  747. subplot(3,1,3)
  748. hold on
  749. [f,xi,bw] = ksdensity(reshape(aLSDp,1,120));
  750. fill(xi,f*bw,'w')
  751. plotSpread(reshape(aLSDp,1,120)','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  752. plot([mean(reshape(aLSDp,1,120)) mean(reshape(aLSDp,1,120))],[0 0.25],'-')
  753. yticks(0:0.05:0.25)
  754. xlim([0 1])
  755. ylim([0 0.25])
  756. %% LSD v SAL - Log LOO - using last 30 minutes of SAL and LSD data
  757. load('G:\GreenLab\data\lsd\normAcuteImag30.mat')
  758. x = []; y = []; a = [];
  759. xP = []; yP = []; aP = [];
  760. xS = []; yS = []; aS = [];
  761. subInds = [1:6,37:42,19:24,43:48,79:84,61:66,85:90,169:174,211:216,175:180];
  762. count = 1;
  763. c = 1;
  764. lsdN = size(allPostLSD30,2);
  765. salN = size(allPostSal30,2);
  766. for ii = 1:lsdN
  767. for jj = 1:salN
  768. inds(c,:) = [ii,jj];
  769. c = c+1;
  770. end
  771. end
  772. for n = 1:30
  773. disp(n)
  774. % LSD data
  775. trainLSD = logicFind(1,~ismember(1:lsdN,inds(n,1)),'==');
  776. testLSD = logicFind(0,~ismember(1:lsdN,inds(n,1)),'==');
  777. thisLSD24 = [];
  778. for ii = trainLSD
  779. rng(ii*count)
  780. thisLSD24 = [thisLSD24;allPostLSD30z{ii}(randperm(size(allPostLSD30z{ii},1),...
  781. minSamp30),:)];
  782. end
  783. lsdTest = allPostLSD30z{testLSD}(randperm(size(allPostLSD30z{testLSD},1),...
  784. minSamp30),:);
  785. % Saline data
  786. trainSal = logicFind(1,~ismember(1:salN,inds(n,2)),'==');
  787. testSal = logicFind(0,~ismember(1:salN,inds(n,2)),'==');
  788. thisSal = [];
  789. for ii = 1:numel(allPostSal30z)
  790. rng((ii+numel(allPostLSD30z))*count)
  791. thisSal = [thisSal;allPostSal30z{ii}(randperm(size(allPostSal30z{ii},1),...
  792. minSamp30),:)];
  793. end
  794. salTest = allPostSal30z{testSal}(randperm(size(allPostSal30z{testSal},1),...
  795. minSamp30),:);
  796. % Combine and build models
  797. trainX = [thisLSD24;thisSal];
  798. trainY = [ones(size(thisLSD24,1),1);zeros(size(thisSal,1),1)];
  799. testX = [lsdTest;salTest];
  800. testY = [ones(minSamp30,1);zeros(minSamp30,1)];
  801. mdl = fitglm(zscore(trainX(:,subInds)),trainY,'distribution','binomial','binomialSize',numel(trainY));
  802. prob = predict(mdl,zscore(testX(:,subInds)));
  803. [x(count,:),y(count,:),~,a(count)] = perfcurve(testY,prob,1);
  804. beta(:,:,count) = table2array(mdl.Coefficients);
  805. % Single feature models
  806. for jj = 1:60
  807. theseTrain = trainX(:,subInds);
  808. theseTest = testX(:,subInds);
  809. mdl = fitglm(zscore(theseTrain(:,jj)),trainY,'distribution','binomial','binomialSize',numel(trainY));
  810. acuteBeta(jj,count,:) = table2array(mdl.Coefficients(2,:));
  811. prob = predict(mdl,zscore(theseTest(:,jj)));
  812. [xS(count,jj,:),yS(count,jj,:),~,aS(count,jj)] = perfcurve(testY,prob,1);
  813. [xSP(count,jj,:),ySP(count,jj,:),~,aSP(count,jj)] = perfcurve(testY(randperm(numel(testY),numel(testY))),prob,1);
  814. end
  815. count = count+1;
  816. end
  817. % Permuted
  818. lsdInds = nchoosek(1:6,3);
  819. salInds = nchoosek(1:5,3);
  820. c = 1;
  821. for ii = 1:size(lsdInds,1)
  822. for jj = 1:size(salInds,1)
  823. permInds(c,:) = cat(2,lsdInds(ii,:),salInds(jj,:));
  824. c = c+1;
  825. end
  826. end
  827. thisLSD = cell(1,numel(allPostLSD30z));
  828. thisSal = cell(1,numel(allPostSal30z));
  829. for ii = 1:size(permInds,1)
  830. disp(ii)
  831. lsdInd = permInds(ii,randperm(3,3));
  832. salInd = permInds(ii,randperm(3,3)+3);
  833. lsdOtherInd = logicFind(1,~ismember(1:6,lsdInd),'==');
  834. salOtherInd = logicFind(1,~ismember(1:5,salInd),'==');
  835. for jj = 1:numel(allPostLSD30z)
  836. thisLSD{jj} = allPostLSD30z{jj}(randperm(size(allPostLSD30z{jj},1),...
  837. minSamp30),:);
  838. end
  839. for jj = 1:numel(allPostSal30z)
  840. thisSal{jj} = allPostSal30z{jj}(randperm(size(allPostSal30z{jj},1),...
  841. minSamp30),:);
  842. end
  843. testX = cat(1,thisLSD{lsdInd(1)},thisSal{salInd(1)});
  844. testY = cat(1,ones(minSamp30,1),zeros(minSamp30,1));
  845. trainX = cat(1,thisLSD{lsdInd(2:3)},thisSal{salInd(2:3)},thisLSD{lsdOtherInd},thisSal{salOtherInd});
  846. trainY = cat(1,ones(minSamp30*4,1),zeros(minSamp30*5,1));
  847. mdl = fitglm(zscore(trainX(:,subInds)),trainY,'Distribution','binomial');
  848. prob = predict(mdl,zscore(testX(:,subInds)));
  849. [xP(ii,:),yP(ii,:),~,aP(ii)] = perfcurve(testY,prob,1);
  850. for jj = 1:numel(subInds)
  851. mdl = fitglm(zscore(trainX(:,subInds(jj))),trainY,'Distribution','binomial');
  852. prob = predict(mdl,testX(:,subInds(jj)));
  853. [~,~,~,aSP(ii,jj)] = perfcurve(testY,prob,1);
  854. end
  855. end
  856. %%
  857. feat = names({'ILL','CA1L','PLL','NAcL','PLR','CA1R','ILR','NAcR'},...
  858. {'d','t','a','b','lg','hg'});
  859. feat = feat(subInds);
  860. %
  861. figure
  862. plot(mean(acuteBeta(:,:,1),2),mean(aS,1),'.k')
  863. xlabel('mean beta')
  864. ylabel('mean auc')
  865. title('single feature')
  866. %
  867. figure
  868. subplot(1,2,1)
  869. plot(mean(acuteBeta(:,:,1),2),mean(acuteBeta(:,:,4),2),'.k')
  870. xlabel('mean beta'); ylabel('mean p')
  871. title('single feature')
  872. subplot(1,2,2)
  873. plot(mean(beta(2:61,1,:),3),mean(beta(2:61,4,:),3),'.k')
  874. xlabel('mean beta'); ylabel('mean p')
  875. title('full')
  876. %
  877. figure
  878. plot(mean(acuteBeta(:,:,1),2),mean(beta(2:61,1,:),3),'.k')
  879. xlabel('single feature beta'); ylabel('full feature beta')
  880. % save('acuteLSDvSalineLogLOOImag30z.mat','a','aP','aS','aSP','acuteBeta',...
  881. % 'beta','x','xP','xS','xSP','y','yP','yS','yP')
  882. %% Plot Log LOO
  883. figure
  884. hold on
  885. plot(mean(x,1),mean(y,1),'-k')
  886. plot(mean(xP,1),mean(yP,1),'--k')
  887. xlabel('FPR'); ylabel('TPR')
  888. set(gca,'xtick',0:0.5:1,'ytick',0:0.5:1)
  889. title('LSD v. Saline Acute: Log LOO')
  890. legend({['Real: ',num2str(round(mean(a),2)),'\pm',...
  891. num2str(round(conf(a,0.95),2))],['Permuted: ',...
  892. num2str(round(mean(aP),2)),'\pm',num2str(round(conf(aP,0.95),2))]},...
  893. 'location','se')
  894. figure
  895. subplot(2,1,1)
  896. hold on
  897. [f,xi,bw] = ksdensity(a);
  898. fill(xi,f*bw,'w')
  899. plotSpread(a','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  900. plot([mean(a) mean(a)],[0 0.25],'-')
  901. yticks(0:0.05:0.25)
  902. xlim([0 1])
  903. ylim([0 0.25])
  904. subplot(2,1,2)
  905. hold on
  906. [f,xi,bw] = ksdensity(aP);
  907. fill(xi,f*bw,'w')
  908. plotSpread(aP','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  909. plot([mean(aP) mean(aP)],[0 0.2],'-')
  910. yticks(0:0.05:0.2)
  911. xlim([0 1])
  912. ylim([0 0.2])
  913. %% Single feature analysis - grid
  914. mASingle = mean(aS,1).*sign(mean(acuteBeta(:,:,1),2))';
  915. [~,sortInd] = sort(abs(mASingle),'descend');
  916. mASingleSort = mASingle(sortInd)';
  917. sortFeat = feat(sortInd)';
  918. for ii = 1:numel(mASingle)
  919. p(ii) = (1+sum(aSP(:,ii)>=mean(aS(:,ii))))/(1+size(aSP,1));
  920. end
  921. pAdj = p.*60;
  922. mAS(pAdj>=0.05) = NaN;
  923. pow = reshape(mAS(1:24),6,4);
  924. coh = reshape(mAS(25:end),6,6);
  925. figure
  926. pcolor(padarray(pow,[1 1],NaN,'post')')
  927. colormap('viridis')
  928. % caxis([-1 1])
  929. set(gca,'xtick',1.5:6.5,'xticklabel',...
  930. {'\delta','\theta','\alpha','\beta','l\gamma','h\gamma'},...
  931. 'ytick',1.5:4.5,'yticklabel',...
  932. {'ILl','ILr','NAl','NAr'})
  933. figure
  934. pcolor(padarray(coh,[1 1],NaN,'post')')
  935. colormap('viridis')
  936. % caxis([-1 1])
  937. set(gca,'xtick',1.5:6.5,'xticklabel',...
  938. {'\delta','\theta','\alpha','\beta','l\gamma','h\gamma'},...
  939. 'ytick',1.5:6.5,'yticklabel',...
  940. {'ILl-ILr','ILl-NAl','ILl-NAr','NAl-ILr','ILr-NAr','NAl-NAr'})
  941. %% Single feature peformance
  942. subInds = [1:6,37:42,19:24,43:48,79:84,61:66,85:90,169:174,211:216,175:180];
  943. feat = names({'ILL','CA1L','PLL','NAcL','PLR','CA1R','ILR','NAcR'},...
  944. {'d','t','a','b','lg','hg'});
  945. feat = feat(subInds);
  946. figure
  947. hold on
  948. for ii = 1:60
  949. plotSpread(aS(:,ii),'xValues',ii-0.25,'binWidth',0.1)
  950. plot(ii,mean(aS(:,ii)),'.k')
  951. plotSpread(aSP(:,ii),'binWidth',0.1,'xValues',ii+0.25,'distributionColors','r')
  952. plot(ii,mean(aSP(:,ii)),'.k')
  953. end
  954. set(gca,'xtick',1:60,'xticklabel',feat)
  955. xtickangle(45)
  956. %% Plot best and worst features
  957. load('normAcuteImag30.mat')
  958. load('acuteLSDvSalineLogLOOImag30.mat')
  959. subInds = [1:6,37:42,19:24,43:48,79:84,61:66,85:90,169:174,211:216,175:180];
  960. feat = names({'ILL','CA1L','PLL','NAcL','PLR','CA1R','ILR','NAcR'},...
  961. {'d','t','a','b','lg','hg'});
  962. feat = feat(subInds);
  963. mASingle = mean(aS,1).*sign(mean(acuteBeta(:,:,1),2))';
  964. [~,sortInd] = sort(abs(mASingle),'descend');
  965. mASingleSort = mASingle(sortInd)';
  966. sortFeat = feat(sortInd)';
  967. allLSD = cat(1,allPostLSD30{:});
  968. allLSD = allLSD(:,subInds);
  969. allSAL = cat(1,allPostSal30{:});
  970. allSAL = allSAL(:,subInds);
  971. mSAL = cellfun(@(x) mean(x,1),allPostSal30,'UniformOutput',false);
  972. mSAL = cat(1,mSAL{:});
  973. mLSD = cellfun(@(x) mean(x,1),allPostLSD30,'UniformOutput',false);
  974. mLSD = cat(1,mLSD{:});
  975. %%
  976. figure
  977. subplot(2,2,1)
  978. hold on
  979. plotSpread(allSAL(:,2),'distributionColors','b','xvalues',1,'binWidth',.001);
  980. plotSpread(mSAL(:,2),'distributionColors','#00ffff','xvalues',1,'binWidth',0.25)
  981. plotSpread(allLSD(:,2),'distributionColors','r','xvalues',2,'binWidth',.001)
  982. p = plotSpread(mLSD(:,2),'distributionColors','#ff9cff','xvalues',2,'binWidth',0.25);
  983. % ylim([-75 175])
  984. xlim([0.5 2.5])
  985. subplot(2,2,2)
  986. hold on
  987. plot(squeeze(mean(xS(:,2,:),1)),squeeze(mean(yS(:,2,:),1)),'k')
  988. plot(squeeze(mean(xSP(:,2,:),1)),squeeze(mean(yP,1)),'k--')
  989. subplot(2,2,3)
  990. hold on
  991. plotSpread(allSAL(:,24),'distributionColors','b','xvalues',1,'binWidth',0.001)
  992. plotSpread(mSAL(:,24),'distributionColors','#00ffff','xvalues',1,'binWidth',0.5)
  993. plotSpread(allLSD(:,24),'distributionColors','r','xvalues',2,'binWidth',0.001)
  994. plotSpread(mLSD(:,24),'distributionColors','#ff9cff','xvalues',2,'binWidth',0.5)
  995. xlim([0.5 2.5])
  996. % ylim([-200 400])
  997. subplot(2,2,4)
  998. hold on
  999. plot(squeeze(mean(xS(:,24,:),1)),squeeze(mean(yS(:,24,:),1)),'k')
  1000. plot(squeeze(mean(xSP(:,24,:),1)),squeeze(mean(yP,1)),'k--')
  1001. %% Plot all features
  1002. % load('normAcuteImag30.mat')
  1003. subInds = [1:6,37:42,19:24,43:48,79:84,61:66,85:90,169:174,211:216,175:180];
  1004. feat = names({'ILL','CA1L','PLL','NAcL','PLR','CA1R','ILR','NAcR'},...
  1005. {'d','t','a','b','lg','hg'});
  1006. feat = feat(subInds);
  1007. allSAL = cat(1,allPostSal30{:});
  1008. allLSD = cat(1,allPostLSD30{:});
  1009. these = cell(1);
  1010. for ii = subInds
  1011. these = {these{:},allSAL(:,ii),allLSD(:,ii)};
  1012. end
  1013. %%
  1014. figure
  1015. subplot(2,2,1)
  1016. violin(these(:,2:13));
  1017. ylim([-250 375])
  1018. subplot(2,2,2)
  1019. violin(these(:,14:25));
  1020. ylim([-250 375])
  1021. subplot(2,2,3)
  1022. violin(these(:,26:37));
  1023. ylim([-250 375])
  1024. subplot(2,2,4)
  1025. violin(these(:,38:49));
  1026. ylim([-250 375])
  1027. %%
  1028. figure
  1029. subplot(3,2,1)
  1030. violin(these(:,50:61));
  1031. set(gca,'xtick',1:2:12,'xticklabel',feat(25:30))
  1032. ylim([-1 1])
  1033. subplot(3,2,2)
  1034. violin(these(:,62:73));
  1035. set(gca,'xtick',1:2:12,'xticklabel',feat(31:36))
  1036. ylim([-1 1])
  1037. subplot(3,2,3)
  1038. violin(these(:,74:85));
  1039. set(gca,'xtick',1:2:12,'xticklabel',feat(37:42))
  1040. ylim([-1 1])
  1041. subplot(3,2,4)
  1042. violin(these(:,86:97));
  1043. set(gca,'xtick',1:2:12,'xticklabel',feat(43:48))
  1044. ylim([-1 1])
  1045. subplot(3,2,5)
  1046. violin(these(:,98:109));
  1047. set(gca,'xtick',1:2:12,'xticklabel',feat(49:54))
  1048. ylim([-1 1])
  1049. subplot(3,2,6)
  1050. violin(these(:,110:121));
  1051. set(gca,'xtick',1:2:12,'xticklabel',feat(55:60))
  1052. ylim([-1 1])
  1053. %%
  1054. figure
  1055. subplot(2,1,1)
  1056. violin(allSAL(:,subInds(1:24)),'facecolor','b');
  1057. set(gca,'xtick',1:24,'xticklabel',feat(1:24))
  1058. ylim([-200 375])
  1059. subplot(2,1,2)
  1060. violin(allLSD(:,subInds(1:24)),'facecolor','r');
  1061. set(gca,'xtick',1:24,'xticklabel',feat(1:24))
  1062. ylim([-200 375])
  1063. figure
  1064. subplot(2,1,1)
  1065. violin(allSAL(:,subInds(25:end)),'facecolor','b');
  1066. set(gca,'xtick',1:36,'xticklabel',feat(25:36))
  1067. ylim([-1 1])
  1068. subplot(2,1,2)
  1069. violin(allLSD(:,subInds(25:end)),'facecolor','r');
  1070. set(gca,'xtick',1:36,'xticklabel',feat(25:36))
  1071. ylim([-1 1])
  1072. %%
  1073. figure
  1074. c = 1;
  1075. for ii = 1:24
  1076. hold on
  1077. plotSpread(allSAL(:,subInds(ii)),'binWidth',0.001,...
  1078. 'xvalues',c,'distributionColors','b')
  1079. [f,xi,bw] = ksdensity(allSAL(:,subInds(ii)));
  1080. plot((f*bw)*4.5+c,xi,'k','lineWidth',2)
  1081. c = c+1;
  1082. plotSpread(allLSD(:,subInds(ii)),'binWidth',0.001,...
  1083. 'xvalues',c,'distributionColors','r');
  1084. [f,xi,bw] = ksdensity(allLSD(:,subInds(ii)));
  1085. plot((f*bw)*4.5+c,xi,'k','lineWidth',2)
  1086. c = c+1;
  1087. end
  1088. set(gca,'xtick',1.5:2:48,'xticklabel',feat)
  1089. figure
  1090. c = 1;
  1091. for ii = 25:60
  1092. hold on
  1093. plotSpread(allSAL(:,subInds(ii)),'binWidth',0.001,...
  1094. 'xvalues',c,'distributionColors','b')
  1095. [f,xi,bw] = ksdensity(allSAL(:,subInds(ii)));
  1096. plot((f*bw)*4.5+c,xi,'k','lineWidth',2)
  1097. c = c+1;
  1098. plotSpread(allLSD(:,subInds(ii)),'binWidth',0.001,...
  1099. 'xvalues',c,'distributionColors','r');
  1100. [f,xi,bw] = ksdensity(allLSD(:,subInds(ii)));
  1101. plot((f*bw)*4.5+c,xi,'k','lineWidth',2)
  1102. c = c+1;
  1103. end
  1104. set(gca,'xtick',1.5:2:72,'xticklabel',feat(25:60))
  1105. %% Single feature analysis - bar plot
  1106. subInds = [1:6,37:42,19:24,43:48,79:84,61:66,85:90,169:174,211:216,175:180];
  1107. mAS = mean(aS,1);
  1108. for ii = 1:60
  1109. [~,p(ii)] = ttest2(aS(:,ii),aSP(:,ii));
  1110. end
  1111. pAdj = p*60;
  1112. feat = names({'ILL','CA1L','PLL','NAcL','PLR','CA1R','ILR','NAcR'},...
  1113. {'d','t','a','b','lg','hg'});
  1114. feat = feat(subInds);
  1115. sigFeat = feat(pAdj<=0.05);
  1116. pow = sum(pAdj(1:24)<=0.05);
  1117. coh = sum(pAdj(25:end)<=0.05);
  1118. for jj = 1:6
  1119. powFreq(jj) = sum(pAdj(1+(jj-1):6:24)<=0.05);
  1120. cohFreq(jj) = sum(pAdj(25+(jj-1):6:end)<=0.05);
  1121. % powFreq(jj) = sum(mAS(1+(jj-1):6:48)>=0.6);
  1122. % cohFreq(jj) = sum(pAdj(49+(jj-1):6:end)>=0.6);
  1123. end
  1124. figure
  1125. x = categorical({'\delta','\theta','\alpha','\beta','l\gamma','h\gamma'});
  1126. x = reordercats(x,{'\delta','\theta','\alpha','\beta','l\gamma','h\gamma'});
  1127. bar(x,[powFreq;cohFreq]','stacked')
  1128. box off
  1129. title('Acute features by frequency')
  1130. legend({'Power','Coherence'})
  1131. %% Frequency networks
  1132. chan = 4;
  1133. sites = {'ILL','ILR','NAcL','NAcR'};
  1134. powInd = chan*6;
  1135. cmbs = nchoosek(1:chan,2);
  1136. freqs = {'d','t','a','b','lg','hg'};
  1137. thisAS = mean(aS,1).*sign(mean(acuteBeta,2))';
  1138. [~,p] = ttest2(aS,aSP);
  1139. pAdj = p.*60;
  1140. for jj = 1:6
  1141. adjMat = [];
  1142. count = powInd+jj;
  1143. for ii = 1:size(cmbs,1)
  1144. % Make sure beta coherence is significant
  1145. if pAdj(count) <= 0.05
  1146. adjMat(cmbs(ii,1),cmbs(ii,2)) = thisAS(count);
  1147. else
  1148. adjMat(cmbs(ii,1),cmbs(ii,2)) = 0;
  1149. end
  1150. count = count+6;
  1151. end
  1152. adjMat = [adjMat;zeros(1,chan)];
  1153. % Find scaling factor based on minimum
  1154. s = 1-min(min(abs(adjMat(adjMat~=0))));
  1155. % Compute graph with scale factor
  1156. g = graph(adjMat,sites,'upper');
  1157. if chan == 8
  1158. % Set up node coordinates based on regular octagon
  1159. x = [1,sqrt(2)/2,0,-sqrt(2)/2,-1,-sqrt(2)/2,0,sqrt(2)/2];
  1160. y = [0,sqrt(2)/2,1,sqrt(2)/2,0,-sqrt(2)/2,-1,-sqrt(2)/2];
  1161. elseif chan == 4
  1162. % Square
  1163. x = [1,1,-1,-1];
  1164. y = [1,-1,1,-1];
  1165. end
  1166. % Set edge color based on edge sign (+ = red; - = blue)
  1167. edgeSign = g.Edges.Weight>=0;
  1168. edgeColor = zeros(numel(edgeSign),3);
  1169. edgeColor(logicFind(1,edgeSign,'=='),:) = repmat([1 0 0],sum(edgeSign),1);
  1170. edgeColor(logicFind(0,edgeSign,'=='),:) = repmat([0 0 1],sum(~edgeSign),1);
  1171. % Set node color based on node sign (+ = red; - = blue)
  1172. nodeSign = thisAS(jj:6:powInd)>=0;
  1173. nodeColor = zeros(numel(nodeSign),3);
  1174. nodeColor(logicFind(1,nodeSign,'=='),:) = repmat([1 0 0],sum(nodeSign),1);
  1175. nodeColor(logicFind(0,nodeSign,'=='),:) = repmat([0 0 1],sum(~nodeSign),1);
  1176. % Check node significance
  1177. sigNode = pAdj(jj:6:powInd) <= 0.05;
  1178. % If not significant, set to white and size 10
  1179. nodeColor(~sigNode,:) = repmat([1 1 1],sum(~sigNode),1);
  1180. theseNodes = abs(thisAS(jj:6:powInd));
  1181. theseNodes(~sigNode) = 10;
  1182. % Plot
  1183. figure('position',[895,400,667,571])
  1184. h = plot(g,'XData',x,'YData',y,'markersize',theseNodes,'linewidth',abs(g.Edges.Weight)+s,'edgecolor',edgeColor,'nodecolor',nodeColor);
  1185. title(freqs{jj})
  1186. axis off
  1187. % print(['LSD',freqs{jj},'.eps'],'-dwinc','-painters')
  1188. end
  1189. %% 24 hour effects: LSD vs. Saline
  1190. [data,samp,files] = collateData(['F:\lsd\processed\imag24Hr\'],{'lsd';...
  1191. 'sal'},{'pow','coh'},'trl','');
  1192. lsd24 = data{1,1};
  1193. sal24 = data{1,2};
  1194. minSamp = min([cellfun(@(x) size(x,1),lsd24);...
  1195. cellfun(@(x) size(x,1),sal24)]);
  1196. save('F:\lsd\LSDvSal24HrImag.mat','lsd24','sal24','minSamp')
  1197. %% Modeling under run24HrLSDvSaline.m
  1198. % cd G:\GreenLab\data\lsd\LSDvSal24Hr\
  1199. % x24 = []; y24 = [];
  1200. % x24R = []; y24R = [];
  1201. % for ii = 1:100
  1202. % load(['LSDvSal24hr_',num2str(ii),'.mat'],'acc','accR','hist','histR')
  1203. % [x24(ii,:),y24(ii,:),~,allA24(ii)] = perfcurve(hist.cfg.naive.testY,...
  1204. % acc{1}.pred,1,'TVals',linspace(0,1,numel(acc{1}.pred)),...
  1205. % 'UseNearest',0);
  1206. % [x24R(ii,:),y24R(ii,:),~,allA24R(ii)] = perfcurve(...
  1207. % histR.cfg.naive.testY,accR{1}.pred,1,'TVals',...
  1208. % linspace(0,1,numel(accR{1}.pred)),'UseNearest',0);
  1209. % end
  1210. %% base vs. 24 hours later (LSD and saline separate)
  1211. [baseData,~,baseFiles] = collateData(['G:\Greenlab\data\lsd\processed\',...
  1212. 'imagWater\'],{'lsd';'sal'},{'pow','coh'},'trl','rel');
  1213. [preData,~,preFiles] = collateData(['G:\Greenlab\data\lsd\processed\',...
  1214. 'imagAcute\'],{'lsd';'sal'},{'pow','coh'},'trl','rel');
  1215. [postData,~,postFiles] = collateData(['G:\Greenlab\data\lsd\processed\',...
  1216. 'imag24HR\'],{'lsd';'sal'},{'pow','coh'},'trl','rel');
  1217. % Combine data from water (baseData) and pre injection (preData)
  1218. ids = {'_26_','_29_','_30_','_37_','_38_','_39_','_80_','_81_','_82_',...
  1219. '_88_','_90_'};
  1220. allBaseData = cell(numel(ids),1);
  1221. for ii = 1:numel(ids)
  1222. for jj = 1:2
  1223. inds = logicFind(1,contains(baseFiles{jj},ids{ii}),'==');
  1224. if ~isempty(inds)
  1225. allBaseData{ii} = cat(1,allBaseData{ii},baseData{jj}{inds});
  1226. end
  1227. inds = logicFind(1,contains(preFiles{jj},ids{ii}),'==');
  1228. if ~isempty(inds)
  1229. allBaseData{ii} = cat(1,allBaseData{ii},preData{jj}{inds,1});
  1230. end
  1231. end
  1232. end
  1233. % get post data
  1234. allPostData = cell(numel(ids),1);
  1235. for ii = 1:numel(ids)
  1236. for jj = 1:2
  1237. inds = logicFind(1,contains(postFiles{jj},ids{ii}),'==');
  1238. if ~isempty(inds)
  1239. allPostData{ii} = cat(1,allPostData{ii},postData{jj}{inds});
  1240. end
  1241. inds = logicFind(1,contains(postFiles{jj},ids{ii}),'==');
  1242. if ~isempty(inds)
  1243. allPostData{ii} = cat(1,allPostData{ii},postData{jj}{inds,1});
  1244. end
  1245. end
  1246. end
  1247. group = [0,1,0,0,0,1,1,0,1,1,1];
  1248. LSD = logicFind(1,group,'==');
  1249. SAL = logicFind(0,group,'==');
  1250. % save('G:\GreenLab\data\lsd\preVpost.mat','allBaseData','allPostData','ids')
  1251. %% LSD pre vs. post - 20 iterations of each animal left out
  1252. for n = 1:20
  1253. disp(n)
  1254. thisLSDpre = cell(numel(LSD),1);
  1255. thisLSDpost = cell(numel(LSD),1);
  1256. for ii = 1:numel(LSD)
  1257. thisLSDpre{ii} = allBaseData{LSD(ii)}(randperm(size(...
  1258. allBaseData{LSD(ii)},1),900),:);
  1259. thisLSDpost{ii} = allPostData{LSD(ii)}(randperm(size(...
  1260. allPostData{LSD(ii)},1),900),:);
  1261. end
  1262. for jj = 1:numel(LSD)
  1263. theseInds = 1:numel(LSD);
  1264. trainX = cat(1,thisLSDpre{theseInds(~ismember(theseInds,jj))},...
  1265. thisLSDpost{theseInds(~ismember(theseInds,jj))});
  1266. trainY = cat(1,zeros(900*5,1),ones(900*5,1));
  1267. testX = cat(1,thisLSDpre{theseInds(jj)},thisLSDpost{theseInds(jj)});
  1268. testY = cat(1,zeros(900,1),ones(900,1));
  1269. mdl = fitglm(zscore(trainX(:,subInds)),trainY,...
  1270. 'distribution','binomial');
  1271. pred = predict(mdl,zscore(testX(:,subInds)));
  1272. [x,y,~,aLSD(n,jj)] = perfcurve(testY,pred,1);
  1273. xLSD(n,jj,:) = interp1(linspace(0,1,numel(x)),x,...
  1274. linspace(0,1,1800));
  1275. yLSD(n,jj,:) = interp1(linspace(0,1,numel(y)),y,...
  1276. linspace(0,1,1800));
  1277. end
  1278. end
  1279. %% permuted LSD pre vs. post
  1280. subInds = [1:6,37:42,19:24,43:48,79:84,61:66,85:90,169:174,211:216,175:180];
  1281. for ii = 1:6
  1282. disp(ii)
  1283. testX = cat(1,allBaseData{ii,1}(randperm(size(allBaseData{ii,1},1),...
  1284. 900),subInds),...
  1285. allPostData{ii}(randperm(size(allPostData{ii},1),900),...
  1286. subInds));
  1287. testY = cat(1,zeros(900,1),ones(900,1));
  1288. otherInd = logicFind(1,~ismember(1:6,ii),'==');
  1289. otherInd1 = nchoosek(otherInd,3);
  1290. for jj = 1:size(otherInd1,1)
  1291. otherInd2(jj,:) = otherInd(~ismember(otherInd,otherInd1(jj,:)));
  1292. end
  1293. for jj = 1:size(otherInd1,1)
  1294. trainX = [];
  1295. trainY = [];
  1296. for k = 1:3
  1297. trainX = [trainX;...
  1298. allPostData{otherInd1(jj,k)}(randperm(size(allPostData{otherInd1(jj,k)},1),900),subInds);...
  1299. allBaseData{otherInd1(jj,k),1}(randperm(size(allBaseData{otherInd1(jj,k),1},1),900),subInds)];
  1300. trainY = [trainY;ones(900,1);zeros(900,1)];
  1301. end
  1302. for k = 1:2
  1303. trainX = [trainX;...
  1304. allPostData{otherInd1(jj,k)}(randperm(size(allPostData{otherInd1(jj,k)},1),900),subInds);...
  1305. allBaseData{otherInd1(jj,k)}(randperm(size(allBaseData{otherInd1(jj,k)},1),900),subInds)];
  1306. trainY = [trainY;zeros(900,1);ones(900,1)];
  1307. end
  1308. mdl = fitglm(zscore(trainX),trainY,'Distribution','binomial');
  1309. prob = predict(mdl,zscore(testX));
  1310. [~,~,~,aLSDp(ii,jj,1)] = perfcurve(testY,prob,1);
  1311. trainX = [];
  1312. trainY = [];
  1313. for k = 1:3
  1314. trainX = [trainX;...
  1315. allPostData{otherInd1(jj,k)}(randperm(size(allPostData{otherInd1(jj,k)},1),900),subInds);...
  1316. allBaseData{otherInd1(jj,k)}(randperm(size(allBaseData{otherInd1(jj,k)},1),900),subInds)];
  1317. trainY = [trainY;zeros(900,1);ones(900,1)];
  1318. end
  1319. for k = 1:2
  1320. trainX = [trainX;...
  1321. allPostData{otherInd1(jj,k)}(randperm(size(allPostData{otherInd1(jj,k)},1),900),subInds);...
  1322. allBaseData{otherInd1(jj,k)}(randperm(size(allBaseData{otherInd1(jj,k)},1),900),subInds)];
  1323. trainY = [trainY;ones(900,1);zeros(900,1)];
  1324. end
  1325. mdl = fitglm(zscore(trainX),trainY,'Distribution','binomial');
  1326. prob = predict(mdl,zscore(testX));
  1327. [~,~,~,aLSDp(ii,jj,2)] = perfcurve(testY,prob,1);
  1328. end
  1329. end
  1330. %% SAL pre vs. post - 20 iterations of each animal left out
  1331. for n = 1:20
  1332. for jj = 1:numel(SAL)
  1333. thisSALpre = cell(numel(SAL),1);
  1334. thisSALpost = cell(numel(SAL),1);
  1335. for ii = 1:numel(SAL)
  1336. thisSALpre{ii} = allBaseData{SAL(ii)}(randperm(size(...
  1337. allBaseData{SAL(ii)},1),900),:);
  1338. thisSALpost{ii} = allPostData{SAL(ii)}(randperm(size(...
  1339. allPostData{SAL(ii)},1),900),:);
  1340. end
  1341. theseInds = 1:numel(SAL);
  1342. trainX = cat(1,thisSALpre{theseInds(~ismember(theseInds,jj))},...
  1343. thisSALpost{theseInds(~ismember(theseInds,jj))});
  1344. trainY = cat(1,zeros(900*4,1),ones(900*4,1));
  1345. testX = cat(1,thisSALpre{theseInds(jj)},thisSALpost{theseInds(jj)});
  1346. testY = cat(1,zeros(900,1),ones(900,1));
  1347. mdl = fitglm(zscore(trainX),trainY,'distribution','binomial');
  1348. pred = predict(mdl,zscore(testX));
  1349. [x,y,~,aSAL(n,jj)] = perfcurve(testY,pred,1);
  1350. xSAL(n,jj,:) = interp1(linspace(0,1,numel(x)),x,...
  1351. linspace(0,1,1800));
  1352. ySAL(n,jj,:) = interp1(linspace(0,1,numel(y)),y,...
  1353. linspace(0,1,1800));
  1354. end
  1355. end
  1356. % save('G:\GreenLab\data\lsd\preVpostModels.mat','aLSD','xLSD','yLSD',...
  1357. % 'aSAL','xSAL','ySAL','aLSDp')
  1358. %%
  1359. figure
  1360. subplot(3,1,1)
  1361. hold on
  1362. [f,xi,bw] = ksdensity(mean(aLSD,1));
  1363. fill(xi,f*bw,'w')
  1364. plotSpread(mean(aLSD,1)','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  1365. plot([mean(aLSD,[1,2]) mean(aLSD,[1,2])],[0 0.25],'-')
  1366. yticks(0:0.05:0.25)
  1367. xlim([0 1])
  1368. ylim([0 0.25])
  1369. subplot(3,1,2)
  1370. hold on
  1371. [f,xi,bw] = ksdensity(reshape(aLSDp,1,120));
  1372. fill(xi,f*bw,'w')
  1373. plotSpread(reshape(aLSDp,1,120)','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  1374. plot([mean(reshape(aLSDp,1,120)) mean(reshape(aLSDp,1,120))],[0 0.25],'-')
  1375. yticks(0:0.05:0.25)
  1376. xlim([0 1])
  1377. ylim([0 0.25])
  1378. subplot(3,1,3)
  1379. hold on
  1380. [f,xi,bw] = ksdensity(mean(aSAL,1));
  1381. fill(xi,f*bw,'w')
  1382. plotSpread(mean(aSAL,1)','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  1383. plot([mean(aSAL,[1,2]) mean(aSAL,[1,2])],[0 0.25],'-')
  1384. yticks(0:0.05:0.25)
  1385. xlim([0 1])
  1386. ylim([0 0.25])
  1387. %% 24 hour effects: drug vs. baseline days (LSD and saline separate)
  1388. % grab baseline files (using data with no behavior from water recordings)
  1389. load('F:\lsd\LSDvSal24HrImag.mat')
  1390. load('F:\lsd\normAcuteImag.mat','data')
  1391. % Load data in the same order as lsd24 and sal24 from above
  1392. [baseLSDData,baseLSDSamp,baseLSDFiles] = collateData(...
  1393. 'F:\lsd\processed\imagWater\',{'29','39','80','82','88','90'},...
  1394. {'pow','coh'},'trl','');
  1395. ids = [29,39,80,82,88,90];
  1396. for ii = 1:numel(ids)
  1397. inds = ~cellfun(@isempty,strfind(baseLSDFiles{1},['Water_',...
  1398. num2str(ids(ii))]));
  1399. allLSDBase{ii} = cat(1,baseLSDData{1}{inds},data{1}{ii,1});
  1400. lsdDiff{ii} = lsd24{ii}-mean(allLSDBase{ii},1);
  1401. end
  1402. [baseSalData,baseSalSamp,baseSalFiles] = collateData(...
  1403. 'F:\lsd\processed\imagWater\',{'26';'30';'37';'38';'81'},...
  1404. {'pow','coh'},'trl','');
  1405. for ii = 1:numel(baseSalData)
  1406. allSalBase{ii} = cat(1,baseSalData{ii}{:},data{2}{ii,1});
  1407. salDiff{ii} = sal24{ii}-mean(allSalBase{ii},1);
  1408. end
  1409. save('F:\lsd\lsdSal24Hr-BaseImag.mat','allLSDBase','allSalBase',...
  1410. 'baseLSDFiles','baseSalFiles','lsdDiff','salDiff','minSamp');
  1411. %% Base-LSD vs. Base-Sal
  1412. load 'G:\GreenLab\data\lsd\lsdSal24Hr-BaseImag.mat'
  1413. c = 1; inds = [];
  1414. for ii = 1:6
  1415. for jj = 1:5
  1416. inds(c,:) = [ii,jj];
  1417. c = c+1;
  1418. end
  1419. end
  1420. salN = numel(salDiff);
  1421. lsdN = numel(lsdDiff);
  1422. % Angela headstage indices
  1423. subInds = [1:6,37:42,19:24,43:48,79:84,61:66,85:90,169:174,211:216,175:180];
  1424. for jj = 1:size(inds,1)
  1425. for k = 1:20
  1426. trainSal = logicFind(1,~ismember(1:salN,inds(jj,2)),'==');
  1427. testSal = logicFind(0,~ismember(1:salN,inds(jj,2)),'==');
  1428. trainLSD = logicFind(1,~ismember(1:lsdN,inds(jj,1)),'==');
  1429. testLSD = logicFind(0,~ismember(1:lsdN,inds(jj,1)),'==');
  1430. thisSal = [];
  1431. for ii = trainSal
  1432. thisSal = [thisSal;salDiff{ii}(randperm(size(salDiff{ii},1),...
  1433. minSamp),:)];
  1434. end
  1435. salTest = salDiff{testSal}(randperm(size(salDiff{testSal},1),...
  1436. minSamp),:);
  1437. thisLSD = [];
  1438. for ii = trainLSD
  1439. thisLSD = [thisLSD;lsdDiff{ii}(randperm(size(lsdDiff{ii},1),...
  1440. minSamp),:)];
  1441. end
  1442. lsdTest = lsdDiff{testLSD}(randperm(size(lsdDiff{testLSD},1),...
  1443. minSamp),:);
  1444. % Combine
  1445. trainX = [thisSal;thisLSD];
  1446. trainY = [zeros(size(thisSal,1),1);ones(size(thisLSD,1),1)];
  1447. testX = [salTest;lsdTest];
  1448. testY = [zeros(minSamp,1);ones(minSamp,1)];
  1449. mdl = fitglm(zscore(trainX(:,subInds)),trainY,...
  1450. 'distribution','binomial','binomialSize',numel(trainY));
  1451. prob = predict(mdl,zscore(testX(:,subInds)));
  1452. [x24(jj,k,:),y24(jj,k,:),~,a24(jj,k)] = perfcurve(testY,prob,1);
  1453. end
  1454. end
  1455. % Permuted
  1456. lsdInds = nchoosek(1:6,3);
  1457. salInds = nchoosek(1:5,3);
  1458. c = 1;
  1459. for ii = 1:size(lsdInds,1)
  1460. for jj = 1:size(salInds,1)
  1461. permInds(c,:) = cat(2,lsdInds(ii,:),salInds(jj,:));
  1462. c = c+1;
  1463. end
  1464. end
  1465. thisLSD = cell(1,numel(lsdDiff));
  1466. thisSal = cell(1,numel(salDiff));
  1467. for ii = 1:size(permInds,1)
  1468. lsdInd = permInds(ii,randperm(3,3));
  1469. salInd = permInds(ii,randperm(3,3)+3);
  1470. lsdOtherInd = logicFind(1,~ismember(1:6,lsdInd),'==');
  1471. salOtherInd = logicFind(1,~ismember(1:5,salInd),'==');
  1472. for jj = 1:numel(lsdDiff)
  1473. thisLSD{jj} = lsdDiff{jj}(randperm(size(lsdDiff{jj},1),...
  1474. minSamp),:);
  1475. end
  1476. for jj = 1:numel(salDiff)
  1477. thisSal{jj} = salDiff{jj}(randperm(size(salDiff{jj},1),...
  1478. minSamp),:);
  1479. end
  1480. testX = cat(1,thisLSD{lsdInd(1)},thisSal{salInd(1)});
  1481. testY = cat(1,ones(minSamp,1),zeros(minSamp,1));
  1482. trainX = cat(1,thisLSD{lsdInd(2:3)},thisSal{salInd(2:3)},thisLSD{lsdOtherInd},thisSal{salOtherInd});
  1483. trainY = cat(1,ones(minSamp*4,1),zeros(minSamp*5,1));
  1484. mdl = fitglm(zscore(trainX(:,subInds)),trainY,'Distribution','binomial');
  1485. prob = predict(mdl,zscore(testX(:,subInds)));
  1486. [xP24(ii,:),yP24(ii,:),~,aP24(ii)] = perfcurve(testY,prob,1);
  1487. end
  1488. % save('G:\GreenLab\data\lsd\LSDvSaline24HrLogLOOImag.mat','a24','x24','y24','aP24','xP24','yP24')
  1489. %%
  1490. load('LSDvSaline24HrLogLOOImag.mat')
  1491. % Plot
  1492. figure
  1493. subplot(2,1,1)
  1494. hold on
  1495. [f,xi,bw] = ksdensity(mean(a24,2));
  1496. fill(xi,f*bw,'w')
  1497. plotSpread(mean(a24,2),'xyori','flipped','xvalues',0.05,'spreadWidth',0.1,'distributionMarkers','.')
  1498. plot([mean(a24,[1,2]) mean(a24,[1,2])],[0 0.25],'-')
  1499. yticks(0:0.05:0.25)
  1500. xlim([0 1])
  1501. ylim([0 0.25])
  1502. subplot(2,1,2)
  1503. hold on
  1504. [f,xi,bw] = ksdensity(aP24);
  1505. fill(xi,f*bw,'w')
  1506. plotSpread(aP24','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  1507. plot([mean(aP24) mean(aP24)],[0 0.25],'-')
  1508. yticks(0:0.05:0.25)
  1509. xlim([0 1])
  1510. ylim([0 0.25])
  1511. %% Plot in 3D feature space
  1512. % Get ordered list of single features
  1513. load('G:\GreenLab\data\lsd\acuteLSDvSalineLogLOOImag.mat','aS')
  1514. mAS = mean(aS,1);
  1515. [sorted,indSort] = sort(mAS,'descend');
  1516. % Subset features
  1517. subInds = [1:6,37:42,19:24,43:48,79:84,61:66,85:90,169:174,211:216,...
  1518. 175:180];
  1519. feat = names({'ILL','CA1L','PLL','NAcL','PLR','CA1R','ILR','NAcR'},...
  1520. {'d','t','a','b','lg','hg'});
  1521. feat = feat(subInds);
  1522. % Get acute data ready
  1523. load('G:\GreenLab\data\lsd\normAcuteImag.mat')
  1524. allSal = cat(1,allPostSal{:});
  1525. allSal = allSal(:,subInds);
  1526. allLSD = cat(1,allPostLSD{:});
  1527. allLSD = allLSD(:,subInds);
  1528. % Get 24Hr post data ready
  1529. load('lsdSal24Hr-BaseImag.mat')
  1530. postSal = cat(1,salDiff{:});
  1531. postLSD = cat(1,lsdDiff{:});
  1532. % Plot - all samples
  1533. figure
  1534. hold on
  1535. scatter3(allSal(:,indSort(1)),allSal(:,indSort(2)),allSal(:,indSort(3)),...
  1536. '.k')
  1537. scatter3(allLSD(:,indSort(1)),allLSD(:,indSort(2)),allLSD(:,indSort(3)),...
  1538. '.r')
  1539. scatter3(postSal(:,indSort(1)),postSal(:,indSort(2)),...
  1540. postSal(:,indSort(3)),'.b')
  1541. scatter3(postLSD(:,indSort(1)),postLSD(:,indSort(2)),...
  1542. postLSD(:,indSort(3)),'.g')
  1543. xlabel(feat{indSort(1)})
  1544. ylabel(feat{indSort(2)})
  1545. zlabel(feat{indSort(3)})
  1546. legend({'acuteSal','acuteLSD','24Sal','24LSD'})
  1547. % Plot - mean within animal
  1548. figure
  1549. hold on
  1550. scatter3(cellfun(@(x) mean(x(:,indSort(1))),allPostSal),...
  1551. cellfun(@(x) mean(x(:,indSort(2))),allPostSal),...
  1552. cellfun(@(x) mean(x(:,indSort(3))),allPostSal),'.k')
  1553. scatter3(cellfun(@(x) mean(x(:,indSort(1))),allPostLSD),...
  1554. cellfun(@(x) mean(x(:,indSort(2))),allPostLSD),...
  1555. cellfun(@(x) mean(x(:,indSort(3))),allPostLSD),'.r')
  1556. scatter3(cellfun(@(x) mean(x(:,indSort(1))),salDiff),...
  1557. cellfun(@(x) mean(x(:,indSort(2))),salDiff),...
  1558. cellfun(@(x) mean(x(:,indSort(3))),salDiff),'.b')
  1559. scatter3(cellfun(@(x) mean(x(:,indSort(1))),lsdDiff),...
  1560. cellfun(@(x) mean(x(:,indSort(2))),lsdDiff),...
  1561. cellfun(@(x) mean(x(:,indSort(3))),lsdDiff),'.g')
  1562. xlabel(feat{indSort(1)})
  1563. ylabel(feat{indSort(2)})
  1564. zlabel(feat{indSort(3)})
  1565. legend({'acuteSal','acuteLSD','24Sal','24LSD'})
  1566. view([163 26])
  1567. %% LSD Stim effects
  1568. % eoi = base1,base2(wash),stim1
  1569. % Raw
  1570. % [data,samp1,files,time1] = collateData(['G:\GreenLab\data\dualSite\',...
  1571. % 'processed\'],{'IL','in'},{'pow','coh'},'trl','');
  1572. % eoi = base,wash,stim
  1573. % Raw
  1574. [data2,samp2,files2,time2] = collateData(['G:\GreenLab\data\lsdStim\'...
  1575. 'processed\baseImag\'],{'mPFC','in'},{'pow','coh'},'trl','');
  1576. % Combine
  1577. for ii = 1%:3
  1578. allData{ii} = [data{1,ii};data2{1,ii}];
  1579. allFiles{ii} = [files{ii,1};files2{ii,1}];
  1580. % relTime{ii} = [time1.rel{1,ii};time2.rel{1,ii}];
  1581. % absTime{ii} = [time1.abs{1,ii};time2.abs{1,ii}];
  1582. % check for any with fewer than 216 features
  1583. feats = cellfun(@(x) size(x,2),allData{ii});
  1584. allData{ii}(feats(:,1)<216,:) = [];
  1585. allFiles{ii}(feats(:,1)<216,:) = [];
  1586. % relTime{ii}(feats(:,1)<216,:) = [];
  1587. % absTime{ii}(feats(:,1)<216,:) = [];
  1588. samps{ii} = cellfun(@(x) size(x,1),allData{ii});
  1589. % minSamps from base and wash
  1590. minSamps{ii} = min(samps{ii}(:,[1,2]),[],2);
  1591. % minSamps from base and stim
  1592. % minSamps{ii} = min(samps{ii}(:,[1,3]),[],2);
  1593. end
  1594. % Only grab first 30 minutes of stim data
  1595. % minutes = 30;
  1596. % data30min = allData;
  1597. % for ii = 1:size(allData{1},1)
  1598. % toGrab = absTime{1,1}{ii,3}(:,1)<=absTime{1}{ii,3}(1)+60*minutes;
  1599. % data30min{1}{ii,3} = allData{1}{ii,3}(toGrab,:);
  1600. % end
  1601. % Grab lsd data; eoi = base,wash,stim
  1602. % Imag
  1603. [lsdData,lsdSamp,lsdFiles,lsdTime] = collateData(['G:\GreenLab\data\'...
  1604. 'lsdStim\processed\postLSDImag\'],{'.mat'},{'pow','coh'},'trl','');
  1605. % % Grab only first 30 minutes of stim data
  1606. % minutes = 30;
  1607. % lsdData30min = lsdData;
  1608. % for ii = 1:size(lsdData{1},1)
  1609. % toGrab = lsdTime.abs{1}{ii,3}(:,1)<=lsdTime.abs{1}{ii,3}(1)+60*minutes;
  1610. % lsdData30min{1}{ii,3} = lsdData{1}{ii,3}(toGrab,:);
  1611. % end
  1612. % % Replace allData and lsdData with 30 minute versions
  1613. % allData = data30min; lsdData = lsdData30min;
  1614. %% Load pow and coh data
  1615. cd 'G:\GreenLab\data\lsdStim\processed\baseImag\'
  1616. for ii = 1:size(files2{1},1)
  1617. load(files2{1}{ii},'psdTrls','coh')
  1618. allSALPowPre{ii} = psdTrls{1}.Pow;
  1619. allSALCohPre{ii} = coh{1}.Cxy;
  1620. allSALPowStim{ii} = psdTrls{3}.Pow;
  1621. allSALCohStim{ii} = coh{3}.Cxy;
  1622. allSALPowPost{ii} = psdTrls{2}.Pow;
  1623. allSALCohPost{ii} = coh{2}.Cxy;
  1624. end
  1625. cd 'G:\GreenLab\data\lsdStim\processed\postLSDImag\'
  1626. for ii = 1:size(lsdFiles{1},1)
  1627. load(lsdFiles{1}{ii},'psdTrls','coh')
  1628. allLSDPowPre{ii} = psdTrls{1}.Pow;
  1629. allLSDCohPre{ii} = coh{1}.Cxy;
  1630. allLSDPowStim{ii} = psdTrls{3}.Pow;
  1631. allLSDCohStim{ii} = coh{3}.Cxy;
  1632. allLSDPowPost{ii} = psdTrls{2}.Pow;
  1633. allLSDCohPost{ii} = coh{2}.Cxy;
  1634. end
  1635. %%
  1636. ids = {'IRDM15';'IRDM16';'IRDM21';'IRDM5';'IRDM6'};
  1637. for ii = 1:numel(ids)
  1638. salInds = contains(files2{1},ids{ii});
  1639. lsdInds = contains(lsdFiles{1},ids{ii});
  1640. salPowPre{ii} = cat(3,allSALPowPre{salInds});
  1641. lsdPowPre{ii} = cat(3,allLSDPowPre{lsdInds});
  1642. salPowStim{ii} = cat(3,allSALPowStim{salInds});
  1643. lsdPowStim{ii} = cat(3,allLSDPowStim{lsdInds});
  1644. salPowPost{ii} = cat(3,allSALPowPost{salInds});
  1645. lsdPowPost{ii} = cat(3,allLSDPowPost{lsdInds});
  1646. salCohPre{ii} = cat(3,allSALCohPre{salInds});
  1647. lsdCohPre{ii} = cat(3,allLSDCohPre{lsdInds});
  1648. salCohStim{ii} = cat(3,allSALCohStim{salInds});
  1649. lsdCohStim{ii} = cat(3,allLSDCohStim{lsdInds});
  1650. salCohPost{ii} = cat(3,allSALCohPost{salInds});
  1651. lsdCohPost{ii} = cat(3,allLSDCohPost{lsdInds});
  1652. end
  1653. %%
  1654. salPowPreM = mean(cat(3,salPowPre{:}),3);
  1655. salPowPreS = std(cat(3,salPowPre{:}),[],3);
  1656. salPowPostM = mean(cat(3,salPowPost{:}),3);
  1657. salPowPostS = std(cat(3,salPowPost{:}),[],3);
  1658. salPowStimM = mean(cat(3,salPowStim{:}),3);
  1659. salPowStimS = std(cat(3,salPowStim{:}),[],3);
  1660. lsdPowPreM = mean(cat(3,lsdPowPre{:}),3);
  1661. lsdPowPreS = std(cat(3,lsdPowPre{:}),[],3);
  1662. lsdPowPostM = mean(cat(3,lsdPowPost{:}),3);
  1663. lsdPowPostS = std(cat(3,lsdPowPost{:}),[],3);
  1664. lsdPowStimM = mean(cat(3,lsdPowStim{:}),3);
  1665. lsdPowStimS = std(cat(3,lsdPowStim{:}),[],3);
  1666. salCohPreM = mean(cat(3,salCohPre{:}),3);
  1667. salCohPreS = std(cat(3,salCohPre{:}),[],3);
  1668. salCohPostM = mean(cat(3,salCohPost{:}),3);
  1669. salCohPostS = std(cat(3,salCohPost{:}),[],3);
  1670. salCohStimM = mean(cat(3,salCohStim{:}),3);
  1671. salCohStimS = std(cat(3,salCohStim{:}),[],3);
  1672. lsdCohPreM = mean(cat(3,lsdCohPre{:}),3);
  1673. lsdCohPreS = std(cat(3,lsdCohPre{:}),[],3);
  1674. lsdCohPostM = mean(cat(3,lsdCohPost{:}),3);
  1675. lsdCohPostS = std(cat(3,lsdCohPost{:}),[],3);
  1676. lsdCohStimM = mean(cat(3,lsdCohStim{:}),3);
  1677. lsdCohStimS = std(cat(3,lsdCohStim{:}),[],3);
  1678. sites = {'lIL','rIL','lOFC','rOFC','lNAcS','rNAcS','lNAcC','rNAcC'};
  1679. cmbs = nchoosek(1:8,2);
  1680. % cInds = [1,4,5,10,11,23];
  1681. % cmbs = cmbs(cInds,:);
  1682. %% raw data
  1683. figure
  1684. for ii = 1:8
  1685. subplot(4,2,ii)
  1686. hold on
  1687. plot(salPowPreM(ii,:),'k')
  1688. plot(salPowStimM(ii,:),'b')
  1689. plot([1 1],[-70 -25],':k')
  1690. plot([5 5],[-70 -25],':k')
  1691. plot([10 10],[-70 -25],':k')
  1692. plot([15 15],[-70 -25],':k')
  1693. plot([30 30],[-70 -25],':k')
  1694. plot([45 45],[-70 -25],':k')
  1695. plot([65 65],[-70 -25],':k')
  1696. plot([70 70],[-70 -25],':k')
  1697. plot([90 90],[-70 -25],':k')
  1698. % plot(salPowPostM(ii,:),'b--')
  1699. ylim([-70 -25])
  1700. title(sites{ii})
  1701. end
  1702. figure
  1703. for ii = 1:size(cmbs,1)
  1704. subplot(7,4,ii)
  1705. hold on
  1706. plot(decimate(salCohPreM(ii,:),5),'k')
  1707. plot(decimate(salCohStimM(ii,:),5),'b')
  1708. plot([1 1],[-0.2 0.15],':k')
  1709. plot([5 5],[-0.2 0.15],':k')
  1710. plot([10 10],[-0.2 0.15],':k')
  1711. plot([15 15],[-0.2 0.15],':k')
  1712. plot([30 30],[-0.2 0.15],':k')
  1713. plot([45 45],[-0.2 0.15],':k')
  1714. plot([65 65],[-0.2 0.15],':k')
  1715. plot([70 70],[-0.2 0.15],':k')
  1716. plot([90 90],[-0.2 0.15],':k')
  1717. % plot(decimate(salCohPostM(cInds(ii),:),5),'b--')
  1718. title([sites{cmbs(ii,1)},'-',sites{cmbs(ii,2)}])
  1719. ylim([-0.2 0.15])
  1720. end
  1721. figure
  1722. for ii = 1:8
  1723. subplot(4,2,ii)
  1724. hold on
  1725. plot(salPowPreM(ii,:),'k')
  1726. plot(salPowStimM(ii,:),'r')
  1727. plot([1 1],[-70 -25],':k')
  1728. plot([5 5],[-70 -25],':k')
  1729. plot([10 10],[-70 -25],':k')
  1730. plot([15 15],[-70 -25],':k')
  1731. plot([30 30],[-70 -25],':k')
  1732. plot([45 45],[-70 -25],':k')
  1733. plot([65 65],[-70 -25],':k')
  1734. plot([70 70],[-70 -25],':k')
  1735. plot([90 90],[-70 -25],':k')
  1736. % plot(salPowPostM(ii,:),'r--')
  1737. title(sites{ii})
  1738. ylim([-70 -25])
  1739. end
  1740. figure
  1741. for ii = 1:size(cmbs,1)
  1742. subplot(7,4,ii)
  1743. hold on
  1744. plot(decimate(lsdCohPreM(ii,:),5),'k')
  1745. plot(decimate(lsdCohStimM(ii,:),5),'r')
  1746. plot([1 1],[-0.2 0.15],':k')
  1747. plot([5 5],[-0.2 0.15],':k')
  1748. plot([10 10],[-0.2 0.15],':k')
  1749. plot([15 15],[-0.2 0.15],':k')
  1750. plot([30 30],[-0.2 0.15],':k')
  1751. plot([45 45],[-0.2 0.15],':k')
  1752. plot([65 65],[-0.2 0.15],':k')
  1753. plot([70 70],[-0.2 0.15],':k')
  1754. plot([90 90],[-0.2 0.15],':k')
  1755. % plot(decimate(lsdCohPostM(cInds(ii),:),5),'r--')
  1756. title([sites{cmbs(ii,1)},'-',sites{cmbs(ii,2)}])
  1757. ylim([-0.2 0.15])
  1758. end
  1759. %% subtraction figure
  1760. salPowDiff = salPowStimM-salPowPreM;
  1761. salPowDiffS = (abs(std(cat(3,salPowStim{:}),[],3)+std(cat(3,salPowPre{:}),[],3)-2*...
  1762. std(cat(3,salPowStim{:},salPowPre{:}),[],3))).^0.5;
  1763. lsdPowDiff = lsdPowStimM-lsdPowPreM;
  1764. lsdPowDiffS = (abs(std(cat(3,lsdPowStim{:}),[],3)+std(cat(3,lsdPowPre{:}),[],3)-2*...
  1765. std(cat(3,lsdPowStim{:},lsdPowPre{:}),[],3))).^0.5;
  1766. x = (abs(std(cat(3,salPowStim{:},lsdPowStim{:}),[],3)+std(cat(3,salPowStim{:},lsdPowStim{:}),[],3)-2*...
  1767. std(cat(3,lsdPowPre{:},lsdPowStim{:},salPowPre{:},salPowStim{:}),[],3))).^0.5;
  1768. lsdSalPowDiff = lsdPowDiff-salPowDiff;
  1769. lsdSalPowDiffS = (abs(lsdPowDiffS+salPowDiffS-2*(x))).^0.5;
  1770. salCohDiff = salCohStimM-salCohPreM;
  1771. salCohDiffS = (abs(std(cat(3,salCohStim{:}),[],3)+std(cat(3,salCohPre{:}),[],3)-2*...
  1772. std(cat(3,salCohStim{:},salCohPre{:}),[],3))).^0.5;
  1773. lsdCohDiff = lsdCohStimM-lsdCohPreM;
  1774. lsdCohDiffS = (abs(std(cat(3,lsdCohStim{:}),[],3)+std(cat(3,lsdCohPre{:}),[],3)-2*...
  1775. std(cat(3,lsdCohStim{:},lsdCohPre{:}),[],3))).^0.5;
  1776. x = (abs(std(cat(3,salCohStim{:},lsdCohStim{:}),[],3)+std(cat(3,salCohStim{:},lsdCohStim{:}),[],3)-2*...
  1777. std(cat(3,lsdCohPre{:},lsdCohStim{:},salCohPre{:},salCohStim{:}),[],3))).^0.5;
  1778. lsdSalCohDiff = lsdCohDiff-salCohDiff;
  1779. lsdSalCohDiffS = (abs(lsdCohDiffS+salCohDiffS-2*(x))).^0.5;
  1780. %%
  1781. figure
  1782. for ii = 1:8
  1783. subplot(2,4,ii)
  1784. hold on
  1785. shadedErrorBar(1:100,lsdPowDiff(ii,:),lsdPowDiffS(ii,:),{'r','linewidth',1.5},1)
  1786. shadedErrorBar(1:100,salPowDiff(ii,:),salPowDiffS(ii,:),{'b','linewidth',1.5},1)
  1787. plot([1 1],[-10.5 1],':k')
  1788. plot([5 5],[-10.5 1],':k')
  1789. plot([10 10],[-10.5 1],':k')
  1790. plot([15 15],[-10.5 1],':k')
  1791. plot([30 30],[-10.5 1],':k')
  1792. plot([45 45],[-10.5 1],':k')
  1793. plot([65 65],[-10.5 1],':k')
  1794. plot([70 70],[-10.5 1],':k')
  1795. plot([90 90],[-10.5 1],':k')
  1796. set(gca,'xtick',0:10:100)
  1797. % xlabel('frequency (Hz)')
  1798. % ylabel('a.u.')
  1799. ylim([-10.5 1])
  1800. title(sites{ii})
  1801. set(gca,'xticklabel',[],'yticklabel',[],'xcolor','k','ycolor','k',...
  1802. 'linewidth',0.75)
  1803. end
  1804. % for ii = 1:8
  1805. % subplot(4,4,8+ii)
  1806. % hold on
  1807. % shadedErrorBar(1:100,lsdSalPowDiff(ii,:),lsdSalPowDiffS(ii,:),'k',1)
  1808. % set(gca,'xtick',0:10:100)
  1809. % ylim([-4 4])
  1810. % plot([0 100],[0 0],'--k')
  1811. % end
  1812. %%
  1813. sites = {'lIL','rIL','lOFC','rOFC','lNAcS','rNAcS','lNAcC','rNAcC'};
  1814. cmbs = nchoosek(1:8,2);
  1815. % cInds = [1,4,5,10,11,23];
  1816. % cmbs = cmbs(cInds,:);
  1817. figure
  1818. for ii = 1:size(cmbs,1)
  1819. subplot(7,4,ii)
  1820. hold on
  1821. shadedErrorBar(1:100,decimate(lsdCohDiff(ii,:),5),...
  1822. decimate(lsdCohDiffS(ii,:),5),{'r','linewidth',1.5},1)
  1823. shadedErrorBar(1:100,decimate(salCohDiff(ii,:),5),...
  1824. decimate(salCohDiffS(ii,:),5),{'b','linewidth',1.5},1)
  1825. plot([1 1],[-6.5 2],':k')
  1826. plot([5 5],[-6.5 2],':k')
  1827. plot([10 10],[-6.5 2],':k')
  1828. plot([15 15],[-6.5 2],':k')
  1829. plot([30 30],[-6.5 2],':k')
  1830. plot([45 45],[-6.5 2],':k')
  1831. plot([65 65],[-6.5 2],':k')
  1832. plot([70 70],[-6.5 2],':k')
  1833. plot([90 90],[-6.5 2],':k')
  1834. set(gca,'xtick',0:10:100)
  1835. % xlabel('frequency (Hz)')
  1836. % ylabel('a.u.')
  1837. ylim([-0.4 0.3])
  1838. title([sites{cmbs(ii,1)},'-',sites{cmbs(ii,2)}])
  1839. % set(gca,'xticklabel',[],'yticklabel',[],'xcolor','k','ycolor','k',...
  1840. % 'linewidth',0.75)
  1841. end
  1842. % for ii = 1:6
  1843. % subplot(4,3,6+ii)
  1844. % hold on
  1845. % % shadedErrorBar(1:100,decimate(lsdSalCohDiff(cInds(ii),:),5),...
  1846. % % decimate(lsdSalCohDiffS(cInds(ii),:),5),'k',1)
  1847. % plot(1:100,decimate(lsdSalCohDiff(cInds(ii),:),5),'k')
  1848. % set(gca,'xtick',0:10:100)
  1849. % ylim([-0.1 0.05])
  1850. % plot([0 100],[0 0],'--k')
  1851. % end
  1852. %%
  1853. % Calculate stim-baseline control vs. stim-baseline drug
  1854. ids = {'IRDM14';'IRDM15';'IRDM16';'IRDM21';'IRDM2_';'IRDM5';'IRDM6'};
  1855. thisBaseDiff = cell(1,numel(ids));
  1856. for ii = 1:numel(ids)
  1857. theseFiles = logicFind(1,~cellfun(@isempty,strfind(allFiles{1},...
  1858. ids{ii})),'==');
  1859. for jj = theseFiles
  1860. if size(allData{1}{jj,1},1)>1
  1861. thisBaseDiff{ii} = [thisBaseDiff{ii};(allData{1}{jj,3}-...
  1862. mean(allData{1}{jj,1},1))./std(allData{1}{jj,1},[],1)];
  1863. end
  1864. end
  1865. end
  1866. thisLSDDiff = cell(1,numel(ids));
  1867. for ii = 1:numel(ids)
  1868. thisLSDDiff{ii} = (lsdData{1}{ii,3}-mean(lsdData{1}{ii,1},1))./...
  1869. std(lsdData{1}{ii,1},[],1);
  1870. end
  1871. % Remove IRDM2 (animal 5) due to low samples and IRDM14 (animal 1) for too
  1872. % few features
  1873. thisBaseDiff = thisBaseDiff([2:4,6:7]);
  1874. thisLSDDiff = thisLSDDiff([2:4,6:7]);
  1875. minSamp = min([cellfun(@(x) size(x,1),thisBaseDiff),cellfun(@(x) ...
  1876. size(x,1),thisLSDDiff)]);
  1877. % save('G:\GreenLab\data\lsdStim\lsd-baseVsal-base_zscore_stimImag_all-216feat.mat','allData',...
  1878. % 'allFiles','lsdData','lsdFiles','lsdSamp','lsdTime','minSamp',...
  1879. % 'minSamps','thisBaseDiff','thisLSDDiff')
  1880. %% plot violins of feature changes
  1881. % load('lsd-baseVsal-base_zscore_stimImag_all-216feat.mat')
  1882. % subInds = [1:12,37:54,79:90,115:126,211:216];
  1883. subInds = 1:216;
  1884. feat = names({'lmPFC','rmPFc','lOFC','rOFC','lNAcS','rNAcS','lNAcC',...
  1885. 'rNAcC'},{'d','t','a','b','lg','hg'})';
  1886. feat = feat(subInds);
  1887. allLSD = cat(1,thisLSDDiff{:});
  1888. allSAL = cat(1,thisBaseDiff{:});
  1889. these = cell(1);
  1890. for ii = subInds
  1891. these = {these{:},allSAL(:,ii),allLSD(:,ii)};
  1892. end
  1893. %% significance
  1894. x = [];
  1895. ID = [];
  1896. group = [];
  1897. for ii = 1:5
  1898. x = [x;thisBaseDiff{ii};thisLSDDiff{ii}];
  1899. ID = [ID;repmat((ii-1)*2+1,size(thisBaseDiff{ii},1),1);...
  1900. repmat(ii*2,size(thisLSDDiff{ii},1),1)];
  1901. group = [group;repmat(0,size(thisBaseDiff{ii},1),1);...
  1902. repmat(1,size(thisLSDDiff{ii},1),1)];
  1903. end
  1904. data = table(x(:,2),group,ID);
  1905. fitglme(data,'group ~ Var1+ID','Distribution','Binomial');
  1906. %% band pow
  1907. figure
  1908. for jj = 1:8
  1909. subplot(4,2,jj)
  1910. h = violin(these(:,(jj-1)*12+2:(jj-1)*12+13),'medc',[]);
  1911. for ii = 1:12
  1912. if iseven(ii)
  1913. h(ii).EdgeColor = 'r';
  1914. h(ii).FaceColor = '#ffd2ed';
  1915. else
  1916. h(ii).EdgeColor = 'b';
  1917. h(ii).FaceColor = '#e0dfff';
  1918. end
  1919. h(ii).FaceAlpha = 1;
  1920. h(ii).LineWidth = 1;
  1921. end
  1922. legend off
  1923. ylim([-400 400])
  1924. end
  1925. %% band coh
  1926. figure
  1927. for jj = 1:28
  1928. subplot(4,7,jj)
  1929. h = violin(these(:,(jj-1)*12+98:(jj-1)*12+109),'medc',[]);
  1930. for ii = 1:12
  1931. if iseven(ii)
  1932. h(ii).EdgeColor = 'r';
  1933. h(ii).FaceColor = '#ffd2ed';
  1934. else
  1935. h(ii).EdgeColor = 'b';
  1936. h(ii).FaceColor = '#e0dfff';
  1937. end
  1938. h(ii).FaceAlpha = 1;
  1939. h(ii).LineWidth = 1;
  1940. end
  1941. title(feat((jj-1)*6+49))
  1942. ylim([-1.25 1.25])
  1943. legend off
  1944. end
  1945. %% power and coherence changes in LSD+stim relative to SAL+stim
  1946. salFiles = fileSearch('H:\Shared drives\dwielDoucetteLab\data\dualSite\processed\toUseSingleImag\','IL');
  1947. [salPow,salCoh] = deal(cell(numel(salFiles),2));
  1948. for ii = 1:numel(salFiles)
  1949. load(salFiles{ii},'psdTrls','coh')
  1950. salPow{ii,1} = psdTrls{1}.Pow;
  1951. salPow{ii,2} = psdTrls{2}.Pow;
  1952. [b,c,t] = size(psdTrls{1}.bandPow);
  1953. salBand{ii,1} = reshape(psdTrls{1}.bandPow,b*c,t)';
  1954. [b,c,t] = size(psdTrls{2}.bandPow);
  1955. salBand{ii,2} = reshape(psdTrls{2}.bandPow,b*c,t)';
  1956. salCoh{ii,1} = coh{1}.Cxy;
  1957. salCoh{ii,2} = coh{2}.Cxy;
  1958. end
  1959. lsdFiles = fileSearch('G:\GreenLab\data\lsd\processed\imagAcute\','LSD');
  1960. [lsdPow,lsdCoh] = deal(cell(numel(lsdFiles),2));
  1961. for ii = 1:numel(lsdFiles)
  1962. load(lsdFiles{ii},'psdTrls','coh')
  1963. lsdPow{ii,1} = psdTrls{1}.Pow;
  1964. lsdPow{ii,2} = psdTrls{2}.Pow;
  1965. [b,c,t] = size(psdTrls{1}.bandPow);
  1966. lsdBand{ii,1} = reshape(psdTrls{1}.bandPow,b*c,t)';
  1967. [b,c,t] = size(psdTrls{2}.bandPow);
  1968. lsdBand{ii,2} = reshape(psdTrls{2}.bandPow,b*c,t)';
  1969. lsdCoh{ii,1} = coh{1}.Cxy;
  1970. lsdCoh{ii,2} = coh{2}.Cxy;
  1971. end
  1972. %%
  1973. x = []; y = []; a = [];
  1974. xS = []; yS = []; aS = [];
  1975. xP = []; yP = []; aP = [];
  1976. xSP = []; ySP = []; aSP = [];
  1977. betaStim = [];
  1978. % Subset indices to match other electrode array
  1979. % subInds = [1:12,37:54,79:90,115:126,211:216];
  1980. % subInds = [5,10:12,29,40,52,58,94,99,192,196];
  1981. subInds = 1:216;
  1982. % c = 1;
  1983. % for ii = 1:5
  1984. % for jj = 1:5
  1985. % cmbs(c,:) = [ii,jj];
  1986. % c = c+1;
  1987. % end
  1988. % end
  1989. % for ii = 1:size(cmbs,1)
  1990. % thisBase = []; thisLSD = [];
  1991. % for jj = 1:size(thisBaseDiff,2)
  1992. % thisBase{jj} = thisBaseDiff{jj}(randperm(size(thisBaseDiff{jj},1),minSamp),:);
  1993. % thisLSD{jj} = thisLSDDiff{jj}(randperm(size(thisLSDDiff{jj},1),minSamp),:);
  1994. % end
  1995. % otherLSD = logicFind(1,~ismember(1:5,cmbs(ii,1)),'==');
  1996. % otherSAL = logicFind(1,~ismember(1:5,cmbs(ii,2)),'==');
  1997. % testX = cat(1,thisBase{cmbs(ii,2)},thisLSD{cmbs(ii,1)});
  1998. % testY = cat(1,zeros(minSamp,1),ones(minSamp,1));
  1999. % trainX = cat(1,thisBase{otherSAL},thisLSD{otherLSD});
  2000. % trainY = cat(1,zeros(minSamp*4,1),ones(minSamp*4,1));
  2001. % mdl = fitglm(trainX(:,subInds),trainY,'distribution','binomial');
  2002. % prob = predict(mdl,testX(:,subInds));
  2003. % [~,~,~,aLOO(ii)] = perfcurve(testY,prob,1);
  2004. % end
  2005. %%
  2006. for n = 1:100
  2007. disp(n)
  2008. thisBase = []; thisLSD = [];
  2009. for ii = 1:size(thisBaseDiff,2)
  2010. thisBase{ii} = thisBaseDiff{ii}(randperm(size(thisBaseDiff{ii},1),minSamp),:);
  2011. thisLSD{ii} = thisLSDDiff{ii}(randperm(size(thisLSDDiff{ii},1),minSamp),:);
  2012. end
  2013. % 80:20
  2014. [trainX,trainY,testX,testY] = trainTest(cat(1,thisBase{:},thisLSD{:}),...
  2015. [zeros(minSamp*numel(thisBase),1);ones(minSamp*numel(thisLSD),1)],0.20);
  2016. mdl = fitglm(trainX(:,subInds),trainY,'distribution','binomial','binomialSize',numel(trainY));
  2017. % betas(:,:,n) = table2array(mdl.Coefficients);
  2018. prob = predict(mdl,zscore(testX(:,subInds)));
  2019. [x{n},y{n},~,a(n)] = perfcurve(testY,prob,1);
  2020. % Permuted
  2021. mdl = fitglm(trainX(:,subInds),trainY(randperm(numel(trainY),numel(trainY)),:),'distribution','binomial');
  2022. prob = predict(mdl,testX(:,subInds));
  2023. [xP{n},yP{n},~,aP(n)] = perfcurve(testY,prob,1);
  2024. c = 1;
  2025. for ii = subInds
  2026. mdl = fitglm(trainX(:,ii),trainY,'distribution','binomial');
  2027. betaStim(c,n) = table2array(mdl.Coefficients(2,1));
  2028. prob = predict(mdl,testX(:,ii));
  2029. [xS{n,c},yS{n,c},~,aS(n,c)] = perfcurve(testY,prob,1);
  2030. [xSP{n,c},ySP{n,c},~,aSP(n,c)]= perfcurve(testY(randperm(numel(...
  2031. testY),numel(testY))),prob,1);
  2032. c = c+1;
  2033. end
  2034. end
  2035. % Single feature analysis
  2036. feat = names({'lmPFC','rmPFc','lOFC','rOFC','lNAcS','rNAcS','lNAcC',...
  2037. 'rNAcC'},{'d','t','a','b','lg','hg'})';
  2038. % feat = names({'lmPFC','rmPFc','lNAcC','rNAcC'},...
  2039. % {'d','t','a','b','lg','hg'})';
  2040. [sortA,sortInd] = sort(mean(aS,1)','descend');
  2041. sortFeat = feat(sortInd);
  2042. sortBeta = mean(betaStim(sortInd,:),2);
  2043. sortA = sortA.*sign(sortBeta(sortInd));
  2044. % save(['G:\GreenLab\data\lsdStim\'...
  2045. % 'lsdStim-base_v_salStim-base_imag_all_216Feat.mat'],'x','y','a',...
  2046. % 'xP','yP','aP','xS','yS','aS','betaStim','xSP','ySP','aSP',...
  2047. % 'sortFeat','sortBeta')
  2048. %% Plot
  2049. % Interpolate x and y vectors to be the same size
  2050. for ii = 1:100
  2051. interptX(ii,:) = interp1(linspace(0,1,numel(x{ii})),x{ii},...
  2052. linspace(0,1,180));
  2053. interptY(ii,:) = interp1(linspace(0,1,numel(y{ii})),y{ii},...
  2054. linspace(0,1,180));
  2055. interptXP(ii,:) = interp1(linspace(0,1,numel(xP{ii})),xP{ii},...
  2056. linspace(0,1,180));
  2057. interptYP(ii,:) = interp1(linspace(0,1,numel(yP{ii})),yP{ii},...
  2058. linspace(0,1,180));
  2059. end
  2060. figure
  2061. hold on
  2062. plot(mean(interptX,1),mean(interptY,1),'-k')
  2063. plot(mean(interptXP,1),mean(interptYP,1),'--k')
  2064. xlabel('FPR'); ylabel('TPR')
  2065. title('Base-LSD vs. Base-Saline Stim: Log')
  2066. set(gca,'xtick',0:0.5:1,'ytick',0:0.5:1)
  2067. legend({['Real: ',num2str(round(mean(a),2)),'\pm',...
  2068. num2str(round(conf(a,0.95),3))],['Permuted: ',...
  2069. num2str(round(mean(aP),2)),'\pm',...
  2070. num2str(round(conf(aP,0.95),2))]},'location','se')
  2071. figure
  2072. hold on
  2073. violin({a',aP'},'facecolor',[1,1,1])
  2074. plotSpread({a',aP'},'distributionColors',{'k','k'})
  2075. title('LSD+stim vs. SAL+stim')
  2076. figure
  2077. subplot(2,1,1)
  2078. hold on
  2079. [f,xi] = ksdensity(a); fill(xi,f/100,'w')
  2080. plotSpread(a','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  2081. plot([mean(a) mean(a)],[0 0.15],'-')
  2082. yticks(0:0.05:0.15)
  2083. xlim([0 1])
  2084. ylim([0 0.15])
  2085. subplot(2,1,2)
  2086. hold on
  2087. [f,xi] = ksdensity(aP); fill(xi,f/100,'w')
  2088. plotSpread(aP','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  2089. plot([mean(aP) mean(aP)],[0 0.15],'-')
  2090. yticks(0:0.05:0.15)
  2091. xlim([0 1])
  2092. ylim([0 0.15])
  2093. sgtitle('LSD+stim vs. SAL+stim')
  2094. %% single feature performance spreads
  2095. % subInds = [1:12,37:54,79:90,115:126,211:216];
  2096. % this = aS(:,subInds);
  2097. % thisP = aSP(:,subInds);
  2098. this = aS;
  2099. thisP = aSP;
  2100. figure
  2101. for ii = 1:8
  2102. subplot(4,2,ii)
  2103. hold on
  2104. plotSpread(this(:,(ii-1)*6+1:ii*6),'xvalues',1-0.75:2*3:12*3-0.75,...
  2105. 'distributionColors','k','binWidth',0.005);
  2106. for jj = 1:6
  2107. plot([(jj-1)*6-1.25 (jj-1)*6+1.75],[mean(this(:,(ii-1)*6+jj)) ...
  2108. mean(this(:,(ii-1)*6+jj))],'k','lineWidth',3)
  2109. plot([(jj-1)*6+1.5 (jj-1)*6+4.25],[mean(thisP(:,(ii-1)*6+jj)) ...
  2110. mean(thisP(:,(ii-1)*6+jj))],'color','#73898e','lineWidth',3)
  2111. end
  2112. plotSpread(thisP(:,(ii-1)*6+1:ii*6),'xvalues',2+0.75:2*3:12*3+0.75,...
  2113. 'distributionColors','#73898e','binWidth',0.005)
  2114. ylim([0.3 0.8])
  2115. title(feat((ii-1)*6+1))
  2116. % set(gca,'xtick',1.5:2:12.5,'xticklabel',feat((ii-1)*6+1:ii*6))
  2117. end
  2118. %%
  2119. starts = (1:10:55)-2;
  2120. stops = (1:10:55)+2;
  2121. startsP = (5:10:55)-2;
  2122. stopsP = (5:10:55)+2;
  2123. figure
  2124. for ii = 1:28
  2125. subplot(4,7,ii)
  2126. hold on
  2127. plotSpread(this(:,(ii-1)*6+1:ii*6),'xvalues',1:10:55,...
  2128. 'distributionColors','k','binWidth',0.005);
  2129. plotSpread(thisP(:,(ii-1)*6+1:ii*6),'xvalues',5:10:55,...
  2130. 'distributionColors','#73898e','binWidth',0.005)
  2131. for jj = 1:6
  2132. plot([starts(jj) stops(jj)],[mean(this(:,(ii-1)*6+jj)) ...
  2133. mean(this(:,(ii-1)*6+jj))],'k','lineWidth',3)
  2134. plot([startsP(jj) stopsP(jj)],[mean(thisP(:,(ii-1)*6+jj)) ...
  2135. mean(thisP(:,(ii-1)*6+jj))],'color','#73898e','lineWidth',3)
  2136. end
  2137. % set(gca,'xtick',1.5:2:12.5,'xticklabel',feat((ii-1)*6+1:(ii-1)*6))
  2138. end
  2139. %%
  2140. figure
  2141. hold on
  2142. plotSpread(this(:,1:24),'xvalues',1:2:48,'distributionColors','k')
  2143. plotSpread(thisP(:,1:24),'xvalues',2:2:48)
  2144. set(gca,'xtick',1.5:2:48.5,'xticklabel',featSub(1:24))
  2145. for ii = 1:24
  2146. plot([(ii-1)*2+1-.25 (ii-1)*2+1+.25],[mean(this(:,ii)) mean(this(:,ii))],'b')
  2147. plot([ii*2-.25 ii*2+.25],[mean(thisP(:,ii)) mean(thisP(:,ii))],'k')
  2148. end
  2149. figure
  2150. hold on
  2151. plotSpread(this(:,25:60),'xvalues',1:2:72,'distributionColors','k')
  2152. plotSpread(thisP(:,25:60),'xvalues',2:2:72)
  2153. set(gca,'xtick',1.5:2:72.5,'xticklabel',featSub(25:60))
  2154. for ii = 1:36
  2155. plot([(ii-1)*2+1-.25 (ii-1)*2+1+.25],[mean(this(:,ii+24)) ...
  2156. mean(this(:,ii+24))],'b')
  2157. plot([ii*2-.25 ii*2+.25],[mean(thisP(:,ii+24)) ...
  2158. mean(thisP(:,ii+24))],'k')
  2159. end
  2160. %%
  2161. mAS = mean(aS,1).*sign(mean(betaStim,2))';
  2162. [h,p] = ttest2(aS,aSP);
  2163. pAdj = p.*numel(p);
  2164. mAS(pAdj>=0.05) = NaN;
  2165. pow = reshape(mAS(1:48),6,8);
  2166. coh = reshape(mAS(49:end),6,28);
  2167. figure
  2168. pcolor(padarray(pow,[1 1],NaN,'post')')
  2169. colormap('viridis')
  2170. caxis([-1 1])
  2171. set(gca,'xtick',1.5:6.5,'xticklabel',...
  2172. {'\delta','\theta','\alpha','\beta','l\gamma','h\gamma'},...
  2173. 'ytick',1.5:8.5,'yticklabel',...
  2174. {'ILl','ILr','OFCl','OFCr','NAcSl','NAcSr','NAcCl','NAcCr'})
  2175. figure
  2176. pcolor(padarray(coh,[1 1],NaN,'post')')
  2177. colormap('viridis')
  2178. caxis([-1 1])
  2179. set(gca,'xtick',1.5:6.5,'xticklabel',...
  2180. {'\delta','\theta','\alpha','\beta','l\gamma','h\gamma'},...
  2181. 'ytick',1.5:6.5,'yticklabel',...
  2182. {'ILl-ILr','ILl-NAl','ILl-NAr','NAl-ILr','ILr-NAr','NAl-NAr'})
  2183. %% Keep data sets separate; saline base v stim & lsd base v stim
  2184. % Raw
  2185. % [data,samp1,files,time1] = collateData(['D:\dualSite\processed\'...
  2186. % 'toUseSingleImag\'],{'IL','in'},{'pow','coh'},'trl','');
  2187. % Raw
  2188. [data2,samp2,files2,time2] = collateData(['G:\GreenLab\data\lsdStim\'...
  2189. 'processed\baseImag\'],{'mPFC','in'},{'pow','coh'},'trl','');
  2190. % Combine
  2191. for ii = 1%:3
  2192. allData{ii} = [data{1,ii};data2{1,ii}];
  2193. allFiles{ii} = [files{ii,1};files2{ii,1}];
  2194. relTime{ii} = [time1.rel{1,ii};time2.rel{1,ii}];
  2195. absTime{ii} = [time1.abs{1,ii};time2.abs{1,ii}];
  2196. % check for any with fewer than 216 features
  2197. feats = cellfun(@(x) size(x,2),allData{ii});
  2198. allData{ii}(feats(:,1)<216,:) = [];
  2199. allFiles{ii}(feats(:,1)<216,:) = [];
  2200. % relTime{ii}(feats(:,1)<216,:) = [];
  2201. % absTime{ii}(feats(:,1)<216,:) = [];
  2202. samps{ii} = cellfun(@(x) size(x,1),allData{ii});
  2203. % minSamps from base and stim
  2204. minSamps{ii} = min(samps{ii}(:,[1,3]),[],2);
  2205. end
  2206. % Raw
  2207. [lsdData,lsdSamp,lsdFiles,lsdTime] = collateData(['G:\GreenLab\data\'...
  2208. 'lsdStim\processed\postLSDImag\'],{'.mat'},{'pow','coh'},'trl','');
  2209. lsdData = lsdData{1}(:,[1,3]);
  2210. % manually set minSamp
  2211. minSamp = 80;
  2212. % Saline: base v. stim model
  2213. ids = {'IRDM14';'IRDM15';'IRDM16';'IRDM21';'IRDM2_';'IRDM5';'IRDM6'};
  2214. thisSaline = cell(numel(ids),2);
  2215. for ii = 1:numel(ids)
  2216. theseFiles = logicFind(1,~cellfun(@isempty,strfind(allFiles{1},...
  2217. ids{ii})),'==');
  2218. for jj = theseFiles
  2219. thisSaline{ii,1} = [thisSaline{ii,1};allData{1}{jj,1}];
  2220. thisSaline{ii,2} = [thisSaline{ii,2};allData{1}{jj,3}];
  2221. end
  2222. end
  2223. % Remove animals 1 and 5 (1 has missing features in LSD condition, and
  2224. % 5 has too few samples in both)
  2225. saline = thisSaline([2:4,6:7],:);
  2226. lsd = lsdData([2:4,6:7],:);
  2227. %%
  2228. [thisTrainX,thisTrainY,thisTestX,thisTestY] = deal(cell(1,6));
  2229. [xSaline,ySaline,tSaline] = deal(cell(1,100));
  2230. [aSaline] = deal(zeros(1,100));
  2231. [betaSaline,aSalineSingle] = deal(zeros(216,100));
  2232. [xSalineSingle,ySalineSingle,tSalineSingle] = deal(cell(216,100));
  2233. for ii = 1:100
  2234. for jj = 1:5
  2235. thisBase = saline{jj,1}(randperm(size(saline{jj,1},1),minSamp),:);
  2236. thisStim = saline{jj,2}(randperm(size(saline{jj,2},1),minSamp),:);
  2237. [thisTrainX{jj},thisTrainY{jj},thisTestX{jj},thisTestY{jj}] = ...
  2238. trainTest([thisBase;thisStim],[zeros(minSamp,1);...
  2239. ones(minSamp,1)],0.2);
  2240. end
  2241. % thisTest = randi(5,1);
  2242. % testX = zscore(cat(1,thisBase{thisTest},thisStim{thisTest}));
  2243. % testY = [zeros(80,1);ones(80,1)];
  2244. % others = logicFind(1,~ismember(1:5,thisTest),'==');
  2245. % trainX = zscore([cat(1,thisBase{others});cat(1,thisStim{others})]);
  2246. % trainY = [zeros(80*4,1);ones(80*4,1)];
  2247. % Normalize x values
  2248. trainX = zscore(cat(1,thisTrainX{:}));
  2249. trainY = cat(1,thisTrainY{:});
  2250. testX = zscore(cat(1,thisTestX{:}));
  2251. testY = cat(1,thisTestY{:});
  2252. % [trainX,trainY,testX,testY] = trainTest([cat(1,saline{:,1});cat(1,saline{:,2})],[zeros(sum(cellfun(@(x) size(x,1),saline(:,1))),1);ones(sum(cellfun(@(x) size(x,1),saline(:,2))),1)],0.2);
  2253. mdl = fitglm(trainX,trainY,'distribution','binomial','binomialSize',numel(trainY));
  2254. prob = predict(mdl,testX);
  2255. [xSaline{ii},ySaline{ii},tSaline{ii},aSaline(ii)] = perfcurve(testY,prob,1);
  2256. % betaSaline(:,:,ii) = table2array(mdl.Coefficients);
  2257. % for n = 1:216
  2258. % mdl = fitglm(trainX(:,n),trainY,'distribution','binomial','binomialSize',numel(trainY));
  2259. % betaSaline(n,ii) = table2array(mdl.Coefficients(2,1));
  2260. % prob = predict(mdl,testX(:,n));
  2261. % [xSalineSingle{n,ii},ySalineSingle{n,ii},tSalineSingle{n,ii},aSalineSingle(n,ii)] = perfcurve(testY,prob,1);
  2262. % end
  2263. end
  2264. %%
  2265. % LSD: base v. stim model - 80:20
  2266. lsd = lsdData([2:4,6:7],:);
  2267. [thisTrainX,thisTrainY,thisTestX,thisTestY] = deal(cell(1,6));
  2268. [xLSD,yLSD,tLSD,xLSDP,yLSDP,tLSDP] = deal(cell(1,100));
  2269. [aLSD,aLSDP] = deal(zeros(1,100));
  2270. [betaLSD,aLSDsingle,aLSDsingleP] = deal(zeros(216,100));
  2271. [xLSDsingle,yLSDsingle,tLSDsingle,xLSDsingleP,yLSDsingleP,tLSDsingleP] = deal(cell(216,100));
  2272. for ii = 1:100
  2273. for jj = 1:5
  2274. thisBase = lsd{jj,1}(randperm(size(lsd{jj,1},1),minSamp),:);
  2275. thisStim = lsd{jj,2}(randperm(size(lsd{jj,2},1),minSamp),:);
  2276. [thisTrainX{jj},thisTrainY{jj},thisTestX{jj},thisTestY{jj}] = ...
  2277. trainTest([thisBase;thisStim],[zeros(minSamp,1);...
  2278. ones(minSamp,1)],0.2);
  2279. end
  2280. % Normalize x values
  2281. trainX = zscore(cat(1,thisTrainX{:}));
  2282. trainY = cat(1,thisTrainY{:});
  2283. testX = zscore(cat(1,thisTestX{:}));
  2284. testY = cat(1,thisTestY{:});
  2285. % [trainX,trainY,testX,testY] = trainTest([cat(1,lsd{:,1});cat(1,lsd{:,2})],[zeros(sum(cellfun(@(x) size(x,1),lsd(:,1))),1);ones(sum(cellfun(@(x) size(x,1),lsd(:,2))),1)],0.2);
  2286. mdl = fitglm(trainX,trainY,'distribution','binomial','binomialSize',numel(trainY));
  2287. prob = predict(mdl,testX);
  2288. [xLSD{ii},yLSD{ii},tLSD{ii},aLSD(ii)] = perfcurve(testY,prob,1);
  2289. [xLSDP{ii},yLSDP{ii},tLSDP{ii},aLSDP(ii)] = perfcurve(testY(randperm(numel(testY),numel(testY))),prob,1);
  2290. % betaLSD(:,:,ii) = table2array(mdl.Coefficients);
  2291. % for n = 1:216
  2292. % mdl = fitglm(trainX(:,n),trainY,'distribution','binomial','binomialSize',numel(trainY));
  2293. % betaLSD(n,ii) = table2array(mdl.Coefficients(2,1));
  2294. % prob = predict(mdl,testX(:,n));
  2295. % [xLSDsingle{n,ii},yLSDsingle{n,ii},~,aLSDsingle(n,ii)] = perfcurve(testY,prob,1);
  2296. %
  2297. % prob = predict(mdl,testX(randperm(size(testX,1)),n));
  2298. % [xLSDsingleP{n,ii},yLSDsingleP{n,ii},~,aLSDsingleP(n,ii)] = perfcurve(testY,prob,1);
  2299. % end
  2300. end
  2301. %% lsd LOO
  2302. [thisTrainX,thisTrainY,thisTestX,thisTestY] = deal(cell(1,6));
  2303. [xLSD,yLSD,tLSD,xLSDP,yLSDP,tLSDP] = deal(cell(1,100));
  2304. [aLSD,aLSDP] = deal(zeros(1,100));
  2305. [betaLSD,aLSDsingle,aLSDsingleP] = deal(zeros(216,100));
  2306. [xLSDsingle,yLSDsingle,tLSDsingle,xLSDsingleP,yLSDsingleP,tLSDsingleP] = deal(cell(216,100));
  2307. thisBase = cell(1,5);
  2308. thisStim = cell(1,5);
  2309. for ii = 1:200
  2310. for jj = 1:5
  2311. thisBase{jj} = lsd{jj,1}(randperm(size(lsd{jj,1},1),minSamp),:);
  2312. thisStim{jj} = lsd{jj,2}(randperm(size(lsd{jj,2},1),minSamp),:);
  2313. end
  2314. thisTest(ii) = randi(5,1);
  2315. testX = zscore(cat(1,thisBase{thisTest(ii)},thisStim{thisTest(ii)}));
  2316. testY = [zeros(80,1);ones(80,1)];
  2317. others = logicFind(1,~ismember(1:5,thisTest(ii)),'==');
  2318. trainX = zscore([cat(1,thisBase{others});cat(1,thisStim{others})]);
  2319. trainY = [zeros(80*4,1);ones(80*4,1)];
  2320. mdl = fitglm(trainX,trainY,'distribution','binomial','binomialSize',numel(trainY));
  2321. prob = predict(mdl,testX);
  2322. [xLSD{ii},yLSD{ii},tLSD{ii},aLSD(ii)] = perfcurve(testY,prob,1);
  2323. end
  2324. %% Permuted - just shuffle group assignments (equally)
  2325. % All possible groups or 3 LSD and 2 SAL
  2326. three = nchoosek(1:5,3);
  2327. two = nchoosek(1:5,2);
  2328. c = 1;
  2329. for ii = 1:size(three,1)
  2330. for jj = 1:size(two,1)
  2331. cmbs(c,:) = [three(ii,:),two(jj,:)];
  2332. c = c+1;
  2333. end
  2334. end
  2335. %%
  2336. for ii = 1:size(cmbs,1)
  2337. disp(ii)
  2338. for k = 1:100
  2339. group1 = []; group2 = [];
  2340. for jj = 1:size(cmbs,2)
  2341. if jj <4
  2342. thisBase = {lsd{cmbs(ii,jj),1}(randperm(size(lsd{cmbs(ii,jj),1},1),minSamp),:)};
  2343. thisStim = {lsd{cmbs(ii,jj),2}(randperm(size(lsd{cmbs(ii,jj),2},1),minSamp),:)};
  2344. group1 = [group1;thisBase,thisStim];
  2345. else
  2346. thisBase = {saline{cmbs(ii,jj),1}(randperm(size(saline{cmbs(ii,jj),1},1),minSamp),:)};
  2347. thisStim = {saline{cmbs(ii,jj),2}(randperm(size(saline{cmbs(ii,jj),2},1),minSamp),:)};
  2348. group1 = [group1;thisBase,thisStim];
  2349. end
  2350. end
  2351. otherInds3 = logicFind(1,~ismember(1:5,cmbs(ii,1:3)),'==');
  2352. for jj = otherInds3
  2353. thisBase = {lsd{jj,1}(randperm(size(lsd{jj,1},1),minSamp),:)};
  2354. thisStim = {lsd{jj,2}(randperm(size(lsd{jj,2},1),minSamp),:)};
  2355. group2 = [group2;thisBase,thisStim];
  2356. end
  2357. otherInds2 = logicFind(1,~ismember(1:5,cmbs(ii,4:5)),'==');
  2358. for jj = otherInds2
  2359. thisBase = {saline{jj,1}(randperm(size(saline{jj,1},1),minSamp),:)};
  2360. thisStim = {saline{jj,2}(randperm(size(saline{jj,2},1),minSamp),:)};
  2361. group2 = [group2;thisBase,thisStim];
  2362. end
  2363. group1base = cat(1,group1{:,1});
  2364. group1stim = cat(1,group1{:,2});
  2365. [trainX,trainY,testX,testY] = trainTest([group1base;group1stim],[zeros(400,1);ones(400,1)],0.2);
  2366. % thisTest = randi(5,1);
  2367. % testX = cat(1,group1{thisTest,1},group1{thisTest,2});
  2368. % testY = [zeros(80,1);ones(80,1)];
  2369. % others = logicFind(1,~ismember(1:5,thisTest),'==');
  2370. % trainX = [cat(1,group1{others,1});cat(1,group1{others,2})];
  2371. % trainY = [zeros(80*4,1);ones(80*4,1)];
  2372. mdl = fitglm(trainX,trainY,'distribution','binomial','binomialSize',numel(trainY));
  2373. pred = predict(mdl,testX);
  2374. [~,~,~,a1(ii,k)] = perfcurve(testY,pred,1);
  2375. group2base = cat(1,group2{:,1});
  2376. group2stim = cat(1,group2{:,2});
  2377. [trainX,trainY,testX,testY] = trainTest([group2base;group2stim],[zeros(400,1);ones(400,1)],0.2);
  2378. % thisTest = randi(5,1);
  2379. % testX = cat(1,group2{thisTest,1},group2{thisTest,2});
  2380. % testY = [zeros(80,1);ones(80,1)];
  2381. % others = logicFind(1,~ismember(1:5,thisTest),'==');
  2382. % trainX = [cat(1,group2{others,1});cat(1,group2{others,2})];
  2383. % trainY = [zeros(80*4,1);ones(80*4,1)];
  2384. mdl = fitglm(trainX,trainY,'distribution','binomial','binomialSize',numel(trainY));
  2385. pred = predict(mdl,testX);
  2386. [~,~,~,a2(ii,k)] = perfcurve(testY,pred,1);
  2387. end
  2388. end
  2389. %% get distribution of differences
  2390. aDiff = mean(a1,2)-mean(a2,2);
  2391. figure
  2392. [f,xi] = ksdensity(aDiff); fill(xi,f/101,'w')
  2393. %% Plot
  2394. % Interpolate x and y vectors to be the same size
  2395. for ii = 1:100
  2396. interptXsaline(ii,:) = interp1(linspace(0,1,numel(xSaline{ii})),xSaline{ii},...
  2397. linspace(0,1,160));
  2398. interptYsaline(ii,:) = interp1(linspace(0,1,numel(ySaline{ii})),ySaline{ii},...
  2399. linspace(0,1,160));
  2400. interptXlsd(ii,:) = interp1(linspace(0,1,numel(xLSD{ii})),xLSD{ii},...
  2401. linspace(0,1,160));
  2402. interptYlsd(ii,:) = interp1(linspace(0,1,numel(yLSD{ii})),yLSD{ii},...
  2403. linspace(0,1,160));
  2404. end
  2405. figure
  2406. hold on
  2407. plot(mean(interptXsaline,1),mean(interptYsaline,1),'-k')
  2408. plot(mean(interptXlsd,1),mean(interptYlsd,1),'--k')
  2409. plot(mean(cat(2,xLSDP{:}),2),mean(cat(2,yLSDP{:}),2),':k')
  2410. xlabel('FPR'); ylabel('TPR')
  2411. title('Saline and LSD: Base vs Stim Log LOO')
  2412. set(gca,'xtick',0:0.5:1,'ytick',0:0.5:1)
  2413. legend({['Saline: ',num2str(round(mean(aSaline),2)),'\pm',...
  2414. num2str(round(conf(aSaline,0.95),3))],['LSD: ',...
  2415. num2str(round(mean(aLSD),2)),'\pm',...
  2416. num2str(round(conf(aLSD,0.95),3))],['Permuted: ',...
  2417. num2str(round(mean(aLSDP),2)),'\pm',...
  2418. num2str(round(conf(aLSDP,0.95),2))]},'location','se')
  2419. figure
  2420. hold on
  2421. violin({aLSD',aSaline',aLSDP'},'facecolor',[1,1,1])
  2422. plotSpread({aLSD',aSaline',aLSDP'},'distributionColors',{'k','k','k'})
  2423. title('LSD+stim and SAL+stim')
  2424. figure
  2425. subplot(3,1,1)
  2426. hold on
  2427. [f,xi] = ksdensity(aLSD); fill(xi,f/100,'w')
  2428. plotSpread(aLSD','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  2429. plot([mean(aLSD) mean(aLSD)],[0 0.15],'-')
  2430. yticks(0:0.05:0.15)
  2431. xlim([0 1])
  2432. ylim([0 0.16])
  2433. subplot(3,1,2)
  2434. hold on
  2435. [f,xi] = ksdensity(aSaline); fill(xi,f/100,'w')
  2436. plotSpread(aSaline','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  2437. plot([mean(aSaline) mean(aSaline)],[0 0.15],'-')
  2438. yticks(0:0.05:0.15)
  2439. xlim([0 1])
  2440. ylim([0 0.16])
  2441. subplot(3,1,3)
  2442. hold on
  2443. [f,xi] = ksdensity(aLSDP); fill(xi,f/100,'w')
  2444. plotSpread(aLSDP','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  2445. plot([mean(aLSDP) mean(aLSDP)],[0 0.15],'-')
  2446. yticks(0:0.05:0.15)
  2447. xlim([0 1])
  2448. ylim([0 0.16])
  2449. sgtitle('LSD+stim vs baseline and SAL+stim vs baseline')
  2450. %%
  2451. % Single feature analysis
  2452. feat = names({'lIL','rIL','lOFC','rOFC','lNAcS','rNAcS','lNAcC',...
  2453. 'rNAcC'},{'d','t','a','b','lg','hg'})';
  2454. lsdMeanA = mean(aLSDsingle,2).*sign(mean(betaLSD,2));
  2455. [lsdSortA,lsdSortInd] = sort(lsdMeanA,'descend');
  2456. lsdFeat = feat(lsdSortInd)';
  2457. betaLSDsort = mean(betaLSD(lsdSortInd,:),2);
  2458. [~,lsdP] = ttest2(aLSDsingle',aLSDsingleP');
  2459. lsdPadj = lsdP.*216;
  2460. lsdPadjSort = lsdPadj(lsdSortInd)';
  2461. lsdMeanA(lsdPadj>0.05) = NaN;
  2462. % lsdMeanA(lsdMeanA>0.6) = NaN;
  2463. salMeanA = mean(aSalineSingle,2).*sign(mean(betaSaline,2));
  2464. [salineSortA,salineSortInd] = sort(salMeanA,'descend');
  2465. salineFeat = feat(salineSortInd)';
  2466. betaSalineSort = betaSaline(salineSortInd);
  2467. [~,salP] = ttest(aSalineSingle'-0.5);
  2468. salPadj = salP.*216;
  2469. salPadjSort = salPadj(salineSortInd)';
  2470. salMeanA(salPadj>0.05) = NaN;
  2471. % salMeanA(salMeanA>0.6) = NaN;
  2472. salPow = reshape(salMeanA(1:48),6,8);
  2473. lsdPow = reshape(lsdMeanA(1:48),6,8);
  2474. salCoh = reshape(salMeanA(49:end),6,28);
  2475. lsdCoh = reshape(lsdMeanA(49:end),6,28);
  2476. figure
  2477. pcolor(padarray(lsdPow,[1 1],NaN,'post')')
  2478. colormap('viridis')
  2479. caxis([-1 1])
  2480. set(gca,'xtick',1.5:6.5,'xticklabel',...
  2481. {'\delta','\theta','\alpha','\beta','l\gamma','h\gamma'},...
  2482. 'ytick',1.5:8.5,'yticklabel',...
  2483. {'ILl','ILr','OFCl','OFCr','NAcSl','NAcSr','NAcCl','NAcCr'})
  2484. figure
  2485. pcolor(padarray(lsdCoh,[1 1],NaN,'post')')
  2486. colormap('viridis')
  2487. caxis([-1 1])
  2488. set(gca,'xtick',1.5:6.5,'xticklabel',...
  2489. {'\delta','\theta','\alpha','\beta','l\gamma','h\gamma'},...
  2490. 'ytick',1.5:6.5,'yticklabel',...
  2491. {'ILl-ILr','ILl-NAl','ILl-NAr','NAl-ILr','ILr-NAr','NAl-NAr'})
  2492. figure
  2493. pcolor(padarray(salPow,[1 1],NaN,'post')')
  2494. colormap('viridis')
  2495. caxis([-1 1])
  2496. set(gca,'xtick',1.5:6.5,'xticklabel',...
  2497. {'\delta','\theta','\alpha','\beta','l\gamma','h\gamma'},...
  2498. 'ytick',1.5:8.5,'yticklabel',...
  2499. {'ILl','ILr','OFCl','OFCr','NAcSl','NAcSr','NAcCl','NAcCr'})
  2500. figure
  2501. pcolor(padarray(salCoh,[1 1],NaN,'post')')
  2502. colormap('viridis')
  2503. caxis([-1 1])
  2504. set(gca,'xtick',1.5:6.5,'xticklabel',...
  2505. {'\delta','\theta','\alpha','\beta','l\gamma','h\gamma'},...
  2506. 'ytick',1.5:6.5,'yticklabel',...
  2507. {'ILl-ILr','ILl-NAl','ILl-NAr','NAl-ILr','ILr-NAr','NAl-NAr'})
  2508. %%
  2509. allLSDdiff = cat(1,thisLSDDiff{1});
  2510. allSalDiff = cat(1,thisBaseDiff{1});
  2511. [mASsort,mASsortInd] = sort(abs(mAS),'descend');
  2512. figure
  2513. hold on
  2514. c = 1;
  2515. for ii = mASsortInd
  2516. if ~isnan(mAS(ii))
  2517. errorbar(mean(allLSDdiff(:,ii)),mAS(ii),std(allLSDdiff(:,ii),[],1),'.b','horizontal')
  2518. errorbar(mean(allSalDiff(:,ii)),mAS(ii),std(allSalDiff(:,ii),[],1),'.r','horizontal')
  2519. % plot([mean(allLSDdiff(:,ii)) mean(allSalDiff(:,ii))],[mAS(ii) mAS(ii)],'-k')
  2520. c = c+1;
  2521. end
  2522. end
  2523. %%
  2524. load('lsdStim-base_v_salStim-base_imag_all_216feat.mat')
  2525. load('lsdStimvBase_salStimvBase_imag_all_216feat.mat')
  2526. lsdMeanA = mean(aLSDsingle,2).*sign(mean(betaLSD,2));
  2527. salMeanA = mean(aSalineSingle,2).*sign(mean(betaSaline,2));
  2528. diffMean = mean(aS,1).*sign(mean(betaStim,2))';
  2529. %%
  2530. figure
  2531. plot(lsdMeanA(1:48),salMeanA(1:48),'.b')
  2532. hold on
  2533. plot(lsdMeanA(49:end),salMeanA(49:end),'.r')
  2534. plot(lsdMeanA(abs(diffMean)>=0.6),salMeanA(abs(diffMean)>=0.6),'ok')
  2535. inds = logicFind(1,abs(diffMean)>=0.6,'==');
  2536. above = diffMean(inds);
  2537. [~,sortInd] = sort(abs(above),'descend');
  2538. sorted = above(sortInd);
  2539. sortedInd = inds(sortInd);
  2540. for ii = 1:numel(sortedInd)
  2541. text(lsdMeanA(sortedInd(ii)),salMeanA(sortedInd(ii)),num2str(ii))
  2542. end
  2543. plot([1 -1],[1 -1],'--k')
  2544. plot([-1 1],[1 -1],'--k')
  2545. set(gca,'xtick',-1:0.5:1,'ytick',-1:0.5:1)
  2546. xlim([-0.81 0.81])
  2547. ylim([-0.81 0.81])
  2548. xlabel('LSD+stim')
  2549. ylabel('SAL+stim')
  2550. %%
  2551. lims = [-1 1;1 1;-1 -1;1 -1];
  2552. figure
  2553. for ii = 1:4
  2554. subplot(2,2,ii)
  2555. plot(lsdMeanA(1:48),salMeanA(1:48),'.b')
  2556. hold on
  2557. plot(lsdMeanA(49:end),salMeanA(49:end),'.r')
  2558. plot(lsdMeanA(abs(diffMean)>0.6),salMeanA(abs(diffMean)>0.6),'ok')
  2559. inds = logicFind(1,abs(diffMean)>0.6,'==');
  2560. above = diffMean(inds);
  2561. [~,sortInd] = sort(abs(above),'descend');
  2562. sorted = above(sortInd);
  2563. sortedInd = inds(sortInd);
  2564. for jj = 1:numel(sortedInd)
  2565. text(lsdMeanA(sortedInd(jj)),salMeanA(sortedInd(jj)),num2str(jj))
  2566. end
  2567. plot([1 -1],[1 -1],'--k')
  2568. plot([-1 1],[1 -1],'--k')
  2569. set(gca,'xtick',-1:0.5:1,'ytick',-1:0.5:1)
  2570. xlim(sort([0.46 0.81].*lims(ii,1),'ascend'))
  2571. ylim(sort([0.46 0.81].*lims(ii,2),'ascend'))
  2572. xlabel('LSD+stim')
  2573. ylabel('SAL+stim')
  2574. end
  2575. %%
  2576. for ii = 1:10
  2577. doubleHist(allLSDdiff(:,mASsortInd(ii)),allSalDiff(:,mASsortInd(ii)))
  2578. end
  2579. %%
  2580. % Count number of incresed effect; decreased effect; and reversal of effect
  2581. reversalInd = sign(mean(allLSDdiff,1))~=sign(mean(allSalDiff,1));
  2582. reversal = sum(reversalInd);
  2583. closerInd = abs(mean(allLSDdiff(:,~reversalInd),1))<abs(mean(allSalDiff(:,~reversalInd),1));
  2584. closer = sum(closerInd);
  2585. furtherInd = abs(mean(allLSDdiff(:,~reversalInd),1))>abs(mean(allSalDiff(:,~reversalInd),1));
  2586. further = sum(furtherInd);
  2587. increaseInd = mean(allLSDdiff(:,~reversalInd),1)>mean(allSalDiff(:,~reversalInd),1);
  2588. increase = sum(increaseInd);
  2589. decreaseInd = mean(allLSDdiff(:,~reversalInd),1)<mean(allSalDiff(:,~reversalInd),1);
  2590. decrease = sum(decreaseInd);
  2591. %% Single feature analysis
  2592. cutoffA = 0.55;
  2593. cutoffP = 0.05;
  2594. for jj = 1:6
  2595. powFreqLSD(jj) = sum(lsdMeanA(1+(jj-1):6:48)>=cutoffA);
  2596. cohFreqLSD(jj) = sum(lsdMeanA(49+(jj-1):6:end)>=cutoffA);
  2597. powFreqSaline(jj) = sum(salineMeanA(1+(jj-1):6:48)>=cutoffA);
  2598. cohFreqSaline(jj) = sum(salineMeanA(49+(jj-1):6:end)>=cutoffA);
  2599. powFreqLSDP(jj) = sum(lsdPadj(1+(jj-1):6:48)<=cutoffP);
  2600. cohFreqLSDP(jj) = sum(lsdP(49+(jj-1):6:end)<=cutoffP);
  2601. powFreqSalineP(jj) = sum(salPadj(1+(jj-1):6:48)<=cutoffP);
  2602. cohFreqSalineP(jj) = sum(salPadj(49+(jj-1):6:end)<=cutoffP);
  2603. end
  2604. figure
  2605. x = categorical({'\delta','\theta','\alpha','\beta','l\gamma','h\gamma'});
  2606. x = reordercats(x,{'\delta','\theta','\alpha','\beta','l\gamma','h\gamma'});
  2607. bar(x,[powFreqLSDP;cohFreqLSDP]','stacked')
  2608. box off
  2609. title('LSD stim features by frequency')
  2610. legend({'Power','Coherence'})
  2611. figure
  2612. x = categorical({'\delta','\theta','\alpha','\beta','l\gamma','h\gamma'});
  2613. x = reordercats(x,{'\delta','\theta','\alpha','\beta','l\gamma','h\gamma'});
  2614. bar(x,[powFreqSalineP;cohFreqSalineP]','stacked')
  2615. box off
  2616. title('Saline stim features by frequency')
  2617. legend({'Power','Coherence'})
  2618. %% Frequency networks
  2619. cmbs = nchoosek(1:8,2);
  2620. freqs = {'d','t','a','b','lg','hg'};
  2621. thisAS = mean(aS,1);
  2622. [~,p] = ttest2(aS,repmat(aP',1,60));
  2623. pAdj = p.*60;
  2624. for jj = 1:6
  2625. adjMat = [];
  2626. count = 48+jj;
  2627. for ii = 1:size(cmbs,1)
  2628. % Make sure beta coherence is significant
  2629. if pAdj(count) <= 0.05
  2630. adjMat(cmbs(ii,1),cmbs(ii,2)) = thisAS(count);
  2631. else
  2632. adjMat(cmbs(ii,1),cmbs(ii,2)) = 0;
  2633. end
  2634. count = count+6;
  2635. end
  2636. adjMat = [adjMat;zeros(1,8)];
  2637. % Find scaling factor based on minimum
  2638. s = 1-min(min(abs(adjMat(adjMat~=0))));
  2639. % Compute graph with scale factor
  2640. g = graph(adjMat,{'lPFC','rPFC','lOFC','rOFC','lNAs','rNAs','lNAc','rNAc'},'upper');
  2641. % Set up node coordinates based on regular octagon
  2642. x = [1,sqrt(2)/2,0,-sqrt(2)/2,-1,-sqrt(2)/2,0,sqrt(2)/2];
  2643. y = [0,sqrt(2)/2,1,sqrt(2)/2,0,-sqrt(2)/2,-1,-sqrt(2)/2];
  2644. % Set edge color based on edge sign (+ = red; - = blue)
  2645. edgeSign = g.Edges.Weight>=0;
  2646. edgeColor = zeros(numel(edgeSign),3);
  2647. edgeColor(logicFind(1,edgeSign,'=='),:) = repmat([1 0 0],sum(edgeSign),1);
  2648. edgeColor(logicFind(0,edgeSign,'=='),:) = repmat([0 0 1],sum(~edgeSign),1);
  2649. % Set node color based on node sign (+ = red; - = blue)
  2650. nodeSign = thisAS(jj:6:48)>=0;
  2651. nodeColor = zeros(numel(nodeSign),3);
  2652. nodeColor(logicFind(1,nodeSign,'=='),:) = repmat([1 0 0],sum(nodeSign),1);
  2653. nodeColor(logicFind(0,nodeSign,'=='),:) = repmat([0 0 1],sum(~nodeSign),1);
  2654. % Check node significance
  2655. sigNode = pAdj(jj:6:48) <= 0.05;
  2656. % If not significant, set to white and size 10
  2657. nodeColor(~sigNode,:) = repmat([1 1 1],sum(~sigNode),1);
  2658. theseNodes = abs(thisAS(jj:6:48));
  2659. theseNodes(~sigNode) = 10;
  2660. % Plot
  2661. figure('position',[895,400,667,571])
  2662. h = plot(g,'XData',x,'YData',y,'markersize',theseNodes,'linewidth',abs(g.Edges.Weight)+s,'edgecolor',edgeColor,'nodecolor',nodeColor);
  2663. title(freqs{jj})
  2664. axis off
  2665. % print(['LSD',freqs{jj},'.eps'],'-dwinc','-painters')
  2666. end
  2667. %% single feature analysis
  2668. feat = names({'lmPFC','rmPFc','lOFC','rOFC','lNAcS','rNAcS','lNAcC',...
  2669. 'rNAcC'},{'d','t','a','b','lg','hg'});
  2670. feat = feat(subInds);
  2671. aSM = mean(aS,1);
  2672. [aSMsort,inds] = sort(aSM','descend');
  2673. featSort = feat(inds)';
  2674. betaM = mean(betaStim,2);
  2675. betaMS = betaM(inds);
  2676. for ii = 1:60
  2677. [~,p(ii)] = ttest(aS(:,ii)-0.5);
  2678. end
  2679. pAdj = p*60;
  2680. sigFeat = feat(pAdj<=0.05);
  2681. pow = sum(pAdj(1:24)<=0.05);
  2682. coh = sum(pAdj(25:end)<=0.05);
  2683. for jj = 1:6
  2684. % powFreq(jj) = sum(pAdj(1+(jj-1):6:48)<=0.05);
  2685. % cohFreq(jj) = sum(pAdj(49+(jj-1):6:end)<=0.05);
  2686. powFreq(jj) = sum(aSM(1+(jj-1):6:48)>=0.6);
  2687. cohFreq(jj) = sum(aSM(49+(jj-1):6:end)>=0.6);
  2688. end
  2689. figure
  2690. x = categorical({'\delta','\theta','\alpha','\beta','l\gamma','h\gamma'});
  2691. x = reordercats(x,{'\delta','\theta','\alpha','\beta','l\gamma','h\gamma'});
  2692. bar(x,[powFreq;cohFreq]','stacked')
  2693. box off
  2694. title('Stim features by frequency')
  2695. legend({'Power','Coherence'})
  2696. %% LSD stim + IRDM; look at features through time
  2697. [lsdIRDM,lsdIRDMsamps,lsdIRDMfiles] = collateData('D:\lsdIRDM\processed\',{'27';'34';'37'},{'pow','coh'},'trl','raw');
  2698. [baseIRDM,baseIRDMsamps,baseIRDMfiles] = collateData('D:\lsdIRDM\processed\control\',{'27';'34';'37'},{'pow','coh'},'trl','raw');
  2699. %%
  2700. % Use first baseline to z-score all subsequent baselines
  2701. for ii = 1:3
  2702. mLSD = mean(lsdIRDM{ii}{1,1},1);
  2703. sLSD = std(lsdIRDM{ii}{1,1},[],1);
  2704. mBase = mean(baseIRDM{ii}{1,1},1);
  2705. sBase = std(baseIRDM{ii}{1,1},[],1);
  2706. c = 1;
  2707. for jj = 2:3
  2708. zLSD(ii,c,:) = (mean(lsdIRDM{ii}{jj,1})-mLSD)./sLSD;
  2709. zBase(ii,c,:) = (mean(baseIRDM{ii}{jj,1})-mBase)./sBase;
  2710. c = c+1;
  2711. end
  2712. end
  2713. %%
  2714. for ii = 1:216
  2715. for jj = 1:2
  2716. [~,p(ii,jj)] = ttest2(zLSD(:,jj,ii),zBase(:,jj,ii));
  2717. end
  2718. end
  2719. %%
  2720. for ii = [5,10:12,29,40,52,58,94,99,192,196]
  2721. figure
  2722. hold on
  2723. plot([1:2],zBase(:,:,ii),'k')
  2724. plot([1:2],zLSD(:,:,ii),'r')
  2725. end
  2726. %% LSD stim + IRDM
  2727. % Skip IRDM36 since no corresponding baseline stim IRDM recordings
  2728. [lsdIRDM,lsdIRDMsamps,lsdIRDMfiles] = collateData('F:\lsdIRDM\processed\',{'27';'32';'34';'37';'41'},{'pow','coh'},'trl','');
  2729. [baseIRDM,baseIRDMsamps,baseIRDMfiles] = collateData('F:\lsdIRDM\processed\control\',{'27';'32';'34';'37';'41'},{'pow','coh'},'trl','');
  2730. % Will return an error since IRDM41 only has 2 LSD recordings
  2731. for ii = 1:5
  2732. for jj = 1:3
  2733. baseDiff{ii,jj} = mean(baseIRDM{ii}{jj,1})-baseIRDM{ii}{jj,3};
  2734. lsdDiff{ii,jj} = mean(lsdIRDM{ii}{jj,1})-lsdIRDM{ii}{jj,3};
  2735. end
  2736. end
  2737. minSamp = min([sum(cellfun(@(x) size(x,1),baseDiff),2);sum(cellfun(@(x) size(x,1),lsdDiff),2)]);
  2738. % Concatenate across days
  2739. for ii = 1:5
  2740. allLSDDiff{ii} = cat(1,lsdDiff{ii,:});
  2741. allBaseDiff{ii} = cat(1,baseDiff{ii,:});
  2742. end
  2743. %% Combine and build models
  2744. for ii = 1:5
  2745. testInd = ii;
  2746. trainInd = 1:5;
  2747. trainInd = trainInd(~ismember(trainInd,testInd));
  2748. thisLSD = []; thisBase = [];
  2749. for jj = trainInd
  2750. thisLSD = [thisLSD;allLSDDiff{jj}(randperm(size(allLSDDiff{jj},1),minSamp),:)];
  2751. thisBase = [thisBase;allBaseDiff{jj}(randperm(size(allBaseDiff{jj},1),minSamp),:)];
  2752. end
  2753. trainX = [thisLSD;thisBase];
  2754. trainY = [ones(size(thisLSD,1),1);zeros(size(thisBase,1),1)];
  2755. testX = [allLSDDiff{testInd}(randperm(size(allLSDDiff{testInd},1),minSamp),:);allBaseDiff{testInd}(randperm(size(allBaseDiff{testInd},1),minSamp),:)];
  2756. testY = [ones(minSamp,1);zeros(minSamp,1)];
  2757. mdl = fitglm(trainX,trainY,'distribution','binomial','binomialSize',numel(trainY));
  2758. prob = predict(mdl,testX);
  2759. [x(ii,:),y(ii,:),t(ii,:),a(ii)] = perfcurve(testY,prob,1);
  2760. % Permuted
  2761. mdl = fitglm(trainX,trainY(randperm(numel(trainY),numel(trainY))),'distribution','binomial','binomialSize',numel(trainY));
  2762. prob = predict(mdl,testX);
  2763. [xP(ii,:),yP(ii,:),tP(ii,:),aP(ii)] = perfcurve(testY,prob,1);
  2764. % Single feature
  2765. % for jj = 1:216
  2766. % mdl = fitglm(trainX(:,jj),trainY,'distribution','binomial','binomialSize',numel(trainY));
  2767. % prob = predict(mdl,testX(:,jj));
  2768. % [xS(ii,jj,:),yS(ii,jj,:),tS(ii,jj,:),aS(ii,jj)] = perfcurve(testY,prob,1);
  2769. % end
  2770. end
  2771. %% Actual leave one out (only five models) - shitty
  2772. x = []; y = []; t = []; a = [];
  2773. xS = []; yS = []; tS = []; aS = [];
  2774. xP = []; yP = []; tP = []; aP = [];
  2775. xSP = []; ySP = []; tSP = []; aSP = [];
  2776. betaStim = [];
  2777. % Subset indices to match other electrode array
  2778. % subInds = [1:12,37:54,79:90,115:126,211:216];
  2779. subInds = 1:216;
  2780. baseN = numel(thisBaseDiff);
  2781. lsdN = numel(thisLSDDiff);
  2782. cmbs = nchoosek(1:5,4);
  2783. for n = 1:5
  2784. trainBaseX = []; trainLSDX = [];
  2785. for ii = cmbs(n,:)
  2786. trainBaseX = [trainBaseX;thisBaseDiff{ii}(randperm(size(thisBaseDiff{ii},1),minSamp),:)];
  2787. trainLSDX = [trainLSDX;thisLSDDiff{ii}(randperm(size(thisLSDDiff{ii},1),minSamp),:)];
  2788. end
  2789. trainBaseY = zeros(size(trainBaseX,1),1);
  2790. trainLSDY = ones(size(trainLSDX,1),1);
  2791. lo = 6-n;
  2792. testBaseX = thisBaseDiff{lo}(randperm(size(thisBaseDiff{lo},1),minSamp),:);
  2793. testLSDX = thisLSDDiff{lo}(randperm(size(thisLSDDiff{lo},1),minSamp),:);
  2794. testBaseY = zeros(size(testBaseX,1),1);
  2795. testLSDY = ones(size(testLSDX,1),1);
  2796. trainX = [trainBaseX;trainLSDX];
  2797. trainY = [trainBaseY;trainLSDY];
  2798. testX = [testBaseX;testLSDX];
  2799. testY = [testBaseY;testLSDY];
  2800. mdl = fitglm(trainX(:,subInds),trainY,'distribution','binomial','binomialSize',numel(trainY));
  2801. prob = predict(mdl,testX(:,subInds));
  2802. [x{n},y{n},t{n},a(n)] = perfcurve(testY,prob,1);
  2803. % Permuted
  2804. mdl = fitglm(trainX(:,subInds),trainY(randperm(numel(trainY),numel(trainY)),:),'distribution','binomial','binomialSize',numel(trainY));
  2805. prob = predict(mdl,testX(:,subInds));
  2806. [xP{n},yP{n},tP{n},aP(n)] = perfcurve(testY,prob,1);
  2807. c = 1;
  2808. for ii = subInds
  2809. mdl = fitglm(trainX(:,ii),trainY,'distribution','binomial','binomialSize',numel(trainY));
  2810. betaStim(c,n) = table2array(mdl.Coefficients(2,1));
  2811. prob = predict(mdl,testX(:,ii));
  2812. [xS{n,c},yS{n,c},tS{n,c},aS(n,c)] = perfcurve(testY,prob,1);
  2813. [xSP{n,c},ySP{n,c},tSP{n,c},aSP(n,c)]= perfcurve(testY(randperm(numel(testY),numel(testY))),prob,1);
  2814. c = c+1;
  2815. end
  2816. end
  2817. % Single feature analysis
  2818. feat = names({'lmPFC','rmPFc','lOFC','rOFC','lNAcS','rNAcS','lNAcC',...
  2819. 'rNAcC'},{'d','t','a','b','lg','hg'})';
  2820. % feat = names({'lmPFC','rmPFc','lNAcC','rNAcC'},...
  2821. % {'d','t','a','b','lg','hg'})';
  2822. [sortA,sortInd] = sort(mean(aS,1)','descend');
  2823. sortFeat = feat(sortInd);
  2824. sortBeta = mean(betaStim(sortInd,:),2);
  2825. % Plot
  2826. figure
  2827. hold on
  2828. plot(mean(cat(2,x{:}),2),mean(cat(2,y{:}),2),'-k')
  2829. plot(mean(cat(2,xP{:}),2),mean(cat(2,yP{:}),2),'--k')
  2830. xlabel('FPR'); ylabel('TPR')
  2831. title('Base-LSD vs. Base-Saline Stim: Log LOO')
  2832. set(gca,'xtick',0:0.5:1,'ytick',0:0.5:1)
  2833. legend({['Real: ',num2str(round(mean(a),2)),'\pm',...
  2834. num2str(round(conf(a,0.95),3))],['Permuted: ',...
  2835. num2str(round(mean(aP),2)),'\pm',...
  2836. num2str(round(conf(aP,0.95),2))]},'location','se')
  2837. %% PCA
  2838. % [coeff,score,latent,~,explained] = pca(allSal);
  2839. % allLSDpca = bsxfun(@minus,allLSD,mean(allLSD))/coeff';
  2840. %
  2841. % figure
  2842. % hold on
  2843. % scatter3(score(:,1),score(:,2),score(:,3),'.')
  2844. % scatter3(allLSDpca(:,1),allLSDpca(:,2),allLSDpca(:,3),'.')
  2845. % scatter3(mean(score(:,1)),mean(score(:,2)),mean(score(:,3)),'ob')
  2846. % scatter3(mean(allLSDpca(:,1)),mean(allLSDpca(:,2)),mean(allLSDpca(:,3)),'or')
  2847. %% LOO simple logisitic
  2848. % load('F:\lsd\LSDvSal24HrRelImag.mat')
  2849. % x24 = []; y24 = []; a24 = [];
  2850. % subInds = [1:6,37:42,19:24,43:48,79:84,61:66,85:90,169:174,211:216,175:180];
  2851. % for n = 1:30
  2852. % c = 1;
  2853. % lsdN = size(lsd24,1);
  2854. % salN = size(sal24,1);
  2855. % for ii = 1:lsdN
  2856. % for jj = 1:salN
  2857. % inds(c,:) = [ii,jj];
  2858. % c = c+1;
  2859. % end
  2860. % end
  2861. % % LSD data
  2862. % trainLSD = logicFind(1,~ismember(1:lsdN,inds(n,1)),'==');
  2863. % testLSD = logicFind(0,~ismember(1:lsdN,inds(n,1)),'==');
  2864. %
  2865. % thisLSD24 = [];
  2866. % for ii = trainLSD
  2867. % rng(ii*n)
  2868. % thisLSD24 = [thisLSD24;lsd24{ii}(randperm(size(lsd24{ii},1),...
  2869. % minSamp),:)];
  2870. % end
  2871. % lsdTest = lsd24{testLSD}(randperm(size(lsd24{testLSD},1),...
  2872. % minSamp),:);
  2873. % % Saline data
  2874. % trainSal = logicFind(1,~ismember(1:salN,inds(n,2)),'==');
  2875. % testSal = logicFind(0,~ismember(1:salN,inds(n,2)),'==');
  2876. % thisSal = [];
  2877. % for ii = 1:numel(sal24)
  2878. % rng((ii+numel(sal24))*n)
  2879. % thisSal = [thisSal;sal24{ii}(randperm(size(sal24{ii},1),...
  2880. % minSamp),:)];
  2881. % end
  2882. % salTest = sal24{testSal}(randperm(size(sal24{testSal},1),...
  2883. % minSamp),:);
  2884. % % Combine and build models
  2885. % trainX = [thisLSD24;thisSal];
  2886. % trainY = [ones(size(thisLSD24,1),1);zeros(size(thisSal,1),1)];
  2887. % testX = [lsdTest;salTest];
  2888. % testY = [ones(minSamp,1);zeros(minSamp,1)];
  2889. %
  2890. % mdl = fitglm(trainX(:,subInds),trainY,'distribution','binomial',...
  2891. % 'binomialSize',numel(trainY));
  2892. % prob = predict(mdl,testX(:,subInds));
  2893. % [x24(n,:),y24(n,:),~,a24(n)] = perfcurve(testY,prob,1);
  2894. % % beta24(:,:,n) = table2array(mdl.Coefficients);
  2895. % % Permuted
  2896. % mdl = fitglm(trainX(:,subInds),trainY(randperm(numel(trainY),numel(trainY))),...
  2897. % 'distribution','binomial','binomialSize',numel(trainY));
  2898. % prob = predict(mdl,testX(:,subInds));
  2899. % [x24P(n,:),y24P(n,:),~,a24P(n)] = perfcurve(testY,prob,1);
  2900. % end
  2901. % %%
  2902. % % Plot
  2903. % load('D:\lsd\LSDvSaline24HrLogLOO.mat')
  2904. % figure
  2905. % hold on
  2906. % plot(mean(x24,1),mean(y24,1),'-k')
  2907. % plot(mean(x24P,1),mean(y24P,1),'--k')
  2908. % xlabel('FPR'); ylabel('TPR')
  2909. % title('LSD v. Saline 24 Hr: Log LOO')
  2910. % set(gca,'xtick',0:0.5:1,'ytick',0:0.5:1)
  2911. % legend({['Real: ',num2str(round(mean(a24),2)),'\pm',...
  2912. % num2str(round(conf(a24,0.95),2))],['Permuted: ',...
  2913. % num2str(round(mean(a24P),2)),'\pm',...
  2914. % num2str(round(conf(a24P,0.95),2))]},'location','se')
  2915. %% Modeling under run24HrDrugvBaseline.m
  2916. % %% Simple log LOO
  2917. % load('F:\lsd\lsdSalBaseline.mat')
  2918. % load('F:\lsd\LSDvSal24HrRel.mat')
  2919. % % LSD data
  2920. % lsd24N = numel(lsd24);
  2921. % lsdBaseN = numel(allLSDBase);
  2922. % c = 1; inds = [];
  2923. % for ii = 1:lsd24N
  2924. % for jj = 1:lsdBaseN
  2925. % inds(c,:) = [ii,jj];
  2926. % c = c+1;
  2927. % end
  2928. % end
  2929. % lsdX = []; lsdY = []; lsdA = []; betaLSD = [];
  2930. % lsdXP = []; lsdYP = []; lsdAP = [];
  2931. % for n = 1:6%size(inds,1)
  2932. % trainLSD24 = logicFind(1,~ismember(1:lsd24N,inds(n,1)),'==');
  2933. % testLSD24 = logicFind(0,~ismember(1:lsd24N,inds(n,1)),'==');
  2934. %
  2935. % trainLSDBase = logicFind(1,~ismember(1:lsdBaseN,inds(n,1)),'==');
  2936. % testLSDBase = logicFind(0,~ismember(1:lsdBaseN,inds(n,1)),'==');
  2937. % % LSD 24
  2938. % thisLSD24 = [];
  2939. % for ii = trainLSD24
  2940. % thisLSD24 = [thisLSD24;lsd24{ii}(randperm(size(lsd24{ii},1),...
  2941. % minSamp),:)];
  2942. % end
  2943. % lsd24Test = lsd24{testLSD24}(randperm(size(lsd24{testLSD24},1),...
  2944. % minSamp),:);
  2945. % % LSD Base
  2946. % thisLSDBase = [];
  2947. % for ii = trainLSDBase
  2948. % thisLSDBase = [thisLSDBase;allLSDBase{ii}(randperm(size(...
  2949. % allLSDBase{ii},1),minSamp),:)];
  2950. % end
  2951. % lsdBaseTest = allLSDBase{testLSDBase}(randperm(size(...
  2952. % allLSDBase{testLSDBase},1),minSamp),:);
  2953. % % Combine and build models
  2954. % trainLSDX{n} = [thisLSD24;thisLSDBase];
  2955. % trainLSDY{n} = [ones(size(thisLSD24,1),1);zeros(size(thisLSDBase,1),1)];
  2956. % testLSDX{n} = [lsd24Test;lsdBaseTest];
  2957. % testLSDY{n} = [ones(minSamp,1);zeros(minSamp,1)];
  2958. %
  2959. % mdlLSD{n} = fitglm(trainLSDX{n},trainLSDY{n},'distribution','binomial',...
  2960. % 'binomialSize',numel(trainLSDY{n}));
  2961. % prob = predict(mdlLSD{n},testLSDX{n});
  2962. % [lsdX(n,:),lsdY(n,:),~,lsdA(n)] = perfcurve(testLSDY{n},prob,1);
  2963. % betaLSD(:,:,n) = table2array(mdlLSD{n}.Coefficients);
  2964. % % Permuted
  2965. % mdl = fitglm(trainLSDX{n},trainLSDY{n}(randperm(numel(trainLSDY{n}),numel(trainLSDY{n}))),...
  2966. % 'distribution','binomial','binomialSize',numel(trainLSDY{n}));
  2967. % prob = predict(mdl,testLSDX{n});
  2968. % [lsdXP(n,:),lsdYP(n,:),~,lsdAP(n)] = perfcurve(testLSDY{n},prob,1);
  2969. % end
  2970. % % Saline data
  2971. % sal24N = numel(sal24);
  2972. % salBaseN = numel(allSalBase);
  2973. % c = 1; inds = [];
  2974. % for ii = 1:sal24N
  2975. % for jj = 1:salBaseN
  2976. % inds(c,:) = [ii,jj];
  2977. % c = c+1;
  2978. % end
  2979. % end
  2980. % salX = []; salY = []; salA = []; betaSal = [];
  2981. % salXP = []; salYP = []; salAP = [];
  2982. % for n = 1:5%size(inds,1)
  2983. % trainSal24 = logicFind(1,~ismember(1:sal24N,inds(n,1)),'==');
  2984. % testSal24 = logicFind(0,~ismember(1:sal24N,inds(n,1)),'==');
  2985. %
  2986. % trainSalBase = logicFind(1,~ismember(1:salBaseN,inds(n,1)),'==');
  2987. % testSalBase = logicFind(0,~ismember(1:salBaseN,inds(n,1)),'==');
  2988. % % Sal 24
  2989. % thisSal24 = [];
  2990. % for ii = trainSal24
  2991. % thisSal24 = [thisSal24;sal24{ii}(randperm(size(sal24{ii},1),...
  2992. % minSamp),:)];
  2993. % end
  2994. % sal24Test = sal24{testSal24}(randperm(size(sal24{testSal24},1),...
  2995. % minSamp),:);
  2996. % % Sal base
  2997. % thisSalBase = [];
  2998. % for ii = trainSalBase
  2999. % thisSalBase = [thisSalBase;allSalBase{ii}(randperm(size(...
  3000. % allSalBase{ii},1),minSamp),:)];
  3001. % end
  3002. % salBaseTest = allSalBase{testSalBase}(randperm(size(...
  3003. % allSalBase{testSalBase},1),minSamp),:);
  3004. % % Combine and build models
  3005. % trainSalX{n} = [thisSal24;thisSalBase];
  3006. % trainSalY{n} = [ones(size(thisSal24,1),1);zeros(size(thisSalBase,1),1)];
  3007. % testSalX{n} = [sal24Test;salBaseTest];
  3008. % testSalY{n} = [ones(minSamp,1);zeros(minSamp,1)];
  3009. %
  3010. % mdlSal{n} = fitglm(trainSalX{n},trainSalY{n},'distribution','binomial',...
  3011. % 'binomialSize',numel(trainSalY{n}));
  3012. % prob = predict(mdlSal{n},testSalX{n});
  3013. % [salX(n,:),salY(n,:),~,salA(n)] = perfcurve(testSalY{n},prob,1);
  3014. % betaSal(:,:,n) = table2array(mdlSal{n}.Coefficients);
  3015. % % Permuted
  3016. % mdl = fitglm(trainSalX{n},trainSalY{n}(randperm(numel(trainSalY{n}),numel(trainSalY{n}))),...
  3017. % 'distribution','binomial','binomialSize',numel(trainSalY{n}));
  3018. % prob = predict(mdl,testSalX{n});
  3019. % [salXP(n,:),salYP(n,:),~,salAP(n)] = perfcurve(testSalY{n},prob,1);
  3020. % end
  3021. % % Apply lsd mdl to sal and vice versa
  3022. % c = 1;
  3023. % for ii = 1:numel(mdlSal)
  3024. % for jj = 1:numel(mdlLSD)
  3025. % % LSD model to saline data
  3026. % prob = predict(mdlLSD{jj},testSalX{ii});
  3027. % [lsdSalX(c,:),lsdSalY(c,:),~,lsdSalA(c)] = perfcurve(testSalY{ii},prob,1);
  3028. %
  3029. % % saline model to LSD data
  3030. % prob = predict(mdlSal{ii},testLSDX{jj});
  3031. % [salLSDX(c,:),salLSDY(c,:),~,salLSDA(c)] = perfcurve(testLSDY{jj},prob,1);
  3032. % c = c+1;
  3033. % end
  3034. % end
  3035. % % Plot
  3036. % % LSD
  3037. % figure
  3038. % hold on
  3039. % plot(mean(lsdX,1),mean(lsdY,1),'-k')
  3040. % plot(mean(lsdXP,1),mean(lsdYP,1),'--k')
  3041. % plot(mean(salLSDX,1),mean(salLSDY,1),'-.k')
  3042. % xlabel('FPR'); ylabel('TPR')
  3043. % title('LSD 24 Hr vs. Baseline: Log LOO')
  3044. % set(gca,'xtick',0:0.5:1,'ytick',0:0.5:1)
  3045. % legend({['Real: ',num2str(round(mean(lsdA),2)),'\pm',...
  3046. % num2str(round(conf(lsdA,0.95),2))],['Permuted: ',...
  3047. % num2str(round(mean(lsdAP),2)),'\pm',...
  3048. % num2str(round(conf(lsdAP,0.95),2))],['Saline Model: ',...
  3049. % num2str(round(mean(salLSDA),2)),'\pm',...
  3050. % num2str(round(conf(salLSDA,0.95),2))]},'location','se')
  3051. % % Saline
  3052. % figure
  3053. % hold on
  3054. % plot(mean(salX,1),mean(salY,1),'-k')
  3055. % plot(mean(salXP,1),mean(salYP,1),'--k')
  3056. % plot(mean(lsdSalX,1),mean(lsdSalY,1),'-.k')
  3057. % xlabel('FPR'); ylabel('TPR')
  3058. % title('Sal 24 Hr vs. Baseline: Log LOO')
  3059. % set(gca,'xtick',0:0.5:1,'ytick',0:0.5:1)
  3060. % legend({['Real: ',num2str(round(mean(salA),2)),'\pm',...
  3061. % num2str(round(conf(salA,0.95),2))],['Permuted: ',...
  3062. % num2str(round(mean(salAP),2)),'\pm',...
  3063. % num2str(round(conf(salAP,0.95),2))],['LSD Model: ',...
  3064. % num2str(round(mean(lsdSalA),2)),'\pm',...
  3065. % num2str(round(conf(lsdSalA,0.95),2))]},'location','se')
  3066. %% New LSD + stim data
  3067. % load baselines
  3068. [baseData,baseSamps,baseFiles] = collateData('F:\LSD+stim\processed\baselines\',...
  3069. {'.mat'},{'pow','coh'},'trl','rel');
  3070. % get average and std of each animal
  3071. for ii = 1:8
  3072. these = contains(baseFiles{1},['LSD',num2str(ii)]);
  3073. baseCat{ii} = cat(1,baseData{1}{these});
  3074. baseM{ii} = mean(cat(1,baseData{1}{these}));
  3075. baseS{ii} = std(cat(1,baseData{1}{these}));
  3076. end
  3077. % load last stim days
  3078. [lastData,lastSamps,lastFiles] = collateData('F:\LSD+stim\processed\lastStim\',...
  3079. {'.mat'},{'pow','coh'},'trl','rel');
  3080. %%
  3081. % Find last stim epoch and split data into stim data and postStim data
  3082. for ii = 1:numel(lastFiles{1})
  3083. load(['F:\LSD+stim\processed\lastStim\',lastFiles{1}{ii}],'hist')
  3084. % Get animal ID
  3085. parts = strsplit(lastFiles{1}{ii},'-');
  3086. ID = str2double(parts{1}(end));
  3087. % Get stim times
  3088. if contains(lastFiles{1}{ii},'sham')
  3089. stimStart = 600;
  3090. stimStop = 6100;
  3091. else
  3092. stimStart = hist.eventTs.t{9}(nearest_idx3(600,hist.eventTs.t{9}));
  3093. stimStop = hist.eventTs.t{9}(end);
  3094. end
  3095. % Stim data
  3096. stimData{ii} = lastData{1}{ii}(...
  3097. lastSamps{1}{ii}(:,1)>(stimStart*2000) & ...
  3098. lastSamps{1}{ii}(:,2)<(stimStop*2000),:);%-baseM{ID})./baseS{ID};
  3099. % Post stim data
  3100. postStimData{ii} = lastData{1}{ii}(...
  3101. lastSamps{1}{ii}(:,2)>(stimStop*2000),:);%-baseM{ID})./baseS{ID};
  3102. end
  3103. %% First build models differentiating between stim and baseline
  3104. % code in runLSDstim.mat
  3105. for ii = 1:100
  3106. clear lsdStimA lsdStimAP lsdSham salStimA salStimAP salShamA salShamAP
  3107. load(['F:\LSD+stim\LSDvSAL_acute\LSDstim',num2str(ii),'.mat'])
  3108. lsdStimAs(ii) = lsdStimA{1}.acc;
  3109. lsdStimAPs(ii) = lsdStimAP{1}.acc;
  3110. lsdShamAs(ii) = lsdShamA{1}.acc;
  3111. lsdShamAPs(ii) = lsdShamAP{1}.acc;
  3112. if exist('salStimA')
  3113. salStimAs(ii) = salStimA{1}.acc;
  3114. else
  3115. salStimAs(ii) = NaN;
  3116. end
  3117. if exist('salStimAP')
  3118. salStimAPs(ii) = salStimAP{1}.acc;
  3119. else
  3120. salStimAPs(ii) = NaN;
  3121. end
  3122. if exist('salShamA')
  3123. salShamAs(ii) = salShamA{1}.acc;
  3124. else
  3125. salShamAs(ii) = NaN;
  3126. end
  3127. if exist('salShamAP')
  3128. salShamAPs(ii) = salShamAP{1}.acc;
  3129. else
  3130. salShamAPs(ii) = NaN;
  3131. end
  3132. end
  3133. %%
  3134. figure
  3135. subplot(2,2,1)
  3136. [f,xi,bw] = ksdensity(lsdStimAPs);
  3137. fill(xi,f*bw,'w')
  3138. plotSpread(lsdStimAPs','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  3139. plot([mean(lsdStimAs) mean(lsdStimAs)],[0 0.25],'-')
  3140. subplot(2,2,2)
  3141. [f,xi,bw] = ksdensity(lsdShamAPs);
  3142. fill(xi,f*bw,'w')
  3143. plotSpread(lsdShamAPs','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  3144. plot([mean(lsdShamAs) mean(lsdShamAs)],[0 0.25],'-')
  3145. subplot(2,2,3)
  3146. [f,xi,bw] = ksdensity(salStimAPs);
  3147. fill(xi,f*bw,'w')
  3148. plotSpread(salStimAPs','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  3149. plot([mean(salStimAs) mean(salStimAs)],[0 0.25],'-')
  3150. subplot(2,2,4)
  3151. [f,xi,bw] = ksdensity(salShamAPs);
  3152. fill(xi,f*bw,'w')
  3153. plotSpread(salShamAPs','xyori','flipped','xvalues',0.05,'spreadWidth',0.1)
  3154. plot([mean(salShamAs) mean(salShamAs)],[0 0.25],'-')
  3155. %%
  3156. thisDiff = [];
  3157. for ii = 1:60
  3158. thisDiff = [thisDiff;lsdStimAPs(ii)-lsdShamAPs(ii);...
  3159. lsdStimAPs(ii)-salStimAPs(ii);...
  3160. lsdStimAPs(ii)-salShamAPs(ii);...
  3161. lsdShamAPs(ii)-salStimAPs(ii);...
  3162. lsdShamAPs(ii)-salShamAPs(ii);...
  3163. salStimAPs(ii)-salShamAPs(ii)];
  3164. end
  3165. %% Then use post stim effects
  3166. % code in runLSDpostSim.mat
  3167. [lsdPostStimAs,lsdPostStimAPs,lsdPostShamAs,lsdPostShamAPs,...
  3168. salPostStimAs,salPostStimAPs,salPostShamAs,salPostShamAPs] = deal([]);
  3169. for ii = 1:60
  3170. load(['F:\LSD+stim\LSDvsSAL_post\LSDpostStim',num2str(ii),'.mat'])
  3171. lsdPostStimAs(ii) = lsdStimPostA{1}.acc;
  3172. lsdPostStimAPs(ii) = lsdStimPostAP{1}.acc;
  3173. lsdPostShamAs(ii) = lsdShamPostA{1}.acc;
  3174. lsdPostShamAPs(ii) = lsdShamPostAP{1}.acc;
  3175. salPostStimAs(ii) = salStimPostA{1}.acc;
  3176. salPostStimAPs(ii) = salStimPostAP{1}.acc;
  3177. salPostShamAs(ii) = salShamPostA{1}.acc;
  3178. salPostShamAPs(ii) = salShamPostAP{1}.acc;
  3179. end
  3180. %% Then use baseline normalized post days in X minute bins
  3181. [postData,postSamps,postFiles] = collateData(...
  3182. 'F:\LSD+stim\processed\postStim\',{'.mat'},{'pow','coh'},'trl','');
  3183. % Number of minutes per bin
  3184. bin = 5;
  3185. binSamps = bin*60*2000;
  3186. % Total length of data to look at (minutes)
  3187. total = 30;
  3188. totalSamps = total*60*2000;
  3189. for ii = 1:numel(postFiles{1})
  3190. for k = 1:total/bin
  3191. thisStart = (k-1)*binSamps+1;
  3192. thisStop = k*binSamps;
  3193. inds = postSamps{1}{ii}(:,1)>=thisStart & ...
  3194. postSamps{1}{ii}(:,2)<=thisStop;
  3195. post{ii,k} = postData{1}{ii}(inds,:);
  3196. end
  3197. end
  3198. %% Build models for each bin
  3199. stimInd = logical([1,0,0,1,1,1,0,0,1,0,0,0,0]);
  3200. shamInd = ~stimInd;
  3201. lsdInd = logical([1,0,1,0,0,1,0,1,1,0,1,0,1]);
  3202. salInd = ~lsdInd;
  3203. n = 50;
  3204. for ii = 1:100
  3205. for k = 1:size(post,2)
  3206. for jj = 1:size(post,1)
  3207. % Grab n samples, or weight as needed
  3208. if size(post{jj,k},1) < n
  3209. thisPost{jj,k} = post{jj,k};
  3210. thisWeight{jj,k} = repmat(n/size(post{jj,k},1),...
  3211. size(post{jj,k},1),1);
  3212. else
  3213. thisPost{jj,k} = post{jj,k}(randperm(size(post{jj,k},1),...
  3214. n),:);
  3215. thisWeight{jj,k} = ones(n,1);
  3216. end
  3217. end
  3218. % Combine into stim and sham groups
  3219. allStim = cat(1,thisPost{stimInd,k});
  3220. allSham = cat(1,thisPost{shamInd,k});
  3221. allW = cat(1,thisWeight{stimInd,k},thisWeight{shamInd,k});
  3222. % 80:20 split
  3223. [trainX,trainY,testX,testY,trainInd] = trainTest([allStim;allSham],...
  3224. [ones(size(allStim,1),1);zeros(size(allSham,1),1)],0.2);
  3225. cfg = lassoNetCfg({testX,testY},[],'n','n','n',100,'1se',...
  3226. allW(trainInd));
  3227. [~,~,~,~,accPostStim{ii,k},~] = lassoNet(trainX,trainY,'binomial',...
  3228. 'class',1,10,1,cfg);
  3229. % Permuted
  3230. stimIndPerm = randperm(numel(postFiles{1}),sum(stimInd));
  3231. shamIndPerm = randperm(numel(postFiles{1}),sum(shamInd));
  3232. allStimPerm = cat(1,thisPost{stimIndPerm,k});
  3233. allShamPerm = cat(1,thisPost{shamIndPerm,k});
  3234. allWPerm = cat(1,thisWeight{stimIndPerm,k},...
  3235. thisWeight{shamIndPerm,k});
  3236. [trainX,trainY,testX,testY,trainInd] = trainTest([allStimPerm;...
  3237. allShamPerm],[ones(size(allStimPerm,1),1);...
  3238. zeros(size(allShamPerm,1),1)],0.2);
  3239. cfg = lassoNetCfg({testX,testY},[],'n','n','n',100,'1se',[]);
  3240. [~,~,~,~,accPostStimP{ii,k},~] = lassoNet(trainX,trainY,...
  3241. 'binomial','class',1,10,1,cfg);
  3242. end
  3243. end
  3244. %% Models of every stim day, using that day's baseline and post stim
  3245. [stimData,stimSamps,stimFiles] = collateData(...
  3246. 'F:\LSD+stim\processed\allStim\',{'.mat'},{'pow','coh'},'trl','rel');
  3247. %%
  3248. [lsdSham,lsdStim,salSham,salStim] = deal(cell(1,2));
  3249. for ii = 1:numel(stimFiles{1})
  3250. load(stimFiles{1}{ii},'hist')
  3251. if contains(stimFiles{1}{ii},'sham')
  3252. firstStim = 600;
  3253. lastStim = 6100;
  3254. else
  3255. firstStim = hist.eventTs.t{9}(nearest_idx3(600,hist.eventTs.t{9}));
  3256. lastStim = hist.eventTs.t{9}(end);
  3257. end
  3258. thisPre = stimData{1}{ii}(stimSamps{1}{ii}(:,1)<firstStim*2000,:);
  3259. thisPost = stimData{1}{ii}(stimSamps{1}{ii}(:,2)>lastStim*2000,:);
  3260. if contains(stimFiles{1}{ii},'sham') && contains(stimFiles{1}{ii},'_LSD_')
  3261. lsdSham = [lsdSham;{thisPre},{thisPost}];
  3262. end
  3263. if contains(stimFiles{1}{ii},'sham') && contains(stimFiles{1}{ii},'_SAL_')
  3264. salSham = [salSham;{thisPre},{thisPost}];
  3265. end
  3266. if contains(stimFiles{1}{ii},'sIL') && contains(stimFiles{1}{ii},'_LSD_')
  3267. lsdStim = [lsdStim;{thisPre},{thisPost}];
  3268. end
  3269. if contains(stimFiles{1}{ii},'sIL') && contains(stimFiles{1}{ii},'_SAL_')
  3270. salStim = [salStim;{thisPre},{thisPost}];
  3271. end
  3272. end
  3273. lsdSham(1,:) = [];
  3274. lsdStim(1,:) = [];
  3275. salSham(1,:) = [];
  3276. salStim(1,:) = [];
  3277. %%
  3278. n = 50;
  3279. % LSD stim
  3280. for k = 1:100
  3281. disp(k)
  3282. this = [];
  3283. for ii = 1:size(lsdStim,1)
  3284. this{ii,1} = lsdStim{ii,1}(randperm(size(lsdStim{ii,1},1),n),:);
  3285. % this{ii,2} = lsdStim{ii,2}(randperm(size(lsdStim{ii,2},1),n),:);
  3286. this{ii,2} = lsdStim{ii,2}(1:50,:);
  3287. end
  3288. [trainX,trainY,testX,testY] = trainTest(cat(1,this{:,1},this{:,2}),...
  3289. cat(1,ones(size(lsdStim,1)*n,1),zeros(size(lsdStim,1)*n,1)),0.2);
  3290. mdl = fitglm(trainX,trainY,'distribution','binomial');
  3291. prob = predict(mdl,testX);
  3292. [~,~,~, lsdStimA(k)] = perfcurve(testY,prob,1);
  3293. % Permuted
  3294. these = logical(round(rand(size(lsdStim,1),1)));
  3295. [trainX,trainY,testX,testY] = trainTest(cat(1,this{these,1},...
  3296. this{~these,2},this{~these,1},this{these,2}),[ones(sum(these)*n*2,1);...
  3297. zeros(sum(~these)*n*2,1)],0.2);
  3298. mdl = fitglm(trainX,trainY,'distribution','binomial');
  3299. prob = predict(mdl,testX);
  3300. [~,~,~,lsdStimAP(k)] = perfcurve(testY,prob,1);
  3301. % LSD sham
  3302. this = [];
  3303. for ii = 1:size(lsdSham,1)
  3304. this{ii,1} = lsdSham{ii,1}(randperm(size(lsdSham{ii,1},1),n),:);
  3305. this{ii,2} = lsdSham{ii,2}(randperm(size(lsdSham{ii,2},1),n),:);
  3306. end
  3307. [trainX,trainY,testX,testY] = trainTest(cat(1,this{:,1},this{:,2}),...
  3308. cat(1,ones(size(lsdSham,1)*n,1),zeros(size(lsdSham,1)*n,1)),0.2);
  3309. mdl = fitglm(trainX,trainY,'distribution','binomial');
  3310. prob = predict(mdl,testX);
  3311. [~,~,~,lsdShamA(k)] = perfcurve(testY,prob,1);
  3312. % Permuted
  3313. these = logical(round(rand(size(lsdSham,1),1)));
  3314. [trainX,trainY,testX,testY] = trainTest(cat(1,this{these,1},...
  3315. this{~these,2},this{~these,1},this{these,2}),[ones(sum(these)*n*2,1);...
  3316. zeros(sum(~these)*n*2,1)],0.2);
  3317. mdl = fitglm(trainX,trainY,'distribution','binomial');
  3318. prob = predict(mdl,testX);
  3319. [~,~,~,lsdShamAP(k)] = perfcurve(testY,prob,1);
  3320. % SAL stim
  3321. this = [];
  3322. for ii = 1:size(salStim,1)
  3323. this{ii,1} = salStim{ii,1}(randperm(size(salStim{ii,1},1),n),:);
  3324. this{ii,2} = salStim{ii,2}(randperm(size(salStim{ii,2},1),n),:);
  3325. end
  3326. [trainX,trainY,testX,testY] = trainTest(cat(1,this{:,1},this{:,2}),...
  3327. cat(1,ones(size(salStim,1)*n,1),zeros(size(salStim,1)*n,1)),0.2);
  3328. mdl = fitglm(trainX,trainY,'distribution','binomial');
  3329. prob = predict(mdl,testX);
  3330. [~,~,~,salStimA(k)] = perfcurve(testY,prob,1);
  3331. % Permuted
  3332. these = logical(round(rand(size(salStim,1),1)));
  3333. [trainX,trainY,testX,testY] = trainTest(cat(1,this{these,1},...
  3334. this{~these,2},this{~these,1},this{these,2}),[ones(sum(these)*n*2,1);...
  3335. zeros(sum(~these)*n*2,1)],0.2);
  3336. mdl = fitglm(trainX,trainY,'distribution','binomial');
  3337. prob = predict(mdl,testX);
  3338. [~,~,~,salStimAP(k)] = perfcurve(testY,prob,1);
  3339. % SAL sham
  3340. this = [];
  3341. for ii = 1:size(salSham,1)
  3342. this{ii,1} = salSham{ii,1}(randperm(size(salSham{ii,1},1),n),:);
  3343. this{ii,2} = salSham{ii,2}(randperm(size(salSham{ii,2},1),n),:);
  3344. end
  3345. [trainX,trainY,testX,testY] = trainTest(cat(1,this{:,1},this{:,2}),...
  3346. cat(1,ones(size(salSham,1)*n,1),zeros(size(salSham,1)*n,1)),0.2);
  3347. mdl = fitglm(trainX,trainY,'distribution','binomial');
  3348. prob = predict(mdl,testX);
  3349. [~,~,~,salShamA(k)] = perfcurve(testY,prob,1);
  3350. % Permuted
  3351. these = logical(round(rand(size(salSham,1),1)));
  3352. [trainX,trainY,testX,testY] = trainTest(cat(1,this{these,1},...
  3353. this{~these,2},this{~these,1},this{these,2}),[ones(sum(these)*n*2,1);...
  3354. zeros(sum(~these)*n*2,1)],0.2);
  3355. mdl = fitglm(trainX,trainY,'distribution','binomial');
  3356. prob = predict(mdl,testX);
  3357. [~,~,~,salShamAP(k)] = perfcurve(testY,prob,1);
  3358. end
  3359. %% Build model of LSD+stim vs base; apply over post