Preprocessing#
Denoising and motion-correction helpers for image stacks.
denoise_filterapplies Gaussian/median/uniform filtering on N-D arrays.regularized_denoiseruns TV, higher-order TV, and smoothness regularizers using project optimizers. See [Rudin1992] for the TV background.denoise_bm3danddenoise_bm4dwrap the bundled external denoiser packages [Dabov2007] [Maggioni2013].linear_drift_correct_framestackandlinear_drift_correct_datasetfit one linear transform-parameter model over a frame stack for long-term drift.TranslationTransform,Rigid2DTransform,Similarity2DTransform, andAffine2DTransformprovide differentiable JAX transforms for that drift fit.register_imageandmotion_correct_framestackprovide SimpleITK-backed image registration.motion_correct_datasethandles MRax experiment dimensions. Useregister_experiment_to_referenceto align an experiment to a reference experiment image dataset.resample_imageapplies 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
bm3dpackage.sigmais the noise standard deviation in input intensity units. If unset, estimate it from local detrended MRI patches, excluding zero-filled background.noise_mask,noise_modelandnoise_coilsselect the estimation region and assumptions. Seemrax.preprocessing.estimate_noise(). Pass package-specific options throughkwargs.
- 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
bm4dpackage.sigmais the noise standard deviation in input intensity units. If unset, estimate it from local detrended MRI patches, excluding zero-filled background.noise_mask,noise_modelandnoise_coilsselect the estimation region and assumptions. Seemrax.preprocessing.estimate_noise(). Pass package-specific options throughkwargs.
- 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) @ coefficientsand 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. Setfit_intercept=Trueto fit a free line.maskrestricts every objective and similarity evaluation to selected reference-grid pixels. Custom metrics and transforms used in this objective must be JAX-compatible. Whenmaskis passed the metric must accept amask=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.orderaccepts"linear"/1or"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.orderaccepts"linear"/1or"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).orderaccepts"linear"/1or"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 iseye(2) + [[a00, a01], [a10, a11]].orderaccepts"linear"/1or"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.ImageRegistrationMethodor a factory accepting(fixed_image, moving_image)to use any SimpleITK registration configuration.initial_transformcan likewise be a transform or a factory when it should be set by the wrapper.moving_pre_transformmaps fixed voxel indices into the moving grid.fixed_spacinggives 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_alignmentrejects 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=Noneall non-spatial experiment dimensions are collapsed into one frame stack in experiment order. Whendimensionsnames a subset such as("t",), each combination of the remaining dimensions receives a separate registration stack before the corrected data is expanded back.estimation_dimensionsaverages 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_frametoreference_frameand 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.