Beispiel #1
0
    def convert_fs_subj_to_tvb_surf(self, subject: Optional[str]=None):
        """
        Merge surfaces and roi maps. Write out in TVB format.
        """

        subjects_dir = os.environ['SUBJECTS_DIR']

        if subject is None:
            subject = os.environ['SUBJECT']

        lh_surf_path = os.path.join(subjects_dir, subject, 'surf', 'lh.pial')
        rh_surf_path = os.path.join(subjects_dir, subject, 'surf', 'rh.pial')
        lh_annot_path = os.path.join(
            subjects_dir, subject, 'label', 'lh.aparc.annot')
        rh_annot_path = os.path.join(
            subjects_dir, subject, 'label', 'rh.aparc.annot')

        lh_surface = IOUtils.read_surface(lh_surf_path, False)
        rh_surface = IOUtils.read_surface(rh_surf_path, False)

        lh_annot = IOUtils.read_annotation(lh_annot_path)
        rh_annot = IOUtils.read_annotation(rh_annot_path)

        surface, region_mapping = self.surface_service.merge_lh_rh(lh_surface, rh_surface, lh_annot.region_mapping,
                                                                   rh_annot.region_mapping)

        numpy.savetxt('%s_ctx_roi_map.txt' %
                      (subject,), region_mapping.flat[:], '%i')
        TVBWriter().write_surface_zip('%s_pial_surf.zip' % (subject,), surface)
def test_write_fs_surface():
    file_path = get_data_file(TEST_FS_SUBJECT, TEST_SURFACE_FOLDER, "lh.pial")
    original_surface = IOUtils.read_surface(file_path, False)
    triangles_number = len(original_surface.triangles)

    output_file_path = get_temporary_files_path("lh-test.pial")
    IOUtils.write_surface(output_file_path, original_surface)

    new_surface = IOUtils.read_surface(output_file_path, False)
    assert triangles_number == len(new_surface.triangles) == 327680
Beispiel #3
0
def test_write_fs_surface():
    file_path = get_data_file(TEST_FS_SUBJECT, TEST_SURFACE_FOLDER, "lh.pial")
    original_surface = IOUtils.read_surface(file_path, False)
    triangles_number = len(original_surface.triangles)

    output_file_path = get_temporary_files_path("lh-test.pial")
    IOUtils.write_surface(output_file_path, original_surface)

    new_surface = IOUtils.read_surface(output_file_path, False)
    assert triangles_number == len(new_surface.triangles) == 327680
Beispiel #4
0
    def test_merge_surfaces(self,):
        h5_surface_path = get_data_file("head2", "SurfaceCortical.h5")
        h5_surface = IOUtils.read_surface(h5_surface_path, False)

        vtx = h5_surface.vertices
        tri = h5_surface.triangles
        nv = vtx.size
        nt = tri.size
        nv2 = int(nv / 2)
        nt2 = int(nt / 2)

        lh_surface = Surface(vtx[:nv2], tri[:nt2])
        rh_surface = Surface(vtx[nv2:], tri[nt2:])

        # h5_region_mapping_path = get_data_file("head2", "RegionMapping.h5")
        # annotation = IOUtils.read_annotation(h5_region_mapping_path)
        #
        # lh_region_mapping = annotation.region_mapping[:len(annotation.region_mapping) / 2]
        # rh_region_mapping = annotation.region_mapping[len(annotation.region_mapping) / 2:]

        out_surface = self.service.merge_surfaces([lh_surface, rh_surface])
        self.assertEqual(len(out_surface.vertices),
                         len(lh_surface.vertices) + len(rh_surface.vertices))
        self.assertEqual(len(out_surface.triangles),
                         len(lh_surface.triangles) + len(rh_surface.triangles))
Beispiel #5
0
    def overlap_volume_surfaces(self, volume_background: os.PathLike, surfaces_path: os.PathLike,
                                use_center_surface: bool, use_cc_point: bool, snapshot_name: str=SNAPSHOT_NAME):
        volume = IOUtils.read_volume(volume_background)

        if use_cc_point:
            ras = self.generic_io.get_ras_coordinates(
                self.read_t1_affine_matrix())
        else:
            ras = volume.get_center_point()

        surfaces = [IOUtils.read_surface(os.path.expandvars(surface), use_center_surface) for surface in
                    surfaces_path]

        for projection in PROJECTIONS:
            try:
                x, y, background_matrix = volume.slice_volume(projection, ras)
            except IndexError:
                ras = volume.get_center_point()
                x, y, background_matrix = volume.slice_volume(projection, ras)
                self.logger.info("The volume center point has been used for %s snapshot of %s and %s.", projection,
                                 volume_background, surfaces_path)

            clear_flag = True
            for surface_index, surface in enumerate(surfaces):
                surf_x_array, surf_y_array = surface.cut_by_plane(
                    projection, ras)
                self.writer.write_matrix_and_surfaces(x, y, background_matrix, surf_x_array, surf_y_array,
                                                      surface_index, clear_flag)
                clear_flag = False
            self.writer.save_figure(
                self.generate_file_name(projection, snapshot_name))
