def test_overlap_2_volumes(self): background_path = get_data_file( self.subject_modif, self.mri_path, self.T1) overlay_path = get_data_file( self.subject_modif, self.mri_path, self.brain) self.processor.overlap_2_volumes(background_path, overlay_path, False) resulted_file_name = self.processor.generate_file_name(AXIAL, SNAPSHOT_NAME) self._assert_writer_path_exists(resulted_file_name)
def test_overlap_volume_surface(self): volume_path = get_data_file(self.subject_modif, self.mri_path, self.T1) surface_path = get_data_file(self.subject, self.surf_path, self.surf) self.processor.overlap_volume_surfaces( volume_path, [surface_path], False, False) resulted_file_name = self.processor.generate_file_name(AXIAL, SNAPSHOT_NAME) self._assert_writer_path_exists(resulted_file_name)
def test_show_aparc_aseg_with_new_values(self): volume_path = get_data_file( self.subject_modif, self.mri_path, "aparc+aseg.nii.gz") conn_measure_path = get_data_file("connectivity_measure.txt") self.processor.show_aparc_aseg_with_new_values( volume_path, conn_measure_path, '', False) resulted_file_name = self.processor.generate_file_name( AXIAL, SNAPSHOT_NAME) self._assert_writer_path_exists(resulted_file_name)
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)
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))
def test_tri_area(self): h5_path = get_data_file('head2', 'SurfaceCortical.h5') h5_io = H5SurfaceIO() surface = h5_io.read(h5_path) rfi = numpy.array([0]) area = self.service.tri_area(surface.vertices[surface.triangles[rfi]]) self.assertEqual(5000, area[0])
def test_write_volume(): in_file_path = get_data_file(TEST_MODIF_SUBJECT, TEST_VOLUME_FOLDER, "T1.nii.gz") volume = IOUtils.read_volume(in_file_path) out_file_path = get_temporary_files_path('T1-out.nii.gz') IOUtils.write_volume(out_file_path, volume) assert os.path.exists(out_file_path)
def test_write_volume(): in_file_path = get_data_file( TEST_MODIF_SUBJECT, TEST_VOLUME_FOLDER, "T1.nii.gz") volume = IOUtils.read_volume(in_file_path) out_file_path = get_temporary_files_path('T1-out.nii.gz') IOUtils.write_volume(out_file_path, volume) assert os.path.exists(out_file_path)
def test_write_annotation(self): file_path = get_data_file( self.subject, self.annot_path, "lh.aparc.annot") annotation = IOUtils.read_annotation(file_path) out_annotation_path = self.temp_file_path("lh-test.aparc.annot") IOUtils.write_annotation(out_annotation_path, annotation) new_annotation = IOUtils.read_annotation(out_annotation_path) self.assertEqual(annotation.region_names, new_annotation.region_names)
def test_read_transformation_matrix_from_gifti_metadata(): file_path = get_data_file(TEST_MODIF_SUBJECT, TEST_SURFACE_FOLDER, "lh.pial.gii") surface_io = IOUtils.surface_io_factory(file_path) surf = surface_io.read(file_path, False) matrix = surface_io.read_transformation_matrix_from_metadata( surf.get_main_metadata()) assert matrix == [[-1, 0, 0, 0], [0, 0, 1, 0], [0, -1, 0, 0], [0, 0, 0, 1]]
def test_read_transformation_matrix_from_gifti_metadata(): file_path = get_data_file( TEST_MODIF_SUBJECT, TEST_SURFACE_FOLDER, "lh.pial.gii") surface_io = IOUtils.surface_io_factory(file_path) surf = surface_io.read(file_path, False) matrix = surface_io.read_transformation_matrix_from_metadata( surf.get_main_metadata()) assert matrix == [[-1, 0, 0, 0], [0, 0, 1, 0], [0, -1, 0, 0], [0, 0, 0, 1]]
def test_vertex_connectivity(self,): surf_path = get_data_file("head2", 'SurfaceCortical.h5') surface_parser = H5SurfaceIO() surface = surface_parser.read(surf_path) conn = self.service.vertex_connectivity(surface) self.assertEqual(conn.shape, (16, 16)) self.assertEqual(conn[0, 1], 1) self.assertEqual(conn[0, 10], 0)
def test_write_transformation_matrix_fs_metadata(): file_path = get_data_file(TEST_FS_SUBJECT, TEST_SURFACE_FOLDER, "lh.pial") surface_io = IOUtils.surface_io_factory(file_path) surf = surface_io.read(file_path, False) surface_io.write_transformation_matrix(surf.get_main_metadata()) matrix = surface_io.read_transformation_matrix_from_metadata( surf.get_main_metadata()) assert matrix.tolist() == [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [ 0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]
def test_annot_to_lut(self,): service = self.annotation.AnnotationService() lut_path = self.temp_file_path('colorLUT-temp.txt') service.annot_to_lut( get_data_file('freesurfer_fsaverage', 'label', 'lh.aparc.annot'), lut_path=lut_path, subject=self.subject ) self.assertTrue(os.path.exists(lut_path))
def test_write_transformation_matrix_fs_metadata(): file_path = get_data_file(TEST_FS_SUBJECT, TEST_SURFACE_FOLDER, "lh.pial") surface_io = IOUtils.surface_io_factory(file_path) surf = surface_io.read(file_path, False) surface_io.write_transformation_matrix(surf.get_main_metadata()) matrix = surface_io.read_transformation_matrix_from_metadata( surf.get_main_metadata()) assert matrix.tolist() == [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.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)
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)
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
def test_extract_subsurf(self,): surface_parser = FreesurferIO() annot_parser = AnnotationIO() surface_file = get_data_file("freesurfer_fsaverage", "surf", "lh.pial") annot_file = get_data_file( "freesurfer_fsaverage", "label", "lh.aparc.annot") surface = surface_parser.read(surface_file, False) verts = surface.vertices annot = annot_parser.read(annot_file) labels = annot.region_mapping verts_mask = labels == 7 subsurf_verts, subsurf_faces = self.service.extract_subsurf( surface, verts_mask, output='verts_triangls')[:2] self.assertEqual(subsurf_verts.all(), verts[numpy.where(verts_mask)].all()) subsurf = self.service.extract_subsurf( surface, verts_mask, output='surface') self.assertEqual(subsurf.vertices.all(), verts[numpy.where(verts_mask)].all())
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
def test_read_lut_by_label(self,): lut_path = get_data_file(self.color_lut_name) service = self.annotation.AnnotationService() labels, names, colors = service.read_lut( lut_path=lut_path, key_mode='label') self.assertIsInstance(names, OrderedDict) self.assertIsInstance(colors, OrderedDict) self.assertEqual(labels, _expected_color_lut_labels) self.assertEqual(list(names.keys()), _expected_color_lut_labels) self.assertEqual(list(colors.keys()), _expected_color_lut_labels)
def test_annot_names_to_labels(self,): service = self.annotation.AnnotationService() lut_path = get_data_file(self.color_lut_name) labels = service.annot_names_to_labels(['Unknown'], lut_path=lut_path) self.assertEqual(0, labels[0]) labels = service.annot_names_to_labels( ['unknown'], add_string='ctx-lh-', lut_path=lut_path ) self.assertEqual(1000, labels[0]) labels = service.annot_names_to_labels( ['bankssts'], add_string='ctx-rh-', lut_path=lut_path ) self.assertEqual(2001, labels[0])
def test_aseg_surf_conc_annot(self,): out_surf_path = get_temporary_files_path("out_aseg") out_annot_path = get_temporary_files_path("out_annot") labels = "10 11" colorLUT = get_data_file("colorLUT.txt") self.service.aseg_surf_conc_annot( data_path, out_surf_path, out_annot_path, labels, colorLUT) self.assertTrue(os.path.exists(out_surf_path)) self.assertTrue(os.path.exists(out_annot_path)) surface_parser = FreesurferIO() surface = surface_parser.read(out_surf_path, False) self.assertEqual(len(surface.vertices), 5714) self.assertEqual(len(surface.triangles), 11420) annotation = IOUtils.read_annotation(out_annot_path) assert_array_equal( annotation.regions_color_table, [[0, 118, 14, 0, 947712], [122, 186, 220, 0, 14465658]])
def test_lut_to_annot_names_ctab(self,): service = self.annotation.AnnotationService() lut_path = get_data_file(self.color_lut_name) names1, ctab1 = service.lut_to_annot_names_ctab(lut_path=lut_path) names2, ctab2 = service.lut_to_annot_names_ctab( lut_path=lut_path, labels=[0, 1, 2] ) names3, ctab3 = service.lut_to_annot_names_ctab( lut_path=lut_path, labels="0 1 2" ) expected = 'Unknown' self.assertEqual(expected, names1[0]) self.assertEqual(expected, names2[0]) self.assertEqual(expected, names3[0]) expected = all([0, 0, 0, 0, service.rgb_to_fs_magic_number([0, 0, 0])]) self.assertEqual(expected, all(ctab1[0])) self.assertEqual(expected, all(ctab2[0])) self.assertEqual(expected, all(ctab3[0]))
def test_parse_annotation(self): file_path = get_data_file( self.subject, self.annot_path, "lh.aparc.annot") annot = IOUtils.read_annotation(file_path) self.assertEqual(_expected_region_names, annot.region_names)
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]
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)
def test_parse_volume(): file_path = get_data_file(TEST_MODIF_SUBJECT, TEST_VOLUME_FOLDER, "T1.nii.gz") volume = IOUtils.read_volume(file_path) assert volume.dimensions == (256, 256, 256)
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
def test_parse_not_volume(): file_path = get_data_file(TEST_FS_SUBJECT, "surf", "lh.pial") volume_io = IOUtils.volume_io_factory(file_path) with pytest.raises(ImageFileError): volume_io.read(file_path)
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
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)
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
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
def test_parse_volume(): file_path = get_data_file( TEST_MODIF_SUBJECT, TEST_VOLUME_FOLDER, "T1.nii.gz") volume = IOUtils.read_volume(file_path) assert volume.dimensions == (256, 256, 256)
def test_parse_h5_volume(): h5_path = get_data_file('head2', 'VolumeT1Background.h5') volume = IOUtils.read_volume(h5_path) assert volume.dimensions == (6, 5, 4)
def test_parse_not_volume(): file_path = get_data_file(TEST_FS_SUBJECT, "surf", "lh.pial") volume_io = IOUtils.volume_io_factory(file_path) with pytest.raises(ImageFileError): volume_io.read(file_path)
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]
def test_parse_not_annotation(self): file_path = get_data_file(self.subject, "surf", "lh.pial") annotation_io = IOUtils.annotation_io_factory(file_path) self.assertRaises(ValueError, annotation_io.read, file_path)
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
def test_parse_h5_annotation(self): h5_path = get_data_file('head2', 'RegionMapping.h5') annotation = IOUtils.read_annotation(h5_path) self.assertEqual(annotation.region_mapping.size, 16)
def test_read_cc_point(): generic_io = GenericIO() file_path = get_data_file("fsaverage_modified", "scripts", "ponscc.cut.log") cc_point = generic_io.read_cc_point(file_path, GenericIO.point_line_flag) assert cc_point == [100.0, 100.0, 100.0, 1]
def test_parse_h5_volume(): h5_path = get_data_file('head2', 'VolumeT1Background.h5') volume = IOUtils.read_volume(h5_path) assert volume.dimensions == (6, 5, 4)