a2_visual_inspection.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. %% 02_visual_inspection
  2. % manually label bad segments
  3. %% initialize
  4. restoredefaultpath;
  5. clear all; close all; pack; clc;
  6. %% pathdef
  7. currentFile = mfilename('fullpath');
  8. [pathstr,~,~] = fileparts( currentFile );
  9. cd(pathstr)
  10. % inputs
  11. pn.eeg_BIDS = fullfile('..', 'data', 'inputs', 'rawdata', 'eeg_BIDS');
  12. pn.channel_locations = fullfile('..', 'data', 'inputs', 'rawdata', 'channel_locations');
  13. pn.events = fullfile('..', 'data', 'inputs', 'rawdata', 'events');
  14. % outputs
  15. pn.eeg_ft = fullfile('..', 'data', 'outputs', 'eeg'); if ~exist(pn.eeg_ft); mkdir(pn.eeg_ft); end
  16. pn.history = fullfile('..', 'data', 'outputs', 'history'); if ~exist(pn.history); mkdir(pn.history); end
  17. % add fieldtrip toolbox
  18. addpath(fullfile('..', 'tools', 'fieldtrip')); ft_defaults;
  19. %% define IDs for visual screening
  20. % N = 33
  21. IDs = tdfread(fullfile(pn.eeg_BIDS, 'participants.tsv'));
  22. IDs = cellstr(IDs.participant_id);
  23. %% loop IDs
  24. for id = 1:length(IDs)
  25. %% load data, start screening
  26. clc;
  27. ID = str2num(IDs{id}); disp(['Processing ',IDs{id}]);
  28. % load config
  29. load(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'config');
  30. % load data
  31. load(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_raw.mat']),'data_eeg');
  32. % data browser
  33. if ~isfield(config,'visual_inspection')
  34. cfg = [];
  35. cfg.continuous = 'yes';
  36. cfg.preproc.demean = 'yes';
  37. cfg.blocksize = 400;
  38. cfg.ylim = [-50 50];
  39. cfg.viewmode = 'vertical';
  40. cfg.plotlabels = 'yes';
  41. cfg.linecolor = [.2 .2 .2];
  42. else
  43. cfg = config.visual_inspection;
  44. end
  45. cfg.channel = {'all'};
  46. % inspect data
  47. cfg = ft_databrowser(cfg,data_eeg);
  48. % check if ICA conducted on this dataset; if so new artifacts not saved
  49. if ~isfield(config,'ica1')
  50. % marked segments
  51. reject = cfg.artfctdef.visual.artifact;
  52. trl = [];
  53. % generate "trial" (i.e. segment) structure for ICA
  54. if exist('reject','var')
  55. if min(size(reject)) ~= 0 % JQK fix: 170915
  56. for j = 1:size(reject,1)+1
  57. if j == 1
  58. trl(j,:) = [1 reject(j,1)-1 0];
  59. elseif j <= size(reject,1)
  60. trl(j,:) = [reject(j-1,2)+1 reject(j,1)-1 0];
  61. else
  62. trl(j,:) = [reject(j-1,2)+1 size(data_eeg.trial{1},2) 0];
  63. end
  64. end; clear j
  65. else
  66. trl = [1 size(data_eeg.trial{1},2) 0];
  67. end
  68. end
  69. % eliminate trials without data points
  70. ex = find((trl(:,1)-trl(:,2))==0);
  71. trl(ex,:) = []; clear ex
  72. % eliminate trials shorter than 3 sec (! srate = 250 Hz)
  73. ex = find((trl(:,2)-trl(:,1))<750);
  74. trl(ex,:) = []; clear ex
  75. % add to config structure
  76. config.visual_inspection = cfg;
  77. config.trl_ica1 = trl;
  78. % save config
  79. save(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'config');
  80. else
  81. warning('ICA already conducted. Changes not saved.')
  82. end
  83. end % id loop