Beispiel #1
0
def load_surf_data(surf_data):
    """Loading data to be represented on a surface mesh.

    Parameters
    ----------
    surf_data : str or numpy.ndarray
        Either a file containing surface data (valid format are .gii,
        .gii.gz, .mgz, .nii, .nii.gz, or Freesurfer specific files such as
        .thickness, .curv, .sulc, .annot, .label), lists of 1D data files are
        returned as 2D arrays, or
        a Numpy array containing surface data.

    Returns
    -------
    data : numpy.ndarray
        An array containing surface data
    """
    # if the input is a filename, load it
    if isinstance(surf_data, str):

        # resolve globbing
        file_list = _resolve_globbing(surf_data)
        # _resolve_globbing handles empty lists

        for f in range(len(file_list)):
            surf_data = file_list[f]
            if (surf_data.endswith('nii') or surf_data.endswith('nii.gz')
                    or surf_data.endswith('mgz')):
                data_part = np.squeeze(get_data(nibabel.load(surf_data)))
            elif (surf_data.endswith('curv') or surf_data.endswith('sulc')
                  or surf_data.endswith('thickness')):
                data_part = fs.io.read_morph_data(surf_data)
            elif surf_data.endswith('annot'):
                data_part = fs.io.read_annot(surf_data)[0]
            elif surf_data.endswith('label'):
                data_part = fs.io.read_label(surf_data)
            elif surf_data.endswith('gii'):
                if LooseVersion(nibabel.__version__) >= LooseVersion('2.1.0'):
                    gii = nibabel.load(surf_data)
                else:
                    gii = gifti.read(surf_data)
                data_part = _gifti_img_to_data(gii)
            elif surf_data.endswith('gii.gz'):
                gii = _load_surf_files_gifti_gzip(surf_data)
                data_part = _gifti_img_to_data(gii)
            else:
                raise ValueError(('The input type is not recognized. %r was '
                                  'given while valid inputs are a Numpy array '
                                  'or one of the following file formats: .gii,'
                                  ' .gii.gz, .mgz, .nii, .nii.gz, Freesurfer '
                                  'specific files such as .curv, .sulc, '
                                  '.thickness, .annot, .label') % surf_data)

            if len(data_part.shape) == 1:
                data_part = data_part[:, np.newaxis]
            if f == 0:
                data = data_part
            elif f > 0:
                try:
                    data = np.concatenate((data, data_part), axis=1)
                except ValueError:
                    raise ValueError('When more than one file is input, all '
                                     'files must contain data with the same '
                                     'shape in axis=0')

    # if the input is a numpy array
    elif isinstance(surf_data, np.ndarray):
        data = surf_data
    else:
        raise ValueError('The input type is not recognized. '
                         'Valid inputs are a Numpy array or one of the '
                         'following file formats: .gii, .gii.gz, .mgz, .nii, '
                         '.nii.gz, Freesurfer specific files such as .curv, '
                         '.sulc, .thickness, .annot, .label')
    return np.squeeze(data)
Beispiel #2
0
def load_surf_mesh(surf_mesh):
    """Loading a surface mesh geometry

    Parameters
    ----------
    surf_mesh : str or numpy.ndarray
        Either a file containing surface mesh geometry (valid formats
        are .gii .gii.gz or Freesurfer specific files such as .orig, .pial,
        .sphere, .white, .inflated) or a list or tuple of two Numpy arrays,
        the first containing the x-y-z coordinates of the mesh
        vertices, the second containing the indices (into coords)
        of the mesh faces.

    Returns
    --------
    [coords, faces] : List of two numpy.ndarray
        The first containing the x-y-z coordinates of the mesh vertices,
        the second containing the indices (into coords) of the mesh faces.
    """
    # if input is a filename, try to load it
    if isinstance(surf_mesh, str):
        # resolve globbing
        file_list = _resolve_globbing(surf_mesh)
        if len(file_list) == 1:
            surf_mesh = file_list[0]
        elif len(file_list) > 1:
            # empty list is handled inside _resolve_globbing function
            raise ValueError(
                ("More than one file matching path: %s \n"
                 "load_surf_mesh can only load one file at a time") %
                surf_mesh)

        if (surf_mesh.endswith('orig') or surf_mesh.endswith('pial')
                or surf_mesh.endswith('white') or surf_mesh.endswith('sphere')
                or surf_mesh.endswith('inflated')):
            coords, faces = fs.io.read_geometry(surf_mesh)
        elif surf_mesh.endswith('gii'):
            if LooseVersion(nibabel.__version__) >= LooseVersion('2.1.0'):
                gifti_img = nibabel.load(surf_mesh)
            else:
                gifti_img = gifti.read(surf_mesh)
            coords, faces = _gifti_img_to_mesh(gifti_img)
        elif surf_mesh.endswith('.gii.gz'):
            gifti_img = _load_surf_files_gifti_gzip(surf_mesh)
            coords, faces = _gifti_img_to_mesh(gifti_img)
        else:
            raise ValueError(
                ('The input type is not recognized. %r was given '
                 'while valid inputs are one of the following '
                 'file formats: .gii, .gii.gz, Freesurfer '
                 'specific files such as .orig, .pial, .sphere, '
                 '.white, .inflated or a list containing two '
                 'Numpy arrays [vertex coordinates, face indices]') %
                surf_mesh)
    elif isinstance(surf_mesh, (list, tuple)):
        try:
            coords, faces = surf_mesh
        except Exception:
            raise ValueError(('If a list or tuple is given as input, '
                              'it must have two elements, the first is '
                              'a Numpy array containing the x-y-z coordinates '
                              'of the mesh vertices, the second is a Numpy '
                              'array containing  the indices (into coords) of '
                              'the mesh faces. The input was a list with '
                              '%r elements.') % len(surf_mesh))
    else:
        raise ValueError('The input type is not recognized. '
                         'Valid inputs are one of the following file '
                         'formats: .gii, .gii.gz, Freesurfer specific files '
                         'such as .orig, .pial, .sphere, .white, .inflated '
                         'or a list containing two Numpy arrays '
                         '[vertex coordinates, face indices]')

    return [coords, faces]
