findBoundsProcesStages.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. % Appies HSMM-MVPA as in Anderson 2016 to find the boundaries of cognitive
  2. % processing stages
  3. bumpM = 4; % number of bumps, 4 bumps == 5 stages
  4. nBuMax = max(bumpM);
  5. nModel = length(bumpM);
  6. nPca = 32; % 32 PCA -> more than 95% variance, 25 -> more than 85%; 20 -> 80% variance on PCA
  7. nSj = 18;
  8. rep = 150;
  9. shape = 2; % gamma distributoin, shape parameter
  10. roiDataR = 'nScoreAveR'; % 'nScoreAve' or 'nScorePca'
  11. dataPath = './data';
  12. dataRead = '/vars4hsmmBL_R.mat';
  13. roiTypeR = ['roi' roiDataR(end-3:end)];
  14. roiTypeR(end) = [];
  15. saveName = ['/hmmBu' num2str(bumpM) 'R' roiTypeR 'Map3StagBL.mat'];
  16. addpath('./hsmm/');
  17. info = {['test ', num2str(bumpM),' bumps n repetitions one models: R; map 4 stage, PCA cov by subject, Baseline']};
  18. load([dataPath, dataRead], roiDataR, 'condR','xR','yR','subjtR')
  19. % right hand
  20. data_ = eval(roiDataR);
  21. data = single(data_(:,1:nPca));
  22. x = xR;
  23. y = yR;
  24. condR_ = condR;
  25. condR_(logical( condR==2 | condR==3 )) = 1; % fan1-target
  26. condR_(logical( condR==4 | condR==5 )) = 2; % fan2-target
  27. condR_(logical( condR==6 | condR==7 )) = 3; % fan1-foil
  28. condR_(logical( condR==8 | condR==9 )) = 4; % fan2-foil
  29. cond = condR_;
  30. subj = subjtR;
  31. % left hand
  32. clear data_ roiDataR xR yR condR condR_ subjtR
  33. % define map settings, 3th stage (retrieval) different gamma
  34. map = [1, 1, 1, 1, 1;
  35. 1, 1, 2, 1, 1;
  36. 1, 1, 3, 1, 1;
  37. 1, 1, 4, 1, 1];
  38. gamIni = repmat([2 200], nBuMax+1,1);
  39. magIni = -1 + 2*rand(nPca, nBuMax, rep);
  40. % bump kernel correlation, removed form inside HSMM
  41. data = calcBumps(data{1,1});
  42. lkhr = zeros(repnModel); % [repetitions, hand response, models(number bumps),]
  43. parpool(30);
  44. parfor r = 1:rep
  45. % do HSMM
  46. for m = 1:nModel % iterate models
  47. [lkhr(r,m),~,~,~]=hsmmEEG50CondMapAllDNew(data,cond,magIni(:,1:bumpM(m),r),gamIni(1:bumpM(m)+1,:),1,x,y,map,shape);
  48. end
  49. end
  50. end
  51. disp('End of random initializations')
  52. % recompute best models
  53. lkh = cell(1,nModel); % hand, model(nBump)
  54. bumpMag = cell(1,nModel);
  55. flatGam = cell(1,nModel);
  56. proBump = cell(1,nModel);
  57. for m = 1:nModel
  58. [~,iBest] = max(lkhr(:,m));
  59. [lkh{1,m},bumpMag{1,m},flatGam{1,m},proBump{1,m}] = hsmmEEG50CondMapAllDNew(data,cond,magIni(:,1:bumpM(m),iBest),gamIni(1:bumpM(m)+1,:),1,x,y,map,shape);
  60. end
  61. % LOOCV, leave-one-out cross validation
  62. lkhCV = zeros(nSj,nModel);
  63. parfor sj = 1:nSj
  64. for m = 1:nModel % iterate models
  65. sTrain = logical(subj ~= sj);
  66. sTest = logical(subj == sj);
  67. [~, magTrain, gammTrain, ~] = hsmmEEG50CondMapAllDNew(data,cond(sTrain),bumpMag{1,m},flatGam{1,m}, 1 ,x{h}(sTrain),y(sTrain),map,shape);
  68. [ lkhCV(sj, m), ~, ~, ~] = hsmmEEG50CondMapAllDNew(data,cond(sTest), magTrain, gammTrain, 0 ,x(sTest),y(sTest),map,shape);
  69. end
  70. end
  71. %save(['./dataSave' saveName], 'lkhr','lkh','bumpMag','flatGam','proBump','info','nPca', 'rep', 'x','y','cond','lkhCV','subj')
  72. %format longG
  73. disp(char(datetime(now,'ConvertFrom','datenum')))
  74. delete(gcp('nocreate'))
  75. clear all
  76. close all