Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def set_data(self, data):
if data is None or len(data) == 0 or len(data.domain) == 0:
return
self.data = data
domain = data.domain
y_i, x_i, c_i, s_i = [int(random.random() * len(domain)) for i in range(4)]
self.plot.set_axis_title(xBottom, domain[x_i].name)
self.plot.set_show_axis_title(xBottom, True)
self.plot.set_axis_title(yLeft, domain[y_i].name)
self.plot.set_show_axis_title(yLeft, True)
if data.domain[x_i].varType == orange.VarTypes.Discrete:
self.plot.set_axis_labels(xBottom, get_variable_values_sorted(domain[x_i]))
else:
self.plot.set_axis_autoscale(xBottom)
if data.domain[y_i].varType == orange.VarTypes.Discrete:
self.plot.set_axis_labels(yLeft, get_variable_values_sorted(domain[y_i]))
else:
self.plot.set_axis_autoscale(yLeft)
x_data = []
y_data = []
c_data = []
s_data = []
color_cont = (domain[c_i].varType == orange.VarTypes.Continuous)
def updateToolTips(self, showToolTips=True, includeCodebook=True):
if self.node and showToolTips:
node = self.node
text = "Items: %i" % len(self.node.examples)
if includeCodebook:
text += "<hr><b>Codebook vector:</b><br>" + "<br>".join(\
[a.variable.name + ": " + str(a) for a in node.referenceExample \
if a.variable != node.referenceExample.domain.classVar])
if node.examples.domain.classVar and len(node.examples):
dist = orange.Distribution(node.examples.domain.classVar, node.examples)
if node.examples.domain.classVar.varType == orange.VarTypes.Continuous:
text += "<hr>Avg " + node.examples.domain.classVar.name + ":" + ("%.3f" % dist.average())
else:
colors = OWColorPalette.ColorPaletteHSV(len(node.examples.domain.classVar.values))
text += "<hr>" + "<br>".join(["<span style="\"color:%s\"">%s</span>" %(colors[i].name(), str(value) + ": " + str(dist[i])) \
for i, value in enumerate(node.examples.domain.classVar.values)])
self.setToolTip(text)
else:
self.setToolTip("")
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.setCurrentWidget(self.boxIndices[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.item(i).isSelected():
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.addItem(str(value))
if currentOper.isInterval:
self.lbVals.setSelectionMode(QListWidget.MultiSelection)
else:
self.lbVals.setSelectionMode(QListWidget.SingleSelection)
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()
# set the outcome variable
ov = getoutvar(self.predictors.values())
if len(self.predictors) and not ov:
self.tasklabel = "N/A (type mismatch)"
self.classes = []
self.selectedClasses = []
self.clear()
self.outvar = None
return
self.warning(0) # clear all warnings
if ov != self.outvar:
self.outvar = ov
# regression or classification?
if self.outvar.varType == orange.VarTypes.Continuous:
self.copt.hide()
self.tasklabel = "Regression"
else:
self.copt.show()
self.classes = [str(v) for v in self.outvar.values]
self.selectedClasses = []
self.tasklabel = "Classification"
self.checkenable()
self.changedFlag = True
def __attrvalue2sql(self, a):
if a.isSpecial():
if a.varType==orange.VarTypes.Continuous:
return self.special_float_value
else:
return self.special_value
if a.varType == orange.VarTypes.Continuous:
return a.value
elif a.varType == orange.VarTypes.Discrete:
return '"%s"' % a.value
else:
return '"%s"' % str(a)
def compute_bic(self):
data = self.data
medoids = [0] + [data[x] for x in self.mc.medoids] # indices in mapping are 1-based
mapping = self.mc.mapping
K = self.K
M = len(data.domain.attributes)
R = float(len(data))
Ri = [mapping.count(x) for x in range(1+K)]
numFreePar = (M+1.) * K * math.log(R, 2.) / 2.
# sigma**2
s2 = 0.
cidx = [i for i, attr in enumerate(data.domain.attributes) if attr.varType in [orange.VarTypes.Continuous, orange.VarTypes.Discrete]]
for x, midx in izip(data, mapping):
medoid = medoids[midx] # medoids has a dummy element at the beginning, so we don't need -1
s2 += sum( [(float(x[i]) - float(medoid[i]))**2 for i in cidx] )
s2 /= (R - K)
# log-lokehood of clusters: l(Dn)
# log-likehood of clustering: l(D)
ld = 0
bicc = []
for k in range(1, 1+K):
print Ri[k], M, K, R, k
ldn = -1. * Ri[k] * ((math.log(2. * math.pi, 2) / -2.) - (M * math.log(s2, 2) / 2.) + (K / 2.) + math.log(Ri[k], 2) - math.log(R, 2))
ld += ldn
bicc.append(ldn - numFreePar)
return ld - numFreePar, bicc
def __attrVal2sql(self, d):
if d.varType == orange.VarTypes.Continuous:
return d.value
elif d.varType == orange.VarTypes.Discrete:
return str(d.value)
else:
return "'%s'" % str(d.value)
def __attrName2sql(self, d):
def __attrname2sql(self, a):
pom = a.name
subs = [("'",''), ("-",'_'), ("/",'_'), ("\\",'_'), ("+",'__'), (" ",'_')]
for s in subs:
pom = pom.replace(*s)
if pom in reserved_words: # name of attributes can't be select,insert....
pom = "$" + pom # for attribute names that are SQL keywords
if a.varType == orange.VarTypes.Continuous:
pom += " FLOAT"
elif a.varType == orange.VarTypes.Discrete:
if a.values:
pom += " ENUM (%s)" % reduce(lambda x, y: "%s, %s" % (x, y), ['"%s"' % i for i in a.values])
else:
pom += ' ENUM ("empty")'
elif a.varType == orange.VarTypes.String:
pom += " " + char_sql
return pom
discType, intervals = self.indiData[i][:2]
cutpoints = ", ".join(str(attr(x)) for x in disc.getValueFrom.transformer.points)
if not cutpoints:
attrs.append((attr.name, "removed"))
elif not discType:
attrs.append((attr.name, cutpoints))
else:
attrs.append((attr.name, "%s (%s)" % (cutpoints, self.shortDiscNamesUnpar[discType])))
elif disc == None:
if attr.varType == orange.VarTypes.Continuous:
attrs.append((attr.name, "left continuous"))
else:
attrs.append((attr.name, "already discrete"))
classVar = self.data.domain.classVar
if classVar:
if classVar.varType == orange.VarTypes.Continuous:
attrs.append(("Class ('%s')" % classVar.name, "%s (%s)" % (self.classIntervalsLabel,
["equal frequency", "equal width", "custom"][self.classDiscretization])))
attrs.append(("Output discretized class", OWGUI.YesNo[self.outputOriginalClass]))
self.reportSettings("Attributes", attrs)