Datasets
fyron.datasets prepares practical dataset folders for clinical imaging AI workflows. It includes tiny synthetic demo data helpers and first-class preparation helpers for nnU-Net v2 segmentation, YOLO classification, and YOLO object detection.
Use this module when your cohort table already identifies local image files, masks, classes, boxes, and train/validation/test splits, and you need a reproducible folder structure with a manifest.
Install
Dataset preparation uses Fyron's base imaging stack:
uv add fyronFor a full local research environment:
uv add "fyron[all]"Core Concepts
| Concept | Meaning |
|---|---|
| manifest | DataFrame, CSV, or JSON table describing image paths, labels, classes, boxes, and splits. |
| split | train, val, or test; validation rows follow the target framework's expected folder layout. |
| copy mode | copy, symlink, or explicit move; default is copy. |
| conversion | NIfTI and DICOM can be converted into the target folder shape when needed. |
| output manifest | DataFrame recording source paths, written paths, split, status, and errors. |
API Contract
| Topic | Contract |
|---|---|
| Input shape | DataFrame, CSV, or JSON manifest describing images, labels, classes, boxes, case IDs, and splits. |
| Required columns/files | nnU-Net needs case/image/split and train/val labels; YOLO classification needs image/class/split; YOLO detection needs image/split/class/bounding-box columns. |
| Return shape | Output manifest DataFrame with source paths, written paths, split, status, and errors. |
| Saved artifacts | nnU-Net folders and dataset.json; YOLO image/label folders and data.yaml; dataset manifests. |
| Failure modes | Missing columns/files, invalid splits, unsupported image formats, malformed boxes, shape mismatches, overwrite conflicts, or conversion failures. |
nnU-Net v2 Preparation
Use prepare_nnunetv2_dataset when you have 3D images and segmentation masks that should be trained with nnU-Net v2.
Fyron creates:
Dataset501_LungTumor/
imagesTr/
case_0001_0000.nii.gz
labelsTr/
case_0001.nii.gz
imagesTs/
case_0002_0000.nii.gz
dataset.jsonThe image suffix _0000 is the nnU-Net channel suffix for channel 0. Label files do not include the channel suffix.
import pandas as pd
from fyron.datasets import prepare_nnunetv2_dataset
manifest = pd.DataFrame(
{
"case_id": ["case_0001", "case_0002"],
"split": ["train", "test"],
"image_path": ["images/case_0001_ct.nii.gz", "images/case_0002_ct.nii.gz"],
"label_path": ["masks/case_0001_mask.nii.gz", ""],
}
)
written = prepare_nnunetv2_dataset(
manifest,
output_dir="datasets/nnunet",
dataset_id=501,
dataset_name="LungTumor",
)Parameters
| Parameter | Meaning |
|---|---|
dataset_id | Numeric nnU-Net dataset ID; written as DatasetXXX_Name. |
dataset_name | Dataset name sanitized into the output folder. |
image_col | Manifest column with NIfTI file paths or DICOM series folders. |
label_col | Manifest column with segmentation masks; required for train and val. |
case_id_col | Stable case identifier used in output filenames. |
channel_names | Optional channel mapping for dataset.json, default {0: "CT"}. |
labels | Optional label mapping for dataset.json, default background/foreground. |
Research Decisions
- Fyron does not resample images or masks for nnU-Net. Align them before calling the preparation helper.
- DICOM input is converted to NIfTI with SimpleITK using the first discovered series.
trainandvalrows both go into nnU-Net's training folders. Keep your split table as the source of truth for cross-validation.- Official format reference: nnU-Net documentation.
YOLO Classification Preparation
Use prepare_yolo_classification_dataset when each image or slice has one class label.
Fyron creates:
yolo_cls/
train/
benign/
case_001.png
malignant/
case_002.png
val/
benign/
case_003.pngfrom fyron.datasets import prepare_yolo_classification_dataset
written = prepare_yolo_classification_dataset(
"cohort.csv",
output_dir="datasets/yolo_cls",
image_col="image_path",
class_col="class",
split_col="split",
sample_id_col="patient_id",
)Image Inputs
| Input | Behavior |
|---|---|
.png, .jpg, .jpeg | copied into split/class/. |
| DICOM file or DICOM series folder | converted to axial PNG slice(s). |
| NIfTI volume | converted to axial PNG slice(s). |
For 3D input, nifti_slice="middle" writes one representative axial slice. Use nifti_slice="all" only when every slice should become a training image.
Official format reference: Ultralytics YOLO classification datasets.
YOLO Object Detection Preparation
Use prepare_yolo_detection_dataset when each image can have zero or more bounding boxes.
Fyron creates:
yolo_det/
images/train/
case_001.png
labels/train/
case_001.txt
data.yamlYOLO label rows use normalized xywh:
class_id x_center y_center width heightfrom fyron.datasets import prepare_yolo_detection_dataset
written = prepare_yolo_detection_dataset(
"boxes.csv",
output_dir="datasets/yolo_det",
image_col="image_path",
class_col="class",
bbox_cols=("x_min", "y_min", "x_max", "y_max"),
split_col="split",
sample_id_col="image_id",
)Box Rules
- Input boxes are pixel
xyxyby default:x_min,y_min,x_max,y_max. - Fyron converts boxes to normalized YOLO
xywh. - Multiple rows for the same image become multiple lines in the same label file.
- If a row has an image but empty box values, Fyron writes an empty label file for a negative image.
- Class IDs are zero-indexed and written to
data.yaml.
Official format reference: Ultralytics YOLO detection datasets.
CLI
fyron dataset-nnunetv2 \
--manifest cohort.csv \
--output-dir datasets/nnunet \
--dataset-id 501 \
--dataset-name LungTumor \
--image-col image_path \
--label-col label_path \
--case-id-col case_id \
--split-col splitfyron dataset-yolo-classify \
--manifest cohort.csv \
--output-dir datasets/yolo_cls \
--image-col image_path \
--class-col label \
--split-col splitfyron dataset-yolo-detect \
--manifest boxes.csv \
--output-dir datasets/yolo_det \
--image-col image_path \
--class-col class \
--bbox-cols x_min,y_min,x_max,y_max \
--split-col splitValidation
from fyron.datasets import validate_nnunetv2_dataset, validate_yolo_dataset
nnunet_qc = validate_nnunetv2_dataset("datasets/nnunet/Dataset501_LungTumor")
yolo_qc = validate_yolo_dataset("datasets/yolo_det", task="detect")Validation returns a DataFrame with path, check, status, and message.
Related Modules
- Imaging for DICOM/NIfTI reading and geometry checks.
- DICOMDownloader for acquiring source DICOM or NIfTI from DICOMweb.
- DICOM SEG for source-referenced segmentation export.
- Clinical AI Study Workflow for study-level artifacts and provenance.