zhuwq0 commited on
Commit
2c10c18
1 Parent(s): f5fc79f

fix output

Browse files
Files changed (1) hide show
  1. quakeflow_nc.py +70 -143
quakeflow_nc.py CHANGED
@@ -120,11 +120,7 @@ class QuakeFlow_NC(datasets.GeneratorBasedBuilder):
120
 
121
  VERSION = datasets.Version("1.1.0")
122
 
123
- degree2km = 111.32
124
  nt = 8192
125
- feature_nt = 512
126
- feature_scale = int(nt / feature_nt)
127
- sampling_rate = 100.0
128
 
129
  # This is an example of a dataset with multiple configurations.
130
  # If you don't want/need to define several sub-sets in your dataset,
@@ -173,30 +169,36 @@ class QuakeFlow_NC(datasets.GeneratorBasedBuilder):
173
  or (self.config.name == "station_train")
174
  or (self.config.name == "station_test")
175
  ):
176
- features=datasets.Features(
177
  {
178
- "data": datasets.Array2D(shape=(3, self.nt), dtype='float32'),
179
- "phase_pick": datasets.Array2D(shape=(3, self.nt), dtype='float32'),
 
 
 
 
 
 
180
  "event_location": datasets.Sequence(datasets.Value("float32")),
181
  "station_location": datasets.Sequence(datasets.Value("float32")),
182
- })
183
-
184
- elif (
185
- (self.config.name == "event")
186
- or (self.config.name == "event_train")
187
- or (self.config.name == "event_test")
188
- ):
189
- features=datasets.Features(
190
  {
191
- "data": datasets.Array3D(shape=(None, 3, self.nt), dtype='float32'),
192
- "phase_pick": datasets.Array3D(shape=(None, 3, self.nt), dtype='float32'),
193
- "event_center" : datasets.Array2D(shape=(None, self.feature_nt), dtype='float32'),
194
- "event_location": datasets.Array3D(shape=(None, 4, self.feature_nt), dtype='float32'),
195
- "event_location_mask": datasets.Array2D(shape=(None, self.feature_nt), dtype='float32'),
196
- "station_location": datasets.Array2D(shape=(None, 3), dtype="float32"),
197
- }
 
 
 
 
198
  )
