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/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 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 new file mode 100644 index 0000000000..95be1bf1a1 --- /dev/null +++ b/tests/test_unet.py @@ -0,0 +1,68 @@ +# 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 parameterized import parameterized + +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([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) + + +if __name__ == '__main__': + unittest.main()