Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -40,49 +40,63 @@ def sample_info(sample_id_str, fieldn): | |
| 40 |  | 
| 41 | 
             
                nodes = plaid_sample.get_nodes()
         | 
| 42 | 
             
                field = plaid_sample.get_field(fieldn)
         | 
| 43 | 
            -
                if nodes.shape[1] == 2:
         | 
| 44 | 
            -
             | 
| 45 | 
            -
             | 
| 46 | 
            -
             | 
| 47 | 
            -
             | 
| 48 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 49 | 
             
                triangles = plaid_sample.get_elements()['TRI_3']
         | 
|  | |
| 50 |  | 
| 51 | 
            -
                 | 
| 52 | 
            -
                if np.linalg.norm(field) > 0:
         | 
| 53 | 
            -
                    norm = mpl.colors.Normalize(vmin=np.min(field), vmax=np.max(field))
         | 
| 54 | 
            -
                    cmap = cm.coolwarm
         | 
| 55 | 
            -
                    m = cm.ScalarMappable(norm=norm, cmap=cmap)
         | 
| 56 |  | 
| 57 | 
            -
             | 
| 58 | 
            -
             | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
                     | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
                # generate  | 
| 65 | 
            -
                 | 
| 66 | 
            -
                 | 
| 67 | 
            -
                 | 
| 68 | 
            -
             | 
| 69 | 
            -
                # compose scene
         | 
| 70 | 
            -
                scene = pyrender.Scene(ambient_light=[.1, .1, .3], bg_color=[0, 0, 0])
         | 
| 71 | 
            -
                camera = pyrender.PerspectiveCamera( yfov=np.pi / 3.0)
         | 
| 72 | 
            -
                light = pyrender.DirectionalLight(color=[1,1,1], intensity=1000.)
         | 
| 73 | 
            -
             | 
| 74 | 
            -
                scene.add(mesh, pose=  np.eye(4))
         | 
| 75 | 
            -
                scene.add(light, pose=  np.eye(4))
         | 
| 76 | 
            -
             | 
| 77 | 
            -
                scene.add(camera, pose=[[ 1,  0,  0,  0],
         | 
| 78 | 
            -
                                        [ 0,  1,  0,  0],
         | 
| 79 | 
            -
                                        [ 0,  0,  1,  3],
         | 
| 80 | 
            -
                                        [ 0,  0,  0,  1]])
         | 
| 81 | 
            -
             | 
| 82 | 
            -
                # render scene
         | 
| 83 | 
            -
                r = pyrender.OffscreenRenderer(1024, 1024)
         | 
| 84 | 
            -
                color, _ = r.render(scene)
         | 
| 85 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 86 |  | 
| 87 |  | 
| 88 | 
             
                str__ = f"Training sample {sample_id_str}\n"
         | 
| @@ -106,20 +120,21 @@ def sample_info(sample_id_str, fieldn): | |
| 106 | 
             
                    for fname in hf_dataset.description['out_fields_names']:
         | 
| 107 | 
             
                        str__ += f"- {fname}\n"
         | 
| 108 |  | 
| 109 | 
            -
                return str__,  | 
| 110 |  | 
| 111 |  | 
| 112 | 
             
            if __name__ == "__main__":
         | 
| 113 |  | 
| 114 | 
            -
                with gr.Blocks() as demo:
         | 
| 115 | 
             
                    gr.Markdown(_HEADER_)
         | 
| 116 | 
             
                    with gr.Row(variant="panel"):
         | 
| 117 | 
            -
                        with gr.Column():
         | 
| 118 | 
             
                            d1 = gr.Slider(0, nb_samples-1, value=0, label="Training sample id", info="Choose between 0 and "+str(nb_samples-1))
         | 
| 119 | 
             
                            output1 = gr.Text(label="Training sample info")
         | 
| 120 | 
            -
                        with gr.Column():
         | 
| 121 | 
             
                            d2 = gr.Dropdown(field_names_train, value=field_names_train[0], label="Field name")        
         | 
| 122 | 
            -
                            output2 = gr.Image(label="Training sample visualization")
         | 
|  | |
| 123 |  | 
| 124 | 
             
                    d1.input(sample_info, [d1, d2], [output1, output2])
         | 
| 125 | 
             
                    d2.input(sample_info, [d1, d2], [output1, output2]) 
         | 
|  | |
| 40 |  | 
| 41 | 
             
                nodes = plaid_sample.get_nodes()
         | 
| 42 | 
             
                field = plaid_sample.get_field(fieldn)
         | 
| 43 | 
            +
                # if nodes.shape[1] == 2:
         | 
| 44 | 
            +
                #     nodes__ = np.zeros((nodes.shape[0],nodes.shape[1]+1))
         | 
| 45 | 
            +
                #     nodes__[:,:-1] = nodes
         | 
| 46 | 
            +
                #     nodes = nodes__
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                norm = (field - field.min()) / (field.max() - field.min())
         | 
| 49 | 
            +
                colormap_func = mpl.pyplot.get_cmap('viridis')
         | 
| 50 | 
            +
                rgb_colors = colormap_func(norm)[:, :3]
         | 
| 51 | 
            +
                
         | 
| 52 | 
            +
                nb_nodes = nodes.shape[0]
         | 
| 53 | 
            +
                
         | 
| 54 | 
             
                triangles = plaid_sample.get_elements()['TRI_3']
         | 
