Skip to content

Commit

Permalink
Add tests and fix errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
madil90 committed Mar 4, 2020
1 parent aa76c79 commit aa8dfbf
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
11 changes: 5 additions & 6 deletions monai/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
"""

import numpy as np
<<<<<<< HEAD
import torch
=======
from skimage.transform import resize
>>>>>>> Add Resize transform (spatial scaling).

import monai
from monai.data.utils import get_random_patch, get_valid_patch_size
Expand Down Expand Up @@ -84,6 +81,7 @@ def __call__(self, img):
return np.flip(img, self.axis)


@export
class Resize:
"""
Resize the input image to given resolution. Uses skimage.transform.resize underneath.
Expand All @@ -103,10 +101,11 @@ class Resize:
anti_aliasing_sigma (float, tuple of floats): Standard deviation for gaussian filtering.
"""

def __init__(self, order=1, mode='reflect', cval=0,
def __init__(self, output_shape, order=1, mode='reflect', cval=0,
clip=True, preserve_range=True,
anti_aliasing=True, anti_aliasing_sigma=None):
assert isinstance(order, int), "order must be integer."
self.output_shape = output_shape
self.order = order
self.mode = mode
self.cval = cval
Expand All @@ -115,8 +114,8 @@ def __init__(self, order=1, mode='reflect', cval=0,
self.anti_aliasing = anti_aliasing
self.anti_aliasing_sigma = anti_aliasing_sigma

def __call__(self, img, output_shape):
return resize(img, output_shape,
def __call__(self, img):
return resize(img, self.output_shape,
mode=self.mode, cval=self.cval,
clip=self.clip, preserve_range=self.preserve_range,
anti_aliasing=self.anti_aliasing,
Expand Down
52 changes: 52 additions & 0 deletions tests/test_resize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright 2020 MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import numpy as np
import skimage
from parameterized import parameterized

from monai.transforms import Resize
from tests.utils import NumpyImageTestCase2D


class ResizeTest(NumpyImageTestCase2D):

@parameterized.expand([
("invalid_order", "order", AssertionError)
])
def test_invalid_inputs(self, _, order, raises):
with self.assertRaises(raises):
resize = Resize(output_shape=(128, 128, 3), order=order)
resize(self.imt)

@parameterized.expand([
((64, 64, 3), 1, 'reflect', 0, True, True, True, None),
((32, 32, 3), 2, 'constant', 1, False, False, False, None)
])
def test_correct_results(self, output_shape, order, mode,
cval, clip, preserve_range,
anti_aliasing, anti_aliasing_sigma):
resize = Resize(output_shape, order, mode, cval, clip,
preserve_range, anti_aliasing,
anti_aliasing_sigma)
expected = skimage.transform.resize(self.imt, output_shape,
order=order, mode=mode,
cval=cval, clip=clip,
preserve_range=preserve_range,
anti_aliasing=anti_aliasing,
anti_aliasing_sigma=anti_aliasing_sigma)
self.assertTrue(np.allclose(resize(self.imt), expected))


if __name__ == '__main__':
unittest.main()
16 changes: 0 additions & 16 deletions tests/test_spatial_transforms.py

This file was deleted.

0 comments on commit aa8dfbf

Please sign in to comment.