def test(self): d = orange.ExampleTable("iris") seplen = [float(e[0]) for e in d] b = orange.BasicAttrStat(0, d) self.assertEqual(b.variable, d.domain[0]) self.assertAlmostEqual(b.min, min(seplen)) self.assertAlmostEqual(b.max, max(seplen)) self.assertAlmostEqual(b.avg, sum(seplen) / len(seplen)) b = orange.BasicAttrStat("sepal length", d) self.assertEqual(b.variable, d.domain[0]) self.assertAlmostEqual(b.min, min(seplen)) self.assertAlmostEqual(b.max, max(seplen)) self.assertAlmostEqual(b.avg, sum(seplen) / len(seplen)) b = orange.BasicAttrStat(d.domain[0], d) self.assertEqual(b.variable, d.domain[0]) self.assertAlmostEqual(b.min, min(seplen)) self.assertAlmostEqual(b.max, max(seplen)) self.assertAlmostEqual(b.avg, sum(seplen) / len(seplen)) b = orange.BasicAttrStat(d.domain[0]) for e in d: b.add(e[0]) self.assertEqual(b.variable, d.domain[0]) self.assertAlmostEqual(b.min, min(seplen)) self.assertAlmostEqual(b.max, max(seplen)) self.assertAlmostEqual(b.avg, sum(seplen) / len(seplen)) b.reset() self.assertEqual(b.avg, 0)
def test_pickle(self): d = orange.ExampleTable("iris") b = orange.BasicAttrStat(0, d) self.assertEqual(b.variable, d.domain[0]) import pickle s = pickle.dumps(b) b2 = pickle.loads(s) self.assertEqual(b.sum, b2.sum) self.assertEqual(b.sum2, b2.sum2) self.assertEqual(b.n, b2.n) self.assertEqual(b.min, b2.min) self.assertEqual(b.max, b2.max) self.assertEqual(b.avg, b2.avg) self.assertEqual(b.dev, b2.dev) self.assertEqual(id(b.variable), id(b2.variable))
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) isSelected = False for name in selectedItemNames: items = self.lbVals.findItems(name, Qt.MatchExactly) for item in items: item.setSelected(1) isSelected = True if not currentOper.isInterval: break if not isSelected: if self.lbVals.count() > 0: self.lbVals.item(0).setSelected(True) else: self.currentVals = [] 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 if self.currentVar in self.data.domain.variables: basstat = self.bas[self.currentVar] else: basstat = orange.BasicAttrStat(self.currentVar, self.data) if basstat.n: min, avg, max = [ "%.3f" % x for x in (basstat.min, basstat.avg, basstat.max) ] self.Num1, self.Num2 = basstat.min, basstat.max else: min = avg = max = "-" self.Num1 = self.Num2 = 0 self.lblMin.setText("Min: %s" % min) self.lblAvg.setText("Avg: %s" % avg) self.lblMax.setText("Max: %s" % max) self.lblDefined.setText("Defined for %i example(s)" % basstat.n) 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.setCurrentWidget(self.boxIndices[0])