From e8553486f23be9dba689f7157884875e5a4134ea Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Mon, 11 Jan 2021 13:55:10 +0000 Subject: [PATCH 1/2] Remove unnecessary lambda expression --- monai/config/deviceconfig.py | 24 ++++++++++++------------ tests/test_activations.py | 2 +- tests/test_activationsd.py | 4 ++-- tests/test_data_stats.py | 4 ++-- tests/test_data_statsd.py | 6 +++--- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/monai/config/deviceconfig.py b/monai/config/deviceconfig.py index ce7ed70655..ec65176e58 100644 --- a/monai/config/deviceconfig.py +++ b/monai/config/deviceconfig.py @@ -123,11 +123,11 @@ def get_system_info() -> OrderedDict: """ output: OrderedDict = OrderedDict() - _dict_append(output, "System", lambda: platform.system()) + _dict_append(output, "System", platform.system) if output["System"] == "Windows": - _dict_append(output, "Win32 version", lambda: platform.win32_ver()) + _dict_append(output, "Win32 version", platform.win32_ver) if hasattr(platform, "win32_edition"): - _dict_append(output, "Win32 edition", lambda: platform.win32_edition()) # type:ignore[attr-defined] + _dict_append(output, "Win32 edition", platform.win32_edition) # type:ignore[attr-defined] elif output["System"] == "Darwin": _dict_append(output, "Mac version", lambda: platform.mac_ver()[0]) else: @@ -135,19 +135,19 @@ def get_system_info() -> OrderedDict: if linux_ver: _dict_append(output, "Linux version", lambda: linux_ver.group(1)) - _dict_append(output, "Platform", lambda: platform.platform()) - _dict_append(output, "Processor", lambda: platform.processor()) - _dict_append(output, "Machine", lambda: platform.machine()) - _dict_append(output, "Python version", lambda: platform.python_version()) + _dict_append(output, "Platform", platform.platform) + _dict_append(output, "Processor", platform.processor) + _dict_append(output, "Machine", platform.machine) + _dict_append(output, "Python version", platform.python_version) if not has_psutil: _dict_append(output, "`psutil` missing", lambda: "run `pip install monai[psutil]`") else: p = psutil.Process() with p.oneshot(): - _dict_append(output, "Process name", lambda: p.name()) - _dict_append(output, "Command", lambda: p.cmdline()) - _dict_append(output, "Open files", lambda: p.open_files()) + _dict_append(output, "Process name", p.name) + _dict_append(output, "Command", p.cmdline) + _dict_append(output, "Open files", p.open_files) _dict_append(output, "Num physical CPUs", lambda: psutil.cpu_count(logical=False)) _dict_append(output, "Num logical CPUs", lambda: psutil.cpu_count(logical=True)) _dict_append(output, "Num usable CPUs", lambda: len(psutil.Process().cpu_affinity())) @@ -204,8 +204,8 @@ def get_gpu_info() -> OrderedDict: _dict_append(output, "cuDNN version", lambda: cudnn_ver) if num_gpus > 0: - _dict_append(output, "Current device", lambda: torch.cuda.current_device()) - _dict_append(output, "Library compiled for CUDA architectures", lambda: torch.cuda.get_arch_list()) + _dict_append(output, "Current device", torch.cuda.current_device) + _dict_append(output, "Library compiled for CUDA architectures", torch.cuda.get_arch_list) for gpu in range(num_gpus): _dict_append(output, "Info for GPU", gpu) gpu_info = torch.cuda.get_device_properties(gpu) diff --git a/tests/test_activations.py b/tests/test_activations.py index 4fb2726de8..1614642d6d 100644 --- a/tests/test_activations.py +++ b/tests/test_activations.py @@ -32,7 +32,7 @@ ] TEST_CASE_3 = [ - {"sigmoid": False, "softmax": False, "other": lambda x: torch.tanh(x)}, + {"sigmoid": False, "softmax": False, "other": torch.tanh}, torch.tensor([[[[0.0, 1.0], [2.0, 3.0]]]]), torch.tensor([[[[0.0000, 0.7616], [0.9640, 0.9951]]]]), (1, 1, 2, 2), diff --git a/tests/test_activationsd.py b/tests/test_activationsd.py index b8384e1469..f186c17716 100644 --- a/tests/test_activationsd.py +++ b/tests/test_activationsd.py @@ -27,7 +27,7 @@ ] TEST_CASE_2 = [ - {"keys": ["pred", "label"], "sigmoid": False, "softmax": False, "other": [lambda x: torch.tanh(x), None]}, + {"keys": ["pred", "label"], "sigmoid": False, "softmax": False, "other": [torch.tanh, None]}, {"pred": torch.tensor([[[[0.0, 1.0], [2.0, 3.0]]]]), "label": torch.tensor([[[[0.0, 1.0], [2.0, 3.0]]]])}, { "pred": torch.tensor([[[[0.0000, 0.7616], [0.9640, 0.9951]]]]), @@ -37,7 +37,7 @@ ] TEST_CASE_3 = [ - {"keys": "pred", "sigmoid": False, "softmax": False, "other": lambda x: torch.tanh(x)}, + {"keys": "pred", "sigmoid": False, "softmax": False, "other": torch.tanh}, {"pred": torch.tensor([[[[0.0, 1.0], [2.0, 3.0]]]])}, {"pred": torch.tensor([[[[0.0000, 0.7616], [0.9640, 0.9951]]]])}, (1, 1, 2, 2), diff --git a/tests/test_data_stats.py b/tests/test_data_stats.py index ab5b547b4b..e7334eb52c 100644 --- a/tests/test_data_stats.py +++ b/tests/test_data_stats.py @@ -78,7 +78,7 @@ "data_shape": True, "value_range": True, "data_value": True, - "additional_info": lambda x: np.mean(x), + "additional_info": np.mean, "logger_handler": None, }, np.array([[0, 1], [1, 2]]), @@ -124,7 +124,7 @@ def test_file(self, input_data, expected_print): "data_shape": True, "value_range": True, "data_value": True, - "additional_info": lambda x: np.mean(x), + "additional_info": np.mean, "logger_handler": handler, } transform = DataStats(**input_param) diff --git a/tests/test_data_statsd.py b/tests/test_data_statsd.py index 25c3554194..a5fae3d66d 100644 --- a/tests/test_data_statsd.py +++ b/tests/test_data_statsd.py @@ -79,7 +79,7 @@ "data_shape": True, "value_range": True, "data_value": True, - "additional_info": lambda x: np.mean(x), + "additional_info": np.mean, }, {"img": np.array([[0, 1], [1, 2]])}, "test data statistics:\nShape: (2, 2)\nValue range: (0, 2)\nValue: [[0 1]\n [1 2]]\nAdditional info: 1.0", @@ -108,7 +108,7 @@ "data_shape": True, "value_range": (True, False), "data_value": (False, True), - "additional_info": (lambda x: np.mean(x), None), + "additional_info": (np.mean, None), }, {"img": np.array([[0, 1], [1, 2]]), "affine": np.eye(2, 2)}, "affine statistics:\nShape: (2, 2)\nValue: [[1. 0.]\n [0. 1.]]", @@ -138,7 +138,7 @@ def test_file(self, input_data, expected_print): "data_shape": True, "value_range": True, "data_value": True, - "additional_info": lambda x: np.mean(x), + "additional_info": np.mean, "logger_handler": handler, } transform = DataStatsd(**input_param) From 0b033d72576edfaaf389c4dae000652d8293461e Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Mon, 11 Jan 2021 16:47:11 +0000 Subject: [PATCH 2/2] fixes compatible attributes in config printing Signed-off-by: Wenqi Li --- monai/config/deviceconfig.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/monai/config/deviceconfig.py b/monai/config/deviceconfig.py index ec65176e58..f543a5a8d4 100644 --- a/monai/config/deviceconfig.py +++ b/monai/config/deviceconfig.py @@ -205,7 +205,8 @@ def get_gpu_info() -> OrderedDict: if num_gpus > 0: _dict_append(output, "Current device", torch.cuda.current_device) - _dict_append(output, "Library compiled for CUDA architectures", torch.cuda.get_arch_list) + if hasattr(torch.cuda, "get_arch_list"): # get_arch_list is new in torch 1.7.1 + _dict_append(output, "Library compiled for CUDA architectures", torch.cuda.get_arch_list) for gpu in range(num_gpus): _dict_append(output, "Info for GPU", gpu) gpu_info = torch.cuda.get_device_properties(gpu)