Version 0.2.1 | Python 3.8+ | MATLAB R2020a+
magatfairy converts MAGAT (Multi Animal Gate and Track Analyzer) experiments into clean H5 files for Python workflows. It ships the MATLAB bridge, conversion scripts, and validation tools so you can drag in data, run a single command, and get analysis-ready H5 outputs.
This tool is designed for researchers who:
- Use MATLAB and MAGAT Analyzer
- Process data from optogenetic genotype experiments
- Have root folders containing ESET directories (or single ESET folders)
- Want to convert their data to H5 format for Python analysis
git clone https://github.com/GilRaitses/magatfairy.git
cd magatfairyNo conda or Docker required. Plain Python 3.8+ with pip is enough. A venv is optional but recommended.
# optional but recommended
python -m venv .venv
# Windows: .\.venv\Scripts\activate
# mac/Linux: source .venv/bin/activate
python src/install/install.pyThis will install:
- numpy
- h5py
- scipy
- matlabengine (MATLAB Engine for Python)
Note: If MATLAB Engine installation fails, you may need to install it manually from MATLAB:
cd matlabroot/extern/engines/python
python setup.py installSee: https://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html
-
Standard machines (recommended):
# optionally inside a venv pip install -e . magatfairy --help
Ensure your Python Scripts dir is on PATH:
- Windows:
%APPDATA%\Python\Python311\Scriptsor your venv’sScripts - mac/Linux:
$HOME/.local/binor your venv’sbin
- Windows:
-
Managed / locked-down machines (no PATH changes):
python -m cli.magatfairy --help python -m cli.magatfairy convert auto /path/to/data
(Uses the module directly; works even if executables are blocked.)
- magatfairy ships bundled MAGAT class folders at
matlab/coreand uses them by default (no external download). - If you need to override with a different MAGAT codebase, set
MAGAT_CODEBASEormagatfairy config set magat_codebase /path/to/codebase.
- Minimal wiring check (no installs):
python -m cli.magatfairy --help - Dependency check/install:
python src/install/install.py(reports Python version, git, MATLAB engine, and installs pip deps) - Built-in CLI check:
magatfairy systemfairy(orpython -m cli.magatfairy systemfairyon locked-down machines)
Quick Start (Recommended):
magatfairy convert autoThis will auto-detect your data type and guide you through conversion.
Interactive Mode:
magatfairyThe tool will guide you through:
- Data Path Selection - Choose root folder with multiple ESETs or single ESET directory
- Output Directory - Specify where H5 files should be saved
- Conversion - Process your data
Note: MAGAT codebase is bundled by default. You can override it with MAGAT_CODEBASE environment variable if needed.
- Python 3.8+
- MATLAB (with MATLAB Engine for Python installed)
Note: MAGAT codebase is bundled with magatfairy at matlab/core and used by default. No external download needed.
- Git - Required for cloning magatfairy repository (if you cloned magatfairy, you have git!)
magatfairy includes a bundled minimal subset of MAGAT (MATLAB Track Analysis) core classes at matlab/core. These are used by default, so no external download is required.
The bundled classes are automatically used when you run magatfairy. No configuration needed.
If you need to use a different MAGAT codebase (e.g., for additional features):
-
Set the
MAGAT_CODEBASEenvironment variable:# Windows PowerShell $env:MAGAT_CODEBASE = "D:\path\to\MAGATAnalyzer-Matlab-Analysis" # Windows CMD set MAGAT_CODEBASE=D:\path\to\MAGATAnalyzer-Matlab-Analysis # macOS/Linux export MAGAT_CODEBASE=/path/to/MAGATAnalyzer-Matlab-Analysis
-
Or use the config command:
magatfairy config set magat_codebase /path/to/MAGATAnalyzer-Matlab-Analysis
The bundled classes include the essential DataManager, ExportManager, TrackManager, and related classes needed for conversion.
GMR61@GMR61/
├── T_Re_Sq_0to250PWM_30#C_Bl_7PWM/
│ ├── matfiles/
│ │ ├── GMR61@GMR61_T_Re_Sq_0to250PWM_30#C_Bl_7PWM_202510301513.mat
│ │ └── ...
│ └── ...
├── T_Re_Sq_50to250PWM_30#C_Bl_7PWM/
│ ├── matfiles/
│ │ └── ...
│ └── ...
└── ...
T_Re_Sq_0to250PWM_30#C_Bl_7PWM/
├── matfiles/
│ ├── GMR61@GMR61_T_Re_Sq_0to250PWM_30#C_Bl_7PWM_202510301513.mat
│ └── ...
└── ...
H5 files are saved to the specified output directory with the following structure:
h5_output/
├── GMR61@GMR61_T_Re_Sq_0to250PWM_30#C_Bl_7PWM_202510301513.h5
├── GMR61@GMR61_T_Re_Sq_0to250PWM_30#C_Bl_7PWM_202510311441.h5
└── ...
Each H5 file contains:
- Complete MAGAT structure
- Tracks with derived quantities
- Global quantities (LED values, etc.)
- ETI (Experiment Time Index) at root level
- Camera calibration data
magatfairy convert batch --root-dir /path/to/GMR61@GMR61 --output-dir /path/to/h5_outputOr use auto-detect (recommended):
magatfairy convert auto /path/to/GMR61@GMR61magatfairy convert auto /path/to/T_Re_Sq_0to250PWM_30#C_Bl_7PWM/Or use batch mode with ESET directory:
magatfairy convert batch --root-dir /path/to/T_Re_Sq_0to250PWM_30#C_Bl_7PWM --output-dir /path/to/h5_outputNote: The --codebase flag is optional. Bundled classes are used by default. Override with MAGAT_CODEBASE environment variable if needed.
If you see "MATLAB Engine Not Found":
- Ensure MATLAB is installed
- Install MATLAB Engine for Python:
cd matlabroot/extern/engines/python python setup.py install
If git is not installed and you need to clone magatfairy:
- Install git: https://git-scm.com/downloads
- Or download magatfairy as a ZIP from GitHub
Ensure your data folder contains subdirectories with matfiles/ folders containing .mat files.
- Run system check:
magatfairy systemfairyto verify environment - Verify that bundled MAGAT classes are present at
matlab/core/@DataManager - Verify that
.matfiles are inmatfiles/subdirectories - Ensure output directory is writable
- Check MATLAB Engine is properly installed
magatfairy/
├── README.md # This file
├── LICENSE # MIT License
├── requirements.txt # Python dependencies
├── pyproject.toml # Packaging + console_scripts entry
├── index.html # Landing page
│
├── matlab/ # Bundled MAGAT core classes
│ └── core/ # Minimal MATLAB classes (DataManager, etc.)
│ ├── @DataManager/ # Data loading and management
│ ├── @ExportManager/ # Export utilities
│ ├── @TrackManager/ # Track management
│ └── ... # Other core classes
│
├── src/ # Source code
│ ├── install/ # Installation script (python src/install/install.py)
│ ├── cli/ # Installed CLI shim (`magatfairy` command)
│ ├── launch_scripts/ # Double-click launchers for each OS
│ ├── mat2h5/ # Core package (stable API)
│ │ ├── __init__.py # Package initialization
│ │ ├── bridge.py # MAGAT Bridge (MATLAB interface)
│ │ ├── config.py # Configuration management
│ │ └── progress.py # Progress tracking utilities
│ ├── scripts/ # User-facing scripts (separate from package)
│ │ ├── convert/ # MATLAB → H5 conversion tools
│ │ │ ├── convert_matlab_to_h5.py # Core conversion logic
│ │ │ ├── batch_export_esets.py # Batch processing
│ │ │ ├── append_camcal_to_h5.py # Camera calibration
│ │ │ └── unlock_h5_file.py # H5 file utilities
│ │ └── analyze/ # H5 analysis scripts
│ │ ├── engineer_data.py # Basic analysis
│ │ └── engineer_dataset_from_h5.py # Enhanced analysis
│ └── validation/ # Validation framework (separate from package)
│ ├── reference/ # MATLAB reference implementations
│ └── validators/ # Python validation scripts
│
└── docs/ # Additional documentation and assets
├── assets/ # Web assets (e.g., fairy-frames.js)
├── field-mapping.md # MATLAB to H5 field reference
└── magat-license.md # MAGAT Analyzer license info
Note: The MAGAT Bridge code is included in src/mat2h5/bridge.py, and MAGAT core classes are bundled at matlab/core. You don't need any external dependencies beyond MATLAB.
magatfairy provides a CLI with subcommands for all operations:
macOS: Double-click src/launch_scripts/magatfairy.command
Linux: Double-click src/launch_scripts/magatfairy.sh
Windows: Double-click src/launch_scripts/magatfairy.bat
This opens a terminal window. Drag your data folder into it and press Enter. H5 files will be saved to exports/ folder in the magatfairy repository.
# Drag a folder into the terminal and press Enter
magatfairy convert auto
# Or provide the path directly
magatfairy convert auto /path/to/your/dataThe tool will automatically detect:
- Genotype (root directory with multiple ESET folders)
- ESET (single folder with matfiles/ subdirectory)
- Experiment (single .mat file)
- Track (track file or tracks directory)
You'll be prompted for:
- Output directory (default:
exports/folder in the magatfairy repository)
Note: MAGAT codebase is bundled and used automatically. Override with MAGAT_CODEBASE environment variable if needed.
# Conversion commands
magatfairy convert batch --root-dir /path/to/data --output-dir /path/to/output
magatfairy convert single --mat file.mat --output file.h5
magatfairy convert append-camcal --eset-dir /path/to/eset
magatfairy convert unlock --file file.h5
# Analysis commands
magatfairy analyze engineer --h5 file.h5
magatfairy analyze dataset --h5 file.h5
# Validation commands
magatfairy validate schema --h5 file.h5
magatfairy validate integrity --mat file.mat --h5 file.h5
magatfairy validate full --base-dir /path/to/data
# Help
magatfairy --help
magatfairy convert --helpScripts can also be run directly:
python src/scripts/convert/batch_export_esets.py --root-dir /path/to/data --output-dir /path/to/outputSave your settings to avoid entering them every time:
magatfairy config set magat_codebase /path/to/magat # Optional: override bundled classes
magatfairy config set default_output /path/to/exports
magatfairy config show # View all settingsNote: magat_codebase is optional. Bundled classes at matlab/core are used by default.
- Install MATLAB Engine for Python:
cd matlabroot/extern/engines/python python setup.py install - See: https://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html
- Run system check:
magatfairy systemfairy(orpython -m cli.magatfairy systemfairy) - Verify MATLAB Engine is installed and accessible
- Check that bundled MAGAT classes are present:
matlab/core/@DataManager - If using custom codebase, verify path:
magatfairy config get magat_codebase - Check log file:
exports/conversion.log
- Use
--skip-existingto skip already-converted files - Use
--resumeto continue from previous progress
- Progress is saved to
exports/.progress.json - Use
--resumeto continue interrupted conversions - Colored progress: Red (beginning), White (middle), Blue (end)
For issues or questions:
- Check the troubleshooting section above
- Review field mapping reference:
docs/field-mapping.md - Check log files in
exports/directory
- Fixed
derivation_rulesexport: MATLAB returns DerivationRules as matlab.object, not dict - Extract struct fields individually via
eng.eval()for reliable cross-platform behavior - Use
require_group()to prevent "group already exists" errors on fallback
- Added
derivation_rulesexport (smoothTime, derivTime, interpTime) for INDYsim compatibility - Fixed stimulus detection for empty onset arrays
- Fixed division-by-zero in progress tracker
- Added unit documentation to all exported fields
- Added
validate_h5_for_analysis.pyvalidation script
- Initial release
- CLI with
magatfairycommand - Batch conversion of ESET directories
- Bundled MATLAB core classes
- systemfairy environment checker
- Colored progress tracking
This project is licensed under the MIT License - see the LICENSE file for details.
magatfairy uses the MAGAT Analyzer (Multi Animal Gate and Track Analyzer) codebase:
- Repository: samuellab/MAGATAnalyzer-Matlab-Analysis
- License: Creative Commons Attribution Share Alike 3.0 United States License
- Copyright: © 2011 Marc Gershow
- Citation: Gershow M, Berck M, Mathew D, Luo L, Kane E, Carlson J, Samuel ADT. Controlling Airborne Chemical Cues For Studying Navigation in Small Animals. Nature Methods. 2012
If you use magatfairy or MAGAT Analyzer in your research, please cite the above publication.
License Notice: Any modification or redistribution of MAGAT Analyzer code must include the original license. See the MAGAT Analyzer repository for full license details.