MyoQuant-SDH-Data / MyoQuant-SDH-Data.py
corentinm7's picture
Rename SDH_16k.py to MyoQuant-SDH-Data.py
5c83b8e
raw
history blame
5.11 kB
# 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 single_metdata in metadata:
img_path = os.path.join(
download_path,
single_metdata["split"],
single_metdata["label"],
single_metdata["file_name"],
)
yield single_metdata["file_name"], {
"image": {"path": img_path, "bytes": open(img_path, "rb").read()},
"label": single_metdata["label"],
}