CSA Header
Parse CSA header information from Siemens MRI acquisitions with Python.
Package & Distribution
Build & Quality Assurance
Code Quality & Style
Citation & License
Some Siemens MRI scans may include CSA headers that provide valuable information about the acquisition and storage of the data. These headers are stored as private data elements, usually looking something like:
(0029, 1010) CSA Image Header Type OB: 'IMAGE NUM 4'
(0029, 1010) CSA Image Header Version OB: '20100114'
(0029, 1010) CSA Image Header Info OB: Array of 11560 bytes
(0029, 1020) CSA Series Header Type OB: 'MR'
(0029, 1020) CSA Series Header Version OB: '20100114'
(0029, 1020) CSA Series Header Info OB: Array of 80248 bytes
The CSA Image Header Info and CSA Series Header Info elements contain encoded information which is crucial for the correct interpretation of the associated acquisition data.
For a detailed explanation on the CSA encoding scheme, please see this excellent article from NiBabel's documentation site.
Features
- Fast and Lightweight: Minimal dependencies (numpy, pydicom)
- Comprehensive Parsing: Supports both CSA header types (Type 1 and Type 2)
- XA Enhanced Support: Reads the MR protocol of syngo MR XA30, XA60 and later Enhanced DICOMs
- ASCCONV Support: Automatic parsing of embedded ASCCONV protocol parameters
- Type-Safe: Complete type hints for all public APIs
- Well-Tested: Tested on Linux, macOS and Windows with every supported Python version
- Python 3.10+: Modern Python with support through Python 3.14
- NiBabel Compatible: Integrates seamlessly with neuroimaging workflows
Supported DICOM Formats
- Siemens V-series DICOMs (such as syngo MR E11): binary CSA headers, usually in
(0029,1010)and(0029,1020). Read them withCsaHeader. - Siemens XA Enhanced DICOMs (syngo MR XA30, XA60 and later): no CSA headers. The MR protocol is XProtocol text
in
SharedFunctionalGroupsSequence. Read it withCsaAsciiHeader.from_dicom().
Table of Contents
- Features
- Installation
- Quickstart
- Advanced Usage
- Integration with NiBabel
- Examples
- Tests
- Contributing
- Citation
- License
Installation
pip install csa_header
Optional Dependencies
For working with the provided examples and automatic example data downloading:
pip install csa_header[examples]
This installs:
nibabelfor NiBabel integration examplespoochfor automatic downloading of example DICOM files
For development (includes pre-commit hooks and IPython):
pip install csa_header[dev]
Quickstart
Option 1: Using Example Data (Easiest!)
The quickest way to get started is using the built-in example data:
>>> # Install with examples support
>>> # pip install csa_header[examples]
>>>
>>> from csa_header.examples import fetch_example_dicom
>>> from csa_header import CsaHeader
>>> import pydicom
>>>
>>> # Fetch example DICOM (downloads once, then cached)
>>> dicom_path = fetch_example_dicom()
>>> dcm = pydicom.dcmread(dicom_path)
>>>
>>> # Method 1: Let from_dicom() find the header
>>> csa_header = CsaHeader.from_dicom(dcm, 'series')
>>> parsed_csa = csa_header.read()
>>> len(parsed_csa)
79
>>>
>>> # Method 2: Read the element yourself
>>> raw_csa = dcm[(0x29, 0x1020)].value
>>> parsed_csa = CsaHeader(raw_csa).read()
>>> len(parsed_csa)
79
New in version 2.1.0: from_dicom() finds the CSA header through its private creator, so you don't need its DICOM tag number.
The example file is an anonymized Siemens MPRAGE scan hosted on Zenodo:
Option 2: Using Your Own DICOM Files
Use pydicom to read a DICOM header:
>>> import pydicom
>>> from csa_header import CsaHeader
>>> dcm = pydicom.dcmread("/path/to/file.dcm")
Recommended approach: let from_dicom() find the header.
>>> # Extract CSA Series Header (or use 'image' for Image Header)
>>> csa_header = CsaHeader.from_dicom(dcm, 'series')
>>> if csa_header:
... parsed_csa = csa_header.read()
... print(f"Found {len(parsed_csa)} CSA tags")
Found 79 CSA tags
Alternative approach: read the element yourself, if you know its tag.
>>> # For CSA Series Header Info: (0x0029, 0x1020)
>>> # For CSA Image Header Info: (0x0029, 0x1010)
>>> data_element = dcm.get((0x29, 0x1020))
>>> if data_element:
... raw_csa = data_element.value
... parsed_csa = CsaHeader(raw_csa).read()
Example parsed CSA header structure:
>>> parsed_csa
{
'NumberOfPrescans': {'VR': 'IS', 'VM': 1, 'value': 0},
'TransmitterCalibration': {'VR': 'DS', 'VM': 1, 'value': 247.102},
'PhaseGradientAmplitude': {'VR': 'DS', 'VM': 1, 'value': 0.0},
'ReadoutGradientAmplitude': {'VR': 'DS', 'VM': 1, 'value': 0.0},
'SelectionGradientAmplitude': {'VR': 'DS', 'VM': 1, 'value': 0.0},
'GradientDelayTime': {'VR': 'DS',
'VM': 3,
'value': [36.0, 35.0, 31.0]},
'RfWatchdogMask': {'VR': 'IS', 'VM': 1, 'value': 0},
'RfPowerErrorIndicator': {'VR': 'DS', 'VM': 1, 'value': None},
'SarWholeBody': {'VR': 'DS', 'VM': 3, 'value': None},
'Sed': {'VR': 'DS',
'VM': 3,
'value': [1000000.0, 324.74800987, 324.74800832]}
...
}
Working with XA Enhanced DICOMs
XA Enhanced DICOMs (syngo MR XA30, XA60 and later) have no binary CSA headers, so CsaHeader.from_dicom() returns
None for them. They store the MR protocol as XProtocol text in a private element of SharedFunctionalGroupsSequence,
which CsaAsciiHeader.from_dicom() reads:
>>> import pydicom
>>> from csa_header import CsaAsciiHeader
>>>
>>> dcm = pydicom.dcmread("xa_enhanced.dcm")
>>> protocol_header = CsaAsciiHeader.from_dicom(dcm)
>>> protocol = protocol_header.parsed
>>> protocol['tProtocolName']
'localizer'
>>> protocol['sSliceArray']['lSize']
13
The protocol is parsed the same way as the MrPhoenixProtocol value of a V-series series header, so one function can
read protocol values from both:
>>> from csa_header import CsaAsciiHeader, CsaHeader
>>>
>>> def read_protocol(dcm):
... """Return the parsed MR protocol of a Siemens DICOM, or None."""
... series_header = CsaHeader.from_dicom(dcm, 'series')
... if series_header is not None:
... return series_header.read().get('MrPhoenixProtocol', {}).get('value')
... protocol_header = CsaAsciiHeader.from_dicom(dcm)
... return protocol_header.parsed if protocol_header is not None else None
Advanced Usage
Extracting ASCCONV Protocol
CSA headers often contain the complete scanner protocol in ASCCONV format under the MrPhoenixProtocol tag. This is automatically parsed into a nested dictionary:
>>> parsed_csa = CsaHeader(raw_csa).read()
>>> protocol = parsed_csa.get('MrPhoenixProtocol')
>>> if protocol:
... # Access protocol parameters
... tr = protocol['alTR'][0] # Repetition time
... te = protocol['alTE'][0] # Echo time
... print(f"TR: {tr} ms, TE: {te} ms")
TR: 2000 ms, TE: 30 ms
Diffusion MRI (DWI) Parameters
Extract diffusion-specific parameters for DTI/DWI analysis:
>>> # From image header (0x0029, 0x1020)
>>> image_header = CsaHeader(dcm[0x0029, 0x1020].value).read()
>>> b_value = image_header.get('B_value')
>>> gradient = image_header.get('DiffusionGradientDirection')
>>> print(f"B-value: {b_value}, Gradient: {gradient}")
B-value: 1000, Gradient: [0.707, 0.707, 0.0]
Functional MRI (fMRI) Parameters
Extract slice timing for slice timing correction:
>>> # From series header (0x0029, 0x1010)
>>> series_header = CsaHeader(dcm[0x0029, 0x1010].value).read()
>>> slice_times = series_header.get('MosaicRefAcqTimes')
>>> n_slices = series_header.get('NumberOfImagesInMosaic')
>>> print(f"Slice times: {slice_times[:3]}... ({n_slices} slices)")
Slice times: [0.0, 52.5, 105.0]... (64 slices)
Integration with NiBabel
csa_header works seamlessly with NiBabel for comprehensive neuroimaging workflows. The from_dicom() method provides a nibabel-style API for extracting CSA headers:
import nibabel as nib
import pydicom
from csa_header import CsaHeader
# Load DICOM with both pydicom and NiBabel
dcm = pydicom.dcmread('scan.dcm')
nib_img = nib.load('scan.dcm')
# Extract the image CSA header
csa_header = CsaHeader.from_dicom(dcm, 'image')
if csa_header:
csa_info = csa_header.read()
# Use NiBabel for image data
data = nib_img.get_fdata()
# Use CSA for metadata
slice_times = csa_info.get('MosaicRefAcqTimes', [])
print(f"Image shape: {data.shape}")
print(f"Slice timing from CSA: {len(slice_times)} time points")
Use Cases with NiBabel
- Slice Timing Correction: Extract
MosaicRefAcqTimesfor accurate fMRI preprocessing - Diffusion Imaging: Get b-values and gradient directions for DTI analysis
- Protocol Verification: Confirm acquisition parameters match expected values
- Quality Assurance: Extract technical parameters for QA pipelines
- BIDS Conversion: Generate complete metadata for BIDS-compliant datasets
See examples/nibabel_integration.py for complete integration examples.
Related Projects
NiBabel
NiBabel is a comprehensive neuroimaging file format library that pioneered CSA header parsing in Python. The csa_header package focuses specifically on CSA header parsing with a lightweight, dependency-minimal approach, while NiBabel provides broader neuroimaging format support.
The from_dicom() method was inspired by nibabel's get_csa_header() function, adapted to fit csa_header's focused API design.
When to use each:
- Use
csa_headerwhen you need fast, lightweight CSA parsing with minimal dependencies - Use NiBabel when you need comprehensive neuroimaging format support (NIfTI, DICOM, MINC, etc.)
- Use both together for complete neuroimaging workflows (see Integration with NiBabel)
For more on NiBabel's CSA header support, see: https://nipy.org/nibabel/dicom/siemens_csa.html
PyDICOM
Our DICOM integration is powered by the excellent PyDICOM library, which provides comprehensive DICOM file parsing capabilities. csa_header extends PyDICOM by providing specialized parsing for Siemens' proprietary CSA header format.
Examples
The examples/ directory contains comprehensive usage examples:
-
basic_usage_example.py: Beginner-friendly introduction using example data
- Automatic download of example DICOM files
- Basic CSA header parsing
- Accessing specific CSA tags
- Perfect for first-time users!
-
nibabel_integration.py: Complete workflow combining csa_header with NiBabel
- Extract DWI parameters (b-values, gradients)
- Extract fMRI parameters (slice timing, TR/TE)
- Parse ASCCONV protocol parameters
- Batch processing multiple DICOM files
Run examples:
# Basic example with automatic data download
python examples/basic_usage_example.py
# NiBabel integration with your own DICOM
python examples/nibabel_integration.py path/to/siemens_dicom.dcm
Tests
This package uses hatch to manage development and packaging. To run the tests, simply run:
hatch run test
Coverage
To run the tests with coverage, run:
hatch run cov
Or, to automatically generate an HTML report and open it in your default browser:
hatch run cov-show
Contributing
Contributions are welcome! This project is community-maintained and aims to be a reliable tool for the medical imaging community.
How to Contribute
- Report Issues: Found a bug or have a feature request? Open an issue
- Submit Pull Requests: See CONTRIBUTING.md for detailed guidelines
- Improve Documentation: Help make the docs better
- Share Examples: Contribute integration examples or use cases
Development Setup
# Clone the repository
git clone https://github.com/open-dicom/csa_header.git
cd csa_header
# Install hatch
pip install hatch
# Run tests
hatch run test:test
# Run linting
hatch run lint:all
# Install pre-commit hooks
hatch run pre-commit install
See CONTRIBUTING.md for complete development guidelines including:
- Code style requirements
- Testing requirements (90%+ coverage)
- Commit message conventions
- Pull request process
Citation
If you use csa_header in your research, please cite it using the following:
BibTeX:
@software{baratz_2025_csa_header,
author = {Baratz, Zvi and Brett, Matthew},
title = {csa_header: Parse CSA header information from Siemens MRI acquisitions},
year = 2025,
publisher = {Zenodo},
version = {v1.0.2},
doi = {10.5281/zenodo.17474448},
url = {https://doi.org/10.5281/zenodo.17474448}
}
APA:
Baratz, Z., & Brett, M. (2025). csa_header: Parse CSA header information from Siemens MRI acquisitions (v1.0.2). Zenodo. https://doi.org/10.5281/zenodo.17474448
For the specific version you're using, please check the Zenodo record for the appropriate DOI and citation information.
Alternatively, you can use the CITATION.cff file in this repository, which is automatically recognized by GitHub and can be imported into reference managers.
License
csa_header is distributed under the terms of the MIT license.
Metadata
Release files for csa-header 2.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| csa_header-2.1.0.tar.gz | 141.1 kB | Without provenance | Details for csa_header-2.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| csa_header-2.1.0-py3-none-any.whl | Python 3 | none | any | Details for csa_header-2.1.0-py3-none-any.whl |
Total release size: 166.3 kB
Release files / csa_header-2.1.0.tar.gz
| Download URL | csa_header-2.1.0.tar.gz |
|---|---|
| Size | 141.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
efcb9d85d526f3d547741d29cfee32a82f99579ea5ebe47a163d93af3cb9150a
|
|
BLAKE2b-256 checksum How to use checksums |
21a10db89e359e1aa8ee786598b1adf9395420d1dc64266ef3fadba4e3df3aed
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / csa_header-2.1.0-py3-none-any.whl
| Download URL | csa_header-2.1.0-py3-none-any.whl |
|---|---|
| Size | 25.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bab23ffbd1bd6a60cb53ae11ab5cfaf5bc4ef7e71e4d747044039d4551f314bd
|
|
BLAKE2b-256 checksum How to use checksums |
fae853b405ce3f2bf72ad0ed8d4967032028480f3e7420ad43c8b9abf45db14c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Security for csa-header 2.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.