Apply alignment EEG positions to fsaverage

Hi everyone,

I have scanned the positions of my electrodes using a 3D scan like the one use in fieldtrip (Localizing electrodes using a 3D-scanner - FieldTrip toolbox). As it is described in that tutorial, the coordinate system in which my electrode positions are after is the CTF system. Now, what I have done, is import the locations into mne and using the code you see below, to add the positions to my raw data. After that, I have used the fig = mne.viz.plot_alignment(ā€¦) to align it to the fsaverage, since I have the individual EEG positions but not the individual MRI. However, what I donā€™t understand or I donā€™t know how to do, is how can I now save the ā€œalignedā€ positions to my raw dataset. Are these already saved but not applied? I want to do this, before I continue with my preprocessing (next step is ICA), since afterwards, we will perform source reconstruction.

Load the json file

with open(pathOut + fā€™{partID}/ā€™ + fā€™sub-{participant}_positionElectrodes_NEW_fiducials.jsonā€™) as f:
mat_file = json.load(f)

factorUnits = 1000 # Convert mm to m
elecpos_data = np.array(mat_file[ā€˜elecposā€™]).reshape(67,3) / factorUnits

Ensure channel names match those in the raw data

ch_names = mat_file[ā€˜labelā€™]

Create a dictionary with channel names as keys and 3D coordinates as values

channel_positions = {ch_names[i]: elecpos_data[i] for i in range(len(ch_names))}

#Find indices for the fiducial points

nas = channel_positions[ā€˜nasā€™]
lpa = channel_positions[ā€˜lpaā€™]
rpa = channel_positions[ā€˜rpaā€™]

#%% Montage creation and alignment

Create a new montage with the rotated positions

montage = mne.channels.make_dig_montage(ch_pos=channel_positions, nasion=nas, lpa=lpa, rpa=rpa)

raw.set_montage(montage)
raw.set_eeg_reference(projection=True) # needed for inverse modeling

#SECOND PART CODE

Step 1: Download and set the fsaverage template MRI

subject = ā€˜fsaverageā€™
subjects_dir = rā€™C:\Users\HuertasPenenS\mne_data\MNE-fsaverage-dataā€™
src = rā€™C:\Users\HuertasPenenS\mne_data\MNE-fsaverage-data\fsaverage\bem\fsaverage-ico-5-src.fifā€™

Load transformation matrix

trans = rā€™C:\Users\HuertasPenenS\mne_data\MNE-fsaverage-data\fsaverage\bem\fsaverage-trans.fifā€™

Step 4: Plot the alignment (head surface and EEG electrodes)

fig = mne.viz.plot_alignment(
raw.info,
subject=subject,
subjects_dir=subjects_dir,
src = src,
surfaces=[ā€˜headā€™], # Plot the head surface
coord_frame=ā€˜mriā€™, # Use MRI coordinate frame
eeg=[ā€˜originalā€™,ā€˜projectedā€™], # Plot EEG electrodes
trans=trans, # Use the identity transformation matrix
show_axes=True,
mri_fiducials=True,
dig=ā€œfiducialsā€,
)
MNE VERSION: 1.7.1
WINDOWS: 11