| 1 |
# MRD File Format |
| 2 |
MRD data can be stored in various formats, but the [HDF5](https://www.hdfgroup.org/solutions/hdf5) format is commonly used due to its good compatibility across programming languages and platforms. HDF5 is a hierarchical data format (much like a file system), which can contain multiple variable organized in groups (like folders in a file system). The variables can contain arrays of data values, custom defined structs, or simple text fields. Interface libraries are provided for C++, Python, and MATLAB to simplify usage. MRD HDF5 files can also be opened using standard HDF tools such as [HDFView](https://www.hdfgroup.org/downloads/hdfview/) or HDF5 packages such as [h5py](https://www.h5py.org/) for Python or the built-in [h5read](https://www.mathworks.com/help/matlab/ref/h5read.html) and associated functions in MATLAB. |
| 3 |
|
| 4 |
The extension ``.mrd`` is used to indicate an HDF5 file containing MRD formatted data as follows: |
| 5 |
``` |
| 6 |
/dataset/xml text of MRD header |
| 7 |
/dataset/data array of raw data (data + AcquisitionHeader + optional trajectory) |
| 8 |
/dataset/waveforms array of waveform (e.g. PMU) data |
| 9 |
/dataset/image_0/data array of image data |
| 10 |
/dataset/image_0/header array of ImageHeaders |
| 11 |
/dataset/image_0/attributes array of image MetaAttributes (xml text) |
| 12 |
/dataset/config text of configuration parameters for reconstruction or image analysis (optional) |
| 13 |
/dataset/config_file file name of configuration parameters for reconstruction or image analysis (optional) |
| 14 |
``` |
| 15 |
|
| 16 |
All data from a complete acquisition are stored in a group (``dataset`` in the above example). An MRD file may contain multiple acquisitions in separate groups, usually in the case of related or dependent acquisitions. |
| 17 |
|
| 18 |
## Reading MRD data in Python |
| 19 |
The [ismrmrd-python](https://www.github.com/ismrmrd/ismrmrd-python) library provides a convenient interface for working with MRD files. It can either be compiled from source or installed from a pip package using the command ``pip install ismrmrd``. The following code shows an example of getting the number of readout lines from a dataset and reading the first line of k-space data: |
| 20 |
```python |
| 21 |
>>> import ismrmrd |
| 22 |
>>> dset = ismrmrd.Dataset('data.mrd') |
| 23 |
>>> nacq = dset.number_of_acquisitions() |
| 24 |
>>> acq = dset.read_acquisition(0) |
| 25 |
>>> dset.close() |
| 26 |
>>> print(acq.getHead()) |
| 27 |
version: 1 |
| 28 |
flags: 0 |
| 29 |
measurement_uid: 281 |
| 30 |
scan_counter: 3 |
| 31 |
acquisition_time_stamp: 23187655 |
| 32 |
physiology_time_stamp: 4304495, 0, 0 |
| 33 |
number_of_samples: 256 |
| 34 |
available_channels: 2 |
| 35 |
active_channels: 2 |
| 36 |
channel_mask: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 37 |
discard_pre: 0 |
| 38 |
discard_post: 0 |
| 39 |
center_sample: 128 |
| 40 |
encoding_space_ref: 0 |
| 41 |
trajectory_dimensions: 0 |
| 42 |
sample_time_us: 15.0 |
| 43 |
position: 0.0, 0.0, 0.0 |
| 44 |
read_dir: -0.9999999403953552, 5.960464477539063e-08, 0.0 |
| 45 |
phase_dir: 5.960464477539063e-08, 0.9999999403953552, 0.0 |
| 46 |
slice_dir: 0.0, 0.0, 1.0 |
| 47 |
patient_table_position: 0.0, 0.0, -1374995.0 |
| 48 |
idx: kspace_encode_step_1: 2 |
| 49 |
kspace_encode_step_2: 0 |
| 50 |
average: 0 |
| 51 |
slice: 0 |
| 52 |
contrast: 0 |
| 53 |
phase: 0 |
| 54 |
repetition: 0 |
| 55 |
set: 0 |
| 56 |
segment: 0 |
| 57 |
user: 0, 0, 0, 0, 0, 64, 0, 0 |
| 58 |
|
| 59 |
user_int: 0, 0, 0, 0, 0, 0, 0, 0 |
| 60 |
user_float: 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 |
| 61 |
|
| 62 |
>>> acq.data.shape |
| 63 |
(2, 256) |
| 64 |
``` |
| 65 |
|
| 66 |
The basic [h5py](https://www.h5py.org) package Python can also be used to read the files: |
| 67 |
```python |
| 68 |
>>> import h5py |
| 69 |
>>> import numpy as np |
| 70 |
>>> f = h5py.File('data.mrd') |
| 71 |
>>> acq = np.array(f['dataset']['data'][0]) |
| 72 |
>>> { k: acq['head'][k] for k in acq['head'].dtype.fields.keys() } |
| 73 |
|
| 74 |
{'version': array(1, dtype=uint16), |
| 75 |
'flags': array(64, dtype=uint64), |
| 76 |
'measurement_uid': array(0, dtype=uint32), |
| 77 |
'scan_counter': array(0, dtype=uint32), |
| 78 |
'acquisition_time_stamp': array(0, dtype=uint32), |
| 79 |
'physiology_time_stamp': array([0, 0, 0], dtype=uint32), |
| 80 |
'number_of_samples': array(512, dtype=uint16), |
| 81 |
'available_channels': array(8, dtype=uint16), |
| 82 |
'active_channels': array(8, dtype=uint16), |
| 83 |
'channel_mask': array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=uint64), |
| 84 |
'discard_pre': array(0, dtype=uint16), |
| 85 |
'discard_post': array(0, dtype=uint16), |
| 86 |
'center_sample': array(256, dtype=uint16), |
| 87 |
'encoding_space_ref': array(0, dtype=uint16), |
| 88 |
'trajectory_dimensions': array(0, dtype=uint16), |
| 89 |
'sample_time_us': array(5., dtype=float32), |
| 90 |
'position': array([0., 0., 0.], dtype=float32), |
| 91 |
'read_dir': array([0., 0., 0.], dtype=float32), |
| 92 |
'phase_dir': array([0., 0., 0.], dtype=float32), |
| 93 |
'slice_dir': array([0., 0., 0.], dtype=float32), |
| 94 |
'patient_table_position': array([0., 0., 0.], dtype=float32), |
| 95 |
'idx': array((0, 0, 0, 0, 0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0, 0, 0]), |
| 96 |
dtype=[('kspace_encode_step_1', '<u2'), ('kspace_encode_step_2', '<u2'), ('average', '<u2'), ('slice', '<u2'), ('contrast', '<u2'), ('phase', '<u2'), ('repetition', '<u2'), ('set', '<u2'), ('segment', '<u2'), ('user', '<u2', (8,))]), |
| 97 |
'user_int': array([0, 0, 0, 0, 0, 0, 0, 0], dtype=int32), |
| 98 |
'user_float': array([0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)} |
| 99 |
``` |
| 100 |
|
| 101 |
## Reading MRD data in MATLAB |
| 102 |
A MATLAB package is also provided in this repository to facilitate easy usage of MRD files. To use it, add the ``matlab`` folder in this repository to the MATLAB path. The following code shows an example of getting the number of readout lines from a dataset and reading the first line of k-space data: |
| 103 |
```matlab |
| 104 |
>> dset = ismrmrd.Dataset('data.mrd'); |
| 105 |
>> nacq = dset.getNumberOfAcquisitions() |
| 106 |
nacq = |
| 107 |
128 |
| 108 |
>> acq = dset.readAcquisition(1) |
| 109 |
acq = |
| 110 |
Acquisition with properties: |
| 111 |
|
| 112 |
head: [1×1 ismrmrd.AcquisitionHeader] |
| 113 |
traj: {[0×256 single]} |
| 114 |
data: {[256×2 single]} |
| 115 |
>> dset.close(); |
| 116 |
>> acq.head |
| 117 |
ans = |
| 118 |
AcquisitionHeader with properties: |
| 119 |
|
| 120 |
version: 1 |
| 121 |
flags: 64 |
| 122 |
measurement_uid: 281 |
| 123 |
scan_counter: 1 |
| 124 |
acquisition_time_stamp: 23187639 |
| 125 |
physiology_time_stamp: [3×1 uint32] |
| 126 |
number_of_samples: 256 |
| 127 |
available_channels: 2 |
| 128 |
active_channels: 2 |
| 129 |
channel_mask: [16×1 uint64] |
| 130 |
discard_pre: 0 |
| 131 |
discard_post: 0 |
| 132 |
center_sample: 128 |
| 133 |
encoding_space_ref: 0 |
| 134 |
trajectory_dimensions: 0 |
| 135 |
sample_time_us: 15 |
| 136 |
position: [3×1 single] |
| 137 |
read_dir: [3×1 single] |
| 138 |
phase_dir: [3×1 single] |
| 139 |
slice_dir: [3×1 single] |
| 140 |
patient_table_position: [3×1 single] |
| 141 |
idx: [1×1 struct] |
| 142 |
user_int: [8×1 int32] |
| 143 |
user_float: [8×1 single] |
| 144 |
FLAGS: [1×1 struct] |
| 145 |
``` |
| 146 |
|
| 147 |
MATLAB also provides [native HDF5 support](https://www.mathworks.com/help/matlab/import_export/import-hdf5-files.html) which can be used to read the data without an external library: |
| 148 |
```matlab |
| 149 |
>> data = h5read('data.mrd', '/dataset/data') |
| 150 |
data = |
| 151 |
struct with fields: |
| 152 |
|
| 153 |
head: [1×1 struct] |
| 154 |
traj: {128×1 cell} |
| 155 |
data: {128×1 cell} |
| 156 |
|
| 157 |
>> data.head |
| 158 |
ans = |
| 159 |
struct with fields: |
| 160 |
|
| 161 |
version: [128×1 uint16] |
| 162 |
flags: [128×1 uint64] |
| 163 |
measurement_uid: [128×1 uint32] |
| 164 |
scan_counter: [128×1 uint32] |
| 165 |
acquisition_time_stamp: [128×1 uint32] |
| 166 |
physiology_time_stamp: [3×128 uint32] |
| 167 |
number_of_samples: [128×1 uint16] |
| 168 |
available_channels: [128×1 uint16] |
| 169 |
active_channels: [128×1 uint16] |
| 170 |
channel_mask: [16×128 uint64] |
| 171 |
discard_pre: [128×1 uint16] |
| 172 |
discard_post: [128×1 uint16] |
| 173 |
center_sample: [128×1 uint16] |
| 174 |
encoding_space_ref: [128×1 uint16] |
| 175 |
trajectory_dimensions: [128×1 uint16] |
| 176 |
sample_time_us: [128×1 single] |
| 177 |
position: [3×128 single] |
| 178 |
read_dir: [3×128 single] |
| 179 |
phase_dir: [3×128 single] |
| 180 |
slice_dir: [3×128 single] |
| 181 |
patient_table_position: [3×128 single] |
| 182 |
idx: [1×1 struct] |
| 183 |
user_int: [8×128 int32] |
| 184 |
user_float: [8×128 single] |
| 185 |
>> |
| 186 |
``` |