|
| 1 | +import numpy as np |
| 2 | +import importlib.util |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | + |
| 6 | +_DELETE_UTILS_PATH = ( |
| 7 | + Path(__file__).resolve().parents[1] / "cellpose/gui/delete_utils.py" |
| 8 | +) |
| 9 | +_DELETE_UTILS_SPEC = importlib.util.spec_from_file_location( |
| 10 | + "cellpose_gui_delete_utils", _DELETE_UTILS_PATH |
| 11 | +) |
| 12 | +delete_utils = importlib.util.module_from_spec(_DELETE_UTILS_SPEC) |
| 13 | +_DELETE_UTILS_SPEC.loader.exec_module(delete_utils) |
| 14 | + |
| 15 | + |
| 16 | +def _legacy_remove_state(cellpix, outpix, ismanual, cellcolors, zdraw, remove_ids): |
| 17 | + cellpix = cellpix.copy() |
| 18 | + outpix = outpix.copy() |
| 19 | + ismanual = ismanual.copy() |
| 20 | + cellcolors = cellcolors.copy() |
| 21 | + zdraw = list(zdraw) |
| 22 | + |
| 23 | + for idx in remove_ids: |
| 24 | + cp = cellpix == idx |
| 25 | + op = outpix == idx |
| 26 | + cellpix[cp] = 0 |
| 27 | + outpix[op] = 0 |
| 28 | + cellpix[cellpix > idx] -= 1 |
| 29 | + outpix[outpix > idx] -= 1 |
| 30 | + ismanual = np.delete(ismanual, idx - 1) |
| 31 | + cellcolors = np.delete(cellcolors, [idx], axis=0) |
| 32 | + del zdraw[idx - 1] |
| 33 | + |
| 34 | + return cellpix, outpix, ismanual, cellcolors, zdraw |
| 35 | + |
| 36 | + |
| 37 | +def _random_state(seed, nz=1, ly=64, lx=64, ncells=40): |
| 38 | + rng = np.random.default_rng(seed) |
| 39 | + dtype = np.uint16 if ncells < 2**16 - 1 else np.uint32 |
| 40 | + |
| 41 | + cellpix = rng.integers(0, ncells + 1, size=(nz, ly, lx), dtype=dtype) |
| 42 | + force_idx = rng.choice(cellpix.size, size=ncells, replace=False) |
| 43 | + cellpix.flat[force_idx] = np.arange(1, ncells + 1, dtype=dtype) |
| 44 | + |
| 45 | + outline_mask = rng.random(cellpix.shape) < 0.2 |
| 46 | + outpix = np.where(outline_mask, cellpix, 0).astype(dtype, copy=False) |
| 47 | + |
| 48 | + ismanual = rng.integers(0, 2, size=ncells, dtype=np.uint8).astype(bool) |
| 49 | + cellcolors = rng.integers(0, 256, size=(ncells + 1, 3), dtype=np.uint8) |
| 50 | + cellcolors[0] = np.array([255, 255, 255], dtype=np.uint8) |
| 51 | + |
| 52 | + zdraw = [] |
| 53 | + for _ in range(ncells): |
| 54 | + nplanes = int(rng.integers(1, max(2, nz + 1))) |
| 55 | + zdraw.append(list(rng.integers(0, max(1, nz), size=nplanes))) |
| 56 | + |
| 57 | + return cellpix, outpix, ismanual, cellcolors, zdraw |
| 58 | + |
| 59 | + |
| 60 | +def _assert_state_equal(expected, actual): |
| 61 | + exp_cellpix, exp_outpix, exp_ismanual, exp_cellcolors, exp_zdraw = expected |
| 62 | + got_cellpix, got_outpix, got_ismanual, got_cellcolors, got_zdraw = actual |
| 63 | + |
| 64 | + assert np.array_equal(exp_cellpix, got_cellpix) |
| 65 | + assert np.array_equal(exp_outpix, got_outpix) |
| 66 | + assert np.array_equal(exp_ismanual, got_ismanual) |
| 67 | + assert np.array_equal(exp_cellcolors, got_cellcolors) |
| 68 | + assert len(exp_zdraw) == len(got_zdraw) |
| 69 | + for z0, z1 in zip(exp_zdraw, got_zdraw): |
| 70 | + assert np.array_equal(np.asarray(z0), np.asarray(z1)) |
| 71 | + |
| 72 | + |
| 73 | +def test_batch_delete_reindex_matches_legacy_small_example(): |
| 74 | + cellpix = np.array( |
| 75 | + [[[1, 1, 2, 2], [1, 3, 3, 2], [4, 4, 5, 5], [4, 0, 5, 5]]], dtype=np.uint16 |
| 76 | + ) |
| 77 | + outpix = np.array( |
| 78 | + [[[1, 0, 2, 0], [0, 3, 0, 2], [4, 0, 5, 0], [0, 0, 0, 5]]], dtype=np.uint16 |
| 79 | + ) |
| 80 | + ismanual = np.array([True, False, True, False, True]) |
| 81 | + cellcolors = np.array( |
| 82 | + [[255, 255, 255], [10, 0, 0], [20, 0, 0], [30, 0, 0], [40, 0, 0], [50, 0, 0]], |
| 83 | + dtype=np.uint8, |
| 84 | + ) |
| 85 | + zdraw = [[0], [0], [0], [0], [0]] |
| 86 | + |
| 87 | + remove_ids = np.array([5, 3, 2], dtype=np.int64) |
| 88 | + |
| 89 | + expected = _legacy_remove_state( |
| 90 | + cellpix, outpix, ismanual, cellcolors, zdraw, remove_ids |
| 91 | + ) |
| 92 | + got = delete_utils.batch_delete_reindex( |
| 93 | + cellpix, outpix, ismanual, cellcolors, zdraw, remove_ids |
| 94 | + )[:5] |
| 95 | + _assert_state_equal(expected, got) |
| 96 | + |
| 97 | + |
| 98 | +def test_batch_delete_reindex_matches_legacy_random_2d(): |
| 99 | + for seed in range(20): |
| 100 | + cellpix, outpix, ismanual, cellcolors, zdraw = _random_state(seed, nz=1) |
| 101 | + rng = np.random.default_rng(seed + 1000) |
| 102 | + ncells = len(cellcolors) - 1 |
| 103 | + remove_n = int(rng.integers(1, ncells + 1)) |
| 104 | + remove_ids = rng.choice(np.arange(1, ncells + 1), size=remove_n, replace=False) |
| 105 | + remove_ids = delete_utils.normalize_remove_ids(remove_ids, ncells) |
| 106 | + |
| 107 | + expected = _legacy_remove_state( |
| 108 | + cellpix, outpix, ismanual, cellcolors, zdraw, remove_ids |
| 109 | + ) |
| 110 | + got = delete_utils.batch_delete_reindex( |
| 111 | + cellpix, outpix, ismanual, cellcolors, zdraw, remove_ids |
| 112 | + )[:5] |
| 113 | + _assert_state_equal(expected, got) |
| 114 | + |
| 115 | + |
| 116 | +def test_batch_delete_reindex_matches_legacy_random_3d(): |
| 117 | + for seed in range(12): |
| 118 | + cellpix, outpix, ismanual, cellcolors, zdraw = _random_state(seed + 100, nz=4) |
| 119 | + rng = np.random.default_rng(seed + 2000) |
| 120 | + ncells = len(cellcolors) - 1 |
| 121 | + remove_n = int(rng.integers(1, ncells + 1)) |
| 122 | + remove_ids = rng.choice(np.arange(1, ncells + 1), size=remove_n, replace=False) |
| 123 | + remove_ids = delete_utils.normalize_remove_ids(remove_ids, ncells) |
| 124 | + |
| 125 | + expected = _legacy_remove_state( |
| 126 | + cellpix, outpix, ismanual, cellcolors, zdraw, remove_ids |
| 127 | + ) |
| 128 | + got = delete_utils.batch_delete_reindex( |
| 129 | + cellpix, outpix, ismanual, cellcolors, zdraw, remove_ids |
| 130 | + )[:5] |
| 131 | + _assert_state_equal(expected, got) |
| 132 | + |
| 133 | + |
| 134 | +def test_batch_delete_reindex_noop_invalid_ids(): |
| 135 | + cellpix, outpix, ismanual, cellcolors, zdraw = _random_state(999, nz=1) |
| 136 | + ncells = len(cellcolors) - 1 |
| 137 | + remove_ids = np.array([0, -1, ncells + 10], dtype=np.int64) |
| 138 | + |
| 139 | + got = delete_utils.batch_delete_reindex( |
| 140 | + cellpix, outpix, ismanual, cellcolors, zdraw, remove_ids |
| 141 | + ) |
| 142 | + got_state = got[:5] |
| 143 | + out_ids = got[5] |
| 144 | + remove_mask = got[6] |
| 145 | + |
| 146 | + _assert_state_equal((cellpix, outpix, ismanual, cellcolors, zdraw), got_state) |
| 147 | + assert out_ids.size == 0 |
| 148 | + assert not remove_mask.any() |
0 commit comments