# Calibration, noise and coil compression

A reconstruction from a receive-coil array needs the sensitivity of each
channel, estimated from calibration data, and combines the channels with equal
weight, which is the maximum-likelihood combination only where their noise is
uncorrelated and of equal variance. Prewhitening makes the noise of the
channels so, and coil compression reduces their number. Each of the three is
expressed in a basis of the channels, and a quantity estimated in one basis
does not describe data in another. The transforms and the estimate are
computed by bartorch; the reconstruction plugin passes the estimate to
{func}`~pulserver.recon.coil_maps`, which returns stored maps only under the
conditions stated under *Reuse*.

## Signal model

The signal of each channel $c$ of $N_c$ is the image $x$ weighted by the
channel's sensitivity $S_c$ on the voxel grid, Fourier encoded, plus complex
Gaussian noise $\mathbf{n}$ of channel covariance
$\Psi = \mathrm{E}[\mathbf{n}\mathbf{n}^H]$: the encoding model bartorch's
[encoding](https://pulserver.github.io/bartorch/latest/explanation/encoding.html)
page states. A linear transform $B$ of the
channels, $\tilde{\mathbf{y}} = B\mathbf{y}$, follows the same model with the
sensitivities $B\mathbf{S}(\mathbf{r})$, $\mathbf{S} = (S_1, \dots, S_{N_c})$,
and the noise covariance $B\Psi B^H$. Two transforms are used:

- prewhitening, $B = W$ with $W\Psi W^H = I$, after which the noise of the
  channels is uncorrelated with unit variance;
- coil compression, $B = A$, the $n \times N_c$ matrix whose rows are the
  conjugated eigenvectors of the channel covariance of the calibration data
  that belong to its $n$ largest eigenvalues, so that the $n$ virtual channels
  are uncorrelated and ordered by decreasing power.

Data that is whitened and then compressed is transformed by $AW$, and
sensitivities estimated from it are $AW\mathbf{S}$ on the voxel grid of the
calibration k-space. A set of maps therefore describes only data with the same
channels in the same order, the same whitening, the same compression and the
same voxel grid at the same position and orientation.

## Noise and prewhitening

Readouts flagged `IS_NOISE_MEASUREMENT` sample $\mathbf{n}$ with the RF
off. From $K$ samples the covariance is estimated as
$\hat\Psi = (K-1)^{-1}\sum_k \mathbf{n}_k\mathbf{n}_k^H$, and $W = L^{-1}$ for
its Cholesky factorisation $\hat\Psi = LL^H$. Estimating $\hat\Psi$ takes more
noise samples than channels.

The variance of the noise in a sample is inversely proportional to the dwell
time. A readout of dwell time $\tau$ is multiplied by $\sqrt{\tau/\tau_0}\,W$,
$\tau_0$ being the dwell time of the noise readouts, and by $W$ alone where
either dwell time is not stated. No other difference between the noise
measurement and the readout is modelled.

{class}`~pulserver.recon.Prewhiten` takes $W$ from the first of these:

1. the noise readouts that precede the first readout of any other kind in the
   stream;
2. the exam's `NOISE_COVARIANCE`, stored by a noise series, when its coil
   labels are those of the header;
3. none, in which case readouts pass unchanged and a warning is logged once,
   or {class}`~pulserver.recon.MissingCalibration` is raised for
   `required=True`.

Noise readouts after the first readout of another kind are consumed and not
used. The gadget applies $W$ on arrival, so the imaging and the calibration
readouts of every unit are whitened alike.

## Coil compression

The basis $A$ is that of the channel covariance
$\sum_k \mathbf{y}_k\mathbf{y}_k^H$ over calibration samples. One basis is
estimated per stream, and every readout or unit is projected onto it, so the
virtual channels of successive units are the same. The basis and the
projection are computed on the host. It is estimated in one of two places:

- as the readouts arrive, by the gadget
  {class}`~pulserver.recon.CompressChannels`, from the calibration readouts
  (`REF` or `IMA`) of the first contrast that lead the scan. They are held
  until the first readout that is not calibration, then placed, projected,
  ahead of it; every later readout is projected as it arrives. The buffers then
  hold $n$ channels, which is what lets the units of every echo of a
  multi-echo volume be open together;
- as a unit closes, by {class}`~pulserver.recon.CoilCompression`, from the
  placed samples of the calibration k-space of the first unit the stream
  compresses, or of its imaging k-space where that unit holds none. This is
  the place for a scan whose calibration lines are spread among its imaging
  lines, such as an echo train.

If the signal of the channels lies in a subspace of dimension at most $n$,
compression discards none of it, and SENSE with compressed data and compressed
maps reconstructs the image of the full set of channels to round-off.
Otherwise the discarded signal is that of the $N_c - n$ smallest eigenvalues.
Maps estimated in a series of its own from uncompressed data are transformed
into the basis of a later series by the same projection as its k-space,
$A\mathbf{S}$; maps estimated from compressed data are in the basis of their
own series and are not transformed.

## Coil sensitivity maps

### Calibration k-space

The calibration k-space of a unit is {attr}`ReconData.ref
<pulserver.recon.ReconData.ref>`. It comes from one of three arrangements.

| Arrangement | Readouts | Maps are estimated by |
| --- | --- | --- |
| Embedded calibration | Lines of the imaging space flagged `IS_PARALLEL_CALIBRATION_AND_IMAGING` | The imaging unit, which holds both `data` and `ref` |
| Calibration unit | Readouts flagged `IS_PARALLEL_CALIBRATION` in a unit with no imaging readouts, as in an encoding space of their own | That unit, whose `data.data` is `None`; the imaging units of the same slice take the maps from the stream |
| Calibration series | A series of its own | The reconstruction of that series, which stores the maps in the exam |

The estimate receives the calibration k-space of a Cartesian encoding space on
the host, zero-filled onto the whole grid of the space, the lines its readouts
cover being at their grid positions, and returns maps on that grid. For a
non-Cartesian space {func}`~pulserver.recon.coil_maps` raises `ValueError`.

### Resolution

{func}`~pulserver.recon.coil_maps` returns the maps of a unit from the first of
these sources, the location being the unit's `slice` counter, and its `set`
counter where that is not 0:

1. The unit's own calibration k-space, from which the maps are estimated and
   stored for the slice, or for the slice and set, replacing any stored there.
2. The maps stored in the stream for the slice and set, then for the slice.
3. The exam's `COIL_SENSITIVITIES`, which holds one set of maps, stored by a
   calibration series.
4. No source: {class}`~pulserver.recon.MissingCalibration` is raised, or
   `None` returned for `required=False`.

A sequence with calibration readouts of its own always uses them; a sequence
designed without them takes the maps of the exam's calibration series. Maps
reach the exam only from a reconstruction plugin that assigns
`context.coil_sensitivities`; maps a series estimates from its own calibration
readouts stay in that series.

The slice is the location because a calibration unit and the imaging units that
use its maps may be in different encoding spaces. The set separates maps
estimated from calibration readouts distorted as the images of their set are:
a FLEET calibration ahead of an EPI reads its lines in both phase-encode
polarities, set 0 forward and set 1 reversed, so each polarity of the EPI is
unfolded by maps displaced as its own image is, and a calibration of set 0
alone, such as a gradient echo, serves every set. A unit that needs maps before
the unit that estimates them has closed finds none in the first three sources.

### Reuse

Maps stored in the stream are returned only to a unit for which every field of
the {class}`~pulserver.recon.CoilSensitivities` is equal to that of the unit.
The fields are compared in this order, and the first that differs is reported.

| Field | Equal to |
| --- | --- |
| `coils` | The coil labels of the header in channel order |
| `noise` | The identifier of the prewhitening in `context.noise`, or `None` on both sides |
| `basis` | The identifier of the basis in `context.coil_compression`, or `None` on both sides |
| `geometry.frame_of_reference` | The `frameOfReferenceUID` of the header |
| `geometry.table_position`, `geometry.position`, `geometry.read_dir`, `geometry.phase_dir`, `geometry.slice_dir` | The same fields of the reference acquisition of the unit's k-space, in the units the acquisition states |
| `geometry.fov`, `geometry.matrix` | The field of view in m and the matrix of the reconstruction space of the unit's encoding space |
| `geometry.grid`, `geometry.extent` | The shape of the voxel axes of the whole k-space of the unit's encoding space, and its size in mm |

Maps of the exam, measured by another series, are returned to a unit with the
same `coils`, `noise`, `frame_of_reference` and `table_position`. On another
position, orientation, grid or extent they are resliced onto the unit's grid
by {func}`bartorch.tools.reslice`, which takes the transform between the grids
from the two geometries, without registration, and interpolates the real and
imaginary parts linearly. A unit whose grid reaches outside the calibration
volume is refused, as is one whose geometry or that of the maps does not state
the orientation. Maps with `basis` `None` are projected onto the basis of a
compressed unit; maps of a compressed series serve only units compressed by the
same basis.

`method` and `source`, the estimate and the `measurementID` of the series that
measured the maps, are recorded and appear in a refusal; they are not compared.
The comparison establishes that the maps and the unit share the listed
quantities. It does not test the accuracy of the maps.

Maps that are not returned are named, each source with why it was not used, in
the message of {class}`~pulserver.recon.MissingCalibration`:

```text
no coil sensitivities for slice 0; sources consulted:
  this unit: it holds no calibration readouts
  this series, slice 0: no maps stored
  this exam: this unit's grid reaches outside the calibration volume (estimated by nlinv_maps from measurement 7)
```

## Interfaces

| Quantity | Interface | State |
| --- | --- | --- |
| Whitening $W$ | {class}`~pulserver.recon.Prewhiten`, a gadget | `context.noise`; the exam key {obj}`~pulserver.recon.NOISE_COVARIANCE` |
| Compression basis $A$ | {class}`~pulserver.recon.CompressChannels`, a gadget; {class}`~pulserver.recon.CoilCompression`, called from `recon` | `context.coil_compression` |
| Maps $\mathbf{S}$ | {func}`~pulserver.recon.coil_maps`, called from `recon`; {class}`~pulserver.recon.CoilSensitivities` | `context.coil_maps` by slice; the exam key {obj}`~pulserver.recon.COIL_SENSITIVITIES`, read and written as `context.coil_sensitivities` |

The state in `context` belongs to one stream. What a series leaves to the next
is what it stores in `context.exam` under the exam keys, which
{doc}`../../explanations/reconstruction` describes. A calibration series publishes its whitening
with {meth}`Prewhiten.publish <pulserver.recon.Prewhiten.publish>` and its maps
by assigning `context.coil_sensitivities`.

Whitening runs on arrival, in the `gadgets` of the plugin. Within `recon`, a
unit is compressed before its maps are requested, because the maps record the
basis in force when they are estimated. The whitening, the compression basis
and the estimate are bartorch's (`tools.whiten`, `tools.cc` and `tools.ccapply`,
and the estimate the plugin names, such as `apps.nlinv_maps`); the interfaces
above hold their results, compare them and route them between units and series.
{doc}`../../user-guide/reconstruction-plugins` shows the calls.

## See also

* {doc}`../../user-guide/reconstruction-plugins` — requesting maps, prewhitening and compressing in a plugin.
* {doc}`../../explanations/reconstruction` — units, the exam cache and workers.
* {doc}`../../api/recon` — the interfaces named above.
