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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
"""P and S phase arrivals dataset for Alaska"""
|
| 16 |
+
|
| 17 |
+
import h5py
|
| 18 |
+
import csv
|
| 19 |
+
import os
|
| 20 |
+
|
| 21 |
+
import datasets
|
| 22 |
+
|
| 23 |
+
_PSAlaska_DESCRIPTION = """
|
| 24 |
+
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
_ManualPick_CITATION = """\
|
| 28 |
+
@InProceedings{huggingface:dataset,
|
| 29 |
+
title = {A great new dataset},
|
| 30 |
+
author={huggingface, Inc.
|
| 31 |
+
},
|
| 32 |
+
year={2020}
|
| 33 |
+
}
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
_ManualPick_DESCRIPTION = """\
|
| 37 |
+
This dataset includes P and S phases recorded by the broadband stations in the Alaska Peninsula
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
_PNTFIter1_CITATION = """
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
_PNTFIter1_DESCRIPTION = """
|
| 44 |
+
This dataset includes P and S phases predicted by the PhaseNet-TF using model trained by the manualpick dataset
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
_PNTFIter1Combined_CITATION = """
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
_PNTFIter1Combined_DESCRIPTION = """
|
| 51 |
+
This dataset includes all P and S phases from PNTFiter1 dataset and all false negative arrivals of manualpick dataset
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
_Data_URL = "/mnt/scratch/jieyaqi/alaska/final/PS_Alaska"
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
class PSAlaskaConfig(datasets.BuilderConfig):
|
| 58 |
+
|
| 59 |
+
def __init__(self, description, data_url, citation, **kwargs):
|
| 60 |
+
"""BuilderConfig for PS_Alaska.
|
| 61 |
+
Args:
|
| 62 |
+
features: `list[string]`, list of the features that will appear in the
|
| 63 |
+
feature dict. Should not include "label".
|
| 64 |
+
data_url: `string`, url to download the zip file from.
|
| 65 |
+
citation: `string`, citation for the data set.
|
| 66 |
+
url: `string`, url for information about the data set.
|
| 67 |
+
label_classes: `list[string]`, the list of classes for the label if the
|
| 68 |
+
label is present as a string. Non-string labels will be cast to either
|
| 69 |
+
'False' or 'True'.
|
| 70 |
+
**kwargs: keyword arguments forwarded to super.
|
| 71 |
+
"""
|
| 72 |
+
super(PSAlaskaConfig, self).__init__(
|
| 73 |
+
version = datasets.Version("1.0.0"),
|
| 74 |
+
**kwargs)
|
| 75 |
+
self.description = description
|
| 76 |
+
self.data_url = data_url
|
| 77 |
+
self.citation = citation
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class PSAlaskaDataset(datasets.GeneratorBasedBuilder):
|
| 81 |
+
"""P and S phase arrivals dataset for Alaska"""
|
| 82 |
+
|
| 83 |
+
VERSION = datasets.Version("1.0.0")
|
| 84 |
+
|
| 85 |
+
BUILDER_CONFIGS = [
|
| 86 |
+
PSAlaskaConfig(
|
| 87 |
+
name="ManualPick",
|
| 88 |
+
description=_ManualPick_DESCRIPTION,
|
| 89 |
+
data_url=_Data_URL+"/ManualPick",
|
| 90 |
+
citation=_ManualPick_CITATION,
|
| 91 |
+
),
|
| 92 |
+
PSAlaskaConfig(
|
| 93 |
+
name="PNTFIter1",
|
| 94 |
+
description=_PNTFIter1_DESCRIPTION,
|
| 95 |
+
data_url=_Data_URL+"/PNTFIter1",
|
| 96 |
+
citation=_PNTFIter1_CITATION,
|
| 97 |
+
),
|
| 98 |
+
PSAlaskaConfig(
|
| 99 |
+
name="PNTFIter1_combined",
|
| 100 |
+
description=_PNTFIter1Combined_DESCRIPTION,
|
| 101 |
+
data_url=_Data_URL+"/PNTFIter1_combined",
|
| 102 |
+
citation=_PNTFIter1Combined_CITATION,
|
| 103 |
+
),
|
| 104 |
+
]
|
| 105 |
+
|
| 106 |
+
DEFAULT_CONFIG_NAME = "PNTFIter1_combined"
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def _info(self):
|
| 110 |
+
|
| 111 |
+
return datasets.DatasetInfo(
|
| 112 |
+
description=_PSAlaska_DESCRIPTION + self.config.description,
|
| 113 |
+
features=datasets.Features(
|
| 114 |
+
{
|
| 115 |
+
"begin_time": datasets.Value("string"),
|
| 116 |
+
"end_time": datasets.Value("string"),
|
| 117 |
+
"component": datasets.Sequence(datasets.Value('string')),
|
| 118 |
+
"dt_s": datasets.Value("float"),
|
| 119 |
+
"event_id": datasets.Value("string"),
|
| 120 |
+
"station": datasets.Value("string"),
|
| 121 |
+
"network": datasets.Value("string"),
|
| 122 |
+
"phase_index": datasets.Sequence(datasets.Value('int32')),
|
| 123 |
+
"phase_time": datasets.Sequence(datasets.Value('string')),
|
| 124 |
+
"phase_type": datasets.Sequence(datasets.Value('string')),
|
| 125 |
+
"waveform": datasets.Array2D(shape=(3, 24000), dtype='float32'),
|
| 126 |
+
}
|
| 127 |
+
),
|
| 128 |
+
supervised_keys=("waveform", "phase_type"),
|
| 129 |
+
citation=self.config.citation,
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def _split_generators(self, dl_manager):
|
| 134 |
+
|
| 135 |
+
urls = self.config.data_url
|
| 136 |
+
data_dir = dl_manager.download_and_extract(urls)
|
| 137 |
+
stationf = dl_manager.download_and_extract(_Data_URL, 'stations.csv')
|
| 138 |
+
stationl = []
|
| 139 |
+
eventl = []
|
| 140 |
+
waveform_files = {}
|
| 141 |
+
with open(stationf, newline='') as csvfile:
|
| 142 |
+
r = csv.reader(csvfile, delimiter=',')
|
| 143 |
+
next(r)
|
| 144 |
+
for row in r:
|
| 145 |
+
stationl.append(row[-1])
|
| 146 |
+
|
| 147 |
+
with open(os.path.join(data_dir, 'catalogs.csv'), newline='') as csvfile:
|
| 148 |
+
r = csv.reader(csvfile, delimiter=',')
|
| 149 |
+
next(r)
|
| 150 |
+
for row in r:
|
| 151 |
+
eventl.append(row[3])
|
| 152 |
+
|
| 153 |
+
for e in eventl:
|
| 154 |
+
waveform_files[e] = os.path.join(data_dir, 'waveform', f'{e}.h5')
|
| 155 |
+
|
| 156 |
+
return [
|
| 157 |
+
datasets.SplitGenerator(
|
| 158 |
+
name="full",
|
| 159 |
+
gen_kwargs={
|
| 160 |
+
"stations": stationl,
|
| 161 |
+
"events": eventl,
|
| 162 |
+
"waveform_files": waveform_files
|
| 163 |
+
},
|
| 164 |
+
),
|
| 165 |
+
]
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def _generate_examples(self, stations, events, waveform_files):
|
| 169 |
+
|
| 170 |
+
for e in events:
|
| 171 |
+
f = h5py.File(waveform_files[e], 'r')
|
| 172 |
+
for sta in f[e].keys():
|
| 173 |
+
key = f'{e}_{sta}'
|
| 174 |
+
meta = f[e][sta].attrs
|
| 175 |
+
yield key, {
|
| 176 |
+
"begin_time": meta['begin_time'],
|
| 177 |
+
"end_time": meta['end_time'],
|
| 178 |
+
"component": meta['component'],
|
| 179 |
+
"dt_s": meta['dt_s'],
|
| 180 |
+
"event_id": meta['event_id'],
|
| 181 |
+
"station": meta['station'],
|
| 182 |
+
"network": meta['network'],
|
| 183 |
+
"phase_index": meta['phase_index'],
|
| 184 |
+
"phase_time": meta['phase_time'],
|
| 185 |
+
"phase_type": meta['phase_type'],
|
| 186 |
+
"waveform": f[e][sta]
|
| 187 |
+
}
|
| 188 |
+
f.close()
|