Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/api/datasets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,11 @@ PyHealth does not ship any data: you download each dataset from its source and p
- `ISIC <https://challenge.isic-archive.com/data/#2018>`__
- Yes
- —
* - :doc:`PH2 <datasets/pyhealth.datasets.PH2Dataset>`
- Restricted / request (form)
- `PH2 <https://www.fc.up.pt/addi/ph2%20database.html>`__
- No
- —
* - :doc:`SUPPORT2 <datasets/pyhealth.datasets.Support2Dataset>`
- Open
- `UCI <https://archive.ics.uci.edu/dataset/880/support2>`__
Expand Down Expand Up @@ -654,6 +659,7 @@ Available Datasets
datasets/pyhealth.datasets.COVIDBLUESDataset
datasets/pyhealth.datasets.ChestImaGenomeDataset
datasets/pyhealth.datasets.HAM10000Dataset
datasets/pyhealth.datasets.PH2Dataset
datasets/pyhealth.datasets.PhysioNetDeIDDataset
datasets/pyhealth.datasets.EEGBCIDataset
datasets/pyhealth.datasets.AHEPADementiaDataset
Expand Down
98 changes: 98 additions & 0 deletions docs/api/datasets/pyhealth.datasets.PH2Dataset.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
pyhealth.datasets.PH2Dataset
============================

Overview
--------

PH2 dermoscopy dataset: 200 images (80 common nevi, 80 atypical nevi, 40
melanomas) from Hospital Pedro Hispano, Matosinhos, Portugal, each with an
expert binary lesion mask. The masks let
:class:`~pyhealth.tasks.PH2ArtifactClassification` build lesion-only,
background-only and bounding-box views of every image. Each image is its own
PyHealth patient (``IMDxxx``).

Getting the data
----------------

- **Source:** `PH2 database <https://www.fc.up.pt/addi/ph2%20database.html>`_
- **Paper:** Mendonça, T., Ferreira, P. M., Marques, J. S., Marcal, A. R. S.,
Rozeira, J. PH2 - A dermoscopic image database for research and benchmarking.
35th Annual International Conference of the IEEE EMBC, 2013.
https://doi.org/10.1109/EMBC.2013.6610779
- **Access:** released for research and educational use; cite the paper. Get the
archive from the page above (it may ask for a short form) and unpack it.
PyHealth cannot download it for you.
- **Annotations:** the clinical diagnosis (0 common nevus, 1 atypical nevus,
2 melanoma) is read from the ``Clinical Diagnosis`` column of the archive's
``PH2_dataset.txt``, or of ``PH2_dataset.csv`` or ``PH2_dataset.xlsx`` (needs
``openpyxl``).
- **root layout** (folders are searched recursively):

.. code-block:: text
PH2Dataset/ <- root
PH2_dataset.txt (or .xlsx / .csv)
PH2 Dataset images/
IMD002/
IMD002_Dermoscopic_Image/IMD002.bmp
IMD002_lesion/IMD002_lesion.bmp
IMD003/ ...
``root`` is only read. The derived metadata CSV goes to ``metadata_dir``.

Manual test cases
-----------------

These checks need the real PH2 archive, so they are not part of the automated
tests (which use synthetic data). Set ``root`` to the folder that holds
``PH2_dataset.txt`` and ``PH2 Dataset images``. Each case lists its input and
the expected result.

.. code-block:: python
from pathlib import Path
import pandas as pd
from PIL import Image
from pyhealth.datasets import PH2Dataset
root = "/path/to/PH2Dataset"
dataset = PH2Dataset(root=root)
meta = pd.read_csv(dataset.metadata_path)
1. **Load and count.** Input: the full archive.
``len(dataset.unique_patient_ids)`` is ``200`` and
``meta["diagnosis"].value_counts().to_dict()`` is
``{"atypical_nevus": 80, "common_nevus": 80, "melanoma": 40}``.

2. **One image.** Input: patient ``"IMD003"``.

.. code-block:: python
(event,) = dataset.get_patient("IMD003").get_events(event_type="ph2")
print(event.diagnosis, Path(event.image_path).exists(),
Path(event.mask_path).exists())
Expected: ``common_nevus True True``.

3. **Image and mask match.** Input: all 200 rows of ``meta``. Expected: for
every row, ``Image.open(image_path).size == Image.open(mask_path).size``.

4. **Missing annotation file.** Input: ``PH2Dataset(root=<an empty folder>)``.
Expected: ``FileNotFoundError`` saying that no ``PH2_dataset.txt``, ``.csv``
or ``.xlsx`` was found.

5. **Partial copy.** Input: a folder with the full ``PH2_dataset.txt`` but only
the ``IMD002``, ``IMD003`` and ``IMD004`` image folders. Expected: the
dataset loads, ``sorted(dataset.unique_patient_ids)`` is
``["IMD002", "IMD003", "IMD004"]``, and a warning reports that 197 annotated
images were dropped for lacking an image or mask.

