Skip to content

Instantly share code, notes, and snippets.

@rcassani
Created November 14, 2024 20:56
Show Gist options
  • Select an option

  • Save rcassani/02cb97a77063d8794879f567b310cc0a to your computer and use it in GitHub Desktop.

Select an option

Save rcassani/02cb97a77063d8794879f567b310cc0a to your computer and use it in GitHub Desktop.
Brainstorm: Project EEG sensors on scalp for registered EEG caps and default anatomies
% Script for Brainstorm
% For the provided anatomies ('testAnatomies'), find their already registered EEG caps
% and adjust the location of the EEG sensor if request (isReplaceLocs) and if
% one or more EEG sensors were moved more than ('distThreshold') millimeters
%
% Raymundo Cassani, 2024
%% ===== PARAMETERS =====
testAnatomies = {'Colin27_2016', 'ICBM152_2023b'};
isReplaceLocs = 1;
distThreshold = 1; % mm
%% ===== CREATE PROTOCOL =====
ProtocolName = 'EEGcaps_project_on_scalp';
% Start brainstorm without the GUI
if ~brainstorm('status')
brainstorm nogui
end
% Delete existing protocol
gui_brainstorm('DeleteProtocol', ProtocolName);
% Create new protocol
gui_brainstorm('CreateProtocol', ProtocolName, 0, 0);
% Default anatomies
anatTemplates = bst_get('AnatomyDefaults');
% EEG caps
eegDefaults = bst_get('EegDefaults');
%% ===== PROJECT EEG CAPS =====
for iAnatomy = 1 : length(testAnatomies)
% Create Subject using the requested anatomy
AnatomyFamily = regexprep(testAnatomies{iAnatomy}, '_.*', '');
[~, iSubject] = db_add_subject(AnatomyFamily, [], 0, 0);
iAnatTemplate = find(strcmpi({anatTemplates.Name}, testAnatomies{iAnatomy}));
if isempty(iAnatTemplate) || length(iAnatTemplate) > 1
error('Could not identify anatomy template');
end
% Set default anatomy
db_set_template(iSubject, anatTemplates(iAnatTemplate), 0);
[~, iSubject] = bst_get('Subject', iSubject);
% Get EEG caps for this anatomy family
iCapFamily = find(strcmpi({eegDefaults.name}, AnatomyFamily));
if isempty(iCapFamily) || length(iCapFamily) > 1
error('Could not identify EEG caps for this anatomy family');
end
% Check caps
for iCap = 1 : length(eegDefaults(iCapFamily).contents)
capName = eegDefaults(iCapFamily).contents(iCap).name;
% Create conditions
iStudyOrg = db_add_condition(iSubject, [capName, '_orginal']);
iStudyPrj = db_add_condition(iSubject, [capName, '_project_scalp']);
% Set EEG cap for both
channelFileOrg = db_set_channel(iStudyOrg, eegDefaults(iCapFamily).contents(iCap).fullpath, 1, 0);
channelFilePrj = db_set_channel(iStudyPrj, eegDefaults(iCapFamily).contents(iCap).fullpath, 1, 0);
% Project EEG sensors on scalp for '_project_scalp' Study
process_channel_project('Compute', channelFilePrj, 'EEG');
% Load locations for both caps
sChannelMatOrg = in_bst_channel(channelFileOrg);
sChannelMatPrj = in_bst_channel(channelFilePrj);
% Compute metric
[Lia, Locb] = ismember({sChannelMatOrg.Channel.Name}, {sChannelMatPrj.Channel.Name});
distances = sqrt(sum(([sChannelMatOrg.Channel.Loc] - [sChannelMatPrj.Channel(Locb).Loc]).^2, 1));
eegDefaults(iCapFamily).contents(iCap).maxDist = max(distances);
eegDefaults(iCapFamily).contents(iCap).avgDist = mean(distances);
if isReplaceLocs && max(distances) > (distThreshold / 1000)
% Save modifications in channel file
bst_save(eegDefaults(iCapFamily).contents(iCap).fullpath, sChannelMatPrj, 'v7');
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment