Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

a3_ica.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. function a3_ica(id, rootpath)
  2. if ismac % run if function is not pre-compiled
  3. id = '5'; % test for example subject
  4. currentFile = mfilename('fullpath');
  5. [pathstr,~,~] = fileparts(currentFile);
  6. cd(fullfile(pathstr,'..', '..'))
  7. rootpath = pwd;
  8. end
  9. % inputs
  10. pn.eeg_BIDS = fullfile(rootpath, 'data', 'inputs', 'rawdata', 'eeg_BIDS');
  11. pn.channel_locations = fullfile(rootpath, 'data', 'inputs', 'rawdata', 'channel_locations');
  12. pn.events = fullfile(rootpath, 'data', 'inputs', 'rawdata', 'events');
  13. pn.tools = fullfile(rootpath, 'tools');
  14. % outputs
  15. pn.eeg_ft = fullfile(rootpath, 'data', 'outputs', 'eeg');
  16. pn.history = fullfile(rootpath, 'data', 'outputs', 'history');
  17. if ismac % run if function is not pre-compiled
  18. addpath(fullfile(pn.tools, 'fieldtrip')); ft_defaults;
  19. addpath(fullfile(pn.tools, 'helpers'));
  20. end
  21. %% define IDs for visual screening
  22. % N = 33
  23. IDs = tdfread(fullfile(pn.eeg_BIDS, 'participants.tsv'));
  24. IDs = cellstr(IDs.participant_id);
  25. id = str2num(id);
  26. display(['processing ' num2str(IDs{id})]);
  27. if ~exist(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_ica.mat']),'file')
  28. %% load raw data & exclude parts containing artifacts
  29. % load config
  30. config = [];
  31. load(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'config');
  32. config_tmp.(['trl']) = config.trl_ica1;
  33. config_tmp.(['visual_inspection']) = config.visual_inspection;
  34. % define segment(s) to be read by fieldtrip
  35. cfg.trl = config.trl_ica1;
  36. % load data
  37. load(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_raw.mat']),'data_eeg');
  38. % remove M2 (collinear with M1 due to avg. mastoid REF: mean(M1,M2)=0)
  39. cfg_preproc = [];
  40. cfg_preproc.channel = {'all', '-M2'};
  41. data_eeg = ft_preprocessing(cfg_preproc, data_eeg);
  42. % adjust final length
  43. cfg.trl(end,2) = size(data_eeg.trial{1},2);
  44. % "segment" data
  45. data = cm_segmentation_of_continuous_data_fieldtrip_20150825(data_eeg,cfg);
  46. % clear cfg structure
  47. clear cfg
  48. %% segmentation for ICA
  49. % define settings
  50. cfg.length = 2;
  51. cfg.n = 5000; % keep all possible trials
  52. cfg.type = 'rnd'; % select trials randomly if > 1500 trials available
  53. cfg.seed = 20170915 + str2num(IDs{id});
  54. % arbitrary segmentation - segments a 2 sec
  55. % NOTE original segments will be overwritten
  56. data = cm_arbitrary_segmentation_fieldtrip_20151002(data,cfg);
  57. %% ICA
  58. % date
  59. dt = date;
  60. % ica config
  61. cfg.method = 'runica';
  62. cfg.channel = {'all'}; % additional channels should be excluded already...
  63. cfg.trials = 'all';
  64. cfg.numcomponent = 'all';
  65. cfg.demean = 'no';
  66. cfg.runica.extended = 1;
  67. cfg.runica.logfile = fullfile(pn.history, ['log_' IDs{id} '_ica1_' dt '.txt']);
  68. % run ICA
  69. icadat = ft_componentanalysis(cfg,data);
  70. %% automatic ICA labeling
  71. [iclabels] = cm_automatic_IC_detection_20170919(data,icadat);
  72. %% save data for ICA labeling
  73. config.trl = config_tmp.trl;
  74. config.visual_inspection = config_tmp.visual_inspection;
  75. % - include ICA solution in data
  76. data.topo = icadat.topo;
  77. data.unmixing = icadat.unmixing;
  78. data.topolabel = icadat.topolabel;
  79. data.cfg = icadat.cfg;
  80. % - include ICA solution in config
  81. config.ica1.date = dt;
  82. config.ica1.topo = icadat.topo;
  83. config.ica1.unmixing = icadat.unmixing;
  84. config.ica1.topolabel = icadat.topolabel;
  85. config.ica1.cfg = icadat.cfg;
  86. config.ica1.iclabels.auto = iclabels;
  87. % fieldtrip format electrode information
  88. chanlocs_besa = readtable(fullfile(pn.channel_locations, 'chanlocs_besa.txt'));
  89. elec.pnt = [chanlocs_besa.Var2, chanlocs_besa.Var3, chanlocs_besa.Var4];
  90. elec.label = chanlocs_besa.Var1;
  91. data.elec = elec;
  92. % EEGLAB format electrode information
  93. chanlocs_ced = readtable(fullfile(pn.channel_locations, 'chanlocs_ced.txt'));
  94. chanlocs_ced = table2struct(chanlocs_ced);
  95. chanlocs_ced = rmfield(chanlocs_ced, {'Number', 'type', 'Var12'});
  96. [~, loc] = ismember(data.label, {chanlocs_ced.labels});
  97. data.chanlocs(1,loc) = chanlocs_ced(loc,1);
  98. % - include channel information in config
  99. config.elec = data.elec;
  100. config.chanlocs = data.chanlocs;
  101. % keep ICA labels
  102. data.iclabels = iclabels;
  103. % save data
  104. save(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_ica.mat']),'data');
  105. % save config
  106. config.preproc_version = '20170915';
  107. save(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'config');
  108. end % file available