API Reference
-------------

.. autoclass:: pyhealth.datasets.PH2Dataset
:members:
:undoc-members:
:show-inheritance:
1 change: 1 addition & 0 deletions docs/api/tasks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ Available Tasks
De-Identification NER <tasks/pyhealth.tasks.DeIDNERTask>
ChestX-ray14 Multilabel Classification <tasks/pyhealth.tasks.ChestXray14MultilabelClassification>
HAM10000 Skin Lesion Classification <tasks/pyhealth.tasks.HAM10000Classification>
PH2 Artifact Sensitivity Classification <tasks/pyhealth.tasks.PH2ArtifactClassification>
MSD Segmentation <tasks/pyhealth.tasks.MSDSegmentationTask>
CHAOS Segmentation Tasks <tasks/pyhealth.tasks.CHAOSSegmentation>
RSNA Pneumonia Classification <tasks/pyhealth.tasks.RSNAPneumoniaClassification>
Expand Down
80 changes: 80 additions & 0 deletions docs/api/tasks/pyhealth.tasks.PH2ArtifactClassification.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
pyhealth.tasks.PH2ArtifactClassification
========================================

Benign vs. malignant classification of PH2 dermoscopy images, with the model
input altered by one of twelve "views" (whole image, lesion only, background
only, lesion-hiding bounding boxes, high- and low-pass filters) and, optionally,
a hand-drawn artifact (dark corner, gel bubble, ink, patches, ruler). It follows
Jin and Ghassemi, `A Study of Artifacts on Melanoma Classification under
Diffusion-Based Perturbations <https://proceedings.mlr.press/v287/jin25b.html>`_
(CHIL 2025). See ``examples/derm/ph2_artifact_classification_resnet50.py``.

.. autoclass:: pyhealth.tasks.PH2ArtifactClassification
:members:
:undoc-members:
:show-inheritance:

Manual test cases
-----------------

These checks need the real PH2 archive, so they are not part of the automated
tests (which use synthetic data). Each case lists its input and the expected
result. Run the code from a script guarded by ``if __name__ == "__main__":``,
because PyHealth starts worker processes.

.. code-block:: python
import numpy as np
from pyhealth.datasets import PH2Dataset
from pyhealth.tasks import PH2ArtifactClassification
from pyhealth.tasks.dermoscopy_artifact_views import make_view
dataset = PH2Dataset(root="/path/to/PH2Dataset")
(event,) = dataset.get_patient("IMD003").get_events(event_type="ph2")
whole = PH2ArtifactClassification("whole", image_size=64)
image, mask = whole._load(event.image_path, event.mask_path) # float, bool
1. **Default samples.** Input: ``dataset.set_task(PH2ArtifactClassification())``.
Expected: 200 samples; each ``sample["image"]`` is a ``float32`` tensor of
shape ``(3, 224, 224)``; 40 samples have ``malignant == 1``.

2. **Lesion view.** Input: ``PH2ArtifactClassification("lesion", image_size=64)``
rendered for ``IMD003``. Expected: every pixel outside the mask equals the
normalised black value ``(-2.118, -2.036, -1.804)``, and every pixel inside
the mask equals the ``whole`` view.

3. **Artifacts spare the lesion.** Input: ``PH2ArtifactClassification("whole",
"ruler", image_size=64)`` rendered for ``IMD003``. Expected: pixels inside
the mask are identical to the ``whole`` view, about 6% of pixels differ, and
the bottom rows (where the ruler is drawn) differ.

4. **Repeatable artifacts.** Input: ``gel_bubble`` rendered twice with
``seed=0``, then once with ``seed=1``. Expected: the two ``seed=0`` outputs
are equal and the ``seed=1`` output differs.

5. **Box coverage.** Input: ``make_view(image, mask, view)`` for ``bbox``,
``bbox70`` and ``bbox90``. Expected: the fraction of black pixels is about
0.17, 0.71 and 0.91; the whole lesion is black in all three.

6. **Bad arguments.** Input: ``view="sepia"``, ``artifact="hair"`` and
``image_size=2``, one at a time. Expected: each raises ``ValueError``.

7. **End to end, no real data.** Input:

.. code-block:: bash
python examples/derm/ph2_artifact_classification_resnet50.py --quick
Expected (about one minute on a CPU): the line ``Lesion check: 5 artifacts
leave the lesion of 5 images unchanged.``, then ``whole original`` with
AUROC about 1.00, and ``whole ruler`` clearly lower (about 0.7) with a
``delta CI95`` whose lower bound is above 0. This shows the test detects
a planted ruler shortcut.

View and artifact helpers
-------------------------

.. automodule:: pyhealth.tasks.dermoscopy_artifact_views
:members:
:undoc-members:
Loading
Loading