def test_write_write_brain_visa_surf():
    surface_path = get_data_file(
        TEST_FS_SUBJECT, TEST_SURFACE_FOLDER, "lh.pial")
    out_path = get_temporary_files_path("lh.pial.tri")

    surface = IOUtils.read_surface(surface_path, False)
    IOUtils.write_surface(out_path, surface)

    assert os.path.exists(out_path)
Beispiel #7
0
def test_write_write_brain_visa_surf():
    surface_path = get_data_file(TEST_FS_SUBJECT, TEST_SURFACE_FOLDER,
                                 "lh.pial")
    out_path = get_temporary_files_path("lh.pial.tri")

    surface = IOUtils.read_surface(surface_path, False)
    IOUtils.write_surface(out_path, surface)

    assert os.path.exists(out_path)
Beispiel #8
0
 def overlap_surface_annotation(self,
                                surface_path,
                                annotation,
                                snapshot_name=SNAPSHOT_NAME):
     annotation = IOUtils.read_annotation(annotation)
     surface = IOUtils.read_surface(surface_path, False)
     self.writer.write_surface_with_annotation(
         surface, annotation,
         self.generate_file_name('surface_annotation', snapshot_name))
Beispiel #9
0
 def test_overlap_surface_annotation(self):
     writer = ImageWriter(SNAPSHOTS_DIRECTORY)
     surface_path = get_data_file(self.head2, "SurfaceCortical.h5")
     surface = IOUtils.read_surface(surface_path, False)
     annot_path = get_data_file(self.head2, "RegionMapping.h5")
     annot = IOUtils.read_annotation(annot_path)
     annot.region_names = ['reg1', 'reg2']
     annot.regions_color_table = numpy.array(
         [[200, 200, 200, 255, 30567], [100, 150, 200, 255, 30568]])
     resulted_file_name = self.processor.generate_file_name(
         'surface_annotation', SNAPSHOT_NAME)
     writer.write_surface_with_annotation(surface, annot, resulted_file_name)
     fname = '%s0' % (resulted_file_name, )
     self._assert_writer_path_exists(fname)
Beispiel #10
0
 def compute_gdist_mat(self, surf_name='pial', max_distance=40.0):
     max_distance = float(max_distance)  # in case passed from sys.argv
     for h in 'rl':
         subjects_dir = os.environ['SUBJECTS_DIR']
         subject = os.environ['SUBJECT']
         surf_path = '%s/%s/surf/%sh.%s' % (subjects_dir, subject, h,
                                            surf_name)
         surface = IOUtils.read_surface(surf_path, False)
         mat_path = '%s/%s/surf/%sh.%s.gdist.mat' % (subjects_dir, subject,
                                                     h, surf_name)
         mat = gdist.local_gdist_matrix(surface.vertices,
                                        surface.triangles.astype('<i4'),
                                        max_distance=max_distance)
         scipy.io.savemat(mat_path, {'gdist': mat})
Beispiel #11
0
    def compute_gdist_mat(self, surf_name: str='pial', max_distance: float=40.0) -> numpy.ndarray:
        max_distance = float(max_distance)  # in case passed from sys.argv
        for h in 'rl':
            subjects_dir = os.environ['SUBJECTS_DIR']
            subject = os.environ['SUBJECT']
            surf_path = '%s/%s/surf/%sh.%s' % (subjects_dir,
                                               subject, h, surf_name)
            surface = IOUtils.read_surface(surf_path, False)
            mat_path = '%s/%s/surf/%sh.%s.gdist.mat' % (
                subjects_dir, subject, h, surf_name)
            mat = gdist.local_gdist_matrix(
                surface.vertices, surface.triangles.astype('<i4'), max_distance=max_distance)
            scipy.io.savemat(mat_path, {'gdist': mat})

            return mat
