"README.md" did not exist on "0294109eed00040057ce9c3074058f18478f65f1"
Newer
Older
#pylint: disable = too-many-instance-attributes, too-many-branches, too-many-public-methods
#pylint: disable = W0622
from __future__ import (absolute_import, division, print_function)
from reduction_gui.widgets.base_widget import BaseWidget
from reduction_gui.reduction.toftof.toftof_reduction import TOFTOFScriptElement
#-------------------------------------------------------------------------------
class TOFTOFSetupWidget(BaseWidget):
''' The one and only tab page. '''
name = 'TOFTOF Reduction'
class DataRunModel(QAbstractTableModel):
Jan Burle
committed
''' The list of data runs and corresponding comments. '''
def __init__(self, parent):
QAbstractTableModel.__init__(self, parent)
self.dataRuns = [] # [(runs, comment), ...]
def _numRows(self):
return len(self.dataRuns)
def _getRow(self, row):
return self.dataRuns[row] if row < self._numRows() else ('', '')
def _isRowEmpty(self, row):
(runs, comment) = self._getRow(row)
return not runs.strip() and not comment.strip()
def _removeTrailingEmptyRows(self):
for row in reversed(range(self._numRows())):
if self._isRowEmpty(row):
del self.dataRuns[row]
else:
break
def _removeEmptyRows(self):
for row in reversed(range(self._numRows())):
if self._isRowEmpty(row):
del self.dataRuns[row]
def _ensureHasRows(self, numRows):
while self._numRows() < numRows:
self.dataRuns.append(('', ''))
def _setCellText(self, row, col, text):
self._ensureHasRows(row + 1)
(runText, comment) = self.dataRuns[row]
text = text.strip()
runText = text
else:
comment = text
self.dataRuns[row] = (runText, comment)
def _getCellText(self, row, col):
return self._getRow(row)[col].strip()
# reimplemented QAbstractTableModel methods
headers = ('Data runs', 'Comment')
selectCell = pyqtSignal(QModelIndex)
def emptyCells(self, indexes):
for index in indexes:
row = index.row()
col = index.column()
self._setCellText(row, col, '')
self._removeEmptyRows()
self.reset()
# indexes is never empty
self.selectCell.emit(indexes[0])
# one additional row for new data
return self._numRows() + 1
return 2
def headerData(self, section, orientation, role):
if Qt.Horizontal == orientation and Qt.DisplayRole == role:
return self.headers[section]
return None
def data(self, index, role):
if Qt.DisplayRole == role or Qt.EditRole == role:
return self._getCellText(index.row(), index.column())
self._setCellText(row, col, text)
self._removeTrailingEmptyRows()
# signal the attached view
self.reset()
# move selection to the next column or row
col = col + 1
if col >= 2:
row = row + 1
col = 0
row = min(row, self.rowCount() - 1)
self.selectCell.emit(self.index(row, col))
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
class DataRunView(QTableView):
def keyPressEvent(self, QKeyEvent):
if self.state() == QAbstractItemView.EditingState:
index = self.currentIndex()
if QKeyEvent.key() in [Qt.Key_Down, Qt.Key_Up]:
self.setFocus()
self.setCurrentIndex(self.model().index(index.row(), index.column()))
else:
QTableView.keyPressEvent(self, QKeyEvent)
if QKeyEvent.key() in [Qt.Key_Delete, Qt.Key_Backspace]:
self.model().emptyCells(self.selectedIndexes())
else:
QTableView.keyPressEvent(self, QKeyEvent)
# tooltips
TIP_prefix = ''
TIP_dataDir = ''
Jan Burle
committed
Jan Burle
committed
Jan Burle
committed
TIP_binEon = ''
TIP_binEstart = ''
TIP_binEstep = ''
TIP_binEend = ''
Jan Burle
committed
TIP_binQon = ''
TIP_binQstart = ''
TIP_binQstep = ''
TIP_binQend = ''
Jan Burle
committed
Jan Burle
committed
Jan Burle
committed
TIP_chkReplaceNaNs = 'Replace NaNs with 0'
TIP_chkCreateDiff = ''
TIP_chkKeepSteps = ''
Jan Burle
committed
TIP_chkSofQW = ''
TIP_chkSofTW = ''
TIP_chkNxspe = 'Save for MSlice'
TIP_chkNexus = 'Save for Mantid'
TIP_chkAscii = 'Will be available soon'
TIP_rbtNormaliseNone = ''
TIP_rbtNormaliseMonitor = ''
TIP_rbtNormaliseTime = ''
TIP_rbtCorrectTOFNone = ''
TIP_rbtCorrectTOFVan = ''
TIP_rbtCorrectTOFSample = ''
def dir_browse_dialog(self, default_dir=''):
"""
Pop up a directory dialog box.
"""
dirname = str(QFileDialog.getExistingDirectory(self, "Select Directory", default_dir, QFileDialog.DontUseNativeDialog))
return dirname
def __init__(self, settings):
def set_spin(spin, minVal = -inf, maxVal = +inf, decimals = 3):
spin.setDecimals(decimals)
if text:
widget.setToolTip(text)
return widget
# ui data elements
self.prefix = tip(QLineEdit(), self.TIP_prefix)
self.dataDir = tip(QLineEdit(), self.TIP_dataDir)
self.saveDir = tip(QLineEdit(), self.TIP_saveDir)
self.vanRuns = tip(QLineEdit(), self.TIP_vanRuns)
self.vanCmnt = tip(QLineEdit(), self.TIP_vanCmnt)
self.ecRuns = tip(QLineEdit(), self.TIP_ecRuns)
self.ecFactor = tip(QDoubleSpinBox(), self.TIP_ecFactor)
Jan Burle
committed
self.binEon = tip(QCheckBox(), self.TIP_binEon)
self.binEstart = tip(QDoubleSpinBox(), self.TIP_binEstart)
self.binEstep = tip(QDoubleSpinBox(), self.TIP_binEstep)
self.binEend = tip(QDoubleSpinBox(), self.TIP_binEend)
set_spin(self.binEstep, decimals = 4)
Jan Burle
committed
self.binQon = tip(QCheckBox(), self.TIP_binQon)
self.binQstart = tip(QDoubleSpinBox(), self.TIP_binQstart)
self.binQstep = tip(QDoubleSpinBox(), self.TIP_binQstep)
self.binQend = tip(QDoubleSpinBox(), self.TIP_binQend)
set_spin(self.binQstart)
set_spin(self.binQstep)
set_spin(self.binQend)
self.maskDetectors = tip(QLineEdit(), self.TIP_maskDetectors)
self.dataRunsView = tip(self.DataRunView(self), self.TIP_dataRunsView)
self.dataRunsView.horizontalHeader().setStretchLastSection(True)
self.dataRunsView.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.runDataModel = TOFTOFSetupWidget.DataRunModel(self)
self.dataRunsView.setModel(self.runDataModel)
self.btnDataDir = tip(QPushButton('Browse'), self.TIP_btnDataDir)
self.btnSaveDir = tip(QPushButton('Browse'), self.TIP_btnSaveDir)
self.chkSubtractECVan = tip(QCheckBox('Subtract empty can from vanadium'), self.TIP_chkSubtractECVan)
self.chkReplaceNaNs = tip(QCheckBox('Replace special values in S(Q,W) with 0'), self.TIP_chkReplaceNaNs)
self.chkCreateDiff = tip(QCheckBox('Create diffractograms'), self.TIP_chkCreateDiff)
self.chkKeepSteps = tip(QCheckBox('Keep intermediate steps'), self.TIP_chkKeepSteps)
self.chkSofQW = tip(QCheckBox('S(Q,W)'), self.TIP_chkSofQW)
self.chkSofTW = tip(QCheckBox('S(2theta,W)'), self.TIP_chkSofTW)
self.chkNxspe = tip(QCheckBox('NXSPE'), self.TIP_chkNxspe)
self.chkNexus = tip(QCheckBox('NeXus'), self.TIP_chkNexus)
self.chkAscii = tip(QCheckBox('Ascii'), self.TIP_chkAscii)
self.rbtNormaliseNone = tip(QRadioButton('none'), self.TIP_rbtNormaliseNone)
self.rbtNormaliseMonitor = tip(QRadioButton('to monitor'), self.TIP_rbtNormaliseMonitor)
self.rbtNormaliseTime = tip(QRadioButton('to time'), self.TIP_rbtNormaliseTime)
self.rbtCorrectTOFNone = tip(QRadioButton('none'), self.TIP_rbtCorrectTOFNone)
self.rbtCorrectTOFVan = tip(QRadioButton('vanadium'), self.TIP_rbtCorrectTOFVan)
self.rbtCorrectTOFSample = tip(QRadioButton('sample'), self.TIP_rbtCorrectTOFSample)
for wgt in widgets:
if isinstance(wgt, QLayout):
box.addLayout(wgt)
elif isinstance(wgt, QWidget):
box.addWidget(wgt)
def hbox(widgets):
return _box(QHBoxLayout, widgets)
def vbox(widgets):
return _box(QVBoxLayout, widgets)
gbDataDir = QGroupBox('Data search directory')
gbPrefix = QGroupBox('Workspace prefix')
gbOptions = QGroupBox('Options')
gbSave = QGroupBox('Save reduced data')
gbInputs = QGroupBox('Inputs')
gbBinning = QGroupBox('Binning')
gbData = QGroupBox('Data')
box = QVBoxLayout()
self._layout.addLayout(box)
box.addLayout(hbox((vbox((gbInputs, gbBinning, gbOptions, 1)), vbox((gbData, gbSave)))))
gbDataDir.setLayout(hbox((self.dataDir, self.btnDataDir)))
gbPrefix.setLayout(hbox((self.prefix,)))
grid = QGridLayout()
grid.addWidget(self.chkSubtractECVan, 0, 0, 1, 4)
Jan Burle
committed
grid.addWidget(label('Normalise','tip'),1, 0)
grid.addWidget(self.rbtNormaliseNone, 1, 1)
grid.addWidget(self.rbtNormaliseMonitor,1, 2)
grid.addWidget(self.rbtNormaliseTime, 1, 3)
grid.addWidget(QLabel('Correct TOF'), 2, 0)
grid.addWidget(self.rbtCorrectTOFNone, 2, 1)
grid.addWidget(self.rbtCorrectTOFVan, 2, 2)
grid.addWidget(self.rbtCorrectTOFSample,2, 3)
grid.addWidget(self.chkReplaceNaNs, 3, 0, 1, 4)
grid.addWidget(self.chkCreateDiff, 4, 0, 1, 4)
grid.addWidget(self.chkKeepSteps, 5, 0, 1, 4)
grid.setColumnStretch(4, 1)
gbOptions.setLayout(grid)
btnGroup = QButtonGroup(self)
btnGroup.addButton(self.rbtNormaliseNone)
btnGroup.addButton(self.rbtNormaliseMonitor)
btnGroup.addButton(self.rbtNormaliseTime)
btnGroup = QButtonGroup(self)
btnGroup.addButton(self.rbtCorrectTOFNone)
btnGroup.addButton(self.rbtCorrectTOFVan)
btnGroup.addButton(self.rbtCorrectTOFSample)
grid = QGridLayout()
grid.addWidget(QLabel('Vanadium runs'), 0, 0)
grid.addWidget(self.vanRuns, 0, 1, 1, 3)
grid.addWidget(self.vanCmnt, 1, 1, 1, 3)
grid.addWidget(QLabel('Empty can runs'),2, 0)
grid.addWidget(self.ecRuns, 2, 1)
grid.addWidget(QLabel('EC factor'), 2, 2)
grid.addWidget(self.ecFactor, 2, 3)
grid.addWidget(QLabel('Mask detectors'),3, 0)
grid.addWidget(self.maskDetectors, 3, 1, 1, 3)
Jan Burle
committed
grid.addWidget(QLabel('on'), 0, 1)
grid.addWidget(QLabel('start'), 0, 2)
grid.addWidget(QLabel('step'), 0, 3)
grid.addWidget(QLabel('end'), 0, 4)
grid.addWidget(QLabel('Energy'), 1, 0)
Jan Burle
committed
grid.addWidget(self.binEon, 1, 1)
grid.addWidget(self.binEstart, 1, 2)
grid.addWidget(self.binEstep, 1, 3)
grid.addWidget(self.binEend, 1, 4)
grid.addWidget(QLabel('Q'), 2, 0)
Jan Burle
committed
grid.addWidget(self.binQon, 2, 1)
grid.addWidget(self.binQstart, 2, 2)
grid.addWidget(self.binQstep, 2, 3)
grid.addWidget(self.binQend, 2, 4)
for col in (0, 2, 3, 4):
grid.setColumnStretch(col, 1)
gbData.setLayout(hbox((self.dataRunsView,)))
grid = QGridLayout()
grid.addWidget(QLabel('Workspaces'), 0, 0)
grid.addWidget(self.chkSofQW, 1, 0)
grid.addWidget(self.chkSofTW, 1, 1)
grid.addWidget(QLabel('Format'), 2, 0)
grid.addWidget(self.chkNxspe, 3, 0)
grid.addWidget(self.chkNexus, 3, 1)
grid.addWidget(self.chkAscii, 3, 2)
grid.setColumnStretch(3, 1)
# disable save Ascii, it is not available for the moment
self.chkAscii.setEnabled(False)
gbSave.setLayout(vbox((label('Directory',''), hbox((self.saveDir, self.btnSaveDir)), grid)))
Jan Burle
committed
self.btnDataDir.clicked.connect(self._onDataDir)
self.btnSaveDir.clicked.connect(self._onSaveDir)
Jan Burle
committed
self.binEon.clicked.connect(self._onBinEon)
self.binQon.clicked.connect(self._onBinQon)
Jan Burle
committed
def _onDataDir(self):
dirname = self.dir_browse_dialog(self.dataDir.text())
if dirname:
self.dataDir.setText(dirname)
def _onSaveDir(self):
dirname = self.dir_browse_dialog(self.saveDir.text())
if dirname:
self.saveDir.setText(dirname)
if not onVal:
self.chkNxspe.setChecked(False)
self.chkReplaceNaNs.setChecked(False)
self.binQon.setChecked(False)
for widget in (self.binEstart, self.binEstep, self.binEend, self.chkCreateDiff, self.chkNxspe, self.binQon,
self.binQstart, self.binQstep, self.binQend, self.chkReplaceNaNs, self.chkSofQW):
Jan Burle
committed
for widget in (self.binQstart, self.binQstep, self.binQend, self.chkReplaceNaNs, self.chkSofQW):
Jan Burle
committed
def _onSelectedCell(self, index):
self.dataRunsView.setCurrentIndex(index)
self.dataRunsView.setFocus()
return lineEdit.text().strip()
elem.facility_name = self._settings.facility_name
elem.instrument_name = self._settings.instrument_name
elem.prefix = line_text(self.prefix)
elem.dataDir = line_text(self.dataDir)
elem.vanRuns = line_text(self.vanRuns)
elem.vanCmnt = line_text(self.vanCmnt)
elem.ecRuns = line_text(self.ecRuns)
elem.ecFactor = self.ecFactor.value()
elem.binEon = self.binEon.isChecked()
elem.binEstart = self.binEstart.value()
elem.binEstep = self.binEstep.value()
elem.binEend = self.binEend.value()
elem.binQon = self.binQon.isChecked()
elem.binQstart = self.binQstart.value()
elem.binQstep = self.binQstep.value()
elem.binQend = self.binQend.value()
elem.maskDetectors = line_text(self.maskDetectors)
elem.subtractECVan = self.chkSubtractECVan.isChecked()
elem.replaceNaNs = self.chkReplaceNaNs.isChecked()
elem.createDiff = self.chkCreateDiff.isChecked()
elem.keepSteps = self.chkKeepSteps.isChecked()
elem.saveSofQW = self.chkSofQW.isChecked()
elem.saveSofTW = self.chkSofTW.isChecked()
elem.saveNXSPE = self.chkNxspe.isChecked()
elem.saveNexus = self.chkNexus.isChecked()
elem.saveAscii = self.chkAscii.isChecked()
elem.normalise = elem.NORM_MONITOR if self.rbtNormaliseMonitor.isChecked() else \
elem.NORM_TIME if self.rbtNormaliseTime.isChecked() else \
elem.NORM_NONE
elem.correctTof = elem.CORR_TOF_VAN if self.rbtCorrectTOFVan.isChecked() else \
elem.CORR_TOF_SAMPLE if self.rbtCorrectTOFSample.isChecked() else \
elem.CORR_TOF_NONE
def set_state(self, toftofScriptElement):
self.vanRuns.setText(elem.vanRuns)
self.vanCmnt.setText(elem.vanCmnt)
self.ecRuns.setText(elem.ecRuns)
self.ecFactor.setValue(elem.ecFactor)
self.runDataModel.reset()
self.binEon.setChecked(elem.binEon)
self._onBinEon(elem.binEon)
self.binEstart.setValue(elem.binEstart)
self.binEstep.setValue(elem.binEstep)
self.binEend.setValue(elem.binEend)
self.binQon.setChecked(elem.binQon)
self._onBinQon(elem.binQon)
self.binQstart.setValue(elem.binQstart)
self.binQstep.setValue(elem.binQstep)
self.binQend.setValue(elem.binQend)
self.maskDetectors.setText(elem.maskDetectors)
self.chkSubtractECVan.setChecked(elem.subtractECVan)
self.chkReplaceNaNs.setChecked(elem.replaceNaNs)
self.chkCreateDiff.setChecked(elem.createDiff)
self.chkKeepSteps.setChecked(elem.keepSteps)
self.saveDir.setText(elem.saveDir)
self.chkSofQW.setChecked(elem.saveSofQW)
self.chkSofTW.setChecked(elem.saveSofTW)
self.chkNxspe.setChecked(elem.saveNXSPE)
self.chkNexus.setChecked(elem.saveNexus)
self.chkAscii.setChecked(elem.saveAscii)
self.rbtNormaliseMonitor.setChecked(True)
self.rbtNormaliseTime.setChecked(True)
else:
self.rbtNormaliseNone.setChecked(True)
self.rbtCorrectTOFVan.setChecked(True)