Metadata-Version: 2.5
Name: lofi-aug
Version: 0.1.0
Summary: Low-field MRI corruption: make good scans look like low-field open-magnet scans, with labels kept exact.
Project-URL: Homepage, https://github.com/lochungtin/lofi-aug
Project-URL: Repository, https://github.com/lochungtin/lofi-aug.git
Project-URL: Bug Tracker, https://github.com/lochungtin/lofi-aug/issues
Project-URL: Changelog, https://github.com/lochungtin/lofi-aug/blob/main/CHANGELOG.md
Author-email: Chung Tin Lo <lochungtin@gmail.com>
License-Expression: MIT
License-File: LICENSE
Keywords: augmentation,low-field,mri,segmentation,simulation
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Image Processing
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: numpy>=1.24
Requires-Dist: pyyaml>=6.0
Requires-Dist: torch>=2.1
Provides-Extra: dev
Requires-Dist: matplotlib>=3.7; extra == 'dev'
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: nibabel>=5.0; extra == 'dev'
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Requires-Dist: types-pyyaml; extra == 'dev'
Provides-Extra: io
Requires-Dist: nibabel>=5.0; extra == 'io'
Provides-Extra: monai
Requires-Dist: monai>=1.3; extra == 'monai'
Provides-Extra: torchio
Requires-Dist: torchio>=0.19; extra == 'torchio'
Provides-Extra: viz
Requires-Dist: matplotlib>=3.7; extra == 'viz'
Description-Content-Type: text/markdown

# LOFI

**Lo**w-**Fi**eld MRI corruption. LOFI makes good scans look like low-field open-magnet scans, for
training models that must work on the bad ones. It only changes intensities, so anatomy stays where
it is and labels stay exact.

[![Branch CI](https://github.com/lochungtin/lofi-aug/actions/workflows/branch-ci.yml/badge.svg)](https://github.com/lochungtin/lofi-aug/actions/workflows/branch-ci.yml)

> Status: pre-release.

## Install

```bash
pip install lofi-aug            # core
pip install "lofi-aug[io]"      # NIfTI support (needed by the command line)
```

Other extras: `viz`, `monai`, `torchio`.

## How it works

A forward model runs in acquisition order, so the artefacts emerge rather than being painted on:

1. **Normalise and map contrast.** The foreground median becomes 1. An optional monotonic map moves
   the tissue histogram to the target's.
2. **Frame.** Pad to the scanner's canvas.
3. **Select slices.** Box-average thick slabs. Each acquisition's slabs can be displaced.
4. **Shade.** Multiply by a smooth receive sensitivity.
5. **Sample k-space.** Add a smooth phase, then crop k-space to the acquired matrix with a taper.
6. **Corrupt k-space.** Add line-by-line phase errors (ghosting), optional EMI tones, then complex
   noise, less what the source already carries at that voxel size.
7. **Magnitude.** This is where the Rician floor comes from.
8. **Resample and combine.** Linear onto the output grid, then average the acquisitions.
9. **FOV.** Warped edge, exact zeros outside it, and a fade towards it.

Scanner-specific choices live in profiles. `low_field_breast` is bundled and fitted to real scans.

## Command line

```bash
lofi profiles
lofi corrupt good.nii.gz --label good_label.nii.gz --profile low_field_breast --n 4 --out out/
lofi measure scan_*.nii.gz
lofi calibrate --target bad_*.nii.gz --sources good_*.nii.gz --out my_scanner.yaml
```

## Python

```python
from lofi.augment import LowFieldCorrupt

corrupt = LowFieldCorrupt("low_field_breast", seed=0)
sample = corrupt({"image": image, "label": label, "spacing": (0.8, 0.8, 1.1)})  # (C, *spatial)
```

The result also holds `"fov"`, and `"spacing"` becomes the output grid's. Labels are moved onto that
grid with the image and set to background outside the FOV.

Framework adapters keep the input grid, since those pipelines fix it themselves. Apply them before
random cropping:

- MONAI: `lofi.integrations.monai.LowFieldCorruptd(["image", "label"], "low_field_breast")`.
- TorchIO: `lofi.integrations.torchio.RandomLowField("low_field_breast")`.

With raw k-space and a noise-only scan, `lofi.physics.corrupt_kspace` applies the field-strength
model directly, including the separate scaling of coil and sample noise.

## Development

```bash
uv sync --extra dev --extra io
uv run pytest
uv run ruff check . && uv run ruff format --check .
uv run mypy
```

`CLAUDE.md` records the conventions and the design as built. The research behind the package
(calibration study, research notes, downstream segmentation studies) is kept outside it, in the
`_lofi` folder of `lochungtin/PhD_Codebase`.

## Licence

MIT.