199
-
200
  return datasets.DatasetInfo(
201
  # This is the description that will appear on the datasets page.
202
  description=_DESCRIPTION,
@@ -268,10 +270,18 @@ class QuakeFlow_NC(datasets.GeneratorBasedBuilder):
268
  for file in filepath:
269
  with fsspec.open(file, "rb") as fs:
270
  with h5py.File(fs, "r") as fp:
271
- # for event_id in sorted(list(fp.keys())):
272
  event_ids = list(fp.keys())
273
  for event_id in event_ids:
274
  event = fp[event_id]
 
 
 
 
 
 
 
 
 
275
  station_ids = list(event.keys())
276
  if (
277
  (self.config.name == "station")
@@ -279,143 +289,60 @@ class QuakeFlow_NC(datasets.GeneratorBasedBuilder):
279
  or (self.config.name == "station_test")
280
  ):
281
  waveforms = np.zeros([3, self.nt], dtype="float32")
282
- phase_pick = np.zeros_like(waveforms)
283
- attrs = event.attrs
284
- event_location = [
285
- attrs["longitude"],
286
- attrs["latitude"],
287
- attrs["depth_km"],
288
- attrs["event_time_index"],
289
- ]
290
-
291
  for i, sta_id in enumerate(station_ids):
292
- waveforms[:, : self.nt] = event[sta_id][:, :self.nt]
293
- # waveforms[:, : self.nt] = event[sta_id][: self.nt, :].T
294
  attrs = event[sta_id].attrs
295
- p_picks = attrs["phase_index"][attrs["phase_type"] == "P"]
296
- s_picks = attrs["phase_index"][attrs["phase_type"] == "S"]
297
- phase_pick[:, :self.nt] = generate_label([p_picks, s_picks], nt=self.nt)
 
298
  station_location = [attrs["longitude"], attrs["latitude"], -attrs["elevation_m"] / 1e3]
299
 
300
  yield f"{event_id}/{sta_id}", {
301
- "data": torch.from_numpy(waveforms).float(),
302
- "phase_pick": torch.from_numpy(phase_pick).float(),
303
- "event_location": torch.from_numpy(np.array(event_location)).float(),
304
- "station_location": torch.from_numpy(np.array(station_location)).float(),
 
 
 
 
 
305
  }
306
 
307
-
308
  elif (
309
  (self.config.name == "event")
310
  or (self.config.name == "event_train")
311
  or (self.config.name == "event_test")
312
  ):
313
- event_attrs = event.attrs
314
 
315
- # avoid stations with P arrival equals S arrival
316
- is_sick = False
317
- for sta_id in station_ids:
318
- attrs = event[sta_id].attrs
319
- if attrs["phase_index"][attrs["phase_type"] == "P"] == attrs["phase_index"][attrs["phase_type"] == "S"]:
320
- is_sick = True
321
- break
322
- if is_sick:
323
- continue
324
-
325
  waveforms = np.zeros([len(station_ids), 3, self.nt], dtype="float32")
326
- phase_pick = np.zeros_like(waveforms)
327
- event_center = np.zeros([len(station_ids), self.nt])
328
- event_location = np.zeros([len(station_ids), 4, self.nt])
329
- event_location_mask = np.zeros([len(station_ids), self.nt])
330
- station_location = np.zeros([len(station_ids), 3])
331
 
332
  for i, sta_id in enumerate(station_ids):
333
- # trace_id = event_id + "/" + sta_id
334
- waveforms[i, :, :] = event[sta_id][:, :self.nt]
335
  attrs = event[sta_id].attrs
336
- p_picks = attrs["phase_index"][attrs["phase_type"] == "P"]
337
- s_picks = attrs["phase_index"][attrs["phase_type"] == "S"]
338
- phase_pick[i, :, :] = generate_label([p_picks, s_picks], nt=self.nt)
339
-
340
- ## TODO: how to deal with multiple phases
341
- # center = (attrs["phase_index"][::2] + attrs["phase_index"][1::2])/2.0
342
- ## assuming only one event with both P and S picks
343
- c0 = ((p_picks) + (s_picks)) / 2.0 # phase center
344
- c0_width = ((s_picks - p_picks) * self.sampling_rate / 200.0).max() if p_picks!=s_picks else 50
345
- dx = round(
346
- (event_attrs["longitude"] - attrs["longitude"])
347
- * np.cos(np.radians(event_attrs["latitude"]))
348
- * self.degree2km,
349
- 2,
350
- )
351
- dy = round(
352
- (event_attrs["latitude"] - attrs["latitude"])
353
- * self.degree2km,
354
- 2,
355
  )
356
- dz = round(
357
- event_attrs["depth_km"] + attrs["elevation_m"] / 1e3,
358
- 2,
359
- )
360
-
361
- event_center[i, :] = generate_label(
362
- [
363
- # [c0 / self.feature_scale],
364
- c0,
365
- ],
366
- label_width=[
367
- c0_width,
368
- ],
369
- # label_width=[
370
- # 10,
371
- # ],
372
- # nt=self.feature_nt,
373
- nt=self.nt,
374
- )[1, :]
375
- mask = event_center[i, :] >= 0.5
376
- event_location[i, 0, :] = (
377
- np.arange(self.nt) - event_attrs["event_time_index"]
378
- ) / self.sampling_rate
379
- # event_location[0, :, i] = (np.arange(self.feature_nt) - 3000 / self.feature_scale) / self.sampling_rate
380
- # print(event_location[i, 1:, mask].shape, event_location.shape, event_location[i][1:, mask].shape)
381
- event_location[i][1:, mask] = np.array([dx, dy, dz])[:, np.newaxis]
382
- event_location_mask[i, :] = mask
383
-
384
- ## station location
385
- station_location[i, 0] = round(
386
- attrs["longitude"]
387
- * np.cos(np.radians(attrs["latitude"]))
388
- * self.degree2km,
389
- 2,
390
- )
391
- station_location[i, 1] = round(attrs["latitude"] * self.degree2km, 2)
392
- station_location[i, 2] = round(-attrs["elevation_m"]/1e3, 2)
393
-
394
- std = np.std(waveforms, axis=1, keepdims=True)
395
- std[std == 0] = 1.0
396
- waveforms = (waveforms - np.mean(waveforms, axis=1, keepdims=True)) / std
397
- waveforms = waveforms.astype(np.float32)
398
 
399
  yield event_id, {
400
- "data": torch.from_numpy(waveforms).float(),
401
- "phase_pick": torch.from_numpy(phase_pick).float(),
402
- "event_center": torch.from_numpy(event_center[:, ::self.feature_scale]).float(),
403
- "event_location": torch.from_numpy(event_location[:, :, ::self.feature_scale]).float(),
404
- "event_location_mask": torch.from_numpy(event_location_mask[:, ::self.feature_scale]).float(),
405
- "station_location": torch.from_numpy(station_location).float(),
 
 
 
406
  }
407
-
408
-
409
- def generate_label(phase_list, label_width=[150, 150], nt=8192):
410
- target = np.zeros([len(phase_list) + 1, nt], dtype=np.float32)
411
-
412
- for i, (picks, w) in enumerate(zip(phase_list, label_width)):
413
- for phase_time in picks:
414
- t = np.arange(nt) - phase_time
415
- gaussian = np.exp(-(t**2) / (2 * (w / 6) ** 2))
416
- gaussian[gaussian < 0.1] = 0.0
417
- target[i + 1, :] += gaussian
418
-
419
- target[0:1, :] = np.maximum(0, 1 - np.sum(target[1:, :], axis=0, keepdims=True))
420
-
421
- return target
 
120
 
121
  VERSION = datasets.Version("1.1.0")
122
 
 
123
  nt = 8192
 
 
 
124
 
125
  # This is an example of a dataset with multiple configurations.
126
  # If you don't want/need to define several sub-sets in your dataset,
 
169
  or (self.config.name == "station_train")
170
  or (self.config.name == "station_test")
171
  ):
