-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.py
284 lines (213 loc) · 10.3 KB
/
template.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# coding=utf-8
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This is a template on how to implement a dataset in the biomedical repo.
A thorough walkthrough on how to implement a dataset can be found here:
https://huggingface.co/docs/datasets/add_dataset.html
This script corresponds to Step 4 in the Biomedical Hackathon guide.
To start, copy this template file and save it as <your_dataset_name>.py in an appropriately named folder within datasets. Then, modify this file as necessary to implement your own method of extracting, and generating examples for your dataset.
There are 3 key elements to implementing a dataset:
(1) `_info`: Create a skeletal structure that describes what is in the dataset and the nature of the features.
(2) `_split_generators`: Download and extract data for each split of the data (ex: train/dev/test)
(3) `_generate_examples`: From downloaded + extracted data, process files for the data in a feature format specified in "info".
----------------------
Step 1: Declare imports
Your imports go here; the only mandatory one is `datasets`, as methods and attributes from this library will be used throughout the script.
We have provided some import statements that we strongly recommend. Feel free to adapt; so long as the style-guide requirements are satisfied (Step 5), then you should be able to push your code.
"""
import datasets
import os # useful for paths
from typing import Iterable, Dict, List
import logging
"""
Step 2: Create keyword descriptors for your dataset
The following variables are used to populate the dataset entry. Common ones include:
- `_DATASETNAME` = "your_dataset_name"
- `_CITATION`: Latex-style citation of the dataset
- `_DESCRIPTION`: Explanation of the dataset
- `_HOMEPAGE`: Where to find the dataset's hosted location
- `_LICENSE`: License to use the dataset
- `_URLs`: How to download the dataset(s), by name; make this in the form of a dictionary where <dataset_name> is the key and <url_of_dataset> is the value
- `_VERSION`: Version of the dataset
"""
_DATASETNAME = "your_dataset_name"
_CITATION = """\
@article{,
author = {},
title = {},
journal = {},
volume = {},
year = {},
url = {},
doi = {},
biburl = {},
bibsource = {}
}
"""
_DESCRIPTION = """\
A description of your dataset
"""
_HOMEPAGE = "Homepage of the dataset"
_LICENSE = "License"
_URLs = {'your_dataset_name': "your_dataset_URL"}
_VERSION = "1.0.0"
"""
Step 3: Change the class name to correspond to your <Your_Dataset_Name>
ex: "ChemProtDataset".
Then, fill all relevant information to `BuilderConfig` which populates information about the class. You may have multiple builder configs (ex: a large dataset separated into multiple partitions) if you populate for different dataset names + descriptions. The following is setup for just 1 dataset, but can be adjusted.
NOTE - train/test/dev splits can be handled in `_split_generators`.
"""
class YourDatasetName(datasets.GeneratorBasedBuilder):
"""Write a short docstring documenting what this dataset is"""
VERSION = datasets.Version(_VERSION)
BUILDER_CONFIGS = [
datasets.BuilderConfig(
name=_DATASETNAME,
version=VERSION,
description=_DESCRIPTION,
),
]
DEFAULT_CONFIG_NAME = _DATASETNAME
"""
Step 4: Populate "information" about the dataset that creates a skeletal structure for an example within the dataset looks like.
The following data structures are useful:
datasets.Features - An instance that defines all descriptors within a feature in an arbitrary nested manner; the "feature" class must strictly adhere to this format.
datasets.Value - the type of the data structure (ex: useful for text, PMIDs)
datasets.Sequence - for information that must be in a continuous sequence (ex: spans in the text, offsets)
An example is as follows for an ENTITY + RELATION dataset.
Your format may differ depending on what the dataset is. Please try to keep the extraction as close to the original dataset as possible. If you're having trouble adapting your dataset, please contact the community channels and an organizer will reach out!
"""
def _info(self):
if self.config.name == _DATASETNAME:
features = datasets.Features(
{
"article_id": datasets.Value("int32"),
"text": datasets.Value("string"),
"entities": datasets.Sequence(
{
"spans": datasets.Sequence(
datasets.Value("int32")
),
"text": datasets.Value("string"),
"entity_type": datasets.Value("string"),
}
),
"relations": datasets.Sequence(
{
"relation_type": datasets.Value("string"),
"arg1": datasets.Value("string"),
"arg2": datasets.Value("string"),
}
),
}
)
return datasets.DatasetInfo(
# This is the description that will appear on the datasets page.
description=_DESCRIPTION,
features=features,
# If there's a common (input, target) tuple from the features,
# specify them here. They'll be used if as_supervised=True in
# builder.as_dataset.
supervised_keys=None,
# Homepage of the dataset for documentation
homepage=_HOMEPAGE,
# License for the dataset if available
license=_LICENSE,
# Citation for the dataset
citation=_CITATION,
)
def _split_generators(
self, dl_manager: datasets.DownloadManager
) -> List[datasets.SplitGenerator]:
"""
Step 5: Download and extract the dataset.
For each config name, run `download_and_extract` from the dl_manager; this will download and unzip any files within a cache directory, specified by `data_dir`.
`download_and_extract` can accept an iterable object and return the same structure with the url replaced with the path to local files:
ex:
output = dl_manager.download_and_extract({"data1:" "url1", "data2": "url2"})
output
>> {"data1": "path1", "data2": "path2"}
Nested zip files can be cached also, but make sure to save their path.
Fill the arguments of "SplitGenerator" with `name` and `gen_kwargs`.
Note:
- `name` can be: datasets.Split.<TRAIN/TEST/VALIDATION> or a string
- all keys in `gen_kwargs` can be passed to `_generate_examples()`. If your dataset has multiple files, you can make a separate key for each file, as shown below:
"""
my_urls = _URLs[self.config.name]
data_dir = dl_manager.download_and_extract(my_urls)
return [
datasets.SplitGenerator(
name="DatasetSplit",
gen_kwargs={
"filepath": data_dir,
"abstract_file": os.path.join(data_dir, "abstract.txt"),
"entity_file": os.path.join(data_dir, "entities.txt"),
"relation_file": os.path.join(data_dir, "relations.txt"),
"split": "Name_of_Split",
},
),
]
def _generate_examples(
self, filepath, abstract_file, entity_file, relation_file, split
):
"""
Step 6: Create a generator that yields (key, example) of the dataset of interest.
The arguments to this function come from `gen_kwargs` returned in `_split_generators()`
The goal of this function is to perform operations on any of the keys of `gen_kwargs` that allow you to extract and process the data.
The following skeleton does the following:
- "extracts" abstracts
- "extracts" entities, assuming the output is of the form specified in `_info`
- "extracts" relations, assuming similarly the output in the form specified in `_info`.
An assumption in this pseudo code is that the abstract, entity, and relation file all have linking keys.
"""
if self.config.name == _DATASETNAME:
abstracts = self._get_abstract(abstract_file)
entities, entity_id = self._get_entities(entity_file)
relations = self._get_relations(relation_file)
for id_, key in enumerate(abstracts.keys()):
yield id_, {
"article_id": pmid,
"text": abstracts[key],
"entities": entities[key],
"relations": relations[key],
}
@staticmethod
def _get_abstract(abstract_file: str) -> Dict[int, str]:
"""
Create a function that can:
- Read the abstract file.
- Return {key: abstract_text} output.
"""
pass
@staticmethod
def _get_entity(entity_file: str) -> Dict[int, Iterable]:
"""
Create a function that can:
- Read the entity file
- Return a {key: entity_list}
Where the entity_list is an iterable where each element has the following form:
{"spans": [start, end], "text": entity_reference, "entity_type": entity_id}
"""
pass
@staticmethod
def _get_relation(relation_file: str) -> Dict[int, Iterable]:
"""
Create a function that can:
- Read the relation file
- Return a {key: relation_list}
Where the relation_list is an iterable where each element has the following form:
{"relation_type": relation_id, "arg1": relation1, "arg2": relation2}
"""
pass