|
import gradio as gr |
|
import pickle |
|
from datasets import load_dataset |
|
from plaid.containers.sample import Sample |
|
|
|
|
|
import numpy as np |
|
import pyrender |
|
from trimesh import Trimesh |
|
import matplotlib as mpl |
|
import matplotlib.cm as cm |
|
|
|
import os |
|
|
|
os.environ["PYOPENGL_PLATFORM"] = "egl" |
|
|
|
|
|
hf_dataset = load_dataset("PLAID-datasets/2D_profile_large", split="all_samples") |
|
|
|
|
|
ids_train = hf_dataset.description["split"]["train"] |
|
nb_samples = len(ids_train) |
|
|
|
|
|
field_names_train = ["Mach", "Pressure", "Velocity-x", "Velocity-y"] |
|
|
|
|
|
_HEADER_ = ''' |
|
<h2><b>Visualization demo of <a href='https://huggingface.co/datasets/PLAID-datasets/2D_profile_large' target='_blank'><b>2D_profile_large dataset</b></b></h2> |
|
''' |
|
|
|
|
|
def round_num(num)->str: |
|
return '%s' % float('%.3g' % num) |
|
|
|
def sample_info(sample_id_str, fieldn): |
|
|
|
sample_ = hf_dataset[ids_train[int(sample_id_str)]]["sample"] |
|
plaid_sample = Sample.model_validate(pickle.loads(sample_)) |
|
|
|
|
|
nodes = plaid_sample.get_nodes() |
|
field = plaid_sample.get_field(fieldn) |
|
|
|
|
|
|
|
|
|
|
|
norm = (field - field.min()) / (field.max() - field.min()) |
|
colormap_func = mpl.pyplot.get_cmap('viridis') |
|
rgb_colors = colormap_func(norm)[:, :3] |
|
|
|
nb_nodes = nodes.shape[0] |
|
|
|
triangles = plaid_sample.get_elements()['TRI_3'] |
|
nb_tris = triangles.shape[0] |
|
|
|
assert field.shape[0] == nb_nodes |
|
|
|
with open("visu.obj", 'w') as f: |
|
for i in range(nb_nodes): |
|
f.write(f"v {-nodes[i,0]} {nodes[i,1]} {0.} {rgb_colors[i,0]} {rgb_colors[i,1]} {rgb_colors[i,2]}\n") |
|
|
|
for i in range(nb_tris): |
|
f.write(f"f {triangles[i,0] + 1} {triangles[i,1] + 1} {triangles[i,2] + 1} \n") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
str__ = f"Training sample {sample_id_str}\n" |
|
str__ += str(plaid_sample)+"\n" |
|
|
|
if len(hf_dataset.description['in_scalars_names'])>0: |
|
str__ += "\ninput scalars:\n" |
|
for sname in hf_dataset.description['in_scalars_names']: |
|
str__ += f"- {sname}: {round_num(plaid_sample.get_scalar(sname))}\n" |
|
if len(hf_dataset.description['out_scalars_names'])>0: |
|
str__ += "\noutput scalars:\n" |
|
for sname in hf_dataset.description['out_scalars_names']: |
|
str__ += f"- {sname}: {round_num(plaid_sample.get_scalar(sname))}\n" |
|
str__ += f"\n\nMesh number of nodes: {nodes.shape[0]}\n" |
|
if len(hf_dataset.description['in_fields_names'])>0: |
|
str__ += "\ninput fields:\n" |
|
for fname in hf_dataset.description['in_fields_names']: |
|
str__ += f"- {fname}\n" |
|
if len(hf_dataset.description['out_fields_names'])>0: |
|
str__ += "\noutput fields:\n" |
|
for fname in hf_dataset.description['out_fields_names']: |
|
str__ += f"- {fname}\n" |
|
|
|
return str__, "./visu.obj" |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
with gr.Blocks(fill_width=True) as demo: |
|
gr.Markdown(_HEADER_) |
|
with gr.Row(variant="panel"): |
|
with gr.Column(scale=1): |
|
d1 = gr.Slider(0, nb_samples-1, value=0, label="Training sample id", info="Choose between 0 and "+str(nb_samples-1)) |
|
output1 = gr.Text(label="Training sample info") |
|
with gr.Column(scale=2, min_width=300): |
|
d2 = gr.Dropdown(field_names_train, value=field_names_train[0], label="Field name") |
|
|
|
output2 = gr.Model3D(label="Training sample visualization") |
|
|
|
d1.input(sample_info, [d1, d2], [output1, output2]) |
|
d2.input(sample_info, [d1, d2], [output1, output2]) |
|
|
|
demo.launch() |
|
|
|
|