172
+ features = datasets.Features(
173
  {
174
+ "data": datasets.Array2D(shape=(3, self.nt), dtype="float32"),
175
+ "phase_pick": datasets.Array2D(shape=(3, self.nt), dtype="float32"),
176
+ "phase_time": datasets.Sequence(datasets.Value("string")),
177
+ "phase_index": datasets.Sequence(datasets.Value("int32")),
178
+ "phase_type": datasets.Sequence(datasets.Value("string")),
179
+ "phase_polarity": datasets.Sequence(datasets.Value("string")),
180
+ "begin_time": datasets.Value("string"),
181
+ "end_time": datasets.Value("string"),
182
  "event_location": datasets.Sequence(datasets.Value("float32")),
183
  "station_location": datasets.Sequence(datasets.Value("float32")),
184
+ },
185
+ )
186
+ elif (self.config.name == "event") or (self.config.name == "event_train") or (self.config.name == "event_test"):
187
+ features = datasets.Features(
 
 
 
 
188
  {
189
+ "data": datasets.Array2D(shape=(3, self.nt), dtype="float32"),
190
+ "phase_pick": datasets.Array2D(shape=(3, self.nt), dtype="float32"),
191
+ "phase_time": datasets.Sequence(datasets.Value("string")),
192
+ "phase_index": datasets.Sequence(datasets.Value("int32")),
193
+ "phase_type": datasets.Sequence(datasets.Value("string")),
194
+ "phase_polarity": datasets.Sequence(datasets.Value("string")),
195
+ "begin_time": datasets.Value("string"),
196
+ "end_time": datasets.Value("string"),
197
+ "event_location": datasets.Sequence(datasets.Value("float32")),
198
+ "station_location": datasets.Sequence(datasets.Value("float32")),
199
+ },
200
  )
201
+
202
  return datasets.DatasetInfo(
203
  # This is the description that will appear on the datasets page.
204
  description=_DESCRIPTION,
 
270
  for file in filepath:
271
  with fsspec.open(file, "rb") as fs:
272
  with h5py.File(fs, "r") as fp:
 
273
  event_ids = list(fp.keys())
274
  for event_id in event_ids:
275
  event = fp[event_id]
276
+ event_attrs = event.attrs
277
+ begin_time = event_attrs["begin_time"]
278
+ end_time = event_attrs["end_time"]
279
+ event_location = [
280
+ event_attrs["longitude"],
281
+ event_attrs["latitude"],
282
+ event_attrs["depth_km"],
283
+ event_attrs["event_time_index"],
284
+ ]
285
  station_ids = list(event.keys())
286
  if (
287
  (self.config.name == "station")
 
289
  or (self.config.name == "station_test")
290
  ):
291
  waveforms = np.zeros([3, self.nt], dtype="float32")
292
+
 
 
 
 
 
 
 
 
293
  for i, sta_id in enumerate(station_ids):
294
+ waveforms[:, : 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"]
298
+ phase_index = attrs["phase_index"]
299
+ phase_polarity = attrs["phase_polarity"]
300
  station_location = [attrs["longitude"], attrs["latitude"], -attrs["elevation_m"] / 1e3]
301
 
302
  yield f"{event_id}/{sta_id}", {
303
+ "data": waveforms,
304
+ "phase_time": phase_time,
305
+ "phase_index": phase_index,
306
+ "phase_type": phase_type,
307
+ "phase_polarity": phase_polarity,
308
+ "begin_time": begin_time,
309
+ "end_time": end_time,
310
+ "event_location": event_location,
311
+ "station_location": station_location,
312
  }
313
 
 
314
  elif (
315
  (self.config.name == "event")
316
  or (self.config.name == "event_train")
317
  or (self.config.name == "event_test")
318
  ):
 
319
 
 
 
 
 
 
 
 
 
 
 
320
  waveforms = np.zeros([len(station_ids), 3, self.nt], dtype="float32")
321
+ phase_type = []
322
+ phase_time = []
323
+ phase_index = []
324
+ phase_polarity = []
325
+ station_location = []
326
 
327
  for i, sta_id in enumerate(station_ids):
328
+ waveforms[i, :, : self.nt] = event[sta_id][:, : self.nt]
 
329
  attrs = event[sta_id].attrs
330
+ phase_type.append(attrs["phase_type"])
331
+ phase_time.append(attrs["phase_time"])
332
+ phase_index.append(attrs["phase_index"])
333
+ phase_polarity.append(attrs["phase_polarity"])
334
+ station_location.append(
335
+ [attrs["longitude"], attrs["latitude"], -attrs["elevation_m"] / 1e3]
 
 
 
 
 
 
 
 
 
 
 
 
 
336
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337
 
338
  yield event_id, {
339
+ "data": waveforms,
340
+ "phase_time": phase_time,
341
+ "phase_index": phase_index,
342
+ "phase_type": phase_type,
343
+ "phase_polarity": phase_polarity,
344
+ "begin_time": begin_time,
345
+ "end_time": end_time,
346
+ "event_location": event_location,
347
+ "station_location": station_location,
348
  }