Gaëtan Caillaut
commited on
Commit
•
33b386f
1
Parent(s):
c35c554
Add loading script
Browse files
cora.py
ADDED
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
"""TODO: Add a description here."""
|
16 |
+
|
17 |
+
|
18 |
+
from datasets import features
|
19 |
+
import pandas
|
20 |
+
import os
|
21 |
+
|
22 |
+
import datasets
|
23 |
+
|
24 |
+
|
25 |
+
# TODO: Add BibTeX citation
|
26 |
+
# Find for instance the citation on arxiv or on the dataset repo/website
|
27 |
+
_CITATION = ""
|
28 |
+
|
29 |
+
# TODO: Add description of the dataset here
|
30 |
+
# You can copy an official description
|
31 |
+
_DESCRIPTION = """\
|
32 |
+
The Cora dataset consists of 2708 scientific publications classified into one of seven classes. The citation network consists of 5429 links. Each publication in the dataset is described by a 0/1-valued word vector indicating the absence/presence of the corresponding word from the dictionary. The dictionary consists of 1433 unique words.
|
33 |
+
"""
|
34 |
+
|
35 |
+
# TODO: Add a link to an official homepage for the dataset here
|
36 |
+
_HOMEPAGE = "https://graphsandnetworks.com/the-cora-dataset/"
|
37 |
+
|
38 |
+
# TODO: Add the licence for the dataset here if you can find it
|
39 |
+
_LICENSE = ""
|
40 |
+
|
41 |
+
# TODO: Add link to the official dataset URLs here
|
42 |
+
# The HuggingFace dataset library don't host the datasets but only point to the original files
|
43 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
44 |
+
_URLs = {
|
45 |
+
"nodes": "https://linqs-data.soe.ucsc.edu/public/lbc/cora.tgz",
|
46 |
+
"edges": "https://linqs-data.soe.ucsc.edu/public/lbc/cora.tgz"
|
47 |
+
}
|
48 |
+
|
49 |
+
|
50 |
+
# TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
|
51 |
+
class CoraDataset(datasets.GeneratorBasedBuilder):
|
52 |
+
"""
|
53 |
+
This dataset is the MNIST equivalent in graph learning and we explore it somewhat explicitly here in function of other articles using again and again this dataset as a testbed."""
|
54 |
+
|
55 |
+
VERSION = datasets.Version("1.0.0")
|
56 |
+
|
57 |
+
# This is an example of a dataset with multiple configurations.
|
58 |
+
# If you don't want/need to define several sub-sets in your dataset,
|
59 |
+
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
|
60 |
+
|
61 |
+
# If you need to make complex sub-parts in the datasets with configurable options
|
62 |
+
# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
|
63 |
+
# BUILDER_CONFIG_CLASS = MyBuilderConfig
|
64 |
+
|
65 |
+
# You will be able to load one or the other configurations in the following list with
|
66 |
+
# data = datasets.load_dataset('my_dataset', 'first_domain')
|
67 |
+
# data = datasets.load_dataset('my_dataset', 'second_domain')
|
68 |
+
BUILDER_CONFIGS = [
|
69 |
+
datasets.BuilderConfig(name="nodes", version=VERSION,
|
70 |
+
description="The Cora dataset"),
|
71 |
+
datasets.BuilderConfig(name="edges", version=VERSION,
|
72 |
+
description="The Cora network")
|
73 |
+
]
|
74 |
+
|
75 |
+
# It's not mandatory to have a default configuration. Just use one if it make sense.
|
76 |
+
DEFAULT_CONFIG_NAME = "nodes"
|
77 |
+
|
78 |
+
def _info(self):
|
79 |
+
# TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
|
80 |
+
# This is the name of the configuration selected in BUILDER_CONFIGS above
|
81 |
+
if self.config.name == "nodes":
|
82 |
+
features_dict = {
|
83 |
+
f"word{i}": datasets.Value("bool")
|
84 |
+
for i in range(1433)
|
85 |
+
}
|
86 |
+
features_dict["node"] = datasets.Value("string")
|
87 |
+
features_dict["label"] = datasets.Value("string")
|
88 |
+
features_dict["neighbors"] = datasets.Sequence(
|
89 |
+
datasets.Value("string")
|
90 |
+
)
|
91 |
+
features = datasets.Features(features_dict)
|
92 |
+
elif self.config.name == "edges": # This is an example to show how to have different features for "first_domain" and "second_domain"
|
93 |
+
features = datasets.Features(
|
94 |
+
{
|
95 |
+
"source": datasets.Value("string"),
|
96 |
+
"target": datasets.Value("string")
|
97 |
+
}
|
98 |
+
)
|
99 |
+
return datasets.DatasetInfo(
|
100 |
+
# This is the description that will appear on the datasets page.
|
101 |
+
description=_DESCRIPTION,
|
102 |
+
# This defines the different columns of the dataset and their types
|
103 |
+
# Here we define them above because they are different between the two configurations
|
104 |
+
features=features,
|
105 |
+
# If there's a common (input, target) tuple from the features,
|
106 |
+
# specify them here. They'll be used if as_supervised=True in
|
107 |
+
# builder.as_dataset.
|
108 |
+
supervised_keys=None,
|
109 |
+
# Homepage of the dataset for documentation
|
110 |
+
homepage=_HOMEPAGE,
|
111 |
+
# License for the dataset if available
|
112 |
+
license=_LICENSE,
|
113 |
+
# Citation for the dataset
|
114 |
+
citation=_CITATION,
|
115 |
+
)
|
116 |
+
|
117 |
+
def _split_generators(self, dl_manager):
|
118 |
+
"""Returns SplitGenerators."""
|
119 |
+
# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
|
120 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
121 |
+
|
122 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
|
123 |
+
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
|
124 |
+
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
125 |
+
my_urls = _URLs[self.config.name]
|
126 |
+
data_dir = dl_manager.download_and_extract(my_urls)
|
127 |
+
return [
|
128 |
+
datasets.SplitGenerator(
|
129 |
+
name=datasets.Split.TRAIN,
|
130 |
+
# These kwargs will be passed to _generate_examples
|
131 |
+
gen_kwargs={
|
132 |
+
"edges_path": os.path.join(data_dir, "cora", "cora.cites"),
|
133 |
+
"nodes_path": os.path.join(data_dir, "cora", "cora.content"),
|
134 |
+
"split": "train"
|
135 |
+
}
|
136 |
+
)
|
137 |
+
]
|
138 |
+
|
139 |
+
def _generate_examples(
|
140 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
141 |
+
self, edges_path, nodes_path, split
|
142 |
+
):
|
143 |
+
""" Yields examples as (key, example) tuples. """
|
144 |
+
# This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
|
145 |
+
# The `key` is here for legacy reason (tfds) and is not important in itself.
|
146 |
+
|
147 |
+
if self.config.name == "nodes":
|
148 |
+
neighbors = {}
|
149 |
+
with open(edges_path, "rt", encoding="UTF-8") as f:
|
150 |
+
for line in f:
|
151 |
+
target, src = line.strip().split()
|
152 |
+
for n in (target, src):
|
153 |
+
if n not in neighbors:
|
154 |
+
neighbors[n] = []
|
155 |
+
neighbors[src].append(target)
|
156 |
+
|
157 |
+
colnames = ["node"] + [f"word{i}" for i in range(1433)] + ["label"]
|
158 |
+
dtypes = [str] + [bool] * 1433 + [str]
|
159 |
+
nodes = pandas.read_csv(
|
160 |
+
nodes_path,
|
161 |
+
sep="\t",
|
162 |
+
header=None,
|
163 |
+
names=colnames,
|
164 |
+
dtype=dict(zip(colnames, dtypes))
|
165 |
+
)
|
166 |
+
col2idx = {col: i for i, col in enumerate(list(nodes))}
|
167 |
+
for id, row in enumerate(nodes.itertuples(index=False, name=None)):
|
168 |
+
n = row[col2idx["node"]]
|
169 |
+
features = {
|
170 |
+
"node": n,
|
171 |
+
"label": row[col2idx["label"]],
|
172 |
+
"neighbors": neighbors[n]
|
173 |
+
}
|
174 |
+
for i in range(1433):
|
175 |
+
feature_name = f"word{i}"
|
176 |
+
features[feature_name] = row[col2idx[feature_name]]
|
177 |
+
yield id, features
|
178 |
+
|
179 |
+
elif self.config.name == "edges":
|
180 |
+
with open(edges_path, "rt", encoding="UTF-8") as f:
|
181 |
+
for id, line in enumerate(f):
|
182 |
+
target, src = line.strip().split()
|
183 |
+
yield id, {"source": src, "target": target}
|