Coregistration, MEG to MRI#

This module may be employed to coregister MEG to MRI images.

src_rec.coreg.Coreg(rotors)#

Container class, populed with the following items:

rotors : numpy.ndarray, shape(9,) Sequence of rotors defining rotation (3), translation (3) and scaling (3).

mri_to_meg_trs : numpy.ndarray, shape(4, 4) Full affine transformation matrix (MRI -> MEG) mri_to_meg_tr : numpy.ndarray, shape(4, 4) Rigid affine transformation matrix (MRI -> MEG) mri_to_meg_rs : numpy.ndarray, shape(4, 4) Rotation & scaling only affine transformation matrix (MRI -> MEG) mri_to_meg_r : numpy.ndarray, shape(4, 4) Rotation only affine transformation matrix (MRI -> MEG)

meg_to_mri_trs : numpy.ndarray, shape(4, 4) Full affine transformation matrix (MEG -> MRI) meg_to_mri_tr : numpy.ndarray, shape(4, 4) Rigid affine transformation matrix (MEG -> MRI) meg_to_mri_rs : numpy.ndarray, shape(4, 4) Rotation & scaling only affine transformation matrix (MEG -> MRI) meg_to_mri_r : numpy.ndarray, shape(4, 4) Rotation only affine transformation matrix (MEG -> MRI)

src_rec.coreg.run(subj_name, anatomy_path, rec_meta_info=None)#

Executes the complete coregistration for a specific subject.

Parameters:
  • subj_name (string) – Name of the subject.

  • anatomy_path (string) – Path to the anatomy folder. This folder should contain a sub-folder for each subject.

  • mode (string) – Either “EEG” or “MEG”.

  • rec_meta_info (mne.io.read_info) – MEG scan meta info, obtailable via mne.io.read_info

Returns:

coreg – Container class, populed with the following items:

rotors : numpy.ndarray, shape(9,) Sequence of rotors defining rotation (3), translation (3) and scaling (3).

mri_to_meg_trs : numpy.ndarray, shape(4, 4) Full affine transformation matrix (MRI -> MEG) mri_to_meg_tr : numpy.ndarray, shape(4, 4) Rigid affine transformation matrix (MRI -> MEG) mri_to_meg_rs : numpy.ndarray, shape(4, 4) Rotation & scaling only affine transformation matrix (MRI -> MEG) mri_to_meg_r : numpy.ndarray, shape(4, 4) Rotation only affine transformation matrix (MRI -> MEG)

meg_to_mri_trs : numpy.ndarray, shape(4, 4) Full affine transformation matrix (MEG -> MRI) meg_to_mri_tr : numpy.ndarray, shape(4, 4) Rigid affine transformation matrix (MEG -> MRI) meg_to_mri_rs : numpy.ndarray, shape(4, 4) Rotation & scaling only affine transformation matrix (MEG -> MRI) meg_to_mri_r : numpy.ndarray, shape(4, 4) Rotation only affine transformation matrix (MEG -> MRI)

Return type:

finnpy.src_rec.coreg.Coreg

src_rec.coreg.plot_coregistration(coreg, meg_pts, bad_hsp_pts, anatomy_path, subj_name, mode='MEG')#

Plots the result of the coregistration from MRI to MEG using mayavi.

Parameters:
  • coreg (numpy.ndarray, shape(4, 4)) – MEG to MRI coregistration matrix.

  • meg_pts (numpy.ndarray, shape(n, 4)) – MEG pts used in the coregistration.

  • anatomy_path (string) – Path to the anatomy folder. This folder should contain a sub-folder for each subject, to be pupulated with the corresponding structural data.

  • subj_name (string) – Subject name.

  • mode (string) – “EEG” or “MEG”.

src_rec.coreg.load_meg_ref_pts(rec_meta_info)#

Load MEG reference points.

Parameters:

rec_meta_info (mne.io.read_info) – MEG scan meta info, obtailable via mne.io.read_info

Returns:

meg_pts – MEG reference points for coregistration.

Return type:

dict, (‘nasion’, ‘lpa’, ‘rpa’, ‘hsp’, ‘coord_frame’)

src_rec.coreg._calc_coreg(subj_name, anatomy_path, meg_pts, registration_scale_type='free', scale=None, use_nasion=True, max_number_of_iterations=500, mode='MEG')#

Coregisters MRI data (src) to MEG data (tgt).

