Utility functions#

This module serves as a collection for methods not strictly limited to a singular application case.

src_rec.utils.apply_inv_transformation(data, trans)#

Apply the inverse of trans to data.

Parameters:
  • data (np.ndarray, shape(n, 3)) – The data to be transformed.

  • trans (np.ndarray, shape(4, 4)) – Transformation matrix.

Returns:

trans_data – Transformed data.

Return type:

np.ndarray, shape(n, 3)

src_rec.utils.calc_quat_angle(a, b)#

Calculate the angle between two quaternions.

Parameters:
  • a (numpy.ndarray, shape(4,)) – The 1st quaternion.

  • b (numpy.ndarray, shape(4,)) – The 2nd quaternion.

Returns:

angle – The angle between a and b. (scale: radians)

Return type:

float

src_rec.utils.orient_mat_to_block_format(orient_mat)#

Transform an orientation matrix (rotation matrix) into (sparse) block format.

Parameters:

orient_mat (numpy.ndarray, shape(valid_vtx_cnt, 3)) – The non-block matrix formatted orientation matrix.

Returns:

rot – The block matrix formatted orientation matrix.

Return type:

numpy.ndarray, shape(valid_vtx_cnt * 3, valid_vtx_cnt)

src_rec.utils.get_eigenbasis(vortex_normals, valid_vert, cluster_grp, cluster_indices, mri_to_meeg_trans, double_precision=40)#

Calculate an orthonormal basis of eigenvector/values for each supporting/valid point.

Parameters:
  • vortex_normals (numpy.ndarray, shape(vtx_cnt, 3)) – Normals of the supporting vertices.

  • valid_vert (numpy.ndarray, shape(vtx_cnt,)) – List of valid/supporting vertices.

  • cluster_grp (list, len(n,)) – Clusters represented by a single vortex.

  • cluster_indices (list, len(n,)) – Cluster indices.

  • mri_to_meeg_trans (numpy.ndarray, shape(4, 4)) – Transformation from MRI to head coordinates.

  • double_precision (double) – Numerical precision of the eigenvectors/values, defaults to 40 digits.

Returns:

evec – Orthonormal eigenbasis.

Return type:

numpy.ndarray, shape(valid_vtx_cnt * 3, 3)

Raises:

AssertionError – A diagonal matrix is expected for the nomal basis calculation.

src_rec.utils.find_nearest_neighbor(src_pts, tgt_pts, method='kdtree')#

Employ one of two methods to find the nearest neighbors.

In case a src_pts a list of points is provided, a model is trained, otherwise, pretrained models are used if a (KDTree or BallTree objects) are provided.

Parameters:
  • src_pts (numpy.ndarray, shape(m, 3)) – Points to build the kd-tree from.

  • tgt_pts (numpy.ndarray, shape(n, 3)) – Point to match to the kd-tree.

  • method (string) – Type of kd-tree chosen to identify neighbors, has to be eitehr ‘kdtree’ or ‘balltree’.

Returns:

result

  • neigh_indicesnp.ndarray, shape(n,)

    Indices of the nearest neighbors.

  • treesklearn.neighbors.KDTree or sklearn.neighbors.BallTree

    Tree build from the src pts. To be used in subsequently method calls to avoid rebuilding the tree.

Return type:

tuple of (np.ndarray, sklearn.neighbors.KDTree or sklearn.neighbors.BallTree)

Raises:

NotImplementedError – Type of kd-tree is invalid, has to be eitehr ‘kdtree’ or ‘balltree’.

src_rec.utils.find_valid_vertices(vertices_a, vertices_b, max_neighborhood_size=5)#

Match freesurfer reconstructed mri vertices (sphere) with model vertices (octahedron).

Parameters:
  • vertices_a (numpy.ndarray, shape(m, 3)) – Freesurfer based vertices (sphere).

  • vertices_b (numpy.ndarray, shape(n, 3)) – Octahedron based vertices.

  • max_neighborhood_size (int) – Maximum size of the neighborhood.

Returns:

vert_valid – Binary list of Freesurfer vertices that have a match in the model vertices (octahedron).

Return type:

numpy.ndarray, shape(m,)

Raises:

AssertionError – If duplicates cannot be resolved, the freesurfer reconstruction may be invalid.