Skip to main content

nifti-finder - Navigate neuroimaging datasets across multiple data structures

Project description

nifti-finder

Navigate neuroimaging datasets (and more) with simple file explorers and filters. Optimized for NIfTI workflows but flexible enough for any file type by adjusting filename patterns.

  • Python: >= 3.10
  • License: Apache 2.0

Installation

pip install nifti-finder
# or from source
pip install -e .

Example usage

Explore NIfTI files with nested structure

from nifti_finder.explorers import NiftiExplorer

# Default: finds all .nii and .nii.gz files
explorer = NiftiExplorer()

for path in explorer.scan("/path/to/dataset"):
    print(path)

Track subject-level progress in BIDS-style datasets

from nifti_finder.explorers import NiftiExplorer
from nifti_finder.filters import ExcludeFileSuffix

from your_package import preprocess

explorer = NiftiExplorer(
    stage_1_pattern="sub-*",
    stage_2_pattern="**/anat/*T1w.nii*",
)

for path in explorer.scan("/path/to/dataset", progress=True, desc="Subjects"):
    preprocess(path)

Output:

Subjects:  50%|███████████████████▌               | 30/60 [00:15<00:15,  2.00 it/s]

Require a segmentation mask in a parallel labels/ tree

from nifti_finder.explorers import NiftiExplorer
from nifti_finder.filters import IncludeIfFileExists

from your_package import preprocess

explorer = NiftiExplorer(
    stage_1_pattern="sub-*",
    stage_2_pattern="**/anat/*T1w.nii*",
    filters=[
        IncludeIfFileExists(
            filename_pattern="*seg*",
            search_in="/labels",
            mirror_relative_to="/path/to/dataset",
        )
    ],
)

for path in explorer.scan("/path/to/dataset"):
    preprocess(path)

Extra functionality

Use with non-NIfTI files (e.g., JSON)

from nifti_finder import NiftiExplorer, AllPurposeFileExplorer

explorer = NiftiExplorer(stage_1_pattern="sub-*", stage_2_pattern="**/*.json")
for p in explorer.scan("/path/to/bids", progress=True, desc="Subjects"):
    print(p)

Explorers support multiple patterns and filters, but will traverse once per pattern.

General-purpose exploration

If you don’t want to assume any nested (subject/... or dataset/subject/...) hierarchy, use AllPurposeFileExplorer for flexible scanning.

from nifti_finder.explorers import AllPurposeFileExplorer

explorer = AllPurposeFileExplorer(pattern="*.json")

for path in explorer.scan("/path/to/dataset"):
    print(path)

Materializing Results

Both NiftiExplorer and AllPurposeExplorer provide convenience methods to turn the streaming output of scan() into concrete Python data structures.

This is useful when you want:

  • A list of paths (with optional sorting, deduplication, or limiting)
  • A single path (first match)
  • A quick boolean check
  • A count
  • Iteration in batches
from nifti_finder.explorers import NiftiExplorer

explorer = NiftiExplorer(stage_1_pattern="sub-*", stage_2_pattern="**/anat/*T1w.nii*")
paths = explorer.list("/path/to/dataset", sort=True, unique=True)

Filtering

Both NiftiExplorer and AllPurposeExplorer allow include/exclude filters to refine results.

from nifti_finder.explorers import AllPurposeFileExplorer
from nifti_finder.filters import (
    IncludeExtension, ExcludeDirPrefix, IncludeIfFileExists
)

explorer = AllPurposeFileExplorer(
    pattern="**/*.nii*",
    filters=[
        ExcludeFileSuffix("preprocessed"),              # drop already preprocessed files
        ExcludeDirPrefix("bad"),                        # drop 'bad' files
        IncludeIfFileExists(filename_pattern="*mask*"), # keep if a brain mask exists in same directory
    ],
    logic="AND",                                        # combination logic
)

for path in explorer.scan("/path/to/dataset"):
    print(path)

Filters can by dynamically adjusted.

explorer.add_filters(ExcludeFileSuffix("mask"))
explorer.remove_filters(ExcludeFileSuffix("mask"))
explorer.clear_filters()

Filters can be composed together to get their own combination logic.

from nifti_finder.filters import ComposeFilter, ExcludeFilePrefix

suffix_filter = ComposeFilter(
    filters=[ExcludeFileSuffix("bet"), ExcludeFileSuffix("mask")],
    logic="OR"
)
prefix_filter = ComposeFilter(
    filters=[ExcludeFileSuffix("pet"), ExcludeFileSuffix("dwi")],
    logic="OR"
)
filename_filter = ComposeFilter(
    filters=[suffix_filter, prefix_filter],
    logic="AND"
)
explorer.add_filters(filename_filter)

API Overview

  • Explorers
    • BasicFileExplorer - pattern-only scanning (any structure).
    • TwoStageFileExplorer - pattern-only scanning with progress tracking (nested structure).
    • AllPurposeFileExplorer - pattern scanning + filters (any dataset structure)
    • NiftiExplorer - pattern scanning (preconfigured for NIfTI) + filters + progress tracking (nested structure)
  • Filters (selected)
    • Include/Exclude: Extension, FilePrefix, FileSuffix, FileRegex, DirectoryPrefix/Suffix/Regex, IfFileExists

Development

# Setup
pip install -e .[test]

# Run tests
pytest -q

Why nifti-finder?

  • Simple patterns for fast discovery
  • Composable filters for precise control
  • Flexible traversal with progress tracking for common neuroimaging layouts
  • Works for any file types (not just NIfTI) by changing patterns

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nifti_finder-0.1.0.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nifti_finder-0.1.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file nifti_finder-0.1.0.tar.gz.

File metadata

  • Download URL: nifti_finder-0.1.0.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for nifti_finder-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e9e218837f6ba92ba6c3fac355796f61a0c7e2546b19a7e12ce67939e30d2672
MD5 6f934a9cb3ce6e31a3058fbdf4f1c02a
BLAKE2b-256 8200b96c43a225386948a577f7fcfaadbd28c0b766e2deea6739f86c6b042871

See more details on using hashes here.

File details

Details for the file nifti_finder-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nifti_finder-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.5

File hashes

Hashes for nifti_finder-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 161483870bd94b748dd5e0f8495e66054c4db0dfdde95cf595e70b00a685af07
MD5 4f0acbdf40bb5a1ddc8683b553c7ea38
BLAKE2b-256 16b46b90adb6cacfe561ee274fe766636e9fb5814a081c7cda11a53836e4e9aa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page