File size: 5,053 Bytes
c9fe814
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48fd36a
c9fe814
 
 
 
 
 
48fd36a
 
c9fe814
 
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
# 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.
"""MyoQuant-SDH-Data: The MyoQuant SDH Model Data."""


import csv
import json
import os

import datasets


_CITATION = """\
@InProceedings{Meyer,
title = {MyoQuant SDH Data},
author={Corentin Meyer},
year={2022}
}
"""
_NAMES = ["control", "sick"]

_DESCRIPTION = """\
This dataset is used to train the SDH model of MyoQuant to detect and quantify anomaly in the mitochondria repartition in SDH stained muscle fiber with myopathy disorders.
"""

_HOMEPAGE = "https://huggingface.co/datasets/corentinm7/MyoQuant-SDH-Data"

_LICENSE = "agpl-3.0"

_URLS = {
    "SDH_16k": "https://huggingface.co/datasets/corentinm7/MyoQuant-SDH-Data/resolve/main/SDH_16k/SDH_16k.zip"
}
_METADATA_URL = {
    "SDH_16k_metadata": "https://huggingface.co/datasets/corentinm7/MyoQuant-SDH-Data/resolve/main/SDH_16k/metadata.jsonl"
}


class SDH_16k(datasets.GeneratorBasedBuilder):
    """This dataset is used to train the SDH model of MyoQuant to detect and quantify anomaly in the mitochondria repartition in SDH stained muscle fiber with myopathy disorders."""

    VERSION = datasets.Version("1.0.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 = MyBuilderConfig

    # 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')

    DEFAULT_CONFIG_NAME = "SDH_16k"  # It's not mandatory to have a default configuration. Just use one if it make sense.

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "image": datasets.Image(),
                    "label": datasets.ClassLabel(num_classes=2, names=_NAMES),
                }
            ),
            supervised_keys=("image", "label"),
            homepage=_HOMEPAGE,
            citation=_CITATION,
            license=_LICENSE,
            task_templates=[
                datasets.ImageClassification(image_column="image", label_column="label")
            ],
        )

    def _split_generators(self, dl_manager):
        archive_path = dl_manager.download(_URLS)
        split_metadata_path = dl_manager.download(_METADATA_URL)
        files_metadata = {}
        with open(split_metadata_path["SDH_16k_metadata"], encoding="utf-8") as f:
            for lines in f.read().splitlines():
                file_json_metdata = json.loads(lines)
                files_metadata.setdefault(file_json_metdata["split"], []).append(
                    file_json_metdata
                )
        downloaded_files = dl_manager.download_and_extract(archive_path)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "download_path": downloaded_files["SDH_16k"],
                    "metadata": files_metadata["train"],
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "download_path": downloaded_files["SDH_16k"],
                    "metadata": files_metadata["validation"],
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "download_path": downloaded_files["SDH_16k"],
                    "metadata": files_metadata["test"],
                },
            ),
        ]

    def _generate_examples(self, download_path, metadata):
        """Generate images and labels for splits."""
        for i, single_metdata in enumerate(metadata):
            img_path = os.path.join(
                download_path,
                single_metdata["split"],
                single_metdata["label"],
                single_metdata["file_name"],
            )
            yield i, {
                "image": img_path,
                "label": single_metdata["label"],
            }