Beispiel #12
0
    def overlap_volume_surfaces(self,
                                volume_background,
                                surfaces_path,
                                use_center_surface,
                                use_cc_point,
                                snapshot_name=SNAPSHOT_NAME):
        volume = IOUtils.read_volume(volume_background)

        if use_cc_point:
            ras = self.generic_io.get_ras_coordinates(
                self.read_t1_affine_matrix())
        else:
            ras = volume.get_center_point()

        surfaces = [
            IOUtils.read_surface(os.path.expandvars(surface),
                                 use_center_surface)
            for surface in surfaces_path
        ]

        for projection in PROJECTIONS:
            try:
                x, y, background_matrix = volume.slice_volume(projection, ras)
            except IndexError:
                ras = volume.get_center_point()
                x, y, background_matrix = volume.slice_volume(projection, ras)
                self.logger.info(
                    "The volume center point has been used for %s snapshot of %s and %s.",
                    projection, volume_background, surfaces_path)

            clear_flag = True
            for surface_index, surface in enumerate(surfaces):
                surf_x_array, surf_y_array = surface.cut_by_plane(
                    projection, ras)
                self.writer.write_matrix_and_surfaces(x, y, background_matrix,
                                                      surf_x_array,
                                                      surf_y_array,
                                                      surface_index,
                                                      clear_flag)
                clear_flag = False
            self.writer.save_figure(
                self.generate_file_name(projection, snapshot_name))
Beispiel #13
0
    def aseg_surf_conc_annot(self,
                             surf_path,
                             out_surf_path,
                             annot_path,
                             label_indices,
                             lut_path=None):
        """
        Concatenate surfaces of one specific label of interest each, to create a single annotated surface.
        """

        lut_path = lut_path or default_lut_path()

        label_names, color_table = self.annotation_service.lut_to_annot_names_ctab(
            lut_path=lut_path, labels=label_indices)
        label_indices = numpy.array(label_indices.split()).astype('i')

        #                  verts tri area_mask cras
        surfaces = []
        out_annotation = Annotation([], [], [])
        label_number = -1

        for label_index in label_indices:
            this_surf_path = surf_path + "-%06d" % int(label_index)

            if os.path.exists(this_surf_path):
                ind_l, = numpy.where(label_indices == label_index)
                out_annotation.add_region_names_and_colors(
                    numpy.array(label_names)[ind_l], color_table[ind_l, :])
                label_number += 1
                surfaces.append(IOUtils.read_surface(this_surf_path, False))
                out_annotation.add_region_mapping(label_number * numpy.ones(
                    (surfaces[-1].n_vertices, ), dtype='int64'))
        out_surface = self.merge_surfaces(surfaces)
        #out_annotation.regions_color_table = numpy.squeeze(numpy.array(out_annotation.regions_color_table).astype('i'))

        IOUtils.write_surface(out_surf_path, out_surface)
        IOUtils.write_annotation(annot_path, out_annotation)
Beispiel #14
0
    def aseg_surf_conc_annot(self, surf_path: str, out_surf_path: str, annot_path: str,
                             label_indices: Union[numpy.ndarray, list], lut_path: Optional[str]=None) -> Surface:
        """
        Concatenate surfaces of one specific label of interest each, to create a single annotated surface.
        """

        lut_path = lut_path or default_lut_path()

        label_names, color_table = self.annotation_service.lut_to_annot_names_ctab(lut_path=lut_path,
                                                                                   labels=label_indices)
        label_indices = numpy.array(label_indices.split()).astype('i')

        #                  verts tri area_mask cras
        surfaces = []
        out_annotation = Annotation([], [], [])
        label_number = -1

        for label_index in label_indices:
            # TODO: This is hardcoded: /aseg-%06d here and also in pegasus dax generator
            this_surf_path = surf_path + "/aseg-%06d" % int(label_index)

            if os.path.exists(this_surf_path):
                ind_l, = numpy.where(label_indices == label_index)
                out_annotation.add_region_names_and_colors(
                    label_names[int(ind_l)],
                    color_table[ind_l, :])
                label_number += 1
                surfaces.append(IOUtils.read_surface(this_surf_path, False))
                out_annotation.add_region_mapping(
                    label_number * numpy.ones((surfaces[-1].n_vertices,), dtype='int64'))
        out_surface = self.merge_surfaces(surfaces)
        # out_annotation.regions_color_table = numpy.squeeze(numpy.array(out_annotation.regions_color_table).astype('i'))

        IOUtils.write_surface(out_surf_path, out_surface)
        IOUtils.write_annotation(annot_path, out_annotation)

        return out_surface
Beispiel #15
0
def test_parse_fs_surface():
    file_path = get_data_file(TEST_FS_SUBJECT, TEST_SURFACE_FOLDER, "lh.pial")
    surf = IOUtils.read_surface(file_path, False)
    assert len(surf.triangles) == 327680
