Skip to content

Commit ef481bf

Browse files
authored
Merge pull request #2 from MouseLand/ring_artifacts-refactor
- [simplify type cheking](yuriyzubov@ee58d70) - [fix: do flow3D smoothing tests on 3d data, not 2d](yuriyzubov@e3d6c80) - CLI refactor: [adjust argparse to allow list/number input to flow3D_smooth](yuriyzubov@cb8aeee)
2 parents 93d024e + cb8aeee commit ef481bf

3 files changed

Lines changed: 26 additions & 19 deletions

File tree

cellpose/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ def get_arg_parser():
109109
"--min_size", required=False, default=15, type=int,
110110
help="minimum number of pixels per mask, can turn off with -1")
111111
algorithm_args.add_argument(
112-
"--flow3D_smooth", required=False, default=0, type=float,
113-
help="stddev of gaussian for smoothing of dP for dynamics in 3D, default of 0 means no smoothing")
112+
"--flow3D_smooth", required=False, default=0, type=float, nargs='+',
113+
help="stddev of gaussian for smoothing of dP for dynamics in 3D, default of 0 means no smoothing. Pass a list of values to allow smoothing of the ZYX axes independently")
114114
algorithm_args.add_argument(
115115
"--flow_threshold", default=0.4, type=float, help=
116116
"flow error threshold, 0 turns off this optional QC step. Default: %(default)s")

cellpose/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,11 @@ def eval(self, x, batch_size=8, resample=True, channels=None, channel_axis=None,
340340
dP = transforms.resize_image(dP.transpose(1, 2, 3, 0), Ly=Ly_0, Lx=Lx_0, no_channels=False).transpose(3, 0, 1, 2)
341341
cellprob = transforms.resize_image(cellprob, Ly=Ly_0, Lx=Lx_0, no_channels=True)
342342

343-
if do_3D and flow3D_smooth > 0:
344-
if isinstance(flow3D_smooth, int) or isinstance(flow3D_smooth, float):
343+
if do_3D and flow3D_smooth:
344+
if isinstance(flow3D_smooth, (int, float)):
345345
flow3D_smooth = [flow3D_smooth]*3
346+
if isinstance(flow3D_smooth, list) and len(flow3D_smooth) == 1:
347+
flow3D_smooth = flow3D_smooth*3
346348
if len(flow3D_smooth) == 3 and any(v > 0 for v in flow3D_smooth):
347349
models_logger.info(f"smoothing flows with ZYX sigma={flow3D_smooth}")
348350
dP = gaussian_filter(dP, [0, *flow3D_smooth])

tests/test_output.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,25 @@ def clear_output(data_dir, image_names):
3939
os.remove(npy_output)
4040

4141

42-
@pytest.mark.parametrize('compute_masks, resample, diameter, flow3D_smooth',
42+
@pytest.mark.parametrize('compute_masks, resample, diameter',
4343
[
44-
(True, True, 40, None),
45-
(True, True, None, None),
46-
(False, True, None, None),
47-
(False, False, None, None),
48-
(True, False, None, None),
49-
(True, False, 40, None),
50-
(False, True, None, 2),
51-
(False, True, None, [2, 0, 0]),
52-
(False, False, None, 2),
44+
(True, True, 40),
45+
(True, True, None),
46+
(False, True, None),
47+
(False, False, None),
48+
(True, False, None),
49+
(True, False, 40),
5350
]
5451
)
55-
def test_class_2D_one_img(data_dir, image_names, cellposemodel_fixture_24layer, compute_masks, resample, diameter, flow3D_smooth):
52+
def test_class_2D_one_img(data_dir, image_names, cellposemodel_fixture_24layer, compute_masks, resample, diameter):
5653
clear_output(data_dir, image_names)
5754

5855
img_file = data_dir / '2D' / image_names[0]
5956

6057
img = io.imread_2D(img_file)
6158
# flowps = io.imread(img_file.parent / (img_file.stem + "_cp4_gt_flowps.tif"))
6259

63-
masks_pred, _, _ = cellposemodel_fixture_24layer.eval(img, normalize=True, compute_masks=compute_masks, resample=resample, diameter=diameter, flow3D_smooth=flow3D_smooth)
60+
masks_pred, _, _ = cellposemodel_fixture_24layer.eval(img, normalize=True, compute_masks=compute_masks, resample=resample, diameter=diameter)
6461

6562
if not compute_masks:
6663
# not compute_masks won't return masks so can't check
@@ -175,13 +172,21 @@ def test_cli_3D_diam_anisotropy_shape(data_dir, image_names_3d, diam, aniso):
175172
compare_mask_shapes(data_dir, image_names_3d[0], "3D")
176173
clear_output(data_dir, image_names_3d)
177174

178-
175+
@pytest.mark.parametrize('flow3D_smooth',
176+
[None, 2, [1., 0., 0.]])
179177
@pytest.mark.slow
180-
def test_cli_3D_one_img(data_dir, image_names_3d):
178+
def test_cli_3D_one_img(data_dir, image_names_3d, flow3D_smooth):
181179
clear_output(data_dir, image_names_3d)
182180
use_gpu = torch.cuda.is_available() or torch.backends.mps.is_available()
183181
gpu_string = "--use_gpu" if use_gpu else ""
184-
cmd = f"python -m cellpose --image_path {str(data_dir / '3D' / image_names_3d[0])} --do_3D --save_tif {gpu_string} --verbose"
182+
183+
flow_string = ''
184+
if isinstance(flow3D_smooth, (float, int)):
185+
flow_string = f" --flow3D_smooth {flow3D_smooth}"
186+
elif isinstance(flow3D_smooth, list):
187+
flow_string = f" --flow3D_smooth {' '.join([str(f) for f in flow3D_smooth])}"
188+
189+
cmd = f"python -m cellpose --image_path {str(data_dir / '3D' / image_names_3d[0])} --do_3D --save_tif {gpu_string} --verbose{flow_string}"
185190
print(cmd)
186191
try:
187192
cmd_stdout = check_output(cmd, stderr=STDOUT, shell=True).decode()

0 commit comments

Comments
 (0)