| 1 | From 7e6bcdfeed54500ca533d2f0eb12078248c43c77 Mon Sep 17 00:00:00 2001
|
| 2 | Message-ID: <7e6bcdfeed54500ca533d2f0eb12078248c43c77.1743682382.git.ngraves@ngraves.fr>
|
| 3 | From: Nicolas Graves <[email protected]>
|
| 4 | Date: Thu, 3 Apr 2025 11:33:58 +0200
|
| 5 | Subject: [PATCH] samples: Use deterministic samples in Guix.
|
| 6 |
|
| 7 | ---
|
| 8 | bed_reader/_sample_data.py | 86 +++++++++-----------------------------
|
| 9 | 1 file changed, 19 insertions(+), 67 deletions(-)
|
| 10 |
|
| 11 | diff --git a/bed_reader/_sample_data.py b/bed_reader/_sample_data.py
|
| 12 | index 6ca4cc0..6a1146e 100644
|
| 13 | |
| 14 | |
| 15 | @@ -1,33 +1,8 @@
|
| 16 | +import os
|
| 17 | import tempfile
|
| 18 | from pathlib import Path, PurePath
|
| 19 | from typing import Union
|
| 20 |
|
| 21 | -try:
|
| 22 | - import pooch
|
| 23 | -
|
| 24 | - """
|
| 25 | - Load sample data.
|
| 26 | - """
|
| 27 | -
|
| 28 | - POOCH = pooch.create(
|
| 29 | - # Use the default cache folder for the OS
|
| 30 | - path=pooch.os_cache("bed_reader"),
|
| 31 | - # The remote data is on Github
|
| 32 | - base_url="https://raw.githubusercontent.com/"
|
| 33 | - + "fastlmm/bed-sample-files/main/",
|
| 34 | - # If this is a development version, get the data from the master branch
|
| 35 | - version_dev="main",
|
| 36 | - # The registry specifies the files that can be fetched
|
| 37 | - env="BED_READER_DATA_DIR",
|
| 38 | - )
|
| 39 | -
|
| 40 | - # Get registry file from package_data
|
| 41 | - registry_file = Path(__file__).parent / "tests/registry.txt"
|
| 42 | - # Load this registry file
|
| 43 | - POOCH.load_registry(registry_file)
|
| 44 | -except ImportError:
|
| 45 | - pooch = None
|
| 46 | -
|
| 47 |
|
| 48 | def sample_file(filepath: Union[str, Path]) -> str:
|
| 49 | """Retrieve a sample .bed file. (Also retrieves associated .fam and .bim files).
|
| 50 | @@ -40,50 +15,41 @@ def sample_file(filepath: Union[str, Path]) -> str:
|
| 51 | Returns
|
| 52 | -------
|
| 53 | str
|
| 54 | - Local name of sample .bed file.
|
| 55 | -
|
| 56 | -
|
| 57 | - .. note::
|
| 58 | - This function requires the :mod:`pooch` package. Install `pooch` with:
|
| 59 | -
|
| 60 | - .. code-block:: bash
|
| 61 | -
|
| 62 | - pip install --upgrade bed-reader[samples]
|
| 63 | -
|
| 64 | -
|
| 65 | - By default this function puts files under the user's cache directory.
|
| 66 | - Override this by setting
|
| 67 | - the `BED_READER_DATA_DIR` environment variable.
|
| 68 | + Local path of sample .bed file.
|
| 69 |
|
| 70 | Example
|
| 71 | --------
|
| 72 |
|
| 73 | .. doctest::
|
| 74 |
|
| 75 | - >>> # pip install bed-reader[samples] # if needed
|
| 76 | >>> from bed_reader import sample_file
|
| 77 | >>>
|
| 78 | >>> file_name = sample_file("small.bed")
|
| 79 | >>> print(f"The local file name is '{file_name}'")
|
| 80 | The local file name is '...small.bed'
|
| 81 | -
|
| 82 | """
|
| 83 | - if pooch is None:
|
| 84 | - raise ImportError(
|
| 85 | - "The function sample_file() requires pooch. "
|
| 86 | - + "Install it with 'pip install --upgrade bed-reader[samples]'.",
|
| 87 | + filepath = Path(filepath)
|
| 88 | + sample_dir = os.environ.get("BED_READER_DATA_DIR")
|
| 89 | + if sample_dir is None:
|
| 90 | + raise EnvironmentError(
|
| 91 | + "BED_READER_DATA_DIR environment variable is not set. "
|
| 92 | + "This should point to the directory containing the sample files."
|
| 93 | )
|
| 94 |
|
| 95 | - filepath = Path(filepath)
|
| 96 | - file_string = str(filepath)
|
| 97 | - if file_string.lower().endswith(".bed"):
|
| 98 | - POOCH.fetch(file_string[:-4] + ".fam")
|
| 99 | - POOCH.fetch(file_string[:-4] + ".bim")
|
| 100 | - return POOCH.fetch(file_string)
|
| 101 | + file_path = Path(sample_dir) / filepath
|
| 102 | +
|
| 103 | + # Check if file exists
|
| 104 | + if not file_path.exists():
|
| 105 | + raise FileNotFoundError(
|
| 106 | + f"Sample file '{filepath}' not found in {sample_dir}. "
|
| 107 | + f"Make sure you're using the latest samples in BED_READER_DATA_DIR."
|
| 108 | + )
|
| 109 | +
|
| 110 | + return str(file_path)
|
| 111 |
|
| 112 |
|
| 113 | def sample_url(filepath: Union[str, Path]) -> str:
|
| 114 | - """Retrieve a URL to a sample .bed file. (Also makes ready associated .fam and .bim files).
|
| 115 | + """Retrieve a URL to a sample .bed file.
|
| 116 |
|
| 117 | Parameters
|
| 118 | ----------
|
| 119 | @@ -95,25 +61,11 @@ def sample_url(filepath: Union[str, Path]) -> str:
|
| 120 | str
|
| 121 | URL to sample .bed file.
|
| 122 |
|
| 123 | -
|
| 124 | - .. note::
|
| 125 | - This function requires the :mod:`pooch` package. Install `pooch` with:
|
| 126 | -
|
| 127 | - .. code-block:: bash
|
| 128 | -
|
| 129 | - pip install --upgrade bed-reader[samples]
|
| 130 | -
|
| 131 | -
|
| 132 | - By default this function puts files under the user's cache directory.
|
| 133 | - Override this by setting
|
| 134 | - the `BED_READER_DATA_DIR` environment variable.
|
| 135 | -
|
| 136 | Example
|
| 137 | --------
|
| 138 |
|
| 139 | .. doctest::
|
| 140 |
|
| 141 | - >>> # pip install bed-reader[samples] # if needed
|
| 142 | >>> from bed_reader import sample_url
|
| 143 | >>>
|
| 144 | >>> url = sample_url("small.bed")
|
| 145 | --
|
| 146 | 2.49.0
|
| 147 |
|