File size: 3,551 Bytes
63c1cc0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
from functools import lru_cache
import gradio as gr
import h5py
import numpy as np
from fsspec import url_to_fs
from matplotlib import cm
from PIL import Image
repo_id = "lhoestq/turbulent_radiative_layer_tcool_demo"
set_path = f"hf://datasets/{repo_id}/**/*.hdf5"
fs, _ = url_to_fs(set_path)
paths = fs.glob(set_path)
files = {path: h5py.File(fs.open(path, "rb", cache_type="none"), "r") for path in paths}
def get_scalar_fields(path: str) -> list[str]:
return list(files[path]["t0_fields"].keys())
def get_trajectories(path: str, field: str) -> list[int]:
return list(range(len(files[path]["t0_fields"][field])))
@lru_cache(maxsize=4)
def get_images(path: str, scalar_field: str, trajectory: int) -> list[Image.Image]:
#The data is of shape (n_trajectories, n_timesteps, x, y)
out = files[path]["t0_fields"][scalar_field][trajectory]
out = np.log(out)
out = (out - out.min()) / (out.max() - out.min())
out = np.uint8(cm.RdBu_r(out) * 255)
return [Image.fromarray(img) for img in out]
default_scalar_fields = get_scalar_fields(paths[0])
default_trajectories = get_trajectories(paths[0], default_scalar_fields[0])
default_images = get_images(paths[0], default_scalar_fields[0], default_trajectories[0])
with gr.Blocks() as demo:
gr.Markdown(f"# π HDF5 Viewer for the [{repo_id}](https://huggingface.co/{repo_id}) Dataset π")
gr.Markdown(f"Showing files at `{set_path}`")
with gr.Row():
files_dropdown = gr.Dropdown(choices=paths, value=paths[0], label="file", scale=4)
scalar_fields_dropdown = gr.Dropdown(choices=default_scalar_fields, value=default_scalar_fields[0], label="scalar field")
trajectory_dropdown = gr.Dropdown(choices=default_trajectories, value=default_trajectories[0], label="sample")
gallery = gr.Gallery(default_images, preview=True, selected_index=len(default_images) // 2)
gr.Markdown("_Tip: click on the image to go forward or backards_")
@files_dropdown.select(inputs=[files_dropdown], outputs=[scalar_fields_dropdown, trajectory_dropdown, gallery])
def _update_file(path: str):
scalar_fields = get_scalar_fields(path)
trajectories = get_trajectories(path, scalar_fields[0])
images = get_images(path, scalar_fields[0], trajectories[0])
yield {
scalar_fields_dropdown: gr.Dropdown(choices=scalar_fields, value=scalar_fields[0]),
trajectory_dropdown: gr.Dropdown(choices=trajectories, value=trajectories[0]),
gallery: gr.Gallery(images)
}
yield {gallery: gr.Gallery(selected_index=len(default_images) // 2)}
@scalar_fields_dropdown.select(inputs=[files_dropdown, scalar_fields_dropdown], outputs=[trajectory_dropdown, gallery])
def _update_scalar_field(path: str, scalar_field: str):
trajectories = get_trajectories(path, scalar_field)
images = get_images(path, scalar_field, trajectories[0])
yield {
trajectory_dropdown: gr.Dropdown(choices=trajectories, value=trajectories[0]),
gallery: gr.Gallery(images)
}
yield {gallery: gr.Gallery(selected_index=len(default_images) // 2)}
@trajectory_dropdown.select(inputs=[files_dropdown, scalar_fields_dropdown, trajectory_dropdown], outputs=[gallery])
def _update_trajectory(path: str, scalar_field: str, trajectory: int):
images = get_images(path, scalar_field, trajectory)
yield {gallery: gr.Gallery(images)}
yield {gallery: gr.Gallery(selected_index=len(default_images) // 2)}
demo.launch() |