Beispiel #16
0
def test_parse_not_existing_fs_surface():
    file_path = "not_existing_surface.pial"
    with pytest.raises(IOError):
        IOUtils.read_surface(file_path, False)
Beispiel #17
0
def test_parse_not_existing_fs_surface():
    file_path = "not_existing_surface.pial"
    with pytest.raises(IOError):
        IOUtils.read_surface(file_path, False)
Beispiel #18
0
def test_parse_fs_surface():
    file_path = get_data_file(TEST_FS_SUBJECT, TEST_SURFACE_FOLDER, "lh.pial")
    surf = IOUtils.read_surface(file_path, False)
    assert len(surf.triangles) == 327680
Beispiel #19
0
def merge_surfs(surf_lh, surf_rh, out_surf_path):
    s_lh = IOUtils.read_surface(surf_lh, False)
    s_rh = IOUtils.read_surface(surf_rh, False)
    surf = surfaceService.merge_surfaces([s_lh, s_rh])
    IOUtils.write_surface(out_surf_path, surf)
Beispiel #20
0
def test_parse_h5_surface():
    h5_path = get_data_file('head2', 'SurfaceCortical.h5')
    surface = IOUtils.read_surface(h5_path, False)
    assert len(surface.vertices) == 16
    assert len(surface.triangles) == 24
Beispiel #21
0
def test_parse_not_existing_gifti_surface():
    file_path = "not_existing_surface.gii"
    with pytest.raises(FileNotFoundError):
        IOUtils.read_surface(file_path, False)
Beispiel #22
0
def test_parse_gifti_centered_surface():
    file_path = get_data_file(
        TEST_MODIF_SUBJECT, TEST_SURFACE_FOLDER, "lh.pial.gii")
    surf = IOUtils.read_surface(file_path, True)
    assert len(surf.triangles) == 327680
Beispiel #23
0
def test_parse_not_surface():
    file_path = get_data_file(TEST_FS_SUBJECT, "label", "lh.aparc.annot")
    with pytest.raises(ValueError):
        IOUtils.read_surface(file_path, False)
