-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
8185 - Refactor test #8231
Open
garciadias
wants to merge
34
commits into
Project-MONAI:dev
Choose a base branch
from
garciadias:8185-tests-refactor
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,718
−3,701
Open
8185 - Refactor test #8231
Changes from 18 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
075ec11
Eliminates code repetition
garciadias e8d3e19
Replace relative import with absolute
garciadias d4b01e6
Directly tests export_ckpt function instead of using command_line_tests
garciadias bfc68a4
Style fix
garciadias 10eb2af
Merge branch 'dev' into 8185-tests-refactor
garciadias 55fcd22
Revert "Style fix"
garciadias ba16743
Revert "Replace relative import with absolute"
garciadias 1667eb7
Revert "Directly tests export_ckpt function instead of using command_…
garciadias 702e740
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9a2c2f4
moves tests to their corresponding folder
garciadias f2cf158
replace relative imports with absolute import
garciadias b3fd8e9
Change locating integrations
garciadias 114fc72
Corrects the download path for integration tests
garciadias 626e050
Move array/* and dictionary/* files to parent directory
garciadias 4c6b59c
Move single files on their parent folders
garciadias 050e71b
Merge remote-tracking branch 'upstream/dev' into dev
garciadias 7d68938
Merge branch 'dev' into 8185-tests-refactor
garciadias 3508211
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2d55074
Recovers deleted file
garciadias 6e9c66e
Merge remote-tracking branch 'upstream/dev' into 8185-tests-refactor
garciadias fa5c75c
Merge remote-tracking branch 'upstream/dev' into dev
garciadias dda65c4
Merge branch 'dev' into 8185-tests-refactor
garciadias bf82225
Start fixing path issues after moving files
garciadias 56c2a48
Keep fixing data paths
garciadias 98d3ab2
rerouting paths
garciadias 6d1af88
Merge remote-tracking branch 'origin/8185-tests-refactor' into 8185-t…
garciadias a0d2395
Solve many more issues
garciadias 9977e98
Merge remote-tracking branch 'origin/8185-tests-refactor' into 8185-t…
garciadias 8831dc0
reorder imports
garciadias 58c461c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] bb85f53
Fix path issues
garciadias 865dc22
Move utils inside utils to avoid coflict
garciadias cf1025a
Keep cleaning tests but reverting utils.utils change
d2e7a2e
Merge remote-tracking branch 'origin/8185-tests-refactor' into 8185-t…
garciadias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,108 @@ | ||
# Copyright (c) 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 __future__ import annotations | ||
|
||
import json | ||
import os | ||
import tempfile | ||
import unittest | ||
|
||
from parameterized import parameterized | ||
|
||
from monai.bundle import ConfigParser | ||
from monai.data import load_net_with_metadata | ||
from monai.networks import save_state | ||
from tests.utils import command_line_tests, skip_if_windows | ||
|
||
TEST_CASE_1 = [True, "", ""] | ||
|
||
TEST_CASE_2 = [True, "model", ""] | ||
|
||
TEST_CASE_3 = [True, "model", "True"] | ||
|
||
TEST_CASE_4 = [False, "", ""] | ||
|
||
TEST_CASE_5 = [False, "model", ""] | ||
|
||
TEST_CASE_6 = [False, "model", "True"] | ||
|
||
|
||
@skip_if_windows | ||
class TestCKPTExport(unittest.TestCase): | ||
def setUp(self): | ||
self.device = os.environ.get("CUDA_VISIBLE_DEVICES") | ||
if not self.device: | ||
os.environ["CUDA_VISIBLE_DEVICES"] = "0" # default | ||
self.meta_file = os.path.join(os.path.dirname(__file__), "testing_data", "metadata.json") | ||
self.config_file = os.path.join(os.path.dirname(__file__), "testing_data", "inference.json") | ||
self.tempdir_obj = tempfile.TemporaryDirectory() | ||
tempdir = self.tempdir_obj.name | ||
self.def_args = {"meta_file": "will be replaced by `meta_file` arg"} | ||
self.def_args_file = os.path.join(tempdir, "def_args.yaml") | ||
|
||
self.ckpt_file = os.path.join(tempdir, "model.pt") | ||
self.ts_file = os.path.join(tempdir, "model.ts") | ||
|
||
self.parser = ConfigParser() | ||
self.parser.export_config_file(config=self.def_args, filepath=self.def_args_file) | ||
self.parser.read_config(self.config_file) | ||
self.net = self.parser.get_parsed_content("network_def") | ||
self.cmd = ["coverage", "run", "-m", "monai.bundle", "ckpt_export", "network_def", "--filepath", self.ts_file] | ||
self.cmd += [ | ||
"--meta_file", | ||
self.meta_file, | ||
"--config_file", | ||
f"['{self.config_file}','{self.def_args_file}']", | ||
"--ckpt_file", | ||
] | ||
|
||
def tearDown(self): | ||
if self.device is not None: | ||
os.environ["CUDA_VISIBLE_DEVICES"] = self.device | ||
else: | ||
del os.environ["CUDA_VISIBLE_DEVICES"] # previously unset | ||
self.tempdir_obj.cleanup() | ||
|
||
@parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3]) | ||
def test_export(self, key_in_ckpt, use_trace): | ||
save_state(src=self.net if key_in_ckpt == "" else {key_in_ckpt: self.net}, path=self.ckpt_file) | ||
full_cmd = self.cmd + [self.ckpt_file, "--key_in_ckpt", key_in_ckpt, "--args_file", self.def_args_file] | ||
if use_trace == "True": | ||
full_cmd += ["--use_trace", use_trace, "--input_shape", "[1, 1, 96, 96, 96]"] | ||
command_line_tests(full_cmd) | ||
self.assertTrue(os.path.exists(self.ts_file)) | ||
|
||
_, metadata, extra_files = load_net_with_metadata( | ||
self.ts_file, more_extra_files=["inference.json", "def_args.json"] | ||
) | ||
self.assertIn("schema", metadata) | ||
self.assertIn("meta_file", json.loads(extra_files["def_args.json"])) | ||
self.assertIn("network_def", json.loads(extra_files["inference.json"])) | ||
|
||
@parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_3]) | ||
def test_default_value(self, key_in_ckpt, use_trace): | ||
ckpt_file = os.path.join(self.tempdir_obj.name, "models/model.pt") | ||
ts_file = os.path.join(self.tempdir_obj.name, "models/model.ts") | ||
|
||
save_state(src=self.net if key_in_ckpt == "" else {key_in_ckpt: self.net}, path=ckpt_file) | ||
|
||
# check with default value | ||
cmd = ["coverage", "run", "-m", "monai.bundle", "ckpt_export", "--key_in_ckpt", key_in_ckpt] | ||
cmd += ["--config_file", self.config_file, "--bundle_root", self.tempdir_obj.name] | ||
if use_trace == "True": | ||
cmd += ["--use_trace", use_trace, "--input_shape", "[1, 1, 96, 96, 96]"] | ||
command_line_tests(cmd) | ||
self.assertTrue(os.path.exists(ts_file)) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need such a detailed folder structure for the tests, or would a simpler, flat structure at the top level suffice? Such as only apps/data/transforms...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the word 'need' is unhelpful here. It's certainly not needed. None of this refactoring is needed. The project survived until now without these.
I think the question is how helpful it is to have the folder separation.
In my opinion, mirroring the structure of the code is the best way for developers to intuitively find the tests related to the portion of the code they are touching. For example, if you are changing something at
monai/transforms/lazy/functional.py
, it would be helpful to find up to the folderlazy
in the tests. In that case, you would find 2 tests there. If you get all tests inside "transforms," you will have 304 test files to go through. Which I think is quite paralyzing.I understand that some simplification could be beneficial to avoid having super-deep trees. I think @ericspod's proposition so far is good, but we can make it shallower if people find the current state too deep.
If testing is difficult or finding tests is hard, people will tend to ignore the tests or write repeated code.
However, I am not very familiar with the project's code. I am just starting to work on this, and I am happy to follow the team's guidance.