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
|
# coding: utf-8
# /*##########################################################################
#
# Copyright (C) 2016-2018 European Synchrotron Radiation Facility
#
# 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.
#
# ###########################################################################*/
__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "08/01/2021"
import numpy
import logging
import silx.gui.plot
from silx.gui import qt
from silx.gui import icons
import pyFAI.utils
from pyFAI.calibrant import Calibrant
from .AbstractCalibrationTask import AbstractCalibrationTask
import pyFAI.detectors
from ..dialog.DetectorSelectorDialog import DetectorSelectorDialog
from ..helper.SynchronizeRawView import SynchronizeRawView
from ..CalibrationContext import CalibrationContext
from ..utils import units
from ..utils import validators
from ..utils import FilterBuilder
from ..helper.SynchronizePlotBackground import SynchronizePlotBackground
_logger = logging.getLogger(__name__)
class ExperimentTask(AbstractCalibrationTask):
def _initGui(self):
qt.loadUi(pyFAI.utils.get_ui_file("calibration-experiment.ui"), self)
icon = icons.getQIcon("pyfai:gui/icons/task-settings")
self.setWindowIcon(icon)
self.initNextStep()
self._detectorLabel.setAcceptDrops(True)
self._image.setAcceptDrops(True)
self._mask.setAcceptDrops(True)
self._imageLoader.setDialogTitle("Load calibration image")
self._maskLoader.setDialogTitle("Load mask image")
self._customDetector.clicked.connect(self.__customDetector)
self.__plot = self.__createPlot(parent=self._imageHolder)
self.__plot.setObjectName("plot-experiment")
self.__plotBackground = SynchronizePlotBackground(self.__plot)
layout = qt.QVBoxLayout(self._imageHolder)
layout.addWidget(self.__plot)
layout.setContentsMargins(1, 1, 1, 1)
self._imageHolder.setLayout(layout)
self._detectorFileDescription.setElideMode(qt.Qt.ElideMiddle)
self._calibrant.setFileLoadable(True)
self._calibrant.sigLoadFileRequested.connect(self.loadCalibrant)
self.__synchronizeRawView = SynchronizeRawView()
self.__synchronizeRawView.registerTask(self)
self.__synchronizeRawView.registerPlot(self.__plot)
validator = validators.AdvancedDoubleValidator(self)
validator.setBottom(0)
validator.setIncludedBound(False, True)
validator.setAllowEmpty(True)
self._energy.setValidator(validator)
self._wavelength.setValidator(validator)
def __createPlot(self, parent):
plot = silx.gui.plot.PlotWidget(parent=parent)
plot.setKeepDataAspectRatio(True)
plot.setDataMargins(0.1, 0.1, 0.1, 0.1)
plot.setGraphXLabel("X")
plot.setGraphYLabel("Y")
colormap = CalibrationContext.instance().getRawColormap()
plot.setDefaultColormap(colormap)
from silx.gui.plot import tools
toolBar = tools.InteractiveModeToolBar(parent=self, plot=plot)
plot.addToolBar(toolBar)
toolBar = tools.ImageToolBar(parent=self, plot=plot)
colormapDialog = CalibrationContext.instance().getColormapDialog()
toolBar.getColormapAction().setColorDialog(colormapDialog)
plot.addToolBar(toolBar)
toolBar = qt.QToolBar(self)
plot3dAction = qt.QAction(self)
plot3dAction.setIcon(icons.getQIcon("pyfai:gui/icons/3d"))
plot3dAction.setText("3D visualization")
plot3dAction.setToolTip("Display a 3D visualization of the detector")
plot3dAction.triggered.connect(self.__display3dDialog)
toolBar.addAction(plot3dAction)
plot.addToolBar(toolBar)
return plot
def __display3dDialog(self):
from ..dialog.Detector3dDialog import Detector3dDialog
dialog = Detector3dDialog(self)
settings = self.model().experimentSettingsModel()
detector = settings.detectorModel().detector()
image = settings.image().value()
mask = settings.mask().value()
colormap = CalibrationContext.instance().getRawColormap()
dialog.setData(detector=detector,
image=image, mask=mask, colormap=colormap,
geometry=None)
dialog.exec_()
def _updateModel(self, model):
self.__synchronizeRawView.registerModel(model.rawPlotView())
settings = model.experimentSettingsModel()
self._calibrant.setModel(settings.calibrantModel())
self._detectorLabel.setDetectorModel(settings.detectorModel())
self._image.setModel(settings.image())
self._imageLoader.setModel(settings.image())
self._mask.setModel(settings.mask())
self._maskLoader.setModel(settings.mask())
self._wavelength.setModelUnit(units.Unit.METER_WL)
self._wavelength.setDisplayedUnit(units.Unit.ANGSTROM)
self._energy.setModelUnit(units.Unit.METER_WL)
self._energy.setDisplayedUnit(units.Unit.ENERGY)
self._wavelength.setModel(settings.wavelength())
self._energy.setModel(settings.wavelength())
settings.image().changed.connect(self.__imageUpdated)
settings.calibrantModel().changed.connect(self.__calibrantChanged)
settings.detectorModel().changed.connect(self.__detectorModelUpdated)
settings.wavelength().changed.connect(self.__waveLengthChanged)
settings.changed.connect(self.__settingsChanged)
self.__imageUpdated()
self.__waveLengthChanged()
self.__calibrantChanged()
self.__detectorModelUpdated()
self.__settingsChanged()
def __settingsChanged(self):
settings = self.model().experimentSettingsModel()
settings.detectorModel().changed.connect(self.__detectorModelUpdated)
image = settings.image().value()
detectorModel = settings.detectorModel().detector()
calibrantModel = settings.calibrantModel().calibrant()
wavelength = settings.wavelength().value()
warnings = []
if image is None:
warnings.append("An image have to be specified")
if detectorModel is None:
warnings.append("A detector have to be specified")
if calibrantModel is None:
warnings.append("A calibrant have to be specified")
if wavelength is None:
warnings.append("An energy have to be specified")
if image is not None and calibrantModel is not None:
try:
detector = settings.detector()
binning = detector.guess_binning(image)
if not binning:
raise Exception("inconsistancy")
except Exception:
warnings.append("Inconsistancy between sizes of image and detector")
self._globalWarnings = warnings
self.updateNextStepStatus()
def nextStepWarning(self):
if len(self._globalWarnings) == 0:
return None
else:
warning = ""
for w in self._globalWarnings:
warning += "<li>%s</li>" % w
warning = "<ul>%s</ul>" % warning
warning = "<html>%s</html>" % warning
return warning
def __customDetector(self):
settings = self.model().experimentSettingsModel()
detector = settings.detectorModel().detector()
dialog = DetectorSelectorDialog(self)
dialog.selectDetector(detector)
result = dialog.exec_()
if result:
newDetector = dialog.selectedDetector()
settings.detectorModel().setDetector(newDetector)
def __waveLengthChanged(self):
settings = self.model().experimentSettingsModel()
self._calibrantPreview.setWaveLength(settings.wavelength().value())
def __calibrantChanged(self):
settings = self.model().experimentSettingsModel()
self._calibrantPreview.setCalibrant(settings.calibrantModel().calibrant())
def __detectorModelUpdated(self):
detector = self.model().experimentSettingsModel().detectorModel().detector()
self._detectorSizeUnit.setVisible(detector is not None)
if detector is None:
self._detectorLabel.setStyleSheet("QLabel { color: red }")
self._detectorSize.setText("")
self._detectorPixelSize.setText("")
self._detectorFileDescription.setVisible(False)
self._detectorFileDescriptionTitle.setVisible(False)
else:
self._detectorLabel.setStyleSheet("QLabel { }")
text = [str(s) for s in detector.max_shape]
text = u" × ".join(text)
self._detectorSize.setText(text)
try:
text = ["%0.1f" % (s * 10 ** 6) for s in [detector.pixel1, detector.pixel2]]
text = u" × ".join(text)
except Exception as e:
# Is heterogeneous detectors have pixel size?
_logger.debug(e, exc_info=True)
text = "N.A."
self._detectorPixelSize.setText(text)
if detector.HAVE_TAPER or detector.__class__ == pyFAI.detectors.Detector:
fileDescription = detector.get_splineFile()
elif isinstance(detector, pyFAI.detectors.NexusDetector):
fileDescription = detector.filename
else:
fileDescription = None
if fileDescription is not None:
fileDescription = fileDescription.strip()
if fileDescription == "":
fileDescription = None
self._detectorFileDescription.setVisible(fileDescription is not None)
self._detectorFileDescriptionTitle.setVisible(fileDescription is not None)
self._detectorFileDescription.setText(fileDescription if fileDescription else "")
self.__updateDetector()
def __displayError(self, label, message=""):
self._error.setVisible(True)
self._binning.setVisible(False)
self._binningLabel.setVisible(False)
# self._nextStep.setEnabled(False)
self._error.setText(label)
self._error.setToolTip(message)
def __updateDetectorTemplate(self):
try:
detector = self.model().experimentSettingsModel().detector()
except Exception:
detector = self.model().experimentSettingsModel().detectorModel().detector()
if detector is None:
self._detectorSize.setText("")
self._detectorPixelSize.setText("")
self.__plot.removeMarker("xmin")
self.__plot.removeMarker("xmax")
self.__plot.removeMarker("ymin")
self.__plot.removeMarker("ymax")
else:
try:
binning = detector.get_binning()
except Exception:
binning = 1, 1
# clamping
if binning[0] == 0:
binning = 1, binning[1]
if binning[1] == 0:
binning = binning[0], 1
shape = detector.max_shape[1] // binning[1], detector.max_shape[0] // binning[0]
self.__plot.addXMarker(x=0, legend="xmin", color="grey")
self.__plot.addXMarker(x=shape[0], legend="xmax", color="grey")
self.__plot.addYMarker(y=0, legend="ymin", color="grey")
self.__plot.addYMarker(y=shape[1], legend="ymax", color="grey")
dummy = numpy.array([[[0xF0, 0xF0, 0xF0]]], dtype=numpy.uint8)
self.__plot.addImage(data=dummy, scale=shape, legend="dummy", z=-10, replace=False)
self.__plot.resetZoom()
def __updateDetector(self):
image = self.model().experimentSettingsModel().image().value()
if image is None:
self.__displayError("No image")
self.__updateDetectorTemplate()
return
try:
detector = self.model().experimentSettingsModel().detector()
if detector is None:
self._error.setVisible(False)
self.__updateDetectorTemplate()
return
binning = detector.get_binning()
except Exception as e:
_logger.error(e.args[0])
_logger.debug("Backtrace", exc_info=True)
self.__displayError("Sizes not valid", "Inconsistency between image and detector")
self.__updateDetectorTemplate()
return
self._detectorSizeUnit.setVisible(detector is not None)
self.__updateDetectorTemplate()
if detector.guess_binning(image):
text = [str(s) for s in binning]
text = u" × ".join(text)
self._binning.setText(text)
self._binning.setVisible(True)
self._binningLabel.setVisible(True)
self._error.setVisible(False)
else:
self.__displayError("Sizes not valid", "Inconsistency between image and detector")
def __imageUpdated(self):
image = self.model().experimentSettingsModel().image().value()
self._imageSize.setVisible(image is not None)
self._imageSizeLabel.setVisible(image is not None)
self._imageSizeUnit.setVisible(image is not None)
if image is not None:
self.__plot.addImage(image, legend="image", z=-1, replace=False, copy=False)
text = [str(s) for s in image.shape]
text = u" × ".join(text)
self._imageSize.setText(text)
else:
self.__plot.removeImage("image")
self._imageSize.setText("")
self.__plot.resetZoom()
self.__updateDetector()
def createImageDialog(self, title, forMask=False, previousFile=None):
dialog = CalibrationContext.instance().createFileDialog(self, previousFile=previousFile)
dialog.setWindowTitle(title)
dialog.setModal(True)
builder = FilterBuilder.FilterBuilder()
builder.addImageFormat("EDF image files", "edf")
builder.addImageFormat("TIFF image files", "tif tiff")
builder.addImageFormat("NumPy binary files", "npy")
builder.addImageFormat("CBF files", "cbf")
builder.addImageFormat("MarCCD image files", "mccd")
if forMask:
builder.addImageFormat("Fit2D mask files", "msk")
dialog.setNameFilters(builder.getFilters())
dialog.setFileMode(qt.QFileDialog.ExistingFile)
return dialog
def createCalibrantDialog(self, title):
dialog = CalibrationContext.instance().createFileDialog(self)
dialog.setWindowTitle(title)
dialog.setModal(True)
builder = FilterBuilder.FilterBuilder()
builder.addFileFormat("Calibrant files", "D d DS ds")
dialog.setNameFilters(builder.getFilters())
dialog.setFileMode(qt.QFileDialog.ExistingFile)
return dialog
def loadCalibrant(self):
dialog = self.createCalibrantDialog("Load calibrant file")
result = dialog.exec_()
if not result:
return
filename = dialog.selectedFiles()[0]
try:
calibrant = Calibrant(filename=filename)
except Exception as e:
_logger.error(e.args[0])
_logger.debug("Backtrace", exc_info=True)
# FIXME Display error dialog
return
except KeyboardInterrupt:
raise
try:
settings = self.model().experimentSettingsModel()
settings.calibrantModel().setCalibrant(calibrant)
except Exception as e:
_logger.error(e.args[0])
_logger.debug("Backtrace", exc_info=True)
# FIXME Display error dialog
except KeyboardInterrupt:
raise
|