Beispiel #24
0
    def sample_vol_on_surf(self,
                           surf_path,
                           vol_path,
                           annot_path,
                           out_surf_path,
                           cras_path,
                           add_string='',
                           vertex_neighbourhood=1,
                           add_lbl=[],
                           lut_path=None):
        """
        Sample a volume of a specific label on a surface, by keeping only those surface vertices, the nearest voxel of
        which is of the given label (+ of possibly additional target labels, such as white matter).
        Allow optionally for vertices within a given voxel distance vn from the target voxels.
        """

        lut_path = lut_path or default_lut_path()

        # Read the inputs
        surface = IOUtils.read_surface(surf_path, False)

        annotation = IOUtils.read_annotation(annot_path)
        labels = self.annotation_service.annot_names_to_labels(
            annotation.region_names, add_string=add_string, lut_path=lut_path)
        region_mapping_indexes = numpy.unique(annotation.region_mapping)

        volume_parser = VolumeIO()
        volume = volume_parser.read(vol_path)
        ras2vox_affine_matrix = numpy.linalg.inv(volume.affine_matrix)

        cras = numpy.loadtxt(cras_path)

        grid, n_grid = self.__prepare_grid(vertex_neighbourhood)

        # Initialize the output mask:
        verts_out_mask = numpy.repeat([False], surface.vertices.shape[0])
        for label_index in range(len(region_mapping_indexes)):

            self.logger.info("%s",
                             add_string + annotation.region_names[label_index])

            # Get the indexes of the vertices corresponding to this label:
            verts_indices_of_label, = numpy.where(
                annotation.region_mapping[:] ==
                region_mapping_indexes[label_index])
            verts_indices_of_label_size = verts_indices_of_label.size
            if verts_indices_of_label_size == 0:
                continue

            # Add any additional labels
            all_labels = [labels[label_index]] + add_lbl

            # get the vertices for current label and add cras to take them to
            # scanner ras
            verts_of_label = surface.vertices[verts_indices_of_label, :]
            verts_of_label += numpy.repeat(numpy.expand_dims(cras, 1).T,
                                           verts_indices_of_label_size,
                                           axis=0)

            # Compute the nearest voxel coordinates using the affine transform
            ijk = numpy.round(
                ras2vox_affine_matrix.dot(numpy.c_[verts_of_label, numpy.ones(verts_indices_of_label_size)].T)[:3].T) \
                .astype('i')

            # Get the labels of these voxels:
            surf_vxls = volume.data[ijk[:, 0], ijk[:, 1], ijk[:, 2]]

            # Vertex mask to keep: those that correspond to voxels of one of
            # the target labels
            # surf_vxls==lbl if only one target label
            verts_keep, = numpy.where(numpy.in1d(surf_vxls, all_labels))
            verts_out_mask[verts_indices_of_label[verts_keep]] = True

            if vertex_neighbourhood > 0:
                # These are now the remaining indexes to be checked for
                # neighboring voxels
                verts_indices_of_label = numpy.delete(verts_indices_of_label,
                                                      verts_keep)
                ijk = numpy.delete(ijk, verts_keep, axis=0)

                for vertex_index in range(verts_indices_of_label.size):
                    # Generate the specific grid centered at the voxel ijk
                    ijk_grid = grid + \
                        numpy.tile(ijk[vertex_index, :], (n_grid, 1))

                    # Remove voxels outside the volume
                    indexes_within_limits = numpy.all(
                        [(ijk_grid[:, 0] >= 0),
                         (ijk_grid[:, 0] < volume.dimensions[0]),
                         (ijk_grid[:, 1] >= 0),
                         (ijk_grid[:, 1] < volume.dimensions[1]),
                         (ijk_grid[:, 2] >= 0),
                         (ijk_grid[:, 2] < volume.dimensions[2])],
                        axis=0)
                    ijk_grid = ijk_grid[indexes_within_limits, :]
                    surf_vxls = volume.data[ijk_grid[:, 0], ijk_grid[:, 1],
                                            ijk_grid[:, 2]]

                    # If any of the neighbors is of the target labels include
                    # the current vertex
                    # surf_vxls==lbl if only one target label
                    if numpy.any(numpy.in1d(surf_vxls, all_labels)):
                        verts_out_mask[
                            verts_indices_of_label[vertex_index]] = True

        # Vertex indexes and vertices to keep:
        verts_out_indices, = numpy.where(verts_out_mask)
        verts_out = surface.vertices[verts_out_indices]

        # TODO maybe: make sure that all voxels of this label correspond to at least one vertex.
        # Create a similar mask for faces by picking only triangles of which
        # all 3 vertices are included
        face_out_mask = numpy.c_[verts_out_mask[surface.triangles[:, 0]],
                                 verts_out_mask[surface.triangles[:, 1]],
                                 verts_out_mask[surface.triangles[:, 2]]].all(
                                     axis=1)
        faces_out = surface.triangles[face_out_mask]

        # The old vertices' indexes of faces have to be transformed to the new
        # vrtx_out_inds:
        for iF in range(faces_out.shape[0]):
            for vertex_index in range(3):
                faces_out[iF, vertex_index], = numpy.where(
                    faces_out[iF, vertex_index] == verts_out_indices)

        surface.vertices = verts_out
        surface.triangles = faces_out

        # Write the output surfaces and annotations to files. Also write files
        # with the indexes of vertices to keep.
        IOUtils.write_surface(out_surf_path, surface)

        annotation.set_region_mapping(
            annotation.get_region_mapping_by_indices([verts_out_indices]))
        IOUtils.write_annotation(out_surf_path + ".annot", annotation)

        numpy.save(out_surf_path + "-idx.npy", verts_out_indices)
        numpy.savetxt(out_surf_path + "-idx.txt", verts_out_indices, fmt='%d')
Beispiel #25
0
 def convert_fs_to_brain_visa(self, in_surf_path):
     surface = IOUtils.read_surface(in_surf_path, False)
     IOUtils.write_surface(in_surf_path + '.tri', surface)
Beispiel #26
0
def generate_surface_zip(in_file, out_file):
    surface = IOUtils.read_surface(in_file, False)
    IOUtils.write_surface(out_file, surface)
