-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1542 Add RandLambdad transform (#1546)
* [DLMED] add RandLambdad transform Signed-off-by: Nic Ma <nma@nvidia.com> * [DLMED] add doc-strings Signed-off-by: Nic Ma <nma@nvidia.com> * [MONAI] python code formatting Signed-off-by: monai-bot <monai.miccai2019@gmail.com> * [DLMED] update according to comments Signed-off-by: Nic Ma <nma@nvidia.com> * [DLMED] fix typo Signed-off-by: Nic Ma <nma@nvidia.com> * [DLMED] change to rtol=1e-05 Signed-off-by: Nic Ma <nma@nvidia.com> * [MONAI] python code formatting Signed-off-by: monai-bot <monai.miccai2019@gmail.com> * fixes seeds Signed-off-by: Wenqi Li <wenqil@nvidia.com> Co-authored-by: monai-bot <monai.miccai2019@gmail.com> Co-authored-by: Wenqi Li <wenqil@nvidia.com>
- Loading branch information
1 parent
2021f24
commit bcdee8c
Showing
4 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright 2020 - 2021 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 | ||
|
||
from monai.transforms import Randomizable | ||
from monai.transforms.utility.dictionary import RandLambdad | ||
|
||
|
||
class RandTest(Randomizable): | ||
""" | ||
randomisable transform for testing. | ||
""" | ||
|
||
def randomize(self, data=None): | ||
self._a = self.R.random() | ||
|
||
def __call__(self, data): | ||
self.randomize() | ||
return data + self._a | ||
|
||
|
||
class TestRandLambdad(unittest.TestCase): | ||
def test_rand_lambdad_identity(self): | ||
img = np.zeros((10, 10)) | ||
data = {"img": img, "prop": 1.0} | ||
|
||
test_func = RandTest() | ||
test_func.set_random_state(seed=134) | ||
expected = {"img": test_func(data["img"]), "prop": 1.0} | ||
test_func.set_random_state(seed=134) | ||
ret = RandLambdad(keys=["img", "prop"], func=test_func, overwrite=[True, False])(data) | ||
np.testing.assert_allclose(expected["img"], ret["img"]) | ||
np.testing.assert_allclose(expected["prop"], ret["prop"]) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |