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

9 part a adding test intensity normalisation transform #33

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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,4 @@ venv.bak/
# mypy
.mypy_cache/
examples/scd_lvsegs.npz
.idea/
12 changes: 12 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
stages:
- build

.base_template : &BASE
script:
- cat README.md

build-ci-test:
stage: build
tags:
- test
<<: *BASE
101 changes: 29 additions & 72 deletions examples/cardiac_segmentation.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion monai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import os
import sys

from .utils.moduleutils import load_submodules, loadSubmodules
from .utils.moduleutils import load_submodules

__copyright__ = "(c) 2020 MONAI Consortium"
__version__tuple__ = (0, 0, 1)
Expand Down
20 changes: 13 additions & 7 deletions monai/data/transforms/intensity_normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from collections.abc import Hashable

import numpy as np
from transform import Transform

from .transform import Transform


class IntensityNormalizer(Transform):
Expand All @@ -20,17 +23,20 @@ class IntensityNormalizer(Transform):
Current implementation can only support 'channel_last' format data.

Args:
apply_keys (tuple or list): run transform on which field of the inout data
apply_keys (a hashable key or a tuple/list of hashable keys): run transform on which field of the input data
subtrahend (ndarray): the amount to subtract by (usually the mean)
divisor (ndarray): the amount to divide by (usually the standard deviation)
dtype: output data format
"""

def __init__(self, apply_keys, subtrahend=None, divisor=None, dtype=np.float32):
Transform.__init__(self)
assert apply_keys is not None and (type(apply_keys) == tuple or type(apply_keys) == list), \
'must set apply_keys for this transform.'
self.apply_keys = apply_keys
_apply_keys = apply_keys if isinstance(apply_keys, (list, tuple)) else (apply_keys,)
if not _apply_keys:
raise ValueError('must set apply_keys for this transform.')
for key in _apply_keys:
if not isinstance(key, Hashable):
raise ValueError('apply_keys should be a hashable or a sequence of hashables used by data[key]')
self.apply_keys = _apply_keys
if subtrahend is not None or divisor is not None:
assert isinstance(subtrahend, np.ndarray) and isinstance(divisor, np.ndarray), \
'subtrahend and divisor must be set in pair and in numpy array.'
Expand All @@ -39,7 +45,7 @@ def __init__(self, apply_keys, subtrahend=None, divisor=None, dtype=np.float32):
self.dtype = dtype

def __call__(self, data):
assert data is not None and type(data) == dict, 'data must be in dict format with keys.'
assert data is not None and isinstance(data, dict), 'data must be in dict format with keys.'
for key in self.apply_keys:
img = data[key]
assert key in data, 'can not find expected key={} in data.'.format(key)
Expand Down
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
103 changes: 103 additions & 0 deletions runtests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#! /bin/bash
set -e
# Test script for running all tests


homedir="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $homedir

#export PYTHONPATH="$homedir:$PYTHONPATH"

# configuration values
doCoverage=false
doQuickTests=false
doNetTests=false
doDryRun=false
doZooTests=false

# testing command to run
cmd="python"
cmdprefix=""


# parse arguments
for i in "$@"
do
case $i in
--coverage)
doCoverage=true
;;
--quick)
doQuickTests=true
doCoverage=true
export QUICKTEST=True
;;
--net)
doNetTests=true
;;
--dryrun)
doDryRun=true
;;
--zoo)
doZooTests=true
;;
*)
echo "runtests.sh [--coverage] [--quick] [--net] [--dryrun] [--zoo]"
exit 1
;;
esac
done


# commands are echoed instead of run in this case
if [ "$doDryRun" = 'true' ]
then
echo "Dry run commands:"
cmdprefix="dryrun "

# create a dry run function which prints the command prepended with spaces for neatness
function dryrun { echo " " $* ; }
fi


# set command and clear previous coverage data
if [ "$doCoverage" = 'true' ]
then
cmd="coverage run -a --source ."
${cmdprefix} coverage erase
fi


# # download test data if needed
# if [ ! -d testing_data ] && [ "$doDryRun" != 'true' ]
# then
# fi


# unit tests
${cmdprefix}${cmd} -m unittest


# network training/inference/eval tests
if [ "$doNetTests" = 'true' ]
then
for i in examples/*.py
do
echo $i
${cmdprefix}${cmd} $i
done
fi


# # run model zoo tests
# if [ "$doZooTests" = 'true' ]
# then
# fi


# report on coverage
if [ "$doCoverage" = 'true' ]
then
${cmdprefix}coverage report --omit='*/test/*' --skip-covered -m
fi

10 changes: 10 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# 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.
84 changes: 84 additions & 0 deletions tests/test_convolutions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# 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.

from .utils import TorchImageTestCase2D

from monai.networks.layers.convolutions import Convolution, ResidualUnit


class TestConvolution2D(TorchImageTestCase2D):
def test_conv1(self):
conv = Convolution(2, self.input_channels, self.output_channels)
out = conv(self.imt)
expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1])
self.assertEqual(out.shape, expected_shape)

def test_conv_only1(self):
conv = Convolution(2, self.input_channels, self.output_channels, conv_only=True)
out = conv(self.imt)
expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1])
self.assertEqual(out.shape, expected_shape)

def test_stride1(self):
conv = Convolution(2, self.input_channels, self.output_channels, strides=2)
out = conv(self.imt)
expected_shape = (1, self.output_channels, self.im_shape[0] // 2, self.im_shape[1] // 2)
self.assertEqual(out.shape, expected_shape)

def test_dilation1(self):
conv = Convolution(2, self.input_channels, self.output_channels, dilation=3)
out = conv(self.imt)
expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1])
self.assertEqual(out.shape, expected_shape)

def test_dropout1(self):
conv = Convolution(2, self.input_channels, self.output_channels, dropout=0.15)
out = conv(self.imt)
expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1])
self.assertEqual(out.shape, expected_shape)

def test_transpose1(self):
conv = Convolution(2, self.input_channels, self.output_channels, is_transposed=True)
out = conv(self.imt)
expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1])
self.assertEqual(out.shape, expected_shape)

def test_transpose2(self):
conv = Convolution(2, self.input_channels, self.output_channels, strides=2, is_transposed=True)
out = conv(self.imt)
expected_shape = (1, self.output_channels, self.im_shape[0] * 2, self.im_shape[1] * 2)
self.assertEqual(out.shape, expected_shape)


class TestResidualUnit2D(TorchImageTestCase2D):
def test_conv_only1(self):
conv = ResidualUnit(2, 1, self.output_channels)
out = conv(self.imt)
expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1])
self.assertEqual(out.shape, expected_shape)

def test_stride1(self):
conv = ResidualUnit(2, 1, self.output_channels, strides=2)
out = conv(self.imt)
expected_shape = (1, self.output_channels, self.im_shape[0] // 2, self.im_shape[1] // 2)
self.assertEqual(out.shape, expected_shape)

def test_dilation1(self):
conv = ResidualUnit(2, 1, self.output_channels, dilation=3)
out = conv(self.imt)
expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1])
self.assertEqual(out.shape, expected_shape)

def test_dropout1(self):
conv = ResidualUnit(2, 1, self.output_channels, dropout=0.15)
out = conv(self.imt)
expected_shape = (1, self.output_channels, self.im_shape[0], self.im_shape[1])
self.assertEqual(out.shape, expected_shape)
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()
47 changes: 47 additions & 0 deletions tests/test_intensity_normalizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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
wyli marked this conversation as resolved.
Show resolved Hide resolved

import numpy as np

from monai.data.transforms.intensity_normalizer import IntensityNormalizer
from tests.utils import NumpyImageTestCase2D


class IntensityNormTestCase(NumpyImageTestCase2D):

def test_image_normalizer_default(self):
data_key = 'image'
normalizer = IntensityNormalizer(data_key) # test a single key
normalised = normalizer({data_key: self.imt})
expected = (self.imt - np.mean(self.imt)) / np.std(self.imt)
self.assertTrue(np.allclose(normalised[data_key], expected))

def test_image_normalizer_default_1(self):
data_key = 'image'
wyli marked this conversation as resolved.
Show resolved Hide resolved
normalizer = IntensityNormalizer([data_key]) # test list of keys
normalised = normalizer({data_key: self.imt})
expected = (self.imt - np.mean(self.imt)) / np.std(self.imt)
self.assertTrue(np.allclose(normalised[data_key], expected))

def test_image_normalizer_default_2(self):
data_keys = ['image_1', 'image_2']
normalizer = IntensityNormalizer(data_keys) # test list of keys
normalised = normalizer(dict(zip(data_keys, (self.imt, self.seg1))))
expected_1 = (self.imt - np.mean(self.imt)) / np.std(self.imt)
expected_2 = (self.seg1 - np.mean(self.seg1)) / np.std(self.seg1)
self.assertTrue(np.allclose(normalised[data_keys[0]], expected_1))
self.assertTrue(np.allclose(normalised[data_keys[1]], expected_2))


if __name__ == '__main__':
unittest.main()
Loading