|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | + |
| 5 | +@pytest.fixture(scope="module") |
| 6 | +def qapp(): |
| 7 | + from qtpy.QtWidgets import QApplication |
| 8 | + app = QApplication.instance() or QApplication([]) |
| 9 | + yield app |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(scope="module") |
| 13 | +def win(qapp): |
| 14 | + from cellpose.gui.gui3d import MainW_3d |
| 15 | + return MainW_3d() |
| 16 | + |
| 17 | + |
| 18 | +def test_saturation_invariants_with_autobtn_unchecked(win): |
| 19 | + """Regression: when 'auto-adjust saturation' is unchecked, loading a 3D |
| 20 | + image must (1) leave self.saturation sized 3 x NZ so that update_plot's |
| 21 | + self.saturation[c][currentZ] index is valid, and (2) preserve the user's |
| 22 | + per-channel values across reloads that change NZ.""" |
| 23 | + from cellpose.gui import io as gui_io |
| 24 | + |
| 25 | + win.autobtn.setChecked(False) |
| 26 | + |
| 27 | + # First load: NZ=10, saturation must be 3 x 10 with valid [lo, hi] entries. |
| 28 | + win.load_3D = True |
| 29 | + win.filename = "test.tif" |
| 30 | + gui_io._initialize_images( |
| 31 | + win, np.zeros((10, 64, 64, 3), dtype=np.float32), load_3D=True |
| 32 | + ) |
| 33 | + |
| 34 | + assert win.NZ == 10 |
| 35 | + assert len(win.saturation) == 3 |
| 36 | + for c in range(3): |
| 37 | + assert len(win.saturation[c]) == win.NZ |
| 38 | + for z in range(win.NZ): |
| 39 | + assert len(win.saturation[c][z]) == 2 # [lo, hi] |
| 40 | + |
| 41 | + # Trigger the original failure path explicitly: move_in_Z -> update_plot |
| 42 | + # -> setLevels(self.saturation[self.color - 1][self.currentZ]). |
| 43 | + win.loaded = True |
| 44 | + win.color = 1 # red channel (color > 0 and < 4 branch) |
| 45 | + win.nchan = 3 |
| 46 | + win.scroll.setValue(win.NZ - 1) |
| 47 | + win.move_in_Z() # would have raised IndexError pre-fix |
| 48 | + |
| 49 | + # User picks custom per-channel values. |
| 50 | + for c in range(3): |
| 51 | + for z in range(win.NZ): |
| 52 | + win.saturation[c][z] = [10 + c, 200 + c] |
| 53 | + |
| 54 | + # Reload with a different NZ; user values should be broadcast across new Z. |
| 55 | + gui_io._initialize_images( |
| 56 | + win, np.zeros((8, 64, 64, 3), dtype=np.float32), load_3D=True |
| 57 | + ) |
| 58 | + |
| 59 | + assert win.NZ == 8 |
| 60 | + for c in range(3): |
| 61 | + assert len(win.saturation[c]) == win.NZ |
| 62 | + for z in range(win.NZ): |
| 63 | + assert win.saturation[c][z] == [10 + c, 200 + c] |
0 commit comments