File size: 847 Bytes
6b02758
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""Data path resolution for the capstone project."""

from __future__ import annotations

import os
from pathlib import Path


def get_data_root(explicit: str | Path | None = None) -> Path:
    """Return the MedMNIST data root, creating it if it does not exist."""
    if explicit is not None:
        root = Path(explicit)
    elif env_root := os.environ.get("CAPSTONE_DATA_ROOT"):
        root = Path(env_root)
    else:
        root = _find_repo_root() / "data" / "medmnist"

    root.mkdir(parents=True, exist_ok=True)
    return root


def _find_repo_root() -> Path:
    current = Path(__file__).resolve().parent
    for directory in (current, *current.parents):
        if (directory / "pyproject.toml").is_file():
            return directory
    msg = "Could not find repository root (no pyproject.toml found)"
    raise RuntimeError(msg)