coord2region.utils.file_handler#
Utilities for managing atlas files.
This module handles downloading, caching, and loading atlas files used by the mapping utilities. It provides helpers for retrieving label information and packing volumetric atlas outputs.
Attributes#
Classes#
Handle file operations for atlas fetching. |
Functions#
|
Save pipeline results to a PDF file or directory. |
|
Save pipeline results to a CSV file. |
|
Save results as a directory with individual JSON files and images. |
Module Contents#
- class coord2region.utils.file_handler.AtlasFileHandler(data_dir: str | None = None, subjects_dir: str | None = None)[source]#
Handle file operations for atlas fetching.
- Parameters:
data_dir (str or None, optional) – Base directory for downloaded atlas files. Defaults to
~/coord2region. Relative paths are interpreted relative to the user’s home directory.subjects_dir (str or None, optional) – FreeSurfer
SUBJECTS_DIR. IfNone, the value is inferred frommne.get_config().
Examples
>>> handler = AtlasFileHandler() >>> handler.data_dir '/home/user/coord2region'
- save(obj, filename: str)[source]#
Save an object to the data directory using pickle.
- Parameters:
obj (Any) – The object to serialize.
filename (str) – Name of the file to save the object to.
- Raises:
ValueError – If the data directory is not writable.
Exception – If there is an error during saving.
Examples
>>> handler = AtlasFileHandler() >>> handler.save({'a': 1}, 'example.pkl')
- load(filename: str)[source]#
Load an object from the data directory.
- Parameters:
filename (str) – Name of the file to load the object from.
- Returns:
The loaded object, or
Noneif the file does not exist.- Return type:
object or None
- Raises:
Exception – If there is an error during loading.
Examples
>>> handler = AtlasFileHandler() >>> handler.load('missing.pkl') None
- fetch_from_local(atlas_file: str, atlas_dir: str, labels: str | list)[source]#
Load an atlas from a local file.
- Parameters:
- Returns:
Dictionary containing the atlas data.
- Return type:
- Raises:
FileNotFoundError – If the atlas or labels file is not found.
Exception – If there is an error during loading.
Examples
>>> handler = AtlasFileHandler() >>> handler.fetch_from_local('atlas.nii.gz', '.', ['A', 'B']) {'vol': array(...), 'hdr': array(...), 'labels': ['A', 'B']}
- fetch_from_url(atlas_url: str, **kwargs)[source]#
Download an atlas from a URL.
- Parameters:
atlas_url (str) – The URL of the atlas file.
**kwargs – Additional arguments for the download.
- Returns:
Local path to the downloaded (and possibly decompressed) file.
- Return type:
- Raises:
RuntimeError – If the download fails.
ValueError – If the data directory is not writable.
Exception – If there is an error during downloading.
Examples
>>> handler = AtlasFileHandler() >>> handler.fetch_from_url('http://example.com/atlas.nii.gz') '/path/to/atlas.nii.gz'
- coord2region.utils.file_handler.save_as_pdf(results: Sequence[Any], path: str) None[source]#
Save pipeline results to a PDF file or directory.