Skip to main content

Flexible file explorers and filters for navigating neuroimaging datasets and beyond.

Project description

PyPI version Python versions License CI

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-*",              # level to compute progress (e.g., root/sub-*/...)
    stage_2_pattern="**/anat/*T1w.nii*",  # rest (e.g., ses-*/anat/T1w.nii.gz)
)

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 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-1.0.0.tar.gz (17.2 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-1.0.0-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nifti_finder-1.0.0.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for nifti_finder-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b2f558beb720bb1f844898f64c072aa712fc51a987666ffd104cfac9c59e9df8
MD5 ad2d74b4dd38b0060d625f0b75124da0
BLAKE2b-256 2224d034ebda02a89417f611397c5a6370021b8273681269683177fc989590dc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nifti_finder-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for nifti_finder-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c8427dc66244c398ff3b09ac1bf57876ab6db196f1fc87033e084fed0b4c392b
MD5 0e8d940b5824c23d3b9b4da05459ded8
BLAKE2b-256 4f363bf091e8fd954115d60f59bc34860b99d7288938639d1977b833262a4397

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