File size: 4,741 Bytes
a0d9759
 
 
d5333b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
---
license: mit
---

# Quakeflow_NC

## Introduction
This dataset is part of the data from NCEDC (Northern California Earthquake Data Center) and is organised as several HDF5 files. The dataset structure is shown below: (File [ncedc_event_dataset_000.h5.txt](./ncedc_event_dataset_000.h5.txt) shows the structure of the firsr shard of the dataset, and you can find more information about the format at [AI4EPS](https://ai4eps.github.io/homepage/ml4earth/seismic_event_format1/))

```
Group: / len:10000
  |- Group: /nc100012 len:5
  |  |-* begin_time = 1987-05-08T00:15:48.890
  |  |-* depth_km = 7.04
  |  |-* end_time = 1987-05-08T00:17:48.890
  |  |-* event_id = nc100012
  |  |-* event_time = 1987-05-08T00:16:14.700
  |  |-* event_time_index = 2581
  |  |-* latitude = 37.5423
  |  |-* longitude = -118.4412
  |  |-* magnitude = 1.1
  |  |-* magnitude_type = D
  |  |-* num_stations = 5
  |  |- Dataset: /nc100012/NC.MRS..EH (shape:(3, 12000))
  |  |  |- (dtype=float32)
  |  |  |  |-* azimuth = 265.0
  |  |  |  |-* component = ['Z']
  |  |  |  |-* distance_km = 39.1
  |  |  |  |-* dt_s = 0.01
  |  |  |  |-* elevation_m = 3680.0
  |  |  |  |-* emergence_angle = 93.0
  |  |  |  |-* event_id = ['nc100012' 'nc100012']
  |  |  |  |-* latitude = 37.5107
  |  |  |  |-* location = 
  |  |  |  |-* longitude = -118.8822
  |  |  |  |-* network = NC
  |  |  |  |-* phase_index = [3274 3802]
  |  |  |  |-* phase_polarity = ['U' 'N']
  |  |  |  |-* phase_remark = ['IP' 'S']
  |  |  |  |-* phase_score = [1 1]
  |  |  |  |-* phase_time = ['1987-05-08T00:16:21.630' '1987-05-08T00:16:26.920']
  |  |  |  |-* phase_type = ['P' 'S']
  |  |  |  |-* snr = [0.         0.         1.98844361]
  |  |  |  |-* station = MRS
  |  |  |  |-* unit = 1e-6m/s
  |  |- Dataset: /nc100012/NN.BEN.N1.EH (shape:(3, 12000))
  |  |  |- (dtype=float32)
  |  |  |  |-* azimuth = 329.0
  |  |  |  |-* component = ['Z']
  |  |  |  |-* distance_km = 22.5
  |  |  |  |-* dt_s = 0.01
  |  |  |  |-* elevation_m = 2476.0
  |  |  |  |-* emergence_angle = 102.0
  |  |  |  |-* event_id = ['nc100012' 'nc100012']
  |  |  |  |-* latitude = 37.7154
  |  |  |  |-* location = N1
  |  |  |  |-* longitude = -118.5741
  |  |  |  |-* network = NN
  |  |  |  |-* phase_index = [3010 3330]
  |  |  |  |-* phase_polarity = ['U' 'N']
  |  |  |  |-* phase_remark = ['IP' 'S']
  |  |  |  |-* phase_score = [0 0]
  |  |  |  |-* phase_time = ['1987-05-08T00:16:18.990' '1987-05-08T00:16:22.190']
  |  |  |  |-* phase_type = ['P' 'S']
  |  |  |  |-* snr = [0.         0.         7.31356192]
  |  |  |  |-* station = BEN
  |  |  |  |-* unit = 1e-6m/s
  ......
  ```

## How to use

### Requirements
- datasets
- h5py
- torch (for PyTorch)

### Usage
```python
import h5py
import numpy as np
import torch
from torch.utils.data import Dataset, IterableDataset, DataLoader
from datasets import load_dataset

# load dataset
# ATTENTION: Streaming(Iterable Dataset) is difficult to support because of the feature of HDF5
# So we recommend to directly load the dataset and convert it into iterable later
# The dataset is very large, so you need to wait for some time at the first time
quakeflow_nc = datasets.load_dataset("AI4EPS/quakeflow_nc", split="train")
quakeflow_nc
```
If you want to use the first several shards of the dataset, you can download the script `quakeflow_nc.py` and change the code below:
```python
# change the 37 to the number of shards you want
_URLS = {
    "NCEDC": [f"{_REPO}/ncedc_event_dataset_{i:03d}.h5" for i in range(37)]
}
```
Then you can use the dataset like this:
```python
quakeflow_nc = datasets.load_dataset("./quakeflow_nc.py", split="train")
quakeflow_nc
```
Then you can change the dataset into PyTorch format iterable dataset, and view the first sample:
```python
quakeflow_nc = quakeflow_nc.to_iterable_dataset()
quakeflow_nc = quakeflow_nc.with_format("torch")
# because add examples formatting to get tensors when using the "torch" format
# has not been implemented yet, we need to manually add the formatting
quakeflow_nc = quakeflow_nc.map(lambda x: {key: torch.from_numpy(np.array(value, dtype=np.float32)) for key, value in x.items()})
try:
    isinstance(quakeflow_nc, torch.utils.data.IterableDataset)
except:
    raise Exception("quakeflow_nc is not an IterableDataset")

# print the first sample of the iterable dataset
for example in quakeflow_nc:
    print("\nIterable test\n")
    print(example.keys())
    for key in example.keys():
        print(key, example[key].shape, example[key].dtype)
    break

dataloader = DataLoader(quakeflow_nc, batch_size=4)

for batch in dataloader:
    print("\nDataloader test\n")
    print(batch.keys())
    for key in batch.keys():
        print(key, batch[key].shape, batch[key].dtype)
    break
```