Beispiel #27
0
    def sample_vol_on_surf(self, surf_path: str, vol_path: str, annot_path: str, out_surf_path: str,
                           cras_path: str, add_string: str='', vertex_neighbourhood: int=1,
                           add_lbl: list=[], lut_path: Optional[str]=None) -> (Surface, Annotation):
        """
        Sample a volume of a specific label on a surface, by keeping only those surface vertices, the nearest voxel of
        which is of the given label (+ of possibly additional target labels, such as white matter).
        Allow optionally for vertices within a given voxel distance vn from the target voxels.
        """

        lut_path = lut_path or default_lut_path()

        # Read the inputs
        surface = IOUtils.read_surface(surf_path, False)

        annotation = IOUtils.read_annotation(annot_path)
        labels = self.annotation_service.annot_names_to_labels(annotation.region_names,
                                                               add_string=add_string, lut_path=lut_path)
        region_mapping_indexes = numpy.unique(annotation.region_mapping)

        volume_parser = VolumeIO()
        volume = volume_parser.read(vol_path)
        ras2vox_affine_matrix = numpy.linalg.inv(volume.affine_matrix)

        cras = numpy.loadtxt(cras_path)

        grid, n_grid = self.__prepare_grid(vertex_neighbourhood)

        # Initialize the output mask:
        verts_out_mask = numpy.repeat([False], surface.vertices.shape[0])
        for label_index in range(len(region_mapping_indexes)):

            self.logger.info("%s", add_string +
                             annotation.region_names[label_index])

            # Get the indexes of the vertices corresponding to this label:
            verts_indices_of_label, = numpy.where(
                annotation.region_mapping[:] == region_mapping_indexes[label_index])
            verts_indices_of_label_size = verts_indices_of_label.size
            if verts_indices_of_label_size == 0:
                continue

            # Add any additional labels
            all_labels = [labels[label_index]] + add_lbl

            # get the vertices for current label and add cras to take them to
            # scanner ras
            verts_of_label = surface.vertices[verts_indices_of_label, :]
            verts_of_label += numpy.repeat(numpy.expand_dims(
                cras, 1).T, verts_indices_of_label_size, axis=0)

            # Compute the nearest voxel coordinates using the affine transform
            ijk = numpy.round(
                ras2vox_affine_matrix.dot(numpy.c_[verts_of_label, numpy.ones(verts_indices_of_label_size)].T)[:3].T) \
                .astype('i')

            # Get the labels of these voxels:
            surf_vxls = volume.data[ijk[:, 0], ijk[:, 1], ijk[:, 2]]

            # Vertex mask to keep: those that correspond to voxels of one of
            # the target labels
            # surf_vxls==lbl if only one target label
            verts_keep, = numpy.where(numpy.in1d(surf_vxls, all_labels))
            verts_out_mask[verts_indices_of_label[verts_keep]] = True

            if vertex_neighbourhood > 0:
                # These are now the remaining indexes to be checked for
                # neighboring voxels
                verts_indices_of_label = numpy.delete(
                    verts_indices_of_label, verts_keep)
                ijk = numpy.delete(ijk, verts_keep, axis=0)

                for vertex_index in range(verts_indices_of_label.size):
                    # Generate the specific grid centered at the voxel ijk
                    ijk_grid = grid + \
                               numpy.tile(ijk[vertex_index, :], (n_grid, 1))

                    # Remove voxels outside the volume
                    indexes_within_limits = numpy.all([(ijk_grid[:, 0] >= 0), (ijk_grid[:, 0] < volume.dimensions[0]),
                                                       (ijk_grid[:, 1] >= 0), (ijk_grid[
                                                                               :, 1] < volume.dimensions[1]),
                                                       (ijk_grid[:, 2] >= 0), (ijk_grid[:, 2] < volume.dimensions[2])],
                                                      axis=0)
                    ijk_grid = ijk_grid[indexes_within_limits, :]
                    surf_vxls = volume.data[
                        ijk_grid[:, 0], ijk_grid[:, 1], ijk_grid[:, 2]]

                    # If any of the neighbors is of the target labels include
                    # the current vertex
                    # surf_vxls==lbl if only one target label
                    if numpy.any(numpy.in1d(surf_vxls, all_labels)):
                        verts_out_mask[
                            verts_indices_of_label[vertex_index]] = True

        # Vertex indexes and vertices to keep:
        verts_out_indices, = numpy.where(verts_out_mask)
        verts_out = surface.vertices[verts_out_indices]

        # TODO maybe: make sure that all voxels of this label correspond to at least one vertex.
        # Create a similar mask for faces by picking only triangles of which
        # all 3 vertices are included
        face_out_mask = numpy.c_[
            verts_out_mask[surface.triangles[:, 0]], verts_out_mask[surface.triangles[:, 1]], verts_out_mask[
                surface.triangles[:, 2]]].all(axis=1)
        faces_out = surface.triangles[face_out_mask]

        # The old vertices' indexes of faces have to be transformed to the new
        # vrtx_out_inds:
        for iF in range(faces_out.shape[0]):
            for vertex_index in range(3):
                faces_out[iF, vertex_index], = numpy.where(
                    faces_out[iF, vertex_index] == verts_out_indices)

        surface.vertices = verts_out
        surface.triangles = faces_out

        # Write the output surfaces and annotations to files. Also write files
        # with the indexes of vertices to keep.
        IOUtils.write_surface(out_surf_path, surface)

        annotation.set_region_mapping(
            annotation.get_region_mapping_by_indices([verts_out_indices]))
        IOUtils.write_annotation(out_surf_path + ".annot", annotation)

        numpy.save(out_surf_path + "-idx.npy", verts_out_indices)
        numpy.savetxt(out_surf_path + "-idx.txt", verts_out_indices, fmt='%d')

        return surface, annotation