Beispiel #3
0
def load_surf_mesh(surf_mesh):
    """Loading a surface mesh geometry

    Parameters
    ----------
    surf_mesh : str or numpy.ndarray or Mesh
        Either a file containing surface mesh geometry (valid formats
        are .gii .gii.gz or Freesurfer specific files such as .orig, .pial,
        .sphere, .white, .inflated) or two Numpy arrays organized in a list,
        tuple or a namedtuple with the fields "coordinates" and "faces", or a
        Mesh object with "coordinates" and "faces" attributes.

    Returns
    --------
    mesh : Mesh
        With the fields "coordinates" and "faces", each containing a
        numpy.ndarray

    """

    # if input is a filename, try to load it
    if isinstance(surf_mesh, str):
        # resolve globbing
        file_list = _resolve_globbing(surf_mesh)
        if len(file_list) == 1:
            surf_mesh = file_list[0]
        elif len(file_list) > 1:
            # empty list is handled inside _resolve_globbing function
            raise ValueError(("More than one file matching path: %s \n"
                             "load_surf_mesh can only load one file at a time")
                             % surf_mesh)

        if (surf_mesh.endswith('orig') or surf_mesh.endswith('pial') or
                surf_mesh.endswith('white') or surf_mesh.endswith('sphere') or
                surf_mesh.endswith('inflated')):
            coords, faces = fs.io.read_geometry(surf_mesh)
            mesh = Mesh(coordinates=coords, faces=faces)
        elif surf_mesh.endswith('gii'):
            coords, faces = _gifti_img_to_mesh(nibabel.load(surf_mesh))
            mesh = Mesh(coordinates=coords, faces=faces)
        elif surf_mesh.endswith('.gii.gz'):
            gifti_img = _load_surf_files_gifti_gzip(surf_mesh)
            coords, faces = _gifti_img_to_mesh(gifti_img)
            mesh = Mesh(coordinates=coords, faces=faces)
        else:
            raise ValueError(('The input type is not recognized. %r was given '
                              'while valid inputs are one of the following '
                              'file formats: .gii, .gii.gz, Freesurfer '
                              'specific files such as .orig, .pial, .sphere, '
                              '.white, .inflated or two Numpy arrays organized '
                              'in a list, tuple or a namedtuple with the '
                              'fields "coordinates" and "faces"'
                              ) % surf_mesh)
    elif isinstance(surf_mesh, (list, tuple)):
        try:
            coords, faces = surf_mesh
            mesh = Mesh(coordinates=coords, faces=faces)
        except Exception:
            raise ValueError(('If a list or tuple is given as input, '
                              'it must have two elements, the first is '
                              'a Numpy array containing the x-y-z coordinates '
                              'of the mesh vertices, the second is a Numpy '
                              'array containing  the indices (into coords) of '
                              'the mesh faces. The input was a list with '
                              '%r elements.') % len(surf_mesh))
    elif (hasattr(surf_mesh, "faces") and hasattr(surf_mesh, "coordinates")):
        coords, faces = surf_mesh.coordinates, surf_mesh.faces
        mesh = Mesh(coordinates=coords, faces=faces)

    else:
        raise ValueError('The input type is not recognized. '
                         'Valid inputs are one of the following file '
                         'formats: .gii, .gii.gz, Freesurfer specific files '
                         'such as .orig, .pial, .sphere, .white, .inflated '
                         'or two Numpy arrays organized in a list, tuple or '
                         'a namedtuple with the fields "coordinates" and '
                         '"faces"')

    return mesh