Skip to content

Commit 0f22950

Browse files
authored
Fix TUEV metadata to use cache directory for permissions (#787)
1 parent 04a58fa commit 0f22950

1 file changed

Lines changed: 50 additions & 7 deletions

File tree

pyhealth/datasets/tuev.py

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ def __init__(
8181
raise ValueError("subset must be one of 'train', 'eval', or 'both'")
8282

8383
self.prepare_metadata()
84+
85+
# Determine where the CSVs are located (shared directory or cache)
86+
root_path = Path(root)
87+
cache_dir = Path.home() / ".cache" / "pyhealth" / "tuev"
88+
89+
# Check if CSVs exist in cache and not in shared location
90+
use_cache = False
91+
for table in tables:
92+
shared_csv = root_path / f"tuev-{table}-pyhealth.csv"
93+
cache_csv = cache_dir / f"tuev-{table}-pyhealth.csv"
94+
if not shared_csv.exists() and cache_csv.exists():
95+
use_cache = True
96+
break
97+
98+
# Use cache directory as root if CSVs are there
99+
if use_cache:
100+
logger.info(f"Using cached metadata from {cache_dir}")
101+
root = str(cache_dir)
84102

85103
super().__init__(
86104
root=root,
@@ -107,12 +125,16 @@ def prepare_metadata(self) -> None:
107125
- segment_id = a_ / a_1 / ...
108126
"""
109127
root = Path(self.root)
128+
cache_dir = Path.home() / ".cache" / "pyhealth" / "tuev"
110129

111130
train_rows: list[dict] = []
112131
eval_rows: list[dict] = []
113132

114133
for split in ("train", "eval"):
115-
if os.path.exists(root / f"tuev-{split}-pyhealth.csv"):
134+
# Check if metadata exists in either shared location or cache
135+
shared_csv = root / f"tuev-{split}-pyhealth.csv"
136+
cache_csv = cache_dir / f"tuev-{split}-pyhealth.csv"
137+
if shared_csv.exists() or cache_csv.exists():
116138
continue
117139

118140
split_dir = root / split
@@ -153,7 +175,9 @@ def prepare_metadata(self) -> None:
153175
}
154176
)
155177

156-
root.mkdir(parents=True, exist_ok=True)
178+
# Setup cache directory as fallback for metadata CSVs
179+
cache_dir = Path.home() / ".cache" / "pyhealth" / "tuev"
180+
cache_dir.mkdir(parents=True, exist_ok=True)
157181

158182
# Write train metadata
159183
if train_rows:
@@ -162,9 +186,18 @@ def prepare_metadata(self) -> None:
162186
["patient_id", "record_id"], inplace=True, na_position="last"
163187
)
164188
train_df.reset_index(drop=True, inplace=True)
165-
train_csv = root / "tuev-train-pyhealth.csv"
166-
train_df.to_csv(train_csv, index=False)
167-
189+
190+
# Try shared location first, fall back to cache if no write permission
191+
train_csv_shared = root / "tuev-train-pyhealth.csv"
192+
train_csv_cache = cache_dir / "tuev-train-pyhealth.csv"
193+
194+
try:
195+
train_csv_shared.parent.mkdir(parents=True, exist_ok=True)
196+
train_df.to_csv(train_csv_shared, index=False)
197+
logger.info(f"Wrote train metadata to {train_csv_shared}")
198+
except (PermissionError, OSError):
199+
train_df.to_csv(train_csv_cache, index=False)
200+
logger.info(f"Wrote train metadata to cache: {train_csv_cache}")
168201

169202
# Write eval metadata
170203
if eval_rows:
@@ -175,8 +208,18 @@ def prepare_metadata(self) -> None:
175208
na_position="last",
176209
)
177210
eval_df.reset_index(drop=True, inplace=True)
178-
eval_csv = root / "tuev-eval-pyhealth.csv"
179-
eval_df.to_csv(eval_csv, index=False)
211+
212+
# Try shared location first, fall back to cache if no write permission
213+
eval_csv_shared = root / "tuev-eval-pyhealth.csv"
214+
eval_csv_cache = cache_dir / "tuev-eval-pyhealth.csv"
215+
216+
try:
217+
eval_csv_shared.parent.mkdir(parents=True, exist_ok=True)
218+
eval_df.to_csv(eval_csv_shared, index=False)
219+
logger.info(f"Wrote eval metadata to {eval_csv_shared}")
220+
except (PermissionError, OSError):
221+
eval_df.to_csv(eval_csv_cache, index=False)
222+
logger.info(f"Wrote eval metadata to cache: {eval_csv_cache}")
180223

181224
@property
182225
def default_task(self) -> EEGEventsTUEV:

0 commit comments

Comments
 (0)