Beispiel #28
0
def test_parse_gifti_centered_surface():
    file_path = get_data_file(TEST_MODIF_SUBJECT, TEST_SURFACE_FOLDER,
                              "lh.pial.gii")
    surf = IOUtils.read_surface(file_path, True)
    assert len(surf.triangles) == 327680
Beispiel #29
0
def test_parse_centered_fs_surface():
    file_path = get_data_file(TEST_MODIF_SUBJECT, TEST_SURFACE_FOLDER,
                              "lh-centered.pial")
    surf = IOUtils.read_surface(file_path, False)
    assert surf.center_ras == [0, 0, 0]
Beispiel #30
0
def test_parse_h5_surface():
    h5_path = get_data_file('head2', 'SurfaceCortical.h5')
    surface = IOUtils.read_surface(h5_path, False)
    assert len(surface.vertices) == 16
    assert len(surface.triangles) == 24
Beispiel #31
0
def test_parse_not_surface():
    file_path = get_data_file(TEST_FS_SUBJECT, "label", "lh.aparc.annot")
    with pytest.raises(ValueError):
        IOUtils.read_surface(file_path, False)
Beispiel #32
0
def test_parse_centered_fs_surface():
    file_path = get_data_file(
        TEST_MODIF_SUBJECT, TEST_SURFACE_FOLDER, "lh-centered.pial")
    surf = IOUtils.read_surface(file_path, False)
    assert surf.center_ras == [0, 0, 0]
Beispiel #33
0
def test_parse_not_existing_gifti_surface():
    file_path = "not_existing_surface.gii"
    with pytest.raises(FileNotFoundError):
        IOUtils.read_surface(file_path, False)
