def cmatrix(self, matrix=None): self.closeContext() self.origMatrix = matrix self.data = data = None if matrix: self.data = data = getattr(matrix, "items", range(matrix.dim)) matrix.matrixType = orange.SymMatrix.Symmetric self.graph.ColorAttr = 0 self.graph.SizeAttr = 0 self.graph.ShapeAttr = 0 self.graph.NameAttr = 0 self.graph.closestPairs = None if isinstance(data, orange.ExampleTable): self.setExampleTable(data) elif isinstance(data, orange.VarList): self.setVarList(data) elif data is not None: self.setList(data) if matrix: self.mds = orngMDS.MDS(matrix) self.mds.points = numpy.random.random( size=[self.mds.n, self.mds.dim]) self.mds.getStress() self.stress = self.getAvgStress( self.stressFunc[self.StressFunc][1]) if data and type(data) == orange.ExampleTable: self.openContext("", self.data) self.graph.setData(self.mds, self.colors, self.sizes, self.shapes, self.names, self.selectedInput) else: self.graph.clear()
normalized_vecs = [vec.hat() for vec in vecs] def acos(x): if x > 1: return _acos(1) if x < -1: return _acos(-1) return _acos(x) concept_labels = aspace.weighted_u.label_list(0) print 'dist' distance = orange.SymMatrix(n) for i in range(n): for j in range(i + 1): distance[i, j] = acos(normalized_vecs[i] * normalized_vecs[j]) print 'setup' mds = orngMDS.MDS(distance) print 'run' mds.run(100) def get_mds_matrix(): array = np.array(mds.points) matrix = LabeledView(DenseTensor(array), [aspace.u.label_list(0), None]) return matrix mds_matrix = get_picklecached_thing('aspace_mds.pickle', get_mds_matrix)
def mdsComponents(self, mdsSteps, mdsRefresh, callbackProgress=None, callbackUpdateCanvas=None, torgerson=0, minStressDelta=0, avgLinkage=False, rotationOnly=False, mdsType=MdsType.componentMDS, scalingRatio=0, mdsFromCurrentPos=0): """Position the network components according to similarities among them.""" if self.vertexDistance == None: self.information('Set distance matrix to input signal') return 1 if self.graph == None: return 1 if self.vertexDistance.dim != self.graph.nVertices: return 1 self.mdsComponents = self.graph.getConnectedComponents() self.mdsRefresh = mdsRefresh self.mdsStep = 0 self.stopMDS = 0 self.vertexDistance.matrixType = orange.SymMatrix.Symmetric self.diag_coors = math.sqrt( (min(self.graph.coors[0]) - max(self.graph.coors[0]))**2 + (min(self.graph.coors[1]) - max(self.graph.coors[1]))**2) self.rotationOnly = rotationOnly self.mdsType = mdsType self.scalingRatio = scalingRatio w = max(self.graph.coors[0]) - min(self.graph.coors[0]) h = max(self.graph.coors[1]) - min(self.graph.coors[1]) d = math.sqrt(w**2 + h**2) #d = math.sqrt(w*h) e = [ math.sqrt((self.graph.coors[0][u] - self.graph.coors[0][v])**2 + (self.graph.coors[1][u] - self.graph.coors[1][v])**2) for (u, v) in self.graph.getEdges() ] self.mdsScaleRatio = d / sum(e) * float(len(e)) #print d / sum(e) * float(len(e)) if avgLinkage: matrix = self.vertexDistance.avgLinkage(self.mdsComponents) else: matrix = self.vertexDistance #if self.mds == None: self.mds = orngMDS.MDS(matrix) if mdsFromCurrentPos: if avgLinkage: for u, c in enumerate(self.mdsComponents): x = sum(self.graph.coors[0][c]) / len(c) y = sum(self.graph.coors[1][c]) / len(c) self.mds.points[u][0] = x self.mds.points[u][1] = y else: for u in range(self.graph.nVertices): self.mds.points[u][0] = self.graph.coors[0][u] self.mds.points[u][1] = self.graph.coors[1][u] # set min stress difference between 0.01 and 0.00001 self.minStressDelta = minStressDelta self.callbackUpdateCanvas = callbackUpdateCanvas self.callbackProgress = callbackProgress if torgerson: self.mds.Torgerson() self.mds.optimize(mdsSteps, orngMDS.SgnRelStress, self.minStressDelta, progressCallback=self.mdsCallback) self.mdsUpdateData(self.mdsComponents, self.mds, callbackUpdateCanvas) if callbackProgress != None: callbackProgress(self.mds.avgStress, self.mdsStep) del self.rotationOnly del self.diag_coors del self.mdsRefresh del self.mdsStep #del self.mds del self.mdsComponents del self.minStressDelta del self.callbackUpdateCanvas del self.callbackProgress del self.mdsType del self.mdsScaleRatio del self.scalingRatio return 0
# Description: Example of advanced use of MDS # Category: association # Classes: orngMDS.MDS # Referenced: orngMDS.htm # Uses: iris.tab import orange, orngMDS, math data = orange.ExampleTable("../datasets/iris.tab") dist = orange.ExamplesDistanceConstructor_Euclidean(data) matrix = orange.SymMatrix(len(data)) for i in range(len(data) - 1): for j in range(i + 1, len(data)): matrix[i, j] = dist(data[i], data[j]) mds = orngMDS.MDS(matrix) #mds.Torgerson() mds.getStress(orngMDS.KruskalStress) i = 0 while 100 > i: i += 1 oldStress = mds.avgStress for j in range(10): mds.SMACOFstep() mds.getStress(orngMDS.KruskalStress) if oldStress * 1e-3 > math.fabs(oldStress - mds.avgStress): break for (p, e) in zip(mds.points, data): print "<%4.2f, %4.2f> %s" % (p[0], p[1], e)