source: orange/orange/OrangeWidgets/Visualize/OWSurveyPlot.py @ 9272:8e98cfb85c3c

Revision 9272:8e98cfb85c3c, 9.5 KB checked in by ales_erjavec <ales.erjavec@…>, 2 years ago (diff)

More fixes to reporting.

Line 
1"""
2<name>Survey Plot</name>
3<description>Survey plot (multiattribute) visualization.</description>
4<contact>Gregor Leban (gregor.leban@fri.uni-lj.si)</contact>
5<icon>icons/SurveyPlot.png</icon>
6<priority>3250</priority>
7"""
8# OWSurveyPlot.py
9#
10# Show data using survey plot visualization method
11#
12from OWVisWidget import *
13from OWSurveyPlotGraph import *
14import OWColorPalette
15import orngVisFuncts
16import OWGUI
17
18###########################################################################################
19##### WIDGET : Survey plot visualization
20###########################################################################################
21class OWSurveyPlot(OWVisWidget):
22    settingsList = ["graph.exampleTracking", "graph.enabledLegend",
23                    "graph.tooltipKind", "showAllAttributes", "colorSettings", "selectedSchemaIndex"]
24    attributeContOrder = ["Unordered","ReliefF", "Fisher discriminant"]
25    attributeDiscOrder = ["Unordered","ReliefF","GainRatio"]
26    contextHandlers = {"": DomainContextHandler("", [ContextField("shownAttributes", DomainContextHandler.RequiredList, selected="selectedShown", reservoir="hiddenAttributes")])}
27
28    def __init__(self,parent=None, signalManager = None):
29        OWWidget.__init__(self, parent, signalManager, "Survey Plot", TRUE)
30
31        self.inputs = [("Examples", ExampleTable, self.setData, Default), ("Attribute Selection List", AttributeList, self.setShownAttributes)]
32        self.outputs = [("Attribute Selection List", AttributeList)]
33
34        #add a graph widget
35        self.graph = OWSurveyPlotGraph(self.mainArea)
36        self.mainArea.layout().addWidget(self.graph)
37        self.connect(self.graphButton, SIGNAL("clicked()"), self.graph.saveToFile)
38
39        #set default settings
40        self.data = None
41        self.showAllAttributes = 0
42        self.graph.exampleTracking = 0
43        self.graph.enabledLegend = 1
44        self.graph.tooltipKind = 1
45        self.attributeSelectionList = None
46        self.graphCanvasColor = str(QColor(Qt.white).name())
47        self.primaryAttribute = "(None)"
48        self.secondaryAttribute = "(None)"
49        self.colorSettings = None
50        self.selectedSchemaIndex = 0
51
52        #load settings
53        self.loadSettings()
54
55        #GUI
56        self.tabs = OWGUI.tabWidget(self.controlArea)
57        self.GeneralTab = OWGUI.createTabPage(self.tabs, "Main")
58        self.SettingsTab = OWGUI.createTabPage(self.tabs, "Settings")
59
60
61        #add controls to self.controlArea widget
62        self.sortingAttrGB = OWGUI.widgetBox(self.GeneralTab, "Sorting")
63        self.primaryAttrCombo = OWGUI.comboBoxWithCaption(self.sortingAttrGB, self, "primaryAttribute", label = '1st:', items = ["(None)"], sendSelectedValue = 1, valueType = str, callback = self.sortingClick, labelWidth = 25)
64        self.secondaryAttrCombo = OWGUI.comboBoxWithCaption(self.sortingAttrGB, self, "secondaryAttribute", label = '2nd:', items = ["(None)"], sendSelectedValue = 1, valueType = str, callback = self.sortingClick, labelWidth = 25)
65
66        self.createShowHiddenLists(self.GeneralTab, callback = self.updateGraph)
67
68        # ##################################
69        # survey plot settings
70        box = OWGUI.widgetBox(self.SettingsTab, "Visual settings")
71        OWGUI.checkBox(box, self, "graph.exampleTracking", "Example tracking", callback = self.updateGraph)
72        OWGUI.checkBox(box, self, "graph.enabledLegend", "Show legend", callback = self.updateGraph)
73
74        box = OWGUI.widgetBox(self.SettingsTab, "Tooltips settings")
75        OWGUI.comboBox(box, self, "graph.tooltipKind", items = ["Don't Show Tooltips", "Show Visible Attributes", "Show All Attributes"], callback = self.updateGraph)
76
77        self.colorButtonsBox = OWGUI.widgetBox(self.SettingsTab, "Colors", orientation = "horizontal")
78        OWGUI.button(self.colorButtonsBox, self, "Set Colors", self.setColors, tooltip = "Set the canvas background color, grid color and color palette for coloring continuous variables", debuggingEnabled = 0)
79        self.SettingsTab.layout().addStretch(100)
80
81        self.icons = self.createAttributeIconDict()
82
83        dlg = self.createColorDialog()
84        self.graph.contPalette = dlg.getContinuousPalette("contPalette")
85        self.graph.discPalette = dlg.getDiscretePalette("discPalette")
86        self.graph.setCanvasBackground(dlg.getColor("Canvas"))
87        self.graph.gridCurve.setPen(QPen(dlg.getColor("Grid")))
88
89        #self.graph.setCanvasBackground(QColor(self.graphCanvasColor))
90        self.cbShowAllAttributes()
91        self.resize(750,700)
92
93    # #####################
94    def setSortCombo(self):
95        items = [str(self.primaryAttrCombo.itemText(i)) for i in range(1, self.primaryAttrCombo.count())]
96        attrs = self.graph.dataDomain and [attr.name for attr in self.graph.dataDomain]
97        if items == attrs:
98            return
99
100        self.primaryAttrCombo.clear()
101        self.secondaryAttrCombo.clear()
102        self.primaryAttrCombo.addItem("(None)")
103        self.secondaryAttrCombo.addItem("(None)")
104        if not self.graph.haveData: return
105        for attr in self.graph.dataDomain:
106            self.primaryAttrCombo.addItem(self.icons[attr.varType], attr.name)
107            self.secondaryAttrCombo.addItem(self.icons[attr.varType], attr.name)
108        self.primaryAttribute = "(None)"
109        self.secondaryAttribute = "(None)"
110
111    def updateGraph(self, *args):
112        self.graph.updateData(self.getShownAttributeList())
113
114
115    # set combo box values with attributes that can be used for coloring the data
116    def sortingClick(self):
117        attrs = [self.primaryAttribute, self.secondaryAttribute]
118        while "(None)" in attrs: attrs.remove("(None)")
119        if attrs and self.data:
120            self.data.sort(attrs)
121
122        self.graph.setData(self.data, sortValuesForDiscreteAttrs = 0, skipIfSame = 0)
123        self.updateGraph()
124
125
126    # receive new data and update all fields
127    def setData(self, data):
128        if data and (len(data) == 0 or len(data.domain) == 0):
129            data = None
130        if self.data and data and self.data.checksum() == data.checksum():
131            return    # check if the new data set is the same as the old one
132
133        self.closeContext()
134        sameDomain = self.data and data and data.domain.checksum() == self.data.domain.checksum() # preserve attribute choice if the domain is the same
135        self.data = data
136
137        self.graph.setData(self.data)
138        if self.graph.dataHasDiscreteClass:
139            self.graph.discPalette.setNumberOfColors(len(self.graph.dataDomain.classVar.values))
140        if not sameDomain:
141            self.setShownAttributes(self.attributeSelectionList)
142            self.resetAttrManipulation()
143            self.setSortCombo()
144        self.openContext("", self.data)
145        self.resetAttrManipulation()
146        self.updateGraph()
147
148    ####### SELECTION signal ################################
149    # receive info about which attributes to show
150    def setShownAttributes(self, attributeSelectionList):
151        self.attributeSelectionList = attributeSelectionList
152        if self.graph.haveData and self.attributeSelectionList and 0 not in [self.graph.attributeNameIndex.has_key(attr) for attr in self.attributeSelectionList]:
153            self.setShownAttributeList(self.attributeSelectionList)
154        else:
155            self.setShownAttributeList()
156
157    # update attribute ordering
158    def updateShownAttributeList(self):
159        self.setShownAttributeList()
160        self.updateGraph()
161
162    def sendShownAttributes(self):
163        self.send("Attribute Selection List", [a[0] for a in self.shownAttributes])
164
165    # just tell the graph to hide the selected rectangle
166    def enterEvent(self, e):
167        if self.graph.selectedRectangle:
168            self.graph.selectedRectangle.detach()
169            self.graph.selectedRectangle = None
170            self.graph.replot()
171
172    def setColors(self):
173        dlg = self.createColorDialog()
174        if dlg.exec_():
175            self.colorSettings = dlg.getColorSchemas()
176            self.selectedSchemaIndex = dlg.selectedSchemaIndex
177            self.graph.contPalette = dlg.getContinuousPalette("contPalette")
178            self.graph.discPalette = dlg.getDiscretePalette("discPalette")
179            self.graph.setCanvasBackground(dlg.getColor("Canvas"))
180            self.graph.gridCurve.setPen(QPen(dlg.getColor("Grid")))
181            self.updateGraph()
182
183    def createColorDialog(self):
184        c = OWColorPalette.ColorPaletteDlg(self, "Color palette")
185        c.createDiscretePalette("discPalette", "Discrete Palette")
186        c.createContinuousPalette("contPalette", "Continuous Palette")
187        box = c.createBox("otherColors", "Other Colors")
188        c.createColorButton(box, "Canvas", "Canvas color", Qt.white)
189        box.layout().addSpacing(5)
190        c.createColorButton(box, "Grid", "Grid color", Qt.black)
191        box.layout().addSpacing(5)
192        c.setColorSchemas(self.colorSettings, self.selectedSchemaIndex)
193        return c
194
195    def sendReport(self):
196        self.reportSettings("",
197                            [("Sorting", ", ".join([x for x in (self.primaryAttribute, self.secondaryAttribute) if x != "(None)"]) or "None"), 
198#                             ("Continuous attribute ordering", self.attrContOrder),
199#                             ("Discrete attribute ordering", self.attrDiscOrder)
200                             ])
201        self.reportRaw("<br/>")
202        self.reportImage(self.graph.saveToFileDirect, QSize(400, 400))
203
204#test widget appearance
205if __name__=="__main__":
206    a=QApplication(sys.argv)
207    ow=OWSurveyPlot()
208    ow.show()
209    data = orange.ExampleTable(r"e:\Development\Orange Datasets\UCI\wine.tab")
210    ow.setData(data)
211    a.exec_()
Note: See TracBrowser for help on using the repository browser.