diff --git a/cellfinder/core/tools/IO.py b/cellfinder/core/tools/IO.py deleted file mode 100644 index 359a6da5..00000000 --- a/cellfinder/core/tools/IO.py +++ /dev/null @@ -1,82 +0,0 @@ -import glob -import os -from typing import Tuple - -import numpy as np -from brainglobe_utils.general.system import get_sorted_file_paths -from dask import array as da -from dask import delayed -from tifffile import TiffFile, imread - - -def get_tiff_meta( - path: str, -) -> Tuple[Tuple[int, int], np.dtype]: - with TiffFile(path) as tfile: - nz = len(tfile.pages) - if not nz: - raise ValueError(f"tiff file {path} has no pages!") - first_page = tfile.pages[0] - - return tfile.pages[0].shape, first_page.dtype - - -lazy_imread = delayed(imread) # lazy reader - - -def read_z_stack(path): - """ - Reads z-stack, lazily, if possible. - - If it's a text file or folder with 2D tiff files use dask to read lazily, - otherwise it's a single file tiff stack and is read into memory. - - :param path: Filename of text file listing 2D tiffs, folder of 2D tiffs, - or single file tiff z-stack. - :return: The data as a dask/numpy array. - """ - if path.endswith(".tiff") or path.endswith(".tif"): - 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) - - return read_with_dask(path) - - -def read_with_dask(path): - """ - Based on https://github.com/tlambert03/napari-ndtiffs - :param path: - :return: - """ - - if path.endswith(".txt"): - with open(path, "r") as f: - filenames = [line.rstrip() for line in f.readlines()] - - else: - filenames = glob.glob(os.path.join(path, "*.tif")) - - shape, dtype = get_tiff_meta(filenames[0]) - lazy_arrays = [lazy_imread(fn) for fn in get_sorted_file_paths(filenames)] - dask_arrays = [ - da.from_delayed(delayed_reader, shape=shape, dtype=dtype) - for delayed_reader in lazy_arrays - ] - stack = da.stack(dask_arrays, axis=0) - return stack diff --git a/tests/core/test_integration/test_detection.py b/tests/core/test_integration/test_detection.py index 727f0741..7d90a007 100644 --- a/tests/core/test_integration/test_detection.py +++ b/tests/core/test_integration/test_detection.py @@ -5,9 +5,9 @@ import numpy as np import pytest from brainglobe_utils.general.system import get_num_processes +from brainglobe_utils.IO.image.load import read_with_dask from cellfinder.core.main import main -from cellfinder.core.tools.IO import read_with_dask data_dir = os.path.join( os.getcwd(), "tests", "data", "integration", "detection" diff --git a/tests/core/test_integration/test_detection_structure_splitting.py b/tests/core/test_integration/test_detection_structure_splitting.py index 87ae7cb2..6fa83d07 100644 --- a/tests/core/test_integration/test_detection_structure_splitting.py +++ b/tests/core/test_integration/test_detection_structure_splitting.py @@ -9,9 +9,9 @@ import os import pytest +from brainglobe_utils.IO.image.load import read_with_dask from cellfinder.core.main import main -from cellfinder.core.tools.IO import read_with_dask data_dir = os.path.join( os.getcwd(), "tests", "data", "integration", "detection" diff --git a/tests/core/test_unit/test_tools/test_IO.py b/tests/core/test_unit/test_tools/test_IO.py deleted file mode 100644 index bba99507..00000000 --- a/tests/core/test_unit/test_tools/test_IO.py +++ /dev/null @@ -1,18 +0,0 @@ -import dask.array as d_array - -from cellfinder.core.tools import IO - -BRAIN_DIR = "tests/data/background" -BRAIN_PATHS = f"{BRAIN_DIR}/background_paths.txt" - - -def test_read_with_dask_txt(): - stack = IO.read_with_dask(BRAIN_PATHS) - assert type(stack) == d_array.Array - - -def test_read_with_dask_glob_txt_equal(): - txt_stack = IO.read_with_dask(BRAIN_PATHS) - glob_stack = IO.read_with_dask(BRAIN_DIR) - - assert d_array.equal(txt_stack, glob_stack).all()