Datasets:
ArXiv:
DOI:
License:
rename data -> waveform
Browse files- example.py +22 -9
- quakeflow_nc.py +15 -19
example.py
CHANGED
|
@@ -1,20 +1,33 @@
|
|
| 1 |
# %%
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
-
from datasets import load_dataset
|
| 4 |
from torch.utils.data import DataLoader
|
| 5 |
|
| 6 |
-
|
| 7 |
-
quakeflow_nc
|
| 8 |
-
"
|
| 9 |
-
|
| 10 |
-
# name="
|
| 11 |
-
split="test",
|
| 12 |
download_mode="force_redownload",
|
|
|
|
|
|
|
| 13 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# print the first sample of the iterable dataset
|
| 16 |
for example in quakeflow_nc:
|
| 17 |
-
print("\nIterable
|
|
|
|
| 18 |
print(example.keys())
|
| 19 |
for key in example.keys():
|
| 20 |
if key == "data":
|
|
@@ -28,7 +41,7 @@ quakeflow_nc = quakeflow_nc.with_format("torch")
|
|
| 28 |
dataloader = DataLoader(quakeflow_nc, batch_size=8, num_workers=0, collate_fn=lambda x: x)
|
| 29 |
|
| 30 |
for batch in dataloader:
|
| 31 |
-
print("\nDataloader
|
| 32 |
print(f"Batch size: {len(batch)}")
|
| 33 |
print(batch[0].keys())
|
| 34 |
for key in batch[0].keys():
|
|
|
|
| 1 |
# %%
|
| 2 |
+
import datasets
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from torch.utils.data import DataLoader
|
| 5 |
|
| 6 |
+
quakeflow_nc = datasets.load_dataset(
|
| 7 |
+
"AI4EPS/quakeflow_nc",
|
| 8 |
+
name="station",
|
| 9 |
+
split="train",
|
| 10 |
+
# name="station_test",
|
| 11 |
+
# split="test",
|
| 12 |
download_mode="force_redownload",
|
| 13 |
+
trust_remote_code=True,
|
| 14 |
+
num_proc=36,
|
| 15 |
)
|
| 16 |
+
# quakeflow_nc = datasets.load_dataset(
|
| 17 |
+
# "./quakeflow_nc.py",
|
| 18 |
+
# name="station",
|
| 19 |
+
# split="train",
|
| 20 |
+
# # name="statoin_test",
|
| 21 |
+
# # split="test",
|
| 22 |
+
# num_proc=36,
|
| 23 |
+
# )
|
| 24 |
+
|
| 25 |
+
print(quakeflow_nc)
|
| 26 |
|
| 27 |
# print the first sample of the iterable dataset
|
| 28 |
for example in quakeflow_nc:
|
| 29 |
+
print("\nIterable dataset\n")
|
| 30 |
+
print(example)
|
| 31 |
print(example.keys())
|
| 32 |
for key in example.keys():
|
| 33 |
if key == "data":
|
|
|
|
| 41 |
dataloader = DataLoader(quakeflow_nc, batch_size=8, num_workers=0, collate_fn=lambda x: x)
|
| 42 |
|
| 43 |
for batch in dataloader:
|
| 44 |
+
print("\nDataloader dataset\n")
|
| 45 |
print(f"Batch size: {len(batch)}")
|
| 46 |
print(batch[0].keys())
|
| 47 |
for key in batch[0].keys():
|
quakeflow_nc.py
CHANGED
|
@@ -167,7 +167,7 @@ class QuakeFlow_NC(datasets.GeneratorBasedBuilder):
|
|
| 167 |
):
|
| 168 |
features = datasets.Features(
|
| 169 |
{
|
| 170 |
-
"
|
| 171 |
"phase_time": datasets.Sequence(datasets.Value("string")),
|
| 172 |
"phase_index": datasets.Sequence(datasets.Value("int32")),
|
| 173 |
"phase_type": datasets.Sequence(datasets.Value("string")),
|
|
@@ -183,7 +183,7 @@ class QuakeFlow_NC(datasets.GeneratorBasedBuilder):
|
|
| 183 |
elif (self.config.name == "event") or (self.config.name == "event_train") or (self.config.name == "event_test"):
|
| 184 |
features = datasets.Features(
|
| 185 |
{
|
| 186 |
-
"
|
| 187 |
"phase_time": datasets.Sequence(datasets.Sequence(datasets.Value("string"))),
|
| 188 |
"phase_index": datasets.Sequence(datasets.Sequence(datasets.Value("int32"))),
|
| 189 |
"phase_type": datasets.Sequence(datasets.Sequence(datasets.Value("string"))),
|
|
@@ -224,19 +224,17 @@ class QuakeFlow_NC(datasets.GeneratorBasedBuilder):
|
|
| 224 |
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
| 225 |
urls = _URLS[self.config.name]
|
| 226 |
# files = dl_manager.download(urls)
|
| 227 |
-
files = dl_manager.download_and_extract(urls)
|
| 228 |
-
|
| 229 |
-
|
|
|
|
| 230 |
|
| 231 |
if self.config.name == "station" or self.config.name == "event":
|
| 232 |
return [
|
| 233 |
datasets.SplitGenerator(
|
| 234 |
name=datasets.Split.TRAIN,
|
| 235 |
# These kwargs will be passed to _generate_examples
|
| 236 |
-
gen_kwargs={
|
| 237 |
-
"filepath": files[:-1],
|
| 238 |
-
"split": "train",
|
| 239 |
-
},
|
| 240 |
),
|
| 241 |
datasets.SplitGenerator(
|
| 242 |
name=datasets.Split.TEST,
|
|
@@ -247,10 +245,7 @@ class QuakeFlow_NC(datasets.GeneratorBasedBuilder):
|
|
| 247 |
return [
|
| 248 |
datasets.SplitGenerator(
|
| 249 |
name=datasets.Split.TRAIN,
|
| 250 |
-
gen_kwargs={
|
| 251 |
-
"filepath": files,
|
| 252 |
-
"split": "train",
|
| 253 |
-
},
|
| 254 |
),
|
| 255 |
]
|
| 256 |
elif self.config.name == "station_test" or self.config.name == "event_test":
|
|
@@ -269,6 +264,7 @@ class QuakeFlow_NC(datasets.GeneratorBasedBuilder):
|
|
| 269 |
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
|
| 270 |
|
| 271 |
for file in filepath:
|
|
|
|
| 272 |
with fsspec.open(file, "rb") as fs:
|
| 273 |
with h5py.File(fs, "r") as fp:
|
| 274 |
event_ids = list(fp.keys())
|
|
@@ -292,10 +288,10 @@ class QuakeFlow_NC(datasets.GeneratorBasedBuilder):
|
|
| 292 |
or (self.config.name == "station_train")
|
| 293 |
or (self.config.name == "station_test")
|
| 294 |
):
|
| 295 |
-
|
| 296 |
|
| 297 |
for i, sta_id in enumerate(station_ids):
|
| 298 |
-
|
| 299 |
attrs = event[sta_id].attrs
|
| 300 |
phase_type = attrs["phase_type"]
|
| 301 |
phase_time = attrs["phase_time"]
|
|
@@ -304,7 +300,7 @@ class QuakeFlow_NC(datasets.GeneratorBasedBuilder):
|
|
| 304 |
station_location = [attrs["longitude"], attrs["latitude"], -attrs["elevation_m"] / 1e3]
|
| 305 |
|
| 306 |
yield f"{event_id}/{sta_id}", {
|
| 307 |
-
"
|
| 308 |
"phase_time": phase_time,
|
| 309 |
"phase_index": phase_index,
|
| 310 |
"phase_type": phase_type,
|
|
@@ -323,7 +319,7 @@ class QuakeFlow_NC(datasets.GeneratorBasedBuilder):
|
|
| 323 |
or (self.config.name == "event_test")
|
| 324 |
):
|
| 325 |
|
| 326 |
-
|
| 327 |
phase_type = []
|
| 328 |
phase_time = []
|
| 329 |
phase_index = []
|
|
@@ -331,7 +327,7 @@ class QuakeFlow_NC(datasets.GeneratorBasedBuilder):
|
|
| 331 |
station_location = []
|
| 332 |
|
| 333 |
for i, sta_id in enumerate(station_ids):
|
| 334 |
-
|
| 335 |
attrs = event[sta_id].attrs
|
| 336 |
phase_type.append(list(attrs["phase_type"]))
|
| 337 |
phase_time.append(list(attrs["phase_time"]))
|
|
@@ -341,7 +337,7 @@ class QuakeFlow_NC(datasets.GeneratorBasedBuilder):
|
|
| 341 |
[attrs["longitude"], attrs["latitude"], -attrs["elevation_m"] / 1e3]
|
| 342 |
)
|
| 343 |
yield event_id, {
|
| 344 |
-
"
|
| 345 |
"phase_time": phase_time,
|
| 346 |
"phase_index": phase_index,
|
| 347 |
"phase_type": phase_type,
|
|
|
|
| 167 |
):
|
| 168 |
features = datasets.Features(
|
| 169 |
{
|
| 170 |
+
"waveform": datasets.Array2D(shape=(3, self.nt), dtype="float32"),
|
| 171 |
"phase_time": datasets.Sequence(datasets.Value("string")),
|
| 172 |
"phase_index": datasets.Sequence(datasets.Value("int32")),
|
| 173 |
"phase_type": datasets.Sequence(datasets.Value("string")),
|
|
|
|
| 183 |
elif (self.config.name == "event") or (self.config.name == "event_train") or (self.config.name == "event_test"):
|
| 184 |
features = datasets.Features(
|
| 185 |
{
|
| 186 |
+
"waveform": datasets.Array3D(shape=(None, 3, self.nt), dtype="float32"),
|
| 187 |
"phase_time": datasets.Sequence(datasets.Sequence(datasets.Value("string"))),
|
| 188 |
"phase_index": datasets.Sequence(datasets.Sequence(datasets.Value("int32"))),
|
| 189 |
"phase_type": datasets.Sequence(datasets.Sequence(datasets.Value("string"))),
|
|
|
|
| 224 |
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
| 225 |
urls = _URLS[self.config.name]
|
| 226 |
# files = dl_manager.download(urls)
|
| 227 |
+
# files = dl_manager.download_and_extract(urls)
|
| 228 |
+
files = [f"waveform_h5/{x}" for x in _FILES]
|
| 229 |
+
for file in sorted(files):
|
| 230 |
+
print(file)
|
| 231 |
|
| 232 |
if self.config.name == "station" or self.config.name == "event":
|
| 233 |
return [
|
| 234 |
datasets.SplitGenerator(
|
| 235 |
name=datasets.Split.TRAIN,
|
| 236 |
# These kwargs will be passed to _generate_examples
|
| 237 |
+
gen_kwargs={"filepath": files[:-1], "split": "train"},
|
|
|
|
|
|
|
|
|
|
| 238 |
),
|
| 239 |
datasets.SplitGenerator(
|
| 240 |
name=datasets.Split.TEST,
|
|
|
|
| 245 |
return [
|
| 246 |
datasets.SplitGenerator(
|
| 247 |
name=datasets.Split.TRAIN,
|
| 248 |
+
gen_kwargs={"filepath": files, "split": "train"},
|
|
|
|
|
|
|
|
|
|
| 249 |
),
|
| 250 |
]
|
| 251 |
elif self.config.name == "station_test" or self.config.name == "event_test":
|
|
|
|
| 264 |
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
|
| 265 |
|
| 266 |
for file in filepath:
|
| 267 |
+
print(f"\nReading {file}")
|
| 268 |
with fsspec.open(file, "rb") as fs:
|
| 269 |
with h5py.File(fs, "r") as fp:
|
| 270 |
event_ids = list(fp.keys())
|
|
|
|
| 288 |
or (self.config.name == "station_train")
|
| 289 |
or (self.config.name == "station_test")
|
| 290 |
):
|
| 291 |
+
waveform = np.zeros([3, self.nt], dtype="float32")
|
| 292 |
|
| 293 |
for i, sta_id in enumerate(station_ids):
|
| 294 |
+
waveform[:, : self.nt] = event[sta_id][:, : self.nt]
|
| 295 |
attrs = event[sta_id].attrs
|
| 296 |
phase_type = attrs["phase_type"]
|
| 297 |
phase_time = attrs["phase_time"]
|
|
|
|
| 300 |
station_location = [attrs["longitude"], attrs["latitude"], -attrs["elevation_m"] / 1e3]
|
| 301 |
|
| 302 |
yield f"{event_id}/{sta_id}", {
|
| 303 |
+
"waveform": waveform,
|
| 304 |
"phase_time": phase_time,
|
| 305 |
"phase_index": phase_index,
|
| 306 |
"phase_type": phase_type,
|
|
|
|
| 319 |
or (self.config.name == "event_test")
|
| 320 |
):
|
| 321 |
|
| 322 |
+
waveform = np.zeros([len(station_ids), 3, self.nt], dtype="float32")
|
| 323 |
phase_type = []
|
| 324 |
phase_time = []
|
| 325 |
phase_index = []
|
|
|
|
| 327 |
station_location = []
|
| 328 |
|
| 329 |
for i, sta_id in enumerate(station_ids):
|
| 330 |
+
waveform[i, :, : self.nt] = event[sta_id][:, : self.nt]
|
| 331 |
attrs = event[sta_id].attrs
|
| 332 |
phase_type.append(list(attrs["phase_type"]))
|
| 333 |
phase_time.append(list(attrs["phase_time"]))
|
|
|
|
| 337 |
[attrs["longitude"], attrs["latitude"], -attrs["elevation_m"] / 1e3]
|
| 338 |
)
|
| 339 |
yield event_id, {
|
| 340 |
+
"waveform": waveform,
|
| 341 |
"phase_time": phase_time,
|
| 342 |
"phase_index": phase_index,
|
| 343 |
"phase_type": phase_type,
|