|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# |
| 4 | +# Copyright (C) 2025 University of Dundee & Open Microscopy Environment. |
| 5 | +# All rights reserved. |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation; either version 2 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License along |
| 18 | +# with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + |
| 21 | +import json |
| 22 | +from pathlib import Path |
| 23 | + |
| 24 | +import pytest |
| 25 | +from omero.model import RoiI |
| 26 | +from omero.testlib.cli import AbstractCLITest |
| 27 | +from omero_rois import mask_from_binary_image |
| 28 | +from omero_zarr.cli import ZarrControl |
| 29 | + |
| 30 | + |
| 31 | +class TestRender(AbstractCLITest): |
| 32 | + |
| 33 | + def setup_method(self, method: str) -> None: |
| 34 | + """Set up the test.""" |
| 35 | + self.args = self.login_args() |
| 36 | + self.cli.register("zarr", ZarrControl, "TEST") |
| 37 | + self.args += ["zarr"] |
| 38 | + |
| 39 | + # export tests |
| 40 | + # ======================================================================== |
| 41 | + |
| 42 | + def test_export_zarr(self, capsys: pytest.CaptureFixture, tmp_path: Path) -> None: |
| 43 | + """Test export of a Zarr image.""" |
| 44 | + sizec = 2 |
| 45 | + images = self.import_fake_file(sizeC=sizec, client=self.client) |
| 46 | + img_id = images[0].id.val |
| 47 | + self.cli.invoke( |
| 48 | + self.args + ["export", f"Image:{img_id}", "--output", str(tmp_path)], |
| 49 | + strict=True, |
| 50 | + ) |
| 51 | + out, err = capsys.readouterr() |
| 52 | + lines = out.split("\n") |
| 53 | + print(lines) |
| 54 | + all_lines = ", ".join(lines) |
| 55 | + assert "Exporting to" in all_lines |
| 56 | + assert "Finished" in all_lines |
| 57 | + |
| 58 | + assert len(list(tmp_path.iterdir())) == 1 |
| 59 | + assert (tmp_path / f"{img_id}.zarr").is_dir() |
| 60 | + |
| 61 | + attrs_text = (tmp_path / f"{img_id}.zarr" / ".zattrs").read_text( |
| 62 | + encoding="utf-8" |
| 63 | + ) |
| 64 | + attrs_json = json.loads(attrs_text) |
| 65 | + print(attrs_json) |
| 66 | + assert "multiscales" in attrs_json |
| 67 | + assert len(attrs_json["omero"]["channels"]) == sizec |
| 68 | + assert attrs_json["omero"]["channels"][0]["window"]["min"] == 0 |
| 69 | + assert attrs_json["omero"]["channels"][0]["window"]["max"] == 255 |
| 70 | + |
| 71 | + arr_text = (tmp_path / f"{img_id}.zarr" / "0" / ".zarray").read_text( |
| 72 | + encoding="utf-8" |
| 73 | + ) |
| 74 | + arr_json = json.loads(arr_text) |
| 75 | + assert arr_json["shape"] == [sizec, 512, 512] |
| 76 | + |
| 77 | + def test_export_plate(self, capsys: pytest.CaptureFixture, tmp_path: Path) -> None: |
| 78 | + |
| 79 | + plates = self.import_plates( |
| 80 | + client=self.client, |
| 81 | + plates=1, |
| 82 | + plate_acqs=1, |
| 83 | + plate_cols=2, |
| 84 | + plate_rows=2, |
| 85 | + fields=1, |
| 86 | + ) |
| 87 | + plate_id = plates[0].id.val |
| 88 | + self.cli.invoke( |
| 89 | + self.args + ["export", f"Plate:{plate_id}", "--output", str(tmp_path)], |
| 90 | + strict=True, |
| 91 | + ) |
| 92 | + out, err = capsys.readouterr() |
| 93 | + lines = out.split("\n") |
| 94 | + print(lines) |
| 95 | + all_lines = ", ".join(lines) |
| 96 | + assert "Exporting to" in all_lines |
| 97 | + assert "Finished" in all_lines |
| 98 | + assert (tmp_path / f"{plate_id}.zarr").is_dir() |
| 99 | + attrs_text = (tmp_path / f"{plate_id}.zarr" / ".zattrs").read_text( |
| 100 | + encoding="utf-8" |
| 101 | + ) |
| 102 | + attrs_json = json.loads(attrs_text) |
| 103 | + print(attrs_json) |
| 104 | + assert len(attrs_json["plate"]["wells"]) == 4 |
| 105 | + assert attrs_json["plate"]["rows"] == [{"name": "A"}, {"name": "B"}] |
| 106 | + assert attrs_json["plate"]["columns"] == [{"name": "1"}, {"name": "2"}] |
| 107 | + |
| 108 | + arr_text = ( |
| 109 | + tmp_path / f"{plate_id}.zarr" / "A" / "1" / "0" / "0" / ".zarray" |
| 110 | + ).read_text(encoding="utf-8") |
| 111 | + arr_json = json.loads(arr_text) |
| 112 | + assert arr_json["shape"] == [512, 512] |
| 113 | + |
| 114 | + def test_export_masks(self, capsys: pytest.CaptureFixture, tmp_path: Path) -> None: |
| 115 | + """Test export of a Zarr image.""" |
| 116 | + images = self.import_fake_file(sizeC=2, client=self.client) |
| 117 | + img_id = images[0].id.val |
| 118 | + size_xy = 512 |
| 119 | + |
| 120 | + # Create a mask |
| 121 | + from skimage.data import binary_blobs |
| 122 | + |
| 123 | + blobs = binary_blobs(length=size_xy, volume_fraction=0.1, n_dim=2).astype( |
| 124 | + "int8" |
| 125 | + ) |
| 126 | + red = [255, 0, 0, 255] |
| 127 | + mask = mask_from_binary_image(blobs, rgba=red, z=0, c=0, t=0) |
| 128 | + |
| 129 | + roi = RoiI() |
| 130 | + roi.setImage(images[0]) |
| 131 | + roi.addShape(mask) |
| 132 | + updateService = self.client.sf.getUpdateService() |
| 133 | + updateService.saveAndReturnObject(roi) |
| 134 | + |
| 135 | + print("tmp_path", tmp_path) |
| 136 | + |
| 137 | + img_args = [f"Image:{img_id}", "--output", str(tmp_path)] |
| 138 | + self.cli.invoke( |
| 139 | + self.args + ["export"] + img_args, |
| 140 | + strict=True, |
| 141 | + ) |
| 142 | + |
| 143 | + self.cli.invoke( |
| 144 | + self.args + ["masks"] + img_args, |
| 145 | + strict=True, |
| 146 | + ) |
| 147 | + |
| 148 | + out, err = capsys.readouterr() |
| 149 | + lines = out.split("\n") |
| 150 | + print(lines) |
| 151 | + all_lines = ", ".join(lines) |
| 152 | + assert "Exporting to" in all_lines |
| 153 | + assert "Finished" in all_lines |
| 154 | + assert "Found 1 mask shapes in 1 ROIs" in all_lines |
| 155 | + |
| 156 | + labels_text = ( |
| 157 | + tmp_path / f"{img_id}.zarr" / "labels" / "0" / ".zattrs" |
| 158 | + ).read_text(encoding="utf-8") |
| 159 | + labels_json = json.loads(labels_text) |
| 160 | + assert labels_json["image-label"]["colors"] == [{"label-value": 1, "rgba": red}] |
| 161 | + |
| 162 | + arr_text = ( |
| 163 | + tmp_path / f"{img_id}.zarr" / "labels" / "0" / "0" / ".zarray" |
| 164 | + ).read_text(encoding="utf-8") |
| 165 | + arr_json = json.loads(arr_text) |
| 166 | + assert arr_json["shape"] == [1, 512, 512] |
0 commit comments