1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
|
# /*#########################################################################
# Copyright (C) 2004-2018 European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF by the Software group.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
""":class:`SilxMaskImageWidget` uses a silx PlotWidget to display a stack,
while offering the same tools as the :class:`StackRoiWindow` (median filter,
background subtraction, ...). In addition to reimplementing existing tools,
it also provides methods to plot a background image underneath the stack
images.
"""
__authors__ = ["P. Knobel"]
__license__ = "MIT"
import copy
import numpy
import os
from PyMca5.PyMcaMath.PyMcaSciPy.signal.median import medfilt2d
from PyMca5.PyMcaGui import PyMcaQt as qt
if hasattr(qt, "QString"):
QString = qt.QString
else:
QString = str
from PyMca5.PyMcaGui.plotting.PyMca_Icons import IconDict
from PyMca5.PyMcaIO import ArraySave
from PyMca5.PyMcaCore import PyMcaDirs
from PyMca5.PyMcaPlugins import MotorInfoWindow
try:
from PyMca5.PyMcaGui.pymca import QPyMcaMatplotlibSave
except ImportError:
QPyMcaMatplotlibSave = None
# temporarily disable logging when importing silx and fabio
import logging
logging.basicConfig()
logging.disable(logging.ERROR)
import silx
from silx.gui.plot import PlotWidget
from silx.gui.plot import PlotActions
from silx.gui.plot import PlotToolButtons
from silx.gui.plot.MaskToolsWidget import MaskToolsWidget, MaskToolsDockWidget
from silx.gui.plot.AlphaSlider import NamedImageAlphaSlider
from silx.gui.plot.Profile import ProfileToolBar
from silx.gui import icons
logging.disable(logging.NOTSET) # restore default logging behavior
def convertToRowAndColumn(x, y, shape,
xScale=None, yScale=None,
safe=True):
"""Return (row, column) of a pixel defined by (x, y) in an image.
:param float x: Abscissa of point
:param float x: Ordinate of point
:param shape: Shape of image (nRows, nColumns)
:param xScale: Tuple of linear scaling parameters (a, b),
x = a + b * column
:param yScale: Tuple of linear scaling parameters (a, b),
y = a + b * row
:param bool safe: If True, always return coordinates within the image's
bounds.
:return: 2-tuple (r, c)
"""
if xScale is None:
c = x
else:
c = (x - xScale[0]) / xScale[1]
if yScale is None:
r = y
else:
r = (y - yScale[0]) / yScale[1]
if safe:
c = min(int(c), shape[1] - 1)
c = max(c, 0)
r = min(int(r), shape[0] - 1)
r = max(r, 0)
else:
c = int(c)
r = int(r)
return r, c
class MyMaskToolsWidget(MaskToolsWidget):
"""Backport of the setSelectionMask behavior implemented in silx 0.6.0,
to synchronize mask parameters with the active image.
This widget must not be used with silx >= 0.6"""
def setSelectionMask(self, mask, copy=True):
"""Set the mask to a new array.
:param numpy.ndarray mask: The array to use for the mask.
:type mask: numpy.ndarray of uint8 of dimension 2, C-contiguous.
Array of other types are converted.
:param bool copy: True (the default) to copy the array,
False to use it as is if possible.
:return: None if failed, shape of mask as 2-tuple if successful.
The mask can be cropped or padded to fit active image,
the returned shape is that of the active image.
"""
mask = numpy.array(mask, copy=False, dtype=numpy.uint8)
if len(mask.shape) != 2:
# _logger.error('Not an image, shape: %d', len(mask.shape))
return None
# ensure all mask attributes are synchronized with the active image
activeImage = self.plot.getActiveImage()
if activeImage is not None and activeImage.getLegend() != self._maskName:
self._activeImageChanged()
self.plot.sigActiveImageChanged.connect(self._activeImageChanged)
if self._data.shape[0:2] == (0, 0) or mask.shape == self._data.shape[0:2]:
self._mask.setMask(mask, copy=copy)
self._mask.commit()
return mask.shape
else:
resizedMask = numpy.zeros(self._data.shape[0:2],
dtype=numpy.uint8)
height = min(self._data.shape[0], mask.shape[0])
width = min(self._data.shape[1], mask.shape[1])
resizedMask[:height, :width] = mask[:height, :width]
self._mask.setMask(resizedMask, copy=False)
self._mask.commit()
return resizedMask.shape
class MyMaskToolsDockWidget(MaskToolsDockWidget):
"""
Regular MaskToolsDockWidget if silx version is at least 0.6.0,
else it uses a backported MaskToolsWidget
"""
def __init__(self, parent=None, plot=None, name='Mask'):
super(MyMaskToolsDockWidget, self).__init__(parent, plot, name)
if silx.version_info < (0, 6):
self.setWidget(MyMaskToolsWidget(plot=plot))
self.widget().sigMaskChanged.connect(self._emitSigMaskChanged)
class SaveImageListAction(qt.QAction):
"""Save current image and mask (if any) in a :class:`MaskImageWidget`
to EDF or CSV"""
def __init__(self, title, maskImageWidget,
clipped=False, subtract=False):
super(SaveImageListAction, self).__init__(QString(title),
maskImageWidget)
self.maskImageWidget = maskImageWidget
self.triggered[bool].connect(self.onTrigger)
self.outputDir = PyMcaDirs.outputDir
"""Default output dir. After each save operation, this is updated
to re-use the same folder for next save."""
self.clipped = clipped
"""If True, clip data range to colormap min and max."""
self.subtract = subtract
"""If True, subtract data min value."""
def onTrigger(self):
filename, saveFilter = self.getOutputFileNameFilter()
if not filename:
return
if filename.lower().endswith(".csv"):
csvseparator = "," if "," in saveFilter else\
";" if ";" in saveFilter else\
"\t"
else:
csvseparator = None
images, labels = self.getImagesLabels()
self.saveImageList(filename, images, labels, csvseparator)
def saveImageList(self, filename, imageList, labels,
csvseparator=None):
if not imageList:
qt.QMessageBox.information(
self,
"No Data",
"Image list is empty.\nNothing to be saved")
return
if filename.lower().endswith(".edf"):
ArraySave.save2DArrayListAsEDF(imageList, filename, labels)
elif filename.lower().endswith(".tif"):
ArraySave.save2DArrayListAsMonochromaticTiff(imageList,
filename,
labels)
elif filename.lower().endswith(".csv"):
assert csvseparator is not None
ArraySave.save2DArrayListAsASCII(imageList, filename, labels,
csv=True,
csvseparator=csvseparator)
else:
ArraySave.save2DArrayListAsASCII(imageList, filename, labels,
csv=False)
def getImagesLabels(self):
"""Return images to be saved and corresponding labels.
Images are:
- image currently displayed clipped to visible colormap range
- mask
If :attr:`subtract` is True, subtract the minimum image sample value
to all samples."""
imageList = []
labels = []
imageData = self.maskImageWidget.getImageData()
colormapDict = self.maskImageWidget.getCurrentColormap()
label = self.maskImageWidget.plot.getGraphTitle()
if not label:
label = "Image01"
label.replace(' ', '_')
if self.clipped and colormapDict is not None:
autoscale = colormapDict['autoscale']
if not autoscale:
vmin = colormapDict['vmin']
vmax = colormapDict['vmax']
imageData = imageData.clip(vmin, vmax)
label += ".clip(%f,%f)" % (vmin, vmax)
if self.subtract:
vmin = imageData.min()
imageData = imageData - vmin
label += "-%f" % vmin
imageList.append(imageData)
labels.append(label)
mask = self.maskImageWidget.getSelectionMask()
if mask is not None and mask.max() > 0:
imageList.append(mask)
labels.append(label + "_Mask")
return imageList, labels
def getOutputFileNameFilter(self):
"""Open a file dialog to get the output file name, and return the
file name and the selected format filter.
Remember output directory in attribute :attr:`outputDir`"""
if os.path.exists(self.outputDir):
initdir = self.outputDir
else:
# folder deleted, reset
initdir = PyMcaDirs.outputDir
filedialog = qt.QFileDialog(self.maskImageWidget)
filedialog.setFileMode(filedialog.AnyFile)
filedialog.setAcceptMode(qt.QFileDialog.AcceptSave)
filedialog.setWindowIcon(qt.QIcon(qt.QPixmap(IconDict["gioconda16"])))
formatlist = ["TIFF Files *.tif",
"ASCII Files *.dat",
"EDF Files *.edf",
'CSV(, separated) Files *.csv',
'CSV(; separated) Files *.csv',
'CSV(tab separated) Files *.csv']
if hasattr(qt, "QStringList"):
strlist = qt.QStringList()
else:
strlist = []
for f in formatlist:
strlist.append(f)
saveFilter = formatlist[0]
if hasattr(filedialog, "setFilters"):
filedialog.setFilters(strlist)
filedialog.selectFilter(saveFilter)
else:
filedialog.setNameFilters(strlist)
filedialog.selectNameFilter(saveFilter)
filedialog.setDirectory(initdir)
ret = filedialog.exec_()
if not ret:
return "", ""
filename = filedialog.selectedFiles()[0]
if filename:
filename = qt.safe_str(filename)
self.outputDir = os.path.dirname(filename)
if hasattr(filedialog, "selectedFilter"):
saveFilter = qt.safe_str(filedialog.selectedFilter())
else:
saveFilter = qt.safe_str(filedialog.selectedNameFilter())
filterused = "." + saveFilter[-3:]
PyMcaDirs.outputDir = os.path.dirname(filename)
if len(filename) < 4 or filename[-4:] != filterused:
filename += filterused
else:
filename = ""
return filename, saveFilter
class SaveMatplotlib(qt.QAction):
"""Save current image ho high quality graphics using matplotlib"""
def __init__(self, title, maskImageWidget):
super(SaveMatplotlib, self).__init__(QString(title),
maskImageWidget)
self.maskImageWidget = maskImageWidget
self.triggered[bool].connect(self.onTrigger)
self._matplotlibSaveImage = None
def onTrigger(self):
imageData = self.maskImageWidget.getImageData()
if self._matplotlibSaveImage is None:
self._matplotlibSaveImage = QPyMcaMatplotlibSave.SaveImageSetup(
None, image=None)
title = "Matplotlib " + self.maskImageWidget.plot.getGraphTitle()
self._matplotlibSaveImage.setWindowTitle(title)
ddict = self._matplotlibSaveImage.getParameters()
colormapDict = self.maskImageWidget.getCurrentColormap()
if colormapDict is not None:
autoscale = colormapDict['autoscale']
vmin = colormapDict['vmin']
vmax = colormapDict['vmax']
colormapType = colormapDict['normalization'] # 'log' or 'linear'
if colormapType == 'log':
colormapType = 'logarithmic'
ddict['linlogcolormap'] = colormapType
if not autoscale:
ddict['valuemin'] = vmin
ddict['valuemax'] = vmax
else:
ddict['valuemin'] = 0
ddict['valuemax'] = 0
# this sets the actual dimensions
origin = self.maskImageWidget._origin
delta = self.maskImageWidget._deltaXY
ddict['xpixelsize'] = delta[0]
ddict['xorigin'] = origin[0]
ddict['ypixelsize'] = delta[1]
ddict['yorigin'] = origin[1]
ddict['xlabel'] = self.maskImageWidget.plot.getGraphXLabel()
ddict['ylabel'] = self.maskImageWidget.plot.getGraphYLabel()
limits = self.maskImageWidget.plot.getGraphXLimits()
ddict['zoomxmin'] = limits[0]
ddict['zoomxmax'] = limits[1]
limits = self.maskImageWidget.plot.getGraphYLimits()
ddict['zoomymin'] = limits[0]
ddict['zoomymax'] = limits[1]
self._matplotlibSaveImage.setParameters(ddict)
self._matplotlibSaveImage.setImageData(imageData)
self._matplotlibSaveImage.show()
self._matplotlibSaveImage.raise_()
class SaveToolButton(qt.QToolButton):
def __init__(self, parent=None, maskImageWidget=None):
"""
:param maskImageWidget: Parent SilxMaskImageWidget
"""
qt.QToolButton.__init__(self, parent)
self.maskImageWidget = maskImageWidget
self.setIcon(icons.getQIcon("document-save"))
self.clicked.connect(self._saveToolButtonSignal)
self.setToolTip('Save Graph')
self._saveMenu = qt.QMenu()
self._saveMenu.addAction(
SaveImageListAction("Image Data", self.maskImageWidget))
self._saveMenu.addAction(
SaveImageListAction("Colormap Clipped Seen Image Data",
self.maskImageWidget, clipped=True))
self._saveMenu.addAction(
SaveImageListAction("Clipped and Subtracted Seen Image Data",
self.maskImageWidget, clipped=True, subtract=True))
# standard silx save action
self._saveMenu.addAction(PlotActions.SaveAction(
plot=self.maskImageWidget.plot, parent=self))
if QPyMcaMatplotlibSave is not None:
self._saveMenu.addAction(SaveMatplotlib("Matplotlib",
self.maskImageWidget))
def _saveToolButtonSignal(self):
self._saveMenu.exec_(self.parent().cursor().pos())
class MedianParameters(qt.QWidget):
def __init__(self, parent=None, use_conditional=False):
qt.QWidget.__init__(self, parent)
self.mainLayout = qt.QHBoxLayout(self)
self.mainLayout.setContentsMargins(0, 0, 0, 0)
self.mainLayout.setSpacing(2)
self.label = qt.QLabel(self)
self.label.setText("Median filter width: ")
self.widthSpin = qt.QSpinBox(self)
self.widthSpin.setMinimum(1)
self.widthSpin.setMaximum(99)
self.widthSpin.setValue(1)
self.widthSpin.setSingleStep(2)
if use_conditional:
self.conditionalLabel = qt.QLabel(self)
self.conditionalLabel.setText("Conditional:")
self.conditionalSpin = qt.QSpinBox(self)
self.conditionalSpin.setMinimum(0)
self.conditionalSpin.setMaximum(1)
self.conditionalSpin.setValue(0)
self.mainLayout.addWidget(self.label)
self.mainLayout.addWidget(self.widthSpin)
if use_conditional:
self.mainLayout.addWidget(self.conditionalLabel)
self.mainLayout.addWidget(self.conditionalSpin)
class SilxMaskImageWidget(qt.QMainWindow):
"""Main window with a plot widget, a toolbar and a slider.
A list of images can be set with :meth:`setImages`.
The mask can be accessed through getter and setter methods:
:meth:`setSelectionMask` and :meth:`getSelectionMask`.
The plot widget can be accessed as :attr:`plot`. It is a silx
plot widget.
The toolbar offers some basic interaction tools:
zoom control, colormap, aspect ratio, y axis orientation,
"save image" menu and a mask widget.
"""
sigMaskImageWidget = qt.pyqtSignal(object)
def __init__(self, parent=None):
qt.QMainWindow.__init__(self, parent=parent)
if parent is not None:
# behave as a widget
self.setWindowFlags(qt.Qt.Widget)
else:
self.setWindowTitle("PyMca - Image Selection Tool")
centralWidget = qt.QWidget(self)
layout = qt.QVBoxLayout(centralWidget)
centralWidget.setLayout(layout)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
# Plot
self.plot = PlotWidget(parent=centralWidget)
self.plot.setWindowFlags(qt.Qt.Widget)
self.plot.setDefaultColormap({'name': 'temperature',
'normalization': 'linear',
'autoscale': True,
'vmin': 0.,
'vmax': 1.})
layout.addWidget(self.plot)
# Mask Widget
self._maskToolsDockWidget = None
# Image selection slider
self.slider = qt.QSlider(self.centralWidget())
self.slider.setOrientation(qt.Qt.Horizontal)
self.slider.setMinimum(0)
self.slider.setMaximum(0)
layout.addWidget(self.slider)
self.slider.valueChanged[int].connect(self.showImage)
# ADD/REMOVE/REPLACE IMAGE buttons
buttonBox = qt.QWidget(self)
buttonBoxLayout = qt.QHBoxLayout(buttonBox)
buttonBoxLayout.setContentsMargins(0, 0, 0, 0)
buttonBoxLayout.setSpacing(0)
self.addImageButton = qt.QPushButton(buttonBox)
icon = qt.QIcon(qt.QPixmap(IconDict["rgb16"]))
self.addImageButton.setIcon(icon)
self.addImageButton.setText("ADD IMAGE")
self.addImageButton.setToolTip("Add image to RGB correlator")
buttonBoxLayout.addWidget(self.addImageButton)
self.removeImageButton = qt.QPushButton(buttonBox)
self.removeImageButton.setIcon(icon)
self.removeImageButton.setText("REMOVE IMAGE")
self.removeImageButton.setToolTip("Remove image from RGB correlator")
buttonBoxLayout.addWidget(self.removeImageButton)
self.replaceImageButton = qt.QPushButton(buttonBox)
self.replaceImageButton.setIcon(icon)
self.replaceImageButton.setText("REPLACE IMAGE")
self.replaceImageButton.setToolTip(
"Replace all images in RGB correlator with this one")
buttonBoxLayout.addWidget(self.replaceImageButton)
self.addImageButton.clicked.connect(self._addImageClicked)
self.removeImageButton.clicked.connect(self._removeImageClicked)
self.replaceImageButton.clicked.connect(self._replaceImageClicked)
layout.addWidget(buttonBox)
# median filter widget
self._medianParameters = {'row_width': 1,
'column_width': 1,
'conditional': 0}
self._medianParametersWidget = MedianParameters(self,
use_conditional=True)
self._medianParametersWidget.widthSpin.setValue(1)
self._medianParametersWidget.widthSpin.valueChanged[int].connect(
self._setMedianKernelWidth)
self._medianParametersWidget.conditionalSpin.valueChanged[int].connect(
self._setMedianConditionalFlag)
layout.addWidget(self._medianParametersWidget)
# motor positions (hidden by default)
self.motorPositionsWidget = MotorInfoWindow.MotorInfoDialog(self,
[""],
[{}])
self.motorPositionsWidget.setMaximumHeight(100)
self.plot.sigPlotSignal.connect(self._updateMotors)
self.motorPositionsWidget.hide()
self._motors_first_update = True
layout.addWidget(self.motorPositionsWidget)
self.setCentralWidget(centralWidget)
# Init actions
self.group = qt.QActionGroup(self)
self.group.setExclusive(False)
self.resetZoomAction = self.group.addAction(
PlotActions.ResetZoomAction(plot=self.plot, parent=self))
self.addAction(self.resetZoomAction)
self.zoomInAction = PlotActions.ZoomInAction(plot=self.plot, parent=self)
self.addAction(self.zoomInAction)
self.zoomOutAction = PlotActions.ZoomOutAction(plot=self.plot, parent=self)
self.addAction(self.zoomOutAction)
self.xAxisAutoScaleAction = self.group.addAction(
PlotActions.XAxisAutoScaleAction(plot=self.plot, parent=self))
self.addAction(self.xAxisAutoScaleAction)
self.yAxisAutoScaleAction = self.group.addAction(
PlotActions.YAxisAutoScaleAction(plot=self.plot, parent=self))
self.addAction(self.yAxisAutoScaleAction)
self.colormapAction = self.group.addAction(
PlotActions.ColormapAction(plot=self.plot, parent=self))
self.addAction(self.colormapAction)
self.copyAction = self.group.addAction(
PlotActions.CopyAction(plot=self.plot, parent=self))
self.addAction(self.copyAction)
self.group.addAction(self.getMaskAction())
# Init toolbuttons
self.saveToolbutton = SaveToolButton(parent=self,
maskImageWidget=self)
self.yAxisInvertedButton = PlotToolButtons.YAxisOriginToolButton(
parent=self, plot=self.plot)
self.keepDataAspectRatioButton = PlotToolButtons.AspectToolButton(
parent=self, plot=self.plot)
self.backgroundButton = qt.QToolButton(self)
self.backgroundButton.setCheckable(True)
self.backgroundButton.setIcon(qt.QIcon(qt.QPixmap(IconDict["subtract"])))
self.backgroundButton.setToolTip(
'Toggle background image subtraction from current image\n' +
'No action if no background image available.')
self.backgroundButton.clicked.connect(self._subtractBackground)
# Creating the toolbar also create actions for toolbuttons
self._toolbar = self._createToolBar(title='Plot', parent=None)
self.addToolBar(self._toolbar)
self._profile = ProfileToolBar(plot=self.plot)
self.addToolBar(self._profile)
self.setProfileToolbarVisible(False)
# add a transparency slider for the stack data
self._alphaSliderToolbar = qt.QToolBar("Alpha slider", parent=self)
self._alphaSlider = NamedImageAlphaSlider(parent=self._alphaSliderToolbar,
plot=self.plot,
legend="current")
self._alphaSlider.setOrientation(qt.Qt.Vertical)
self._alphaSlider.setToolTip("Adjust opacity of stack image overlay")
self._alphaSliderToolbar.addWidget(self._alphaSlider)
self.addToolBar(qt.Qt.RightToolBarArea, self._alphaSliderToolbar)
# hide optional tools and actions
self.setAlphaSliderVisible(False)
self.setBackgroundActionVisible(False)
self.setMedianFilterWidgetVisible(False)
self.setProfileToolbarVisible(False)
self._images = []
"""List of images, as 2D numpy arrays or 3D numpy arrays (RGB(A)).
"""
self._labels = []
"""List of image labels.
"""
self._bg_images = []
"""List of background images, as 2D numpy arrays or 3D numpy arrays
(RGB(A)).
These images are not active, their colormap cannot be changed and
they cannot be the base image used for drawing a mask.
"""
self._bg_labels = []
self._deltaXY = (1.0, 1.0) # TODO: allow different scale and origin for each image
"""Current image scale (Xscale, Yscale) (in axis units per image pixel).
The scale is adjusted to keep constant width and height for the image
when a crop operation is applied."""
self._origin = (0., 0.)
"""Current image origin: coordinate (x, y) of sample located at
(row, column) = (0, 0)"""
# scales and origins for background images
self._bg_deltaXY = []
self._bg_origins = []
def sizeHint(self):
return qt.QSize(500, 400)
def _createToolBar(self, title, parent):
"""Create a QToolBar with crop, rotate and flip operations
:param str title: The title of the QMenu
:param qt.QWidget parent: See :class:`QToolBar`
"""
toolbar = qt.QToolBar(title, parent)
# Order widgets with actions
objects = self.group.actions()
# Add standard push buttons to list
index = objects.index(self.colormapAction)
objects.insert(index + 1, self.keepDataAspectRatioButton)
objects.insert(index + 2, self.yAxisInvertedButton)
objects.insert(index + 3, self.saveToolbutton)
objects.insert(index + 4, self.backgroundButton)
for obj in objects:
if isinstance(obj, qt.QAction):
toolbar.addAction(obj)
else:
# keep reference to toolbutton's action for changing visibility
if obj is self.keepDataAspectRatioButton:
self.keepDataAspectRatioAction = toolbar.addWidget(obj)
elif obj is self.yAxisInvertedButton:
self.yAxisInvertedAction = toolbar.addWidget(obj)
elif obj is self.saveToolbutton:
self.saveAction = toolbar.addWidget(obj)
elif obj is self.backgroundButton:
self.bgAction = toolbar.addWidget(obj)
else:
raise RuntimeError()
return toolbar
def _getMaskToolsDockWidget(self):
"""DockWidget with image mask panel (lazy-loaded)."""
if self._maskToolsDockWidget is None:
self._maskToolsDockWidget = MyMaskToolsDockWidget(
plot=self.plot, name='Mask')
self._maskToolsDockWidget.hide()
self.addDockWidget(qt.Qt.RightDockWidgetArea,
self._maskToolsDockWidget)
# self._maskToolsDockWidget.setFloating(True)
self._maskToolsDockWidget.sigMaskChanged.connect(
self._emitMaskImageWidgetSignal)
return self._maskToolsDockWidget
def _setMedianKernelWidth(self, value):
kernelSize = numpy.asarray(value)
if len(kernelSize.shape) == 0:
kernelSize = [kernelSize.item()] * 2
self._medianParameters['row_width'] = kernelSize[0]
self._medianParameters['column_width'] = kernelSize[1]
self._medianParametersWidget.widthSpin.setValue(int(kernelSize[0]))
self.showImage(self.slider.value())
def _setMedianConditionalFlag(self, value):
self._medianParameters['conditional'] = int(value)
self._medianParametersWidget.conditionalSpin.setValue(int(value))
self.showImage(self.slider.value())
def _subtractBackground(self):
"""When background button is clicked, this causes showImage to
display the data after subtracting the stack background image.
This background image is unrelated to the background images set
with :meth:`setBackgroundImages`, it is simply the first data image
whose label ends with 'background'."""
current = self.getCurrentIndex()
self.showImage(current)
def _updateMotors(self, ddict):
if not ddict["event"] == "mouseMoved":
return
if not self.motorPositionsWidget.isVisible():
return
motorsValuesAtCursor = self._getPositionersFromXY(ddict["x"],
ddict["y"])
if motorsValuesAtCursor is None:
return
self.motorPositionsWidget.table.updateTable(
legList=[self.plot.getActiveImage().getLegend()],
motList=[motorsValuesAtCursor])
if self._motors_first_update:
self._select_motors()
self._motors_first_update = False
def _select_motors(self):
"""This methods sets the motors in the comboboxes when the widget
is first initialized."""
for i, combobox in enumerate(self.motorPositionsWidget.table.header.boxes):
# First item (index 0) in combobox is "", so first motor name is at index 1.
# First combobox in header.boxes is at index 1 (boxes[0] is None).
if i == 0:
continue
if i < combobox.count():
combobox.setCurrentIndex(i)
def _getPositionersFromXY(self, x, y):
"""Return positioner values for a stack pixel identified
by it's (x, y) coordinates.
"""
activeImage = self.plot.getActiveImage()
if activeImage is None:
return None
info = activeImage.getInfo()
if not info or not isinstance(info, dict):
return None
positioners = info.get("positioners", {})
nRows, nCols = activeImage.getData().shape
xScale, yScale = activeImage.getScale()
xOrigin, yOrigin = activeImage.getOrigin()
r, c = convertToRowAndColumn(
x, y,
shape=(nRows, nCols),
xScale=(xOrigin, xScale),
yScale=(yOrigin, yScale),
safe=True)
idx1d = r * nCols + c
positionersAtIdx = {}
for motorName, motorValues in positioners.items():
if numpy.isscalar(motorValues):
positionersAtIdx[motorName] = motorValues
elif len(motorValues.shape) == 1:
positionersAtIdx[motorName] = motorValues[idx1d]
else:
positionersAtIdx[motorName] = motorValues.reshape((-1,))[idx1d]
return positionersAtIdx
# widgets visibility toggling
def setBackgroundActionVisible(self, visible):
"""Set visibility of the background toolbar button.
:param visible: True to show tool button, False to hide it.
"""
self.bgAction.setVisible(visible)
def setProfileToolbarVisible(self, visible):
"""Set visibility of the profile toolbar
:param visible: True to show toolbar, False to hide it.
"""
self._profile.setVisible(visible)
def setMedianFilterWidgetVisible(self, visible):
"""Set visibility of the median filter parametrs widget.
:param visible: True to show widget, False to hide it.
"""
self._medianParametersWidget.setVisible(visible)
def setMotorPositionsVisible(self, flag):
"""Show or hide motor positions widget"""
self.motorPositionsWidget.setVisible(flag)
def setAlphaSliderVisible(self, visible):
"""Set visibility of the transparency slider widget in the right
toolbar area.
:param visible: True to show widget, False to hide it.
"""
self._alphaSliderToolbar.setVisible(visible)
def setImagesAlpha(self, alpha):
"""Set the opacity of the images layer.
Full opacity means that the background images layer will not
be visible.
:param float alpha: Opacity of images layer, in [0., 1.]
"""
self._alphaSlider.setValue(round(alpha * 255))
def getMaskAction(self):
"""QAction toggling image mask dock widget
:rtype: QAction
"""
return self._getMaskToolsDockWidget().toggleViewAction()
def _emitMaskImageWidgetSignal(self):
mask = self.getSelectionMask()
if not mask.size:
# workaround to ignore the empty mask emitted when the mask widget
# is initialized
return
self.sigMaskImageWidget.emit(
{"event": "selectionMaskChanged",
"current": self.getSelectionMask(),
"id": id(self)})
def setSelectionMask(self, mask, copy=True):
"""Set the mask to a new array.
:param numpy.ndarray mask: The array to use for the mask.
Mask type: array of uint8 of dimension 2,
Array of other types are converted.
:param bool copy: True (the default) to copy the array,
False to use it as is if possible.
:return: None if failed, shape of mask as 2-tuple if successful.
The mask can be cropped or padded to fit active image,
the returned shape is that of the active image.
"""
# disconnect temporarily to avoid infinite loop
self._getMaskToolsDockWidget().sigMaskChanged.disconnect(
self._emitMaskImageWidgetSignal)
if mask is None and silx.version_info <= (0, 7, 0):
self._getMaskToolsDockWidget().resetSelectionMask()
ret = None
else:
# from silx 0.8 onwards, setSelectionMask(None) is supported
ret = self._getMaskToolsDockWidget().setSelectionMask(mask,
copy=copy)
self._getMaskToolsDockWidget().sigMaskChanged.connect(
self._emitMaskImageWidgetSignal)
return ret
def getSelectionMask(self, copy=True):
"""Get the current mask as a 2D 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' image.
If there is no active image, None is returned.
:rtype: 2D numpy.ndarray of uint8
"""
return self._getMaskToolsDockWidget().getSelectionMask(copy=copy)
@staticmethod
def _RgbaToGrayscale(image):
"""Convert RGBA image to 2D array of grayscale values
(Luma coding)
:param image: RGBA image, as a numpy array of shapes
(nrows, ncols, 3/4)
:return: Image as a 2D array
"""
if len(image.shape) == 2:
return image
assert len(image.shape) == 3
imageData = image[:, :, 0] * 0.299 +\
image[:, :, 1] * 0.587 +\
image[:, :, 2] * 0.114
return imageData
def getImageData(self):
"""Return current image data to be sent to RGB correlator
:return: Image as a 2D array
"""
index = self.slider.value()
image = self._images[index]
return self._RgbaToGrayscale(image)
def getFirstBgImageData(self):
"""Return first bg image data to be sent to RGB correlator
:return: Image as a 2D array
"""
image = self._bg_images[0]
return self._RgbaToGrayscale(image)
def getBgImagesDict(self):
"""Return a dict containing the data for all background images."""
bgimages = {}
for i, label in enumerate(self._bg_labels):
data = self._bg_images[i]
origin = self._bg_origins[i]
delta_w, delta_h = self._bg_deltaXY[i]
w, h = delta_w * data.shape[1], delta_h * data.shape[0]
bgimages[label] = {"data": data,
"origin": origin,
"width": w,
"height": h}
return bgimages
def _addImageClicked(self):
imageData = self.getImageData()
ddict = {
'event': "addImageClicked",
'image': imageData,
'title': self.plot.getGraphTitle(),
'id': id(self)}
self.sigMaskImageWidget.emit(ddict)
def _replaceImageClicked(self):
imageData = self.getImageData()
ddict = {
'event': "replaceImageClicked",
'image': imageData,
'title': self.plot.getGraphTitle(),
'id': id(self)}
self.sigMaskImageWidget.emit(ddict)
def _removeImageClicked(self):
imageData = self.getImageData()
ddict = {
'event': "removeImageClicked",
'image': imageData,
'title': self.plot.getGraphTitle(),
'id': id(self)}
self.sigMaskImageWidget.emit(ddict)
def showImage(self, index=0):
"""Show data image corresponding to index. Update slider to index.
"""
if not self._images:
return
assert index < len(self._images)
bg_index = None
if self.backgroundButton.isChecked():
for i, imageName in enumerate(self._labels):
if imageName.lower().endswith('background'):
bg_index = i
break
mf_text = ""
a = self._medianParameters['row_width']
b = self._medianParameters['column_width']
if max(a, b) > 1:
mf_text = "MF(%d,%d) " % (a, b)
imdata = self._getMedianData(self._images[index])
if bg_index is None:
self.plot.setGraphTitle(mf_text + self._labels[index])
else:
self.plot.setGraphTitle(mf_text + self._labels[index] + " Net")
imdata -= self._images[bg_index]
if len(self._infos) > 1:
info = self._infos[index]
else:
info = self._infos[0]
self.plot.addImage(imdata,
legend="current",
origin=self._origin,
scale=self._deltaXY,
replace=False,
z=0,
info=info)
self.plot.setActiveImage("current")
self.slider.setValue(index)
def _getMedianData(self, data):
data = copy.copy(data)
if max(self._medianParameters['row_width'],
self._medianParameters['column_width']) > 1:
data = medfilt2d(data,
[self._medianParameters['row_width'],
self._medianParameters['column_width']],
conditional=self._medianParameters['conditional'])
return data
def setImages(self, images, labels=None,
origin=None, height=None, width=None,
infos=None):
"""Set the list of data images.
All images share the same origin, width and height.
:param images: List of 2D or 3D (for RGBA data) numpy arrays
of image data. All images must have the same shape.
:type images: List of ndarrays
:param labels: list of image names
:param origin: Image origin: coordinate (x, y) of sample located at
(row, column) = (0, 0). If None, use (0., 0.)
:param height: Image height in Y axis units. If None, use the
image height in number of pixels.
:param width: Image width in X axis units. If None, use the
image width in number of pixels.
:param infos: List of info dicts, one per image, or None.
"""
self._images = images
if labels is None:
labels = ["Image %d" % (i + 1) for i in range(len(images))]
if infos is None:
infos = [{} for _img in images]
self._labels = labels
self._infos = infos
height_pixels, width_pixels = images[0].shape[0:2]
height = height or height_pixels
width = width or width_pixels
self._deltaXY = (float(width) / width_pixels,
float(height) / height_pixels)
self._origin = origin or (0., 0.)
current = self.slider.value()
self.slider.setMaximum(len(self._images) - 1)
if current < len(self._images):
self.showImage(current)
else:
self.showImage(0)
# _maskParamsCache = width, height, self._origin, self._deltaXY
# if _maskParamsCache != self._maskParamsCache:
# self._maskParamsCache = _maskParamsCache
# self.resetMask(width, height, self._origin, self._deltaXY)
def _updateBgScales(self, heights, widths):
"""Recalculate BG scales
(e.g after a crop operation on :attr:`_bg_images`)"""
self._bg_deltaXY = []
for w, h, img in zip(widths,
heights,
self._bg_images):
self._bg_deltaXY.append(
(float(w) / img.shape[1], float(h) / img.shape[0])
)
def setBackgroundImages(self, images, labels=None,
origins=None, heights=None, widths=None):
"""Set the list of background images.
Each image should be a tile and have an origin (x, y) tuple,
a height and a width defined, so that all images can be plotted
on the same background layer.
:param images: List of 2D or 3D (for RGBA data) numpy arrays
of image data. All images must have the same shape.
:type images: List of ndarrays
:param labels: list of image names
:param origins: Images origins: list of coordinate tuples (x, y)
of sample located at (row, column) = (0, 0).
If None, use (0., 0.) for all images.
:param heights: Image height in Y axis units. If None, use the
image height in number of pixels.
:param widths: Image width in X axis units. If None, use the
image width in number of pixels.
"""
self._bg_images = images
if labels is None:
labels = ["Background image %d" % (i + 1) for i in range(len(images))]
# delete existing images
for label in self._bg_labels:
self.plot.removeImage(label)
self._bg_labels = labels
if heights is None:
heights = [image.shape[0] for image in images]
else:
assert len(heights) == len(images)
if widths is None:
widths = [image.shape[1] for image in images]
else:
assert len(widths) == len(images)
if origins is None:
origins = [(0, 0) for _img in images]
else:
assert len(origins) == len(images)
self._bg_origins = origins
self._updateBgScales(heights, widths)
for bg_deltaXY, bg_orig, label, img in zip(self._bg_deltaXY,
self._bg_origins,
labels,
images):
# FIXME: we use z=-1 because the mask is always on z=1,
# so the data must be on z=0. To be fixed after the silx mask
# is improved
self.plot.addImage(img,
origin=bg_orig,
scale=bg_deltaXY,
legend=label,
replace=False,
z=-1) # TODO: z=0
def getCurrentIndex(self):
"""
:return: Index of slider widget used for image selection.
"""
return self.slider.value()
def getCurrentColormap(self):
"""Return colormap dict associated with the current image.
If the current image is a RGBA Image, return None.
See doc of silx.gui.plot.Plot for an explanation about the colormap
dictionary.
"""
image = self.plot.getImage(legend="current")
if not hasattr(image, "getColormap"): # isinstance(image, silx.gui.plot.items.ImageRgba):
return None
return self.plot.getImage(legend="current").getColormap()
def showAndRaise(self):
self.show()
self.raise_()
if __name__ == "__main__":
app = qt.QApplication([])
w = SilxMaskImageWidget()
w.show()
w.plot.addImage([[0, 1, 2], [2, 1, -1]])
app.exec_()
|