From ab033e1bd701127fb5f2bcbd8e41795d6ac476f8 Mon Sep 17 00:00:00 2001 From: Matt Einhorn Date: Mon, 22 Apr 2024 01:53:15 -0400 Subject: [PATCH] Apply review suggestions. --- cellfinder/core/tools/IO.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/cellfinder/core/tools/IO.py b/cellfinder/core/tools/IO.py index 3975d5a5..359a6da5 100644 --- a/cellfinder/core/tools/IO.py +++ b/cellfinder/core/tools/IO.py @@ -36,22 +36,22 @@ def read_z_stack(path): :return: The data as a dask/numpy array. """ if path.endswith(".tiff") or path.endswith(".tif"): - tiff = TiffFile(path) - if not len(tiff.series): - raise ValueError( - f"Attempted to load {path} but couldn't read a z-stack" - ) - if len(tiff.series) != 1: - raise ValueError( - f"Attempted to load {path} but found multiple stacks" - ) - - axes = tiff.series[0].axes.lower() - if set(axes) != {"x", "y", "z"} or axes[0].lower() != "z": - raise ValueError( - f"Attempted to load {path} but didn't find a zyx or " - f"zxy stack. Found {axes} axes" - ) + with TiffFile(path) as tiff: + if not len(tiff.series): + raise ValueError( + f"Attempted to load {path} but couldn't read a z-stack" + ) + if len(tiff.series) != 1: + raise ValueError( + f"Attempted to load {path} but found multiple stacks" + ) + + axes = tiff.series[0].axes.lower() + if set(axes) != {"x", "y", "z"} or axes[0] != "z": + raise ValueError( + f"Attempted to load {path} but didn't find a zyx or " + f"zxy stack. Found {axes} axes" + ) return imread(path)