FLUO-SC / fluo_sc.py
Matheus
Fix loader: find first valid label after modality (skip 'data' folder); bump version
cb7237c
# fluo_sc.py
import os
import csv
import glob
import datasets
_CITATION = r"""
@dataset{rocha_fluo_sc_2024,
title = {FLUO-SC: a fluorescence image dataset of skin lesions collected from smartphones},
author = {Rocha, Matheus Becali and Krohling, Renato and Pratavieira, Sebasti{\~a}o and others},
year = {2024},
publisher = {Mendeley Data},
version = {1},
doi = {10.17632/s8n68jj678.1},
url = {https://doi.org/10.17632/s8n68jj678.1}
}
"""
_DESCRIPTION = """
FLUO-SC: Fluorescence skin lesion image dataset (clinical/white-light and fluorescence images).
This Hugging Face repository mirrors the original dataset published on Mendeley Data (CC BY 4.0).
"""
_HOMEPAGE = "https://data.mendeley.com/datasets/s8n68jj678/1"
_LICENSE = "CC BY 4.0"
LABELS = ["BCC", "SCC", "MEL", "ACK", "SEK", "NEV"]
MODALITIES = ["CLI", "FLUO"]
def _repo_root():
# On HF Hub, working dir is repo root.
return os.getcwd()
def _find_data_dir():
data_dir = os.path.join(_repo_root(), "data")
if not os.path.isdir(data_dir):
raise FileNotFoundError("Could not find 'data/' directory at repo root.")
return data_dir
def iter_images(root):
for ext in exts:
yield from glob.glob(os.path.join(root, "**", ext), recursive=True)
class FluoSc(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("1.0.3")
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"image": datasets.Image(),
"label": datasets.ClassLabel(names=LABELS),
"modality": datasets.ClassLabel(names=MODALITIES),
"path": datasets.Value("string"),
}
),
homepage=_HOMEPAGE,
citation=_CITATION,
license=_LICENSE,
)
def _split_generators(self, dl_manager):
data_dir = _find_data_dir()
# If zipped shards exist, extract them (reduces Hub request count massively).
cli_zip = os.path.join(data_dir, "CLI.zip")
fluo_zip = os.path.join(data_dir, "FLUO.zip")
extracted_base = None
extracted_any = False
# dl_manager.extract returns a folder path where the archive is extracted
# For ZIP containing folder "CLI/...", extracted path typically ends with ".../CLI"
# We'll use its parent as base to have ".../<CLI|FLUO>/..."
if os.path.isfile(cli_zip):
cli_extracted = dl_manager.extract(cli_zip)
extracted_base = os.path.dirname(cli_extracted)
extracted_any = True
if os.path.isfile(fluo_zip):
fluo_extracted = dl_manager.extract(fluo_zip)
# If only FLUO.zip exists, base should be parent of FLUO extracted dir
if extracted_base is None:
extracted_base = os.path.dirname(fluo_extracted)
extracted_any = True
# Prefer extracted content if present; otherwise use plain folders under data/
scan_base = extracted_base if extracted_any else data_dir
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"scan_base": scan_base, "repo_root": _repo_root(), "data_dir": data_dir},
)
]
def _generate_examples(self, scan_base, repo_root, data_dir):
import os
import glob
idx = 0
exts = ("*.jpg", "*.jpeg", "*.png", "*.JPG", "*.JPEG", "*.PNG")
def iter_images(root):
for ext in exts:
yield from glob.glob(os.path.join(root, "**", ext), recursive=True)
for img_path in sorted(iter_images(scan_base)):
parts = os.path.normpath(img_path).split(os.sep)
# 1) find the modality at any depth
mod_i = None
modality = None
for i, p in enumerate(parts):
if p in MODALITIES: # ["CLI", "FLUO"]
modality = p
mod_i = i
break
if modality is None:
continue
# 2) find the FIRST valid class after the modality
label = None
for j in range(mod_i + 1, len(parts)):
if parts[j] in LABELS: # ["BCC","SCC","MEL","ACK","SEK","NEV"]
label = parts[j]
break
if label is None:
continue
yield idx, {
"image": img_path,
"label": label,
"modality": modality,
"path": f"{modality}/{label}/{os.path.basename(img_path)}",
}
idx += 1
if idx == 0:
raise FileNotFoundError(
"No valid images found. Expected paths containing CLI/ or FLUO/ and a label folder (BCC/SCC/MEL/ACK/SEK/NEV)."
)