Skip to content
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

fixes randbiasfield (#2645) #182

Merged
merged 1 commit into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions monai/transforms/intensity/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,22 +431,19 @@ def __init__(
self.coeff_range = coeff_range
self.dtype = dtype

def _generate_random_field(
self,
spatial_shape: Tuple[int, ...],
rank: int,
degree: int,
coeff: Tuple[int, ...],
):
self._coeff = [1.0]

def _generate_random_field(self, spatial_shape: Sequence[int], degree: int, coeff: Sequence[float]):
"""
products of polynomials as bias field estimations
"""
rank = len(spatial_shape)
coeff_mat = np.zeros((degree + 1,) * rank)
coords = [np.linspace(-1.0, 1.0, dim, dtype=np.float32) for dim in spatial_shape]
if rank == 2:
coeff_mat[np.tril_indices(degree + 1)] = coeff
field = np.polynomial.legendre.leggrid2d(coords[0], coords[1], coeff_mat)
elif rank == 3:
return np.polynomial.legendre.leggrid2d(coords[0], coords[1], coeff_mat)
if rank == 3:
pts: List[List[int]] = [[0, 0, 0]]
for i in range(degree + 1):
for j in range(degree + 1 - i):
Expand All @@ -456,16 +453,12 @@ def _generate_random_field(
pts = pts[1:]
np_pts = np.stack(pts)
coeff_mat[np_pts[:, 0], np_pts[:, 1], np_pts[:, 2]] = coeff
field = np.polynomial.legendre.leggrid3d(coords[0], coords[1], coords[2], coeff_mat)
else:
raise NotImplementedError("only supports 2D or 3D fields")
return field
return np.polynomial.legendre.leggrid3d(coords[0], coords[1], coords[2], coeff_mat)
raise NotImplementedError("only supports 2D or 3D fields")

def randomize(self, data: np.ndarray) -> None:
super().randomize(None)
self.spatial_shape = data.shape[1:]
self.rank = len(self.spatial_shape)
n_coeff = int(np.prod([(self.degree + k) / k for k in range(1, self.rank + 1)]))
n_coeff = int(np.prod([(self.degree + k) / k for k in range(1, len(data.shape[1:]) + 1)]))
self._coeff = self.R.uniform(*self.coeff_range, n_coeff).tolist()

def __call__(self, img: np.ndarray):
Expand All @@ -475,17 +468,15 @@ def __call__(self, img: np.ndarray):
self.randomize(data=img)
if not self._do_transform:
return img
num_channels = img.shape[0]
num_channels, *spatial_shape = img.shape
_bias_fields = np.stack(
[
self._generate_random_field(
spatial_shape=self.spatial_shape, rank=self.rank, degree=self.degree, coeff=self._coeff
)
self._generate_random_field(spatial_shape=spatial_shape, degree=self.degree, coeff=self._coeff)
for _ in range(num_channels)
],
axis=0,
)
return (img * _bias_fields).astype(self.dtype)
return (img * np.exp(_bias_fields)).astype(self.dtype)


class NormalizeIntensity(Transform):
Expand Down
17 changes: 6 additions & 11 deletions tests/test_random_bias_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@

TEST_CASES_2D = [{}, (3, 32, 32)]
TEST_CASES_3D = [{}, (3, 32, 32, 32)]
TEST_CASES_2D_ZERO_RANGE = [{"coeff_range": (0.0, 0.0)}, (3, 32, 32)]
TEST_CASES_2D_ONES = [{"coeff_range": (1.0, 1.0)}, np.asarray([[[2, -2], [2, 10]]])]
TEST_CASES_2D_ZERO_RANGE = [{"coeff_range": (0.0, 0.0)}, (2, 3, 3)]
TEST_CASES_2D_ONES = [{"coeff_range": (1.0, 1.0)}, np.asarray([[[7.389056, 0.1353353], [7.389056, 22026.46]]])]


class TestRandBiasField(unittest.TestCase):
@parameterized.expand(
[
TEST_CASES_2D,
TEST_CASES_3D,
]
)
@parameterized.expand([TEST_CASES_2D, TEST_CASES_3D])
def test_output_shape(self, class_args, img_shape):
for degree in [1, 2, 3]:
bias_field = RandBiasField(degree=degree, **class_args)
Expand All @@ -44,16 +39,16 @@ def test_output_shape(self, class_args, img_shape):
@parameterized.expand([TEST_CASES_2D_ZERO_RANGE])
def test_zero_range(self, class_args, img_shape):
bias_field = RandBiasField(**class_args)
img = np.random.rand(*img_shape)
img = np.ones(img_shape)
output = bias_field(img)
np.testing.assert_equal(output, np.zeros(img_shape))
np.testing.assert_allclose(output, np.ones(img_shape), rtol=1e-3)

@parameterized.expand([TEST_CASES_2D_ONES])
def test_one_range_input(self, class_args, expected):
bias_field = RandBiasField(**class_args)
img = np.ones([1, 2, 2])
output = bias_field(img)
np.testing.assert_equal(output, expected.astype(bias_field.dtype))
np.testing.assert_allclose(output, expected.astype(bias_field.dtype), rtol=1e-3)

def test_zero_prob(self):
bias_field = RandBiasField(prob=0.0)
Expand Down
18 changes: 8 additions & 10 deletions tests/test_random_bias_fieldd.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@
TEST_CASES_2D = [{}, (3, 32, 32)]
TEST_CASES_3D = [{}, (3, 32, 32, 32)]
TEST_CASES_2D_ZERO_RANGE = [{"coeff_range": (0.0, 0.0)}, (3, 32, 32)]
TEST_CASES_2D_ONES = [{"coeff_range": (1.0, 1.0)}, np.asarray([[[2, -2], [2, 10]]])]
TEST_CASES_2D_ONES = [
{"coeff_range": (1.0, 1.0)},
np.asarray([[[7.3890562e00, 1.3533528e-01], [7.3890562e00, 2.2026465e04]]]),
]


class TestRandBiasFieldd(unittest.TestCase):
@parameterized.expand(
[
TEST_CASES_2D,
TEST_CASES_3D,
]
)
@parameterized.expand([TEST_CASES_2D, TEST_CASES_3D])
def test_output_shape(self, class_args, img_shape):
key = "img"
bias_field = RandBiasFieldd(keys=[key], **class_args)
Expand All @@ -41,17 +39,17 @@ def test_output_shape(self, class_args, img_shape):
def test_zero_range(self, class_args, img_shape):
key = "img"
bias_field = RandBiasFieldd(keys=[key], **class_args)
img = np.random.rand(*img_shape)
img = np.ones(img_shape)
output = bias_field({key: img})
np.testing.assert_equal(output[key], np.zeros(img_shape))
np.testing.assert_allclose(output[key], np.ones(img_shape))

@parameterized.expand([TEST_CASES_2D_ONES])
def test_one_range_input(self, class_args, expected):
key = "img"
bias_field = RandBiasFieldd(keys=[key], **class_args)
img = np.ones([1, 2, 2])
output = bias_field({key: img})
np.testing.assert_equal(output[key], expected.astype(bias_field.rand_bias_field.dtype))
np.testing.assert_allclose(output[key], expected.astype(bias_field.rand_bias_field.dtype), rtol=1e-3)

def test_zero_prob(self):
key = "img"
Expand Down