Changeset 3494:9be9a292a21f in orange


Ignore:
Timestamp:
04/04/07 13:40:00 (7 years ago)
Author:
Gregor <Gregor@…>
Branch:
default
Convert:
fb44a4d4252c5790bf3dac69ecd56725b00832ab
Message:

* empty log message *

Location:
orange
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • orange/OrangeWidgets/OWMosaicOptimization.py

    r3415 r3494  
    66from orngScaleData import getVariableValuesSorted 
    77 
    8 mosaicMeasures = [("Pearson's Chi Square", CHI_SQUARE),("Cramer's Phi (Correlation With Class)", CRAMERS_PHI),("Information Gain (In % Of Class Entropy Removed)", INFORMATION_GAIN), ("Gain Ratio", GAIN_RATIO), ("Interaction Gain (In % Of Class Entropy Removed)", INTERACTION_GAIN),("Average Probability Of Correct Classification", AVERAGE_PROBABILITY_OF_CORRECT_CLASSIFICATION), ("Gini index", GINI_INDEX), ("CN2 Rules", CN2_RULES)] 
     8mosaicMeasures = [("Pearson's Chi Square", CHI_SQUARE), 
     9                  ("Cramer's Phi (correlation with class)", CRAMERS_PHI), 
     10                  ("Information Gain (in % of class entropy removed)", INFORMATION_GAIN), 
     11                  ("Gain Ratio", GAIN_RATIO), 
     12                  ("Interaction Gain (in % of class entropy removed)", INTERACTION_GAIN), 
     13                  ("Average Probability Of Correct Classification", AVERAGE_PROBABILITY_OF_CORRECT_CLASSIFICATION), 
     14                  ("Gini index", GINI_INDEX), 
     15                  ("CN2 Rules", CN2_RULES)] 
    916 
    1017class OWMosaicOptimization(OWBaseWidget, orngMosaic): 
     
    1825    percentDataNums = [ 5 ,  10 ,  15 ,  20 ,  30 ,  40 ,  50 ,  60 ,  70 ,  80 ,  90 ,  100 ] 
    1926    #evaluationTimeNums = [0.5, 1, 2, 5, 10, 20, 30, 40, 60, 80, 120] 
    20      
     27 
    2128    def __init__(self, parentWidget = None, signalManager = None): 
    22         OWBaseWidget.__init__(self, None, signalManager, "Mosaic Optimization Dialog") 
     29        OWBaseWidget.__init__(self, None, signalManager, "Mosaic Optimization Dialog", savePosition = True) 
    2330        orngMosaic.__init__(self) 
    2431 
     
    3441        self.optimizeAttributeValueOrder = 0 
    3542        self.VizRankClassifierName = "Mosaic Learner" 
    36          
    37                  
     43 
     44 
    3845        self.lastSaveDirName = os.getcwd() 
    3946        self.selectedClasses = [] 
     
    4148        self.cancelArgumentation = 0 
    4249        self.useTimeLimit = 0 
    43          
     50 
    4451        self.attrLenDict = {} 
    4552        self.shownResults = [] 
    46         
     53 
    4754        self.loadSettings() 
    4855 
    4956        self.tabs = QTabWidget(self, 'tabWidget') 
    5057        self.controlArea.addWidget(self.tabs) 
    51          
     58 
    5259        self.MainTab = QVGroupBox(self) 
    5360        self.SettingsTab = QVGroupBox(self) 
     
    5562        self.ArgumentationTab = QVGroupBox(self) 
    5663        self.ClassificationTab = QVGroupBox(self) 
    57          
     64 
    5865        self.tabs.insertTab(self.MainTab, "Main") 
    5966        self.tabs.insertTab(self.SettingsTab, "Settings") 
     
    6168        self.tabs.insertTab(self.ClassificationTab, "Classification") 
    6269        self.tabs.insertTab(self.ManageTab, "Manage & Save") 
    63          
     70 
    6471        # ########################### 
    6572        # MAIN TAB 
     
    8491        self.resultList = QListBox(self.resultsBox) 
    8592        self.resultList.setMinimumSize(200,200) 
    86         self.connect(self.resultList, SIGNAL("selectionChanged()"), self.showSelectedAttributes)  
     93        self.connect(self.resultList, SIGNAL("selectionChanged()"), self.showSelectedAttributes) 
    8794 
    8895        OWGUI.checkBox(self.optimizeOrderSubBox, self, "optimizeAttributeOrder", "Optimize order of attributes", callback = self.optimizeCurrentAttributeOrder, tooltip = "Order the visualized attributes so that it will enhance class separation") 
     
    9097 
    9198        self.optimizeOrderButton = OWGUI.button(self.buttonsBox, self, "Optimize Current Attribute Order", callback = self.optimizeCurrentAttributeOrder, tooltip = "Optimize the order of currently visualized attributes") 
    92          
     99 
    93100 
    94101        # ########################## 
     
    98105        self.ignoreSmallCellsBox = OWGUI.widgetBox(self.SettingsTab, "Ignore Small Cells" ) 
    99106        self.ignoreSmallCellsCombo = OWGUI.checkBox(self.ignoreSmallCellsBox, self, "ignoreTooSmallCells", "Ignore cells where expected number of cases is less than 5", tooltip = "Statisticians advise that in cases when the number of expected examples is less than 5 we ignore the cell \nsince it can significantly influence the chi-square value.") 
    100          
     107 
    101108        self.testingBox = OWGUI.widgetBox(self.SettingsTab, "Testing Method") 
    102109        self.testingCombo = OWGUI.comboBox(self.testingBox, self, "testingMethod", items = ["10 fold cross validation", "70/30 separation 10 times "], tooltip = "Method for evaluating the class separation in the projection.") 
    103110        self.percentDataUsedCombo= OWGUI.comboBoxWithCaption(self.testingBox, self, "percentDataUsed", "Percent of data used: ", items = [5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], sendSelectedValue = 1, valueType = int, tooltip = "In case that we have a large dataset the evaluation of each projection can take a lot of time.\nWe can therefore use only a subset of randomly selected examples, evaluate projection on them and thus make evaluation faster.") 
    104          
     111 
    105112        OWGUI.comboBox(self.SettingsTab, self, "attrDisc", box = "Measure for Ranking Attributes", items = [val for (val, m) in discMeasures], callback = self.removeEvaluatedAttributes) 
    106113 
     
    156163        OWGUI.separator(self.classConfidenceBox, 20, 0) 
    157164        OWGUI.spin(self.classConfidenceBox, self, "classConfidence", 0, 99, 1, label = 'Confidence Interval (%):    ', tooltip = 'Confidence interval used in deciding whether to use a set of attributes independently or dependently') 
    158          
     165 
    159166        OWGUI.button(self.ClassificationTab, self, "Resend Learner", callback = self.resendLearner, tooltip = "Resend learner with new settings. You need to press this \nonly when you are sending mosaic learner signal to other widgets.") 
    160167 
     
    164171        self.dialogsBox = OWGUI.widgetBox(self.ManageTab, "Dialogs") 
    165172        self.manageResultsBox = OWGUI.widgetBox(self.ManageTab, "Manage Projections") 
    166          
     173 
    167174        self.attrLenList = QListBox(self.visualizedAttributesBox) 
    168175        self.attrLenList.setSelectionMode(QListBox.Multi) 
     
    174181        OWGUI.button(self.buttonBox7, self, "Attribute Interactions", self.interactionAnalysis, debuggingEnabled = 0) 
    175182 
    176         self.buttonBox8 = OWGUI.widgetBox(self.dialogsBox, orientation = "horizontal")     
     183        self.buttonBox8 = OWGUI.widgetBox(self.dialogsBox, orientation = "horizontal") 
    177184        OWGUI.button(self.buttonBox8, self, "Graph Projection Scores", self.graphProjectionQuality, debuggingEnabled = 0) 
    178185        OWGUI.button(self.buttonBox8, self, "Outlier Identification", self.identifyOutliers, debuggingEnabled = 0) 
     
    189196        self.controlArea.addWidget(self.statusBar) 
    190197        self.controlArea.activate() 
    191          
     198 
    192199        self.updateMestimateComboState() 
    193200        self.updateClassMethodsCombo() 
    194201        self.updateGUI() 
    195202 
    196          
    197  
    198     # ##############################################################     
     203 
     204 
     205    # ############################################################## 
    199206    # EVENTS 
    200207    def showSelectedAttributes(self, attrs = None): 
     
    217224            self.parentWidget.setShownAttributes(attrs) 
    218225        self.resultList.setFocus() 
    219              
     226 
    220227 
    221228    def optimizeCurrentAttributeOrder(self, attrs = None, updateGraph = 1): 
     
    225232 
    226233            if not attrs: 
    227                 attrs = self.parentWidget.getShownAttributes() 
    228                  
     234                attrs = self.parentWidget.getShownAttributeList() 
     235 
    229236            bestPlacements = self.findOptimalAttributeOrder(attrs, self.optimizeAttributeValueOrder) 
    230237            if updateGraph: 
     
    233240                    attrList, valueOrder = bestPlacements[0][1], bestPlacements[0][2] 
    234241                    self.parentWidget.setShownAttributes(attrList, customValueOrderDict = dict([(attrList[i], tuple(valueOrder[i])) for i in range(len(attrList))]) ) 
    235              
     242 
    236243            self.optimizeOrderButton.setText("Optimize Current Attribute Order") 
    237244            return bestPlacements 
     
    239246            self.stopOptimization = 1 
    240247            return [] 
    241          
     248 
    242249 
    243250    def updateGUI(self): 
     
    255262        self.classTau.hide() 
    256263        self.classConfidenceBox.hide() 
    257          
     264 
    258265        if self.classificationMethod == MOS_TOPPROJ: 
    259266            self.classTopProjCount.show() 
     
    262269        elif self.classificationMethod == MOS_COMBINING: 
    263270            self.classConfidenceBox.show() 
    264          
    265  
    266     # selected measure for attribute ranking has changed. recompute attribute importances         
     271 
     272 
     273    # selected measure for attribute ranking has changed. recompute attribute importances 
    267274    def removeEvaluatedAttributes(self): 
    268275        self.evaluatedAttributes = None 
     
    274281        # check which attribute lengths do we want to show 
    275282        if hasattr(self, "skipUpdate"): return 
    276          
     283 
    277284        self.attrLenDict = {} 
    278285        for i in range(self.attrLenList.count()): 
     
    300307                self.shownResults.append(self.results[i]) 
    301308        qApp.processEvents() 
    302          
     309 
    303310        if self.resultList.count() > 0: self.resultList.setCurrentItem(0) 
    304311 
    305     def setData(self, data, removeUnusedValues = 0, onlyDrilling = 0): 
     312    def setData(self, data, removeUnusedValues = 0): 
    306313        orngMosaic.setData(self, data, removeUnusedValues) 
    307314 
     
    310317        self.argumentList.clear() 
    311318        self.selectedClasses = [] 
    312                  
     319 
    313320        if not self.data: return 
    314          
     321 
    315322        if hasattr(self.data, "name"): self.datasetName = data.name 
    316323        else: self.datasetName = "" 
     
    322329            self.classValueList.insertItem(val) 
    323330        self.updateShownArguments() 
    324          
     331 
    325332        if len(self.data.domain.classVar.values) > 0: 
    326333            self.classValueList.setCurrentItem(0) 
     
    335342        self.argumentList.clear() 
    336343        self.arguments = [[] for i in range(self.classValueList.count())] 
    337                  
     344 
    338345        if not example and not self.parentWidget.subsetData: 
    339346            QMessageBox.information( None, "Argumentation", 'To find arguments you first have to provide an example that you wish to classify. \nYou can do this by sending the example to the Mosaic display widget through the "Example Subset" signal.', QMessageBox.Ok + QMessageBox.Default) 
     
    342349            QMessageBox.information( None, "Argumentation", 'To find arguments you first have to evaluate some projections by clicking "Start evaluating projections" in the Main tab.', QMessageBox.Ok + QMessageBox.Default) 
    343350            return None, None 
    344          
     351 
    345352        if not self.data: 
    346353            QMessageBox.critical(None,'No data','There is no data or no class value is selected in the Manage tab.',QMessageBox.Ok) 
     
    348355 
    349356        if example == None: example = self.parentWidget.subsetData[0] 
    350          
     357 
    351358        self.findArgumentsButton.hide() 
    352359        self.stopArgumentationButton.show() 
    353         
     360 
    354361        classValue, dist = orngMosaic.findArguments(self, example) 
    355          
     362 
    356363        self.stopArgumentationButton.hide() 
    357364        self.findArgumentsButton.show() 
    358          
     365 
    359366        values = getVariableValuesSorted(self.data, self.data.domain.classVar.name) 
    360367        self.argumentationClassValue = values.index(classValue)     # activate the class that has the highest probability 
     
    367374                s += "<nobr>&nbsp &nbsp &nbsp &nbsp %s : %.2f%%</nobr><br>" % (key, dist[key]*100) 
    368375            QMessageBox.information(None, "Classification results", s, QMessageBox.Ok + QMessageBox.Default) 
    369          
     376 
    370377        return (classValue, dist) 
    371378 
    372     
     379 
    373380    def finishedAddingResults(self): 
    374381        self.cancelOptimization = 0 
    375382        self.skipUpdate = 1 
    376          
     383 
    377384        self.attrLenDict = dict([(i,1) for i in range(self.attributeCount+1)]) 
    378          
     385 
    379386        self.attrLenList.clear() 
    380387        for i in range(1,5): 
     
    387394        self.resultList.setCurrentItem(0) 
    388395 
    389     
     396 
    390397    # ############################################################## 
    391398    # Loading and saving projection files 
     
    415422 
    416423        orngMosaic.save(self, name) 
    417          
     424 
    418425        self.setStatusBarText("Saved %d visualizations" % (len(self.results))) 
    419426 
     
    447454        self.SettingsTab.setEnabled(0) 
    448455        self.ManageTab.setEnabled(0) 
    449          
     456 
    450457    def enableControls(self): 
    451458        self.startOptimizationButton.show() 
     
    457464    # Auxiliary functions 
    458465    # ###################################################### 
    459      
     466 
    460467    def getSelectedProjection(self): 
    461468        currentItem = self.resultList.currentItem() 
     
    470477        if self.cancelOptimization: return 1 
    471478        if self.useTimeLimit:       return orngMosaic.isEvaluationCanceled(self) 
    472          
     479 
    473480    def destroy(self, dw = 1, dsw = 1): 
    474481        self.saveSettings() 
     
    483490        s += "- " + self.buildAttrString(attrList) 
    484491        self.argumentList.insertItem(s, index) 
    485             
     492 
    486493    def updateShownArguments(self): 
    487494        self.argumentList.clear() 
     
    489496        classVal = str(self.classValueList.currentText()) 
    490497        self.logitLabel.setText("log odds = %.2f" % self.logits.get(classVal, -1)) 
    491          
     498 
    492499        if self.classificationMethod == MOS_COMBINING: 
    493500            self.logitLabel.show() 
     
    499506            (argScore, accuracy, attrList, index, error) = self.arguments[classVal][i] 
    500507            self.insertArgument(argScore, error, attrList, i) 
    501              
     508 
    502509 
    503510    def argumentSelected(self): 
     
    539546        dialog.setData(self.results, self.data, OWkNNOptimization.VIZRANK_MOSAIC) 
    540547        dialog.show() 
    541  
    542      
    543 class OWMosaicTreeDialog(OWBaseWidget, orngMosaic): 
    544     #settingsList = [] 
    545     def __init__(self, mosaicOptimizationWidget, mosaicWidget, signalManager = None): 
    546         OWBaseWidget.__init__(self, None, signalManager, "Mosaic Tree Dialog") 
    547         orngMosaic.__init__(self) 
    548  
    549         self.mosaicOptimizationWidget = mosaicOptimizationWidget 
    550         self.mosaicWidget = mosaicWidget 
    551         self.wholeDataSet = None 
    552  
    553         self.controlArea = QVBoxLayout(self) 
    554         self.controls = OWGUI.widgetBox(self, box = 1) 
    555         self.controlArea.addWidget(self.controls) 
    556  
    557         drillingBox = OWGUI.widgetBox(self.controls, "Drilling Tree") 
    558         self.drillingTree = QListView(drillingBox) 
    559         self.drillingTree.setRootIsDecorated(1) 
    560         self.drillingTree.setAllColumnsShowFocus(1) 
    561         self.drillingTree.addColumn('Visualized Attributes') 
    562         self.drillingTree.addColumn('# inst.')         
    563         self.drillingTree.setColumnWidth(0, 300) 
    564         self.drillingTree.setColumnWidthMode(0, QListView.Maximum) 
    565         self.drillingTree.setColumnAlignment(0, QListView.AlignLeft) 
    566         self.drillingTree.setColumnWidth(1, 50) 
    567         self.drillingTree.setColumnWidthMode(1, QListView.Manual) 
    568         self.drillingTree.setColumnAlignment(1, QListView.AlignRight) 
    569         self.connect(self.drillingTree, SIGNAL("selectionChanged(QListViewItem *)"), self.selectedItemChanged) 
    570         self.connect(self.drillingTree, SIGNAL("rightButtonPressed(QListViewItem *, const QPoint &, int )"), self.removeItemPopup) 
    571         self.drillingTree.resize(100, 100) 
    572         self.drillingItems = {} 
    573         self.drillUpdateInProgress = 0 
    574         
    575         drillingButt = OWGUI.widgetBox(self.controls, "") 
    576         OWGUI.button(drillingButt, self, "Explore Current Selection", callback = self.eploreCurrentSelection, tooltip = "Visualize only examples in the selected boxes and find interesting projections of them.") 
    577  
    578         self.drillingPopupMenu = QPopupMenu(self) 
    579         self.drillingPopupMenu.insertItem("Remove item and children", self.removeSelectedItem) 
    580         self.controlArea.activate() 
    581  
    582         self.resize(300,450) 
    583  
    584     def setData(self, data): 
    585         self.wholeDataSet = data 
    586         self.initDrillingTree() 
    587  
    588     # clear drilling tree and create a new root 
    589     def initDrillingTree(self): 
    590         self.drillingItems = {} 
    591         self.drillingTree.clear() 
    592         self.drillingTree.setColumnWidth(0, self.drillingTree.width() - self.drillingTree.columnWidth(1)-4) 
    593  
    594         if self.wholeDataSet: 
    595             root = QListViewItem(self.drillingTree, "<root>", str(len(self.wholeDataSet))) 
    596             root.setOpen(1) 
    597             self.drillingTree.insertItem(root) 
    598             self.drillingItems[str(root)] = {"data": self.wholeDataSet} 
    599             self.drillingTree.setSelected(root, 1)             
    600  
    601  
    602     # new element is added into the drillingTree 
    603     def eploreCurrentSelection(self): 
    604         if not self.wholeDataSet: 
    605             return 
    606          
    607         if self.mosaicWidget.selectionConditionsHistorically == []: 
    608             QMessageBox.information(self, "Select some cells first", "To drill deeper you first have to select cells that are of interest to you\nby clicking them or selecting them by drawing a rectangle.", QMessageBox.Ok) 
    609             return 
    610  
    611         selectedIndices = self.mosaicWidget.getSelectedExamples(asExampleTable = 0) 
    612         selectedData = self.mosaicOptimizationWidget.data.selectref(selectedIndices) 
    613         unselectedData = self.mosaicOptimizationWidget.data.selectref(selectedIndices, negate = 1) 
    614         #self.mosaicWidget.setData(selectedData, onlyDrilling = 1) 
    615         attrList = self.mosaicWidget.getShownAttributes() 
    616          
    617         selectedItem = self.drillingTree.selectedItem()     # current selection 
    618          
    619         shownAttrs = reduce(lambda x,y: x+', '+y, attrList) 
    620         newListItem = QListViewItem(selectedItem, shownAttrs) 
    621                  
    622         # if newListItem was the first child bellow the root we have to add another child that will actually show only selected examples in newListItem 
    623         if str(selectedItem.text(0)) == "<root>": 
    624             self.drillingItems[str(newListItem)] = {"selectedIndices": selectedIndices, "attrs": list(attrList), "selectionConditions": list(self.mosaicWidget.selectionConditions), "selectionConditionsHistorically": list(self.mosaicWidget.selectionConditionsHistorically)} 
    625             newListItem.setText(1, str(len(self.mosaicOptimizationWidget.data))) 
    626             newListItem.setOpen(1) 
    627              
    628             newnewListItem = QListViewItem(newListItem, shownAttrs) 
    629             self.drillingItems[str(newnewListItem)] = {"attrs": list(attrList)} 
    630             newnewListItem.setText(1, str(len(selectedData))) 
    631             newnewListItem.setOpen(1) 
    632             self.drillingTree.setSelected(newnewListItem, 1) 
    633         else: 
    634             self.drillingItems[str(selectedItem)] = {"selectedIndices": selectedIndices, "attrs": list(attrList), "selectionConditions": list(self.mosaicWidget.selectionConditions), "selectionConditionsHistorically": list(self.mosaicWidget.selectionConditionsHistorically)} 
    635             self.drillingItems[str(newListItem)] = {"attrs": list(attrList)} 
    636             newListItem.setText(1, str(len(selectedData))) 
    637             newListItem.setOpen(1) 
    638             self.drillingTree.setSelected(newListItem, 1) 
    639  
    640     # a different attribute set was selected in mosaic. update the attributes in the selected node 
    641     def updateSelection(self): 
    642         if not self.wholeDataSet: return 
    643         if self.drillUpdateInProgress: return 
    644  
    645         selectedItem = self.drillingTree.selectedItem() 
    646         if not selectedItem or str(selectedItem.text(0)) == "<root>":   # we don't change the title of the root.  
    647             return 
    648  
    649         attrList = self.mosaicWidget.getShownAttributes() 
    650  
    651         # if this is the last element in the tree, then update the element's values 
    652         if not selectedItem.firstChild():    
    653             selectedItem.setText(0, reduce(lambda x,y: x+', '+y, attrList)) 
    654             self.drillingItems[str(selectedItem)]["attrs"] = list(attrList) 
    655             self.drillingItems[str(selectedItem)]["selectionConditions"] = list(self.mosaicWidget.selectionConditions) 
    656             self.drillingItems[str(selectedItem)]["selectionConditionsHistorically"] = list(self.mosaicWidget.selectionConditionsHistorically) 
    657  
    658         # add a sibling if we changed any value 
    659         else: 
    660 ##            print attrList, self.drillingItems[str(selectedItem)].get("attrs", []) 
    661 ##            print self.mosaicWidget.selectionConditions, self.drillingItems[str(selectedItem)].get("selectionConditions", []) 
    662 ##            print self.mosaicWidget.selectionConditionsHistorically, self.drillingItems[str(selectedItem)].get("selectionConditionsHistorically", []) 
    663             if attrList != self.drillingItems[str(selectedItem)].get("attrs", []) or \ 
    664                     self.mosaicWidget.selectionConditions != self.drillingItems[str(selectedItem)].get("selectionConditions", []) or \ 
    665                     self.mosaicWidget.selectionConditionsHistorically != self.drillingItems[str(selectedItem)].get("selectionConditionsHistorically", []): 
    666                 parent = selectedItem.parent() 
    667                 newListItem = QListViewItem(parent, reduce(lambda x,y: x+', '+y, attrList)) 
    668                 newListItem.setOpen(1) 
    669                 newListItem.setText(1, str(selectedItem.text(1)))   # new item has the same number of examples as the selected item                 
    670                 self.drillingItems[str(newListItem)] = {"attrs": list(attrList), "selectionConditions": list(self.mosaicWidget.selectionConditions), "selectionConditionsHistorically": list(self.mosaicWidget.selectionConditionsHistorically)} 
    671                 self.drillingTree.setSelected(newListItem, 1) 
    672                  
    673  
    674     # we selected a different item in the tree 
    675     def selectedItemChanged(self, newSelection): 
    676         if not newSelection or str(newSelection.text(0)) == "<root>": 
    677             self.mosaicWidget.setData(self.wholeDataSet, onlyDrilling = 1) 
    678             self.mosaicWidget.setSubsetData(None) 
    679             return 
    680          
    681         indices = self.getItemIndices(newSelection) 
    682         selectedData = self.wholeDataSet 
    683         unselectedData = orange.ExampleTable(self.wholeDataSet.domain) 
    684         for ind in indices: 
    685             unselectedData.extend(selectedData.selectref(ind, negate = 1)) 
    686             selectedData = selectedData.selectref(ind)  
    687  
    688         self.drillUpdateInProgress = 1 
    689          
    690         # set data 
    691         self.mosaicWidget.setData(selectedData, onlyDrilling = 1) 
    692         self.mosaicWidget.subsetData = unselectedData        #self.mosaicWidget.setSubsetData(unselectedData) 
    693         # set shown attributes 
    694         self.mosaicWidget.setShownAttributes(self.drillingItems[str(newSelection)].get("attrs", None)) 
    695         # set selections 
    696         self.mosaicWidget.selectionConditions = list(self.drillingItems[str(newSelection)].get("selectionConditions", [])) 
    697         self.mosaicWidget.selectionConditionsHistorically = list(self.drillingItems[str(newSelection)].get("selectionConditionsHistorically", [])) 
    698         self.mosaicWidget.updateGraph() 
    699  
    700         self.drillUpdateInProgress = 0 
    701  
    702     def getItemIndices(self, item): 
    703         indices = [] 
    704         parent = item.parent() 
    705         while parent: 
    706             parentIndices = self.drillingItems[str(parent)].get("selectedIndices", None) 
    707             if parentIndices: 
    708                 indices.insert(0, parentIndices)        # insert indices in reverse order 
    709             parent = parent.parent() 
    710         return indices 
    711      
    712     def removeSelectedItem(self): 
    713         item = self.drillingTree.selectedItem() 
    714         if not item: 
    715             return 
    716         if str(item.text(0)) == "<root>": 
    717             self.initDrillingTree() 
    718         else: 
    719             item.parent().takeItem(item) 
    720                  
    721     def removeItemPopup(self, item, point, i): 
    722         self.drillingPopupMenu.popup(point, 0) 
    723  
    724     def resizeEvent(self, ev): 
    725         OWBaseWidget.resizeEvent(self, ev)         
    726         self.drillingTree.setColumnWidth(0, self.drillingTree.width()-self.drillingTree.columnWidth(1)-4) 
    727548 
    728549 
  • orange/orngLinProj.py

    r3411 r3494  
    4242        self.attrsNum = [5, 10, 20, 30, 50, 70, 100, 150, 200, 300, 500, 750, 1000] 
    4343        #attrsNum = [5, 10, 20, 30, 50, 70, 100, 150, 200, 300, 500, 750, 1000, 2000, 3000, 5000, 10000, 50000] 
    44          
     44 
    4545    def setData(self, data): 
    4646        self.rawdata = data 
    4747        self.s2nMixData = None 
    4848        self.classPermutationList = None 
    49          
     49 
    5050    # save subsetdata. first example from this dataset can be used with argumentation - it can find arguments for classifying the example to the possible class values 
    5151    def setSubsetData(self, subsetdata): 
    5252        self.subsetdata = subsetdata 
    53      
     53 
    5454 
    5555    def showAllAttributes(self): 
     
    9595            #### Need to rotate and mirror here 
    9696            pass 
    97              
     97 
    9898        self.graph.anchorData = anchors 
    9999 
     
    110110            if self.__class__ != FreeViz: from qt import qApp 
    111111 
    112             for c in range((singleStep and 1) or 50):                 
     112            for c in range((singleStep and 1) or 50): 
    113113                for i in range(steps): 
    114114                    if self.__class__ != FreeViz and self.cancelOptimization == 1: return 
     
    125125        attrIndices = [ai[label] for label in self.getShownAttributeList()] 
    126126        if self.__class__ != FreeViz: from qt import qApp 
    127         
     127 
    128128        # repeat until less than 1% energy decrease in 5 consecutive iterations*steps steps 
    129129        positions = [numpy.array([x[:2] for x in self.graph.anchorData])] 
     
    142142                self.graph.potentialsBmp = None 
    143143                self.graph.updateData() 
    144                  
     144 
    145145            positions = positions[-49:]+[numpy.array([x[:2] for x in self.graph.anchorData])] 
    146146            if len(positions)==50: 
     
    159159        if not XAnchors: XAnchors = numpy.array([a[0] for a in anchorData], numpy.Float) 
    160160        if not YAnchors: YAnchors = numpy.array([a[1] for a in anchorData], numpy.Float) 
    161          
     161 
    162162        transProjData = self.graph.createProjectionAsNumericArray(attrIndices, validData = validData, XAnchors = XAnchors, YAnchors = YAnchors, scaleFactor = self.graph.scaleFactor, normalize = self.graph.normalizeExamples, useAnchorData = 1) 
    163163        if not transProjData: 
    164164            return anchorData 
    165          
     165 
    166166        projData = numpy.transpose(transProjData) 
    167167        x_positions = projData[0]; y_positions = projData[1]; classData = projData[2] 
     
    175175 
    176176        # compute the positions of all the points. we will try to move all points so that the center will be in the (0,0) 
    177         xCenterVector = -numpy.sum(x_positions) / len(x_positions)    
     177        xCenterVector = -numpy.sum(x_positions) / len(x_positions) 
    178178        yCenterVector = -numpy.sum(y_positions) / len(y_positions) 
    179179        centerVectorLength = math.sqrt(xCenterVector*xCenterVector + yCenterVector*yCenterVector) 
    180180 
    181181        meanDestinationVectors = [] 
    182          
     182 
    183183        for i in range(classCount): 
    184184            xDir = 0.0; yDir = 0.0; rs = 0.0 
     
    199199            #yDir = rs * (yDir/s) 
    200200            meanDestinationVectors.append((xDir, yDir)) 
    201              
     201 
    202202 
    203203        maxLength = math.sqrt(max([x**2 + y**2 for (x,y) in meanDestinationVectors])) 
     
    209209        FXs = numpy.zeros(len(x_positions), numpy.Float)        # forces 
    210210        FYs = numpy.zeros(len(x_positions), numpy.Float) 
    211          
     211 
    212212        for c in range(classCount): 
    213213            ind = (classData == c) 
    214214            numpy.putmask(FXs, ind, meanDestinationVectors[c][0] - x_positions) 
    215215            numpy.putmask(FYs, ind, meanDestinationVectors[c][1] - y_positions) 
    216              
     216 
    217217        # compute gradient for all anchors 
    218218        GXs = numpy.array([sum(FXs * selectedData[i]) for i in range(len(anchorData))], numpy.Float) 
     
    221221        m = max(max(abs(GXs)), max(abs(GYs))) 
    222222        GXs /= (20*m); GYs /= (20*m) 
    223          
     223 
    224224        newXAnchors = XAnchors + GXs 
    225225        newYAnchors = YAnchors + GYs 
    226226 
    227         # normalize so that the anchor most far away will lie on the circle         
     227        # normalize so that the anchor most far away will lie on the circle 
    228228        m = math.sqrt(max(newXAnchors**2 + newYAnchors**2)) 
    229229        newXAnchors /= m 
     
    234234        """ 
    235235        for a in range(len(anchorData)): 
    236             x = anchorData[a][0]; y = anchorData[a][1];  
     236            x = anchorData[a][0]; y = anchorData[a][1]; 
    237237            self.parentWidget.graph.addCurve("lll%i" % i, QColor(0, 0, 0), QColor(0, 0, 0), 10, style = QwtCurve.Lines, symbol = QwtSymbol.None, xData = [x, x+GXs[a]], yData = [y, y+GYs[a]], forceFilledSymbols = 1, lineWidth=3) 
    238          
     238 
    239239        for i in range(classCount): 
    240240            self.parentWidget.graph.addCurve("lll%i" % i, QColor(0, 0, 0), QColor(0, 0, 0), 10, style = QwtCurve.Lines, symbol = QwtSymbol.None, xData = [averages[i][0], meanDestinationVectors[i][0]], yData = [averages[i][1], meanDestinationVectors[i][1]], forceFilledSymbols = 1, lineWidth=3) 
     
    246246        return [(newXAnchors[i], newYAnchors[i], anchorData[i][2]) for i in range(len(anchorData))], (newXAnchors, newYAnchors) 
    247247 
    248              
     248 
    249249    def optimize_SLOW_Separation(self, attrIndices, anchorData, XAnchors = None, YAnchors = None): 
    250250        dataSize = len(self.graph.rawdata) 
     
    254254        if not XAnchors: XAnchors = numpy.array([a[0] for a in anchorData], numpy.Float) 
    255255        if not YAnchors: YAnchors = numpy.array([a[1] for a in anchorData], numpy.Float) 
    256          
     256 
    257257        transProjData = self.graph.createProjectionAsNumericArray(attrIndices, validData = validData, XAnchors = XAnchors, YAnchors = YAnchors, scaleFactor = self.graph.scaleFactor, normalize = self.graph.normalizeExamples, useAnchorData = 1) 
    258258        if not transProjData: 
    259259            return anchorData 
    260          
     260 
    261261        projData = numpy.transpose(transProjData) 
    262262        x_positions = projData[0]; x_positions2 = numpy.array(x_positions) 
     
    268268        GXs = numpy.zeros(len(anchorData), numpy.Float)        # gradients 
    269269        GYs = numpy.zeros(len(anchorData), numpy.Float) 
    270          
     270 
    271271        rotateArray = range(len(x_positions)); rotateArray = rotateArray[1:] + [0] 
    272272        for i in range(len(x_positions)-1): 
     
    279279            rs2 += numpy.where(rs2 == 0.0, 0.0001, 0.0)    # replace zeros to avoid divisions by zero 
    280280            rs = numpy.sqrt(rs2) 
    281              
     281 
    282282            F = numpy.zeros(len(x_positions), numpy.Float) 
    283283            classDiff = numpy.where(classData == classData2, 1, 0) 
     
    293293        m = max(max(abs(GXs)), max(abs(GYs))) 
    294294        GXs /= (20*m); GYs /= (20*m) 
    295          
     295 
    296296        newXAnchors = XAnchors + GXs 
    297297        newYAnchors = YAnchors + GYs 
    298298 
    299         # normalize so that the anchor most far away will lie on the circle         
     299        # normalize so that the anchor most far away will lie on the circle 
    300300        m = math.sqrt(max(newXAnchors**2 + newYAnchors**2)) 
    301301        newXAnchors /= m 
     
    303303        return [(newXAnchors[i], newYAnchors[i], anchorData[i][2]) for i in range(len(anchorData))], (newXAnchors, newYAnchors) 
    304304 
    305              
     305 
    306306##    def recomputeEnergy(self, newEnergy = None): 
    307307##        if not newEnergy: 
     
    320320                attributes = self.getShownAttributeList() 
    321321                attrIndices = [ai[label] for label in attributes] 
    322                  
     322 
    323323            validData = self.graph.getValidList(attrIndices) 
    324324            self.graph.normalizeExamples = 0 
    325              
     325 
    326326            selectedData = numpy.compress(validData, numpy.take(self.graph.noJitteringScaledData, attrIndices, axis = 0), axis = 1) 
    327327            classData = numpy.compress(validData, self.graph.noJitteringScaledData[ai[self.graph.rawdata.domain.classVar.name]]) 
     
    331331                selectedData = numpy.compress(indices, selectedData, axis = 1) 
    332332                classData = numpy.compress(indices, classData) 
    333              
     333 
    334334            selectedData = numpy.transpose(selectedData) 
    335              
    336             s = numpy.sum(selectedData, axis=0)/float(len(selectedData))   
     335 
     336            s = numpy.sum(selectedData, axis=0)/float(len(selectedData)) 
    337337            selectedData -= s       # substract average value to get zero mean 
    338338 
     
    343343                    L[i,j] = -int(classData[i] != classData[j]) 
    344344                    L[j,i] = -int(classData[i] != classData[j]) 
    345              
     345 
    346346            s = numpy.sum(L, axis=0)      # doesn't matter which axis since the matrix L is symmetrical 
    347347            for i in range(len(selectedData)): 
     
    354354            else: 
    355355                matrix = numpy.transpose(selectedData) 
    356              
     356 
    357357            # compute selectedDataT * L * selectedData 
    358358            if SPCA: 
    359359                matrix = numpy.dot(matrix, L) 
    360                  
     360 
    361361            matrix = numpy.dot(matrix, selectedData) 
    362362 
    363363            vals, vectors = eig(matrix) 
    364364            firstInd  = list(vals).index(max(vals))     # save the index of the largest eigenvector 
    365             vals[firstInd] = -1    
     365            vals[firstInd] = -1 
    366366            secondInd = list(vals).index(max(vals));    # save the index of the second largest eigenvector 
    367367 
     
    384384##            type, val, traceback = sys.exc_info() 
    385385##            sys.excepthook(type, val, traceback) 
    386              
     386 
    387387            names = self.graph.attributeNames 
    388388            attributes = [names[attrIndices[i]] for i in range(len(attrIndices))] 
     
    394394    # S2N HEURISTIC FUNCTIONS 
    395395    # ############################################################### 
    396      
     396 
    397397    # if autoSetParameters is set then try different values for parameters and see how good projection do we get 
    398398    # if not then just use current parameters to place anchors 
     
    400400        if self.__class__ != FreeViz: 
    401401            import qt 
    402                  
     402 
    403403        if not self.rawdata.domain.classVar or not self.rawdata.domain.classVar.varType == orange.VarTypes.Discrete: 
    404404            if self.__class__ != FreeViz: 
     
    409409 
    410410        import orngVizRank 
    411         vizrank = orngVizRank.VizRank(orngVizRank.RADVIZ, graph = self.graph) 
     411        if self.graph.normalizeExamples: 
     412            vizrank = orngVizRank.VizRank(orngVizRank.RADVIZ) 
     413        else: 
     414            vizrank = orngVizRank.VizRank(orngVizRank.LINEAR_PROJECTION) 
    412415        vizrank.qualityMeasure = orngVizRank.AVERAGE_CORRECT 
    413416        vizrank.setData(self.rawdata) 
    414          
     417 
    415418        if self.autoSetParameters: 
    416419            results = {} 
     
    422425                    if self.attrsNum[self.attrsNum.index(val)-1] > len(self.rawdata.domain.attributes): continue    # allow the computations once 
    423426                    self.s2nPlaceAttributes = val 
    424                     self.s2nMixAnchors(0) 
     427                    if not self.s2nMixAnchors(0): 
     428                        return 
    425429                    if self.__class__ != FreeViz: 
    426430                        qt.qApp.processEvents() 
    427                          
     431 
    428432                    acc, other = vizrank.kNNComputeAccuracy(self.graph.createProjectionAsExampleTable(None, useAnchorData = 1)) 
    429433                    if hasattr(self, "setStatusBarText"): 
    430434                        if results.keys() != []: self.setStatusBarText("Current projection value is %.2f (best is %.2f)" % (acc, max(results.keys()))) 
    431435                        else:                    self.setStatusBarText("Current projection value is %.2f" % (acc)) 
    432                                                               
     436 
    433437                    results[acc] = (perm, val) 
    434438            if results.keys() == []: return 
     
    436440            if self.__class__ != FreeViz: 
    437441                qt.qApp.processEvents() 
    438             self.s2nMixAnchors(0)        # update the best number of attributes 
     442            if not self.s2nMixAnchors(0):        # update the best number of attributes 
     443                return 
    439444 
    440445            results = [] 
     
    444449            for val in range(10): 
    445450                self.s2nSpread = val 
    446                 self.s2nMixAnchors(0) 
     451                if not self.s2nMixAnchors(0): 
     452                    return 
    447453                acc, other = vizrank.kNNComputeAccuracy(self.graph.createProjectionAsExampleTable(attrIndices, useAnchorData = 1)) 
    448454                results.append(acc) 
     
    465471            if not self.rawdata.domain.classVar or not self.rawdata.domain.classVar.varType == orange.VarTypes.Discrete: 
    466472                qt.QMessageBox.critical( None, "Error", 'This heuristic works only in data sets with a discrete class value.', QMessageBox.Ok) 
    467                 return 
    468          
     473                return 0 
     474 
    469475        # compute the quality of attributes only once 
    470476        if self.s2nMixData == None: 
     
    476482            classCount = len(self.s2nMixData[1]) 
    477483            attrs = self.s2nMixData[0][:(self.s2nPlaceAttributes/classCount)*classCount] 
    478              
     484 
     485        if len(attrs) == 0: return 0 
     486 
    479487        arr = [0]       # array that will tell where to put the next attribute 
    480488        for i in range(1,len(attrs)/2): arr += [i,-i] 
    481489 
    482         if len(attrs) == 0: return 
    483490        phi = (2*math.pi*self.s2nSpread)/(len(attrs)*10.0) 
    484491        anchorData = []; start = [] 
     
    502509            self.graph.updateData(attrNames) 
    503510            self.graph.repaint() 
     511        return 1 
    504512 
    505513 
     
    507515 
    508516# ############################################################################# 
    509 # class that represents FreeViz classifier  
     517# class that represents FreeViz classifier 
    510518class FreeVizClassifier(orange.Classifier): 
    511519    def __init__(self, data, freeviz): 
     
    513521 
    514522        if self.FreeViz.__class__ != FreeViz: 
    515             self.FreeViz.parentWidget.cdata(data) 
     523            self.FreeViz.parentWidget.setData(data) 
    516524            self.FreeViz.parentWidget.showAllAttributes = 1 
    517525        else: 
    518526            self.FreeViz.graph.setData(data) 
    519527            self.FreeViz.showAllAttributes() 
    520              
     528 
    521529        #self.FreeViz.randomAnchors() 
    522530        self.FreeViz.radialAnchors() 
    523531        self.FreeViz.optimizeSeparation() 
    524          
     532 
    525533        graph = self.FreeViz.graph 
    526534        ai = graph.attributeNameIndex 
     
    537545                                      graph.anchorData, offsets, normalizers, averages, graph.normalizeExamples, law=self.FreeViz.law) 
    538546 
    539     # for a given example run argumentation and find out to which class it most often fall         
     547    # for a given example run argumentation and find out to which class it most often fall 
    540548    def __call__(self, example, returnType): 
    541549        #example.setclass(0) 
     
    549557        self.FreeViz = freeviz 
    550558        self.name = "FreeViz Learner" 
    551          
     559 
    552560    def __call__(self, examples, weightID = 0): 
    553561        return FreeVizClassifier(examples, self.FreeViz) 
     
    563571 
    564572        if self.FreeViz.__class__ != FreeViz: 
    565             self.FreeViz.parentWidget.cdata(data) 
     573            self.FreeViz.parentWidget.setData(data) 
    566574        else: 
    567575            self.FreeViz.setData(data) 
    568576            self.FreeViz.graph.setData(data) 
    569              
     577 
    570578        self.FreeViz.s2nMixAnchorsAutoSet() 
    571579 
     
    577585            self.FreeViz.parentWidget.subsetdata(table)      # show the example is we use the widget 
    578586        else: 
    579             self.FreeViz.graph.setSubsetData(table)        
    580              
     587            self.FreeViz.graph.setSubsetData(table) 
     588 
    581589        anchorData = self.FreeViz.graph.anchorData 
    582590        attributeNameIndex = self.FreeViz.graph.attributeNameIndex 
     
    585593        attrListIndices = [attributeNameIndex[val[2]] for val in anchorData] 
    586594        attrVals = [scaleFunction(example, index) for index in attrListIndices] 
    587                  
     595 
    588596        table = self.FreeViz.graph.createProjectionAsExampleTable(attrListIndices, scaleFactor = self.FreeViz.graph.trueScaleFactor, useAnchorData = 1) 
    589597        kValue = int(math.sqrt(len(self.data))) 
     
    593601        classifier = knn(table, 0) 
    594602        (classVal, dist) = classifier(orange.Example(table.domain, [xTest, yTest, "?"]), orange.GetBoth) 
    595          
     603 
    596604        if returnType == orange.GetBoth: return classVal, dist 
    597605        else:                            return classVal 
    598          
     606 
    599607 
    600608class S2NHeuristicLearner(orange.Learner): 
     
    604612        self.FreeViz = freeviz 
    605613        self.name = "S2N Feature Selection Learner" 
    606          
     614 
    607615    def __call__(self, examples, weightID = 0): 
    608616        return S2NHeuristicClassifier(examples, self.FreeViz) 
  • orange/orngMosaic.py

    r3411 r3494  
    115115        if not self.data: 
    116116            return 
    117          
     117 
    118118        self.attributeNameIndex = dict([(self.data.domain[i].name, i) for i in range(len(self.data.domain))]) 
    119119 
     
    133133                self.logits[self.classVals[i]] = log(p) 
    134134 
    135      
     135 
    136136    # given a dataset return a list of attributes where attribute are sorted by their decreasing importance for class discrimination 
    137137    def getEvaluatedAttributes(self, data): 
     
    147147            self.setStatusBarText("Evaluating attributes...") 
    148148            qApp.setOverrideCursor(QWidget.waitCursor) 
    149          
     149 
    150150        try: 
    151151            # evaluate attributes using the selected attribute measure 
     
    163163            self.setStatusBarText("") 
    164164            qApp.restoreOverrideCursor() 
    165              
     165 
    166166        if not self.evaluatedAttributes: return [] 
    167167        else:                            return self.evaluatedAttributes 
     
    192192        self.contingencies.update(conts) 
    193193        return conts 
    194              
     194 
    195195 
    196196    def isEvaluationCanceled(self): 
     
    211211        self.clearResults() 
    212212        self.resultListIndices = [] 
    213          
     213 
    214214        if self.__class__.__name__ == "OWMosaicOptimization": 
    215215            self.disableControls() 
    216216            self.parentWidget.progressBarInit() 
    217217            from qt import qApp 
    218              
     218 
    219219        self.startTime = time.time() 
    220220 
     
    223223        else:                          minLength = 1 
    224224 
    225         # generate cn2 rules and show projections that have  
     225        # generate cn2 rules and show projections that have 
    226226        if self.qualityMeasure == CN2_RULES: 
    227227            ruleFinder = orange.RuleBeamFinder() 
     
    238238                from OWCN2 import CN2ProgressBar 
    239239                learner.progressCallback = CN2ProgressBar(self.parentWidget) 
    240                                  
     240 
    241241            classifier = learner(self.data) 
    242              
     242 
    243243            self.dictResults = {} 
    244244            for rule in classifier.rules: 
     
    256256                score = sum([e[0] for e in el]) / float(len(el)) 
    257257                self.insertItem(score, el[0][1], self.findTargetIndex(score, max), 0, extraInfo = el) 
    258              
    259         else:             
     258 
     259        else: 
    260260            evaluatedAttrs = self.getEvaluatedAttributes(self.data) 
    261261            if evaluatedAttrs == []: 
     
    274274                for u in range(minLength-1, maxLength): 
    275275                    combinations = orngVisFuncts.combinations(evaluatedAttrs[:z], u) 
    276                      
     276 
    277277                    for attrList in combinations: 
    278278                        triedPossibilities += 1 
    279                          
     279 
    280280                        attrs = [evaluatedAttrs[z]] + attrList 
    281281                        diffVals = reduce(operator.mul, [max(1, len(self.data.domain[attr].values)) for attr in attrs]) 
     
    288288                            self.finishEvaluation(triedPossibilities) 
    289289                            return 
    290                          
     290 
    291291                        self.insertItem(val, attrs, self.findTargetIndex(val, max), triedPossibilities) 
    292                          
     292 
    293293                        if self.__class__.__name__ == "OWMosaicOptimization": 
    294294                            self.parentWidget.progressBarSet(100.0*triedPossibilities/float(totalPossibilities)) 
     
    311311    def _Evaluate(self, attrs): 
    312312        newFeature, quality = FeatureByCartesianProduct(self.data, attrs) 
    313          
     313 
    314314        retVal = -1 
    315315        if self.qualityMeasure in [CHI_SQUARE, CRAMERS_PHI]: 
     
    328328                if vals: 
    329329                    retVal = sqrt(retVal / (len(self.data) * vals)) 
    330                      
     330 
    331331        elif self.qualityMeasure == GAIN_RATIO: 
    332332            retVal = orange.MeasureAttribute_gainRatio(newFeature, self.data) 
     
    353353            d = self.data.select([newFeature, self.data.domain.classVar])     # create a dataset that has only this new feature and class info 
    354354 
    355             if not self.cvIndices:             
     355            if not self.cvIndices: 
    356356                if self.testingMethod == PROPORTION_TEST: 
    357357                    pick = orange.MakeRandomIndices2(stratified = orange.MakeRandomIndices.StratifiedIfPossible, p0 = 0.7, randomGenerator = 0) 
     
    360360                    ind = orange.MakeRandomIndicesCV(d, 10, randomGenerator = 0, stratified = orange.MakeRandomIndices.StratifiedIfPossible) 
    361361                    self.cvIndices = [[val == i for val in ind] for i in range(10)] 
    362              
     362 
    363363            acc = 0.0; count = 0 
    364364            for ind in self.cvIndices: 
     
    378378                        count += testClassDist[i] 
    379379            retVal = 100*acc / max(1, float(count)) 
    380              
     380 
    381381        del newFeature, quality 
    382382        return retVal 
    383383 
    384     #  
     384    # 
    385385    # ARGUMENTATION FUNCTIONS 
    386     #  
     386    # 
    387387    def findArguments(self, example = None): 
    388388        self.arguments = dict([(val, []) for val in self.classVals]) 
    389          
     389 
    390390        if not example or len(self.results) == 0: return None, None 
    391391        if not (self.data and self.data.domain.classVar and self.logits and self.classVals): return None, None 
     
    409409                    self.insertArgument(args[val][0], args[val][1], attrList, pos) 
    410410                    qApp.processEvents() 
    411             usedArguments += 1  
     411            usedArguments += 1 
    412412 
    413413        predictions = [] 
     
    429429                value = self.logits[val] + sum([v[0] for v in self.arguments[val]]) 
    430430                predictions.append(e**value / (1 + e**value))       # use predictions from all arguments to classify an example 
    431              
     431 
    432432        classValue = self.data.domain.classVar[predictions.index(max(predictions))] 
    433433        if sum(predictions) == 0: 
     
    466466                    prob *= self.contingencies[attr][example[attr]][i] / float(max(1,sum(self.contingencies[attr][example[attr]].values()))) 
    467467                eps += P_Ci * abs( actualProbabilities[i]/float(lenSubData) - (prob / P_Ci) ) 
    468                                                                                
     468 
    469469            for i in range(len(self.aprioriDistribution)): 
    470470                arguments[self.classVals[i]] = (actualProbabilities[i], (eps, lenSubData)) 
     
    496496        return arguments 
    497497 
    498          
     498 
    499499    # probability estimation function 
    500500    def estimateClassProbabilities(self, data, example, attrList, subData = None, subDataDistribution = None, aprioriDistribution = None, probabilityEstimation = -1, mValue = -1): 
     
    502502        if aprioriDistribution == None: aprioriDistribution = self.aprioriDistribution 
    503503        if mValue == -1:                mValue = self.mValue 
    504          
     504 
    505505        if not subData: 
    506506            attrVals = [example[attr] for attr in attrList] 
     
    513513        lenData = len(data) 
    514514        lenSubData = len(subData) 
    515          
     515 
    516516        # estimate probabilities 
    517517        if probabilityEstimation == RELATIVE: 
     
    571571                        partialVals = sum([abs(val) for val in vals] + errors)        # vals may have all - or all + values. errors are all +. to sum, we must make all vals +. 
    572572                        complexVal = abs(args[key][0]) - args[key][1]       # value and error of the combination of attributes 
    573                         if not sameSign or partialVals >= complexVal:    
     573                        if not sameSign or partialVals >= complexVal: 
    574574                            args.pop(key)   # remove the combination of attributes because a split exists, that produces a more important argument 
    575575                            break 
     
    603603                    arguments.pop(i) 
    604604 
    605              
     605 
    606606    # compute probability that the combination of attribute values in attrValues is significantly different from being independent 
    607607    # see Kononenko: Semi-naive Bayes 
     
    627627 
    628628            indicesToKeep = [val[1] for val in argList[1].values() + argList[2].values() + argList[3].values() + argList[4].values()] 
    629              
     629 
    630630            for i in range(len(arguments))[::-1]:       # we remove all arguments that are not in indicesToKeep 
    631631                if i not in indicesToKeep: 
    632632                    arguments.pop(i) 
    633                  
    634  
    635              
     633 
     634 
     635 
    636636    def getInteractionImportanceProbability(self, attrList, attrValues, subData = None, contingencies = None, aprioriDistribution = None, subDataDistribution = None): 
    637637        assert len(attrList) == len(attrValues) 
     
    660660                N_aj = sum(self.classDistributionsForExample[attrList[j]]) 
    661661                product *= N_ci_aj / N_aj       # P(ci|aj) 
    662              
     662 
    663663            eps += (aprioriDistribution[i]/lenData) * abs((subDataDistribution[i]/lenSubData) - (product*lenData)/aprioriDistribution[i]) 
    664664            #eps += abs((subDataDistribution[i]/lenSubData) - (product*lenData)/aprioriDistribution[i]) 
     
    674674    def getArgumentIndex(self, value, classValue): 
    675675        if len(self.arguments[classValue]) == 0: return 0 
    676          
     676 
    677677        top = 0; bottom = len(self.arguments[classValue]) 
    678678        while (bottom-top) > 1: 
     
    695695        projData = orange.ExampleTable(domain) 
    696696        projData.domain.classVar.distributed = True 
    697          
     697 
    698698        triedIndices = [0]*(len(attrs)) 
    699699        maxVals = [len(val) for val in valueOrder] 
     
    720720            if len(attrs) > 2: xpos += (2 + maxVals[-1]) * triedIndices[-3] # add the space of 3 between each different value of third attribute 
    721721            if len(attrs) > 3: ypos += (4 + maxVals[-2]) * triedIndices[-4] # add the space of 4 between each different value of fourth attribute 
    722              
     722 
    723723        distance = orange.ExamplesDistanceConstructor_Manhattan() 
    724724        distance.normalize = 0 
     
    744744                quotients = map(operator.div, cont[key], apriori) 
    745745                conditions[key] = (dist[key], self.data.domain.classVar.values[quotients.index(max(quotients))], cont[key]) 
    746          
     746 
    747747        domain = orange.Domain([orange.FloatVariable("xVar"), orange.FloatVariable("yVar"), self.data.domain.classVar]) 
    748748        self.weightID = orange.newmetaid() 
    749749        domain.addmeta(self.weightID, orange.FloatVariable("ExampleWeight")) 
    750          
     750 
    751751        # create permutations of attributes and attribute values 
    752752        attrPerms = orngVisFuncts.permutations(range(len(attrs))) 
     
    795795            self.setStatusBarText("") 
    796796            self.parentWidget.progressBarFinished() 
    797              
     797 
    798798        bestPlacements.sort() 
    799799        bestPlacements.reverse() 
     
    816816        if funct(score, self.results[top][SCORE]) == score: 
    817817            return top 
    818         else:  
     818        else: 
    819819            return bottom 
    820820 
     
    822822    def insertItem(self, score, attrList, index, tryIndex, extraInfo = []): 
    823823        self.results.insert(index, (score, attrList, tryIndex, extraInfo)) 
    824          
     824 
    825825        if self.__class__.__name__ == "OWMosaicOptimization": 
    826              
     826 
    827827            self.resultList.insertItem("%.3f : %s" % (score, self.buildAttrString(attrList)), index) 
    828828            self.resultListIndices.insert(index, index) 
     
    831831    def buildAttrString(self, attrList): 
    832832        if len(attrList) == 0: return "" 
    833          
     833 
    834834        strList = attrList[0] 
    835835        for attr in attrList[1:]: 
     
    846846            filter.check[v] = v.name in attrList 
    847847        return filter(temp) 
    848      
    849  
    850     #  
     848 
     849 
     850    # 
    851851    # LOADING, SAVING, ..... 
    852852    # 
    853     # save evaluated projections into a file         
     853    # save evaluated projections into a file 
    854854    def save(self, filename): 
    855855        dict = {} 
     
    858858        dict["dataCheckSum"] = self.data.checksum() 
    859859 
    860         file = open(filename, "wt")         
     860        file = open(filename, "wt") 
    861861        file.write("%s\n" % (str(dict))) 
    862862        for (score, attrList, tryIndex, extraInfo) in self.results: 
     
    878878            else: 
    879879                print "dataset checksum does not agree with the checksum in the projections file. loading anyway" 
    880                  
     880 
    881881        #self.setSettings(settings) 
    882882        for key in settings.keys(): 
     
    891891 
    892892        return ind 
    893          
     893 
    894894 
    895895# ############################################################################# 
     
    901901        if self.Mosaic.__class__.__name__ == "OWMosaicOptimization": 
    902902            self.Mosaic.parentWidget.subsetdataHander(None) 
    903             self.Mosaic.parentWidget.cdata(data) 
     903            self.Mosaic.parentWidget.setData(data) 
    904904        else: 
    905905            self.Mosaic.setData(data) 
    906906 
    907         #if self.Mosaic.__class__.__name__ == "OWMosaicOptimization": self.Mosaic.useTimeLimit = 1    
     907        #if self.Mosaic.__class__.__name__ == "OWMosaicOptimization": self.Mosaic.useTimeLimit = 1 
    908908        self.Mosaic.evaluateProjections() 
    909909        #if self.Mosaic.__class__.__name__ == "OWMosaicOptimization": del self.Mosaic.useTimeLimit 
    910910 
    911911 
    912     # for a given example run argumentation and find out to which class it most often fall         
     912    # for a given example run argumentation and find out to which class it most often fall 
    913913    def __call__(self, example, returnType = orange.GetBoth): 
    914914        # if in widget, also show the example 
     
    922922        if returnType == orange.GetBoth: return classVal, prob 
    923923        else:                            return classVal 
    924          
     924 
    925925 
    926926# ############################################################################# 
     
    931931        self.Mosaic = mosaic 
    932932        self.name = self.Mosaic.learnerName 
    933          
     933 
    934934    def __call__(self, examples, weightID = 0): 
    935935        return MosaicVizRankClassifier(self.Mosaic, examples) 
  • orange/orngVisFuncts.py

    r3413 r3494  
    263263         
    264264    def __call__(self, attribute, data): 
    265         if data.domain[attribute].varType == orange.VarTypes.Discrete: 
    266             print "S2NMeasureMix can not evaluate discrete attributes" 
    267             return -1 
    268          
     265                 
    269266        # if the data changed clear the attribute values 
    270267        if data != self.dataMix: 
     
    285282                newData = mergeClassValues(data, c) 
    286283                for attrIndex in range(len(attrs)): 
     284                    if data.domain[attrIndex].varType == orange.VarTypes.Discrete:      # ignore discrete attributes 
     285                        continue     
    287286                    val = S2NMeasure.__call__(self, attrs[attrIndex], newData) 
    288287                    if statistics[0][attrIndex] == None: 
     
    290289                    else: 
    291290                        aves = [stat[attrIndex].avg for stat in statistics] 
    292                         if max(aves) != aves[classVarIndex] : val = -val 
     291                        if max(aves) != aves[classVarIndex] : 
     292                            val = -val 
    293293                        attrValsList.append((val, attrs[attrIndex])) 
    294294                attrValsList.sort() 
     
    301301                for i in range(len(arr)): 
    302302                    attrPositionsDict[arr[i]].append(i) 
    303  
    304             ableToAdd = 1 
     303             
    305304            numClasses = len(classVar.values) 
    306305            currPos = [0 for i in range(numClasses)] 
    307306            self.sortedAttrList = [] 
     307            ableToAdd = 1 
    308308            while ableToAdd:    # sometimes some attributes are duplicated. in such cases we will add only one instance of such attribute to the list 
    309309                ableToAdd = 0 
    310310                for i in range(numClasses): 
    311311                    pos = currPos[i] 
    312                     while pos < len(cls[i]) and cls[i][pos] == None: pos += 1 
     312                    while pos < len(cls[i]) and cls[i][pos] == None: 
     313                        pos += 1 
    313314                    currPos[i] = pos + 1 
    314                     if pos >= len(cls[i]): continue 
     315                    if pos >= len(cls[i]): 
     316                        continue 
    315317                    ableToAdd = 1 
    316318                     
     
    318320                    self.sortedAttrList.append(attr) 
    319321                    attrPositions = attrPositionsDict[attr]     # get indices in cls where attribute attr is placed 
    320                     for j in range(numClasses): cls[j][attrPositions[j]] = None 
    321                      
     322                    for j in range(numClasses): 
     323                        cls[j][attrPositions[j]] = None 
    322324                     
    323325            count = len(self.sortedAttrList) 
Note: See TracChangeset for help on using the changeset viewer.