📄 owfeatureselection.py
字号:
elif currentOper.varType==orange.VarTypes.Continuous:
# show / hide "and" label and 2nd line edit box
if currentOper.isInterval:
self.lblAndCon.show()
self.leNum2.show()
else:
self.lblAndCon.hide()
self.leNum2.hide()
# display attribute statistics
self.lblMin.setText("Min: %.3f" % self.bas[self.currentVar].min)
self.lblAvg.setText("Avg: %.3f" % self.bas[self.currentVar].avg)
self.lblMax.setText("Max: %.3f" % self.bas[self.currentVar].max)
self.Num1 = self.bas[self.currentVar].min
self.Num2 = self.bas[self.currentVar].max
elif currentOper.varType==orange.VarTypes.String:
# show / hide "and" label and 2nd line edit box
if currentOper.isInterval:
self.lblAndStr.show()
self.leStr2.show()
else:
self.lblAndStr.hide()
self.leStr2.hide()
else:
self.valuesStack.raiseWidget(0)
def insertCriteriaTableRow(self, cond, row):
"""Inserts condition at the given row.
"""
numRows = self.criteriaTable.numRows()
self.criteriaTable.setNumRows(numRows + 1)
for r in range(numRows, row, -1):
self.criteriaTable.swapRows(r-1,r)
self.criteriaTable.updateCell(r, 0)
self.criteriaTable.updateCell(r, 1)
self.putContitionToTable(row, cond)
self.criteriaTable.setCurrentCell(row,1)
def getCondtionFromSelection(self):
"""Returns a condition according to the currently selected attribute / operator / values.
"""
if self.currentVar.varType == orange.VarTypes.Continuous:
val1 = float(self.Num1)
val2 = float(self.Num2)
elif self.currentVar.varType == orange.VarTypes.String:
val1 = self.Str1
val2 = self.Str2
elif self.currentVar.varType == orange.VarTypes.Discrete:
val1 = self.currentVals
val2 = None
if not self.currentOperatorDict[self.currentVar.varType].isInterval:
val2 = None
return Condition(True, "AND", self.currentVar.name, self.currentOperatorDict[self.currentVar.varType], self.NegateCondition, val1, val2, self.CaseSensitive)
def putContitionToTable(self, row, cond):
"""Writes out the condition to the given row in a criteria table.
"""
# column 0 getFilter(self, domain, variable, value1, value2, negate, caseSensitive)
if cond.type == "OR":
cw = QLabel("", self)
else:
cw = QCheckBox(str(len(cond.operator.getFilter(self.fs.data.domain, cond.varName, cond.val1, cond.val2, cond.negated, cond.caseSensitive)(self.fs.data))), self)
cw.setChecked(cond.enabled)
self.connect(cw, SIGNAL("toggled(bool)"), lambda val: self.criteriaActiveChange(cond, val))
self.criteriaTable.setCellWidget(row, 0, cw)
# column 1
if cond.type == "OR":
txt = "OR"
else:
txt = ""
if cond.negated:
txt += "NOT "
txt += cond.varName + " " + str(cond.operator) + " "
if cond.operator != Operator.operatorDef:
if cond.operator.varType == orange.VarTypes.Discrete:
if cond.operator.isInterval:
if len(cond.val1) > 0:
txt += "["
for name in cond.val1:
txt += "%s, " % name
txt = txt[0:-2] + "]"
else:
txt += "[]"
else:
txt += cond.val1[0]
elif cond.operator.varType == orange.VarTypes.String:
if cond.caseSensitive:
cs = " (C)"
else:
cs = ""
if cond.operator.isInterval:
txt += "'%s'%s and '%s'%s" % (cond.val1, cs, cond.val2, cs)
else:
txt += "'%s'%s" % (cond.val1, cs)
elif cond.operator.varType == orange.VarTypes.Continuous:
if cond.operator.isInterval:
txt += str(cond.val1) + " and " + str(cond.val2)
else:
txt += str(cond.val1)
OWGUI.tableItem(self.criteriaTable, row, 1, txt , editType=QTableItem.Never)
def updateFilteredDataLens(self, cond=None):
"""Updates the number of examples that match individual conditions in criteria table.
If cond is given, updates the given row and the corresponding OR row;
if cond==None, updates the number of examples in OR rows.
"""
if cond:
condIdx = self.Conditions.index(cond)
# idx1: the first non-OR condition above the clicked condition
# idx2: the first OR condition below the clicked condition
idx1 = 0
idx2 = len(self.Conditions)
for i in range(condIdx,idx1-1,-1):
if self.Conditions[i].type == "OR":
idx1 = i+1
break
for i in range(condIdx+1,idx2):
if self.Conditions[i].type == "OR":
idx2 = i
break
fdListAll = self.getFilterList(self.fs.data.domain, self.Conditions[idx1:idx2], enabledOnly=False)
fdListEnabled = self.getFilterList(self.fs.data.domain, self.Conditions[idx1:idx2], enabledOnly=True)
# if we click on the row which has a preceeding OR: update OR at index idx1-1
if idx1 > 0:
self.criteriaTable.cellWidget(idx1-1,0).setText(str(len(orange.Filter_conjunction(fdListEnabled[0])(self.fs.data))))
# update the clicked row
self.criteriaTable.cellWidget(condIdx,0).setText(str(len(fdListAll[0][condIdx-idx1](self.fs.data))))
elif len(self.Conditions) > 0:
# update all "OR" rows
fdList = self.getFilterList(self.fs.data.domain, self.Conditions, enabledOnly=True)
idx = 1
for row,cond in enumerate(self.Conditions):
if cond.type == "OR":
self.criteriaTable.cellWidget(row,0).setText(str(len(orange.Filter_conjunction(fdList[idx])(self.fs.data))))
idx += 1
def updateInfoIn(self, data):
"""Updates data in info box.
"""
if data:
varList = data.domain.variables.native() + data.domain.getmetas().values()
self.dataInAttributesLabel.setText("%s attribute%s" % self.sp(varList))
self.dataInExamplesLabel.setText("%s example%s" % self.sp(data))
else:
self.dataInExamplesLabel.setText("No examples.")
self.dataInAttributesLabel.setText("No attributes.")
def updateInfoOut(self, data):
"""Updates data out info box.
"""
if data:
varList = data.domain.variables.native() + data.domain.getmetas().values()
self.dataOutAttributesLabel.setText("%s attribute%s" % self.sp(varList))
self.dataOutExamplesLabel.setText("%s example%s" % self.sp(data))
else:
self.dataOutExamplesLabel.setText("No examples.")
self.dataOutAttributesLabel.setText("No attributes.")
############################################################################################################################################################
## Utility functions #######################################################################################################################################
############################################################################################################################################################
def sp(self, l, capitalize=True):
"""Input: list; returns tupple (str(len(l)), "s"/"")
"""
n = len(l)
if n == 0:
if capitalize:
return "No", "s"
else:
return "no", "s"
elif n == 1:
return str(n), ''
else:
return str(n), 's'
def prinConditions(self):
"""For debugging only.
"""
print "idx\tE\ttype\tattr\toper\tneg\tval1\tval2\tcs"
for i,cond in enumerate(self.Conditions):
if cond.type == "OR":
print "%i\t%i\t%s" % (i+1, int(cond.enabled),cond.type)
else:
print "%i\t%i\t%s\t%s\t%s\t%i\t%s\t%s\t%i" % (i+1,
int(cond.enabled),cond.type,cond.varName,str(cond.operator),
int(cond.negated),str(cond.val1),str(cond.val2),int(cond.caseSensitive))
class Condition:
def __init__(self, enabled, type, attribute = None, operator = None, negate = False, value1 = None, value2 = None, caseSensitive = False):
self.enabled = enabled # True/False
self.type = type # "AND"/"OR"
self.varName = attribute # orange.Variable
self.operator = operator # Operator
self.negated = negate # True/False
self.val1 = value1 # string/float
self.val2 = value2 # string/float
self.caseSensitive = caseSensitive # True/False
class Operator:
operatorsD = staticmethod(["equals","in"])
operatorsC = staticmethod(["=","<","<=",">",">=","between","outside"])
operatorsS = staticmethod(["=","<","<=",">",">=","contains","begins with","ends with","between","outside"])
operatorDef = staticmethod("is defined")
getOperators = staticmethod(lambda: Operator.operatorsD + Operator.operatorsS + [Operator.operatorDef])
_operFilter = {"=":orange.Filter_values.Equal,
"<":orange.Filter_values.Less,
"<=":orange.Filter_values.LessEqual,
">":orange.Filter_values.Greater,
">=":orange.Filter_values.GreaterEqual,
"between":orange.Filter_values.Between,
"outside":orange.Filter_values.Outside,
"contains":orange.Filter_values.Contains,
"begins with":orange.Filter_values.BeginsWith,
"ends with":orange.Filter_values.EndsWith}
def __init__(self, operator, varType):
"""Members: operator, varType, isInterval.
"""
assert operator in Operator.getOperators(), "Unknown operator: %s" % str(operator)
self.operator = operator
self.varType = varType
self.isInterval = False
if operator in Operator.operatorsC and Operator.operatorsC.index(operator) > 4 \
or operator in Operator.operatorsD and Operator.operatorsD.index(operator) > 0 \
or operator in Operator.operatorsS and Operator.operatorsS.index(operator) > 7:
self.isInterval = True
def __eq__(self, other):
assert other in Operator.getOperators()
return self.operator == other
def __ne__(self, other):
assert other in Operator.getOperators()
return self.operator != other
def __repr__(self):
return str(self.operator)
def __strr__(self):
return str(self.operator)
def getFilter(self, domain, variable, value1, value2, negate, caseSensitive):
"""Returns orange filter.
"""
if self.operator == Operator.operatorDef:
f = orange.Filter_isDefined(domain=domain)
for v in domain.variables:
f.check[v] = 0
try:
f.check[variable] = 1
except:
print "Error: orange.Filter_isDefined cannot handle meta attributes (%s)." % variable
elif self.operator in Operator.operatorsD:
f = orange.Filter_values(domain=domain)
f[variable] = value1
else:
f = orange.Filter_values(domain=domain)
if value2:
f[variable] = (Operator._operFilter[str(self.operator)], value1, value2)
else:
f[variable] = (Operator._operFilter[str(self.operator)], value1)
if self.varType == orange.VarTypes.String:
f[variable].caseSensitive = caseSensitive
f.negate = negate
return f
if __name__=="__main__":
import sys
from orngTextCorpus import *
lem = lemmatizer.FSALemmatization('/home/mkolar/Docs/Diplomski/repository/orange/OrangeWidgets/TextData/engleski_rjecnik.fsa')
for word in loadWordSet('/home/mkolar/Docs/Diplomski/repository/orange/OrangeWidgets/TextData/engleski_stoprijeci.txt'):
lem.stopwords.append(word)
data = TextCorpusLoader('/home/mkolar/Docs/Diplomski/repository/orange/OrangeWidgets/Other/reuters-exchanges-small.xml', lem = lem)
a=QApplication(sys.argv)
ow=OWFeatureSelection()
a.setMainWidget(ow)
ow.show()
ow.onDataInput(data.data)
a.exec_loop()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -