Download code/load_t4_structural_dynamics_v1.py from HaomingLuo/AgentFEM-Structural-Dynamics-Virtual-Sensing: direct link, hf CLI and curl.
- Browser
- Download file 2.22 kB
-
https://huggingface.co/datasets/HaomingLuo/AgentFEM-Structural-Dynamics-Virtual-Sensing/resolve/main/code/load_t4_structural_dynamics_v1.py
- Command line
-
hf download hf://datasets/HaomingLuo/AgentFEM-Structural-Dynamics-Virtual-Sensing/code/load_t4_structural_dynamics_v1.py
-
curl -L -o load_t4_structural_dynamics_v1.py https://huggingface.co/datasets/HaomingLuo/AgentFEM-Structural-Dynamics-Virtual-Sensing/resolve/main/code/load_t4_structural_dynamics_v1.py
2.22 kB
| """Lightweight h5py reader for the sharded T4 v1 dataset.""" | |
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| import h5py | |
| import numpy as np | |
| DEFAULT_DATA_DIR = Path(__file__).resolve().parents[1] / "data" / "t4_structural_dynamics_v1" | |
| def load_index(data_dir: str | Path = DEFAULT_DATA_DIR) -> list[dict]: | |
| path = Path(data_dir) / "index.jsonl" | |
| return [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines() if line] | |
| def trajectory_ids(data_dir: str | Path = DEFAULT_DATA_DIR) -> tuple[str, ...]: | |
| return tuple(item["trajectory_id"] for item in load_index(data_dir)) | |
| def load_trajectory( | |
| trajectory_id: str, | |
| data_dir: str | Path = DEFAULT_DATA_DIR, | |
| *, | |
| include_fields: bool = True, | |
| ) -> dict[str, object]: | |
| root = Path(data_dir) | |
| record = next(item for item in load_index(root) if item["trajectory_id"] == trajectory_id) | |
| with h5py.File(root / record["shard"], "r") as h5: | |
| group = h5[record["group"]] | |
| case = h5[record["case_id"]] | |
| arrays = { | |
| name: np.asarray(value) | |
| for name, value in group.items() | |
| if isinstance(value, h5py.Dataset) | |
| } | |
| if include_fields: | |
| for name in ("time_s", "displacement_m", "velocity_m_per_s", "acceleration_m_per_s2"): | |
| arrays[f"field_{name}" if name == "time_s" else name] = np.asarray( | |
| group[f"fields/{name}"] | |
| ) | |
| arrays["reference_geometry_m"] = np.asarray(case["common/reference_geometry_m"]) | |
| arrays["topology"] = np.asarray(case["common/topology"]) | |
| arrays["sensor_coordinates_m"] = np.asarray(case["common/sensor_coordinates_m"]) | |
| return { | |
| "record": record, | |
| "configuration": json.loads(case.attrs["config_json"]), | |
| "quality": json.loads(case.attrs["quality_json"]), | |
| "excitation": json.loads(group.attrs["spec_json"]), | |
| "arrays": arrays, | |
| } | |
| if __name__ == "__main__": | |
| ids = trajectory_ids() | |
| sample = load_trajectory(ids[0], include_fields=False) | |
| print(len(ids), ids[0]) | |
| print({name: value.shape for name, value in sample["arrays"].items()}) | |