Parameters:
  • subj_name (string) – Name of the subject.

  • anatomy_path (string) – Path to the anatomy folder. This folder should contain a sub-folder for each subject, to be pupulated with the corresponding structural data.

  • meg_pts (dict) – Dictionary with the following keys populated: nasion, lpa, rpa, hpi, hsp.

  • registration_scale_type (string) – Can be either “free” or “restricted”. If free, the initial registration may be scaled with a uniform factor, no scaling with restricted.

  • scale (numpy.ndarray, shape(3)) – Scale anatomy for registration.

  • use_nasion (boolean) – Flag whether to use the nasion registration point. Not recommended for defaced patients.

  • max_number_of_iterations (int) – Number of iterations per registration step (total 3), defaults to 500 per registration step.

  • mode (string) – “EEG” or “MEG”.

Returns:

coreg_rotors – Coregistration rotors

Return type:

numpy.ndarray, shape (9,)

src_rec.coreg._load_mri_ref_pts(anatomy_path, subj_name)#

Load MEG reference points.

Parameters:
  • anatomy_path (string) – Path to the anatomy folder. This folder should contain a sub-folder for each subject, to be pupulated with the corresponding structural data.

  • subj_name (string) – Subject name.

Returns:

meg_pts – MRI reference points for coregistration.

Return type:

dict, (‘nasion’, ‘lpa’, ‘rpa’, ‘hsp’, ‘coord_frame’)

src_rec.coreg._format_fiducials(pre_mri_ref_pts)#

Transforms an mne-fiducials object into an dictionary containing the fiducials.

Parameters:

pre_mri_ref_pts (list of dict(), obtained via mne.io.read_fiducials) – MNE-formatted list of MRI fiducials.

Returns:

mri_ref_pts – MRI reference points for coregistration.

Return type:

dict(), (‘LPA’, ‘NASION’, ‘RPA’)

src_rec.coreg._load_hd_surf(anatomy_path, subj_name)#

Load freesurfer extracted hd surface model vertices. If this information does not yet exists, it is created using freesurfer.

Parameters:
  • anatomy_path (string) – Path to the anatomy folder. This folder should contain a sub-folder for each subject.

  • subj_name (string) – Subject name.

Returns:

hd_surf_vert – High resulution surface model generated via freesurfer.

Return type:

numpy.ndarray, shape (n, 3)

src_rec.coreg._registrate_3d_points_restricted(src_pts, tgt_pts, weights=[1.0, 10.0, 1.0], scale=False)#

Registrates src points to tgt points via Horns method. The resulting 4x4 transformation matrix may contain translation, rotation, and scaling.

Parameters:
  • src_pts (numpy.ndarray or list, shape(n, 3)) – Source points for the coregistration.

  • tgt_pts (numpy.ndarray or list, shape(n, 3)) – Target points for the coregistration.

  • weights (numpy.ndarray or list, shape(n)) – Weights of the individual pts.

  • scale (boolean,) – Flag whether to apply uniform scaling, defaults to False

Returns:

  • est_rotors (np.ndarray, shape(9,)) – Rotors of the estimated transformation.

  • est_mat (np.ndarray, shape(4, 4)) – Rotation matrix of the estimated transformation.

src_rec.coreg._get_ref_ptn_cnt(meg_pts)#

Get the total and hsp counts from MEG pts.

Parameters:

meg_pts (dict, ('lpa', rpa', 'nasion', 'hpi', 'hsp', 'coord_frame')) – Reference MEG points for the coregistration.

Returns:

  • ptn_cnt (int) – Number of points without hsp-points.

  • hsp_cnt (int) – Number of hsp-points.

src_rec.coreg._refine_registration(src_pts, tgt_pts, last_rotors, last_mat, weights, trans_thresh=0.002, angle_thresh=0.002, scale_thresh=0.002, max_number_of_iterations=500, registration_scale_type='free', mode='MEG')#

Refines an initial registration.

Parameters:
  • src_pts (numpy.ndarray or list, shape(n1, 3)) – Source points for the coregistration.

  • tgt_pts (numpy.ndarray or list, shape(n1, 3)) – Target points for the coregistration.

  • last_rotors (np.ndarray, shape(9,)) – Original rotor estimates.

  • last_mat (np.ndarray, shape(4, 4)) – Original rotation matrix estimate.

  • weights (numpy.ndarray or list, shape(n,)) – Weights for the coregistration.

  • trans_thresh (float) – Translation error threshold.

  • angle_thresh (float) – Rotation error threshold.

  • scale_thresh (float) – Scaling error threshold.

  • max_number_of_iterations (int) – Number of iterations.

  • registration_scale_type (string) – If “free”, scaling is estimated across 3 axis, if “reduced”, scaling is uniform, defaults to “free”.

  • mode (string) – “EEG” or “MEG”.

Returns:

  • last_rotors (np.ndarray, shape(9,)) – Updated rotor estimates.

  • last_mat (np.ndarray, shape(4, 4)) – Updated rotation matrix estimate.

  • tgt_pts_full (np.ndarray, shape(m2, 3)) – Target points used for the coregistration.

  • src_pts_full (np.ndarray, shape(n2, 3)) – Source points used for the coregistration.

src_rec.coreg._rm_bad_head_shape_pts(meg_pts, mri_pts, trans_mat, distance_thresh=0.005)#

Identify MEG points whose distance is too far from MRI points and remove those.

Parameters:
  • meg_pts (numpy.ndarray or list, (m, 3)) – MEG reference points.

  • mri_pts (numpy.ndarray or list, (n, 3)) – MRI reference points.

  • trans_mat (np.ndarray, shape(4, 4)) – MEG to MRI transformation matrix.

  • distance_threshold (float) – Maximum distance. Defaults to 5 mm.

Returns:

meg_pts – Pruned list of MEG pts.

Return type:

np.ndarray, shape(m - x, 3)

src_rec.coreg._registrate_3d_points_free(src_pts, tgt_pts, weights=[1.0, 10.0, 1.0], initial_guess=None)#

Registrates src points to tgt points via least squares minimizing. The resulting 4x4 transformation matrix may contain translation, rotation, and scaling.

Parameters:
  • src_pts (numpy.ndarray, shape(m, 4)) – Source points for the registration.

  • tgt_pts (numpy.ndarray, shape(n, 4)) – Target points for the registration.

  • weights (numpy.ndarray, shape(m, 1)) – (Source) weights for the registration.

  • initial_guess (numpy.ndarray or list or tuple or None, shape(9,)) – Initial transformation guess, defaults to None for no translation/rotation/scaling.

Returns:

  • est_rotors (numpy.ndarray, shape(9,)) – Updated rotor estimate.

  • est_mat (numpy.ndarray, shape(4, 4)) – Updated transformation matrix.

src_rec.coreg._translation(x, y, z)#

Calculates a translation matrix from x, y, and z.

Parameters:
  • x (float) – shift

  • y (float) – shift

  • z (float) – shift

Returns:

trans – Transformation matrix.

Return type:

numpy.ndarray, shape(4, 4)

src_rec.coreg._rotation(x=0, y=0, z=0)#

Calculates a rotation matrix from x, y, and z.

Parameters:
  • x (float) – angle

  • y (float) – angle

  • z (float) – angle

Returns:

trans – Transformation matrix.

Return type:

numpy.ndarray, shape(4, 4)

src_rec.coreg._scaling(x=1, y=1, z=1)#

Calculates a scaling matrix from x, y, and z.

Parameters:
  • x (float) – scale

  • y (float) – scale

  • z (float) – scale

Returns:

trans – Transformation matrix.

Return type:

numpy.ndarray, shape(4, 4)

src_rec.coreg._get_transformation_matrix(rotors)#

Produces a full transformation matrix from rotors.

Parameters:

rotors (numpy.ndarray, shape(9,)) – Sequence of rotors defining rotation (3), translation (3) and scaling (3).

Returns:

trans – Transformation matrix.

Return type:

numpy.ndarray, shape(4, 4)

src_rec.coreg._get_rot_and_scale_mat(rotors)#

Produces a rotation and scaling matrix from rotors.

Parameters:

rotors (numpy.ndarray, shape(9,)) – Sequence of rotors defining rotation (3), translation (3) and scaling (3).

Returns:

trans – Transformation matrix.

Return type:

numpy.ndarray, shape(4, 4)

src_rec.coreg._get_trans_and_rot_mat(rotors)#

Produces a rigid transformation matrix from rotors.

Parameters:

rotors (numpy.ndarray, shape(9,)) – Sequence of rotors defining rotation (3), translation (3) and scaling (3).

Returns:

trans – Transformation matrix.

Return type:

numpy.ndarray, shape(4, 4)

src_rec.coreg._get_rot_mat(rotors)#

Produces a rotatio matrix from rotors.

Parameters:

rotors (numpy.ndarray, shape(9,)) – Sequence of rotors defining rotation (3), translation (3) and scaling (3).

Returns:

trans – Transformation matrix.

Return type:

numpy.ndarray, shape(4, 4)