Download process/map_iquique.py from OpenTSLab/MachineHearing: direct link, hf CLI and curl.
- Browser
- Download file 1.94 kB
-
https://huggingface.co/datasets/OpenTSLab/MachineHearing/resolve/main/process/map_iquique.py
- Command line
-
hf download hf://datasets/OpenTSLab/MachineHearing/process/map_iquique.py
-
curl -L -o map_iquique.py https://huggingface.co/datasets/OpenTSLab/MachineHearing/resolve/main/process/map_iquique.py
1.94 kB
| import argparse | |
| import csv | |
| import json | |
| from pathlib import Path | |
| import h5py | |
| import numpy as np | |
| MHB_ROOT = Path(__file__).resolve().parent.parent | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("data_dir", type=Path) | |
| args = parser.parse_args() | |
| data_dir = args.data_dir.expanduser().resolve() | |
| assert (data_dir / "waveforms.hdf5").is_file() | |
| assert (data_dir / "metadata.csv").is_file() | |
| with (data_dir / "metadata.csv").open("r", encoding="utf-8") as f: | |
| trace_names = {row["trace_name"] for row in csv.DictReader(f)} | |
| count = 0 | |
| with h5py.File(data_dir / "waveforms.hdf5", "r") as h5, \ | |
| (MHB_ROOT / "meta_data.jsonl").open("r", encoding="utf-8") as f: | |
| for line in f: | |
| if not line.strip(): | |
| continue | |
| record = json.loads(line) | |
| if record["task_name"] != "Iquique": | |
| continue | |
| trace_name = record["meta_data"]["trace_name"] | |
| assert trace_name in trace_names, f"Missing trace: {trace_name}" | |
| bucket, position = trace_name.split("$") | |
| index, channels, length = position.split(",") | |
| index, channels, length = int(index), int(channels[1:]), int(length[1:]) | |
| assert channels == 3 and length == record["input_ts"]["length"] | |
| assert record["id"] == f"{bucket.removeprefix('bucket')}_{index}_{channels}_{length}" | |
| signal = h5["data"][bucket][index, :channels, :length].T.astype(np.float32) | |
| assert signal.shape == (length, channels) | |
| dst = MHB_ROOT / record["input_ts"]["path"] | |
| assert dst.name == f"{record['id']}.npy" | |
| dst.parent.mkdir(parents=True, exist_ok=True) | |
| np.save(dst, signal) | |
| count += 1 | |
| assert count == 502, f"Expected 502, got {count}" | |
| print(f"Iquique: created {count} NPY files") | |
| if __name__ == "__main__": | |
| main() | |