From ffe1cd1a2a58f777408ccd8c101a0f27c2dc4ccb Mon Sep 17 00:00:00 2001 From: John Wu Date: Sat, 26 Jul 2025 22:07:34 -0500 Subject: [PATCH 1/2] create mimic3 initialize test case with demo dataset --- tests/core/test_mimic3.py | 95 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 tests/core/test_mimic3.py diff --git a/tests/core/test_mimic3.py b/tests/core/test_mimic3.py new file mode 100644 index 000000000..745f28f8d --- /dev/null +++ b/tests/core/test_mimic3.py @@ -0,0 +1,95 @@ +import unittest +import tempfile +import shutil +import subprocess +import os +from pathlib import Path + +from pyhealth.datasets import MIMIC3Dataset + + +class TestMIMIC3Demo(unittest.TestCase): + """Test MIMIC3 dataset with demo data downloaded from PhysioNet.""" + + # Class-level variables for shared dataset + temp_dir = None + demo_dataset_path = None + dataset = None + + @classmethod + def setUpClass(cls): + """Download demo dataset once for all tests.""" + cls.temp_dir = tempfile.mkdtemp() + cls._download_demo_dataset() + cls._load_dataset() + + @classmethod + def tearDownClass(cls): + """Clean up downloaded dataset after all tests.""" + if cls.temp_dir and os.path.exists(cls.temp_dir): + shutil.rmtree(cls.temp_dir) + + @classmethod + def _download_demo_dataset(cls): + """Download MIMIC-III demo dataset using wget.""" + download_url = "https://physionet.org/files/mimiciii-demo/1.4/" + + # Use wget to download the demo dataset recursively + cmd = [ + "wget", + "-r", + "-N", + "-c", + "-np", + "--directory-prefix", + cls.temp_dir, + download_url, + ] + + try: + subprocess.run(cmd, check=True, capture_output=True, text=True) + except subprocess.CalledProcessError as e: + raise unittest.SkipTest(f"Failed to download MIMIC-III demo dataset: {e}") + except FileNotFoundError: + raise unittest.SkipTest("wget not available - skipping download test") + + # Find the downloaded dataset path + physionet_dir = ( + Path(cls.temp_dir) / "physionet.org" / "files" / "mimiciii-demo" / "1.4" + ) + if physionet_dir.exists(): + cls.demo_dataset_path = str(physionet_dir) + else: + raise unittest.SkipTest("Downloaded dataset not found in expected location") + + @classmethod + def _load_dataset(cls): + """Load the dataset once for all tests.""" + tables = ["diagnoses_icd", "procedures_icd", "prescriptions", "noteevents"] + + cls.dataset = MIMIC3Dataset(root=cls.demo_dataset_path, tables=tables) + + def test_stats(self): + """Test .stats() method execution.""" + try: + self.dataset.stats() + except Exception as e: + self.fail(f"dataset.stats() failed: {e}") + + def test_get_events(self): + """Test get_patient and get_events methods with patient 10006.""" + # Test get_patient method + patient = self.dataset.get_patient("10006") + self.assertIsNotNone(patient, msg="Patient 10006 should exist in demo dataset") + + # Test get_events method + events = patient.get_events() + self.assertIsNotNone(events, msg="get_events() should not return None") + self.assertIsInstance(events, list, msg="get_events() should return a list") + self.assertGreater( + len(events), 0, msg="get_events() should not return an empty list" + ) + + +if __name__ == "__main__": + unittest.main() From 53032633bd2d1642f10ec7b2de1f214a2efb8c11 Mon Sep 17 00:00:00 2001 From: John Wu Date: Sat, 26 Jul 2025 22:19:56 -0500 Subject: [PATCH 2/2] remove extra nonsense from Claude --- tests/core/test_mimic3.py | 46 +++++++++++++++------------------------ 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/tests/core/test_mimic3.py b/tests/core/test_mimic3.py index 745f28f8d..92ad32516 100644 --- a/tests/core/test_mimic3.py +++ b/tests/core/test_mimic3.py @@ -11,26 +11,18 @@ class TestMIMIC3Demo(unittest.TestCase): """Test MIMIC3 dataset with demo data downloaded from PhysioNet.""" - # Class-level variables for shared dataset - temp_dir = None - demo_dataset_path = None - dataset = None - - @classmethod - def setUpClass(cls): - """Download demo dataset once for all tests.""" - cls.temp_dir = tempfile.mkdtemp() - cls._download_demo_dataset() - cls._load_dataset() - - @classmethod - def tearDownClass(cls): - """Clean up downloaded dataset after all tests.""" - if cls.temp_dir and os.path.exists(cls.temp_dir): - shutil.rmtree(cls.temp_dir) - - @classmethod - def _download_demo_dataset(cls): + def setUp(self): + """Download and set up demo dataset for each test.""" + self.temp_dir = tempfile.mkdtemp() + self._download_demo_dataset() + self._load_dataset() + + def tearDown(self): + """Clean up downloaded dataset after each test.""" + if self.temp_dir and os.path.exists(self.temp_dir): + shutil.rmtree(self.temp_dir) + + def _download_demo_dataset(self): """Download MIMIC-III demo dataset using wget.""" download_url = "https://physionet.org/files/mimiciii-demo/1.4/" @@ -42,7 +34,7 @@ def _download_demo_dataset(cls): "-c", "-np", "--directory-prefix", - cls.temp_dir, + self.temp_dir, download_url, ] @@ -55,19 +47,17 @@ def _download_demo_dataset(cls): # Find the downloaded dataset path physionet_dir = ( - Path(cls.temp_dir) / "physionet.org" / "files" / "mimiciii-demo" / "1.4" + Path(self.temp_dir) / "physionet.org" / "files" / "mimiciii-demo" / "1.4" ) if physionet_dir.exists(): - cls.demo_dataset_path = str(physionet_dir) + self.demo_dataset_path = str(physionet_dir) else: raise unittest.SkipTest("Downloaded dataset not found in expected location") - @classmethod - def _load_dataset(cls): - """Load the dataset once for all tests.""" + def _load_dataset(self): + """Load the dataset for testing.""" tables = ["diagnoses_icd", "procedures_icd", "prescriptions", "noteevents"] - - cls.dataset = MIMIC3Dataset(root=cls.demo_dataset_path, tables=tables) + self.dataset = MIMIC3Dataset(root=self.demo_dataset_path, tables=tables) def test_stats(self): """Test .stats() method execution."""