Filtering and Mapping Guide
This guide provides practical examples for using Flywheel's filtering and mapping patterns in Bulk Import and Export operations. Learn how to precisely control which files are processed and how they're organized in your Flywheel project.
Getting Started
Understanding Metadata
Filtering and mapping patterns use Flywheel metadata fields extensively. For a comprehensive overview of metadata types, structure, and usage in Flywheel, see Understanding Metadata in Flywheel.
Complex Scenarios: Use Rule Files
When command-line options become unwieldy or you need multiple sets of rules with first-match-wins logic, consider using Rule Files to define your import/export configuration in a reusable YAML format.
Understanding Filter Logic
Flywheel uses two types of filters:
- Include filters (
-i/--include): At least one must match for a file to be processed - Exclude filters (
-e/--exclude): If any match, the file is skipped
If no include filters are specified, all files are included by default (unless excluded).
Basic Syntax
Each filter follows this pattern:
Examples:
Common Use Cases
File Type Filtering
Include Specific File Types
Exclude File Types
Directory Structure Filtering
By Directory Depth
By Directory Pattern
Size-Based Filtering
File Size Ranges
Using Human-Readable Sizes
DICOM-Specific Filtering (Export Only)
Export Operations Only
DICOM metadata filtering is only available for exports, not imports. During imports, filters can only use file system information (path, name, size, etc.).
To use DICOM metadata in export filters, you must first extract the DICOM headers into Flywheel metadata by running the File Metadata Importer gear.
By Modality
By Series Description
By Patient Information
Mapping Patterns
Mapping patterns define how source paths are transformed into Flywheel's hierarchy structure.
Basic Mapping Syntax
Standard Variables
{subject}or{sub}- Subject label{session}or{ses}- Session label{acquisition}or{acq}- Acquisition label{file}- File name (shorthand for{file.name})- DICOM tags like
{PatientID},{StudyDate},{SeriesDescription}, etc.
Common Mapping Examples
Simple Directory Mapping
Skip Directory Levels
Extract from File Names
Container Label Derivation
This is the most common mapping use case - deriving Flywheel container labels from either file paths or DICOM metadata.
From File Path Structure
From DICOM Metadata
Advanced Mapping Patterns
Date-Based Organization
DICOM File Naming
Note: The --dicom-instance-name option (available in version 20.5+) controls the naming of ZIP archives created when grouping DICOM files during import.
Multiple Path Components
Real-World Examples
Example 1: Custom DICOM Import with Date-Based Labels
Scenario: Import DICOM data with custom labels incorporating dates and custom ZIP file naming
Source structure:
Import command:
Custom Mapping with Dates
This example demonstrates:
- Session labels: Combine study date with description (e.g.,
20240315_Brain_Protocol) - Acquisition labels: Include series number, modality, and description (e.g.,
001_MR_T1_MPRAGE) - ZIP file naming: Custom format using series metadata instead of default naming
- Important: Subject labels use the default
{PatientID}mapping. Custom mappings override defaults, so--type dicomre-enables DICOM-specific behaviors (grouping, zipping) before applying these custom rules
Example 2: Multi-Modal Export
Scenario: Export T1 and fMRI data for analysis
Export command:
DICOM Metadata Extraction Required
This export example requires that DICOM headers have been extracted into Flywheel metadata by running the File Metadata Importer gear before the export.
Custom Export Path
The --path value used here starts with {subject.label}/, which avoids creating a project-level folder at the top of the export destination. The default path is {project.label}/{subject.label}/{session.label}/{acquisition.label}/{file.name}, which includes the project folder.
Example 3: Quality Control Filter
Scenario: Import only high-quality, complete scans
Import command:
Import Filtering Limitations
This import example filters by filename patterns (e.g., excluding files with "localizer" or "scout" in the name). Import filters cannot use DICOM metadata tags. To filter by actual DICOM SeriesDescription values, you would need to import all files first, then use an export operation with DICOM metadata filters.
Testing Your Patterns
Before running full imports or exports, test your patterns using the test or dry-run modes.
Import Test Command
For imports, the import test command shows exactly how patterns will be applied and what metadata will be extracted from a specific file:
This shows:
- Whether the file matches your filters
- What metadata would be extracted
- How the file would be organized in Flywheel
Dry-Run Mode
For both imports and exports, the --dry-run option performs a simulated operation without making any actual changes:
Dry-run mode:
- Processes only a small subset of the data (not the full dataset)
- Shows a preview of how patterns will be applied
- Presents a summary report of how data would be processed
- Makes no actual changes to files or Flywheel
Troubleshooting Common Issues
Pattern Not Matching
- Check case sensitivity: String matching is case-insensitive, but verify your patterns
- Test incrementally: Start with simple patterns and add complexity
- Use the test command: Test patterns on sample files first
Incorrect Mapping
- Verify source structure: Ensure your mapping pattern matches the actual file paths
- Check for missing components: All required fields (
sub,ses, etc.) must have values - Test with representative files: Use files from different parts of your dataset
Performance Issues
- Be specific: More specific patterns reduce processing time
- Use exclude filters: Exclude unwanted files early in the process
- Optimize depth filtering: Use depth filters to avoid scanning unnecessary directories
Best Practices
Filter Design
- Start broad, then narrow: Begin with inclusive patterns and add exclusions
- Test on sample data: Always test on a small subset first
- Document your patterns: Keep track of complex filter combinations
- Use meaningful names: Choose descriptive variable names in mappings
Maintainability
- Use rules files: Store complex patterns in YAML files for reuse
- Comment your patterns: Document the purpose of complex filters
- Version your rules: Keep track of pattern changes over time
- Test after changes: Verify patterns still work after modifications