add comments + fix typos
Browse files
app.py
CHANGED
@@ -1,3 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from functools import lru_cache
|
2 |
|
3 |
import gradio as gr
|
@@ -14,16 +29,18 @@ paths = fs.glob(set_path)
|
|
14 |
files = {path: h5py.File(fs.open(path, "rb", cache_type="none"), "r") for path in paths}
|
15 |
|
16 |
def get_scalar_fields(path: str) -> list[str]:
|
|
|
17 |
return list(files[path]["t0_fields"].keys())
|
18 |
|
19 |
def get_trajectories(path: str, field: str) -> list[int]:
|
|
|
20 |
return list(range(len(files[path]["t0_fields"][field])))
|
21 |
|
22 |
@lru_cache(maxsize=4)
|
23 |
def get_images(path: str, scalar_field: str, trajectory: int) -> list[Image.Image]:
|
24 |
-
#The data is of shape (n_trajectories,
|
25 |
out = files[path]["t0_fields"][scalar_field][trajectory]
|
26 |
-
out = np.log(out)
|
27 |
out = (out - out.min()) / (out.max() - out.min())
|
28 |
out = np.uint8(cm.RdBu_r(out) * 255)
|
29 |
return [Image.fromarray(img) for img in out]
|
@@ -36,11 +53,11 @@ with gr.Blocks() as demo:
|
|
36 |
gr.Markdown(f"# 💠 HDF5 Viewer for the [{repo_id}](https://huggingface.co/datasets/{repo_id}) Dataset 🌊")
|
37 |
gr.Markdown(f"Showing files at `{set_path}`")
|
38 |
with gr.Row():
|
39 |
-
files_dropdown = gr.Dropdown(choices=paths, value=paths[0], label="
|
40 |
-
scalar_fields_dropdown = gr.Dropdown(choices=default_scalar_fields, value=default_scalar_fields[0], label="
|
41 |
-
trajectory_dropdown = gr.Dropdown(choices=default_trajectories, value=default_trajectories[0], label="
|
42 |
gallery = gr.Gallery(default_images, preview=True, selected_index=len(default_images) // 2)
|
43 |
-
gr.Markdown("_Tip: click on the image to go forward or
|
44 |
|
45 |
@files_dropdown.select(inputs=[files_dropdown], outputs=[scalar_fields_dropdown, trajectory_dropdown, gallery])
|
46 |
def _update_file(path: str):
|
|
|
1 |
+
# All the datasets will use the same format: a collection of HDF5 files with data cubes
|
2 |
+
# in t0_fields: scalar fields, like density, pressure, energy
|
3 |
+
# the data is of shape (n_trajectories, n_time_steps, x, y)
|
4 |
+
# in t1_fields: vector fields, like velocity (size=2 => vx, vy)
|
5 |
+
# the data is of shape (n_trajectories, n_time_steps, x, y, vx/vy)
|
6 |
+
# in t2_fields: tensor fields, like ???
|
7 |
+
# the data is of shape (n_trajectories, n_time_steps, x, y, d1, d2), with d1, d2 in [0, 1]
|
8 |
+
# ie, instead of 1 additional dimension for velocity: a (2,2) matrix where each component
|
9 |
+
# (0,0),(1,0),(0,1),(1,1) can be plotted
|
10 |
+
# Size:
|
11 |
+
# - n_trajectories: 8 to 256
|
12 |
+
# - n_time_steps: 101
|
13 |
+
# - x: 128 to 512
|
14 |
+
# - y: 128 to 512
|
15 |
+
# - physical fields: 2 to 8 (density, pressure, energy, velocity…)
|
16 |
from functools import lru_cache
|
17 |
|
18 |
import gradio as gr
|
|
|
29 |
files = {path: h5py.File(fs.open(path, "rb", cache_type="none"), "r") for path in paths}
|
30 |
|
31 |
def get_scalar_fields(path: str) -> list[str]:
|
32 |
+
# TODO: support t1_fields (vector) and t2_fields (tensor)
|
33 |
return list(files[path]["t0_fields"].keys())
|
34 |
|
35 |
def get_trajectories(path: str, field: str) -> list[int]:
|
36 |
+
# The first dimension is the trajectory (8 to 256)
|
37 |
return list(range(len(files[path]["t0_fields"][field])))
|
38 |
|
39 |
@lru_cache(maxsize=4)
|
40 |
def get_images(path: str, scalar_field: str, trajectory: int) -> list[Image.Image]:
|
41 |
+
# The data is of shape (n_trajectories, n_time_steps, x, y)
|
42 |
out = files[path]["t0_fields"][scalar_field][trajectory]
|
43 |
+
out = np.log(out) # not sure why
|
44 |
out = (out - out.min()) / (out.max() - out.min())
|
45 |
out = np.uint8(cm.RdBu_r(out) * 255)
|
46 |
return [Image.fromarray(img) for img in out]
|
|
|
53 |
gr.Markdown(f"# 💠 HDF5 Viewer for the [{repo_id}](https://huggingface.co/datasets/{repo_id}) Dataset 🌊")
|
54 |
gr.Markdown(f"Showing files at `{set_path}`")
|
55 |
with gr.Row():
|
56 |
+
files_dropdown = gr.Dropdown(choices=paths, value=paths[0], label="File", scale=4)
|
57 |
+
scalar_fields_dropdown = gr.Dropdown(choices=default_scalar_fields, value=default_scalar_fields[0], label="Physical field")
|
58 |
+
trajectory_dropdown = gr.Dropdown(choices=default_trajectories, value=default_trajectories[0], label="Trajectory")
|
59 |
gallery = gr.Gallery(default_images, preview=True, selected_index=len(default_images) // 2)
|
60 |
+
gr.Markdown("_Tip: click on the image to go forward or backwards_")
|
61 |
|
62 |
@files_dropdown.select(inputs=[files_dropdown], outputs=[scalar_fields_dropdown, trajectory_dropdown, gallery])
|
63 |
def _update_file(path: str):
|