Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Complex Number Handling

Handling Complex MRI/MRS Data

Magnetic resonance data is inherently complex-valued, but many standard tools lack native support for complex numbers. Splitting this data into separate real and imaginary channels is frequently required for:

The xmris accessor provides dedicated utilities (to_real_imag and to_complex) to safely reshape and reconstruct complex xarray.DataArray objects while preserving all metadata and physical coordinates.

1. Splitting Complex Data into Channels

Let’s generate a synthetic complex Free Induction Decay (FID) signal.

Original Shape: (512,)
Original Dtype: complex128

By calling .xmr.to_real_imag(), the array is expanded with a new dimension containing the real and imaginary components.


Split Shape: (512, 2)
Split Dtype: float64
Loading...

The array cleanly expanded from (512,) to (512, 2). Because it remains a standard xarray.DataArray, we can easily plot the two channels side-by-side.

<Figure size 1200x600 with 1 Axes>

2. Customizing the Split Dimension

If your specific ML pipeline expects the channel dimension to have a specific name (e.g., "channel" instead of "component"), you can pass these arguments directly to the function.

Custom Dimension: ('time', 'channel')

3. Reconstructing the Complex Array

After processing the split channels (e.g., passing them through a neural network), you can seamlessly collapse the dimension back down to a standard complex array for downstream signal processing (like an FFT) using .xmr.to_complex().

Shape Restored: (512,)
Data Type Restored: complex128
Exact Mathematical Recovery: True