def _run_interface(self, runtime): import os.path as op import nibabel.gifti as ng import numpy as np import skimage.measure as sm import nilearn.image as nimg import slam.io as sio import slam.differential_geometry as sdg from nipype.utils.filemanip import split_filename # Generate output mesh filename from the input image name _, fname, _ = split_filename(self.inputs.image_file) gii_file = op.abspath(op.join(runtime.cwd, fname + ".gii")) # Load the largest connected component of the input image img = nimg.largest_connected_component_img(self.inputs.image_file) # TODO: check if the input image is correct (binary) # Run the marching cube algorithm verts, faces, normals, values = sm.marching_cubes_lewiner( img.get_data(), self.inputs.level) # Convert vertices coordinates to image space # TODO: check that is correct by plotting the mesh on the image x, y, z = nimg.coord_transform(verts[:, 0], verts[:, 1], verts[:, 2], img.affine) mm_verts = np.array([x, y, z]).T # Save the mesh as Gifti # FIXME: FreeView can not open the mesh (but anatomist do) gii = ng.GiftiImage(darrays=[ ng.GiftiDataArray(mm_verts, intent='NIFTI_INTENT_POINTSET'), ng.GiftiDataArray(faces, intent='NIFTI_INTENT_TRIANGLE') ]) gii.meta = ng.GiftiMetaData().from_dict({ "volume_file": self.inputs.image_file, "marching_cube_level": str(self.inputs.level), "smoothing_iterations": str(self.inputs.smoothing_iter), "smoothing_dt": str(self.inputs.smoothing_dt) }) ng.write(gii, gii_file) # Optional: Smooth the marching cube output with SLAM if self.inputs.smoothing_iter > 0: mesh = sdg.laplacian_mesh_smoothing( sio.load_mesh(gii_file), nb_iter=self.inputs.smoothing_iter, dt=self.inputs.smoothing_dt) sio.write_mesh(mesh, gii_file) return runtime
def test_basic(self): mesh_a = self.sphere_A.copy() mesh_a_save = self.sphere_A.copy() sio.write_mesh(mesh_a, "tests/data/io/temp.gii") # Non modification assert (mesh_a.vertices == mesh_a_save.vertices).all() assert (mesh_a.faces == mesh_a_save.faces).all() mesh_b = sio.load_mesh('tests/data/io/temp.gii') precision_A = .00001 # Correctness assert (np.isclose(mesh_a.vertices, mesh_b.vertices, precision_A).all()) assert (np.isclose(mesh_a.faces, mesh_b.faces, precision_A).all())
"marching_cube_level": self.input_spec.level, "smoothing_iterations": self.input_spec.smoothing_iter, "smoothing_dt": self.input_spec.smoothing_dt ======= "volume_file": self.inputs.image_file, "marching_cube_level": str(self.inputs.level), "smoothing_iterations": str(self.inputs.smoothing_iter), "smoothing_dt": str(self.inputs.smoothing_dt) >>>>>>> 02924efd7fd9ff93e10f0e0030a43b878812e288 }) ng.write(gii, gii_file) # Optional: Smooth the marching cube output with SLAM <<<<<<< HEAD if self.input_spec.smoothing_iter > 0: mesh = sdg.laplacian_mesh_smoothing( sio.load_mesh(gii_file), nb_iter=self.input_spec.smoothing_iter, dt=self.input_spec.smoothing_dt) sio.write_mesh(mesh, gii_file) ======= if self.inputs.smoothing_iter > 0: mesh = sdg.laplacian_mesh_smoothing( sio.load_mesh(gii_file), nb_iter=self.inputs.smoothing_iter, dt=self.inputs.smoothing_dt) sio.write_mesh(mesh, gii_file) return runtime >>>>>>> 02924efd7fd9ff93e10f0e0030a43b878812e288
ks = [Ks[i]] X, Y, faces, Zs = sps.generate_quadric(Ks, nstep=nstep, ratio=sigma) Z = Zs[0] coords = np.array([X, Y, Z]).transpose() mesh = trimesh.Trimesh(faces=faces, vertices=coords, process=False) # mesh.show() # Estimate curvatures : cc = 'hex_quad_k1_' + str(Ks[i][0]) + '_k2_' + str(Ks[i][1]) + '_' + str(nstep) + '_noise_' + str( int(sigma * 1000)) # os.chdir(r"/hpc/meca/users/souani.a/curvature/Compute_curvatures/Noise") # os.mkdir(cc ) os.chdir(r"/hpc/meca/users/souani.a/curvature/Compute_curvatures/Noise/"+cc) sio.write_mesh(mesh, cc+'.gii') list_exampels.append(cc) print("done for " + cc) data = list() for curv_type in curv_types: mesh_file = 'hex_quad_k1_' + str(Ks[i][0]) + '_k2_' + str(Ks[i][1]) + '_' + str(nstep) + '.gii' texture_file = cc + '_curv_mean_' + curv_type + '.gii' os.chdir("/hpc/meca/users/souani.a/curvature/Compute_curvatures/Quadrics") mesh = sio.load_mesh(mesh_file) # mesh.apply_transform(mesh.principal_inertia_transform) os.chdir( "/hpc/meca/users/souani.a/curvature/Compute_curvatures/Noise/" + cc) tex = sio.load_texture(texture_file) # splt.pyglet_plot(mesh, tex.darray, plot_colormap=True)
# Distributions plt.figure(1) plt.subplot(2,3,i+1) nbins=np.sqrt(len(sub_meshes[0].edges_unique_length)) plt.hist(sub_meshes[0].edges_unique_length,int(nbins)) plt.figure(2) plt.subplot(2,3,i+1) nbins=np.sqrt(len(sub_meshes[0].area_faces)) plt.hist(sub_meshes[0].area_faces,int(nbins)) # Patches A=stop.edges_to_adjacency_matrix(sub_meshes[0]) L=sp.diags([A.sum(axis=0)],[0],A.shape)-A L=L.toarray() curvature=np.sum(np.dot(L, sub_meshes[0].vertices)*sub_meshes[0].vertex_normals,axis=1) splt.pyglet_plot(sub_meshes[0], curvature, 'jet', True) sio.write_mesh(sub_meshes[0], "sub_mesh"+str(i)) plt.figure(1) plt.title('Distribution of edges length',pad=170) plt.figure(2) plt.title('Distribution of faces area',pad=170) # Density all_areas=[] all_pts=[] for i,val in enumerate(unique_values): inds = texture == val sub_meshes, sub_tex, sub_corresp = stop.cut_mesh(mesh, inds) sub_meshes[0].area_faces.sum()