diff --git a/.github/workflows/publish_pypi.yml b/.github/workflows/publish_pypi.yml index 5e79d76..2a8ae23 100644 --- a/.github/workflows/publish_pypi.yml +++ b/.github/workflows/publish_pypi.yml @@ -18,9 +18,9 @@ jobs: MACOSX_DEPLOYMENT_TARGET: "10.15" strategy: matrix: - os: [ubuntu-22.04, macos-15-intel, windows-latest] + os: [ubuntu-22.04, macos-15-intel, windows-2022] cibw_archs: ["auto64"] - cibw_build: ["cp310", "cp311", "cp312", "cp313"] + cibw_build: ["cp310", "cp311", "cp312", "cp313", "cp314", "cp315"] steps: - uses: actions/checkout@v3 @@ -30,11 +30,11 @@ jobs: name: Add MSVS Path - name: Add NASM - if: matrix.os == 'windows-latest' + if: matrix.os == 'windows-2022' uses: ilammy/setup-nasm@v1 - name: Add Ninja - if: matrix.os == 'windows-latest' + if: matrix.os == 'windows-2022' uses: seanmiddleditch/gha-setup-ninja@master - uses: actions/setup-python@v4 @@ -89,10 +89,12 @@ jobs: CIBW_ARCHS: ${{ matrix.cibw_archs }} CIBW_BEFORE_TEST_LINUX: dnf -y module reset maven || true && dnf -y module enable maven:3.8 && - dnf -y install --setopt=install_weak_deps=False java-17-openjdk-headless && + dnf -y install --setopt=install_weak_deps=False java-17-openjdk-devel && dnf -y install --setopt=install_weak_deps=False maven maven-openjdk17 - CIBW_TEST_REQUIRES: bfio requests numpy ome_zarr + CIBW_TEST_REQUIRES: bfio requests numpy CIBW_TEST_COMMAND: python -W default -m unittest discover -s {project}/tests -v + # ponytail: jpype1/numcodecs (via bfio) have no cp315 wheels on Linux/Windows; remove once they do + CIBW_TEST_SKIP: "cp315-manylinux_* cp315-win_*" - name: Install Dependencies run: python -m pip install --upgrade twine requests @@ -111,9 +113,9 @@ jobs: MACOSX_DEPLOYMENT_TARGET: "11.0" strategy: matrix: - os: [macos-14] + os: [macos-15] cibw_archs: ["arm64"] - cibw_build: ["cp310", "cp311", "cp312", "cp313"] + cibw_build: ["cp310", "cp311", "cp312", "cp313", "cp314", "cp315"] steps: - uses: actions/checkout@v3 @@ -151,7 +153,7 @@ jobs: DYLD_LIBRARY_PATH=$REPAIR_LIBRARY_PATH delocate-listdeps {wheel} && DYLD_LIBRARY_PATH=$REPAIR_LIBRARY_PATH delocate-wheel --require-archs {delocate_archs} -w {dest_dir} {wheel} CIBW_ARCHS: ${{ matrix.cibw_archs }} - CIBW_TEST_REQUIRES: bfio requests numpy ome_zarr + CIBW_TEST_REQUIRES: bfio requests numpy CIBW_TEST_COMMAND: python -W default -m unittest discover -s {project}/tests -v - name: Install Dependencies diff --git a/.github/workflows/wheel_build.yml b/.github/workflows/wheel_build.yml index 595ca05..a9ec6f5 100644 --- a/.github/workflows/wheel_build.yml +++ b/.github/workflows/wheel_build.yml @@ -17,9 +17,9 @@ jobs: MACOSX_DEPLOYMENT_TARGET: "10.15" strategy: matrix: - os: [ubuntu-22.04, macos-15-intel, windows-latest] + os: [ubuntu-22.04, macos-15-intel, windows-2022] cibw_archs: ["auto64"] - cibw_build: ["cp310", "cp311", "cp312", "cp313"] + cibw_build: ["cp310", "cp311", "cp312", "cp313", "cp314", "cp315"] steps: - uses: actions/checkout@v3 @@ -29,11 +29,11 @@ jobs: name: Add MSVS Path - name: Add NASM - if: matrix.os == 'windows-latest' + if: matrix.os == 'windows-2022' uses: ilammy/setup-nasm@v1 - name: Add Ninja - if: matrix.os == 'windows-latest' + if: matrix.os == 'windows-2022' uses: seanmiddleditch/gha-setup-ninja@master - uses: actions/setup-python@v4 @@ -88,10 +88,12 @@ jobs: CIBW_ARCHS: ${{ matrix.cibw_archs }} CIBW_BEFORE_TEST_LINUX: dnf -y module reset maven || true && dnf -y module enable maven:3.8 && - dnf -y install --setopt=install_weak_deps=False java-17-openjdk-headless && + dnf -y install --setopt=install_weak_deps=False java-17-openjdk-devel && dnf -y install --setopt=install_weak_deps=False maven maven-openjdk17 - CIBW_TEST_REQUIRES: "bfio requests numpy ome_zarr" + CIBW_TEST_REQUIRES: "bfio requests numpy" CIBW_TEST_COMMAND: python -W default -m unittest discover -s {project}/tests -v + # ponytail: jpype1/numcodecs (via bfio) have no cp315 wheels on Linux/Windows; remove once they do + CIBW_TEST_SKIP: "cp315-manylinux_* cp315-win_*" - name: Upload Artifact uses: actions/upload-artifact@v4 @@ -109,9 +111,9 @@ jobs: MACOSX_DEPLOYMENT_TARGET: "11.0" strategy: matrix: - os: [macos-14] + os: [macos-15] cibw_archs: ["arm64"] - cibw_build: ["cp310", "cp311", "cp312", "cp313"] + cibw_build: ["cp310", "cp311", "cp312", "cp313", "cp314", "cp315"] steps: - uses: actions/checkout@v3 @@ -149,7 +151,7 @@ jobs: DYLD_LIBRARY_PATH=$REPAIR_LIBRARY_PATH delocate-listdeps {wheel} && DYLD_LIBRARY_PATH=$REPAIR_LIBRARY_PATH delocate-wheel --require-archs {delocate_archs} -w {dest_dir} {wheel} CIBW_ARCHS: ${{ matrix.cibw_archs }} - CIBW_TEST_REQUIRES: "bfio requests numpy ome_zarr zarr" + CIBW_TEST_REQUIRES: "bfio requests numpy zarr" CIBW_TEST_COMMAND: python -W default -m unittest discover -s {project}/tests -v - name: Upload Artifact diff --git a/tests/_data.py b/tests/_data.py new file mode 100644 index 0000000..3d1787b --- /dev/null +++ b/tests/_data.py @@ -0,0 +1,41 @@ +"""Download helpers for test data, with retries and size/zip validation.""" +import pathlib +import shutil +import time +import urllib.request +import zipfile + +# Pinned to a commit of sameeul/polus-test-data (merge of PR #3). +DATA_REPO = ( + "https://raw.githubusercontent.com/sameeul/polus-test-data/" + "9ec79af23d155a2bdb74823fa6fc6f204bbe2ca4/bfio" +) +SMALL_ZARR_URL = f"{DATA_REPO}/small_5d.zarr.zip" + + +def fetch(url, dest, tries=4, timeout=60): + """Download url to dest, retrying with backoff; raises on final failure.""" + dest = pathlib.Path(dest) + for i in range(tries): + try: + with urllib.request.urlopen(url, timeout=timeout) as r: + data = r.read() + dest.write_bytes(data) + return dest + except Exception: + if i == tries - 1: + raise + time.sleep(2**i) + + +def fetch_zarr(url, test_dir): + """Download a zipped zarr and unpack it into test_dir.""" + test_dir = pathlib.Path(test_dir) + z = fetch(url, test_dir / "tmp_zarr.zip") + with zipfile.ZipFile(z) as zf: + zf.extractall(test_dir) + z.unlink() + + +def clean(test_dir): + shutil.rmtree(test_dir, ignore_errors=True) diff --git a/tests/test_read.py b/tests/test_read.py index e97adc6..3f9e67f 100644 --- a/tests/test_read.py +++ b/tests/test_read.py @@ -1,19 +1,20 @@ from bfiocpp import TSReader, Seq, FileType import unittest -import requests, pathlib, shutil, logging, sys -# SEE : Initialization of bio-formats java backend https://bio-formats.readthedocs.io/en/stable/developers/java-library.html -# The order of initialization between ome_zarr.utils and bfio matters -from ome_zarr.utils import download as zarr_download +import pathlib, shutil, logging, sys import bfio import numpy as np import random TEST_IMAGES = { - "5025551.zarr": "https://uk1s3.embassy.ebi.ac.uk/idr/zarr/v0.4/idr0054A/5025551.zarr", "p01_x01_y01_wx0_wy0_c1.ome.tif": "https://raw.githubusercontent.com/sameeul/polus-test-data/main/bfio/p01_x01_y01_wx0_wy0_c1.ome.tif", "Plate1-Blue-A-12-Scene-3-P3-F2-03.czi": "https://downloads.openmicroscopy.org/images/Zeiss-CZI/idr0011/Plate1-Blue-A_TS-Stinger/Plate1-Blue-A-12-Scene-3-P3-F2-03.czi", } +try: + from ._data import SMALL_ZARR_URL, fetch, fetch_zarr +except ImportError: # run via `unittest discover -s tests` + from _data import SMALL_ZARR_URL, fetch, fetch_zarr + TEST_DIR = pathlib.Path(__file__).with_name("data") logging.basicConfig( @@ -30,21 +31,12 @@ def setUpModule(): """Download images for testing""" TEST_DIR.mkdir(exist_ok=True) + fetch_zarr(SMALL_ZARR_URL, TEST_DIR) + for file, url in TEST_IMAGES.items(): logger.info(f"setup - Downloading: {file}") - - if not file.endswith(".zarr"): - if TEST_DIR.joinpath(file).exists(): - continue - - r = requests.get(url) - - with open(TEST_DIR.joinpath(file), "wb") as fw: - fw.write(r.content) - else: - if TEST_DIR.joinpath(file).exists(): - shutil.rmtree(TEST_DIR.joinpath(file)) - zarr_download(url, str(TEST_DIR)) + if not TEST_DIR.joinpath(file).exists(): + fetch(url, TEST_DIR.joinpath(file)) """Load the czi image, and save as a npy file for further testing.""" with bfio.BioReader( @@ -210,14 +202,14 @@ class TestOmeZarrRead(unittest.TestCase): def test_read_zarr_2d_slice(self): """test_read_zarr_2d_slice - Read tiff using TSReader""" br = TSReader( - str(TEST_DIR.joinpath("5025551.zarr/0")), + str(TEST_DIR.joinpath("small_5d.zarr/0")), FileType.OmeZarrV2, "", ) - assert br._X == 2702 - assert br._Y == 2700 + assert br._X == 302 + assert br._Y == 300 assert br._Z == 1 - assert br._C == 27 + assert br._C == 4 assert br._T == 1 rows = Seq(0, br._Y - 1, 1) @@ -228,24 +220,24 @@ def test_read_zarr_2d_slice(self): tmp = br.data(rows, cols, layers, channels, tsteps) assert tmp.dtype == np.uint8 - assert tmp.sum() == 183750394 - assert tmp.shape == (1, 1, 1, 2700, 2702) + assert tmp.sum() == 11522078 + assert tmp.shape == (1, 1, 1, 300, 302) def test_read_zarr_4d_slice(self): """test_read_zarr_4d_slice - Read tiff using TSReader""" br = TSReader( - str(TEST_DIR.joinpath("5025551.zarr/0")), + str(TEST_DIR.joinpath("small_5d.zarr/0")), FileType.OmeZarrV2, "", ) - rows = Seq(0, 1023, 1) - cols = Seq(0, 1023, 1) + rows = Seq(0, 255, 1) + cols = Seq(0, 255, 1) layers = Seq(0, 0, 1) channels = Seq(0, 3, 1) tsteps = Seq(0, 0, 1) tmp = br.data(rows, cols, layers, channels, tsteps) assert tmp.dtype == np.uint8 - assert tmp.sum() == 81778531 - assert tmp.shape == (1, 4, 1, 1024, 1024) + assert tmp.sum() == 33383535 + assert tmp.shape == (1, 4, 1, 256, 256) diff --git a/tests/test_write.py b/tests/test_write.py index 92eb662..ecef888 100644 --- a/tests/test_write.py +++ b/tests/test_write.py @@ -1,16 +1,14 @@ from bfiocpp import TSReader, TSWriter, Seq, FileType import unittest -import requests, pathlib, shutil, logging, sys -# SEE : Initialization of bio-formats java backend https://bio-formats.readthedocs.io/en/stable/developers/java-library.html -# The order of initialization between ome_zarr.utils and bfio matters -from ome_zarr.utils import download as zarr_download +import pathlib, shutil, logging, sys import bfio import numpy as np import tempfile, os -TEST_IMAGES = { - "5025551.zarr": "https://uk1s3.embassy.ebi.ac.uk/idr/zarr/v0.4/idr0054A/5025551.zarr", -} +try: + from ._data import SMALL_ZARR_URL, fetch_zarr +except ImportError: # run via `unittest discover -s tests` + from _data import SMALL_ZARR_URL, fetch_zarr TEST_DIR = pathlib.Path(__file__).with_name("data") @@ -27,22 +25,7 @@ def setUpModule(): """Download images for testing""" TEST_DIR.mkdir(exist_ok=True) - - for file, url in TEST_IMAGES.items(): - logger.info(f"setup - Downloading: {file}") - - if not file.endswith(".zarr"): - if TEST_DIR.joinpath(file).exists(): - continue - - r = requests.get(url) - - with open(TEST_DIR.joinpath(file), "wb") as fw: - fw.write(r.content) - else: - if TEST_DIR.joinpath(file).exists(): - shutil.rmtree(TEST_DIR.joinpath(file)) - zarr_download(url, str(TEST_DIR)) + fetch_zarr(SMALL_ZARR_URL, TEST_DIR) def tearDownModule(): @@ -58,15 +41,15 @@ def test_write_zarr_5d(self): """test_write_zarr_5d - Write zarr using TSWriter""" br = TSReader( - str(TEST_DIR.joinpath("5025551.zarr/0")), + str(TEST_DIR.joinpath("small_5d.zarr/0")), FileType.OmeZarrV2, "", ) - assert br._X == 2702 - assert br._Y == 2700 + assert br._X == 302 + assert br._Y == 300 assert br._Z == 1 - assert br._C == 27 + assert br._C == 4 assert br._T == 1 rows = Seq(0, br._Y - 1, 1) @@ -99,22 +82,22 @@ def test_write_zarr_5d(self): tmp = br.data(rows, cols, layers, channels, tsteps) assert tmp.dtype == np.uint8 - assert tmp.sum() == 183750394 - assert tmp.shape == (1, 1, 1, 2700, 2702) + assert tmp.sum() == 11522078 + assert tmp.shape == (1, 1, 1, 300, 302) def test_write_zarr_3d(self): """test_write_zarr_5d - Write zarr using TSWriter""" br = TSReader( - str(TEST_DIR.joinpath("5025551.zarr/0")), + str(TEST_DIR.joinpath("small_5d.zarr/0")), FileType.OmeZarrV2, "", ) - assert br._X == 2702 - assert br._Y == 2700 + assert br._X == 302 + assert br._Y == 300 assert br._Z == 1 - assert br._C == 27 + assert br._C == 4 assert br._T == 1 rows = Seq(0, br._Y - 1, 1) @@ -147,22 +130,22 @@ def test_write_zarr_3d(self): tmp = br.data(rows, cols, layers) assert tmp.dtype == np.uint8 - assert tmp.sum() == 183750394 - assert tmp.shape == (1, 2700, 2702) + assert tmp.sum() == 11522078 + assert tmp.shape == (1, 300, 302) def test_write_zarr_chunk_5d(self): """test_write_zarr_5d - Write zarr using TsWriter""" br = TSReader( - str(TEST_DIR.joinpath("5025551.zarr/0")), + str(TEST_DIR.joinpath("small_5d.zarr/0")), FileType.OmeZarrV2, "", ) - assert br._X == 2702 - assert br._Y == 2700 + assert br._X == 302 + assert br._Y == 300 assert br._Z == 1 - assert br._C == 27 + assert br._C == 4 assert br._T == 1 rows = Seq(0, br._Y - 1, 1) @@ -177,7 +160,7 @@ def test_write_zarr_chunk_5d(self): test_file_path = os.path.join(dir, 'out/test.ome.zarr') print(tmp.shape) - chunk_shape = [1, 1, 1, 1350, 1351] + chunk_shape = [1, 1, 1, 150, 151] #chunk_shape = tmp.shape print("dtype: " + str(tmp.dtype)) bw = TSWriter(test_file_path, tmp.shape, chunk_shape, str(tmp.dtype), "TCZYX") @@ -217,21 +200,21 @@ def test_write_zarr_chunk_5d(self): print(tmp) assert tmp.dtype == np.uint8 - assert tmp.shape == (1, 1, 1, 2700, 2702) - assert tmp.sum() == 183750394 + assert tmp.shape == (1, 1, 1, 300, 302) + assert tmp.sum() == 11522078 def test_write_zarr_3d(self): """test_write_zarr_5d - Write zarr using TsWriter""" br = TSReader( - str(TEST_DIR.joinpath("5025551.zarr/0")), + str(TEST_DIR.joinpath("small_5d.zarr/0")), FileType.OmeZarrV2, "", ) - assert br._X == 2702 - assert br._Y == 2700 + assert br._X == 302 + assert br._Y == 300 assert br._Z == 1 - assert br._C == 27 + assert br._C == 4 assert br._T == 1 rows = Seq(0, br._Y - 1, 1) @@ -241,7 +224,7 @@ def test_write_zarr_3d(self): tsteps = Seq(0, 0, 1) tmp = br.data(rows, cols, layers, channels, tsteps) - # Update shape to (1, 3, 1, 2700, 2702) + # Update shape to (1, 3, 1, 300, 302) tmp = np.repeat(tmp, 3, axis=1) with tempfile.TemporaryDirectory() as dir: @@ -267,22 +250,22 @@ def test_write_zarr_3d(self): tmp = br.data(rows, cols, layers, channels, tsteps) assert tmp.dtype == np.uint8 - assert tmp.sum() == 3*183750394 - assert tmp.shape == (1, 3, 1, 2700, 2702) + assert tmp.sum() == 3*11522078 + assert tmp.shape == (1, 3, 1, 300, 302) def test_write_zarr_chunk_3d(self): """test_write_zarr_5d - Write zarr using TsWriter""" br = TSReader( - str(TEST_DIR.joinpath("5025551.zarr/0")), + str(TEST_DIR.joinpath("small_5d.zarr/0")), FileType.OmeZarrV2, "", ) - assert br._X == 2702 - assert br._Y == 2700 + assert br._X == 302 + assert br._Y == 300 assert br._Z == 1 - assert br._C == 27 + assert br._C == 4 assert br._T == 1 rows = Seq(0, br._Y - 1, 1) @@ -292,7 +275,7 @@ def test_write_zarr_chunk_3d(self): tsteps = Seq(0, 0, 1) tmp = br.data(rows, cols, layers, channels, tsteps) - # Update shape to (1, 3, 1, 2700, 2702) + # Update shape to (1, 3, 1, 300, 302) tmp = np.repeat(tmp, 3, axis=1) with tempfile.TemporaryDirectory() as dir: @@ -344,13 +327,13 @@ def test_write_zarr_chunk_3d(self): tmp = br.data(rows, cols, layers, channels, tsteps) assert tmp.dtype == np.uint8 - assert tmp.sum() == 3*183750394 - assert tmp.shape == (1, 3, 1, 2700, 2702) + assert tmp.sum() == 3*11522078 + assert tmp.shape == (1, 3, 1, 300, 302) def test_invalid_dimension_order_no_X(self): br = TSReader( - str(TEST_DIR.joinpath("5025551.zarr/0")), + str(TEST_DIR.joinpath("small_5d.zarr/0")), FileType.OmeZarrV2, "", ) @@ -373,7 +356,7 @@ def test_invalid_dimension_order_no_X(self): def test_invalid_dimension_order_no_Y(self): br = TSReader( - str(TEST_DIR.joinpath("5025551.zarr/0")), + str(TEST_DIR.joinpath("small_5d.zarr/0")), FileType.OmeZarrV2, "", ) @@ -396,7 +379,7 @@ def test_invalid_dimension_order_no_Y(self): def test_invalid_dimension_order_character(self): br = TSReader( - str(TEST_DIR.joinpath("5025551.zarr/0")), + str(TEST_DIR.joinpath("small_5d.zarr/0")), FileType.OmeZarrV2, "", )