doc & scripts update (#1)
Browse files- update urls (a6afd6d500ff8c8b26ac2f8dbf8988ffefbe0b1c)
- update doc (665e5756680c48f6f41e6cf5fa4328f9dbd4ddeb)
Co-authored-by: kylewhy <kylewhy@users.noreply.huggingface.co>
- CEED.py +15 -30
- README.md +176 -1
- example.py +1 -0
CEED.py
CHANGED
|
@@ -123,25 +123,11 @@ _FILES_SC = [
|
|
| 123 |
]
|
| 124 |
|
| 125 |
_URLS = {
|
| 126 |
-
"
|
| 127 |
-
"
|
| 128 |
-
"station_train": [f"{_REPO_NC}/{x}" for x in _FILES_NC[:-1]] + [f"{_REPO_SC}/{x}" for x in _FILES_SC[:-1]],
|
| 129 |
-
"event_train": [f"{_REPO_NC}/{x}" for x in _FILES_NC[:-1]] + [f"{_REPO_SC}/{x}" for x in _FILES_SC[:-1]],
|
| 130 |
-
"station_test": [f"{_REPO_NC}/{x}" for x in _FILES_NC[-1:]] + [f"{_REPO_SC}/{x}" for x in _FILES_SC[-1:]],
|
| 131 |
-
"event_test": [f"{_REPO_NC}/{x}" for x in _FILES_NC[-1:]] + [f"{_REPO_SC}/{x}" for x in _FILES_SC[-1:]],
|
| 132 |
}
|
| 133 |
|
| 134 |
|
| 135 |
-
class BatchBuilderConfig(datasets.BuilderConfig):
|
| 136 |
-
"""
|
| 137 |
-
yield a batch of event-based sample, so the number of sample stations can vary among batches
|
| 138 |
-
Batch Config for CEED
|
| 139 |
-
"""
|
| 140 |
-
|
| 141 |
-
def __init__(self, **kwargs):
|
| 142 |
-
super().__init__(**kwargs)
|
| 143 |
-
|
| 144 |
-
|
| 145 |
# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
|
| 146 |
class CEED(datasets.GeneratorBasedBuilder):
|
| 147 |
"""CEED: A dataset of earthquake waveforms organized by earthquake events and based on the HDF5 format."""
|
|
@@ -254,7 +240,15 @@ class CEED(datasets.GeneratorBasedBuilder):
|
|
| 254 |
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
|
| 255 |
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
|
| 256 |
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
# files = dl_manager.download(urls)
|
| 259 |
files = dl_manager.download_and_extract(urls)
|
| 260 |
# files = ["waveform_h5/1989.h5", "waveform_h5/1990.h5"]
|
|
@@ -266,13 +260,13 @@ class CEED(datasets.GeneratorBasedBuilder):
|
|
| 266 |
name=datasets.Split.TRAIN,
|
| 267 |
# These kwargs will be passed to _generate_examples
|
| 268 |
gen_kwargs={
|
| 269 |
-
"filepath": files[:-
|
| 270 |
"split": "train",
|
| 271 |
},
|
| 272 |
),
|
| 273 |
datasets.SplitGenerator(
|
| 274 |
name=datasets.Split.TEST,
|
| 275 |
-
gen_kwargs={"filepath": files[-
|
| 276 |
),
|
| 277 |
]
|
| 278 |
elif self.config.name == "station_train" or self.config.name == "event_train":
|
|
@@ -319,11 +313,7 @@ class CEED(datasets.GeneratorBasedBuilder):
|
|
| 319 |
station_ids = list(event.keys())
|
| 320 |
if len(station_ids) == 0:
|
| 321 |
continue
|
| 322 |
-
if (
|
| 323 |
-
(self.config.name == "station")
|
| 324 |
-
or (self.config.name == "station_train")
|
| 325 |
-
or (self.config.name == "station_test")
|
| 326 |
-
):
|
| 327 |
waveforms = np.zeros([3, self.nt], dtype="float32")
|
| 328 |
|
| 329 |
for i, sta_id in enumerate(station_ids):
|
|
@@ -349,12 +339,7 @@ class CEED(datasets.GeneratorBasedBuilder):
|
|
| 349 |
"station_location": station_location,
|
| 350 |
}
|
| 351 |
|
| 352 |
-
elif (
|
| 353 |
-
(self.config.name == "event")
|
| 354 |
-
or (self.config.name == "event_train")
|
| 355 |
-
or (self.config.name == "event_test")
|
| 356 |
-
):
|
| 357 |
-
|
| 358 |
waveforms = np.zeros([len(station_ids), 3, self.nt], dtype="float32")
|
| 359 |
phase_type = []
|
| 360 |
phase_time = []
|
|
|
|
| 123 |
]
|
| 124 |
|
| 125 |
_URLS = {
|
| 126 |
+
"train": [f"{_REPO_NC}/{x}" for x in _FILES_NC[:-1]] + [f"{_REPO_SC}/{x}" for x in _FILES_SC[:-1]],
|
| 127 |
+
"test": [f"{_REPO_NC}/{x}" for x in _FILES_NC[-1:]] + [f"{_REPO_SC}/{x}" for x in _FILES_SC[-1:]],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
}
|
| 129 |
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
|
| 132 |
class CEED(datasets.GeneratorBasedBuilder):
|
| 133 |
"""CEED: A dataset of earthquake waveforms organized by earthquake events and based on the HDF5 format."""
|
|
|
|
| 240 |
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
|
| 241 |
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
|
| 242 |
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
| 243 |
+
if self.config.name in ["station", "event"]:
|
| 244 |
+
urls = _URLS["train"] + _URLS["test"]
|
| 245 |
+
elif self.config.name in ["station_train", "event_train"]:
|
| 246 |
+
urls = _URLS["train"]
|
| 247 |
+
elif self.config.name in ["station_test", "event_test"]:
|
| 248 |
+
urls = _URLS["test"]
|
| 249 |
+
else:
|
| 250 |
+
raise ValueError("config.name is not in BUILDER_CONFIGS")
|
| 251 |
+
|
| 252 |
# files = dl_manager.download(urls)
|
| 253 |
files = dl_manager.download_and_extract(urls)
|
| 254 |
# files = ["waveform_h5/1989.h5", "waveform_h5/1990.h5"]
|
|
|
|
| 260 |
name=datasets.Split.TRAIN,
|
| 261 |
# These kwargs will be passed to _generate_examples
|
| 262 |
gen_kwargs={
|
| 263 |
+
"filepath": files[:-2],
|
| 264 |
"split": "train",
|
| 265 |
},
|
| 266 |
),
|
| 267 |
datasets.SplitGenerator(
|
| 268 |
name=datasets.Split.TEST,
|
| 269 |
+
gen_kwargs={"filepath": files[-2:], "split": "test"},
|
| 270 |
),
|
| 271 |
]
|
| 272 |
elif self.config.name == "station_train" or self.config.name == "event_train":
|
|
|
|
| 313 |
station_ids = list(event.keys())
|
| 314 |
if len(station_ids) == 0:
|
| 315 |
continue
|
| 316 |
+
if ("station" in self.config.name):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
waveforms = np.zeros([3, self.nt], dtype="float32")
|
| 318 |
|
| 319 |
for i, sta_id in enumerate(station_ids):
|
|
|
|
| 339 |
"station_location": station_location,
|
| 340 |
}
|
| 341 |
|
| 342 |
+
elif ("event" in self.config.name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
waveforms = np.zeros([len(station_ids), 3, self.nt], dtype="float32")
|
| 344 |
phase_type = []
|
| 345 |
phase_time = []
|
README.md
CHANGED
|
@@ -2,4 +2,179 @@
|
|
| 2 |
license: mit
|
| 3 |
---
|
| 4 |
|
| 5 |
-
## CEED: *C*alifornia *E*arthquak*E* *D*ataset for Machine Learning and Cloud Computing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
license: mit
|
| 3 |
---
|
| 4 |
|
| 5 |
+
## CEED: *C*alifornia *E*arthquak*E* *D*ataset for Machine Learning and Cloud Computing
|
| 6 |
+
|
| 7 |
+
The California EarthquakE Dataset (CEED) is a dataset of earthquake waveforms and metadata for machine learning and cloud computing. The dataset structure is shown below, and you can find more information about the format at [AI4EPS](https://ai4eps.github.io/homepage/ml4earth/seismic_event_format1/)
|
| 8 |
+
|
| 9 |
+
```
|
| 10 |
+
Group: / len:60424
|
| 11 |
+
|- Group: /ci38457511 len:35
|
| 12 |
+
| |-* begin_time = 2019-07-06T03:19:23.668000
|
| 13 |
+
| |-* depth_km = 8.0
|
| 14 |
+
| |-* end_time = 2019-07-06T03:21:23.668000
|
| 15 |
+
| |-* event_id = ci38457511
|
| 16 |
+
| |-* event_time = 2019-07-06T03:19:53.040000
|
| 17 |
+
| |-* event_time_index = 2937
|
| 18 |
+
| |-* latitude = 35.7695
|
| 19 |
+
| |-* longitude = -117.5993
|
| 20 |
+
| |-* magnitude = 7.1
|
| 21 |
+
| |-* magnitude_type = w
|
| 22 |
+
| |-* nt = 12000
|
| 23 |
+
| |-* nx = 35
|
| 24 |
+
| |-* sampling_rate = 100
|
| 25 |
+
| |-* source = SC
|
| 26 |
+
| |- Dataset: /ci38457511/CI.CCC..HH (shape:(3, 12000))
|
| 27 |
+
| | |- (dtype=float32)
|
| 28 |
+
| | | |-* azimuth = 141.849479
|
| 29 |
+
| | | |-* back_azimuth = 321.986302
|
| 30 |
+
| | | |-* component = ENZ
|
| 31 |
+
| | | |-* depth_km = -0.67
|
| 32 |
+
| | | |-* distance_km = 34.471389
|
| 33 |
+
| | | |-* dt_s = 0.01
|
| 34 |
+
| | | |-* elevation_m = 670.0
|
| 35 |
+
| | | |-* event_id = ['ci38457511' 'ci38457511' 'ci37260300']
|
| 36 |
+
| | | |-* instrument = HH
|
| 37 |
+
| | | |-* latitude = 35.52495
|
| 38 |
+
| | | |-* local_depth_m = 0.0
|
| 39 |
+
| | | |-* location =
|
| 40 |
+
| | | |-* longitude = -117.36453
|
| 41 |
+
| | | |-* network = CI
|
| 42 |
+
| | | |-* p_phase_index = 3575
|
| 43 |
+
| | | |-* p_phase_polarity = U
|
| 44 |
+
| | | |-* p_phase_score = 0.8
|
| 45 |
+
| | | |-* p_phase_status = manual
|
| 46 |
+
| | | |-* p_phase_time = 2019-07-06T03:19:59.422000
|
| 47 |
+
| | | |-* phase_index = [ 3575 4184 11826]
|
| 48 |
+
| | | |-* phase_picking_channel = ['HHZ' 'HNN' 'HHZ']
|
| 49 |
+
| | | |-* phase_polarity = ['U' 'N' 'N']
|
| 50 |
+
| | | |-* phase_remark = ['i' 'e' 'e']
|
| 51 |
+
| | | |-* phase_score = [0.8 0.5 0.5]
|
| 52 |
+
| | | |-* phase_status = manual
|
| 53 |
+
| | | |-* phase_time = ['2019-07-06T03:19:59.422000' '2019-07-06T03:20:05.509000' '2019-07-06T03:21:21.928000']
|
| 54 |
+
| | | |-* phase_type = ['P' 'S' 'P']
|
| 55 |
+
| | | |-* s_phase_index = 4184
|
| 56 |
+
| | | |-* s_phase_polarity = N
|
| 57 |
+
| | | |-* s_phase_score = 0.5
|
| 58 |
+
| | | |-* s_phase_status = manual
|
| 59 |
+
| | | |-* s_phase_time = 2019-07-06T03:20:05.509000
|
| 60 |
+
| | | |-* snr = [ 637.9865898 286.9100766 1433.04052911]
|
| 61 |
+
| | | |-* station = CCC
|
| 62 |
+
| | | |-* unit = 1e-6m/s
|
| 63 |
+
| |- Dataset: /ci38457511/CI.CCC..HN (shape:(3, 12000))
|
| 64 |
+
| | |- (dtype=float32)
|
| 65 |
+
| | | |-* azimuth = 141.849479
|
| 66 |
+
| | | |-* back_azimuth = 321.986302
|
| 67 |
+
| | | |-* component = ENZ
|
| 68 |
+
| | | |-* depth_km = -0.67
|
| 69 |
+
| | | |-* distance_km = 34.471389
|
| 70 |
+
| | | |-* dt_s = 0.01
|
| 71 |
+
| | | |-* elevation_m = 670.0
|
| 72 |
+
| | | |-* event_id = ['ci38457511' 'ci38457511' 'ci37260300']
|
| 73 |
+
......
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
## Getting Started
|
| 77 |
+
|
| 78 |
+
### Requirements
|
| 79 |
+
- datasets
|
| 80 |
+
- h5py
|
| 81 |
+
- fsspec
|
| 82 |
+
- pytorch
|
| 83 |
+
|
| 84 |
+
### Usage
|
| 85 |
+
Import the necessary packages:
|
| 86 |
+
```python
|
| 87 |
+
import h5py
|
| 88 |
+
import numpy as np
|
| 89 |
+
import torch
|
| 90 |
+
from datasets import load_dataset
|
| 91 |
+
```
|
| 92 |
+
We have 6 configurations for the dataset:
|
| 93 |
+
- "station"
|
| 94 |
+
- "event"
|
| 95 |
+
- "station_train"
|
| 96 |
+
- "event_train"
|
| 97 |
+
- "station_test"
|
| 98 |
+
- "event_test"
|
| 99 |
+
|
| 100 |
+
"station" yields station-based samples one by one, while "event" yields event-based samples one by one. The configurations with no suffix are the full dataset, while the configurations with suffix "_train" and "_test" only have corresponding split of the full dataset. Train split contains data from 1970 to 2019, while test split contains data in 2020.
|
| 101 |
+
|
| 102 |
+
The sample of `station` is a dictionary with the following keys:
|
| 103 |
+
- `data`: the waveform with shape `(3, nt)`, the default time length is 8192
|
| 104 |
+
- `begin_time`: the begin time of the waveform data
|
| 105 |
+
- `end_time`: the end time of the waveform data
|
| 106 |
+
- `phase_time`: the phase arrival time
|
| 107 |
+
- `phase_index`: the time point index of the phase arrival time
|
| 108 |
+
- `phase_type`: the phase type
|
| 109 |
+
- `phase_polarity`: the phase polarity in ('U', 'D', 'N')
|
| 110 |
+
- `event_time`: the event time
|
| 111 |
+
- `event_time_index`: the time point index of the event time
|
| 112 |
+
- `event_location`: the event location with shape `(3,)`, including latitude, longitude, depth
|
| 113 |
+
- `station_location`: the station location with shape `(3,)`, including latitude, longitude and depth
|
| 114 |
+
|
| 115 |
+
The sample of `event` is a dictionary with the following keys:
|
| 116 |
+
- `data`: the waveform with shape `(n_station, 3, nt)`, the default time length is 8192
|
| 117 |
+
- `begin_time`: the begin time of the waveform data
|
| 118 |
+
- `end_time`: the end time of the waveform data
|
| 119 |
+
- `phase_time`: the phase arrival time with shape `(n_station,)`
|
| 120 |
+
- `phase_index`: the time point index of the phase arrival time with shape `(n_station,)`
|
| 121 |
+
- `phase_type`: the phase type with shape `(n_station,)`
|
| 122 |
+
- `phase_polarity`: the phase polarity in ('U', 'D', 'N') with shape `(n_station,)`
|
| 123 |
+
- `event_time`: the event time
|
| 124 |
+
- `event_time_index`: the time point index of the event time
|
| 125 |
+
- `event_location`: the space-time coordinates of the event with shape `(n_staion, 3)`
|
| 126 |
+
- `station_location`: the space coordinates of the station with shape `(n_station, 3)`, including latitude, longitude and depth
|
| 127 |
+
|
| 128 |
+
The default configuration is `station_test`. You can specify the configuration by argument `name`. For example:
|
| 129 |
+
```python
|
| 130 |
+
# load dataset
|
| 131 |
+
# ATTENTION: Streaming(Iterable Dataset) is complex to support because of the feature of HDF5
|
| 132 |
+
# So we recommend to directly load the dataset and convert it into iterable later
|
| 133 |
+
# The dataset is very large, so you need to wait for some time at the first time
|
| 134 |
+
|
| 135 |
+
# to load "station_test" with test split
|
| 136 |
+
ceed = load_dataset("AI4EPS/CEED", split="test")
|
| 137 |
+
# or
|
| 138 |
+
ceed = load_dataset("AI4EPS/CEED", name="station_test", split="test")
|
| 139 |
+
|
| 140 |
+
# to load "event" with train split
|
| 141 |
+
ceed = load_dataset("AI4EPS/CEED", name="event", split="train")
|
| 142 |
+
```
|
| 143 |
+
|
| 144 |
+
#### Example loading the dataset
|
| 145 |
+
```python
|
| 146 |
+
ceed = load_dataset("AI4EPS/CEED", name="station_test", split="test")
|
| 147 |
+
|
| 148 |
+
# print the first sample of the iterable dataset
|
| 149 |
+
for example in ceed:
|
| 150 |
+
print("\nIterable test\n")
|
| 151 |
+
print(example.keys())
|
| 152 |
+
for key in example.keys():
|
| 153 |
+
if key == "data":
|
| 154 |
+
print(key, np.array(example[key]).shape)
|
| 155 |
+
else:
|
| 156 |
+
print(key, example[key])
|
| 157 |
+
break
|
| 158 |
+
|
| 159 |
+
# %%
|
| 160 |
+
ceed = ceed.with_format("torch")
|
| 161 |
+
dataloader = DataLoader(ceed, batch_size=8, num_workers=0, collate_fn=lambda x: x)
|
| 162 |
+
|
| 163 |
+
for batch in dataloader:
|
| 164 |
+
print("\nDataloader test\n")
|
| 165 |
+
print(f"Batch size: {len(batch)}")
|
| 166 |
+
print(batch[0].keys())
|
| 167 |
+
for key in batch[0].keys():
|
| 168 |
+
if key == "data":
|
| 169 |
+
print(key, np.array(batch[0][key]).shape)
|
| 170 |
+
else:
|
| 171 |
+
print(key, batch[0][key])
|
| 172 |
+
break
|
| 173 |
+
```
|
| 174 |
+
|
| 175 |
+
#### Extension
|
| 176 |
+
|
| 177 |
+
If you want to introduce new features in to labels, we recommend to make a copy of `CEED.py` and modify the `_generate_examples` method. Check [AI4EPS/EQNet](https://github.com/AI4EPS/EQNet/blob/master/eqnet/data/quakeflow_nc.py) for an example. To load the dataset with your modified script, specify the path to the script in `load_dataset` function:
|
| 178 |
+
```python
|
| 179 |
+
ceed = load_dataset("path/to/your/CEED.py", name="station_test", split="test", trust_remote_code=True)
|
| 180 |
+
```
|
example.py
CHANGED
|
@@ -10,6 +10,7 @@ ceed = load_dataset(
|
|
| 10 |
# name="event_test",
|
| 11 |
split="test",
|
| 12 |
download_mode="force_redownload",
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
# print the first sample of the iterable dataset
|
|
|
|
| 10 |
# name="event_test",
|
| 11 |
split="test",
|
| 12 |
download_mode="force_redownload",
|
| 13 |
+
trust_remote_code=True,
|
| 14 |
)
|
| 15 |
|
| 16 |
# print the first sample of the iterable dataset
|