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
23 changes: 20 additions & 3 deletions pyhealth/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,24 @@


class Visit:
"""This class is deprecated and should not be used."""
"""Removed in PyHealth 2.0; read visits with ``Patient.get_events``.

Creating an instance raises at once with the replacement, instead of
returning an object with none of its attributes set.

Example:
>>> from pyhealth.data import Visit
>>> try:
... Visit()
... except NotImplementedError as err:
... print(str(err).split(";")[0])
Visit was removed in PyHealth 2.0
"""

def __init__(self, *args, **kwargs):
import warnings
warnings.warn("The Visit class is deprecated and will be removed in a future version.", DeprecationWarning)
raise NotImplementedError(
"Visit was removed in PyHealth 2.0; use "
"Patient.get_events(event_type=...) to read a patient's events. "
"Event types are the dataset's table names, which differ by "
"dataset."
)
87 changes: 47 additions & 40 deletions pyhealth/datasets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,56 @@
class BaseEHRDataset:
"""This class is deprecated and should not be used."""
def _removed_1x_class(name: str, replacement: str) -> type:
"""Builds a placeholder for a PyHealth 1.x class removed in 2.0.

The name stays importable so old imports load, but creating an instance
(or an instance of a subclass) raises at once with the 2.x replacement,
instead of returning an object with none of its attributes set.

Example:
>>> from pyhealth.datasets import SampleEHRDataset
>>> try:
... SampleEHRDataset()
... except NotImplementedError as err:
... print(str(err).split(";")[0])
SampleEHRDataset was removed in PyHealth 2.0. Use SampleDataset \
(returned by BaseDataset.set_task) instead
"""

def __init__(self, *args, **kwargs):
import warnings

warnings.warn(
"The BaseEHRDataset class is deprecated and will be removed in a future version.",
DeprecationWarning,
)


class BaseSignalDataset:
"""This class is deprecated and should not be used."""

def __init__(self, *args, **kwargs):
import warnings

warnings.warn(
"The BaseSignalDataset class is deprecated and will be removed in a future version.",
DeprecationWarning,
)


class SampleEHRDataset:
"""This class is deprecated and should not be used."""

def __init__(self, *args, **kwargs):
import warnings

warnings.warn(
"The SampleEHRDataset class is deprecated and will be removed in a future version.",
DeprecationWarning,
docs = "https://pyhealth.readthedocs.io/en/latest/api/datasets.html"
cls = type(self).__name__
if cls != name:
# An in-tree or user dataset still built on the 1.x base class
# (e.g. CardiologyDataset, ISRUCDataset, SHHSDataset,
# MIMICExtractDataset). It never worked on 2.x: the stub set no
# attributes and provided no set_task.
raise NotImplementedError(
f"{cls} is built on {name}, which was removed in PyHealth "
f"2.0, so {cls} does not work yet. It needs porting to "
f"{replacement}; see {docs}"
)
raise NotImplementedError(
f"{name} was removed in PyHealth 2.0. Use {replacement} instead; "
f"see {docs}"
)

return type(
name,
(),
{
"__init__": __init__,
"__doc__": f"Removed in PyHealth 2.0; use {replacement}.",
},
)

class SampleSignalDataset:
"""This class is deprecated and should not be used."""

def __init__(self, *args, **kwargs):
import warnings

warnings.warn(
"The SampleSignalDataset class is deprecated and will be removed in a future version.",
DeprecationWarning,
)
BaseEHRDataset = _removed_1x_class("BaseEHRDataset", "BaseDataset")
BaseSignalDataset = _removed_1x_class("BaseSignalDataset", "BaseDataset")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[medium] In-tree dataset subclasses of the removed stubs now raise a misleading error

The PR says nothing in pyhealth/ uses these classes, but this is not true. CardiologyDataset (cardiology.py:11, calls super().init at :50), ISRUCDataset (isruc.py:76, inherits init) and SHHSDataset (shhs.py:8) subclass BaseSignalDataset. MIMICExtractDataset (mimicextract.py:12, super().init at :141) subclasses BaseEHRDataset. All four are exported from pyhealth/datasets/init.py, and ISRUCDataset is constructed in code in pyhealth/calib/calibration/hb.py:248 and dircal.py:213. The stub never set the attributes these classes use (root, dev, kwargs, filepath), so they could not work before either. With this change, constructing them raises 'BaseSignalDataset was removed in PyHealth 2.0. Use BaseDataset ...', which names the wrong cause for a 2.x class. Fix: port these four datasets onto BaseDataset/SampleDataset in this PR, or state in the description that they are known to be unusable, correct the 'nothing uses them' claim, and cover the calib call sites. No test exercises these classes.

SampleEHRDataset = _removed_1x_class(
"SampleEHRDataset", "SampleDataset (returned by BaseDataset.set_task)"
)
SampleSignalDataset = _removed_1x_class(
"SampleSignalDataset", "SampleDataset (returned by BaseDataset.set_task)"
)


from .base_dataset import BaseDataset
Expand Down
66 changes: 66 additions & 0 deletions tests/core/test_removed_1x_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""PyHealth 1.x classes fail at once with a pointer to the 2.x API.

They used to be stubs whose ``__init__`` only warned, so a subclass built
an object with none of its attributes set and failed much later (#1201).
"""

import unittest

from pyhealth.data import Visit
from pyhealth.datasets import (
BaseEHRDataset,
BaseSignalDataset,
SampleEHRDataset,
SampleSignalDataset,
)

REMOVED = {
BaseEHRDataset: "BaseDataset",
BaseSignalDataset: "BaseDataset",
SampleEHRDataset: "SampleDataset",
SampleSignalDataset: "SampleDataset",
Visit: "Patient.get_events",
}


class TestRemoved1xClasses(unittest.TestCase):
def test_instantiating_raises_with_replacement(self):
for cls, replacement in REMOVED.items():
with self.subTest(cls=cls.__name__):
with self.assertRaises(NotImplementedError) as ctx:
cls(root="data", tables=["DIAGNOSES_ICD"])
message = str(ctx.exception)
self.assertIn(f"{cls.__name__} was removed in PyHealth 2.0", message)
self.assertIn(replacement, message)

def test_subclass_raises_too(self):
for cls in REMOVED:
with self.subTest(cls=cls.__name__):
subclass = type("Legacy" + cls.__name__, (cls,), {})
with self.assertRaises(NotImplementedError):
subclass()

def test_in_tree_legacy_datasets_name_themselves(self):
from pyhealth.datasets import (
CardiologyDataset,
ISRUCDataset,
MIMICExtractDataset,
SHHSDataset,
)

for cls in (CardiologyDataset, ISRUCDataset, SHHSDataset):
with self.subTest(cls=cls.__name__):
with self.assertRaises(NotImplementedError) as ctx:
cls(root="data")
message = str(ctx.exception)
self.assertIn(f"{cls.__name__} is built on", message)
self.assertIn("does not work yet", message)
with self.assertRaises(NotImplementedError) as ctx:
MIMICExtractDataset(root="data", tables=["C"])
self.assertIn(
"MIMICExtractDataset is built on BaseEHRDataset", str(ctx.exception)
)


if __name__ == "__main__":
unittest.main()
Loading