| 55 | 
            +
                nb_tris = tris.shape[0]
         | 
| 56 |  | 
| 57 | 
            +
                assert field.shape[0] == nb_nodes
         | 
|  | |
|  | |
|  | |
|  | |
| 58 |  | 
| 59 | 
            +
                with open("visu.obj", 'w') as f:
         | 
| 60 | 
            +
                    for i in range(nb_nodes):
         | 
| 61 | 
            +
                        f.write(f"v {nodes[i,0]} {nodes[i,1]} {0.} {rgb_colors[i,0]} {rgb_colors[i,1]} {rgb_colors[i,2]}\n")
         | 
| 62 | 
            +
                   
         | 
| 63 | 
            +
                    for i in range(nb_tris):
         | 
| 64 | 
            +
                        f.write(f"f {quads[i,0] + 1} {quads[i,1] + 1} {quads[i,2] + 1} \n")
         | 
| 65 | 
            +
                        
         | 
| 66 | 
            +
                # # generate colormap
         | 
| 67 | 
            +
                # if np.linalg.norm(field) > 0:
         | 
| 68 | 
            +
                #     norm = mpl.colors.Normalize(vmin=np.min(field), vmax=np.max(field))
         | 
| 69 | 
            +
                #     cmap = cm.coolwarm
         | 
| 70 | 
            +
                #     m = cm.ScalarMappable(norm=norm, cmap=cmap)
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 71 |  | 
| 72 | 
            +
                #     vertex_colors = m.to_rgba(field)[:,:3]
         | 
| 73 | 
            +
                # else:
         | 
| 74 | 
            +
                #     vertex_colors = 1+np.zeros((field.shape[0], 3))
         | 
| 75 | 
            +
                #     vertex_colors[:,0] = 0.2298057
         | 
| 76 | 
            +
                #     vertex_colors[:,1] = 0.01555616
         | 
| 77 | 
            +
                #     vertex_colors[:,2] = 0.15023281
         | 
| 78 | 
            +
             | 
| 79 | 
            +
                # # generate mesh
         | 
| 80 | 
            +
                # trimesh = Trimesh(vertices = nodes, faces = triangles)
         | 
| 81 | 
            +
                # trimesh.visual.vertex_colors = vertex_colors
         | 
| 82 | 
            +
                # mesh = pyrender.Mesh.from_trimesh(trimesh, smooth=False)
         | 
| 83 | 
            +
             | 
| 84 | 
            +
                # # compose scene
         | 
| 85 | 
            +
                # scene = pyrender.Scene(ambient_light=[.1, .1, .3], bg_color=[0, 0, 0])
         | 
| 86 | 
            +
                # camera = pyrender.PerspectiveCamera( yfov=np.pi / 3.0)
         | 
| 87 | 
            +
                # light = pyrender.DirectionalLight(color=[1,1,1], intensity=1000.)
         | 
| 88 | 
            +
             | 
| 89 | 
            +
                # scene.add(mesh, pose=  np.eye(4))
         | 
| 90 | 
            +
                # scene.add(light, pose=  np.eye(4))
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                # scene.add(camera, pose=[[ 1,  0,  0,  0],
         | 
| 93 | 
            +
                #                         [ 0,  1,  0,  0],
         | 
| 94 | 
            +
                #                         [ 0,  0,  1,  3],
         | 
| 95 | 
            +
                #                         [ 0,  0,  0,  1]])
         | 
| 96 | 
            +
             | 
| 97 | 
            +
                # # render scene
         | 
| 98 | 
            +
                # r = pyrender.OffscreenRenderer(1024, 1024)
         | 
| 99 | 
            +
                # color, _ = r.render(scene)
         | 
| 100 |  | 
| 101 |  | 
| 102 | 
             
                str__ = f"Training sample {sample_id_str}\n"
         | 
|  | |
| 120 | 
             
                    for fname in hf_dataset.description['out_fields_names']:
         | 
| 121 | 
             
                        str__ += f"- {fname}\n"
         | 
| 122 |  | 
| 123 | 
            +
                return str__, "./visu.obj"
         | 
| 124 |  | 
| 125 |  | 
| 126 | 
             
            if __name__ == "__main__":
         | 
| 127 |  | 
| 128 | 
            +
                with gr.Blocks(fill_width=True) as demo:
         | 
| 129 | 
             
                    gr.Markdown(_HEADER_)
         | 
| 130 | 
             
                    with gr.Row(variant="panel"):
         | 
| 131 | 
            +
                        with gr.Column(scale=1):
         | 
| 132 | 
             
                            d1 = gr.Slider(0, nb_samples-1, value=0, label="Training sample id", info="Choose between 0 and "+str(nb_samples-1))
         | 
| 133 | 
             
                            output1 = gr.Text(label="Training sample info")
         | 
| 134 | 
            +
                        with gr.Column(scale=2, min_width=300):
         | 
| 135 | 
             
                            d2 = gr.Dropdown(field_names_train, value=field_names_train[0], label="Field name")        
         | 
| 136 | 
            +
                            # output2 = gr.Image(label="Training sample visualization")    
         | 
| 137 | 
            +
                            output2 = gr.Model3D(label="Training sample visualization")
         | 
| 138 |  | 
| 139 | 
             
                    d1.input(sample_info, [d1, d2], [output1, output2])
         | 
| 140 | 
             
                    d2.input(sample_info, [d1, d2], [output1, output2]) 
         | 

