kimmk135 commited on
Commit
c679d7c
·
1 Parent(s): 4fc95cc

Add MIMIC-CXR download script and update README with detailed instructions

Browse files
Files changed (2) hide show
  1. README.md +27 -6
  2. download_mimic.py +109 -0
README.md CHANGED
@@ -40,15 +40,36 @@ VLM-SubtleBench is a benchmark designed to evaluate VLMs on **subtle comparative
40
 
41
  ## Medical Data (MIMIC-CXR)
42
 
43
- The medical domain QA entries (362 attribute comparison pairs from MIMIC-CXR chest X-rays) are included in `qa.json`, but the corresponding images are not included due to [PhysioNet licensing requirements](https://physionet.org/content/mimic-cxr-jpg/2.1.0/).
44
 
45
- To obtain the medical images:
46
 
47
- 1. Obtain credentialed access to [MIMIC-CXR-JPG v2.1.0](https://physionet.org/content/mimic-cxr-jpg/2.1.0/) on PhysioNet
48
- 2. Download the required chest X-ray images
49
- 3. Place them under `images/mimic/` following the path structure referenced in `qa.json` (e.g., `images/mimic/p15/p15592981/s55194630/{hash}.jpg`)
 
50
 
51
- The image paths preserve the original MIMIC-CXR directory hierarchy, so files can be copied directly from a standard MIMIC-CXR-JPG download.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  ## Download and Evaluation
54
 
 
40
 
41
  ## Medical Data (MIMIC-CXR)
42
 
43
+ The medical domain QA entries (362 attribute comparison pairs from MIMIC-CXR chest X-rays, 664 unique images) are included in `qa.json`, but the corresponding images are not included due to [PhysioNet licensing requirements](https://physionet.org/content/mimic-cxr-jpg/2.1.0/).
44
 
45
+ ### Step 1: Obtain PhysioNet Credentialed Access
46
 
47
+ 1. Create an account at [PhysioNet](https://physionet.org/)
48
+ 2. Complete the required [CITI training course](https://physionet.org/about/citi-course/) for "Data or Specimens Only Research"
49
+ 3. Go to [MIMIC-CXR-JPG v2.1.0](https://physionet.org/content/mimic-cxr-jpg/2.1.0/) and sign the data use agreement
50
+ 4. Wait for your access to be approved
51
 
52
+ ### Step 2: Download Images
53
+
54
+ We provide a script that automatically downloads only the 664 images required by `qa.json` and places them at the expected paths (`images/mimic/...`).
55
+
56
+ ```bash
57
+ python download_mimic.py --user <physionet-username> --password <physionet-password>
58
+ ```
59
+
60
+ The script:
61
+ - Parses `qa.json` to find all required MIMIC-CXR image paths
62
+ - Downloads each image from PhysioNet via `wget`
63
+ - Places them under `images/mimic/` preserving the original directory hierarchy (e.g., `images/mimic/p15/p15592981/s55194630/{hash}.jpg`)
64
+ - Skips images that already exist, so it is safe to re-run
65
+
66
+ You can also download individual images manually:
67
+
68
+ ```bash
69
+ wget --user <username> --password <password> \
70
+ https://physionet.org/files/mimic-cxr-jpg/2.1.0/files/p15/p15000170/s54385701/3ea0cd5d-b6ef4a9d-bd053deb-a611067c-284e4144.jpg \
71
+ -O images/mimic/p15/p15000170/s54385701/3ea0cd5d-b6ef4a9d-bd053deb-a611067c-284e4144.jpg
72
+ ```
73
 
74
  ## Download and Evaluation
75
 
download_mimic.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Download MIMIC-CXR images required by qa.json from PhysioNet.
3
+
4
+ Prerequisites:
5
+ 1. Create a PhysioNet account at https://physionet.org/
6
+ 2. Complete the required CITI training course
7
+ 3. Sign the data use agreement for MIMIC-CXR-JPG v2.1.0
8
+ https://physionet.org/content/mimic-cxr-jpg/2.1.0/
9
+
10
+ Usage:
11
+ python download_mimic.py --user <username> --password <password>
12
+ """
13
+
14
+ import argparse
15
+ import json
16
+ import os
17
+ import subprocess
18
+ import sys
19
+
20
+
21
+ BASE_URL = "https://physionet.org/files/mimic-cxr-jpg/2.1.0/files"
22
+
23
+
24
+ def get_required_images(qa_path: str) -> list[str]:
25
+ """Extract unique MIMIC image paths from qa.json."""
26
+ with open(qa_path) as f:
27
+ data = json.load(f)
28
+
29
+ images = set()
30
+ for entry in data:
31
+ for key in ("image_1", "image_2"):
32
+ path = entry.get(key, "")
33
+ if path.startswith("images/mimic/"):
34
+ images.add(path)
35
+ return sorted(images)
36
+
37
+
38
+ def download_images(images: list[str], user: str, password: str, root_dir: str):
39
+ """Download images from PhysioNet and place them at the expected paths."""
40
+ total = len(images)
41
+ print(f"Found {total} MIMIC-CXR images to download.\n")
42
+
43
+ skipped = 0
44
+ downloaded = 0
45
+ failed = 0
46
+
47
+ for i, image_path in enumerate(images, 1):
48
+ dest = os.path.join(root_dir, image_path)
49
+
50
+ if os.path.exists(dest):
51
+ skipped += 1
52
+ continue
53
+
54
+ # images/mimic/p15/p15592981/s55194630/xxx.jpg -> p15/p15592981/s55194630/xxx.jpg
55
+ physionet_path = image_path.removeprefix("images/mimic/")
56
+ url = f"{BASE_URL}/{physionet_path}"
57
+
58
+ os.makedirs(os.path.dirname(dest), exist_ok=True)
59
+
60
+ print(f"[{i}/{total}] Downloading {physionet_path} ...")
61
+ result = subprocess.run(
62
+ [
63
+ "wget", "-q", "-O", dest,
64
+ "--user", user,
65
+ "--password", password,
66
+ url,
67
+ ],
68
+ capture_output=True,
69
+ text=True,
70
+ )
71
+
72
+ if result.returncode != 0:
73
+ failed += 1
74
+ print(f" FAILED: {result.stderr.strip()}")
75
+ # Remove empty file on failure
76
+ if os.path.exists(dest) and os.path.getsize(dest) == 0:
77
+ os.remove(dest)
78
+ else:
79
+ downloaded += 1
80
+
81
+ print(f"\nDone: {downloaded} downloaded, {skipped} skipped (already exist), {failed} failed.")
82
+ if failed > 0:
83
+ print("Re-run the script to retry failed downloads.")
84
+
85
+
86
+ def main():
87
+ parser = argparse.ArgumentParser(description="Download MIMIC-CXR images for VLM-SubtleBench.")
88
+ parser.add_argument("--user", required=True, help="PhysioNet username")
89
+ parser.add_argument("--password", required=True, help="PhysioNet password")
90
+ parser.add_argument("--qa-path", default=None, help="Path to qa.json (default: qa.json in the same directory as this script)")
91
+ args = parser.parse_args()
92
+
93
+ root_dir = os.path.dirname(os.path.abspath(__file__))
94
+ qa_path = args.qa_path or os.path.join(root_dir, "qa.json")
95
+
96
+ if not os.path.exists(qa_path):
97
+ print(f"Error: qa.json not found at {qa_path}", file=sys.stderr)
98
+ sys.exit(1)
99
+
100
+ images = get_required_images(qa_path)
101
+ if not images:
102
+ print("No MIMIC-CXR images found in qa.json.")
103
+ sys.exit(0)
104
+
105
+ download_images(images, args.user, args.password, root_dir)
106
+
107
+
108
+ if __name__ == "__main__":
109
+ main()