katielink commited on
Commit
c27e08d
·
1 Parent(s): a69396d

Add dataset loading script

Browse files
Files changed (1) hide show
  1. synapse_test_dataset.py +120 -0
synapse_test_dataset.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # TODO: Address all TODOs and remove all explanatory comments
15
+ """TODO: Add a description here."""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+
22
+ import datasets
23
+ import synapseclient
24
+
25
+
26
+ _DESCRIPTION = """\
27
+ This is a test Synapse dataset.
28
+ """
29
+
30
+ # TODO: Add a link to an official homepage for the dataset here
31
+ _HOMEPAGE = ""
32
+
33
+ # TODO: Add the licence for the dataset here if you can find it
34
+ _LICENSE = ""
35
+
36
+ # Synapse ID for dataset
37
+ _SYN_ID = ""
38
+
39
+
40
+ # TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
41
+ class SynapseTestDataset(datasets.GeneratorBasedBuilder):
42
+ """TODO: Short description of my dataset."""
43
+
44
+ VERSION = datasets.Version("1.1.0")
45
+
46
+ # Custom download function for Synapse
47
+ def _download_from_synapse(syn_id, path):
48
+ syn = synapseclient.Synapse()
49
+ syn.login()
50
+ syn.get(entity=syn_id, downloadLocation=path)
51
+
52
+ def _info(self):
53
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
54
+ features = datasets.Features(
55
+ {
56
+ "feature_a": datasets.Value("string"),
57
+ "feature_b": datasets.Value("string"),
58
+ "feature_c": datasets.Value("string")
59
+ # These are the features of your dataset like images, labels ...
60
+ }
61
+ )
62
+ return datasets.DatasetInfo(
63
+ # This is the description that will appear on the datasets page.
64
+ description=_DESCRIPTION,
65
+ # This defines the different columns of the dataset and their types
66
+ features=features, # Here we define them above because they are different between the two configurations
67
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
68
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
69
+ # supervised_keys=("sentence", "label"),
70
+ # Homepage of the dataset for documentation
71
+ homepage=_HOMEPAGE,
72
+ # License for the dataset if available
73
+ license=_LICENSE,
74
+ )
75
+
76
+ def _split_generators(self, dl_manager):
77
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
78
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
79
+
80
+ # This is where we can call the custom download function for Synapse
81
+ data_dir = dl_manager.download_custom(_SYN_ID, self._download_from_synapse)
82
+ return [
83
+ datasets.SplitGenerator(
84
+ name=datasets.Split.TRAIN,
85
+ # These kwargs will be passed to _generate_examples
86
+ gen_kwargs={
87
+ "filepath": os.path.join(data_dir, "train.jsonl"),
88
+ "split": "train",
89
+ },
90
+ ),
91
+ datasets.SplitGenerator(
92
+ name=datasets.Split.VALIDATION,
93
+ # These kwargs will be passed to _generate_examples
94
+ gen_kwargs={
95
+ "filepath": os.path.join(data_dir, "dev.jsonl"),
96
+ "split": "dev",
97
+ },
98
+ ),
99
+ datasets.SplitGenerator(
100
+ name=datasets.Split.TEST,
101
+ # These kwargs will be passed to _generate_examples
102
+ gen_kwargs={
103
+ "filepath": os.path.join(data_dir, "test.jsonl"),
104
+ "split": "test"
105
+ },
106
+ ),
107
+ ]
108
+
109
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
110
+ def _generate_examples(self, filepath, split):
111
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
112
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
113
+ with open(filepath, encoding="utf-8") as f:
114
+ for key, row in enumerate(f):
115
+ data = json.loads(row)
116
+ yield key, {
117
+ "feature_a": data["feature_a"],
118
+ "feature_b": data["feature_b"],
119
+ "feature_c": "" if split == "test" else data["answer"],
120
+ }