def test_binarize(): binarize_spec = Preprocessing(name="binarize", kwargs={"threshold": 14}) data = xr.DataArray(np.arange(30).reshape(2, 3, 5), dims=("x", "y", "c")) expected = xr.zeros_like(data) expected[{"x": slice(1, None)}] = 1 preprocessing = make_preprocessing([binarize_spec]) result = preprocessing(data) xr.testing.assert_allclose(expected, result)
def test_scale_linear_no_channel(): spec = Preprocessing(name="scale_linear", kwargs={ "offset": 1, "gain": 2, "axes": "yx" }) data = xr.DataArray(np.arange(6).reshape(2, 3), dims=("x", "y")) expected = xr.DataArray(np.array([[1, 3, 5], [7, 9, 11]]), dims=("x", "y")) preprocessing = make_preprocessing([spec]) result = preprocessing(data) xr.testing.assert_allclose(expected, result)
def test_scale_linear(): spec = Preprocessing(name="scale_linear", kwargs={ "offset": [1, 2, 42], "gain": [1, 2, 3], "axes": "yx" }) data = xr.DataArray(np.arange(6).reshape(1, 2, 3), dims=("x", "y", "c")) expected = xr.DataArray(np.array([[[1, 4, 48], [4, 10, 57]]]), dims=("x", "y", "c")) preprocessing = make_preprocessing([spec]) result = preprocessing(data) xr.testing.assert_allclose(expected, result)
def test_zero_mean_unit_variance_preprocessing(): zero_mean_spec = Preprocessing(name="zero_mean_unit_variance", kwargs={}) data = xr.DataArray(np.arange(9).reshape(3, 3), dims=("x", "y")) expected = xr.DataArray( np.array([ [-1.54919274, -1.16189455, -0.77459637], [-0.38729818, 0.0, 0.38729818], [0.77459637, 1.16189455, 1.54919274], ]), dims=("x", "y"), ) preprocessing = make_preprocessing([zero_mean_spec]) result = preprocessing(data) xr.testing.assert_allclose(expected, result)
def test_combination_of_preprocessing_steps_with_dims_specified(): zero_mean_spec = Preprocessing(name="zero_mean_unit_variance", kwargs={"axes": ("x", "y")}) data = xr.DataArray(np.arange(18).reshape(2, 3, 3), dims=("c", "x", "y")) expected = xr.DataArray( np.array([ [-1.54919274, -1.16189455, -0.77459637], [-0.38729818, 0.0, 0.38729818], [0.77459637, 1.16189455, 1.54919274], ]), dims=("x", "y"), ) preprocessing = make_preprocessing([ADD_BATCH_DIM, zero_mean_spec]) result = preprocessing(data) xr.testing.assert_allclose(expected, result[0][0])
def make_ensure_dtype_preprocessing(dtype): return Preprocessing(name="__tiktorch_ensure_dtype", kwargs={"dtype": dtype})
from typing import List import numpy as np import xarray as xr from bioimageio.spec.nodes import Preprocessing from ._types import Transform ADD_BATCH_DIM = Preprocessing(name="__tiktorch_add_batch_dim", kwargs=None) def make_ensure_dtype_preprocessing(dtype): return Preprocessing(name="__tiktorch_ensure_dtype", kwargs={"dtype": dtype}) def scale_linear(tensor: xr.DataArray, *, gain, offset, axes) -> xr.DataArray: """scale the tensor with a fixed multiplicative and additive factor""" scale_axes = tuple(ax for ax in tensor.dims if (ax not in axes and ax != "b")) if scale_axes: gain = xr.DataArray(np.atleast_1d(gain), dims=scale_axes) offset = xr.DataArray(np.atleast_1d(offset), dims=scale_axes) return tensor * gain + offset def zero_mean_unit_variance(tensor: xr.DataArray, axes=None, eps=1.0e-6, mode="per_sample") -> xr.DataArray: if axes:
def test_unknown_preprocessing_should_raise(): mypreprocessing = Preprocessing(name="mycoolpreprocessing", kwargs={"axes": ("x", "y")}) with pytest.raises(NotImplementedError): make_preprocessing([mypreprocessing])