File size: 7,105 Bytes
0425370 |
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 |
# 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.
"""Collection of datasets for the MJP."""
import pathlib
from collections import defaultdict
from dataclasses import dataclass
from typing import Optional
import datasets
import torch
from fim.data.utils import load_file
from fim.typing import Path, Paths
# TODO: Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """\
@InProceedings{huggingface:dataset,
title = {A great new dataset},
author={huggingface, Inc.
},
year={2020}
}
"""
# TODO: Add description of the dataset here
# You can copy an official description
_DESCRIPTION = """\
This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
"""
# TODO: Add a link to an official homepage for the dataset here
_HOMEPAGE = ""
# TODO: Add the licence for the dataset here if you can find it
_LICENSE = ""
# TODO: Add link to the official dataset URLs here
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
# _URLS = {
# "first_domain": "https://huggingface.co/great-new-dataset-first_domain.zip",
# "second_domain": "https://huggingface.co/great-new-dataset-second_domain.zip",
# }
_ROOT_URL = "data/DFR"
@dataclass
class MJPDatasetsBuilderConfig(datasets.BuilderConfig):
"""MJPDatasets builder config.."""
file_name: Optional[str] = None
# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
class MJP(datasets.GeneratorBasedBuilder):
"""TODO: Short description of my dataset."""
VERSION = datasets.Version("1.1.0")
# This is an example of a dataset with multiple configurations.
# If you don't want/need to define several sub-sets in your dataset,
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
# If you need to make complex sub-parts in the datasets with configurable options
# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
BUILDER_CONFIG_CLASS = MJPDatasetsBuilderConfig
# You will be able to load one or the other configurations in the following list with
# data = datasets.load_dataset('my_dataset', 'first_domain')
# data = datasets.load_dataset('my_dataset', 'second_domain')
BUILDER_CONFIGS = [
MJPDatasetsBuilderConfig(
name="DFR_V=0",
file_name="6_st_DFR_V=0.zip",
version=VERSION,
description="This part of my dataset covers a first domain",
),
MJPDatasetsBuilderConfig(
name="DFR_V=1",
file_name="6_st_DFR_V=1.zip",
version=VERSION,
description="This part of my dataset covers a first domain",
),
MJPDatasetsBuilderConfig(
name="DFR_V=2",
file_name="6_st_DFR_V=2.zip",
version=VERSION,
description="This part of my dataset covers a first domain",
),
MJPDatasetsBuilderConfig(
name="DFR_V=3",
file_name="6_st_DFR_V=3.zip",
version=VERSION,
description="This part of my dataset covers a first domain",
),
]
DEFAULT_CONFIG_NAME = "DFR_V=0"
files_to_load = {
"observation_times": "fine_grid_grid.pt",
"observation_values": "fine_grid_noisy_sample_paths.pt",
"time_normalization_factors": "fine_grid_time_normalization_factors.pt",
"sequence_lengths": "fine_grid_mask_seq_lengths.pt",
"ground_truth_intensity_matrices": "fine_grid_intensity_matrices.pt",
"adjacency_matrices": "fine_grid_adjacency_matrices.pt",
"ground_truth_initial_distributions": "fine_grid_initial_distributions.pt",
}
def _info(self):
features = datasets.Features(
{
"observation_times": datasets.Sequence(datasets.Sequence(datasets.Sequence(datasets.Value("float32")))),
"observation_values": datasets.Sequence(datasets.Sequence(datasets.Sequence(datasets.Value("uint32")))),
"time_normalization_factors": datasets.Value("float32"),
"sequence_lengths": datasets.Sequence(datasets.Sequence(datasets.Value("int32"))),
"ground_truth_intensity_matrices": datasets.Sequence(datasets.Sequence(datasets.Value("float32"))),
"adjacency_matrices": datasets.Sequence(datasets.Sequence(datasets.Value("float32"))),
"ground_truth_initial_distributions": datasets.Sequence(datasets.Value("uint64")),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
# 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.
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
urls = f"{_ROOT_URL}/{self.config.file_name}"
data_dir = dl_manager.download_and_extract(urls)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={"datadir": pathlib.Path(data_dir) / self.config.file_name.split(".")[0]},
)
]
def __get_files(self, path: Path) -> Paths:
files_to_load = [(key, pathlib.Path(path) / file_name) for key, file_name in self.files_to_load.items()]
return files_to_load
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
def _generate_examples(self, datadir):
data = defaultdict(list)
files_to_load = self.__get_files(datadir)
for key, file_path in files_to_load:
data[key].append(load_file(file_path))
for k, v in data.items():
data[k] = torch.cat(v)
print(k, data[k].shape)
for id in range(len(data["observation_times"])):
yield id, {k: v[id].tolist() for k, v in data.items() if k in self.info.features}
|