Datasets:
File size: 3,023 Bytes
82df7d3 8bb3be3 82df7d3 | 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #!/usr/bin/env python3
"""Shared bootstrap for the standalone loader test scripts.
`MedVision.py` imports `datasets` at module scope, so any test that wants to
reach its module-level helpers has to satisfy that import first. The stub below
is a stdlib-only stand-in used only when the real library is absent — everything
under test is module-level and pure, so nothing stubbed here is ever executed;
the stub only has to let the module import and build its BUILDER_CONFIGS.
This lives in one place because both test scripts need it and the stub must
mirror `MedVision.py`'s `from datasets import (...)` list: with two copies, a new
symbol imported by the loader has to be added to both in lockstep or one suite
dies at import.
Not importable as a package — the test scripts are run as
`python scripts/<name>.py`, which puts `scripts/` on `sys.path[0]`, so a plain
`import _medvision_test_support` works.
"""
import importlib.util
import logging
import os
import sys
import tempfile
import types
_HERE = os.path.dirname(os.path.abspath(__file__))
MEDVISION_PY = os.path.join(_HERE, "..", "MedVision.py")
INFO_CSV = os.path.join(_HERE, "..", "info", "v1.3.0", "ConfigurationsList_All.csv")
def install_datasets_stub():
"""Register a minimal stdlib-only `datasets` in sys.modules."""
m = types.ModuleType("datasets")
class BuilderConfig:
def __init__(self, name=None, version=None, **kw):
self.name = name
self.version = version
for k, v in kw.items():
setattr(self, k, v)
def create_config_id(self, config_kwargs, custom_features=None):
# Return the injected kwargs so tests can read the fingerprint token
# directly instead of parsing a hashed id.
return dict(config_kwargs or {})
class GeneratorBasedBuilder:
pass
def _passthrough(*a, **k):
return a[0] if len(a) == 1 else (a or k)
m.BuilderConfig = BuilderConfig
m.GeneratorBasedBuilder = GeneratorBasedBuilder
m.Split = types.SimpleNamespace(TRAIN="train", TEST="test")
m.SplitGenerator = _passthrough
m.DatasetInfo = _passthrough
m.Features = _passthrough
m.Value = _passthrough
m.Sequence = _passthrough
m.logging = types.SimpleNamespace(get_logger=logging.getLogger)
sys.modules["datasets"] = m
def load_loader(tmp_prefix="medvision_test_"):
"""Import MedVision.py and return the module.
Points MedVision_DATA_DIR at a throwaway directory if unset — the loader
raises at import time without it — and falls back to the stub when the real
`datasets` is not installed.
"""
os.environ.setdefault("MedVision_DATA_DIR", tempfile.mkdtemp(prefix=tmp_prefix))
try:
import datasets # noqa: F401
except ModuleNotFoundError:
install_datasets_stub()
spec = importlib.util.spec_from_file_location("medvision_loader", MEDVISION_PY)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
|