Skip to content

setup_benchmark

class BenchmarkSetup
dataclass
#

Bases: ABC

name: str
classvar
#

The name of the benchmark group.

def download(path)
abstractmethod classmethod
#

Download the data from the source.

PARAMETER DESCRIPTION
path

The root path to download to. Will install to path/name

TYPE: Path

Source code in src/mfpbench/setup_benchmark.py
@classmethod
@abstractmethod
def download(cls, path: Path) -> None:
    """Download the data from the source.

    Args:
        path: The root path to download to.
            Will install to
            path/[name][mfpbench.setup_benchmark.BenchmarkSetup.name]
    """
    ...

def default_location()
classmethod
#

Get the default location for the data.

Source code in src/mfpbench/setup_benchmark.py
@classmethod
def default_location(cls) -> Path:
    """Get the default location for the data."""
    return DATAROOT / cls.name

def default_requirements_path()
classmethod
#

Get the default location for the data.

Source code in src/mfpbench/setup_benchmark.py
@classmethod
def default_requirements_path(cls) -> Path:
    """Get the default location for the data."""
    return REQ_DIR / f"{cls.name}.txt"

def install_cmd(requirements_path)
classmethod
#

Get the command to install the requirements.

PARAMETER DESCRIPTION
requirements_path

The path to the requirements.txt file.

TYPE: Path

Source code in src/mfpbench/setup_benchmark.py
@classmethod
def install_cmd(cls, requirements_path: Path) -> str:
    """Get the command to install the requirements.

    Args:
        requirements_path: The path to the requirements.txt file.
    """
    return f"python -m pip install -r {requirements_path.absolute()}"

def install(requirements_path)
classmethod
#

Install the requirements to download the data.

PARAMETER DESCRIPTION
requirements_path

The path to the requirements.txt file.

TYPE: Path

Source code in src/mfpbench/setup_benchmark.py
@classmethod
def install(cls, requirements_path: Path) -> None:
    """Install the requirements to download the data.

    Args:
        requirements_path: The path to the requirements.txt file.
    """
    cmd = cls.install_cmd(requirements_path)
    print(f"Running: {cmd}")
    subprocess.run(cmd, shell=True, check=True)  # noqa: S602

def source(name)
classmethod
#

Get all the sources.

Source code in src/mfpbench/setup_benchmark.py
@classmethod
def source(cls, name: str) -> type[BenchmarkSetup]:
    """Get all the sources."""
    subclasses = cls.__subclasses__()

    found = first_true(subclasses, pred=lambda x: x.name == name, default=None)
    if found is None:
        names = [subclass.name for subclass in subclasses]
        raise ValueError(f"No source with {name=}\nPlease choose from {names}")

    return found

def sources()
classmethod
#

Get all the sources.

Source code in src/mfpbench/setup_benchmark.py
@classmethod
def sources(cls) -> list[type[BenchmarkSetup]]:
    """Get all the sources."""
    return cls.__subclasses__()

def download_status(source, datadir=None) #

Check whether the data is downloaded for some source.

Source code in src/mfpbench/setup_benchmark.py
def download_status(source: str, datadir: Path | None = None) -> bool:
    """Check whether the data is downloaded for some source."""
    datadir = datadir if datadir is not None else DATAROOT
    _source = BenchmarkSetup.source(source)
    source_path = datadir / _source.name
    return source_path.exists() and bool(
        next(source_path.iterdir(), False),  # noqa: FBT003
    )

def print_download_status(sources=None, datadir=None) #

Print the status of the data.

PARAMETER DESCRIPTION
sources

The benchmarks to check the status of. None for all.

TYPE: list[str] | None DEFAULT: None

datadir

Where the root data directory is

TYPE: Path | None DEFAULT: None

Source code in src/mfpbench/setup_benchmark.py
def print_download_status(
    sources: list[str] | None = None,
    datadir: Path | None = None,
) -> None:
    """Print the status of the data.

    Args:
        sources: The benchmarks to check the status of. `None` for all.
        datadir: Where the root data directory is
    """
    datadir = datadir if datadir is not None else DATAROOT
    s = f"root: {datadir.absolute()}"
    print(s)
    print("-" * len(s))

    if (sources is not None and "all" in sources) or sources is None:
        names = [source.name for source in BenchmarkSetup.sources()]
    else:
        names = sources

    for name in names:
        if download_status(name, datadir=datadir):
            print(f"[✓] {name}")
        else:
            print(f"[x] {name: <20} python -m mfpbench download --benchmark {name}")

def print_requirements(benchmarks) #

Print the status of the data.

PARAMETER DESCRIPTION
benchmarks

The benchmarks to check the status of. None for all.

TYPE: list[str]

Source code in src/mfpbench/setup_benchmark.py
def print_requirements(benchmarks: list[str]) -> None:
    """Print the status of the data.

    Args:
        benchmarks: The benchmarks to check the status of. `None` for all.
    """
    sources = BenchmarkSetup.sources()
    if benchmarks is not None and "all" not in benchmarks:
        sources = [source for source in sources if source.name in benchmarks]

    for source in sources:
        print("=" * len(source.name))
        print(f"{source.name}")
        print("=" * len(source.name))

        path = source.default_requirements_path()
        pathstr = f"path: {path}"
        cmd = source.install_cmd(path)
        cmdstr = f"cmd: {cmd}"
        execpath = sys.executable
        execstr = f"exec: {execpath}"
        n = max(len(pathstr), len(cmdstr), len(execstr))

        print(pathstr)
        print(execstr)
        print(cmdstr)
        print("-" * n)
        if not path.exists():
            print("Not found!")
        else:
            print(f"# {path}")
            with path.open("r") as f:
                print(f.read())
        print()

def setup(benchmark, *, datadir=None, download=True, install=False, force=False) #

Download data for a benchmark.

PARAMETER DESCRIPTION
benchmark

The benchmark to download the data for.

TYPE: str

datadir

Where the root data directory is

TYPE: Path | None DEFAULT: None

download

Whether to download the data

TYPE: bool DEFAULT: True

install

Whether to install the requirements for the benchmark. If True, will install the default. If a str, tries to interpret it as a full path.

TYPE: str | bool DEFAULT: False

force

Whether to force redownload of the data

TYPE: bool DEFAULT: False

Source code in src/mfpbench/setup_benchmark.py
def setup(
    benchmark: str,
    *,
    datadir: Path | None = None,
    download: bool = True,
    install: str | bool = False,
    force: bool = False,
) -> None:
    """Download data for a benchmark.

    Args:
        benchmark: The benchmark to download the data for.
        datadir: Where the root data directory is
        download: Whether to download the data
        install: Whether to install the requirements for the benchmark.
            If True, will install the default. If a str, tries to interpret
            it as a full path.
        force: Whether to force redownload of the data
    """
    datadir = datadir if datadir is not None else DATAROOT

    source = BenchmarkSetup.source(benchmark)
    source_path = datadir / source.name

    if download:
        if source_path.exists() and force:
            print(f"Removing {source_path}")
            shutil.rmtree(source_path)

        if not source_path.exists() or next(source_path.iterdir(), None) is None:
            print(f"Downloading to {source_path}")
            source_path.mkdir(exist_ok=True, parents=True)
            source.download(source_path)
            print(f"Finished downloading to {source_path}")
        else:
            print(f"Already found something at {source_path}")
            pass

    if install is not False:
        if install is True:
            req_path = source.default_requirements_path()
        else:
            req_path = Path(install)
            if not req_path.exists():
                raise FileNotFoundError(f"Could not find requirements at {req_path}")

        print(f"Installing requirements at {req_path}")
        source.install(req_path)