lhoestq HF staff commited on
Commit
63c1cc0
β€’
1 Parent(s): 065b3e6

add hdf5 viewer

Browse files
Files changed (4) hide show
  1. .gitignore +1 -0
  2. README.md +2 -2
  3. app.py +73 -0
  4. requirements.txt +4 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __*
README.md CHANGED
@@ -1,7 +1,7 @@
1
  ---
2
  title: Visualization Turbulent Radiative Layer 2D
3
- emoji: πŸ”₯
4
- colorFrom: yellow
5
  colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 4.44.0
 
1
  ---
2
  title: Visualization Turbulent Radiative Layer 2D
3
+ emoji: πŸ’ πŸŒŠ
4
+ colorFrom: blue
5
  colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 4.44.0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from functools import lru_cache
2
+
3
+ import gradio as gr
4
+ import h5py
5
+ import numpy as np
6
+ from fsspec import url_to_fs
7
+ from matplotlib import cm
8
+ from PIL import Image
9
+
10
+ repo_id = "lhoestq/turbulent_radiative_layer_tcool_demo"
11
+ set_path = f"hf://datasets/{repo_id}/**/*.hdf5"
12
+ fs, _ = url_to_fs(set_path)
13
+ 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, n_timesteps, x, y)
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]
30
+
31
+ default_scalar_fields = get_scalar_fields(paths[0])
32
+ default_trajectories = get_trajectories(paths[0], default_scalar_fields[0])
33
+ default_images = get_images(paths[0], default_scalar_fields[0], default_trajectories[0])
34
+
35
+ with gr.Blocks() as demo:
36
+ gr.Markdown(f"# πŸ’  HDF5 Viewer for the [{repo_id}](https://huggingface.co/{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="file", scale=4)
40
+ scalar_fields_dropdown = gr.Dropdown(choices=default_scalar_fields, value=default_scalar_fields[0], label="scalar field")
41
+ trajectory_dropdown = gr.Dropdown(choices=default_trajectories, value=default_trajectories[0], label="sample")
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 backards_")
44
+
45
+ @files_dropdown.select(inputs=[files_dropdown], outputs=[scalar_fields_dropdown, trajectory_dropdown, gallery])
46
+ def _update_file(path: str):
47
+ scalar_fields = get_scalar_fields(path)
48
+ trajectories = get_trajectories(path, scalar_fields[0])
49
+ images = get_images(path, scalar_fields[0], trajectories[0])
50
+ yield {
51
+ scalar_fields_dropdown: gr.Dropdown(choices=scalar_fields, value=scalar_fields[0]),
52
+ trajectory_dropdown: gr.Dropdown(choices=trajectories, value=trajectories[0]),
53
+ gallery: gr.Gallery(images)
54
+ }
55
+ yield {gallery: gr.Gallery(selected_index=len(default_images) // 2)}
56
+
57
+ @scalar_fields_dropdown.select(inputs=[files_dropdown, scalar_fields_dropdown], outputs=[trajectory_dropdown, gallery])
58
+ def _update_scalar_field(path: str, scalar_field: str):
59
+ trajectories = get_trajectories(path, scalar_field)
60
+ images = get_images(path, scalar_field, trajectories[0])
61
+ yield {
62
+ trajectory_dropdown: gr.Dropdown(choices=trajectories, value=trajectories[0]),
63
+ gallery: gr.Gallery(images)
64
+ }
65
+ yield {gallery: gr.Gallery(selected_index=len(default_images) // 2)}
66
+
67
+ @trajectory_dropdown.select(inputs=[files_dropdown, scalar_fields_dropdown, trajectory_dropdown], outputs=[gallery])
68
+ def _update_trajectory(path: str, scalar_field: str, trajectory: int):
69
+ images = get_images(path, scalar_field, trajectory)
70
+ yield {gallery: gr.Gallery(images)}
71
+ yield {gallery: gr.Gallery(selected_index=len(default_images) // 2)}
72
+
73
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ h5py
2
+ huggingface_hub
3
+ Pillow
4
+ numpy