def compute_region_details(atlas_suffix: AtlasSuffix, fs_color_lut: os.PathLike, t1: os.PathLike, lh_cort: os.PathLike,
                           rh_cort: os.PathLike, lh_cort_annot: os.PathLike, rh_cort_annot: os.PathLike,
                           lh_subcort: os.PathLike, rh_subcort: os.PathLike, lh_subcort_annot: os.PathLike,
                           rh_subcort_annot: os.PathLike):
    annot_cort_lh = IOUtils.read_annotation(lh_cort_annot)
    annot_cort_rh = IOUtils.read_annotation(rh_cort_annot)

    annot_subcort_lh = IOUtils.read_annotation(lh_subcort_annot)
    annot_subcort_rh = IOUtils.read_annotation(rh_subcort_annot)

    mapping = MappingService(atlas_suffix, annot_cort_lh, annot_cort_rh, annot_subcort_lh, annot_subcort_rh)
    mapping.generate_region_mapping_for_cort_annot(annot_cort_lh, annot_cort_rh)
    mapping.generate_region_mapping_for_subcort_annot(annot_subcort_lh, annot_subcort_rh)

    surface_service = SurfaceService()

    surf_cort_lh = IOUtils.read_surface(lh_cort, False)
    surf_cort_rh = IOUtils.read_surface(rh_cort, False)

    full_cort_surface = surface_service.merge_surfaces([surf_cort_lh, surf_cort_rh])

    surf_subcort_lh = IOUtils.read_surface(lh_subcort, False)
    surf_subcort_rh = IOUtils.read_surface(rh_subcort, False)

    full_subcort_surface = surface_service.merge_surfaces([surf_subcort_lh, surf_subcort_rh])

    genericIO.write_list_to_txt_file(mapping.cort_region_mapping, AsegFiles.RM_CORT_TXT.value.replace("%s", atlas_suffix))
    genericIO.write_list_to_txt_file(mapping.subcort_region_mapping,
                                     AsegFiles.RM_SUBCORT_TXT.value.replace("%s", atlas_suffix))

    vox2ras_file = "vox2ras.txt"
    subprocess.call(["mri_info", "--vox2ras", t1, "--o", vox2ras_file])

    surf_subcort_filename = "surface_subcort.zip"
    IOUtils.write_surface(surf_subcort_filename, full_subcort_surface)

    surf_cort_filename = "surface_cort.zip"
    IOUtils.write_surface(surf_cort_filename, full_cort_surface)

    os.remove(vox2ras_file)

    cort_subcort_full_surf = surface_service.merge_surfaces([full_cort_surface, full_subcort_surface])
    cort_subcort_full_region_mapping = mapping.cort_region_mapping + mapping.subcort_region_mapping

    dict_fs_custom = mapping.get_mapping_for_connectome_generation()
    genericIO.write_dict_to_txt_file(dict_fs_custom, AsegFiles.FS_CUSTOM_TXT.value.replace("%s", atlas_suffix))

    region_areas = surface_service.compute_areas_for_regions(mapping.get_all_regions(), cort_subcort_full_surf,
                                                             cort_subcort_full_region_mapping)
    genericIO.write_list_to_txt_file(region_areas, AsegFiles.AREAS_TXT.value.replace("%s", atlas_suffix))

    region_centers = surface_service.compute_centers_for_regions(mapping.get_all_regions(), cort_subcort_full_surf,
                                                                 cort_subcort_full_region_mapping)
    cort_subcort_lut = mapping.get_entire_lut()
    region_names = list(cort_subcort_lut.values())

    with open(AsegFiles.CENTERS_TXT.value.replace("%s", atlas_suffix), "w") as f:
        for idx, (val_x, val_y, val_z) in enumerate(region_centers):
            f.write("%s %.2f %.2f %.2f\n" % (region_names[idx], val_x, val_y, val_z))

    region_orientations = surface_service.compute_orientations_for_regions(mapping.get_all_regions(),
                                                                           cort_subcort_full_surf,
                                                                           cort_subcort_full_region_mapping)

    lh_region_centers = surface_service.compute_centers_for_regions(mapping.get_lh_regions(), surf_cort_lh,
                                                                    mapping.lh_region_mapping)
    lh_region_orientations = surface_service.compute_orientations_for_regions(mapping.get_lh_regions(), surf_cort_lh,
                                                                              mapping.lh_region_mapping)
    with open(AsegFiles.LH_DIPOLES_TXT.value.replace("%s", atlas_suffix), "w") as f:
        for idx, (val_x, val_y, val_z) in enumerate(lh_region_centers):
            f.write("%.2f %.2f %.2f %.2f %.2f %.2f\n" % (
                val_x, val_y, val_z, lh_region_orientations[idx][0], lh_region_orientations[idx][1],
                lh_region_orientations[idx][2]))

    rh_region_centers = surface_service.compute_centers_for_regions(mapping.get_rh_regions(), surf_cort_rh,
                                                                    mapping.rh_region_mapping)
    rh_region_orientations = surface_service.compute_orientations_for_regions(mapping.get_rh_regions(), surf_cort_rh,
                                                                              mapping.rh_region_mapping)
    with open(AsegFiles.RH_DIPOLES_TXT.value.replace("%s", atlas_suffix), "w") as f:
        for idx, (val_x, val_y, val_z) in enumerate(rh_region_centers):
            f.write("%.2f %.2f %.2f %.2f %.2f %.2f\n" % (
                val_x, val_y, val_z, rh_region_orientations[idx][0], rh_region_orientations[idx][1],
                rh_region_orientations[idx][2]))

    numpy.savetxt(AsegFiles.ORIENTATIONS_TXT.value.replace("%s", atlas_suffix), region_orientations, fmt='%.2f %.2f %.2f')

    annotation_service = AnnotationService()
    lut_dict, _, _ = annotation_service.read_lut(fs_color_lut, "name")
    rm_index_dict = mapping.get_mapping_for_aparc_aseg(lut_dict)
    genericIO.write_dict_to_txt_file(rm_index_dict, AsegFiles.RM_TO_APARC_ASEG_TXT.value.replace("%s", atlas_suffix))

    genericIO.write_list_to_txt_file(mapping.is_cortical_region_mapping(),
                                     AsegFiles.CORTICAL_TXT.value.replace("%s", atlas_suffix))
Beispiel #35
0
 def overlap_surface_annotation(
         self, surface_path: os.PathLike, annotation_path: os.PathLike, snapshot_name: str=SNAPSHOT_NAME):
     annotation = IOUtils.read_annotation(annotation_path)
     surface = IOUtils.read_surface(surface_path, False)
     self.writer.write_surface_with_annotation(surface, annotation,
                                               self.generate_file_name('surface_annotation', snapshot_name))
Beispiel #36
0
 def convert_fs_to_brain_visa(self, in_surf_path: str, out_surf_path: Optional[str]=None):
     surface = IOUtils.read_surface(in_surf_path, False)
     if out_surf_path is None:
         out_surf_path = in_surf_path + '.tri'
     IOUtils.write_surface(out_surf_path, surface)