diff --git a/nilearn/_utils/logger.py b/nilearn/_utils/logger.py index 9a02e2e1da..5a5a209a71 100644 --- a/nilearn/_utils/logger.py +++ b/nilearn/_utils/logger.py @@ -71,36 +71,37 @@ def log( is the one which is most likely to have been written in the user's script. """ - if verbose >= msg_level: - stack = inspect.stack() - object_frame = None - object_self = None - for f in reversed(stack): - frame = f[0] - current_self = frame.f_locals.get("self", None) - if isinstance(current_self, object_classes): - object_frame = frame - func_name = f[3] - object_self = current_self - break - - if object_frame is None: # no object found: use stack_level - if stack_level >= len(stack): - func_name = "" - else: - object_frame, _, _, func_name = stack[stack_level][:4] - object_self = object_frame.f_locals.get("self", None) - - if object_self is not None: - func_name = f"{object_self.__class__.__name__}.{func_name}" - - if _has_rich(): - print(f"[blue]\\[{func_name}][/blue] {escape(msg)}") + if verbose < msg_level: + return + stack = inspect.stack() + object_frame = None + object_self = None + for f in reversed(stack): + frame = f[0] + current_self = frame.f_locals.get("self", None) + if isinstance(current_self, object_classes): + object_frame = frame + func_name = f[3] + object_self = current_self + break + + if object_frame is None: # no object found: use stack_level + if stack_level >= len(stack): + func_name = "" else: - print(f"[{func_name}] {msg}") + object_frame, _, _, func_name = stack[stack_level][:4] + object_self = object_frame.f_locals.get("self", None) - if with_traceback: - traceback.print_exc() + if object_self is not None: + func_name = f"{object_self.__class__.__name__}.{func_name}" + + if _has_rich(): + print(f"[blue]\\[{func_name}][/blue] {escape(msg)}") + else: + print(f"[{func_name}] {msg}") + + if with_traceback: + traceback.print_exc() def compose_err_msg(msg, **kwargs): diff --git a/nilearn/_utils/numpy_conversions.py b/nilearn/_utils/numpy_conversions.py index af91e91b7f..01060a4b66 100644 --- a/nilearn/_utils/numpy_conversions.py +++ b/nilearn/_utils/numpy_conversions.py @@ -73,7 +73,7 @@ def as_ndarray(arr, copy=False, dtype=None, order="K"): if isinstance(arr, (np.memmap, list, tuple)) or ( isinstance(arr, np.ndarray) and copy ): - ret = np.array(arr, copy=True, dtype=dtype, order=order) + return np.array(arr, copy=True, dtype=dtype, order=order) # if the order does not change and dtype does not change or # bool to/from 1-byte dtype, # no need to create a copy @@ -86,10 +86,9 @@ def as_ndarray(arr, copy=False, dtype=None, order="K"): or (order == "C" and arr.flags["C_CONTIGUOUS"]) or order in ("K", "A", None) ): - ret = arr.view(dtype=dtype) + return arr.view(dtype=dtype) else: - ret = np.asarray(arr, dtype=dtype, order=order) - return ret + return np.asarray(arr, dtype=dtype, order=order) def csv_to_array(csv_path, delimiters=" \t,;", **kwargs): diff --git a/nilearn/_utils/param_validation.py b/nilearn/_utils/param_validation.py index 59095805e5..8677e67e26 100644 --- a/nilearn/_utils/param_validation.py +++ b/nilearn/_utils/param_validation.py @@ -106,14 +106,13 @@ def _get_mask_extent(mask_img): if hasattr(mask_img, "affine"): affine = mask_img.affine prod_vox_dims = 1.0 * np.abs(np.linalg.det(affine[:3, :3])) - mask_extent = prod_vox_dims * _get_data(mask_img).astype(bool).sum() + return prod_vox_dims * _get_data(mask_img).astype(bool).sum() else: # sum number of True values in both hemispheres - mask_extent = ( + return ( mask_img.data.parts["left"].sum() + mask_img.data.parts["right"].sum() ) - return mask_extent def adjust_screening_percentile(