'Compute the center of mass of activity.
This function computes the spatial center of mass on the surface
as well as the temporal center of mass as in [1]_.
.. note:: All activity must occur in a single hemisphere, otherwise
an error is raised. The "mass" of each point in space for
computing the spatial center of mass is computed by summing
across time, and vice-versa for each point in time in
computing the temporal center of mass. This is useful for
quantifying spatio-temporal cluster locations, especially
when combined with :func:`mne.vertex_to_mni`.
Parameters
subject : string | None
The subject the stc is defined for.
hemi : int, or None
Calculate the center of mass for the left (0) or right (1)
hemisphere. If None, one of the hemispheres must be all zeroes,
and the center of mass will be calculated for the other
hemisphere (useful for getting COM for clusters).
restrict_vertices : bool | array of int | instance of SourceSpaces
If True, returned vertex will be one from stc. Otherwise, it could
be any vertex from surf. If an array of int, the returned vertex
will come from that array. If instance of SourceSpaces (as of
0.13), the returned vertex will be from the given source space.
For most accuruate estimates, do not restrict vertices.
subjects_dir : str, or None
Path to the SUBJECTS_DIR. If None, the path is obtained by using
the environment variable SUBJECTS_DIR.
surf : str
The surface to use for Euclidean distance center of mass
finding. The default here is "sphere", which finds the center
of mass on the spherical surface to help avoid potential issues
with cortical folding.
See Also
mne.Label.center_of_mass
mne.vertex_to_mni
Returns
vertex : int
Vertex of the spatial center of mass for the inferred hemisphere,
with each vertex weighted by the sum of the stc across time. For a
boolean stc, then, this would be weighted purely by the duration
each vertex was active.
hemi : int
Hemisphere the vertex was taken from.
t : float
Time of the temporal center of mass (weighted by the sum across
source vertices).
References
.. [1] Larson and Lee, "The cortical dynamics underlying effective
switching of auditory spatial attention", NeuroImage 2012.'

def center_of_mass(self, subject=None, hemi=None, restrict_vertices=False, subjects_dir=None, surf='sphere'):

    if (not isinstance(surf, string_types)):
        raise TypeError(('surf    must    be    a    string,    got    %s' % (type(surf),)))
    subject = _check_subject(self.subject, subject)
    if np.any((self.data < 0)):
        raise ValueError('Cannot    compute    COM    with    negative    values')
    values = np.sum(self.data, axis=1)
    vert_inds = [np.arange(len(self.vertices[0])), (np.arange(len(self.vertices[1])) + len(self.vertices[0]))]
    if (hemi is None):
        hemi = np.where(np.array([np.sum(values[vi]) for vi in vert_inds]))[0]
        if (not (len(hemi) == 1)):
            raise ValueError('Could    not    infer    hemisphere')
        hemi = hemi[0]
    if (hemi not in [0, 1]):
        raise ValueError('hemi    must    be    0    or    1')
    vertices = self.vertices[hemi]
    values = values[vert_inds[hemi]]
    del vert_inds
    vertex = _center_of_mass(vertices, values, hemi=['lh', 'rh'][hemi], surf=surf, subject=subject, subjects_dir=subjects_dir, restrict_vertices=restrict_vertices)
    masses = np.sum(self.data, axis=0).astype(float)
    t_ind = (np.sum((masses * np.arange(self.shape[1]))) / np.sum(masses))
    t = (self.tmin + (self.tstep * t_ind))
    return (vertex, hemi, t)
