@Davi1990 I would recommend you not to modify the actual coordinate vales if its not required. All you need to check what values MNE expect (millimeters/meters) while setting up the montage. If its a must to re-scale the coordinates then (only) perform the re-scale and create montage afterwards.
Here is the code for your solution:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: diptyajit das <bmedasdiptyajit@gmail.com>
Created on Apr 25, 2023
Task: current electrode positions look wrong, require re-scaling?
"""
print(__doc__)
# import necessary modules
import mne
from mne import read_epochs
from mne.channels import make_dig_montage
# ---------------------------------
# test code:
# epoch file
fname = '/home/dip_meg/Downloads/sub-02-epoched.fif'
epo = read_epochs(fname)
# re-scale factor (current electrode positions are wrong, needs re-scaling )
scale_factor = 1000
# fiducial points # re-scale
lpa = epo.info['dig'][0].get('r')/scale_factor
nasion = epo.info['dig'][1].get('r')/scale_factor
rpa = epo.info['dig'][2].get('r')/scale_factor
fid_points = [lpa, nasion, rpa]
# get the cardinal points
digi = epo.info['dig']
# extract eeg sensors positions from info directory and rescaling
eeg_dig_pos = []
for i in range(len(fid_points), len(epo.info['dig'])):
loc = epo.info['dig'][i].get('r')/scale_factor # rescaling
eeg_dig_pos.append(loc)
# zip the eeg sensors position (x,y,z coordinates) along with their names
eeg_chs = epo.info['ch_names'][-len(epo.info['dig']):]
eeg_ch_pos = dict(zip(eeg_chs, eeg_dig_pos))
# create the EEG montage
montage = make_dig_montage(ch_pos=eeg_ch_pos, nasion=nasion, lpa=lpa, rpa=rpa, coord_frame='head')
# set the montage
epochs = epo.set_montage(montage=montage)
print(epochs.info['dig'])
# visualize the montage
mne.viz.plot_montage(montage, sphere='auto')
# Check that the locations of EEG electrodes is correct with respect to MRI (fsaverage surface)
bem = '/media/dip_meg/sub-02/bem/sub-02-bem.fif'
src = '/media/dip_meg/sub-02/sub-02-src.fif'
trans = '/media/dip_meg/sub-02/bem/sub-02-fsaverage-trans.fif'
# I have created a trans file on fsaverge surface for testing,
# use your real subject's trans file (for accurate fitting)
# Check that the locations of EEG electrodes is correct with respect to MRI
mne.viz.plot_alignment(
epochs.info, src=src, eeg=['original', 'projected'], trans=trans,
show_axes=True, mri_fiducials=True, dig='fiducials')
Test results:
# After re-scaling.
In [93]: epochs.info['dig']
Out[93]:
[<DigPoint | LPA : (-71.6, -2.7, 1.8) mm : head frame>,
<DigPoint | Nasion : (-10.6, 93.1, 17.2) mm : head frame>,
<DigPoint | RPA : (57.8, -0.6, -4.3) mm : head frame>,
<DigPoint | EEG #1 : (60.3, 54.7, 19.9) mm : head frame>,
<DigPoint | EEG #2 : (56.8, 66.8, 44.9) mm : head frame>,
<DigPoint | EEG #3 : (52.7, 71.6, 63.2) mm : head frame>,
<DigPoint | EEG #4 : (36.2, 79.0, 82.4) mm : head frame>,
<DigPoint | EEG #5 : (24.1, 74.4, 98.9) mm : head frame>,
<DigPoint | EEG #6 : (14.1, 65.3, 111.6) mm : head frame>,
<DigPoint | EEG #7 : (2.1, 50.7, 124.2) mm : head frame>,
<DigPoint | EEG #8 : (-6.0, 36.6, 131.0) mm : head frame>,
<DigPoint | EEG #9 : (-18.9, 12.5, 137.0) mm : head frame>,
<DigPoint | EEG #10 : (51.2, 77.4, 27.2) mm : head frame>,
<DigPoint | EEG #11 : (44.5, 85.0, 45.1) mm : head frame>,
<DigPoint | EEG #12 : (31.8, 90.7, 69.9) mm : head frame>,
<DigPoint | EEG #13 : (16.2, 87.6, 86.8) mm : head frame>,
<DigPoint | EEG #14 : (2.1, 80.7, 102.1) mm : head frame>,
<DigPoint | EEG #15 : (-8.5, 67.1, 115.4) mm : head frame>,
<DigPoint | EEG #16 : (-20.9, 52.7, 122.6) mm : head frame>,
<DigPoint | EEG #17 : (-29.5, 30.6, 130.3) mm : head frame>,
<DigPoint | EEG #18 : (30.9, 94.5, 32.7) mm : head frame>,
<DigPoint | EEG #19 : (23.2, 98.9, 51.8) mm : head frame>,
<DigPoint | EEG #20 : (7.7, 97.4, 72.7) mm : head frame>,
<DigPoint | EEG #21 : (-8.6, 90.0, 90.4) mm : head frame>,
<DigPoint | EEG #22 : (-22.0, 77.9, 103.1) mm : head frame>,
<DigPoint | EEG #23 : (-32.1, 64.5, 110.9) mm : head frame>,
<DigPoint | EEG #24 : (-40.6, 42.0, 120.6) mm : head frame>,
<DigPoint | EEG #25 : (7.9, 102.8, 37.0) mm : head frame>,
<DigPoint | EEG #26 : (-4.6, 103.6, 55.6) mm : head frame>,
<DigPoint | EEG #27 : (-18.4, 97.3, 73.8) mm : head frame>,
<DigPoint | EEG #28 : (-32.4, 85.7, 87.1) mm : head frame>,
<DigPoint | EEG #29 : (-42.0, 72.5, 98.7) mm : head frame>,
<DigPoint | EEG #30 : (-50.8, 53.6, 108.2) mm : head frame>,
<DigPoint | EEG #31 : (-8.9, 100.4, 22.3) mm : head frame>,
<DigPoint | EEG #32 : (-20.2, 105.9, 40.1) mm : head frame>,
<DigPoint | EEG #33 : (-36.7, 99.0, 56.6) mm : head frame>,
<DigPoint | EEG #34 : (-45.8, 93.2, 69.2) mm : head frame>,
<DigPoint | EEG #35 : (-53.6, 77.8, 80.5) mm : head frame>,
<DigPoint | EEG #36 : (-60.2, 63.6, 89.5) mm : head frame>,
<DigPoint | EEG #37 : (-45.7, 93.5, 32.8) mm : head frame>,
<DigPoint | EEG #38 : (-58.0, 84.1, 45.7) mm : head frame>,
<DigPoint | EEG #39 : (-65.5, 73.6, 58.3) mm : head frame>,
<DigPoint | EEG #40 : (-71.2, 57.8, 73.6) mm : head frame>,
<DigPoint | EEG #41 : (-68.1, 43.3, 98.8) mm : head frame>,
<DigPoint | EEG #42 : (-63.2, 36.3, 107.4) mm : head frame>,
<DigPoint | EEG #43 : (-54.2, 21.3, 122.0) mm : head frame>,
<DigPoint | EEG #44 : (-40.0, 10.4, 133.0) mm : head frame>,
<DigPoint | EEG #45 : (-24.5, -7.9, 140.5) mm : head frame>,
<DigPoint | EEG #46 : (-65.8, 74.3, 28.2) mm : head frame>,
<DigPoint | EEG #47 : (-74.3, 60.9, 39.2) mm : head frame>,
<DigPoint | EEG #48 : (-78.4, 49.3, 57.5) mm : head frame>,
<DigPoint | EEG #49 : (-79.4, 37.2, 74.3) mm : head frame>,
<DigPoint | EEG #50 : (-81.2, 31.6, 83.3) mm : head frame>,
<DigPoint | EEG #51 : (-76.3, 23.9, 100.3) mm : head frame>,
<DigPoint | EEG #52 : (-69.9, 12.0, 111.3) mm : head frame>,
<DigPoint | EEG #53 : (-45.4, -12.2, 132.1) mm : head frame>,
<DigPoint | EEG #54 : (-75.9, 48.5, 15.1) mm : head frame>,
<DigPoint | EEG #55 : (-85.0, 45.7, 23.0) mm : head frame>,
<DigPoint | EEG #56 : (-84.7, 29.1, 54.2) mm : head frame>,
<DigPoint | EEG #57 : (-85.5, 16.4, 72.1) mm : head frame>,
<DigPoint | EEG #58 : (-83.5, 10.0, 91.6) mm : head frame>,
<DigPoint | EEG #59 : (-72.9, -5.4, 110.0) mm : head frame>,
<DigPoint | EEG #60 : (-61.3, -20.0, 122.1) mm : head frame>,
<DigPoint | EEG #61 : (-81.5, 26.1, 15.9) mm : head frame>,
<DigPoint | EEG #62 : (-85.4, 19.4, 32.0) mm : head frame>,
<DigPoint | EEG #63 : (-88.9, -0.8, 73.9) mm : head frame>,
<DigPoint | EEG #64 : (-85.5, -10.5, 87.8) mm : head frame>,
<DigPoint | EEG #65 : (-77.3, -25.6, 105.4) mm : head frame>,
<DigPoint | EEG #66 : (-80.9, 12.6, 0.3) mm : head frame>,
<DigPoint | EEG #67 : (-81.7, 12.2, 1.8) mm : head frame>,
<DigPoint | EEG #68 : (-83.6, 6.9, 15.4) mm : head frame>,
<DigPoint | EEG #69 : (-87.1, -4.6, 35.4) mm : head frame>,
<DigPoint | EEG #70 : (-89.4, -8.6, 54.1) mm : head frame>,
<DigPoint | EEG #71 : (-89.0, -22.2, 68.2) mm : head frame>,
<DigPoint | EEG #72 : (-82.2, -29.5, 83.5) mm : head frame>,
<DigPoint | EEG #73 : (-85.6, -25.8, 33.1) mm : head frame>,
<DigPoint | EEG #74 : (-84.8, -34.3, 49.6) mm : head frame>,
<DigPoint | EEG #75 : (-83.6, -40.4, 70.3) mm : head frame>,
<DigPoint | EEG #76 : (-77.5, -43.3, 92.3) mm : head frame>,
<DigPoint | EEG #77 : (-65.0, -44.3, 110.9) mm : head frame>,
<DigPoint | EEG #78 : (-48.5, -37.7, 127.2) mm : head frame>,
<DigPoint | EEG #79 : (-27.2, -29.9, 137.3) mm : head frame>,
<DigPoint | EEG #80 : (-12.5, -25.5, 146.6) mm : head frame>,
<DigPoint | EEG #81 : (-78.8, -42.7, 22.2) mm : head frame>,
<DigPoint | EEG #82 : (-81.2, -46.0, 41.9) mm : head frame>,
<DigPoint | EEG #83 : (-79.6, -55.2, 58.2) mm : head frame>,
<DigPoint | EEG #84 : (-74.8, -58.3, 80.7) mm : head frame>,
<DigPoint | EEG #85 : (-63.7, -60.3, 101.0) mm : head frame>,
<DigPoint | EEG #86 : (-45.8, -55.1, 121.9) mm : head frame>,
<DigPoint | EEG #87 : (-27.5, -50.9, 132.9) mm : head frame>,
<DigPoint | EEG #88 : (-2.2, -39.7, 138.2) mm : head frame>,
<DigPoint | EEG #89 : (-75.8, -47.8, 6.3) mm : head frame>,
<DigPoint | EEG #90 : (-77.4, -57.1, 28.9) mm : head frame>,
<DigPoint | EEG #91 : (-74.2, -66.3, 40.9) mm : head frame>,
<DigPoint | EEG #92 : (-69.3, -75.3, 65.8) mm : head frame>,
<DigPoint | EEG #93 : (-58.1, -79.5, 90.3) mm : head frame>,
<DigPoint | EEG #94 : (-43.9, -75.5, 109.9) mm : head frame>,
<DigPoint | EEG #95 : (-36.1, -77.8, 122.0) mm : head frame>,
<DigPoint | EEG #96 : (-8.0, -78.0, 117.6) mm : head frame>,
<DigPoint | EEG #97 : (-66.6, -64.5, 8.1) mm : head frame>,
<DigPoint | EEG #98 : (-66.4, -73.8, 29.7) mm : head frame>,
<DigPoint | EEG #99 : (-60.2, -84.5, 44.7) mm : head frame>,
<DigPoint | EEG #100 : (-49.4, -90.9, 68.3) mm : head frame>,
<DigPoint | EEG #101 : (-37.8, -89.7, 87.8) mm : head frame>,
<DigPoint | EEG #102 : (-24.9, -83.6, 106.4) mm : head frame>,
<DigPoint | EEG #103 : (-51.3, -86.3, 18.0) mm : head frame>,
<DigPoint | EEG #104 : (-44.7, -94.3, 35.3) mm : head frame>,
<DigPoint | EEG #105 : (-36.4, -98.6, 52.1) mm : head frame>,
<DigPoint | EEG #106 : (-24.9, -100.4, 75.5) mm : head frame>,
<DigPoint | EEG #107 : (-15.8, -96.1, 92.0) mm : head frame>,
<DigPoint | EEG #108 : (-6.0, -87.7, 106.1) mm : head frame>,
<DigPoint | EEG #109 : (-30.2, -100.4, 21.4) mm : head frame>,
<DigPoint | EEG #110 : (-19.8, -104.6, 36.3) mm : head frame>,
<DigPoint | EEG #111 : (-13.6, -102.6, 53.2) mm : head frame>,
<DigPoint | EEG #112 : (-2.9, -101.3, 79.6) mm : head frame>,
<DigPoint | EEG #113 : (9.6, -94.7, 92.4) mm : head frame>,
<DigPoint | EEG #114 : (12.6, -83.8, 107.1) mm : head frame>,
<DigPoint | EEG #115 : (15.3, -66.0, 122.1) mm : head frame>,
<DigPoint | EEG #116 : (19.2, -46.7, 133.0) mm : head frame>,
<DigPoint | EEG #117 : (17.6, -25.7, 137.9) mm : head frame>,
<DigPoint | EEG #118 : (11.3, -3.2, 141.1) mm : head frame>,
<DigPoint | EEG #119 : (-5.6, -110.6, 27.2) mm : head frame>,
<DigPoint | EEG #120 : (-2.4, -109.5, 36.7) mm : head frame>,
<DigPoint | EEG #121 : (5.5, -106.7, 57.0) mm : head frame>,
<DigPoint | EEG #122 : (16.2, -106.4, 76.7) mm : head frame>,
<DigPoint | EEG #123 : (31.7, -96.0, 102.6) mm : head frame>,
<DigPoint | EEG #124 : (33.7, -77.7, 108.1) mm : head frame>,
<DigPoint | EEG #125 : (36.0, -58.7, 119.1) mm : head frame>,
<DigPoint | EEG #126 : (34.7, -35.3, 130.8) mm : head frame>,
<DigPoint | EEG #127 : (31.7, -10.2, 134.0) mm : head frame>,
<DigPoint | EEG #128 : (11.6, -106.5, 22.7) mm : head frame>,
<DigPoint | EEG #129 : (19.7, -106.6, 35.5) mm : head frame>,
<DigPoint | EEG #130 : (29.9, -98.8, 57.2) mm : head frame>,
<DigPoint | EEG #131 : (40.3, -89.8, 72.8) mm : head frame>,
<DigPoint | EEG #132 : (47.7, -77.7, 89.4) mm : head frame>,
<DigPoint | EEG #133 : (50.9, -62.8, 103.4) mm : head frame>,
<DigPoint | EEG #134 : (51.0, -41.9, 116.2) mm : head frame>,
<DigPoint | EEG #135 : (47.7, -17.4, 124.3) mm : head frame>,
<DigPoint | EEG #136 : (43.0, -91.8, 20.4) mm : head frame>,
<DigPoint | EEG #137 : (44.3, -87.3, 37.8) mm : head frame>,
<DigPoint | EEG #138 : (52.6, -80.5, 56.2) mm : head frame>,
<DigPoint | EEG #139 : (56.2, -71.7, 66.5) mm : head frame>,
<DigPoint | EEG #140 : (63.0, -55.9, 82.8) mm : head frame>,
<DigPoint | EEG #141 : (62.3, -44.6, 98.4) mm : head frame>,
<DigPoint | EEG #142 : (63.4, -22.7, 107.1) mm : head frame>,
<DigPoint | EEG #143 : (43.7, -84.5, 12.5) mm : head frame>,
<DigPoint | EEG #144 : (59.7, -74.7, 35.7) mm : head frame>,
<DigPoint | EEG #145 : (65.7, -63.7, 45.1) mm : head frame>,
<DigPoint | EEG #146 : (67.7, -50.8, 68.1) mm : head frame>,
<DigPoint | EEG #147 : (70.7, -36.7, 78.1) mm : head frame>,
<DigPoint | EEG #148 : (70.4, -23.5, 90.7) mm : head frame>,
<DigPoint | EEG #149 : (56.9, -64.3, 14.4) mm : head frame>,
<DigPoint | EEG #150 : (64.6, -58.0, 26.7) mm : head frame>,
<DigPoint | EEG #151 : (70.0, -45.4, 42.0) mm : head frame>,
<DigPoint | EEG #152 : (72.4, -33.8, 55.8) mm : head frame>,
<DigPoint | EEG #153 : (75.0, -17.3, 74.5) mm : head frame>,
<DigPoint | EEG #154 : (71.2, -5.9, 92.6) mm : head frame>,
<DigPoint | EEG #155 : (60.8, 1.5, 109.3) mm : head frame>,
<DigPoint | EEG #156 : (46.6, 5.9, 121.4) mm : head frame>,
<DigPoint | EEG #157 : (30.2, 13.9, 131.0) mm : head frame>,
<DigPoint | EEG #158 : (4.0, 20.3, 139.0) mm : head frame>,
<DigPoint | EEG #159 : (65.0, -47.5, 12.6) mm : head frame>,
<DigPoint | EEG #160 : (70.0, -42.1, 23.1) mm : head frame>,
<DigPoint | EEG #161 : (76.1, -22.3, 39.7) mm : head frame>,
<DigPoint | EEG #162 : (77.3, -9.3, 52.1) mm : head frame>,
<DigPoint | EEG #163 : (76.0, 1.5, 69.5) mm : head frame>,
<DigPoint | EEG #164 : (69.6, 11.6, 90.7) mm : head frame>,
<DigPoint | EEG #165 : (56.2, 20.0, 109.2) mm : head frame>,
<DigPoint | EEG #166 : (36.1, 27.6, 121.3) mm : head frame>,
<DigPoint | EEG #167 : (12.9, 29.5, 132.1) mm : head frame>,
<DigPoint | EEG #168 : (75.4, -2.6, 30.3) mm : head frame>,
<DigPoint | EEG #169 : (76.5, 16.3, 53.0) mm : head frame>,
<DigPoint | EEG #170 : (73.9, 27.5, 69.7) mm : head frame>,
<DigPoint | EEG #171 : (65.4, 33.7, 87.4) mm : head frame>,
<DigPoint | EEG #172 : (50.0, 45.0, 104.3) mm : head frame>,
<DigPoint | EEG #173 : (29.4, 47.2, 121.3) mm : head frame>,
<DigPoint | EEG #174 : (72.6, 10.9, 13.2) mm : head frame>,
<DigPoint | EEG #175 : (73.4, 21.9, 31.1) mm : head frame>,
<DigPoint | EEG #176 : (71.4, 32.6, 46.5) mm : head frame>,
<DigPoint | EEG #177 : (66.7, 42.9, 73.2) mm : head frame>,
<DigPoint | EEG #178 : (57.0, 48.5, 88.9) mm : head frame>,
<DigPoint | EEG #179 : (36.1, 56.9, 108.4) mm : head frame>,
<DigPoint | EEG #180 : (71.8, 17.4, 1.2) mm : head frame>,
<DigPoint | EEG #181 : (69.4, 30.0, 14.9) mm : head frame>,
<DigPoint | EEG #182 : (66.6, 41.9, 32.9) mm : head frame>,
<DigPoint | EEG #183 : (63.8, 55.0, 53.1) mm : head frame>,
<DigPoint | EEG #184 : (56.3, 61.3, 75.0) mm : head frame>,
<DigPoint | EEG #185 : (43.1, 65.3, 92.7) mm : head frame>]
Note, you have to create a -trans file to plot head, sensor, and source space alignment in 3D. Currently, I have created one with a fsaverage
head surface for testing. It looks good to me. I would also recommend you to stick to MNE’s naming convention while saving data in different file extensions.
I hope this solves your issue. If you are happy with the solution, mark it as the solution so that others can find it easily.
best,
Dip