File size: 1,940 Bytes
9b6e0aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

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()