Rule Files
Rule files provide a way to define complex import and export rules in a reusable YAML format. They are especially useful when command-line arguments become unwieldy or when you need multiple sets of rules with first-match-wins logic.
When to Use Rule Files
Rule files are ideal for:
- Complex rule sets - When command-line options become too long or difficult to manage
- Reusable configurations - Save and reuse rule sets across multiple import/export jobs
- Multiple rule priorities - Define multiple rules where the first matching rule is applied
- Depth-based logic - Apply different rules based on directory depth or file location
- Team sharing - Share standardized import/export configurations with your team
- Version control - Track changes to import/export logic over time
Understanding Rule Options
Rule files use the same filtering, mapping, and configuration options available via command-line arguments.
Before creating rule files, review:
- For Import Rules: Bulk Import - Customizing Import Rules
- For Export Rules: Bulk Export Settings
- For Pattern Syntax: Pattern Syntax Reference
Basic Structure
Rule files are YAML documents containing a list of rule objects. Each rule is evaluated in order, and the first rule that matches a file is applied.
Import Rule File
Export Rule File
Rule Structure
Each rule in a rule file is a YAML object containing:
level(required) - Container level:project,subject,session, oracquisition- Filter fields (
include,exclude) - Control which files match this rule - Configuration fields - Vary between import and export rules
Import Rule Structure
Import rules support the following field groups:
- Filters:
include,exclude - Mappings:
mappings,defaults,overrides - Processing:
type,zip,zip_single,group_by - DICOM-specific:
dicom_instance_name,dicom_group_by,dicom_split_localizer
For detailed field descriptions and examples, see Bulk Import - Customizing Import Rules.
Export Rule Structure
Export rules support the following field groups:
- Filters:
include,exclude - Path template:
path - Extraction:
unzip,unzip_path - Metadata:
metadata
For detailed field descriptions and examples, see Bulk Export Settings
Using Rule Files
Import with Rule File
Export with Rule File
Rule Evaluation Order
Rules are evaluated in the order they appear in the file. The first rule that matches a file is applied. This allows you to handle different file types or directory structures with different logic.
Real-World Examples
Example 1: Multi-Depth Research Data Import
Complex import rules handling different directory structures based on depth:
Example 2: Structured Data Export
Export data with separate rules for different file types and metadata:
Example 3: Multi-Site Clinical Study
Different import logic based on directory structure:
Best Practices
Rule Organization
- Order by specificity - Place more specific rules before general ones
- Use comments - Document the purpose of each rule
- Group related rules - Keep rules for similar data types together
- Test incrementally - Test each rule addition with a small dataset
Performance Optimization
- Minimize rules - Combine similar rules when possible
- Efficient filters - Use exclude filters to skip files early
- Depth-based filtering - Use depth filters to quickly categorize files
Maintainability
- Version control - Store rule files in git alongside your analysis code
- Document assumptions - Add comments explaining data structure expectations
- Use descriptive names - Name rule files clearly (e.g.,
bids-import-rules.yaml) - Test with dry-run - Always test rule changes with
--dry-runfirst
Troubleshooting
Files Not Matching Any Rules
If files aren't being processed, check:
- Rule order - Is a more general rule matching first?
- Filter syntax - Are your include/exclude patterns correct?
- Depth filters - Does the depth match your directory structure?
- Field availability - Are you using fields available for import vs export?
Use fw-beta import test to see how a single file matches your rules:
Debugging Rule Evaluation
To understand which rule matches a file:
- Add explicit depth filters to each rule for clarity
- Use the
--dry-runflag to see what would happen without actually transferring files - Check the import/export report for details on how each file was processed
See Also
- Pattern Syntax Reference - Complete syntax for filters and mappings
- Filtering and Mapping Guide - Practical examples and use cases
- Pattern Quick Reference - Quick lookup for common patterns