Datasets:

ArXiv:
File size: 4,580 Bytes
783865a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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.
"""Monster-Monash custom downloader"""


import numpy as np
import os
import datasets


_DATASET = "LenDB"
_SHAPE = (3, 540)
#_DESCRIPTION = ""
#_CITATION = ""
#_HOMEPAGE = ""
#_LICENSE = ""

_URLS = {
    'data': f"{_DATASET}_X.npy",
    'labels': f"{_DATASET}_y.npy",
    'fold_0': "test_indices_fold_0.txt",
    'fold_1': "test_indices_fold_1.txt",
    'fold_2': "test_indices_fold_2.txt",
    'fold_3': "test_indices_fold_3.txt",
    'fold_4': "test_indices_fold_4.txt",
    }


class Monster(datasets.GeneratorBasedBuilder):
    """Generic Monster class for all downloader."""

    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="full", version=VERSION, description="All data"),
        datasets.BuilderConfig(name="fold_0", version=VERSION, description="Cross-validation fold 0"),
        datasets.BuilderConfig(name="fold_1", version=VERSION, description="Cross-validation fold 1"),
        datasets.BuilderConfig(name="fold_2", version=VERSION, description="Cross-validation fold 2"),
        datasets.BuilderConfig(name="fold_3", version=VERSION, description="Cross-validation fold 3"),
        datasets.BuilderConfig(name="fold_4", version=VERSION, description="Cross-validation fold 4"),
    ]

    DEFAULT_CONFIG_NAME = "full"  # By default all data is returned in a single split.

    def _info(self):
        features = datasets.Features(
            {
                "X": datasets.Array2D(_SHAPE, "float32"),
                "y": datasets.Value("int64")
            }
        )
        return datasets.DatasetInfo(
#            description=_DESCRIPTION,
            features=features,
            supervised_keys=("X", "y"),
#            homepage=_HOMEPAGE,
#            license=_LICENSE,
#            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        data = dl_manager.download_and_extract(_URLS['data'])
        labels = dl_manager.download_and_extract(_URLS['labels'])
        if self.config.name == "full":
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={
                        "data": data,
                        "labels": labels,
                        "fold": None,
                        "split": "all",
                    },
                ),
            ]
        else: 
            fold = dl_manager.download_and_extract(_URLS[self.config.name])
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={
                        "data": data,
                        "labels": labels,
                        "fold": fold,
                        "split": "train",
                    },
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    gen_kwargs={
                        "data": data,
                        "labels": labels,
                        "fold": fold,
                        "split": "test"
                    },
                ),
            ]

    def _generate_examples(self, data, labels, fold, split):
        X = np.load(data)
        y = np.load(labels)
        if self.config.name == "full":
            for row in range(y.shape[0]):
                yield(row, {"X": X[row], "y": y[row]})
        else:
            test_indices = np.loadtxt(fold, dtype='int')
            if split == "test":
                for row in test_indices:
                    yield(int(row), {"X": X[row], "y": y[row]})
            elif split == "train":
                train_indices = np.delete(np.arange(y.shape[0]), test_indices)
                for row in train_indices:
                    yield(int(row), {"X": X[row], "y": y[row]})