Utility functions#

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

src_rec.utils.run_subprocess_in_custom_working_directory(subject_id, cmd)#

Creates a custom work directory to run a freesurfer command within.

Parameters:
  • subject_id (string) – Name of the subject whose data is worked on.

  • cmd (string) – The freesurfer command to be executed in the custom environment.

src_rec.utils.fast_cross_product(a, b)#

Calculates the cross product between two vectors. Seemingly numpy has too much overhead here to be fast.

Parameters:
  • a (numpy.ndarray, shape(n,)) – The 1st vector in the cross product.

  • b (numpy.ndarray(n,)) – The 2nd vector in the cross product.

Returns:

res – Crossproduct of vectors a x b.

Return type:

numpy.ndarray, shape(n,)

src_rec.utils.norm_vert(vert)#

Normalizes a vector. Seemingly numpy has too much overhead here to be fast.

Parameters:

vert (numpy.ndarray, shape(n,)) – The to be normalized vector.

Returns:

vert – The normalized vector.

Return type:

numpy.ndarray, shape(n,)

src_rec.utils.magn_of_vec(vec)#

Calculates the magnitude of a vector

Parameters:

vec (numpy.ndarray, shape(n,)) – The vector in question.

Returns:

magn – The magnitude of vec.

Return type:

float

src_rec.utils.apply_inv_transformation(data, trans)#

Applies the inverse of trans to data.

Parameters:#

datanp.ndarray, shape(n, 3)

The data to be transformed.

transnp.ndarray, shape(4, 4)

Transformation matrix.

Results#

trans_datanp.ndarray, shape(n, 3)

Transformed data.

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)#

Transforms 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_meg_trans, double_precision=40)#

Calculates 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_meg_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)

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

Employs 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_ptsnumpy.ndarray, shape(m, 3)

Points to build the kd-tree from.

tgt_ptsnumpy.ndarray, shape(n, 3)

Point to match to the kd-tree.

Results#

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.

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

Matches 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.

Returns:

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

Return type:

numpy.ndarray, shape(m,)