def testCopy(self): """Make sure the copy function is correctly processing """ colormapObject = Colormap(name='toto', colors=numpy.array([12, 13, 14]), vmin=None, vmax=None, normalization=Colormap.LOGARITHM) colormapObject2 = colormapObject.copy() self.assertTrue(colormapObject == colormapObject2) colormapObject.setColormapLUT(numpy.array([0, 1])) self.assertFalse(colormapObject == colormapObject2) colormapObject2 = colormapObject.copy() self.assertTrue(colormapObject == colormapObject2) colormapObject.setNormalization(Colormap.LINEAR) self.assertFalse(colormapObject == colormapObject2)
def testEditableMode(self): """Make sure the colormap will raise NotEditableError when try to change a colormap not editable""" colormap = Colormap() colormap.setEditable(False) with self.assertRaises(NotEditableError): colormap.setVRange(0., 1.) with self.assertRaises(NotEditableError): colormap.setVMin(1.) with self.assertRaises(NotEditableError): colormap.setVMax(1.) with self.assertRaises(NotEditableError): colormap.setNormalization(Colormap.LOGARITHM) with self.assertRaises(NotEditableError): colormap.setName('magma') with self.assertRaises(NotEditableError): colormap.setColormapLUT(numpy.array([0, 1])) with self.assertRaises(NotEditableError): colormap._setFromDict(colormap._toDict()) state = colormap.saveState() with self.assertRaises(NotEditableError): colormap.restoreState(state)
class BaseMaskToolsWidget(qt.QWidget): """Base class for :class:`MaskToolsWidget` (image mask) and :class:`scatterMaskToolsWidget`""" sigMaskChanged = qt.Signal() _maxLevelNumber = 255 def __init__(self, parent=None, plot=None, mask=None): """ :param parent: Parent QWidget :param plot: Plot widget on which to operate :param mask: Instance of subclass of :class:`BaseMask` (e.g. :class:`ImageMask`) """ super(BaseMaskToolsWidget, self).__init__(parent) # register if the user as force a color for the corresponding mask level self._defaultColors = numpy.ones((self._maxLevelNumber + 1), dtype=numpy.bool) # overlays colors set by the user self._overlayColors = numpy.zeros((self._maxLevelNumber + 1, 3), dtype=numpy.float32) # as parent have to be the first argument of the widget to fit # QtDesigner need but here plot can't be None by default. assert plot is not None self._plot = plot self._maskName = '__MASK_TOOLS_%d' % id(self) # Legend of the mask self._colormap = Colormap(name="", normalization='linear', vmin=0, vmax=self._maxLevelNumber, colors=None) self._defaultOverlayColor = rgba('gray') # Color of the mask self._setMaskColors(1, 0.5) if not isinstance(mask, BaseMask): raise TypeError("mask is not an instance of BaseMask") self._mask = mask self._mask.sigChanged.connect(self._updatePlotMask) self._mask.sigChanged.connect(self._emitSigMaskChanged) self._drawingMode = None # Store current drawing mode self._lastPencilPos = None self._multipleMasks = 'exclusive' self._maskFileDir = qt.QDir.home().absolutePath() self.plot.sigInteractiveModeChanged.connect( self._interactiveModeChanged) self._initWidgets() def _emitSigMaskChanged(self): """Notify mask changes""" self.sigMaskChanged.emit() def getSelectionMask(self, copy=True): """Get the current mask as a numpy array. :param bool copy: True (default) to get a copy of the mask. If False, the returned array MUST not be modified. :return: The array of the mask with dimension of the 'active' plot item. If there is no active image or scatter, an empty array is returned. :rtype: numpy.ndarray of uint8 """ return self._mask.getMask(copy=copy) def multipleMasks(self): """Return the current mode of multiple masks support. See :meth:`setMultipleMasks` """ return self._multipleMasks def setMultipleMasks(self, mode): """Set the mode of multiple masks support. Available modes: - 'single': Edit a single level of mask - 'exclusive': Supports to 256 levels of non overlapping masks :param str mode: The mode to use """ assert mode in ('exclusive', 'single') if mode != self._multipleMasks: self._multipleMasks = mode self.levelWidget.setVisible(self._multipleMasks != 'single') self.clearAllBtn.setVisible(self._multipleMasks != 'single') @property def maskFileDir(self): """The directory from which to load/save mask from/to files.""" if not os.path.isdir(self._maskFileDir): self._maskFileDir = qt.QDir.home().absolutePath() return self._maskFileDir @maskFileDir.setter def maskFileDir(self, maskFileDir): self._maskFileDir = str(maskFileDir) @property def plot(self): """The :class:`.PlotWindow` this widget is attached to.""" return self._plot def setDirection(self, direction=qt.QBoxLayout.LeftToRight): """Set the direction of the layout of the widget :param direction: QBoxLayout direction """ self.layout().setDirection(direction) def _initWidgets(self): """Create widgets""" layout = qt.QBoxLayout(qt.QBoxLayout.LeftToRight) layout.addWidget(self._initMaskGroupBox()) layout.addWidget(self._initDrawGroupBox()) layout.addWidget(self._initThresholdGroupBox()) layout.addStretch(1) self.setLayout(layout) @staticmethod def _hboxWidget(*widgets, **kwargs): """Place widgets in widget with horizontal layout :param widgets: Widgets to position horizontally :param bool stretch: True for trailing stretch (default), False for no trailing stretch :return: A QWidget with a QHBoxLayout """ stretch = kwargs.get('stretch', True) layout = qt.QHBoxLayout() layout.setContentsMargins(0, 0, 0, 0) for widget in widgets: layout.addWidget(widget) if stretch: layout.addStretch(1) widget = qt.QWidget() widget.setLayout(layout) return widget def _initTransparencyWidget(self): """ Init the mask transparency widget """ transparencyWidget = qt.QWidget(self) grid = qt.QGridLayout() grid.setContentsMargins(0, 0, 0, 0) self.transparencySlider = qt.QSlider(qt.Qt.Horizontal, parent=transparencyWidget) self.transparencySlider.setRange(3, 10) self.transparencySlider.setValue(8) self.transparencySlider.setToolTip( 'Set the transparency of the mask display') self.transparencySlider.valueChanged.connect(self._updateColors) grid.addWidget(qt.QLabel('Display:', parent=transparencyWidget), 0, 0) grid.addWidget(self.transparencySlider, 0, 1, 1, 3) grid.addWidget( qt.QLabel('<small><b>Transparent</b></small>', parent=transparencyWidget), 1, 1) grid.addWidget( qt.QLabel('<small><b>Opaque</b></small>', parent=transparencyWidget), 1, 3) transparencyWidget.setLayout(grid) return transparencyWidget def _initMaskGroupBox(self): """Init general mask operation widgets""" # Mask level self.levelSpinBox = qt.QSpinBox() self.levelSpinBox.setRange(1, self._maxLevelNumber) self.levelSpinBox.setToolTip( 'Choose which mask level is edited.\n' 'A mask can have up to 255 non-overlapping levels.') self.levelSpinBox.valueChanged[int].connect(self._updateColors) self.levelWidget = self._hboxWidget(qt.QLabel('Mask level:'), self.levelSpinBox) # Transparency self.transparencyWidget = self._initTransparencyWidget() # Buttons group invertBtn = qt.QPushButton('Invert') invertBtn.setShortcut(qt.Qt.CTRL + qt.Qt.Key_I) invertBtn.setToolTip('Invert current mask <b>%s</b>' % invertBtn.shortcut().toString()) invertBtn.clicked.connect(self._handleInvertMask) clearBtn = qt.QPushButton('Clear') clearBtn.setShortcut(qt.QKeySequence.Delete) clearBtn.setToolTip('Clear current mask level <b>%s</b>' % clearBtn.shortcut().toString()) clearBtn.clicked.connect(self._handleClearMask) invertClearWidget = self._hboxWidget(invertBtn, clearBtn, stretch=False) undoBtn = qt.QPushButton('Undo') undoBtn.setShortcut(qt.QKeySequence.Undo) undoBtn.setToolTip('Undo last mask change <b>%s</b>' % undoBtn.shortcut().toString()) self._mask.sigUndoable.connect(undoBtn.setEnabled) undoBtn.clicked.connect(self._mask.undo) redoBtn = qt.QPushButton('Redo') redoBtn.setShortcut(qt.QKeySequence.Redo) redoBtn.setToolTip('Redo last undone mask change <b>%s</b>' % redoBtn.shortcut().toString()) self._mask.sigRedoable.connect(redoBtn.setEnabled) redoBtn.clicked.connect(self._mask.redo) undoRedoWidget = self._hboxWidget(undoBtn, redoBtn, stretch=False) self.clearAllBtn = qt.QPushButton('Clear all') self.clearAllBtn.setToolTip('Clear all mask levels') self.clearAllBtn.clicked.connect(self.resetSelectionMask) loadBtn = qt.QPushButton('Load...') loadBtn.clicked.connect(self._loadMask) saveBtn = qt.QPushButton('Save...') saveBtn.clicked.connect(self._saveMask) self.loadSaveWidget = self._hboxWidget(loadBtn, saveBtn, stretch=False) layout = qt.QVBoxLayout() layout.addWidget(self.levelWidget) layout.addWidget(self.transparencyWidget) layout.addWidget(invertClearWidget) layout.addWidget(undoRedoWidget) layout.addWidget(self.clearAllBtn) layout.addWidget(self.loadSaveWidget) layout.addStretch(1) maskGroup = qt.QGroupBox('Mask') maskGroup.setLayout(layout) return maskGroup def _initDrawGroupBox(self): """Init drawing tools widgets""" layout = qt.QVBoxLayout() self.browseAction = PanModeAction(self.plot, self.plot) self.addAction(self.browseAction) # Draw tools self.rectAction = qt.QAction(icons.getQIcon('shape-rectangle'), 'Rectangle selection', None) self.rectAction.setToolTip( 'Rectangle selection tool: (Un)Mask a rectangular region <b>R</b>') self.rectAction.setShortcut(qt.QKeySequence(qt.Qt.Key_R)) self.rectAction.setCheckable(True) self.rectAction.triggered.connect(self._activeRectMode) self.addAction(self.rectAction) self.polygonAction = qt.QAction(icons.getQIcon('shape-polygon'), 'Polygon selection', None) self.polygonAction.setShortcut(qt.QKeySequence(qt.Qt.Key_S)) self.polygonAction.setToolTip( 'Polygon selection tool: (Un)Mask a polygonal region <b>S</b><br>' 'Left-click to place polygon corners<br>' 'Right-click to place the last corner') self.polygonAction.setCheckable(True) self.polygonAction.triggered.connect(self._activePolygonMode) self.addAction(self.polygonAction) self.pencilAction = qt.QAction(icons.getQIcon('draw-pencil'), 'Pencil tool', None) self.pencilAction.setShortcut(qt.QKeySequence(qt.Qt.Key_P)) self.pencilAction.setToolTip( 'Pencil tool: (Un)Mask using a pencil <b>P</b>') self.pencilAction.setCheckable(True) self.pencilAction.triggered.connect(self._activePencilMode) self.addAction(self.pencilAction) self.drawActionGroup = qt.QActionGroup(self) self.drawActionGroup.setExclusive(True) self.drawActionGroup.addAction(self.rectAction) self.drawActionGroup.addAction(self.polygonAction) self.drawActionGroup.addAction(self.pencilAction) actions = (self.browseAction, self.rectAction, self.polygonAction, self.pencilAction) drawButtons = [] for action in actions: btn = qt.QToolButton() btn.setDefaultAction(action) drawButtons.append(btn) container = self._hboxWidget(*drawButtons) layout.addWidget(container) # Mask/Unmask radio buttons maskRadioBtn = qt.QRadioButton('Mask') maskRadioBtn.setToolTip( 'Drawing masks with current level. Press <b>Ctrl</b> to unmask') maskRadioBtn.setChecked(True) unmaskRadioBtn = qt.QRadioButton('Unmask') unmaskRadioBtn.setToolTip( 'Drawing unmasks with current level. Press <b>Ctrl</b> to mask') self.maskStateGroup = qt.QButtonGroup() self.maskStateGroup.addButton(maskRadioBtn, 1) self.maskStateGroup.addButton(unmaskRadioBtn, 0) self.maskStateWidget = self._hboxWidget(maskRadioBtn, unmaskRadioBtn) layout.addWidget(self.maskStateWidget) self.maskStateWidget.setHidden(True) # Pencil settings self.pencilSetting = self._createPencilSettings(None) self.pencilSetting.setVisible(False) layout.addWidget(self.pencilSetting) layout.addStretch(1) drawGroup = qt.QGroupBox('Draw tools') drawGroup.setLayout(layout) return drawGroup def _createPencilSettings(self, parent=None): pencilSetting = qt.QWidget(parent) self.pencilSpinBox = qt.QSpinBox(parent=pencilSetting) self.pencilSpinBox.setRange(1, 1024) pencilToolTip = """Set pencil drawing tool size in pixels of the image on which to make the mask.""" self.pencilSpinBox.setToolTip(pencilToolTip) self.pencilSlider = qt.QSlider(qt.Qt.Horizontal, parent=pencilSetting) self.pencilSlider.setRange(1, 50) self.pencilSlider.setToolTip(pencilToolTip) pencilLabel = qt.QLabel('Pencil size:', parent=pencilSetting) layout = qt.QGridLayout() layout.addWidget(pencilLabel, 0, 0) layout.addWidget(self.pencilSpinBox, 0, 1) layout.addWidget(self.pencilSlider, 1, 1) pencilSetting.setLayout(layout) self.pencilSpinBox.valueChanged.connect(self._pencilWidthChanged) self.pencilSlider.valueChanged.connect(self._pencilWidthChanged) return pencilSetting def _initThresholdGroupBox(self): """Init thresholding widgets""" layout = qt.QVBoxLayout() # Thresholing self.belowThresholdAction = qt.QAction( icons.getQIcon('plot-roi-below'), 'Mask below threshold', None) self.belowThresholdAction.setToolTip( 'Mask image where values are below given threshold') self.belowThresholdAction.setCheckable(True) self.belowThresholdAction.triggered[bool].connect( self._belowThresholdActionTriggered) self.betweenThresholdAction = qt.QAction( icons.getQIcon('plot-roi-between'), 'Mask within range', None) self.betweenThresholdAction.setToolTip( 'Mask image where values are within given range') self.betweenThresholdAction.setCheckable(True) self.betweenThresholdAction.triggered[bool].connect( self._betweenThresholdActionTriggered) self.aboveThresholdAction = qt.QAction( icons.getQIcon('plot-roi-above'), 'Mask above threshold', None) self.aboveThresholdAction.setToolTip( 'Mask image where values are above given threshold') self.aboveThresholdAction.setCheckable(True) self.aboveThresholdAction.triggered[bool].connect( self._aboveThresholdActionTriggered) self.thresholdActionGroup = qt.QActionGroup(self) self.thresholdActionGroup.setExclusive(False) self.thresholdActionGroup.addAction(self.belowThresholdAction) self.thresholdActionGroup.addAction(self.betweenThresholdAction) self.thresholdActionGroup.addAction(self.aboveThresholdAction) self.thresholdActionGroup.triggered.connect( self._thresholdActionGroupTriggered) self.loadColormapRangeAction = qt.QAction( icons.getQIcon('view-refresh'), 'Set min-max from colormap', None) self.loadColormapRangeAction.setToolTip( 'Set min and max values from current colormap range') self.loadColormapRangeAction.setCheckable(False) self.loadColormapRangeAction.triggered.connect( self._loadRangeFromColormapTriggered) widgets = [] for action in self.thresholdActionGroup.actions(): btn = qt.QToolButton() btn.setDefaultAction(action) widgets.append(btn) spacer = qt.QWidget() spacer.setSizePolicy(qt.QSizePolicy.Expanding, qt.QSizePolicy.Preferred) widgets.append(spacer) loadColormapRangeBtn = qt.QToolButton() loadColormapRangeBtn.setDefaultAction(self.loadColormapRangeAction) widgets.append(loadColormapRangeBtn) container = self._hboxWidget(*widgets, stretch=False) layout.addWidget(container) form = qt.QFormLayout() self.minLineEdit = qt.QLineEdit() self.minLineEdit.setText('0') self.minLineEdit.setValidator(qt.QDoubleValidator(self)) self.minLineEdit.setEnabled(False) form.addRow('Min:', self.minLineEdit) self.maxLineEdit = qt.QLineEdit() self.maxLineEdit.setText('0') self.maxLineEdit.setValidator(qt.QDoubleValidator(self)) self.maxLineEdit.setEnabled(False) form.addRow('Max:', self.maxLineEdit) self.applyMaskBtn = qt.QPushButton('Apply mask') self.applyMaskBtn.clicked.connect(self._maskBtnClicked) self.applyMaskBtn.setEnabled(False) form.addRow(self.applyMaskBtn) self.maskNanBtn = qt.QPushButton('Mask not finite values') self.maskNanBtn.setToolTip('Mask Not a Number and infinite values') self.maskNanBtn.clicked.connect(self._maskNotFiniteBtnClicked) form.addRow(self.maskNanBtn) thresholdWidget = qt.QWidget() thresholdWidget.setLayout(form) layout.addWidget(thresholdWidget) layout.addStretch(1) self.thresholdGroup = qt.QGroupBox('Threshold') self.thresholdGroup.setLayout(layout) return self.thresholdGroup # track widget visibility and plot active image changes def changeEvent(self, event): """Reset drawing action when disabling widget""" if (event.type() == qt.QEvent.EnabledChange and not self.isEnabled() and self.drawActionGroup.checkedAction()): # Disable drawing tool by setting interaction to zoom self.browseAction.trigger() def save(self, filename, kind): """Save current mask in a file :param str filename: The file where to save to mask :param str kind: The kind of file to save in 'edf', 'tif', 'npy' :raise Exception: Raised if the process fails """ self._mask.save(filename, kind) def getCurrentMaskColor(self): """Returns the color of the current selected level. :rtype: A tuple or a python array """ currentLevel = self.levelSpinBox.value() if self._defaultColors[currentLevel]: return self._defaultOverlayColor else: return self._overlayColors[currentLevel].tolist() def _setMaskColors(self, level, alpha): """Set-up the mask colormap to highlight current mask level. :param int level: The mask level to highlight :param float alpha: Alpha level of mask in [0., 1.] """ assert 0 < level <= self._maxLevelNumber colors = numpy.empty((self._maxLevelNumber + 1, 4), dtype=numpy.float32) # Set color colors[:, :3] = self._defaultOverlayColor[:3] # check if some colors has been directly set by the user mask = numpy.equal(self._defaultColors, False) colors[mask, :3] = self._overlayColors[mask, :3] # Set alpha colors[:, -1] = alpha / 2. # Set highlighted level color colors[level, 3] = alpha # Set no mask level colors[0] = (0., 0., 0., 0.) self._colormap.setColormapLUT(colors) def resetMaskColors(self, level=None): """Reset the mask color at the given level to be defaultColors :param level: The index of the mask for which we want to reset the color. If none we will reset color for all masks. """ if level is None: self._defaultColors[level] = True else: self._defaultColors[:] = True self._updateColors() def setMaskColors(self, rgb, level=None): """Set the masks color :param rgb: The rgb color :param level: The index of the mask for which we want to change the color. If none set this color for all the masks """ if level is None: self._overlayColors[:] = rgb self._defaultColors[:] = False else: self._overlayColors[level] = rgb self._defaultColors[level] = False self._updateColors() def getMaskColors(self): """masks colors getter""" return self._overlayColors def _updateColors(self, *args): """Rebuild mask colormap when selected level or transparency change""" self._setMaskColors( self.levelSpinBox.value(), self.transparencySlider.value() / self.transparencySlider.maximum()) self._updatePlotMask() self._updateInteractiveMode() def _pencilWidthChanged(self, width): old = self.pencilSpinBox.blockSignals(True) try: self.pencilSpinBox.setValue(width) finally: self.pencilSpinBox.blockSignals(old) old = self.pencilSlider.blockSignals(True) try: self.pencilSlider.setValue(width) finally: self.pencilSlider.blockSignals(old) self._updateInteractiveMode() def _updateInteractiveMode(self): """Update the current mode to the same if some cached data have to be updated. It is the case for the color for example. """ if self._drawingMode == 'rectangle': self._activeRectMode() elif self._drawingMode == 'polygon': self._activePolygonMode() elif self._drawingMode == 'pencil': self._activePencilMode() def _handleClearMask(self): """Handle clear button clicked: reset current level mask""" self._mask.clear(self.levelSpinBox.value()) self._mask.commit() def _handleInvertMask(self): """Invert the current mask level selection.""" self._mask.invert(self.levelSpinBox.value()) self._mask.commit() # Handle drawing tools UI events def _interactiveModeChanged(self, source): """Handle plot interactive mode changed: If changed from elsewhere, disable drawing tool """ if source is not self: self.pencilAction.setChecked(False) self.rectAction.setChecked(False) self.polygonAction.setChecked(False) self._releaseDrawingMode() self._updateDrawingModeWidgets() def _releaseDrawingMode(self): """Release the drawing mode if is was used""" if self._drawingMode is None: return self.plot.sigPlotSignal.disconnect(self._plotDrawEvent) self._drawingMode = None def _activeRectMode(self): """Handle rect action mode triggering""" self._releaseDrawingMode() self._drawingMode = 'rectangle' self.plot.sigPlotSignal.connect(self._plotDrawEvent) color = self.getCurrentMaskColor() self.plot.setInteractiveMode('draw', shape='rectangle', source=self, color=color) self._updateDrawingModeWidgets() def _activePolygonMode(self): """Handle polygon action mode triggering""" self._releaseDrawingMode() self._drawingMode = 'polygon' self.plot.sigPlotSignal.connect(self._plotDrawEvent) color = self.getCurrentMaskColor() self.plot.setInteractiveMode('draw', shape='polygon', source=self, color=color) self._updateDrawingModeWidgets() def _activePencilMode(self): """Handle pencil action mode triggering""" self._releaseDrawingMode() self._drawingMode = 'pencil' self.plot.sigPlotSignal.connect(self._plotDrawEvent) color = self.getCurrentMaskColor() width = self.pencilSpinBox.value() self.plot.setInteractiveMode('draw', shape='pencil', source=self, color=color, width=width) self._updateDrawingModeWidgets() def _updateDrawingModeWidgets(self): self.maskStateWidget.setVisible(self._drawingMode is not None) self.pencilSetting.setVisible(self._drawingMode == 'pencil') # Handle plot drawing events def _isMasking(self): """Returns true if the tool is used for masking, else it is used for unmasking. :rtype: bool""" # First draw event, use current modifiers for all draw sequence doMask = (self.maskStateGroup.checkedId() == 1) if qt.QApplication.keyboardModifiers() & qt.Qt.ControlModifier: doMask = not doMask return doMask # Handle threshold UI events def _belowThresholdActionTriggered(self, triggered): if triggered: self.minLineEdit.setEnabled(True) self.maxLineEdit.setEnabled(False) self.applyMaskBtn.setEnabled(True) def _betweenThresholdActionTriggered(self, triggered): if triggered: self.minLineEdit.setEnabled(True) self.maxLineEdit.setEnabled(True) self.applyMaskBtn.setEnabled(True) def _aboveThresholdActionTriggered(self, triggered): if triggered: self.minLineEdit.setEnabled(False) self.maxLineEdit.setEnabled(True) self.applyMaskBtn.setEnabled(True) def _thresholdActionGroupTriggered(self, triggeredAction): """Threshold action group listener.""" if triggeredAction.isChecked(): # Uncheck other actions for action in self.thresholdActionGroup.actions(): if action is not triggeredAction and action.isChecked(): action.setChecked(False) else: # Disable min/max edit self.minLineEdit.setEnabled(False) self.maxLineEdit.setEnabled(False) self.applyMaskBtn.setEnabled(False) def _maskBtnClicked(self): if self.belowThresholdAction.isChecked(): if self.minLineEdit.text(): self._mask.updateBelowThreshold(self.levelSpinBox.value(), float(self.minLineEdit.text())) self._mask.commit() elif self.betweenThresholdAction.isChecked(): if self.minLineEdit.text() and self.maxLineEdit.text(): min_ = float(self.minLineEdit.text()) max_ = float(self.maxLineEdit.text()) self._mask.updateBetweenThresholds(self.levelSpinBox.value(), min_, max_) self._mask.commit() elif self.aboveThresholdAction.isChecked(): if self.maxLineEdit.text(): max_ = float(self.maxLineEdit.text()) self._mask.updateAboveThreshold(self.levelSpinBox.value(), max_) self._mask.commit() def _maskNotFiniteBtnClicked(self): """Handle not finite mask button clicked: mask NaNs and inf""" self._mask.updateNotFinite(self.levelSpinBox.value()) self._mask.commit()