Skip to content

Commit

Permalink
[REF] fix last-if-guard and lift-return-into-if (nilearn#4909)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Remi-Gau and pre-commit-ci[bot] authored Dec 9, 2024
1 parent 31866cf commit 4808962
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
57 changes: 29 additions & 28 deletions nilearn/_utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<top_level>"
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 = "<top_level>"
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):
Expand Down
7 changes: 3 additions & 4 deletions nilearn/_utils/numpy_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
5 changes: 2 additions & 3 deletions nilearn/_utils/param_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 4808962

Please sign in to comment.