From 2a92e9e16afa9a7cf89b92f6e4e35a5fe853ec93 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Tue, 14 Jan 2020 14:26:13 +0000 Subject: [PATCH 1/4] initial unit tests for 2d/3d unet --- tests/test_unet.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/test_unet.py diff --git a/tests/test_unet.py b/tests/test_unet.py new file mode 100644 index 0000000000..362dd26626 --- /dev/null +++ b/tests/test_unet.py @@ -0,0 +1,43 @@ +import unittest + +import torch +from parameterized import parameterized + +from monai.networks.nets.unet import UNet + + +class TestUNET(unittest.TestCase): + + @parameterized.expand([ + [ + { + 'dimensions': 2, + 'in_channels': 1, + 'num_classes': 3, + 'channels': (16, 32, 64), + 'strides': (2, 2), + 'num_res_units': 1, + }, + torch.randn(16, 1, 32, 32), + (16, 32, 32), + ], + [ + { + 'dimensions': 3, + 'in_channels': 1, + 'num_classes': 3, + 'channels': (16, 32, 64), + 'strides': (2, 2), + 'num_res_units': 1, + }, + torch.randn(16, 1, 32, 32, 32), + (16, 32, 32, 32), + ], + ]) + def test_shape(self, input_param, input_data, expected_shape): + result = UNet(**input_param).forward(input_data)[1] + self.assertEqual(result.shape, expected_shape) + + +if __name__ == '__main__': + unittest.main() From f73e7e60520fa13472b343e91ba492e904b49902 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Tue, 14 Jan 2020 15:22:24 +0000 Subject: [PATCH 2/4] adding license info --- tests/test_unet.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/test_unet.py b/tests/test_unet.py index 362dd26626..ddd6bb9049 100644 --- a/tests/test_unet.py +++ b/tests/test_unet.py @@ -1,3 +1,14 @@ +# 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 torch From b94530e8f9613330989524cb9eb0a556c31e8993 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Fri, 17 Jan 2020 12:45:18 +0000 Subject: [PATCH 3/4] unit tests update - triggering unit tests via github workflow - renamed testconvolutions.py to test_convolutions.py - test unet test cases as variables for readability --- .github/workflows/pythonapp.yml | 7 +- ...stconvolutions.py => test_convolutions.py} | 0 tests/test_unet.py | 66 +++++++++++-------- 3 files changed, 43 insertions(+), 30 deletions(-) rename tests/{testconvolutions.py => test_convolutions.py} (100%) diff --git a/.github/workflows/pythonapp.yml b/.github/workflows/pythonapp.yml index 9251be0c36..4c8d04b8a9 100644 --- a/.github/workflows/pythonapp.yml +++ b/.github/workflows/pythonapp.yml @@ -25,7 +25,6 @@ jobs: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. flake8 . --count --statistics -# - name: Test with pytest -# run: | -# pip install pytest -# pytest + - name: Test and coverage + run: | + ./runtests.sh --coverage diff --git a/tests/testconvolutions.py b/tests/test_convolutions.py similarity index 100% rename from tests/testconvolutions.py rename to tests/test_convolutions.py diff --git a/tests/test_unet.py b/tests/test_unet.py index ddd6bb9049..95be1bf1a1 100644 --- a/tests/test_unet.py +++ b/tests/test_unet.py @@ -16,35 +16,49 @@ from monai.networks.nets.unet import UNet +TEST_CASE_1 = [ # single channel 2D, batch 16 + { + 'dimensions': 2, + 'in_channels': 1, + 'num_classes': 3, + 'channels': (16, 32, 64), + 'strides': (2, 2), + 'num_res_units': 1, + }, + torch.randn(16, 1, 32, 32), + (16, 32, 32), +] + +TEST_CASE_2 = [ # single channel 3D, batch 16 + { + 'dimensions': 3, + 'in_channels': 1, + 'num_classes': 3, + 'channels': (16, 32, 64), + 'strides': (2, 2), + 'num_res_units': 1, + }, + torch.randn(16, 1, 32, 24, 48), + (16, 32, 24, 48), +] + +TEST_CASE_3 = [ # 4-channel 3D, batch 16 + { + 'dimensions': 3, + 'in_channels': 4, + 'num_classes': 3, + 'channels': (16, 32, 64), + 'strides': (2, 2), + 'num_res_units': 1, + }, + torch.randn(16, 4, 32, 64, 48), + (16, 32, 64, 48), +] + class TestUNET(unittest.TestCase): - @parameterized.expand([ - [ - { - 'dimensions': 2, - 'in_channels': 1, - 'num_classes': 3, - 'channels': (16, 32, 64), - 'strides': (2, 2), - 'num_res_units': 1, - }, - torch.randn(16, 1, 32, 32), - (16, 32, 32), - ], - [ - { - 'dimensions': 3, - 'in_channels': 1, - 'num_classes': 3, - 'channels': (16, 32, 64), - 'strides': (2, 2), - 'num_res_units': 1, - }, - torch.randn(16, 1, 32, 32, 32), - (16, 32, 32, 32), - ], - ]) + @parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3]) def test_shape(self, input_param, input_data, expected_shape): result = UNet(**input_param).forward(input_data)[1] self.assertEqual(result.shape, expected_shape) From a450179c9b04ef90c7b93365acee01482c8b3290 Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Fri, 17 Jan 2020 13:22:24 +0000 Subject: [PATCH 4/4] fixes dependency and runtests exit code --- requirements.txt | 1 + runtests.sh | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 23493d8ecf..e45f176cda 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,3 +12,4 @@ pillow pandas coverage nibabel +parameterized diff --git a/runtests.sh b/runtests.sh index 102e63c68c..299b47dee6 100755 --- a/runtests.sh +++ b/runtests.sh @@ -1,4 +1,5 @@ #! /bin/bash +set -e # Test script for running all tests @@ -52,8 +53,8 @@ done if [ "$doDryRun" = 'true' ] then echo "Dry run commands:" - cmdprefix="dryrun " - + cmdprefix="dryrun " + # create a dry run function which prints the command prepended with spaces for neatness function dryrun { echo " " $* ; } fi