Skip to content

Commit 45897c6

Browse files
Merge pull request #1458 from MouseLand/3d_ortho_fix
Fix: Exit early from `update_orthos()` when orthos are off
2 parents ead065e + 571d2f4 commit 45897c6

1 file changed

Lines changed: 94 additions & 92 deletions

File tree

cellpose/gui/gui3d.py

Lines changed: 94 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -426,102 +426,104 @@ def update_crosshairs(self):
426426
self.hLineOrtho[0].setPos(self.yortho)
427427

428428
def update_ortho(self):
429-
if self.NZ > 1 and self.orthobtn.isChecked():
430-
dzcurrent = self.dz
431-
self.dz = min(100, max(3, int(self.dzedit.text())))
432-
self.zaspect = max(0.01, min(100., float(self.zaspectedit.text())))
433-
self.dzedit.setText(str(self.dz))
434-
self.zaspectedit.setText(str(self.zaspect))
435-
if self.dz != dzcurrent:
436-
self.pOrtho[0].setXRange(-self.dz / 3, self.dz * 2 + self.dz / 3)
437-
self.pOrtho[1].setYRange(-self.dz / 3, self.dz * 2 + self.dz / 3)
438-
dztot = min(self.NZ, self.dz * 2)
439-
y = self.yortho
440-
x = self.xortho
441-
z = self.currentZ
442-
if dztot == self.NZ:
443-
zmin, zmax = 0, self.NZ
429+
if not (self.NZ > 1 and self.orthobtn.isChecked()):
430+
return
431+
432+
dzcurrent = self.dz
433+
self.dz = min(100, max(3, int(self.dzedit.text())))
434+
self.zaspect = max(0.01, min(100., float(self.zaspectedit.text())))
435+
self.dzedit.setText(str(self.dz))
436+
self.zaspectedit.setText(str(self.zaspect))
437+
if self.dz != dzcurrent:
438+
self.pOrtho[0].setXRange(-self.dz / 3, self.dz * 2 + self.dz / 3)
439+
self.pOrtho[1].setYRange(-self.dz / 3, self.dz * 2 + self.dz / 3)
440+
dztot = min(self.NZ, self.dz * 2)
441+
y = self.yortho
442+
x = self.xortho
443+
z = self.currentZ
444+
if dztot == self.NZ:
445+
zmin, zmax = 0, self.NZ
446+
else:
447+
if z - self.dz < 0:
448+
zmin = 0
449+
zmax = zmin + self.dz * 2
450+
elif z + self.dz >= self.NZ:
451+
zmax = self.NZ
452+
zmin = zmax - self.dz * 2
444453
else:
445-
if z - self.dz < 0:
446-
zmin = 0
447-
zmax = zmin + self.dz * 2
448-
elif z + self.dz >= self.NZ:
449-
zmax = self.NZ
450-
zmin = zmax - self.dz * 2
454+
zmin, zmax = z - self.dz, z + self.dz
455+
self.zc = z - zmin
456+
self.update_crosshairs()
457+
458+
is_image_view = self.view == 'image'
459+
is_restored_view = self.view == 'restored'
460+
461+
rgb_list = ['red', 'green', 'blue']
462+
463+
if is_image_view or is_restored_view:
464+
for j in range(2):
465+
if j == 0:
466+
if is_image_view:
467+
image = self.stack[zmin:zmax, :, x].transpose(1, 0, 2).copy()
468+
else:
469+
image = self.stack_filtered[zmin:zmax, :,
470+
x].transpose(1, 0, 2).copy()
451471
else:
452-
zmin, zmax = z - self.dz, z + self.dz
453-
self.zc = z - zmin
454-
self.update_crosshairs()
455-
456-
is_image_view = self.view == 'image'
457-
is_restored_view = self.view == 'restored'
458-
459-
rgb_list = ['red', 'green', 'blue']
460-
461-
if is_image_view or is_restored_view:
462-
for j in range(2):
463-
if j == 0:
464-
if is_image_view:
465-
image = self.stack[zmin:zmax, :, x].transpose(1, 0, 2).copy()
466-
else:
467-
image = self.stack_filtered[zmin:zmax, :,
468-
x].transpose(1, 0, 2).copy()
472+
image = self.stack[
473+
zmin:zmax,
474+
y, :].copy() if is_image_view else self.stack_filtered[zmin:zmax,
475+
y, :].copy()
476+
if self.nchan == 1:
477+
# show single channel
478+
image = image[..., 0]
479+
if self.color == 'rgb':
480+
self.imgOrtho[j].setImage(image, autoLevels=False, lut=None)
481+
if self.nchan > 1:
482+
levels = np.array([
483+
self.saturation[0][self.currentZ],
484+
self.saturation[1][self.currentZ],
485+
self.saturation[2][self.currentZ]
486+
])
487+
self.imgOrtho[j].setLevels(levels)
469488
else:
470-
image = self.stack[
471-
zmin:zmax,
472-
y, :].copy() if is_image_view else self.stack_filtered[zmin:zmax,
473-
y, :].copy()
474-
if self.nchan == 1:
475-
# show single channel
476-
image = image[..., 0]
477-
if self.color == 'rgb':
478-
self.imgOrtho[j].setImage(image, autoLevels=False, lut=None)
479-
if self.nchan > 1:
480-
levels = np.array([
481-
self.saturation[0][self.currentZ],
482-
self.saturation[1][self.currentZ],
483-
self.saturation[2][self.currentZ]
484-
])
485-
self.imgOrtho[j].setLevels(levels)
486-
else:
487-
self.imgOrtho[j].setLevels(
488-
self.saturation[0][self.currentZ])
489-
elif self.color in rgb_list:
490-
color_index = rgb_list.index(self.color)
491-
if self.nchan > 1:
492-
image = image[..., color_index]
493-
self.imgOrtho[j].setImage(image, autoLevels=False,
494-
lut=self.cmap[color_index + 1])
495-
if self.nchan > 1:
496-
self.imgOrtho[j].setLevels(
497-
self.saturation[color_index][self.currentZ])
498-
else:
499-
self.imgOrtho[j].setLevels(
500-
self.saturation[0][self.currentZ])
501-
elif self.color == 'gray':
502-
if image.ndim > 2:
503-
# exclude blank channels:
504-
ranges = np.ptp(image, tuple(range(image.ndim-1)))
505-
range_mask = ranges > 1e-5
506-
image = image[..., range_mask]
507-
image = image.astype("float32").mean(axis=2).astype("uint8")
508-
self.imgOrtho[j].setImage(image, autoLevels=False, lut=None)
509-
self.imgOrtho[j].setLevels(self.saturation[0][self.currentZ])
510-
elif self.color == 'spectral':
511-
if image.ndim > 2:
512-
image = image.astype("float32").mean(axis=2).astype("uint8")
513-
self.imgOrtho[j].setImage(image, autoLevels=False,
514-
lut=self.cmap[0])
515-
self.imgOrtho[j].setLevels(self.saturation[0][self.currentZ])
516-
self.pOrtho[0].setAspectLocked(lock=True, ratio=self.zaspect)
517-
self.pOrtho[1].setAspectLocked(lock=True, ratio=1. / self.zaspect)
489+
self.imgOrtho[j].setLevels(
490+
self.saturation[0][self.currentZ])
491+
elif self.color in rgb_list:
492+
color_index = rgb_list.index(self.color)
493+
if self.nchan > 1:
494+
image = image[..., color_index]
495+
self.imgOrtho[j].setImage(image, autoLevels=False,
496+
lut=self.cmap[color_index + 1])
497+
if self.nchan > 1:
498+
self.imgOrtho[j].setLevels(
499+
self.saturation[color_index][self.currentZ])
500+
else:
501+
self.imgOrtho[j].setLevels(
502+
self.saturation[0][self.currentZ])
503+
elif self.color == 'gray':
504+
if image.ndim > 2:
505+
# exclude blank channels:
506+
ranges = np.ptp(image, tuple(range(image.ndim-1)))
507+
range_mask = ranges > 1e-5
508+
image = image[..., range_mask]
509+
image = image.astype("float32").mean(axis=2).astype("uint8")
510+
self.imgOrtho[j].setImage(image, autoLevels=False, lut=None)
511+
self.imgOrtho[j].setLevels(self.saturation[0][self.currentZ])
512+
elif self.color == 'spectral':
513+
if image.ndim > 2:
514+
image = image.astype("float32").mean(axis=2).astype("uint8")
515+
self.imgOrtho[j].setImage(image, autoLevels=False,
516+
lut=self.cmap[0])
517+
self.imgOrtho[j].setLevels(self.saturation[0][self.currentZ])
518+
self.pOrtho[0].setAspectLocked(lock=True, ratio=self.zaspect)
519+
self.pOrtho[1].setAspectLocked(lock=True, ratio=1. / self.zaspect)
518520

519-
else:
520-
image = np.zeros((10, 10), "uint8")
521-
self.imgOrtho[0].setImage(image, autoLevels=False, lut=None)
522-
self.imgOrtho[0].setLevels([0.0, 255.0])
523-
self.imgOrtho[1].setImage(image, autoLevels=False, lut=None)
524-
self.imgOrtho[1].setLevels([0.0, 255.0])
521+
else:
522+
image = np.zeros((10, 10), "uint8")
523+
self.imgOrtho[0].setImage(image, autoLevels=False, lut=None)
524+
self.imgOrtho[0].setLevels([0.0, 255.0])
525+
self.imgOrtho[1].setImage(image, autoLevels=False, lut=None)
526+
self.imgOrtho[1].setLevels([0.0, 255.0])
525527

526528
zrange = zmax - zmin
527529
self.layer_ortho = [

0 commit comments

Comments
 (0)