示例#1
0
    def to_original_src(self,
                        src_orig,
                        subject_orig=None,
                        subjects_dir=None,
                        verbose=None):
        """Get the connectivity from a morphed source to the original subject.

        Parameters
        ----------
        src_orig : instance of SourceSpaces
            The original source spaces that were morphed to the current
            subject.
        subject_orig : str | None
            The original subject. For most source spaces this shouldn't need
            to be provided, since it is stored in the source space itself.
        subjects_dir : string, or None
            Path to SUBJECTS_DIR if it is not set in the environment.
        verbose : bool | str | int | None
            If not None, override default verbose level (see
            :func:`mne.verbose` and :ref:`Logging documentation <tut_logging>`
            for more).

        Returns
        -------
        con : instance of VertexConnectivity
            The transformed connectivity.

        See Also
        --------
        mne.morph_source_spaces
        """
        if self.subject is None:
            raise ValueError('con.subject must be set')

        src_orig = _ensure_src(src_orig, kind='surf')
        subject_orig = _ensure_src_subject(src_orig, subject_orig)

        data_idx, vertices = _get_morph_src_reordering(
            vertices=self.vertices,
            src_from=src_orig,
            subject_from=subject_orig,
            subject_to=self.subject,
            subjects_dir=subjects_dir,
            verbose=verbose,
        )

        # Map the pairs to new vertices
        mapping = np.argsort(data_idx)
        pairs = [[mapping[p_] for p_ in p] for p in self.pairs]
        vertex_degree = self.source_degree[:, data_idx]
        return VertexConnectivity(
            data=self.data,
            pairs=pairs,
            vertices=vertices,
            vertex_degree=vertex_degree,
            subject=subject_orig,
        )
示例#2
0
def get_morph_src_mapping(src_from,
                          src_to,
                          subject_from=None,
                          subject_to=None,
                          subjects_dir=None):
    """Get a mapping between an original source space and its morphed version.
    It is assumed that the number of vertices and their positions match between
    the source spaces, only the ordering is different. This is commonly the
    case when using :func:`morph_source_spaces`.
    Parameters
    ----------
    src_from : instance of SourceSpaces
        The original source space that was morphed to the target subject.
    src_to : instance of SourceSpaces | list of two arrays
        Either the source space to which ``src_from`` was morphed, or the
        vertex numbers of this source space.
    subject_from : str | None
        The name of the Freesurfer subject to which ``src_from`` belongs. By
        default, the value stored in the SourceSpaces object is used.
    subject_to : str | None
        The name of the Freesurfer subject to which ``src_to`` belongs. By
        default, the value stored in the SourceSpaces object is used.
    subjects_dir : str | None
        Path to SUBJECTS_DIR if it is not set in the environment.
    Returns
    -------
    from_to : dict | pair of dicts
        For each hemisphere, a
        dictionary mapping vertex numbers from src_from -> src_to.
    to_from : dict | pair of dicts
        For each hemisphere, a
        dictionary mapping vertex numbers from src_to -> src_from.
    See also
    --------
    _get_morph_src_reordering
    """
    if subject_from is None:
        subject_from = src_from[0]['subject_his_id']
    if subject_to is None:
        subject_to = src_to[0]['subject_his_id']
    subjects_dir = mne.utils.get_subjects_dir(subjects_dir, raise_error=True)

    src_from = _ensure_src(src_from, kind='surf')
    subject_from = _ensure_src_subject(src_from, subject_from)

    if isinstance(src_to, SourceSpaces):
        to_vert_lh = src_to[0]['vertno']
        to_vert_rh = src_to[1]['vertno']
    else:
        if subject_to is None:
            ValueError('When supplying vertex numbers as `src_to`, the '
                       '`subject_to` parameter must be set.')
        to_vert_lh, to_vert_rh = src_to

    order, from_vert = _get_morph_src_reordering([to_vert_lh, to_vert_rh],
                                                 src_from,
                                                 subject_from,
                                                 subject_to,
                                                 subjects_dir=subjects_dir)
    from_vert_lh, from_vert_rh = from_vert

    # Re-order the vertices of src_to to match the ordering of src_from
    to_n_lh = len(to_vert_lh)
    to_vert_lh = to_vert_lh[order[:to_n_lh]]
    to_vert_rh = to_vert_rh[order[to_n_lh:] - to_n_lh]

    # Create the mappings
    from_to = [
        dict(zip(from_vert_lh, to_vert_lh)),
        dict(zip(from_vert_rh, to_vert_rh))
    ]
    to_from = [
        dict(zip(to_vert_lh, from_vert_lh)),
        dict(zip(to_vert_rh, from_vert_rh))
    ]

    return from_to, to_from