Skip to content

Commit

Permalink
Merge branch 'dev' into 7047-simplify-resnet-pretrained
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrau98 authored Oct 6, 2023
2 parents cc5aa25 + 100db27 commit e6a29e5
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 18 deletions.
4 changes: 3 additions & 1 deletion monai/apps/auto3dseg/bundle_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@
from monai.config import PathLike
from monai.utils import ensure_tuple, look_up_option, run_cmd
from monai.utils.enums import AlgoKeys
from monai.utils.misc import MONAIEnvVars

logger = get_logger(module_name=__name__)
ALGO_HASH = os.environ.get("MONAI_ALGO_HASH", "3e11dd0")
ALGO_HASH = MONAIEnvVars.algo_hash()


__all__ = ["BundleAlgo", "BundleGen"]

Expand Down
2 changes: 1 addition & 1 deletion monai/metrics/hausdorff_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def _compute_percentile_hausdorff_distance(

# for both pred and gt do not have foreground
if surface_distance.shape == (0,):
return torch.tensor(torch.nan, dtype=torch.float, device=surface_distance.device)
return torch.tensor(np.nan, dtype=torch.float, device=surface_distance.device)

if not percentile:
return surface_distance.max()
Expand Down
2 changes: 1 addition & 1 deletion monai/metrics/surface_dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def compute_surface_dice(
boundary_correct = gt_true + pred_true
if boundary_complete == 0:
# the class is neither present in the prediction, nor in the reference segmentation
nsd[b, c] = torch.nan
nsd[b, c] = torch.tensor(np.nan)
else:
nsd[b, c] = boundary_correct / boundary_complete

Expand Down
2 changes: 1 addition & 1 deletion monai/metrics/surface_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,6 @@ def compute_average_surface_distance(
class_index=c,
)
surface_distance = torch.cat(distances)
asd[b, c] = torch.nan if surface_distance.shape == (0,) else surface_distance.mean()
asd[b, c] = torch.tensor(np.nan) if surface_distance.shape == (0,) else surface_distance.mean()

return convert_data_type(asd, output_type=torch.Tensor, device=y_pred.device, dtype=torch.float)[0]
4 changes: 2 additions & 2 deletions monai/metrics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@ def get_surface_distance(
"""
lib: ModuleType = torch if isinstance(seg_pred, torch.Tensor) else np
if not seg_gt.any():
dis = lib.inf * lib.ones_like(seg_gt, dtype=lib.float32)
dis = np.inf * lib.ones_like(seg_gt, dtype=lib.float32)
else:
if not lib.any(seg_pred):
dis = lib.inf * lib.ones_like(seg_gt, dtype=lib.float32)
dis = np.inf * lib.ones_like(seg_gt, dtype=lib.float32)
dis = dis[seg_gt]
return convert_to_dst_type(dis, seg_pred, dtype=dis.dtype)[0]
if distance_metric == "euclidean":
Expand Down
2 changes: 1 addition & 1 deletion monai/networks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def copy_model_state(
if inplace and isinstance(dst, torch.nn.Module):
if isinstance(dst, (nn.DataParallel, nn.parallel.DistributedDataParallel)):
dst = dst.module
dst.load_state_dict(dst_dict)
dst.load_state_dict(dst_dict) # type: ignore
return dst_dict, updated_keys, unchanged_keys


Expand Down
2 changes: 1 addition & 1 deletion monai/optimizers/novograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __setstate__(self, state):
for group in self.param_groups:
group.setdefault("amsgrad", False)

def step(self, closure: Callable[[], T] | None = None) -> T | None:
def step(self, closure: Callable[[], T] | None = None) -> T | None: # type: ignore
"""Performs a single optimization step.
Arguments:
Expand Down
4 changes: 2 additions & 2 deletions monai/transforms/inverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from __future__ import annotations

import os
import warnings
from collections.abc import Hashable, Mapping
from contextlib import contextmanager
Expand All @@ -34,6 +33,7 @@
convert_to_numpy,
convert_to_tensor,
)
from monai.utils.misc import MONAIEnvVars

__all__ = ["TraceableTransform", "InvertibleTransform"]

Expand Down Expand Up @@ -70,7 +70,7 @@ class TraceableTransform(Transform):
`MONAI_TRACE_TRANSFORM` when initializing the class.
"""

tracing = os.environ.get("MONAI_TRACE_TRANSFORM", "1") != "0"
tracing = MONAIEnvVars.trace_transform() != "0"

def set_tracing(self, tracing: bool) -> None:
"""Set whether to trace transforms."""
Expand Down
2 changes: 1 addition & 1 deletion monai/transforms/signal/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def __call__(self, signal: NdarrayOrTensor) -> NdarrayOrTensor:
Args:
signal: signal to be filled
"""
signal = torch.nan_to_num(convert_to_tensor(signal), nan=self.replacement)
signal = torch.nan_to_num(convert_to_tensor(signal, track_meta=True), nan=self.replacement)
return signal


Expand Down
24 changes: 24 additions & 0 deletions monai/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,30 @@ def debug() -> bool:
def doc_images() -> str | None:
return os.environ.get("MONAI_DOC_IMAGES")

@staticmethod
def algo_hash() -> str | None:
return os.environ.get("MONAI_ALGO_HASH", "249bf4b")

@staticmethod
def trace_transform() -> str | None:
return os.environ.get("MONAI_TRACE_TRANSFORM", "1")

@staticmethod
def eval_expr() -> str | None:
return os.environ.get("MONAI_EVAL_EXPR", "1")

@staticmethod
def allow_missing_reference() -> str | None:
return os.environ.get("MONAI_ALLOW_MISSING_REFERENCE", "1")

@staticmethod
def extra_test_data() -> str | None:
return os.environ.get("MONAI_EXTRA_TEST_DATA", "1")

@staticmethod
def testing_algo_template() -> str | None:
return os.environ.get("MONAI_TESTING_ALGO_TEMPLATE", None)


class ImageMetaKey:
"""
Expand Down
1 change: 1 addition & 0 deletions tests/min_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def run_testsuit():
"test_unetr_block",
"test_vit",
"test_vitautoenc",
"test_vnet",
"test_write_metrics_reports",
"test_wsireader",
"test_zoom",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_monai_env_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ class TestMONAIEnvVars(unittest.TestCase):
@classmethod
def setUpClass(cls):
super(__class__, cls).setUpClass()
cls.orig_value = os.environ.get("MONAI_DEBUG")
cls.orig_value = str(MONAIEnvVars.debug())

@classmethod
def tearDownClass(cls):
if cls.orig_value is not None:
os.environ["MONAI_DEBUG"] = cls.orig_value
else:
os.environ.pop("MONAI_DEBUG")
print("MONAI debug value:", os.environ.get("MONAI_DEBUG"))
print("MONAI debug value:", str(MONAIEnvVars.debug()))
super(__class__, cls).tearDownClass()

def test_monai_env_vars(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_monai_utils_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from parameterized import parameterized

from monai.utils.misc import check_kwargs_exist_in_class_init, run_cmd, to_tuple_of_dictionaries
from monai.utils.misc import MONAIEnvVars, check_kwargs_exist_in_class_init, run_cmd, to_tuple_of_dictionaries

TO_TUPLE_OF_DICTIONARIES_TEST_CASES = [
({}, tuple(), tuple()),
Expand Down Expand Up @@ -75,7 +75,7 @@ def _custom_user_function(self, cls, *args, **kwargs):

class TestCommandRunner(unittest.TestCase):
def setUp(self):
self.orig_flag = os.environ.get("MONAI_DEBUG")
self.orig_flag = str(MONAIEnvVars.debug())

def tearDown(self):
if self.orig_flag is not None:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_network_consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@

import monai.networks.nets as nets
from monai.utils import set_determinism
from monai.utils.misc import MONAIEnvVars
from tests.utils import assert_allclose

extra_test_data_dir = os.environ.get("MONAI_EXTRA_TEST_DATA")
extra_test_data_dir = MONAIEnvVars.extra_test_data()

TESTS = []
if extra_test_data_dir is not None:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import monai.transforms as mt
from monai.data import Dataset
from monai.utils.misc import MONAIEnvVars


class FaultyTransform(mt.Transform):
Expand All @@ -31,7 +32,7 @@ class TestTransform(unittest.TestCase):
@classmethod
def setUpClass(cls):
super(__class__, cls).setUpClass()
cls.orig_value = os.environ.get("MONAI_DEBUG")
cls.orig_value = str(MONAIEnvVars.debug())

@classmethod
def tearDownClass(cls):
Expand Down
3 changes: 2 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from monai.data.meta_tensor import MetaTensor, get_track_meta
from monai.networks import convert_to_onnx, convert_to_torchscript
from monai.utils import optional_import
from monai.utils.misc import MONAIEnvVars
from monai.utils.module import pytorch_after
from monai.utils.tf32 import detect_default_tf32
from monai.utils.type_conversion import convert_data_type
Expand Down Expand Up @@ -75,7 +76,7 @@ def get_testing_algo_template_path():
https://github.com/Project-MONAI/MONAI/blob/1.1.0/monai/apps/auto3dseg/bundle_gen.py#L380-L381
"""
return os.environ.get("MONAI_TESTING_ALGO_TEMPLATE", None)
return MONAIEnvVars.testing_algo_template()


def clone(data: NdarrayTensor) -> NdarrayTensor:
Expand Down

0 comments on commit e6a29e5

Please sign in to comment.