Changeset 10860:44fb1a39ded6 in orange for Orange/OrangeWidgets/OWNetworkHist.py
 Timestamp:
 05/07/12 13:40:41 (2 years ago)
 Branch:
 default
 rebase_source:
 796478979ddab536f07ba9b90d4f4ff9e3170605
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

Orange/OrangeWidgets/OWNetworkHist.py
r9671 r10860 13 13 14 14 class OWNetworkHist(): 15 15 16 16 def __init__(self, parent=None, type=0): 17 17 self.parent = parent 18 18 19 19 def addHistogramControls(self, parent=None): 20 20 # set default settings … … 30 30 self.excludeLimit = 1 31 31 self.percentil = 0 32 32 33 33 if parent is None: 34 34 parent = self.controlArea 35 36 boxGeneral = OWGUI.widgetBox(parent, box ="Distance boundaries")37 35 36 boxGeneral = OWGUI.widgetBox(parent, box="Distance boundaries") 37 38 38 OWGUI.lineEdit(boxGeneral, self, "spinLowerThreshold", "Lower:", orientation='horizontal', callback=self.changeLowerSpin, valueType=float) 39 39 OWGUI.lineEdit(boxGeneral, self, "spinUpperThreshold", "Upper:", orientation='horizontal', callback=self.changeUpperSpin, valueType=float) 40 ribg = OWGUI.radioButtonsInBox(boxGeneral, self, "andor", [], orientation='horizontal', callback =self.generateGraph)41 OWGUI.appendRadioButton(ribg, self, "andor", "OR", callback =self.generateGraph)42 b = OWGUI.appendRadioButton(ribg, self, "andor", "AND", callback =self.generateGraph)40 ribg = OWGUI.radioButtonsInBox(boxGeneral, self, "andor", [], orientation='horizontal', callback=self.generateGraph) 41 OWGUI.appendRadioButton(ribg, self, "andor", "OR", callback=self.generateGraph) 42 b = OWGUI.appendRadioButton(ribg, self, "andor", "AND", callback=self.generateGraph) 43 43 b.setEnabled(False) 44 44 OWGUI.spin(boxGeneral, self, "kNN", 0, 1000, 1, label="kNN:", orientation='horizontal', callback=self.generateGraph) 45 45 OWGUI.doubleSpin(boxGeneral, self, "percentil", 0, 100, 0.1, label="Percentil:", orientation='horizontal', callback=self.setPercentil, callbackOnReturn=1) 46 46 47 47 # Options 48 48 self.attrColor = "" 49 ribg = OWGUI.radioButtonsInBox(parent, self, "netOption", [], "Options", callback =self.generateGraph)50 OWGUI.appendRadioButton(ribg, self, "netOption", "All vertices", callback =self.generateGraph)51 OWGUI.appendRadioButton(ribg, self, "netOption", "Exclude small components", callback =self.generateGraph)52 OWGUI.spin(OWGUI.indentedBox(ribg), self, "excludeLimit", 1, 100, 1, label="Less vertices than: ", callback =(lambda h=True: self.generateGraph(h)))53 OWGUI.appendRadioButton(ribg, self, "netOption", "Largest connected component only", callback =self.generateGraph)49 ribg = OWGUI.radioButtonsInBox(parent, self, "netOption", [], "Options", callback=self.generateGraph) 50 OWGUI.appendRadioButton(ribg, self, "netOption", "All vertices", callback=self.generateGraph) 51 OWGUI.appendRadioButton(ribg, self, "netOption", "Exclude small components", callback=self.generateGraph) 52 OWGUI.spin(OWGUI.indentedBox(ribg), self, "excludeLimit", 1, 100, 1, label="Less vertices than: ", callback=(lambda h=True: self.generateGraph(h))) 53 OWGUI.appendRadioButton(ribg, self, "netOption", "Largest connected component only", callback=self.generateGraph) 54 54 OWGUI.appendRadioButton(ribg, self, "netOption", "Connected component with vertex") 55 55 self.attribute = None 56 self.attributeCombo = OWGUI.comboBox(ribg, self, "attribute", box ="Filter attribute")#, callback=self.setVertexColor)57 58 ribg = OWGUI.radioButtonsInBox(parent, self, "dstWeight", [], "Distance > Weight", callback =self.generateGraph)59 OWGUI.appendRadioButton(ribg, self, "dstWeight", "Weight := distance", callback =self.generateGraph)60 OWGUI.appendRadioButton(ribg, self, "dstWeight", "Weight := 1  distance", callback =self.generateGraph)61 56 self.attributeCombo = OWGUI.comboBox(ribg, self, "attribute", box="Filter attribute")#, callback=self.setVertexColor) 57 58 ribg = OWGUI.radioButtonsInBox(parent, self, "dstWeight", [], "Distance > Weight", callback=self.generateGraph) 59 OWGUI.appendRadioButton(ribg, self, "dstWeight", "Weight := distance", callback=self.generateGraph) 60 OWGUI.appendRadioButton(ribg, self, "dstWeight", "Weight := 1  distance", callback=self.generateGraph) 61 62 62 self.label = '' 63 63 self.searchString = OWGUI.lineEdit(self.attributeCombo.box, self, "label", callback=self.setSearchStringTimer, callbackOnType=True) 64 64 self.searchStringTimer = QTimer(self) 65 65 self.connect(self.searchStringTimer, SIGNAL("timeout()"), self.generateGraph) 66 66 67 67 if str(self.netOption) != '3': 68 68 self.attributeCombo.box.setEnabled(False) 69 69 70 70 def setPercentil(self): 71 71 self.spinLowerThreshold = self.histogram.minValue 72 72 net = orngNetwork.Network(self.matrix.dim, 0) 73 lower, upper = net.getDistanceMatrixThreshold(self.matrix, self.percentil /100)73 lower, upper = net.getDistanceMatrixThreshold(self.matrix, self.percentil / 100) 74 74 self.spinUpperThreshold = upper 75 75 self.generateGraph() 76 76 77 77 def enableAttributeSelection(self): 78 78 self.attributeCombo.box.setEnabled(True) 79 79 80 80 def setSearchStringTimer(self): 81 81 self.searchStringTimer.stop() … … 84 84 def setMatrix(self, data): 85 85 if data == None: return 86 86 87 87 self.matrix = data 88 88 # draw histogram … … 91 91 #print "values:",values 92 92 self.histogram.setValues(values) 93 93 94 94 low = min(values) 95 95 upp = max(values) … … 100 100 if (self.matrix != None): 101 101 if hasattr(self.matrix, "items"): 102 102 103 103 if isinstance(self.matrix.items, orange.ExampleTable): 104 104 vars = list(self.matrix.items.domain.variables) 105 105 106 106 metas = self.matrix.items.domain.getmetas(0) 107 107 for i, var in metas.iteritems(): 108 108 vars.append(var) 109 109 110 110 self.icons = self.createAttributeIconDict() 111 111 112 112 for var in vars: 113 113 try: 114 114 self.attributeCombo.addItem(self.icons[var.varType], unicode(var.name)) 115 115 except: 116 print " error adding ", var, " to the attribute combo"116 print "Error adding ", var, " to the attribute combo." 117 117 118 118 def changeLowerSpin(self): 119 119 self.percentil = 0 120 120 121 121 if self.spinLowerThreshold < self.histogram.minValue: 122 122 self.spinLowerThreshold = self.histogram.minValue 123 123 elif self.spinLowerThreshold > self.histogram.maxValue: 124 124 self.spinLowerThreshold = self.histogram.maxValue 125 125 126 126 if self.spinLowerThreshold >= self.spinUpperThreshold: 127 127 self.spinUpperThreshold = self.spinLowerThreshold 128 129 self.generateGraph() 130 128 129 self.generateGraph() 130 131 131 def changeUpperSpin(self): 132 132 self.percentil = 0 133 133 134 134 if self.spinUpperThreshold < self.histogram.minValue: 135 135 self.spinUpperThreshold = self.histogram.minValue 136 136 elif self.spinUpperThreshold > self.histogram.maxValue: 137 137 self.spinUpperThreshold = self.histogram.maxValue 138 138 139 139 if self.spinUpperThreshold <= self.spinLowerThreshold: 140 140 self.spinLowerThreshold = self.spinUpperThreshold 141 142 self.generateGraph() 143 144 def generateGraph(self, N_changed =False):141 142 self.generateGraph() 143 144 def generateGraph(self, N_changed=False): 145 145 self.searchStringTimer.stop() 146 146 self.attributeCombo.box.setEnabled(False) … … 148 148 matrix = None 149 149 self.warning('') 150 150 151 151 if N_changed: 152 152 self.netOption = 1 153 153 154 154 if self.matrix == None: 155 155 self.infoa.setText("No data loaded.") 156 156 self.infob.setText("") 157 157 return 158 158 159 159 #print len(self.histogram.yData), len(self.histogram.xData) 160 nEdgesEstimate = 2 * sum([self.histogram.yData[i] for i, e in enumerate(self.histogram.xData) if self.spinLowerThreshold <= e <= self.spinUpperThreshold])161 160 nEdgesEstimate = 2 * sum([self.histogram.yData[i] for i, e in enumerate(self.histogram.xData) if self.spinLowerThreshold <= e <= self.spinUpperThreshold]) 161 162 162 if nEdgesEstimate > 200000: 163 163 self.graph = None … … 168 168 graph = orngNetwork.Network(self.matrix.dim, 0) 169 169 matrix = self.matrix 170 171 if hasattr(self.matrix, "items"): 170 171 if hasattr(self.matrix, "items"): 172 172 if type(self.matrix.items) == orange.ExampleTable: 173 173 #graph.setattr("items", self.data.items) … … 178 178 #graph.setattr("items", list(items)) 179 179 graph.items = list(items) 180 180 181 181 # set the threshold 182 182 # set edges where distance is lower than threshold 183 183 184 184 self.warning(0) 185 185 if self.kNN >= self.matrix.dim: … … 187 187 nedges = graph.fromDistanceMatrix(self.matrix, self.spinLowerThreshold, self.spinUpperThreshold, min(self.kNN, self.matrix.dim  1), self.andor) 188 188 edges = graph.getEdges() 189 189 190 190 #print graph.nVertices, self.matrix.dim 191 191 192 192 if self.dstWeight == 1: 193 193 if graph.directed: 194 for u, v in edges:194 for u, v in edges: 195 195 foo = 1 196 if str(graph[u, v]) != "0":197 foo = 1.0  float(graph[u, v])198 199 graph[u, v] = foo200 else: 201 for u, v in edges:196 if str(graph[u, v]) != "0": 197 foo = 1.0  float(graph[u, v]) 198 199 graph[u, v] = foo 200 else: 201 for u, v in edges: 202 202 if u <= v: 203 203 foo = 1 204 if str(graph[u, v]) != "0":205 foo = 1.0  float(graph[u, v])206 207 graph[u, v] = foo208 204 if str(graph[u, v]) != "0": 205 foo = 1.0  float(graph[u, v]) 206 207 graph[u, v] = foo 208 209 209 n = len(edges) 210 210 #print 'self.netOption',self.netOption … … 213 213 components = [x for x in graph.getConnectedComponents() if len(x) > self.excludeLimit] 214 214 if len(components) > 0: 215 include = reduce(lambda x, y: x+y, components)215 include = reduce(lambda x, y: x + y, components) 216 216 if len(include) > 1: 217 217 self.graph = orngNetwork.Network(graph.getSubGraph(include)) … … 240 240 if self.attributeCombo.currentText() != '' and self.label != '': 241 241 components = graph.getConnectedComponents() 242 242 243 243 txt = self.label.lower() 244 244 #print 'txt:',txt … … 252 252 if len(component) > 0: 253 253 vertices.extend(component) 254 254 255 255 if len(vertices) > 0: 256 256 #print "n vertices:", len(vertices), "n set vertices:", len(set(vertices)) … … 260 260 else: 261 261 self.graph = graph 262 262 263 263 if matrix != None: 264 matrix.items 264 matrix.items = self.graph.items 265 265 self.graph_matrix = matrix 266 266 267 267 self.pconnected = nedges 268 268 self.nedges = n … … 273 273 if hasattr(self, "infoc"): 274 274 self.infoc.setText("%d edges (%d average)" % (n, n / float(self.matrix.dim))) 275 275 276 276 #print 'self.graph:',self.graph+ 277 277 if hasattr(self, "sendSignals"): 278 278 self.sendSignals() 279 279 280 280 self.histogram.setBoundary(self.spinLowerThreshold, self.spinUpperThreshold)
Note: See TracChangeset
for help on using the changeset viewer.