📄 owfeatureselection.py
字号:
else:
prevVarType = None
prevVarName = None
self.currentVar = self.fs.data.domain[text]
if self.currentVar:
currVarType = self.currentVar.varType
currVarName = self.currentVar.name
else:
currVarType = None
currVarName = None
if currVarType != prevVarType:
self.updateOperatorStack()
if currVarName != prevVarName:
self.updateValuesStack()
def lbOperatorsChange(self):
"""Updates value stack, only if necessary.
"""
if self.currentVar:
varType = self.currentVar.varType
self.currentOperatorDict[varType] = Operator(self.lbOperatorsDict[varType].currentText(), varType)
self.updateValuesStack()
def lbValsChange(self):
"""Updates list of selected discrete values (self.currentVals).
"""
self.currentVals = []
for i in range(0, self.lbVals.count()):
if self.lbVals.isSelected(i):
self.currentVals.append(str(self.lbVals.text(i)))
def OnNewCondition(self):
"""Updates conditions and condition table, sends out new data.
"""
# update self.Conditions
row = self.criteriaTable.currentRow()
if row == self.criteriaTable.numRows():
row -= 1
cond = self.getCondtionFromSelection()
self.Conditions.insert(row+1, cond)
# update self.criteriaTable
self.insertCriteriaTableRow(cond, row+1)
self.updateFilteredDataLens()
# enable Update/Remove buttons
self.btnUpdate.setEnabled(True)
self.btnRemove.setEnabled(True)
# send out new data
if self.updateOnChange:
self.setOutput()
def OnUpdateCondition(self):
"""Calls remove and insert.
TODO: sends out data twice - fix that!
"""
# update self.Conditions
row = self.criteriaTable.currentRow()
if row < 0:
return
cond = self.getCondtionFromSelection()
self.Conditions[row] = cond
# update self.criteriaTable
self.criteriaTable.clearCellWidget(row, 0)
self.criteriaTable.clearCell(row, 1)
self.putContitionToTable(row, cond)
self.updateFilteredDataLens()
# send out new data
if self.updateOnChange:
self.setOutput()
def OnRemoveCondition(self):
"""Removes current condition table row, shifts rows up, updates conditions and sends out new data.
"""
# update self.Conditions
currRow = self.criteriaTable.currentRow()
if currRow < 0:
return
self.Conditions.pop(currRow)
# update self.criteriaTable
numRows = self.criteriaTable.numRows()
for r in range(currRow, numRows - 1):
self.criteriaTable.swapRows(r+1,r)
self.criteriaTable.updateCell(r, 0)
self.criteriaTable.updateCell(r, 1)
for c in range(2):
self.criteriaTable.clearCellWidget(numRows-1, 0)
self.criteriaTable.clearCell(numRows-1, 1)
self.criteriaTable.hideRow(numRows - 1)
self.criteriaTable.setNumRows(numRows - 1)
if currRow == numRows - 1:
self.criteriaTable.setCurrentCell(currRow-1,1)
else:
self.criteriaTable.setCurrentCell(currRow,1)
self.updateFilteredDataLens()
# disable Update/Remove buttons
if len(self.Conditions) == 0:
self.btnUpdate.setEnabled(False)
self.btnRemove.setEnabled(False)
# send out new data
if self.updateOnChange:
self.setOutput()
def OnDisjunction(self):
"""Updates conditions and condition table, sends out new data.
"""
# update self.Conditions
row = self.criteriaTable.currentRow()
if row == self.criteriaTable.numRows():
row -= 1
cond = Condition(True, "OR")
self.Conditions.insert(row+1, cond)
# update self.criteriaTable
self.insertCriteriaTableRow(cond, row+1)
self.updateFilteredDataLens()
# enable Update/Remove buttons
self.btnUpdate.setEnabled(True)
self.btnRemove.setEnabled(True)
# send out new data
if self.updateOnChange:
self.setOutput()
def btnMoveUpClicked(self):
"""Moves the selected condition one row up.
"""
currRow = self.criteriaTable.currentRow()
numRows = self.criteriaTable.numRows()
if currRow < 1 or currRow >= numRows:
return
self.Conditions = self.Conditions[:currRow-1] + [self.Conditions[currRow], self.Conditions[currRow-1]] + self.Conditions[currRow+1:]
self.criteriaTable.swapRows(currRow, currRow-1)
self.criteriaTable.setCurrentCell(currRow-1,1)
self.criteriaTable.updateCell(currRow, 0)
self.criteriaTable.updateCell(currRow, 1)
self.criteriaTable.updateCell(currRow-1, 0)
self.criteriaTable.updateCell(currRow-1, 1)
self.updateFilteredDataLens()
self.updateMoveButtons()
# send out new data
if self.updateOnChange:
self.setOutput()
def btnMoveDownClicked(self):
"""Moves the selected condition one row down.
"""
currRow = self.criteriaTable.currentRow()
numRows = self.criteriaTable.numRows()
if currRow < 0 or currRow >= numRows-1:
return
self.Conditions = self.Conditions[:currRow] + [self.Conditions[currRow+1], self.Conditions[currRow]] + self.Conditions[currRow+2:]
self.criteriaTable.swapRows(currRow, currRow+1)
self.criteriaTable.setCurrentCell(currRow+1,1)
self.criteriaTable.updateCell(currRow, 0)
self.criteriaTable.updateCell(currRow, 1)
self.criteriaTable.updateCell(currRow+1, 0)
self.criteriaTable.updateCell(currRow+1, 1)
self.updateFilteredDataLens()
self.updateMoveButtons()
# send out new data
if self.updateOnChange:
self.setOutput()
def currentCriteriaChange(self, row, col):
"""Handles current row change in criteria table;
select attribute and operator, and set values according to the selected condition.
"""
if row < 0:
return
cond = self.Conditions[row]
if cond.type != "OR":
# attribute
lbItem = self.lbAttr.findItem(cond.varName)
if lbItem:
self.lbAttr.setCurrentItem(lbItem)
# not
self.cbNot.setChecked(cond.negated)
# operator
for vt,lb in self.lbOperatorsDict.items():
if vt == self.name2var[cond.varName].varType:
lb.show()
else:
lb.hide()
lbItem = self.lbOperatorsDict[self.name2var[cond.varName].varType].findItem(str(cond.operator))
if lbItem:
self.lbOperatorsDict[self.name2var[cond.varName].varType].setCurrentItem(lbItem)
# values
self.valuesStack.raiseWidget(self.name2var[cond.varName].varType)
if self.name2var[cond.varName].varType == orange.VarTypes.Continuous:
self.leNum1.setText(str(cond.val1))
if cond.operator.isInterval:
self.leNum2.setText(str(cond.val2))
elif self.name2var[cond.varName].varType == orange.VarTypes.String:
self.leStr1.setText(str(cond.val1))
if cond.operator.isInterval:
self.leStr2.setText(str(cond.val2))
self.cbCaseSensitive.setChecked(cond.caseSensitive)
elif self.name2var[cond.varName].varType == orange.VarTypes.Discrete:
for val in cond.val1:
lbItem = self.lbVals.findItem(val)
if lbItem:
self.lbVals.setSelected(lbItem, True)
self.updateMoveButtons()
def criteriaActiveChange(self, condition, active):
"""Handles clicks on criteria table checkboxes, send out new data.
"""
condition.enabled = active
# update the numbers of examples that matches "OR" filter
self.updateFilteredDataLens(condition)
# send out new data
if self.updateOnChange:
self.setOutput()
############################################################################################################################################################
## Interface state management - updates interface elements based on selection in list boxes ################################################################
############################################################################################################################################################
def updateMoveButtons(self):
"""enable/disable Move Up/Down buttons
"""
row = self.criteriaTable.currentRow()
numRows = self.criteriaTable.numRows()
if row > 0:
self.btnMoveUp.setEnabled(True)
else:
self.btnMoveUp.setEnabled(False)
if row < numRows-1:
self.btnMoveDown.setEnabled(True)
else:
self.btnMoveDown.setEnabled(False)
def updateOperatorStack(self):
"""Raises listbox with appropriate operators.
"""
if self.currentVar:
varType = self.currentVar.varType
else:
varType = 0
for vt,lb in self.lbOperatorsDict.items():
if vt == varType:
lb.show()
else:
lb.hide()
def updateValuesStack(self):
"""Raises appropriate widget for values from stack,
fills listBox for discrete attributes,
shows statistics for continuous attributes.
"""
if self.currentVar:
varType = self.currentVar.varType
else:
varType = 0
currentOper = self.currentOperatorDict.get(varType,None)
if currentOper:
# raise widget
self.valuesStack.raiseWidget(currentOper.varType)
if currentOper.varType==orange.VarTypes.Discrete:
# store selected discrete values, refill values list box, set single/multi selection mode, restore selected item(s)
selectedItemNames = []
for i in range(self.lbVals.count()):
if self.lbVals.isSelected(i):
selectedItemNames.append(str(self.lbVals.item(i).text()))
self.lbVals.clear()
curVarValues = []
for value in self.currentVar:
curVarValues.append(str(value))
curVarValues.sort()
for value in curVarValues:
self.lbVals.insertItem(str(value))
if currentOper.isInterval:
self.lbVals.setSelectionMode(QListBox.Multi)
else:
self.lbVals.setSelectionMode(QListBox.Single)
isSelected = False
for name in selectedItemNames:
item = self.lbVals.findItem(name)
if item:
self.lbVals.setSelected(item, True)
isSelected = True
if not currentOper.isInterval:
break
if not isSelected and self.lbVals.count() > 0:
self.lbVals.setSelected(0, True)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -