Skip to content
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
7 changes: 3 additions & 4 deletions .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ pillow
pandas
coverage
nibabel
parameterized
5 changes: 3 additions & 2 deletions runtests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#! /bin/bash
set -e
# Test script for running all tests


Expand Down Expand Up @@ -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
Expand Down
File renamed without changes.
53 changes: 53 additions & 0 deletions tests/test_dice_loss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 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.losses.dice import DiceLoss

TEST_CASE_1 = [
{
'include_background': False,
},
{
'pred': torch.tensor([[[[1., -1.], [-1., 1.]]]]),
'ground': torch.tensor([[[[1., 0.], [1., 1.]]]]),
'smooth': 1e-6,
},
0.307576,
]

TEST_CASE_2 = [
{
'include_background': True,
},
{
'pred': torch.tensor([[[[1., -1.], [-1., 1.]]], [[[1., -1.], [-1., 1.]]]]),
'ground': torch.tensor([[[[1., 1.], [1., 1.]]], [[[1., 0.], [1., 0.]]]]),
'smooth': 1e-4,
},
0.416636,
]


class TestDiceLoss(unittest.TestCase):

@parameterized.expand([TEST_CASE_1, TEST_CASE_2])
def test_shape(self, input_param, input_data, expected_val):
result = DiceLoss(**input_param).forward(**input_data)
self.assertAlmostEqual(result.item(), expected_val, places=5)


if __name__ == '__main__':
unittest.main()
68 changes: 68 additions & 0 deletions tests/test_unet.py
Original file line number Diff line number Diff line change
@@ -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()