Changeset 3788:e613b5bae4a6 in orange


Ignore:
Timestamp:
07/18/07 14:05:50 (7 years ago)
Author:
Gregor <Gregor@…>
Branch:
default
Convert:
2de07887e76706f248bb4ae7f058d8d43e958194
Message:

* empty log message *

Location:
orange
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • orange/OrangeCanvas/orngCanvas.pyw

    r3694 r3788  
    868868    def keyPressEvent(self, e): 
    869869        QMainWindow.keyPressEvent(self,e) 
    870         print "state", e.state(), Qt.ControlButton 
    871870        if e.state() & Qt.ControlButton != 0: 
    872871            self.ctrlPressed = 1 
  • orange/OrangeCanvas/orngCanvasItems.py

    r3519 r3788  
    121121        painter.drawLine(QPoint(startX, startY), QPoint(endX, endY)) 
    122122 
    123  
    124     """ 
    125     # draw the line 
    126     def drawShape(self, painter): 
    127         (startX, startY) = (self.startPoint().x(), self.startPoint().y()) 
    128         (endX, endY)  = (self.endPoint().x(), self.endPoint().y()) 
    129  
    130         if self.getEnabled(): lineStyle = Qt.SolidLine 
    131         else:                 lineStyle = Qt.DashLine 
    132  
    133         painter.setPen(QPen(QColor("green"), 1, lineStyle)) 
    134         painter.drawLine(QPoint(startX, startY), QPoint(endX, endY)) 
    135  
    136         if len(self.colors) == 1: 
    137             painter.setPen(QPen(QColor(self.colors[0]), 6, lineStyle)) 
    138             painter.drawLine(QPoint(startX, startY), QPoint(endX, endY)) 
    139         elif len(self.colors) == 2: 
    140             painter.setPen(QPen(QColor(self.colors[1]), 3, lineStyle)) 
    141             painter.drawLine(QPoint(startX, startY+3), QPoint(endX, endY+3)) 
    142             painter.setPen(QPen(QColor(self.colors[0]), 3, lineStyle)) 
    143             painter.drawLine(QPoint(startX, startY-3), QPoint(endX, endY-3)) 
    144         elif len(self.colors) == 3: 
    145             painter.setPen(QPen(QColor(self.colors[2]), 2, lineStyle)) 
    146             painter.drawLine(QPoint(startX, startY+3), QPoint(endX, endY+3)) 
    147             painter.setPen(QPen(QColor(self.colors[1]), 2, lineStyle)) 
    148             painter.drawLine(QPoint(startX, startY)   , QPoint(endX, endY)) 
    149             painter.setPen(QPen(QColor(self.colors[0]), 2, lineStyle)) 
    150             painter.drawLine(QPoint(startX, startY-3), QPoint(endX, endY-3)) 
    151  
    152     # draw the line on the printer 
    153     def printShape(self, painter): 
    154         (startX, startY) = (self.startPoint().x(), self.startPoint().y()) 
    155         (endX, endY)  = (self.endPoint().x(), self.endPoint().y()) 
    156  
    157         fact = 10 
    158  
    159         if self.getEnabled(): 
    160             lineStyle = Qt.SolidLine 
    161         else: 
    162             lineStyle = Qt.DotLine 
    163  
    164         if len(self.colors) == 1: 
    165             painter.setPen(QPen(QColor(self.colors[0]), 6*fact, lineStyle)) 
    166             painter.drawLine(QPoint(startX, startY), QPoint(endX, endY)) 
    167         elif len(self.colors) == 2: 
    168             painter.setPen(QPen(QColor(self.colors[0]), 3*fact, lineStyle)) 
    169             painter.drawLine(QPoint(startX, startY-2), QPoint(endX, endY-2)) 
    170             painter.setPen(QPen(QColor(self.colors[1]), 3*fact, lineStyle)) 
    171             painter.drawLine(QPoint(startX, startY+1), QPoint(endX, endY+1)) 
    172         elif len(self.colors) == 3: 
    173             painter.setPen(QPen(QColor(self.colors[0]), 2*fact, lineStyle)) 
    174             painter.drawLine(QPoint(startX, startY-3), QPoint(endX, endY-3)) 
    175             painter.setPen(QPen(QColor(self.colors[1]), 2*fact, lineStyle)) 
    176             painter.drawLine(QPoint(startX, startY-1), QPoint(endX, endY-1)) 
    177             painter.setPen(QPen(QColor(self.colors[2]), 2*fact, lineStyle)) 
    178             painter.drawLine(QPoint(startX, startY+1), QPoint(endX, endY+1)) 
    179     """ 
    180  
    181     def moveBy(self, dx, dy): 
    182         x1 = self.startPoint().x(); y1 = self.startPoint().y() 
    183         x2 = self.endPoint().x(); y2 = self.endPoint().y() 
    184         self.setPoints(x1 + dx, y1 + dy, x2 + dx, y2 + dy) 
    185  
    186123    # set the line positions based on the position of input and output widgets 
    187124    def updateLinePos(self): 
     
    333270        self.outLines = []              # list of connected lines on output 
    334271        self.caption = widget.name 
    335         self.captionWidth = 0 
    336272        self.xPos = 0 
    337273        self.yPos = 0 
     
    576512        #painter.setBrush(QBrush(self.NoBrush)) 
    577513        #rect = painter.boundingRect(0,0,200,20,0,self.caption) 
    578         #self.captionWidth = rect.width() 
    579514        #painter.drawText(self.x()+34-rect.width()/2, self.y()+52+2, rect.width(), rect.height(), 0, self.caption) 
    580515        #painter.drawPixmap(self.x()+2+8, self.y()+2, self.image) 
  • orange/OrangeCanvas/orngSignalManager.py

    r3694 r3788  
    1919        self.handler = handler 
    2020 
    21         if type(parameters) == str: parameters = eval(parameters)   # in registry, parameters are stored as strings 
     21        if isinstance(parameters, str): parameters = eval(parameters)   # in registry, parameters are stored as strings 
    2222        # if we have the old definition of parameters then transform them 
    2323        if parameters in [0,1]: 
     
    3636        self.type = signalType 
    3737 
    38         if type(parameters) == str: parameters = eval(parameters) 
     38        if isinstance(parameters, str): parameters = eval(parameters) 
    3939        if parameters in [0,1]: # old definition of parameters 
    4040            self.default = not parameters 
     
    8989            #sys.stdout = self.stdout 
    9090            self.debugFile.close() 
     91            self.debugFile = None 
    9192        if debugMode: 
    9293            self.debugFile = open(debugFileName, "wt", 0) 
     
    102103        if self.debugFile: 
    103104            self.debugFile.close() 
     105            self.debugFile = None 
    104106        sys.stderr = self.stderr 
    105107        #sys.stdout = self.stdout 
     
    119121 
    120122        self.debugFile.write(strValue) 
    121         if type(object) == orange.ExampleTable: 
     123        if isinstance(object, orange.ExampleTable): 
    122124            name = " " + getattr(object, "name", "") 
    123125            self.debugFile.write(". Token type = ExampleTable" + name + ". len = " + str(len(object))) 
    124         elif type(object) == list: 
     126        elif isinstance(object, list): 
    125127            self.debugFile.write(". Token type = %s. Value = %s" % (str(type(object)), str(object[:10]))) 
    126128        elif object != None: 
     
    131133 
    132134    def exceptionHandler(self, type, value, tracebackInfo): 
     135        if not self.debugFile: return 
    133136        import traceback, os 
    134137        list = traceback.extract_tb(tracebackInfo, 10) 
  • orange/OrangeCanvas/orngView.py

    r3567 r3788  
    138138            self.selectedLine.updateTooltip() 
    139139 
    140     def unselecteAllWidgets(self): 
     140    def unselectAllWidgets(self): 
    141141        for item in self.selWidgets: item.setSelected(0) 
    142142        self.selWidgets = [] 
     
    146146 
    147147    # return number of items in "items" of type "type" 
    148     def findItemTypeCount(self, items, type): 
    149         count = 0 
     148    def findItemTypeCount(self, items, Type): 
     149        return sum([isinstance(item, Type) for item in items]) 
     150 
     151    # find and return first item of type Type 
     152    def findFirstItemType(self, items, Type): 
    150153        for item in items: 
    151             try: 
    152                 type.rtti(item) 
    153                 count = count+1 
    154             except TypeError: 
    155                 pass 
    156         return count 
    157  
    158     # find and return first item of type "type" 
    159     def findFirstItemType(self, items, type): 
    160         for item in items: 
    161             try: 
    162                 type.rtti(item) 
     154            if isinstance(item, Type): 
    163155                return item 
    164             except TypeError: 
    165                 pass 
    166156        return None 
    167157 
    168158    # find and return all items of type "type" 
    169     def findAllItemType(self, items, type): 
     159    def findAllItemType(self, items, Type): 
    170160        ret = [] 
    171161        for item in items: 
    172             try: 
    173                 type.rtti(item) 
     162            if isinstance(item, Type): 
    174163                ret.append(item) 
    175             except TypeError: 
    176                 pass 
    177164        return ret 
    178165 
     
    201188            self.tempRect = None 
    202189            self.bMultipleSelection = True 
    203             self.unselecteAllWidgets() 
     190            self.unselectAllWidgets() 
    204191 
    205192        # we clicked on a widget or on a line 
     
    220207                        self.tempLine.setPoints(pos.x(), pos.y(), pos.x(), pos.y()) 
    221208                        self.tempLine.show() 
    222                         self.unselecteAllWidgets() 
     209                        self.unselectAllWidgets() 
    223210                        self.canvas().update() 
    224211 
     
    228215 
    229216                    if widget not in self.selWidgets and self.doc.canvasDlg.ctrlPressed == 0: 
    230                         self.unselecteAllWidgets() 
     217                        self.unselectAllWidgets() 
    231218                        self.selWidgets = [widget] 
    232219                        self.bMultipleSelection = False 
     
    250237                    self.widgetPopup.popup(ev.globalPos()) 
    251238                else: 
    252                     self.unselecteAllWidgets() 
     239                    self.unselectAllWidgets() 
    253240 
    254241            # if we right clicked on a line we show a popup menu 
     
    256243                if ev.button() == QMouseEvent.RightButton: 
    257244                    self.bMultipleSelection = False 
    258                     self.unselecteAllWidgets() 
     245                    self.unselectAllWidgets() 
    259246                    self.selectedLine = line 
    260247                    self.linePopup.setItemChecked(self.menupopupLinkEnabledID, self.selectedLine.getEnabled()) 
     
    266253 
    267254            else: 
    268                 self.unselecteAllWidgets() 
     255                self.unselectAllWidgets() 
    269256 
    270257        self.doc.canvas.update() 
  • orange/OrangeWidgets/OWFreeVizOptimization.py

    r3700 r3788  
    170170        self.subsetdata = subsetdata 
    171171 
    172     def destroy(self, dw = 1, dsw = 1): 
    173         self.saveSettings() 
    174  
    175172    def setStatusBarText(self, text): 
    176173        self.statusBar.message(text) 
  • orange/OrangeWidgets/OWGUI.py

    r3782 r3788  
    909909            master.connect(control, SIGNAL(signal), cback) 
    910910        cback.opposite = cfront 
    911         if value and cfront: 
     911        if value and cfront and hasattr(master, "controlledAttributes"): 
    912912            master.controlledAttributes[value] = cfront 
    913913    return cback 
     
    932932        self.attribute = attribute 
    933933        self.f = f 
     934        if not hasattr(widget, "callbackDeposit"): 
     935            widget.callbackDeposit = [] 
    934936        widget.callbackDeposit.append(self) 
    935937        self.disabled = False 
  • orange/OrangeWidgets/OWkNNOptimization.py

    r3700 r3788  
    1414                    "useExampleWeighting", "projOptimizationMethod", "attrSubsetSelection", "optimizationType", "attributeCount", 
    1515                    "locOptOptimizeProjectionByPermutingAttributes", "timeLimit", "projectionLimit", "storeEachPermutation", 
    16                     "boxLocalOptimization", "boxStopOptimization"] 
     16                    "boxLocalOptimization", "boxStopOptimization", "clearPreviousProjections"] 
    1717    resultsListLenNums = [ 10, 100 ,  500 ,  1000 ,  5000 ,  10000, 20000, 50000, 100000, 500000 ] 
    1818    percentDataNums = [ 5 ,  10 ,  15 ,  20 ,  30 ,  40 ,  50 ,  60 ,  70 ,  80 ,  90 ,  100 ] 
     
    4545        self.optimizeBestProjection = 0                     # do we want to try to locally improve the best projections 
    4646        self.optimizeBestProjectionTime = 10                 # how many minutes do we want to try to locally optimize the best projections 
     47        self.clearPreviousProjections = 1 
    4748 
    4849        self.boxLocalOptimization = 1 
     
    124125        if visualizationMethod != SCATTERPLOT: 
    125126            OWGUI.checkBox(self.optimizationSettingsBox, self, 'storeEachPermutation', 'Save all projections for each permutation of attributes', tooltip = "Do you want to see in the projection list all evaluated projections or only the best projection for each attribute permutation.\nUsually this value is unchecked.") 
     127        OWGUI.checkBox(self.optimizationSettingsBox, self, 'clearPreviousProjections', 'Remove previously evaluated projections', tooltip = "Do you want to continue evaluation where it was stopped or do \nyou want to evaluate them from the start (by clearing the list of evaluated projections)?") 
    126128 
    127129        if visualizationMethod == LINEAR_PROJECTION: 
     
    730732    def identifyOutliers(self): 
    731733        if not self.identifyOutliersDlg: 
    732             self.identifyOutliersDlg = OWGraphIdentifyOutliers(self, signalManager = self.signalManager, widget = self.parentWidget, graph = self.graph) 
     734            self.identifyOutliersDlg = OWGraphIdentifyOutliers(self, signalManager = self.signalManager, widget = self.parentWidget) 
    733735        self.identifyOutliersDlg.show() 
    734736        self.identifyOutliersDlg.setData(self.results, self.data, VIZRANK_POINT) 
     
    775777    def evaluateProjections(self): 
    776778        if str(self.startOptimizationButton.text()) == "Start Evaluating Projections": 
     779            if self.attributeCount >= 10 and self.projOptimizationMethod == 0 and self.visualizationMethod != SCATTERPLOT and self.attrSubsetSelection != GAMMA_SINGLE and QMessageBox.critical(self, 'VizRank', 'You chose to evaluate projections with a high number of attributes. Since VizRank has to evaluate different placements\nof these attributes there will be a high number of projections to evaluate. Do you still want to proceed?','Continue','Cancel', '', 0,1): 
     780                return 
     781            if not self.data.domain.classVar or not self.data.domain.classVar.varType == orange.VarTypes.Discrete: 
     782                QMessageBox.information( None, self.parentName, "Projections can be evaluated only for data with a discrete class.", QMessageBox.Ok + QMessageBox.Default) 
     783                return 
     784            self.startOptimizationButton.setText("Stop Evaluation") 
     785            self.parentWidget.progressBarInit() 
     786            self.disableControls() 
     787 
    777788            try: 
    778                 if self.attributeCount >= 10 and self.projOptimizationMethod == 0 and self.visualizationMethod != SCATTERPLOT and self.attrSubsetSelection != GAMMA_SINGLE and QMessageBox.critical(self, 'VizRank', 'You chose to evaluate projections with a high number of attributes. Since VizRank has to evaluate different placements\nof these attributes there will be a high number of projections to evaluate. Do you still want to proceed?','Continue','Cancel', '', 0,1): 
    779                     return 
    780                 if not self.data.domain.classVar or not self.data.domain.classVar.varType == orange.VarTypes.Discrete: 
    781                     QMessageBox.information( None, self.parentName, "Projections can be evaluated only for data with a discrete class.", QMessageBox.Ok + QMessageBox.Default) 
    782                     return 
    783                 self.startOptimizationButton.setText("Stop Evaluation") 
    784  
    785                 self.parentWidget.progressBarInit() 
    786                 self.disableControls() 
    787                 evaluatedProjections = VizRank.evaluateProjections(self) 
    788                 self.enableControls() 
    789                 self.parentWidget.progressBarFinished() 
    790  
    791                 secs = time.time() - self.startTime 
    792                 self.setStatusBarText("Finished evaluation (evaluated %s projections in %d min, %d sec)" % (orngVisFuncts.createStringFromNumber(evaluatedProjections), secs/60, secs%60)) 
    793                 self.finishedAddingResults() 
    794                 qApp.processEvents() 
    795                 if self.parentWidget: 
    796                     self.parentWidget.showSelectedAttributes() 
     789                evaluatedProjections = VizRank.evaluateProjections(self, self.clearPreviousProjections) 
    797790            except: 
     791                evaluatedProjections = 0 
    798792                type, val, traceback = sys.exc_info() 
    799793                sys.excepthook(type, val, traceback)  # print the exception 
    800             self.startOptimizationButton.setText("Start Evaluating Projections") 
    801         else: 
    802             self.cancelEvaluation = 1 
    803             self.cancelOptimization = 1 
    804  
    805  
    806     def optimizeBestProjections(self, restartWhenImproved = 1): 
    807         try: 
    808             self.startOptimizationButton.setText("Stop Optimization") 
    809  
    810             self.disableControls() 
    811             evaluatedProjections = VizRank.optimizeBestProjections(self, restartWhenImproved) 
     794 
    812795            self.enableControls() 
     796            self.parentWidget.progressBarFinished() 
    813797 
    814798            secs = time.time() - self.startTime 
     
    818802            if self.parentWidget: 
    819803                self.parentWidget.showSelectedAttributes() 
     804            self.startOptimizationButton.setText("Start Evaluating Projections") 
     805        else: 
     806            self.cancelEvaluation = 1 
     807            self.cancelOptimization = 1 
     808 
     809 
     810    def optimizeBestProjections(self, restartWhenImproved = 1): 
     811        self.startOptimizationButton.setText("Stop Optimization") 
     812        self.disableControls() 
     813        try: 
     814            evaluatedProjections = VizRank.optimizeBestProjections(self, restartWhenImproved) 
    820815        except: 
     816            evaluatedProjections = 0 
    821817            type, val, traceback = sys.exc_info() 
    822818            sys.excepthook(type, val, traceback)  # print the exception 
     819 
     820        self.enableControls() 
     821        secs = time.time() - self.startTime 
     822        self.setStatusBarText("Finished evaluation (evaluated %s projections in %d min, %d sec)" % (orngVisFuncts.createStringFromNumber(evaluatedProjections), secs/60, secs%60)) 
     823        self.finishedAddingResults() 
     824        qApp.processEvents() 
     825        if self.parentWidget: 
     826            self.parentWidget.showSelectedAttributes() 
    823827        self.startOptimizationButton.setText("Start Evaluating Projections") 
    824828 
     
    835839        if self.useProjectionLimit: stop = stop or self.optimizedProjectionsCount >= self.projectionLimit 
    836840        return stop 
    837  
    838     def destroy(self, dw = 1, dsw = 1): 
    839         self.saveSettings() 
    840         OWBaseWidget.destroy(self, dw, dsw) 
    841  
    842841 
    843842    # ###################################################### 
     
    904903##        qApp.processEvents() 
    905904 
    906  
    907 VIZRANK_POINT = 0 
    908 CLUSTER_POINT = 1 
    909 VIZRANK_MOSAIC = 2 
    910905 
    911906# ############################################################################# 
     
    12321227        self.colorAttributes = 1 
    12331228        self.progressLines = 1 
     1229        self.geneToSet = None 
    12341230        self.useGeneSets = 0 
    12351231        self.recentGeneSets = [] 
     
    15471543# ############################################################################# 
    15481544# draw a graph for all the evaluated projections that shows how is the classification accuracy falling when we are moving from the best to the worst evaluated projections 
    1549 class OWGraphIdentifyOutliers(OWWidget): 
     1545class OWGraphIdentifyOutliers(VizRankOutliers, OWWidget): 
    15501546    settingsList = ["projectionCountList", "showLegend", "showAllClasses", "sortProjections", "showClickedProjection"] 
    1551     def __init__(self,parent=None, signalManager = None, widget = None, graph = None): 
    1552         OWWidget.__init__(self, parent, signalManager, "Outlier Identification", wantGraph = 1, wantStatusBar = 1, savePosition = True) 
     1547    def __init__(self,vizrank=None, signalManager = None, widget = None): 
     1548        OWWidget.__init__(self, vizrank, signalManager, "Outlier Identification", wantGraph = 1, wantStatusBar = 1, savePosition = True) 
     1549        VizRankOutliers.__init__(self, vizrank) 
    15531550 
    15541551        self.projectionCountList = ["5", "10", "20", "50", "100", "200", "500", "1000", "2000", "5000", "10000", "Other..."] 
     
    15611558        self.showClickedProjection = 1 
    15621559 
    1563         self.projectionIndices = [] 
    1564         self.matrixOfPredictions = None 
    1565         self.projectionGraph = graph 
    1566         self.parent = parent 
    15671560        self.widget = widget 
    1568         self.graphMatrix = None 
    1569         self.results = None 
    1570         self.data = None 
    1571         self.dialogType = -1 
    1572         self.evaluatedExamples = [] 
    15731561 
    15741562        self.loadSettings() 
     
    16231611 
    16241612    def setData(self, results, data, dialogType): 
    1625         self.results = results 
    1626         self.data = data 
    1627         self.dialogType = dialogType 
    1628         self.matrixOfPredictions = None 
    1629  
    1630         if dialogType == VIZRANK_POINT: 
    1631             self.ATTR_LIST = ATTR_LIST 
    1632             self.ACCURACY = ACCURACY 
    1633         elif dialogType == VIZRANK_MOSAIC: 
    1634             import orngMosaic 
    1635             self.ATTR_LIST = orngMosaic.ATTR_LIST 
    1636             self.ACCURACY = orngMosaic.SCORE 
     1613        VizRankOutliers.setData(self, results, data, dialogType) 
    16371614 
    16381615        # example index combo 
     
    16671644        self.selectedExampleChanged() 
    16681645 
    1669  
    16701646    def evaluateProjections(self): 
    16711647        if not self.results or not self.data: return 
    16721648 
    1673         # compute predictions 
    16741649        self.widget.progressBarInit() 
    1675  
    1676         projCount = min(int(self.projectionCount), len(self.results)) 
    1677         classCount = len(self.data.domain.classVar.values) 
    1678         existing = 0 
    1679         if self.matrixOfPredictions != None: 
    1680             existing = numpy.shape(self.matrixOfPredictions)[0]/classCount 
    1681             if existing < projCount: 
    1682                 self.matrixOfPredictions = numpy.resize(self.matrixOfPredictions, (projCount*classCount, len(self.data))) 
    1683             elif existing > projCount: 
    1684                 self.matrixOfPredictions = self.matrixOfPredictions[0:classCount*projCount,:] 
    1685         else: 
    1686             self.matrixOfPredictions = -1 * numpy.ones((projCount*classCount, len(self.data)), numpy.float) 
    1687  
    1688  
    1689         # compute the matrix of predictions 
    1690         results = self.results[existing:min(len(self.results),projCount)] 
    1691         index = 0 
    1692  
    16931650        self.widgetStatusArea.show() 
    1694         for result in results: 
    1695             if self.dialogType == VIZRANK_POINT: 
    1696                 acc, other, tableLen, attrList, tryIndex, generalDict = result 
    1697                 attrIndices = [self.projectionGraph.attributeNameIndex[attr] for attr in attrList] 
    1698                 validDataIndices = self.projectionGraph.getValidIndices(attrIndices) 
    1699                 table = self.projectionGraph.createProjectionAsExampleTable(attrIndices, settingsDict = generalDict)    # TO DO: this does not work with polyviz!!! 
    1700                 qApp.processEvents()        # allow processing of other events 
    1701                 acc, probabilities = self.parent.kNNClassifyData(table) 
    1702  
    1703             elif self.dialogType == VIZRANK_MOSAIC: 
    1704                 from orngCI import FeatureByCartesianProduct 
    1705                 acc, attrList, tryIndex, other = result 
    1706                 probabilities = numpy.zeros((len(self.data), len(self.data.domain.classVar.values)), numpy.float) 
    1707                 newFeature, quality = FeatureByCartesianProduct(self.data, attrList) 
    1708                 dist = orange.ContingencyAttrClass(newFeature, self.data) 
    1709                 data = self.data.select([newFeature, self.data.domain.classVar])     # create a dataset that has only this new feature and class info 
    1710                 clsVals = len(self.data.domain.classVar.values) 
    1711                 validDataIndices = range(len(data)) 
    1712                 for i, ex in enumerate(data): 
    1713                     try: 
    1714                         prob = dist[ex[0]] 
    1715                         for j in range(clsVals): 
    1716                             probabilities[i][j] = prob[j] / float(sum(prob.values())) 
    1717                     except: 
    1718                         validDataIndices.remove(i) 
    1719  
    1720  
    1721             #self.matrixOfPredictions[(existing + index)*classCount:(existing + index +1)*classCount] = numpy.transpose(probabilities) 
    1722             probabilities = numpy.transpose(probabilities) 
    1723             for i in range(classCount): 
    1724                 numpy.put(self.matrixOfPredictions[(existing + index)*classCount + i], validDataIndices, probabilities[i]) 
    1725  
    1726             index += 1 
    1727             self.setStatusBarText("Evaluated %s/%s projections..." % (orngVisFuncts.createStringFromNumber(existing + index), orngVisFuncts.createStringFromNumber(projCount))) 
    1728             self.widget.progressBarSet(100.0*(index)/float(projCount-existing)) 
    1729  
    1730         # update the list box with probabilities for all examples 
    17311651        self.exampleList.clear() 
    1732         projCount = min(int(self.projectionCount), len(self.results)) 
    1733         classCount = len(self.data.domain.classVar.values) 
    1734  
    1735         for i in range(len(self.data)): 
    1736             matrix = numpy.transpose(numpy.reshape(self.matrixOfPredictions[:, i], (projCount, classCount))) 
    1737             valid = numpy.where(matrix[int(self.data[i].getclass())] != -1, 1, 0) 
    1738             data = numpy.compress(valid, matrix[int(self.data[i].getclass())]) 
    1739             if len(data): ave_acc = numpy.sum(data) / float(len(data)) 
    1740             else:         ave_acc = 0 
    1741             self.insertItemToExampleList(ave_acc, i) 
     1652 
     1653        VizRankOutliers.evaluateProjections(self, qApp) 
     1654 
     1655        for i, (prob, exIndex, classPredictions) in enumerate(self.evaluatedExamples): 
     1656            self.exampleList.insertItem("%.2f - %d" % (prob, exIndex), i) 
    17421657 
    17431658        self.widget.progressBarFinished() 
    17441659        self.widgetStatusArea.hide() 
    1745  
    17461660 
    17471661    def toggleShowPredictions(self): 
     
    17611675                corrClass = int(self.data[i].getclass()) 
    17621676                predictions = self.matrixOfPredictions[corrClass::classCount,i] 
    1763                 predictions = numpy.compress(predictions != -1, predictions) 
     1677                predictions = numpy.compress(predictions != -100, predictions) 
    17641678                predictions = predictions**3 
    17651679                if len(predictions):    # prevent division by zero! 
     
    17851699 
    17861700        if self.dialogType == VIZRANK_POINT: 
    1787             valid = self.projectionGraph.getValidList([self.projectionGraph.attributeNameIndex[attr] for attr in self.widget.getShownAttributeList()]) 
     1701            valid = self.widgetGraph.getValidList([self.widgetGraph.attributeNameIndex[attr] for attr in self.widget.getShownAttributeList()]) 
    17881702            insideColors = numpy.zeros(len(self.data)) 
    17891703            insideColors[self.selectedExampleIndex] = 1 
     
    17971711 
    17981712        if self.parent.parentName == "Polyviz": 
    1799             selected, unselected = self.projectionGraph.getSelectionsAsIndices(self.widget.getShownAttributeList(), self.widget.attributeReverse) 
    1800         else: 
    1801             selected, unselected = self.projectionGraph.getSelectionsAsIndices(self.widget.getShownAttributeList()) 
     1713            selected, unselected = self.widgetGraph.getSelectionsAsIndices(self.widget.getShownAttributeList(), self.widget.attributeReverse) 
     1714        else: 
     1715            selected, unselected = self.widgetGraph.getSelectionsAsIndices(self.widget.getShownAttributeList()) 
    18021716 
    18031717        if len(selected) != 1: 
     
    18071721        self.selectedExampleChanged() 
    18081722 
    1809  
    1810     # insert new result - give parameters: accuracy of projection, number of examples in projection and list of attributes. 
    1811     def insertItemToExampleList(self, val, exampleIndex): 
    1812         top = 0; bottom = len(self.evaluatedExamples) 
    1813         index = 0 
    1814  
    1815         while (bottom-top) > 1: 
    1816             mid  = (bottom + top)/2 
    1817             if min(val, self.evaluatedExamples[mid][0]) == val: bottom = mid 
    1818             else: top = mid 
    1819  
    1820         if len(self.evaluatedExamples) == 0: index = 0 
    1821         elif min(val, self.evaluatedExamples[top][0]) == val: 
    1822             index = top 
    1823         else: 
    1824             index = bottom 
    1825  
    1826         self.evaluatedExamples.insert(index, (val, exampleIndex)) 
    1827         self.exampleList.insertItem("%.2f - %d" % (val, exampleIndex), index) 
    1828  
    1829  
     1723    
    18301724    def exampleListSelectionChanged(self): 
    1831         (val, exampleIndex) = self.evaluatedExamples[self.exampleList.currentItem()] 
     1725        (val, exampleIndex, classPredictions) = self.evaluatedExamples[self.exampleList.currentItem()] 
    18321726        self.selectedExampleIndex = exampleIndex 
    18331727        self.selectedExampleChanged() 
     
    18471741        self.graph.setAxisScale(QwtPlot.xBottom, 0, 1, 0.2) 
    18481742 
    1849         valid = numpy.where(self.graphMatrix[0] != -1, 1, 0) 
     1743        valid = numpy.where(self.graphMatrix[0] != -100, 1, 0) 
    18501744        allValid = numpy.sum(valid) == len(valid) 
    18511745        nrOfClasses = len(self.data.domain.classVar.values) 
     
    18611755 
    18621756        self.projectionIndices = [val[1] for val in indices] 
    1863         classVariableValues = getVariableValuesSorted(self.parent.data, self.parent.data.domain.classVar.name) 
     1757        classVariableValues = getVariableValuesSorted(self.data, self.data.domain.classVar.name) 
    18641758        classColors = ColorPaletteHSV(len(classVariableValues)) 
    18651759 
     
    18671761            x = 0 
    18681762            s = "Predicted class probabilities:<br>" 
     1763            invalidValue = 0 
    18691764            for j in classes: 
    18701765                (prob, index) = indices[i] 
     1766                if self.graphMatrix[j][index] < 0: 
     1767                    invalidValue = 1 
     1768                    continue 
    18711769                s += "&nbsp; &nbsp; &nbsp; %s: %.2f%%<br>" % (classVariableValues[j], 100*self.graphMatrix[j][index]) 
    18721770                if not self.showAllClasses and int(self.data[self.selectedExampleIndex].getclass()) != j: 
     
    18751773                self.graph.insertCurve(RectangleCurve(self.graph, QPen(classColors.getColor(j)), QBrush(classColors.getColor(j)), [x, x+xDiff, x+xDiff, x], [i, i, i+1, i+1])) 
    18761774                x += xDiff 
    1877             self.graph.tips.addToolTip(0, i, s[:-4], 1, 1) 
     1775            if not invalidValue: 
     1776                self.graph.tips.addToolTip(0, i, s[:-4], 1, 1) 
    18781777 
    18791778        if self.showLegend: 
    1880             self.graph.addCurve("<b>" + self.parent.data.domain.classVar.name + ":</b>", QColor(0,0,0), QColor(0,0,0), 0, symbol = QwtSymbol.None, enableLegend = 1) 
     1779            self.graph.addCurve("<b>" + self.data.domain.classVar.name + ":</b>", QColor(0,0,0), QColor(0,0,0), 0, symbol = QwtSymbol.None, enableLegend = 1) 
    18811780            for i,val in enumerate(classVariableValues): 
    18821781                self.graph.addCurve(val, classColors[i], classColors[i], 15, symbol = QwtSymbol.Rect, enableLegend = 1) 
     
    19061805            if y >= len(self.projectionIndices): return 
    19071806            projIndex = self.projectionIndices[y] 
    1908             self.parent.resultList.setSelected(projIndex, 1) 
     1807            self.vizrank.resultList.setSelected(projIndex, 1) 
    19091808 
    19101809            if self.dialogType == VIZRANK_POINT: 
    19111810                attrs = self.parent.shownResults[projIndex][self.ATTR_LIST] 
    1912                 valid = self.projectionGraph.getValidList([self.projectionGraph.attributeNameIndex[attr] for attr in attrs]) 
     1811                valid = self.widgetGraph.getValidList([self.widgetGraph.attributeNameIndex[attr] for attr in attrs]) 
    19131812                insideColors = numpy.zeros(len(self.data)) 
    19141813                insideColors[self.selectedExampleIndex] = 1 
  • orange/OrangeWidgets/Visualize/OWLinProj.py

    r3722 r3788  
    479479        return c 
    480480 
     481    def saveSettings(self): 
     482        OWWidget.saveSettings(self) 
     483        self.vizrank.saveSettings() 
     484        self.freeVizDlg.saveSettings() 
     485 
    481486    def destroy(self, dw = 1, dsw = 1): 
    482487##        self.clusterDlg.hide() 
  • orange/OrangeWidgets/Visualize/OWMosaicDisplay.py

    r3701 r3788  
    8686                    "showSubsetDataBoxes", "removeUnusedValues"] 
    8787 
    88     contextHandlers = {"": DomainContextHandler("", ["manualAttributeValuesDict"], loadImperfect = 0)} 
     88    contextHandlers = {"": DomainContextHandler("", ["attr1", "attr2", "attr3", "attr4", "manualAttributeValuesDict"], loadImperfect = 0)} 
    8989 
    9090    def __init__(self,parent=None, signalManager = None): 
     
    387387        if args.get("erasePrevious", 1): 
    388388            for item in self.canvas.allItems(): 
    389                 if type(item) != SelectionRectangle: 
     389                if not isinstance(item, SelectionRectangle): 
    390390                    item.setCanvas(None)    # remove all canvas items, except SelectionCurves 
    391391            for (rect, tip) in self.tooltips: 
     
    482482 
    483483    ##  DRAW DATA - draw rectangles for attributes in attrList inside rect (x0,x1), (y0,y1) 
    484     def DrawData(self, attrList, (x0, x1), (y0, y1), side, condition, totalAttrs, lastValueForFirstAttribute = 0, usedAttrs = [], usedVals = [], attrVals = "", **args): 
     484    def DrawData(self, attrList, (x0, x1), (y0, y1), side, condition, totalAttrs, usedAttrs = [], usedVals = [], attrVals = "", **args): 
    485485        if self.conditionalDict[attrVals] == 0: 
    486             self.addRect(x0, x1, y0, y1, attrVals = attrVals) 
    487             self.DrawText(side, attrList[0], (x0, x1), (y0, y1), totalAttrs, lastValueForFirstAttribute, attrVals)  # store coordinates for later drawing of labels 
     486            self.addRect(x0, x1, y0, y1, "", usedAttrs, usedVals, attrVals = attrVals) 
     487            self.DrawText(side, attrList[0], (x0, x1), (y0, y1), totalAttrs, usedAttrs, usedVals, attrVals)  # store coordinates for later drawing of labels 
    488488            return 
    489489 
     
    500500            if whole == 0: edge = (y1-y0)/float(len(values)-1) 
    501501 
    502         currPos = 0.0 
    503502        if attrVals == "": counts = [self.conditionalDict[val] for val in values] 
    504503        else:              counts = [self.conditionalDict[attrVals + "-" + val] for val in values] 
    505504        total = sum(counts) 
    506505 
    507         for i in range(len(counts)): 
     506        # if we are visualizing the third attribute and the first attribute has the last value, we have to reverse the order in which the boxes will be drawn 
     507        # otherwise, if the last cell, nearest to the labels of the fourth attribute, is empty, we wouldn't be able to position the labels 
     508        valRange = range(len(values)) 
     509        if len(attrList + usedAttrs) == 4 and len(usedAttrs) == 2: 
     510            attr1Values = self.attributeValuesDict.get(usedAttrs[0], None) or getVariableValuesSorted(self.data, usedAttrs[0]) 
     511            if usedVals[0] == attr1Values[-1]: 
     512                valRange = valRange[::-1] 
     513 
     514        for i in valRange: 
     515            start = i*edge + whole * float(sum(counts[:i])/float(total)) 
     516            end   = i*edge + whole * float(sum(counts[:i+1])/float(total)) 
    508517            val = values[i] 
    509             size = whole*float(counts[i])/float(total) 
    510518            htmlVal = getHtmlCompatibleString(val) 
    511519            if attrVals != "": newAttrVals = attrVals + "-" + val 
    512520            else:              newAttrVals = val 
    513521 
    514             if side % 2 == 0:   # if drawing horizontal 
    515                 if len(attrList) == 1:  self.addRect(x0+currPos, x0+currPos+size, y0, y1, condition + 4*"&nbsp;" + attr + ": <b>" + htmlVal + "</b><br>", usedAttrs + [attr], usedVals + [val], newAttrVals, **args) 
    516                 else:                   self.DrawData(attrList[1:], (x0+currPos, x0+currPos+size), (y0, y1), side +1, condition + 4*"&nbsp;" + attr + ": <b>" + htmlVal + "</b><br>", totalAttrs, lastValueForFirstAttribute + int(val == values[-1]), usedAttrs + [attr], usedVals + [val], newAttrVals, **args) 
     522            if side % 2 == 0:   # if we are moving horizontally 
     523                if len(attrList) == 1:  self.addRect(x0+start, x0+end, y0, y1, condition + 4*"&nbsp;" + attr + ": <b>" + htmlVal + "</b><br>", usedAttrs + [attr], usedVals + [val], newAttrVals, **args) 
     524                else:                   self.DrawData(attrList[1:], (x0+start, x0+end), (y0, y1), side +1, condition + 4*"&nbsp;" + attr + ": <b>" + htmlVal + "</b><br>", totalAttrs, usedAttrs + [attr], usedVals + [val], newAttrVals, **args) 
    517525            else: 
    518                 if len(attrList) == 1:  self.addRect(x0, x1, y0+currPos, y0+currPos+size, condition + 4*"&nbsp;" + attr + ": <b> " + htmlVal + "</b><br>", usedAttrs + [attr], usedVals + [val], newAttrVals, **args) 
    519                 else:                   self.DrawData(attrList[1:], (x0, x1), (y0+currPos, y0+currPos+size), side +1, condition + 4*"&nbsp;" + attr + ": <b>" + htmlVal + "</b><br>", totalAttrs, lastValueForFirstAttribute, usedAttrs + [attr], usedVals + [val], newAttrVals, **args) 
    520             currPos += size + edge 
    521  
    522         self.DrawText(side, attrList[0], (x0, x1), (y0, y1), totalAttrs, lastValueForFirstAttribute, attrVals) 
     526                if len(attrList) == 1:  self.addRect(x0, x1, y0+start, y0+end, condition + 4*"&nbsp;" + attr + ": <b> " + htmlVal + "</b><br>", usedAttrs + [attr], usedVals + [val], newAttrVals, **args) 
     527                else:                   self.DrawData(attrList[1:], (x0, x1), (y0+start, y0+end), side +1, condition + 4*"&nbsp;" + attr + ": <b>" + htmlVal + "</b><br>", totalAttrs, usedAttrs + [attr], usedVals + [val], newAttrVals, **args) 
     528 
     529        self.DrawText(side, attrList[0], (x0, x1), (y0, y1), totalAttrs, usedAttrs, usedVals, attrVals) 
    523530 
    524531 
    525532    ###################################################################### 
    526533    ## DRAW TEXT - draw legend for all attributes in attrList and their possible values 
    527     def DrawText(self, side, attr, (x0, x1), (y0, y1), totalAttrs, lastValueForFirstAttribute, attrVals): 
     534    def DrawText(self, side, attr, (x0, x1), (y0, y1), totalAttrs, usedAttrs, usedVals, attrVals): 
    528535        if self.drawnSides[side]: return 
    529         #if side == RIGHT and lastValueForFirstAttribute != 2: return 
     536 
     537        # the text on the right will be drawn when we are processing visualization of the last value of the first attribute 
    530538        if side == RIGHT: 
    531             if lastValueForFirstAttribute != 2: return 
    532 ##            elif not self.conditionalDict[attrVals]: 
    533 ##                self.conditionalDict[attrVals] = [1 for i in range(len(getVariableValuesSorted(self.data, attr)))] 
     539            attr1Values = self.attributeValuesDict.get(usedAttrs[0], None) or getVariableValuesSorted(self.data, usedAttrs[0]) 
     540            if usedVals[0] != attr1Values[-1]: 
     541                return 
    534542 
    535543        if not self.conditionalDict[attrVals]: 
     
    586594 
    587595        rect = OWCanvasRectangle(self.canvas, x0, y0, x1-x0, y1-y0, z = 30) 
    588  
    589         # we have to remember which conditions were new in this update so that when we right click we can only remove the last added selections 
    590         if isinstance(self.selectionRectangle, QRect) and rect in self.canvas.collisions(self.selectionRectangle) and tuple(usedVals) not in self.selectionConditions: 
    591             self.recentlyAdded = getattr(self, "recentlyAdded", []) + [tuple(usedVals)] 
    592             self.selectionConditions = self.selectionConditions + [tuple(usedVals)] 
    593  
    594         # show rectangle selected or not 
    595         if tuple(usedVals) in self.selectionConditions: 
    596             rect.setPen(QPen(Qt.black, 3, Qt.DotLine)) 
    597596 
    598597        # if we have selected a rule that contains this combination of attr values then show a kind of selection of this rectangle 
     
    606605                    r.setPen(QPen(self.colorPalette[counts.index(max(counts))], 2, Qt.DashLine)) 
    607606 
    608  
    609607        if not self.conditionalDict[attrVals]: return rect 
     608 
     609        # we have to remember which conditions were new in this update so that when we right click we can only remove the last added selections 
     610        if isinstance(self.selectionRectangle, QRect) and rect in self.canvas.collisions(self.selectionRectangle) and tuple(usedVals) not in self.selectionConditions: 
     611            self.recentlyAdded = getattr(self, "recentlyAdded", []) + [tuple(usedVals)] 
     612            self.selectionConditions = self.selectionConditions + [tuple(usedVals)] 
     613 
     614        # show rectangle selected or not 
     615        if tuple(usedVals) in self.selectionConditions: 
     616            rect.setPen(QPen(Qt.black, 3, Qt.DotLine)) 
     617 
    610618        if self.interiorColoring == CLASS_DISTRIBUTION and (not self.data.domain.classVar or not self.data.domain.classVar.varType == orange.VarTypes.Discrete): 
    611619            return rect 
     
    862870            return selectedIndices 
    863871 
    864  
     872    def saveSettings(self): 
     873        OWWidget.saveSettings(self) 
     874        self.optimizationDlg.saveSettings() 
     875         
    865876 
    866877 
     
    920931    a.setMainWidget(ow) 
    921932    ow.show() 
    922     data = orange.ExampleTable(r"e:\Development\Python23\Lib\site-packages\Orange\Datasets\UCI\imports-85.tab") 
    923     ow.setData(data) 
     933    for d in ["zoo.tab", "iris.tab", "zoo.tab"]: 
     934        data = orange.ExampleTable(r"e:\Development\Orange Datasets\UCI\\" + d) 
     935        ow.setData(data) 
     936        ow.handleNewSignals() 
    924937    a.exec_loop() 
  • orange/OrangeWidgets/Visualize/OWParallelCoordinates.py

    r3725 r3788  
    370370        return c 
    371371 
     372    def saveSettings(self): 
     373        OWWidget.saveSettings(self) 
     374        self.optimizationDlg.saveSettings() 
     375 
    372376    def destroy(self, dw = 1, dsw = 1): 
    373377        self.optimizationDlg.hide() 
     
    494498        self.vizrankSettingsBox.setEnabled(self.optimizationMeasure) 
    495499 
    496     def destroy(self, dw = 1, dsw = 1): 
    497         self.saveSettings() 
    498  
    499500    # if user clicks new attribute list in optimization dialog, we update shown attributes 
    500501    def showSelectedAttributes(self): 
  • orange/OrangeWidgets/Visualize/OWScatterPlot.py

    r3700 r3788  
    436436    # SCATTERPLOT SETTINGS 
    437437    # ############################################################################################################################################################## 
     438    def saveSettings(self): 
     439        OWWidget.saveSettings(self) 
     440        self.vizrank.saveSettings() 
    438441 
    439442    #update status on progress bar - gets called by OWScatterplotGraph 
  • orange/orngLinProj.py

    r3698 r3788  
    487487            attributes = self.getShownAttributeList() 
    488488            attrIndices = [ai[label] for label in attributes] 
     489        if len(attrIndices) == 0: return None 
    489490 
    490491        validData = self.graph.getValidList(attrIndices) 
     492        if sum(validData) == 0: return None 
     493         
    491494        dataMatrix = numpy.compress(validData, numpy.take(self.graph.noJitteringScaledData, attrIndices, axis = 0), axis = 1) 
    492495        hasClass = self.graph.rawdata.domain.classVar != None 
  • orange/orngMosaic.py

    r3701 r3788  
    10171017    def updateStatus(self, evaluatingProjections): 
    10181018        if self.statusFunct: 
    1019             s = "%sCurrent tree has %d nodes" % (evaluatingProjections and "Evaluating projections. " or "", self.nodeCount) 
     1019            s = "%sCurrent tree has %d nodes" % (evaluatingProjections and "Please wait, evaluating projections. " or "", self.nodeCount) 
    10201020            self.statusFunct(s) 
    10211021 
     
    10271027        self.mosaic.evaluateProjections() 
    10281028        if self.mosaic.cancelTreeBuilding or len(self.mosaic.results) == 0:       # or self.mosaic.results[0][0] <= 0:     # if no results or score <=0 then stop building 
    1029             #self.nodeCount += 1 
    10301029            self.updateStatus(0) 
    10311030            return None 
     
    10361035        dist = orange.Distribution(newFeature, gen).values() 
    10371036        if max(dist) == sum(dist):    # if all examples belong to one attribute value then this is obviously a useless attribute and we should stop building 
    1038             #self.nodeCount += 1 
    10391037            self.updateStatus(0) 
    10401038            return None 
    10411039 
    1042 ##        if len(attrList) > 1: 
    1043 ##            # remove all other attributes and examples with missing values and then try to combine different attribute values 
    1044 ##            subgen = orange.Preprocessor_dropMissing(gen.select(attrList + [gen.domain.classVar.name])) 
    1045 ##            #newFeature, quality = FeatureByIM(subgen, attrList, binary = 0, measure = orange.MeasureAttribute_info()) 
    1046 ##            newFeature, quality = FeatureByIM(subgen, attrList, binary = 0, measure = MeasureAttribute_MDL()) 
    1047 ##        else: 
    1048 ##            newFeature = gen.domain[attrList[0]] 
    1049 ##            dist = orange.Distribution(newFeature, gen).values() 
    1050 ##            if max(dist) == sum(dist):    # if all examples belong to one attribute value then this is obviously a useless attribute and we should stop building 
    1051 ##                return None 
    1052 ##            newFeature.getValueFrom = orange.ClassifierByLookupTable(newFeature, newFeature, list(newFeature.values) + ["?"]) 
    10531040        self.nodeCount += 1 
    10541041        self.updateStatus(0) 
  • orange/orngVizRank.py

    r3699 r3788  
    88from orngLinProj import FreeViz 
    99 
     10# used for outlier detection 
     11VIZRANK_POINT = 0 
     12CLUSTER_POINT = 1 
     13VIZRANK_MOSAIC = 2 
     14 
    1015# quality measure 
    1116CLASS_ACCURACY = 0 
     
    5560GAMMA_SINGLE = 2 
    5661 
     62PROJOPT_NONE = 0 
     63PROJOPT_SPCA = 1 
     64PROJOPT_PLS = 2 
     65 
    5766contMeasures = [("None", None), ("ReliefF", orange.MeasureAttribute_relief(k=10, m=50)), 
    5867                ("Signal to Noise Ratio", orngVisFuncts.S2NMeasure()), ("Signal to Noise OVA", orngVisFuncts.S2NMeasureMix())] 
     
    6978LINEAR_PROJECTION = 3 
    7079POLYVIZ = 4 
     80KNN_IN_ORIGINAL_SPACE = 10 
    7181 
    7282# optimization type 
     
    8595                graph.normalizeExamples = 1 
    8696                graph.scalingByVariance = 0 
    87             elif visualizationMethod == LINEAR_PROJECTION: 
     97            elif visualizationMethod in [LINEAR_PROJECTION, KNN_IN_ORIGINAL_SPACE]: 
    8898                import orngScaleLinProjData 
    8999                graph = orngScaleLinProjData.orngScaleLinProjData() 
     
    120130        self.attrDisc = DISC_MEAS_RELIEFF 
    121131        self.attrSubsetSelection = GAMMA_ALL                # how do we find attribute subsets to evaluate - deterministic according to attribute ranking score or using gamma distribution - if using gamma, do we want to evaluate all possible permutations of attributes or only one 
    122         self.projOptimizationMethod = 0                     # None, supervisedPCA, partial least square 
     132        self.projOptimizationMethod = PROJOPT_NONE          # None, supervisedPCA, partial least square 
    123133        self.useExampleWeighting = 0                        # weight examples, so that the class that has a low number of examples will have higher weights 
     134        self.evaluationData = {} 
     135        self.evaluationData["triedCombinations"] = {} 
    124136 
    125137        self.externalLearner = None                         # do we use knn or some external learner 
     
    390402 
    391403            # create a new attribute that is a cartesian product of the two visualized attributes 
    392             nattr = orange.EnumVariable(values=['i' for i in range(NUMBER_OF_INTERVALS*NUMBER_OF_INTERVALS)]) 
     404            nattr = orange.EnumVariable(values=[str(i) for i in range(NUMBER_OF_INTERVALS*NUMBER_OF_INTERVALS)]) 
    393405            nattr.getValueFrom = orange.ClassifierByLookupTable2(nattr, testTable.domain[0], testTable.domain[1]) 
    394406            for i in range(NUMBER_OF_INTERVALS*NUMBER_OF_INTERVALS): nattr.getValueFrom.lookupTable[i] = i 
     
    419431        if self.qualityMeasure == AVERAGE_CORRECT: 
    420432            for res in results.results: 
     433                if not res.probabilities[0]: continue 
    421434                prediction[res.actualClass] += res.probabilities[0][res.actualClass] 
    422435                countsByFold[res.iterationNumber] += 1 
     
    426439            #return orngStat.BrierScore(results)[0], results 
    427440            for res in results.results: 
    428                 val = 0 
    429                 for prob in res.probabilities[0]: val += prob*prob 
    430                 val = val - 2*res.probabilities[0][res.actualClass] + 1 
    431                 prediction[res.actualClass] += val 
     441                if not res.probabilities[0]: continue 
     442                prediction[res.actualClass] += sum([prob*prob for prob in res.probabilities[0]]) - 2*res.probabilities[0][res.actualClass] + 1 
    432443                countsByFold[res.iterationNumber] += 1 
    433444 
     
    601612                for i in range(minLength, maxLength+1): 
    602613                    if i > len(attributes): continue        # if we don't have enough attributes 
    603                     if self.projOptimizationMethod != 0: 
     614                    if self.projOptimizationMethod != 0 or self.visualizationMethod == KNN_IN_ORIGINAL_SPACE: 
    604615                        permutationIndices[i] = [range(i)] 
    605616                    else: 
     
    663674                attrs.sort() 
    664675                if not triedDict.has_key(tuple(attrs)) and len(attrs) == attrCount: 
    665                     #self.evaluationData["triedCombinations"][tuple(attrs)] = 1     # we don't want to save used combinations since we only test one permutation 
     676                    self.evaluationData["triedCombinations"][tuple(attrs)] = 1     # this is not the best, since we don't want to save used combinations since we only test one permutation 
    666677                    #return [filter(None, attrList)]        # problem: using filter removes value 0 from the array, which means that the attribute ranked as best wont be in the projections 
    667678                    return [attrList] 
     
    704715 
    705716            # create only one permutation, because its all we need 
    706             elif self.projOptimizationMethod != 0: 
     717            elif self.projOptimizationMethod != 0 or self.visualizationMethod == KNN_IN_ORIGINAL_SPACE: 
    707718                permutations.append(reduce(operator.add, combination)) 
    708719            else: 
     
    733744    # MAIN FUNCTION FOR EVALUATING PROJECTIONS 
    734745    # ########################################################################## 
    735     def evaluateProjections(self): 
     746    def evaluateProjections(self, clearPreviousProjections = 1): 
    736747        random.seed(0)      # always use the same seed to make results repeatable 
    737748        if not self.data: return 0 
     
    741752            self.timeLimit = 2 * 60 
    742753 
    743         self.evaluatedProjectionsCount = 0 
    744         self.optimizedProjectionsCount = 0 
    745754        self.startTime = time.time() 
    746         self.evaluationData = {}            # clear all previous data about tested permutations and stuff 
    747         self.evaluationData["triedCombinations"] = {} 
    748  
     755 
     756        if clearPreviousProjections: 
     757            self.evaluatedProjectionsCount = 0 
     758            self.optimizedProjectionsCount = 0 
     759            self.evaluationData = {}            # clear all previous data about tested permutations and stuff 
     760            self.evaluationData["triedCombinations"] = {} 
     761            self.clearResults() 
     762 
     763        self.clearArguments() 
    749764        maxFunct = self.getMaxFunct() 
    750         self.clearResults() 
    751         self.clearArguments() 
    752765 
    753766        if self.__class__.__name__ == "OWVizRank": 
     
    768781 
    769782            for i in range(len(evaluatedAttributes)): 
     783                attr1 = self.attributeNameIndex[evaluatedAttributes[i]] 
    770784                for j in range(i): 
    771                     attr1 = self.attributeNameIndex[evaluatedAttributes[j]]; attr2 = self.attributeNameIndex[evaluatedAttributes[i]] 
     785                    attr2 = self.attributeNameIndex[evaluatedAttributes[j]] 
    772786                    self.evaluatedProjectionsCount += 1 
    773787                    if self.isEvaluationCanceled(): 
     
    811825                    attrIndices = permutations[0] 
    812826 
    813                     if self.projOptimizationMethod != 0: 
    814                         xanchors, yanchors, (attrNames, newIndices) = self.freeviz.findProjection(self.projOptimizationMethod, attrIndices, setAnchors = 0, percentDataUsed = self.percentDataUsed) 
    815                         table = self.graph.createProjectionAsExampleTable(newIndices, domain = domain, XAnchors = xanchors, YAnchors = yanchors) 
     827                    # if we use SPCA, PLS or KNN_IN_ORIGINAL_SPACE 
     828                    if self.projOptimizationMethod != 0 or self.visualizationMethod == KNN_IN_ORIGINAL_SPACE: 
     829                        if self.visualizationMethod == KNN_IN_ORIGINAL_SPACE: 
     830                            table = self.data.select([self.data.domain[attr] for attr in attrIndices] + [self.data.domain.classVar] ) 
     831                            xanchors, yanchors = self.graph.createXAnchors(len(attrIndices)), self.graph.createYAnchors(len(attrIndices)) 
     832                        else: 
     833                            xanchors, yanchors, (attrNames, newIndices) = self.freeviz.findProjection(self.projOptimizationMethod, attrIndices, setAnchors = 0, percentDataUsed = self.percentDataUsed) 
     834                            table = self.graph.createProjectionAsExampleTable(newIndices, domain = domain, XAnchors = xanchors, YAnchors = yanchors) 
    816835                        if len(table) < self.minNumOfExamples: continue 
    817836                        self.evaluatedProjectionsCount += 1 
     
    11951214                self.insertItem(i, accuracy, other_results, lenTable, attrList, tryIndex, generalDict) 
    11961215                i += 1 
     1216 
     1217 
     1218# ############################################################################################################################################### 
     1219# ######           VIZRANK OUTLIERS            ############################################################################################## 
     1220# ############################################################################################################################################### 
     1221class VizRankOutliers: 
     1222    def __init__(self, vizrank): 
     1223        self.vizrank = vizrank 
     1224        if hasattr(vizrank, "graph"): 
     1225            self.widgetGraph = vizrank.graph 
     1226        else: 
     1227            self.widgetGraph = None 
     1228 
     1229        self.projectionIndices = [] 
     1230        self.matrixOfPredictions = None 
     1231        self.graphMatrix = None 
     1232        self.results = None 
     1233        self.data = None 
     1234        self.dialogType = -1 
     1235        self.evaluatedExamples = [] 
     1236 
     1237 
     1238    def setData(self, results, data, dialogType): 
     1239        self.results = results 
     1240        self.data = data 
     1241        self.dialogType = dialogType 
     1242        self.matrixOfPredictions = None 
     1243 
     1244        if dialogType == VIZRANK_POINT: 
     1245            self.ATTR_LIST = ATTR_LIST 
     1246            self.ACCURACY = ACCURACY 
     1247        elif dialogType == VIZRANK_MOSAIC: 
     1248            import orngMosaic 
     1249            self.ATTR_LIST = orngMosaic.ATTR_LIST 
     1250            self.ACCURACY = orngMosaic.SCORE 
     1251 
     1252    def evaluateProjections(self, qApp = None): 
     1253        if not self.results or not self.data: return 
     1254 
     1255        projCount = min(int(self.projectionCount), len(self.results)) 
     1256        classCount = len(self.data.domain.classVar.values) 
     1257        existing = 0 
     1258        if self.matrixOfPredictions != None: 
     1259            existing = numpy.shape(self.matrixOfPredictions)[0]/classCount 
     1260            if existing < projCount: 
     1261                self.matrixOfPredictions = numpy.resize(self.matrixOfPredictions, (projCount*classCount, len(self.data))) 
     1262            elif existing > projCount: 
     1263                self.matrixOfPredictions = self.matrixOfPredictions[0:classCount*projCount,:] 
     1264        else: 
     1265            self.matrixOfPredictions = -100 * numpy.ones((projCount*classCount, len(self.data)), numpy.float) 
     1266 
     1267        # compute the matrix of predictions 
     1268        results = self.results[existing:min(len(self.results),projCount)] 
     1269        index = 0 
     1270        for result in results: 
     1271            if self.dialogType == VIZRANK_POINT: 
     1272                acc, other, tableLen, attrList, tryIndex, generalDict = result 
     1273                attrIndices = [self.widgetGraph.attributeNameIndex[attr] for attr in attrList] 
     1274                validDataIndices = self.widgetGraph.getValidIndices(attrIndices) 
     1275                table = self.widgetGraph.createProjectionAsExampleTable(attrIndices, settingsDict = generalDict)    # TO DO: this does not work with polyviz!!! 
     1276                acc, probabilities = self.vizrank.kNNClassifyData(table) 
     1277 
     1278            elif self.dialogType == VIZRANK_MOSAIC: 
     1279                from orngCI import FeatureByCartesianProduct 
     1280                acc, attrList, tryIndex, other = result 
     1281                probabilities = numpy.zeros((len(self.data), len(self.data.domain.classVar.values)), numpy.float) 
     1282                newFeature, quality = FeatureByCartesianProduct(self.data, attrList) 
     1283                dist = orange.ContingencyAttrClass(newFeature, self.data) 
     1284                data = self.data.select([newFeature, self.data.domain.classVar])     # create a dataset that has only this new feature and class info 
     1285                clsVals = len(self.data.domain.classVar.values) 
     1286                validDataIndices = range(len(data)) 
     1287                for i, ex in enumerate(data): 
     1288                    try: 
     1289                        prob = dist[ex[0]] 
     1290                        for j in range(clsVals): 
     1291                            probabilities[i][j] = prob[j] / float(sum(prob.values())) 
     1292                    except: 
     1293                        validDataIndices.remove(i) 
     1294 
     1295            #self.matrixOfPredictions[(existing + index)*classCount:(existing + index +1)*classCount] = numpy.transpose(probabilities) 
     1296            probabilities = numpy.transpose(probabilities) 
     1297            for i in range(classCount): 
     1298                numpy.put(self.matrixOfPredictions[(existing + index)*classCount + i], validDataIndices, probabilities[i]) 
     1299 
     1300            index += 1 
     1301            if hasattr(self, "setStatusBarText"): 
     1302                self.setStatusBarText("Evaluated %s/%s projections..." % (orngVisFuncts.createStringFromNumber(existing + index), orngVisFuncts.createStringFromNumber(projCount))) 
     1303                self.widget.progressBarSet(100.0*(index)/float(projCount-existing)) 
     1304            if qApp: 
     1305                qApp.processEvents() 
     1306 
     1307        # generate a sorted list of (probability, exampleIndex, classDistribution) 
     1308        projCount = min(int(self.projectionCount), len(self.results)) 
     1309        classCount = len(self.data.domain.classVar.values) 
     1310        self.evaluatedExamples = [] 
     1311        for exIndex in range(len(self.data)): 
     1312            matrix = numpy.transpose(numpy.reshape(self.matrixOfPredictions[:, exIndex], (projCount, classCount))) 
     1313            valid = numpy.where(matrix[int(self.data[exIndex].getclass())] != -100, 1, 0) 
     1314            data = numpy.compress(valid, matrix[int(self.data[exIndex].getclass())]) 
     1315            if len(data): aveAcc = numpy.sum(data) / float(len(data)) 
     1316            else:         aveAcc = 0 
     1317            classPredictions = [] 
     1318            for ind, val in enumerate(self.data.domain.classVar.values): 
     1319                data = numpy.compress(valid, matrix[ind]) 
     1320                if len(data): acc = numpy.sum(data) / float(len(data)) 
     1321                else:         acc = 0 
     1322                classPredictions.append((acc, val)) 
     1323            self.evaluatedExamples.append((aveAcc, exIndex, classPredictions)) 
     1324        self.evaluatedExamples.sort() 
     1325 
     1326    # take the self.evaluatedExamples list and find examples where probability of the "correct" class is lower than probability of some other class 
     1327    # change class value of such examples to class value that has the highest probability 
     1328    def changeClassToMostProbable(self): 
     1329        if not self.data or not self.evalueatedExamples or len(self.evaluatedExamples) != len(self.data): 
     1330            print "no data or outliers not found yet. Run evaluateProjections() first." 
     1331            return 
     1332 
     1333        for (aveAcc, exInd, classPredictions) in self.evaluatedExamples: 
     1334            (acc, clsVal) = max(classPredictions) 
     1335            if clsVal != self.data[exInd].getclass().value:   # if the most probable class is not the current class then change the class 
     1336                self.data[exInd].setclass(clsVal) 
    11971337 
    11981338 
Note: See TracChangeset for help on using the changeset viewer.