Preprocessing#

Denoising and motion-correction helpers for image stacks.

  • denoise_filter applies Gaussian/median/uniform filtering on N-D arrays.

  • regularized_denoise runs TV, higher-order TV, and smoothness regularizers using project optimizers. See [Rudin1992] for the TV background.

  • denoise_bm3d and denoise_bm4d wrap the bundled external denoiser packages [Dabov2007] [Maggioni2013].

  • linear_drift_correct_framestack and linear_drift_correct_dataset fit one linear transform-parameter model over a frame stack for long-term drift.

  • TranslationTransform, Rigid2DTransform, Similarity2DTransform, and Affine2DTransform provide differentiable JAX transforms for that drift fit.

  • register_image and motion_correct_framestack provide SimpleITK-backed image registration.

  • motion_correct_dataset handles MRax experiment dimensions. Use register_experiment_to_reference to align an experiment to a reference experiment image dataset.

  • resample_image applies a SimpleITK transform to another image on a chosen reference image grid.

mrax.preprocessing.denoise.denoise_filter(array: Any, *, method: str = 'gaussian', sigma: float | tuple[float, ...] = 1.0, size: int | tuple[int, ...] = 3, mode: str = 'reflect', truncate: float = 4.0, preserve_nan: bool = False, complex_mode: str = 'real_imag') → Array#

Denoise with simple multidimensional filters (JAX implementation).

mrax.preprocessing.denoise.regularized_denoise(data: Any, *, regularizer: str | Callable | None = 'tv', weight: float = 0.1, num_steps: int = 50, step_size: float = 0.1, optimizer_name: str = 'adam', axes: tuple[int, ...] | None = None, mask: Any | None = None, init: Any | None = None, complex_mode: str = 'real_imag', return_history: bool = False, device: str | Device = 'cpu') → Array | tuple[Array, list[float] | dict[str, list[float]]]#

Denoise using a regularizer optimized with the project optimizers.

mrax.preprocessing.denoise.denoise_bm3d(array: Any, *args, sigma: float | None = None, stage: str = 'full', noise_mask=None, noise_model: str = 'auto', noise_coils: int = 1, **kwargs) → Array#

BM3D denoising via the external bm3d package.

sigma is the noise standard deviation in input intensity units. If unset, estimate it from local detrended MRI patches, excluding zero-filled background. noise_mask, noise_model and noise_coils select the estimation region and assumptions. See mrax.preprocessing.estimate_noise(). Pass package-specific options through kwargs.

mrax.preprocessing.denoise.denoise_bm4d(array: Any, *args, sigma: float | None = None, stage: str = 'full', noise_mask=None, noise_model: str = 'auto', noise_coils: int = 1, **kwargs) → Array#

BM4D denoising via the external bm4d package.

sigma is the noise standard deviation in input intensity units. If unset, estimate it from local detrended MRI patches, excluding zero-filled background. noise_mask, noise_model and noise_coils select the estimation region and assumptions. See mrax.preprocessing.estimate_noise(). Pass package-specific options through kwargs.

mrax.preprocessing.drift.linear_drift_correct_framestack(frames: ~numpy.ndarray, *, transform: ~mrax.preprocessing.drift.ImageTransform | ~typing.Callable[[~jax.Array, ~jax.Array], ~jax.Array] | None = None, metric: ~typing.Callable[[~jax.Array, ~jax.Array], ~jax.Array | float] = <function normalized_cross_correlation>, reference: str | int | ~numpy.ndarray = 'first', times: ~typing.Iterable[float] | None = None, anchor_frame: int | None = None, identity_parameters: ~typing.Iterable[float] | None = None, fit_intercept: bool = False, frame_weights: ~typing.Iterable[float] | None = None, mask: ~numpy.ndarray | ~jax.Array | None = None, normalize_intensity: bool = False, max_parameter_change: float | ~typing.Iterable[float] | None = None, optimizer_name: str = 'adam', step_size: float = 0.05, num_steps: int = 200, optimizer_factory=None, optimizer=None, device: str | ~jaxlib._jax.Device = 'cpu') → FrameStackLinearDriftResult#

Fit and apply a global linear transform-parameter drift to a frame stack.

The JAX optimizer fits the coefficients of the shared transform curve identity + design(time) @ coefficients and evaluates the chosen image transform and similarity metric for each frame under that shared model. By default the line is anchored at identity for the reference frame or the first frame. Set fit_intercept=True to fit a free line. mask restricts every objective and similarity evaluation to selected reference-grid pixels. Custom metrics and transforms used in this objective must be JAX-compatible. When mask is passed the metric must accept a mask= keyword. Motion estimation uses linear interpolation when nearest-neighbour output is selected. The lowest finite objective determines the retained coefficients.

mrax.preprocessing.drift.linear_drift_correct_dataset(experiment: Experiment, dataset: DataSet, *, dimensions: Iterable[str] | None = None, reference: str | int | ndarray = 'first', times: Iterable[float] | None = None, mask: ndarray | None = None, mask_dims: Iterable[str] | None = None, device: str | Device = 'cpu', **drift_kwargs) → LinearDriftCorrectionResult#

Fit grouped linear drift corrections and restore MRax dataset dimensions.

Named masks follow each reference group. Slice packages stay independent.

mrax.preprocessing.drift.apply_image_transform_stack(frames: ndarray, transform: ImageTransform | Callable[[Array, Array], Array], parameters: ndarray) → ndarray#

Apply one per-frame image-transform parameter vector to a frame stack.

class mrax.preprocessing.drift.TranslationTransform(order: int | str = 'linear', mode: str = 'constant', cval: float = 0.0)#

N-D JAX subpixel translation in MRax array-axis order.

Parameters are shifts in array-axis order. For a 2D (x, y) MRax image, a parameter vector (dx, dy) moves the sampled image along those axes. order accepts "linear"/1 or "nearest"/0.

class mrax.preprocessing.drift.Rigid2DTransform(order: int | str = 'linear', mode: str = 'constant', cval: float = 0.0)#

2D translation plus rotation about the image center.

Parameters are (dx, dy, angle_radians) in MRax array-axis order. order accepts "linear"/1 or "nearest"/0.

class mrax.preprocessing.drift.Similarity2DTransform(order: int | str = 'linear', mode: str = 'constant', cval: float = 0.0)#

2D translation, rotation, and isotropic log-scale.

Parameters are (dx, dy, angle_radians, log_scale). order accepts "linear"/1 or "nearest"/0.

class mrax.preprocessing.drift.Affine2DTransform(order: int | str = 'linear', mode: str = 'constant', cval: float = 0.0)#

2D translation plus a small affine update about the image center.

Parameters are (dx, dy, a00, a01, a10, a11) and the affine matrix is eye(2) + [[a00, a01], [a10, a11]]. order accepts "linear"/1 or "nearest"/0.

mrax.preprocessing.motion.register_image(fixed: ndarray, moving: ndarray, *, registration_method: Any | Callable[[Any, Any], Any] | None = None, initial_transform: Any | Callable[[Any, Any], Any] | None = None, transform_in_place: bool = False, interpolator: str | Any = 'linear', default_value: float = 0.0, output_pixel_type: Any | None = None, normalize_intensity: bool = False, fixed_mask: ndarray | None = None, moving_mask: ndarray | None = None, moving_pre_transform: Any | None = None, fixed_spacing: Iterable[float] | None = None, validate_alignment: bool = False, max_displacement_voxels: float | None = None) → ImageRegistrationResult#

Register one moving image to one fixed image with SimpleITK.

The default is rigid Mattes mutual-information registration. Pass a configured SimpleITK.ImageRegistrationMethod or a factory accepting (fixed_image, moving_image) to use any SimpleITK registration configuration. initial_transform can likewise be a transform or a factory when it should be set by the wrapper. moving_pre_transform maps fixed voxel indices into the moving grid. fixed_spacing gives physical voxel sizes in SimpleITK column-first order. Optimization uses physical distances. The returned transform uses voxel indices, and the final output is sampled once from the original moving data. validate_alignment rejects large losses of normalized mutual information or signal support in the reference region. This is a heuristic sanity check.

mrax.preprocessing.motion.motion_correct_framestack(frames: ndarray, *, reference: str | int | ndarray = 'first', progress_callback: Callable[[int, int], None] | None = None, **registration_kwargs) → FrameStackMotionResult#

Register a (frame, *spatial) stack to one reference image.

mrax.preprocessing.motion.motion_correct_dataset(experiment: Experiment, dataset: DataSet, *, dimensions: Iterable[str] | None = None, reference: str | int | ndarray = 'first', estimation_dimensions: Iterable[str] = (), progress_callback: Callable[[int, int], None] | None = None, mask: ndarray | None = None, mask_dims: Iterable[str] | None = None, **registration_kwargs) → MotionCorrectionResult#

Motion-correct MRax dataset data while preserving experiment dimensions.

With dimensions=None all non-spatial experiment dimensions are collapsed into one frame stack in experiment order. When dimensions names a subset such as ("t",), each combination of the remaining dimensions receives a separate registration stack before the corrected data is expanded back. estimation_dimensions averages magnitude along selected group dimensions to estimate motion. The resulting transforms resample each original image. Slice packages stay independent.

mrax.preprocessing.motion.register_experiment_to_reference(reference_experiment: Experiment, reference_dataset: DataSet, moving_experiment: Experiment, moving_dataset: DataSet, *, reference_frame: str | int | ndarray = 'first', moving_frame: str | int | ndarray = 'first', interpolator: str | Any = 'linear', default_value: float = 0.0, output_pixel_type: Any | None = None, **registration_kwargs) → ExperimentRegistrationResult#

Register one MRax experiment to a reference experiment image.

The transform is estimated from moving_frame to reference_frame and then applied to every moving frame. The returned experiment keeps the moving non-spatial dimensions and adopts the reference spatial grid.

mrax.preprocessing.motion.resample_image(moving: ndarray, reference: ndarray, transform, *, interpolator: str | Any = 'linear', default_value: float = 0.0, output_pixel_type: Any | None = None) → ndarray#

Resample one image onto a reference image grid with a SimpleITK transform.