-
Notifications
You must be signed in to change notification settings - Fork 3
✨ Expose typed Python API for the seven wk-* operations #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ec0cf74
:sparkles: Expose typed Python API for the seven wk-* operations
vanandrew 7817620
:wrench: Loosen Codecov thresholds (1% project / 90% patch)
vanandrew 51e6c7a
:fire: remove compose.yml
vanandrew c8cf620
:bug: Centralise noise-frame and metadata validation in _metadata hel…
vanandrew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| coverage: | ||
| status: | ||
| project: | ||
| default: | ||
| target: auto | ||
| threshold: 1% | ||
| patch: | ||
| default: | ||
| target: 90% | ||
| threshold: 1% |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| """Typed Python entry points for the seven warpkit operations. | ||
|
|
||
| Every ``wk-*`` CLI tool is mirrored here as a typed Python function so | ||
| library callers (e.g. nipype interfaces, fmriprep) can drive warpkit without | ||
| shelling out or fabricating ``argv``. Each function takes keyword-only | ||
| arguments, raises :class:`ValueError` on validation problems, writes its | ||
| outputs to disk, and returns a frozen dataclass with the absolute paths of | ||
| the written NIfTIs. | ||
|
|
||
| Mapping CLI -> Python: | ||
|
|
||
| * ``wk-medic`` -> :func:`medic` -> :class:`MedicResult` | ||
| * ``wk-unwrap-phase`` -> :func:`unwrap_phase` -> :class:`UnwrapPhaseResult` | ||
| * ``wk-compute-fieldmap`` -> :func:`compute_fieldmap` -> :class:`ComputeFieldmapResult` | ||
| * ``wk-apply-warp`` -> :func:`apply_warp` -> :class:`ApplyWarpResult` | ||
| * ``wk-convert-warp`` -> :func:`convert_warp` -> :class:`ConvertWarpResult` | ||
| * ``wk-convert-fieldmap`` -> :func:`convert_fieldmap` -> :class:`ConvertFieldmapResult` | ||
| * ``wk-compute-jacobian`` -> :func:`compute_jacobian` -> :class:`ComputeJacobianResult` | ||
|
|
||
| The CLI flag-name → Python kwarg mapping is the obvious dash-to-underscore | ||
| transform; the only difference is ``--TEs`` (kept for MR convention) → | ||
| ``tes`` (lowercase, per repo style). | ||
| """ | ||
|
|
||
| from .scripts.apply_warp import ApplyWarpResult, apply_warp | ||
| from .scripts.compute_fieldmap import ComputeFieldmapResult, compute_fieldmap | ||
| from .scripts.compute_jacobian import ComputeJacobianResult, compute_jacobian | ||
| from .scripts.convert_fieldmap import ConvertFieldmapResult, convert_fieldmap | ||
| from .scripts.convert_warp import ConvertWarpResult, convert_warp | ||
| from .scripts.medic import MedicResult, medic | ||
| from .scripts.unwrap_phase import UnwrapPhaseResult, unwrap_phase | ||
|
|
||
| __all__ = [ | ||
| "ApplyWarpResult", | ||
| "ComputeFieldmapResult", | ||
| "ComputeJacobianResult", | ||
| "ConvertFieldmapResult", | ||
| "ConvertWarpResult", | ||
| "MedicResult", | ||
| "UnwrapPhaseResult", | ||
| "apply_warp", | ||
| "compute_fieldmap", | ||
| "compute_jacobian", | ||
| "convert_fieldmap", | ||
| "convert_warp", | ||
| "medic", | ||
| "unwrap_phase", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| """Shared acquisition-metadata helpers for the warpkit script entry points. | ||
|
|
||
| Centralises: | ||
|
|
||
| * coercing user-supplied images (``Path`` / ``str`` / ``Nifti1Image``) into | ||
| ``Nifti1Image`` objects, | ||
| * loading echo time / total readout time / phase encoding direction from | ||
| BIDS-style JSON sidecars, | ||
| * the "either ``--metadata`` or direct args" mutex/either-or check shared by | ||
| ``wk-medic``, ``wk-unwrap-phase``, and ``wk-compute-fieldmap``. | ||
|
|
||
| Validation errors raise :class:`ValueError`; the CLI shims forward those to | ||
| ``parser.error`` so the user-visible behaviour (``SystemExit(2)``) is | ||
| preserved. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from collections.abc import Sequence | ||
| from os import PathLike | ||
| from typing import cast | ||
|
|
||
| import nibabel as nib | ||
|
|
||
|
|
||
| def ensure_image(x: PathLike[str] | str | nib.Nifti1Image) -> nib.Nifti1Image: | ||
| """Coerce a path or in-memory image into a ``Nifti1Image``.""" | ||
| if isinstance(x, nib.Nifti1Image): | ||
| return x | ||
| return cast(nib.Nifti1Image, nib.load(str(x))) | ||
|
|
||
|
|
||
| def ensure_images( | ||
| xs: Sequence[PathLike[str] | str | nib.Nifti1Image], | ||
| ) -> list[nib.Nifti1Image]: | ||
| return [ensure_image(x) for x in xs] | ||
|
|
||
|
|
||
| def load_acquisition_from_metadata( | ||
| metadata_paths: Sequence[PathLike[str] | str], | ||
| *, | ||
| require_trt_pe: bool = True, | ||
| ) -> tuple[list[float], float | None, str | None]: | ||
| """Read EchoTime (s → ms) — and optionally TotalReadoutTime (s) and | ||
| PhaseEncodingDirection — from BIDS-style JSON sidecars. | ||
|
|
||
| Per-echo ``EchoTime`` is read from each file; ``TotalReadoutTime`` and | ||
| ``PhaseEncodingDirection`` are taken from the first. When | ||
| ``require_trt_pe=False`` the latter two are skipped so callers that only | ||
| need echo times (e.g. ``unwrap_phase``) accept sidecars that omit them. | ||
| Missing required keys raise :class:`ValueError`. | ||
| """ | ||
| metadatas = [] | ||
| for j in metadata_paths: | ||
| with open(j) as f: | ||
| metadatas.append(json.load(f)) | ||
| try: | ||
| tes_ms = [float(m["EchoTime"]) * 1000 for m in metadatas] | ||
| except KeyError: | ||
| raise ValueError( | ||
| "metadata sidecar is missing required key: 'EchoTime'." | ||
| ) from None | ||
| if not require_trt_pe: | ||
| return tes_ms, None, None | ||
| missing = [ | ||
| k | ||
| for k in ("TotalReadoutTime", "PhaseEncodingDirection") | ||
| if k not in metadatas[0] | ||
| ] | ||
| if missing: | ||
| raise ValueError( | ||
| "metadata sidecar is missing required key(s): " | ||
| f"{', '.join(repr(k) for k in missing)}." | ||
| ) | ||
| trt = float(metadatas[0]["TotalReadoutTime"]) | ||
| ped = str(metadatas[0]["PhaseEncodingDirection"]) | ||
| return tes_ms, trt, ped | ||
|
|
||
|
|
||
| def resolve_acquisition( | ||
| *, | ||
| metadata: Sequence[PathLike[str] | str] | None, | ||
| tes: Sequence[float] | None, | ||
| total_readout_time: float | None = None, | ||
| phase_encoding_direction: str | None = None, | ||
| require_trt_pe: bool = True, | ||
| ) -> tuple[list[float], float | None, str | None]: | ||
| """Resolve echo times / TRT / PED from either BIDS metadata or direct args. | ||
|
|
||
| Mirrors the either-or / mutex logic in the CLI scripts. Set | ||
| ``require_trt_pe=False`` for callers that only need echo times (e.g. | ||
| ``unwrap_phase``); the error message then matches that script's wording. | ||
|
|
||
| Error messages reference the dash-form CLI flags (``--metadata``, | ||
| ``--TEs``, ...) so CLI tests continue to match; nipype/library callers | ||
| will see the same text via ``ValueError``. | ||
| """ | ||
| flag_map = { | ||
| "tes": "--TEs", | ||
| "total_readout_time": "--total-readout-time", | ||
| "phase_encoding_direction": "--phase-encoding-direction", | ||
| } | ||
| if require_trt_pe: | ||
| direct_vals = { | ||
| "tes": tes, | ||
| "total_readout_time": total_readout_time, | ||
| "phase_encoding_direction": phase_encoding_direction, | ||
| } | ||
| else: | ||
| direct_vals = {"tes": tes} | ||
| direct_supplied = [k for k, v in direct_vals.items() if v is not None] | ||
|
|
||
| if metadata is not None and direct_supplied: | ||
| names = ", ".join(flag_map[k] for k in direct_supplied) | ||
| raise ValueError( | ||
| f"--metadata is mutually exclusive with {names}; pass one or the " | ||
| "other, not both." | ||
| ) | ||
| if metadata is None and len(direct_supplied) != len(direct_vals): | ||
| if require_trt_pe: | ||
| missing = [flag_map[k] for k in direct_vals if k not in direct_supplied] | ||
| raise ValueError( | ||
| "either --metadata or all of --TEs, --total-readout-time, and " | ||
| f"--phase-encoding-direction must be provided (missing: {', '.join(missing)})." | ||
| ) | ||
| raise ValueError("either --metadata or --TEs must be provided.") | ||
|
|
||
| if metadata is not None: | ||
| return load_acquisition_from_metadata(metadata, require_trt_pe=require_trt_pe) | ||
|
|
||
| return list(tes or []), total_readout_time, phase_encoding_direction | ||
|
|
||
|
|
||
| def trim_noise_frames(images: list[nib.Nifti1Image], n: int) -> list[nib.Nifti1Image]: | ||
| """Trim the last ``n`` frames from each 4D image. Returns the input list | ||
| unchanged when ``n == 0``. | ||
|
|
||
| When ``n > 0`` each image must be 4D with strictly more than ``n`` frames; | ||
| otherwise ``[..., :-n]`` would either chop the Z dimension of a 3D volume | ||
| or yield an empty 4D series that crashes downstream consumers. Both raise | ||
| :class:`ValueError`. | ||
| """ | ||
| if n == 0: | ||
| return images | ||
| if n < 0: | ||
| raise ValueError(f"noise_frames must be non-negative; got {n}.") | ||
| for idx, img in enumerate(images): | ||
| if img.ndim != 4: | ||
| raise ValueError( | ||
| f"noise_frames={n} requires 4D images; image #{idx} has " | ||
| f"ndim={img.ndim}." | ||
| ) | ||
| n_frames = img.shape[-1] | ||
| if n >= n_frames: | ||
| raise ValueError( | ||
| f"noise_frames={n} would leave 0 frames in image #{idx} " | ||
| f"(has {n_frames} frame(s))." | ||
| ) | ||
| return [ | ||
| nib.Nifti1Image(img.dataobj[..., :-n], img.affine, img.header) for img in images | ||
| ] | ||
|
vanandrew marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.