cache
lacuna.utils.cache
¶
Unified cache and temp directory management for Lacuna.
Provides consistent cache and temp locations that can be configured via: 1. CLI config file (cache_dir, tmp_dir in YAML) 2. Environment variables (LACUNA_CACHE_DIR, LACUNA_TMP_DIR) 3. Platform-specific defaults
This is particularly important for HPC environments where /tmp may not be writable or has limited space.
Configuration Priority (highest to lowest)
- CLI config file (cache_dir/tmp_dir in YAML)
- Environment variable (LACUNA_CACHE_DIR/LACUNA_TMP_DIR)
- Platform-specific default (~/.cache/lacuna)
Environment Variables
LACUNA_CACHE_DIR : str Base directory for all Lacuna cache files (downloads, transforms, etc.) Default: ~/.cache/lacuna (Linux/macOS), %LOCALAPPDATA%/lacuna/cache (Windows)
LACUNA_TMP_DIR : str Base directory for temporary files created during analysis. Default: Falls back to cache_dir/tmp if not set. This is useful for HPC systems where /tmp is not writable.
configure_cache(cache_dir=None, tmp_dir=None)
¶
Configure cache and temp directories from CLI config.
Call this at CLI startup to set cache locations from the config file. These settings take precedence over environment variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Path
|
Base cache directory. If None, uses env var or platform default. |
None
|
tmp_dir
|
Path
|
Temp directory. If None, uses cache_dir/tmp. |
None
|
Examples:
>>> from lacuna.utils.cache import configure_cache
>>> configure_cache(cache_dir=Path("/data/lacuna_cache"))
Source code in src/lacuna/utils/cache.py
get_cache_dir()
¶
Get the Lacuna cache directory.
The cache directory is determined by (in order of priority): 1. CLI config file (set via configure_cache()) 2. LACUNA_CACHE_DIR environment variable 3. Platform-specific default: - $XDG_CACHE_HOME/lacuna on Linux/macOS (typically ~/.cache/lacuna) - %LOCALAPPDATA%/lacuna/cache on Windows
Returns:
| Type | Description |
|---|---|
Path
|
Path to cache directory (created if doesn't exist) |
Examples:
Configure custom cache location:
>>> import os
>>> os.environ['LACUNA_CACHE_DIR'] = '/path/to/my/cache'
>>> from lacuna.utils.cache import get_cache_dir
>>> cache_dir = get_cache_dir()
Source code in src/lacuna/utils/cache.py
get_tdi_cache_dir()
¶
Get the TDI cache subdirectory.
Returns:
| Type | Description |
|---|---|
Path
|
Path to TDI cache directory (created if doesn't exist) |
Source code in src/lacuna/utils/cache.py
get_temp_base_dir()
¶
Get the base temporary directory for Lacuna.
The temp directory can be configured via the LACUNA_TEMP_DIR environment variable. If not set, falls back to LACUNA_CACHE_DIR/tmp. This is important for HPC environments where /tmp may not be writable or has limited space.
Returns:
| Type | Description |
|---|---|
Path
|
Path to temp base directory (created if doesn't exist) |
Examples:
Configure custom temp location for HPC:
>>> import os
>>> os.environ['LACUNA_TMP_DIR'] = '/scratch/user/tmp'
>>> from lacuna.utils.cache import get_temp_base_dir
>>> temp_dir = get_temp_base_dir()
Source code in src/lacuna/utils/cache.py
get_temp_dir(prefix='')
¶
Get a temporary directory within the Lacuna temp location.
Creates a unique temporary directory that can be configured via LACUNA_TEMP_DIR (or LACUNA_CACHE_DIR) environment variable, providing consistent temp location across the package. This is important for HPC environments where /tmp may not be writable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
Prefix for the temp directory name |
''
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to created temporary directory |
Examples:
>>> temp_dir = get_temp_dir(prefix="snm_sub01_")
>>> # temp_dir is e.g. ~/.cache/lacuna/tmp/snm_sub01_abc123/
>>> # Or if LACUNA_TEMP_DIR=/scratch: /scratch/snm_sub01_abc123/
Source code in src/lacuna/utils/cache.py
get_transform_cache_dir()
¶
Get the transform cache subdirectory.
Returns:
| Type | Description |
|---|---|
Path
|
Path to transform cache directory (created if doesn't exist) |
Source code in src/lacuna/utils/cache.py
make_temp_file(suffix='', prefix='', delete=False, mode='w+b')
¶
Create a temporary file in Lacuna's temp directory.
This is a replacement for tempfile.NamedTemporaryFile that uses the configurable LACUNA_TEMP_DIR instead of the system default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
suffix
|
str
|
File suffix (e.g., '.nii.gz') |
''
|
prefix
|
str
|
File prefix |
''
|
delete
|
bool
|
Whether to delete the file when closed (default: False for compatibility with external tools that need to read the file) |
False
|
mode
|
str
|
File mode (default: 'w+b' for binary write) |
'w+b'
|
Returns:
| Type | Description |
|---|---|
NamedTemporaryFile
|
A NamedTemporaryFile object with the file in LACUNA_TEMP_DIR |
Examples:
>>> with make_temp_file(suffix='.nii.gz') as f:
... nib.save(img, f.name)
... # Use f.name with external tool