Changeset 7287:66f0c13befb9 in orange
 Timestamp:
 02/03/11 00:30:15 (3 years ago)
 Branch:
 default
 Convert:
 25fa4cfa54eebab8ced0ea120ead6f65d0cd6362
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

orange/Orange/network/__init__.py
r7282 r7287 159 159 label = "" 160 160 161 fp.write("\tnode\n\t[\n\t\tid\t%d\n\t\tlabel\t\"%s\"\n\t]\n" % (v, label)) 161 fp.write("\tnode\n\t[\n\t\tid\t%d\n\t\tlabel\t\"%s\"\n\t]\n" % 162 (v, label)) 162 163 163 164 for u,v in self.getEdges(): … … 170 171 self.items.save(name + "_items.tab") 171 172 172 if hasattr(self, 'links') and self.links != None and len(self.links) > 0: 173 if hasattr(self, 'links') and self.links != None and \ 174 len(self.links) > 0: 173 175 (name, ext) = os.path.splitext(fp.name) 174 176 self.links.save(name + "_links.tab") … … 182 184 """ 183 185 name = '' 184 fp.write('### This file was generated with Orange Network Visualizer ### \n\n\n')186 fp.write('### Generated with Orange Network Visualizer ### \n\n\n') 185 187 if name == '': 186 188 fp.write('*Network ' + '"no name" \n\n') … … 243 245 self.items.save(name + "_items.tab") 244 246 245 if hasattr(self, 'links') and self.links != None and len(self.links) > 0: 247 if hasattr(self, 'links') and self.links != None \ 248 and len(self.links) > 0: 246 249 (name, ext) = os.path.splitext(fp.name) 247 250 self.links.save(name + "_links.tab") … … 284 287 285 288 class NetworkOptimization(orangeom.NetworkOptimization): 286 """main class for performing network layout optimization. Network structure is defined in orangeom.Network class.""" 289 290 """main class for performing network layout optimization. Network structure 291 is defined in orangeom.Network class. 292 293 """ 287 294 288 295 def __init__(self, network=None, name="None"): … … 319 326 320 327 for u in component: 321 u_ = numpy.array([self.graph.coors[0][u], self.graph.coors[1][u]]) 328 u_ = numpy.array([self.graph.coors[0][u], 329 self.graph.coors[1][u]]) 322 330 force = numpy.array([0.0, 0.0]) 323 331 for v in outer_vertices: 324 v_ = numpy.array([self.graph.coors[0][v], self.graph.coors[1][v]]) 332 v_ = numpy.array([self.graph.coors[0][v], 333 self.graph.coors[1][v]]) 325 334 d = self.vertexDistance[u, v] 326 335 norm = numpy.linalg.norm(v_  u_) … … 342 351 def collapse(self): 343 352 if len(self.graph.getNodes(1)) > 0: 344 nodes = list(set(range(self.graph.nVertices))  set(self.graph.getNodes(1))) 353 nodes = list(set(range(self.graph.nVertices))  \ 354 set(self.graph.getNodes(1))) 345 355 346 356 if len(nodes) > 0: … … 371 381 #print graphstomerge 372 382 #print used 373 subgraph = orangeom.Network(subgraph.getSubGraphMergeClusters(graphstomerge)) 383 subgraph = orangeom.Network( 384 subgraph.getSubGraphMergeClusters(graphstomerge)) 374 385 375 386 nodescomp = list(set(range(self.graph.nVertices))  used) … … 460 471 self.graph.coors[1][component] = y + y_center 461 472 462 def rotateComponents(self, maxSteps=100, minMoment=0.000000001, callbackProgress=None, callbackUpdateCanvas=None): 473 def rotateComponents(self, maxSteps=100, minMoment=0.000000001, 474 callbackProgress=None, callbackUpdateCanvas=None): 463 475 """Rotate the network components using a spring model.""" 464 476 if self.vertexDistance == None: … … 474 486 475 487 # rotate only components with more than one vertex 476 components = [component for component in self.graph.getConnectedComponents() if len(component) > 1] 488 components = [component for component \ 489 in self.graph.getConnectedComponents() \ 490 if len(component) > 1] 477 491 vertices = set(range(self.graph.nVertices)) 478 492 step = 0 … … 480 494 temperature = [[30.0, 1] for i in range(len(components))] 481 495 dirChange = [0] * len(components) 482 while step < maxSteps and (max(M) > minMoment or min(M) < minMoment) and not self.stopRotate: 496 while step < maxSteps and (max(M) > minMoment or min(M) < minMoment) \ 497 and not self.stopRotate: 483 498 M = [0] * len(components) 484 499 … … 505 520 L = [(u_x  v_x), (u_y  v_y)] 506 521 R = [(u_x  x_center), (u_y  y_center)] 507 e = math.sqrt((v_x  x_center) ** 2 + (v_y  y_center) ** 2) 522 e = math.sqrt((v_x  x_center) ** 2 + \ 523 (v_y  y_center) ** 2) 508 524 509 525 M[i] += (1  d) / (e ** 2) * numpy.cross(R, L) … … 537 553 self.rotateVertices(components, phi) 538 554 if callbackUpdateCanvas: callbackUpdateCanvas() 539 if callbackProgress : callbackProgress(min([dirChange[i] for i in range(len(dirChange)) if M[i] != 0]), 9) 555 if callbackProgress : callbackProgress(min([dirChange[i] for i \ 556 in range(len(dirChange)) if M[i] != 0]), 9) 540 557 step += 1 541 558 … … 546 563 y_mds = [] 547 564 phi = [None] * len(components) 548 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) 565 self.diag_coors = math.sqrt(( \ 566 min(self.graph.coors[0])  max(self.graph.coors[0]))**2 + \ 567 (min(self.graph.coors[1])  max(self.graph.coors[1]))**2) 549 568 550 569 if self.mdsType == MdsType.MDS: … … 570 589 y_avg_mds = sum(y) / len(y) 571 590 # compute rotation angle 572 c = [numpy.linalg.norm(numpy.cross(mds.points[u], [self.graph.coors[0][u],self.graph.coors[1][u]])) for u in component] 573 n = [numpy.vdot([self.graph.coors[0][u],self.graph.coors[1][u]], [self.graph.coors[0][u],self.graph.coors[1][u]]) for u in component] 591 c = [numpy.linalg.norm(numpy.cross(mds.points[u], \ 592 [self.graph.coors[0][u],self.graph.coors[1][u]])) \ 593 for u in component] 594 n = [numpy.vdot([self.graph.coors[0][u], \ 595 self.graph.coors[1][u]], \ 596 [self.graph.coors[0][u], \ 597 self.graph.coors[1][u]]) for u in component] 574 598 phi[i] = sum(c) / sum(n) 575 599 #print phi … … 584 608 y_mds.append(y_avg_mds) 585 609 586 component_props.append((x_avg_graph, y_avg_graph, x_avg_mds, y_avg_mds, phi)) 610 component_props.append((x_avg_graph, y_avg_graph, \ 611 x_avg_mds, y_avg_mds, phi)) 587 612 588 613 w = max(self.graph.coors[0])  min(self.graph.coors[0]) … … 625 650 for i in range(len(components)): 626 651 for j in range(i + 1, len(components)): 627 x_avg_graph_i, y_avg_graph_i, x_avg_mds_i, y_avg_mds_i, phi_i = component_props[i] 628 x_avg_graph_j, y_avg_graph_j, x_avg_mds_j, y_avg_mds_j, phi_j = component_props[j] 629 e_fr += math.sqrt((x_avg_graph_ix_avg_graph_j)**2 + (y_avg_graph_iy_avg_graph_j)**2) 652 x_avg_graph_i, y_avg_graph_i, x_avg_mds_i, \ 653 y_avg_mds_i, phi_i = component_props[i] 654 x_avg_graph_j, y_avg_graph_j, x_avg_mds_j, \ 655 y_avg_mds_j, phi_j = component_props[j] 656 e_fr += math.sqrt((x_avg_graph_ix_avg_graph_j)**2 + \ 657 (y_avg_graph_iy_avg_graph_j)**2) 630 658 e_count += 1 631 659 self.mdsScaleRatio = e_fr / e_count … … 647 675 self.mdsScaleRatio = e_fr / e_count 648 676 649 diag_mds = math.sqrt((max(x_mds)  min(x_mds))**2 + (max(y_mds)  min(y_mds))**2) 677 diag_mds = math.sqrt((max(x_mds)  min(x_mds))**2 + (max(y_mds)  \ 678 min(y_mds))**2) 650 679 e = [math.sqrt((self.graph.coors[0][u]  self.graph.coors[0][v])**2 + 651 680 (self.graph.coors[1][u]  self.graph.coors[1][v])**2) for … … 693 722 for i in range(len(components)): 694 723 for j in range(i + 1, len(components)): 695 x_avg_graph_i, y_avg_graph_i, x_avg_mds_i, y_avg_mds_i, phi_i = component_props[i] 696 x_avg_graph_j, y_avg_graph_j, x_avg_mds_j, y_avg_mds_j, phi_j = component_props[j] 697 e_mds += math.sqrt((x_avg_mds_ix_avg_mds_j)**2 + (y_avg_mds_iy_avg_mds_j)**2) 724 x_avg_graph_i, y_avg_graph_i, x_avg_mds_i, \ 725 y_avg_mds_i, phi_i = component_props[i] 726 x_avg_graph_j, y_avg_graph_j, x_avg_mds_j, \ 727 y_avg_mds_j, phi_j = component_props[j] 728 e_mds += math.sqrt((x_avg_mds_ix_avg_mds_j)**2 + \ 729 (y_avg_mds_iy_avg_mds_j)**2) 698 730 e_count += 1 699 731 r = self.mdsScaleRatio / e_mds * e_count … … 717 749 for i in range(len(components)): 718 750 component = components[i] 719 x_avg_graph, y_avg_graph, x_avg_mds, y_avg_mds, phi = component_props[i] 751 x_avg_graph, y_avg_graph, x_avg_mds, \ 752 y_avg_mds, phi = component_props[i] 720 753 721 754 # if phi[i]: # rotate vertices … … 729 762 # translate vertices 730 763 if not self.rotationOnly: 731 self.graph.coors[0][component] = (self.graph.coors[0][component]  x_avg_graph) / r + x_avg_mds 732 self.graph.coors[1][component] = (self.graph.coors[1][component]  y_avg_graph) / r + y_avg_mds 764 self.graph.coors[0][component] = \ 765 (self.graph.coors[0][component]  x_avg_graph) / r + x_avg_mds 766 self.graph.coors[1][component] = \ 767 (self.graph.coors[1][component]  y_avg_graph) / r + y_avg_mds 733 768 734 769 if callbackUpdateCanvas: … … 738 773 """Refresh the UI when running MDS on network components.""" 739 774 if not self.mdsStep % self.mdsRefresh: 740 self.mdsUpdateData(self.mdsComponents, self.mds, self.callbackUpdateCanvas) 775 self.mdsUpdateData(self.mdsComponents, 776 self.mds, 777 self.callbackUpdateCanvas) 741 778 742 779 if self.mdsType == MdsType.exactSimulation: 743 self.mds.points = [[self.graph.coors[0][i], self.graph.coors[1][i]] for i in range(len(self.graph.coors))] 780 self.mds.points = [[self.graph.coors[0][i], \ 781 self.graph.coors[1][i]] \ 782 for i in range(len(self.graph.coors))] 744 783 self.mds.freshD = 0 745 784 … … 754 793 return 1 755 794 756 def mdsComponents(self, mdsSteps, mdsRefresh, callbackProgress=None, callbackUpdateCanvas=None, torgerson=0, minStressDelta = 0, avgLinkage=False, rotationOnly=False, mdsType=MdsType.componentMDS, scalingRatio=0, mdsFromCurrentPos=0): 757 """Position the network components according to similarities among them.""" 795 def mdsComponents(self, mdsSteps, mdsRefresh, callbackProgress=None, \ 796 callbackUpdateCanvas=None, torgerson=0, \ 797 minStressDelta=0, avgLinkage=False, rotationOnly=False, \ 798 mdsType=MdsType.componentMDS, scalingRatio=0, \ 799 mdsFromCurrentPos=0): 800 """Position the network components according to similarities among 801 them. 802 803 """ 758 804 759 805 if self.vertexDistance == None: … … 772 818 self.stopMDS = 0 773 819 self.vertexDistance.matrixType = orange.SymMatrix.Symmetric 774 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) 820 self.diag_coors = math.sqrt((min(self.graph.coors[0])  \ 821 max(self.graph.coors[0]))**2 + \ 822 (min(self.graph.coors[1])  \ 823 max(self.graph.coors[1]))**2) 775 824 self.rotationOnly = rotationOnly 776 825 self.mdsType = mdsType … … 815 864 self.mds.Torgerson() 816 865 817 self.mds.optimize(mdsSteps, orngMDS.SgnRelStress, self.minStressDelta, progressCallback=self.mdsCallback) 866 self.mds.optimize(mdsSteps, orngMDS.SgnRelStress, self.minStressDelta,\ 867 progressCallback=self.mdsCallback) 818 868 self.mdsUpdateData(self.mdsComponents, self.mds, callbackUpdateCanvas) 819 869 … … 835 885 return 0 836 886 837 def mdsComponentsAvgLinkage(self, mdsSteps, mdsRefresh, callbackProgress=None, callbackUpdateCanvas=None, torgerson=0, minStressDelta = 0, scalingRatio=0, mdsFromCurrentPos=0): 838 return self.mdsComponents(mdsSteps, mdsRefresh, callbackProgress, callbackUpdateCanvas, torgerson, minStressDelta, True, scalingRatio=scalingRatio, mdsFromCurrentPos=mdsFromCurrentPos) 887 def mdsComponentsAvgLinkage(self, mdsSteps, mdsRefresh, \ 888 callbackProgress=None, \ 889 callbackUpdateCanvas=None, torgerson=0, \ 890 minStressDelta = 0, scalingRatio=0,\ 891 mdsFromCurrentPos=0): 892 return self.mdsComponents(mdsSteps, mdsRefresh, callbackProgress, \ 893 callbackUpdateCanvas, torgerson, \ 894 minStressDelta, True, \ 895 scalingRatio=scalingRatio, \ 896 mdsFromCurrentPos=mdsFromCurrentPos) 839 897 840 898 def saveNetwork(self, fn): 841 print "This method is deprecated. You should use orngNetwork.Network.saveNetwork"899 print "This method is deprecated. Use orngNetwork.Network.saveNetwork" 842 900 name = '' 843 901 try: … … 850 908 return 1 851 909 852 graphFile.write('### This file was generated with Orange Network Visualizer### \n\n\n')910 graphFile.write('### Generated with Orange.network ### \n\n\n') 853 911 if name == '': 854 912 graphFile.write('*Network ' + '"no name" \n\n') … … 859 917 #izpis opisov vozlisc 860 918 print "e", self.graph.nEdgeTypes 861 graphFile.write('*Vertices %8d %8d\n' % (self.graph.nVertices, self.graph.nEdgeTypes)) 919 graphFile.write('*Vertices %8d %8d\n' % (self.graph.nVertices, \ 920 self.graph.nEdgeTypes)) 862 921 for v in range(self.graph.nVertices): 863 922 graphFile.write('% 8d ' % (v + 1)) … … 887 946 for (i, j) in self.graph.getEdges(): 888 947 if len(self.graph[i, j]) > 0: 889 graphFile.write('%8d %8d %f' % (i + 1, j + 1, float(str(self.graph[i, j])))) 948 graphFile.write('%8d %8d %f' % (i + 1, j + 1, \ 949 float(str(self.graph[i, j])))) 890 950 graphFile.write('\n') 891 951 else: … … 893 953 for (i, j) in self.graph.getEdges(): 894 954 if len(self.graph[i, j]) > 0: 895 graphFile.write('%8d %8d %f' % (i + 1, j + 1, float(str(self.graph[i, j])))) 955 graphFile.write('%8d %8d %f' % (i + 1, j + 1, \ 956 float(str(self.graph[i, j])))) 896 957 graphFile.write('\n') 897 958 … … 910 971 911 972 def readNetwork(self, fn, directed=0): 912 print "This method is deprecated. You should use orngNetwork.Network.readNetwork"973 print "This method is deprecated. Use orngNetwork.Network.readNetwork" 913 974 network = Network(1,directed) 914 975 net = network.readPajek(fn, directed) … … 956 1017 if len(nbh) == 0: continue 957 1018 lbls = [labels[u] for u in nbh] 958 lbls = [(len(list(c)), l) for l, c in itertools.groupby(lbls)] 1019 lbls = [(len(list(c)), l) for l, c \ 1020 in itertools.groupby(lbls)] 959 1021 m = max(lbls)[0] 960 1022 mlbls = [l for c, l in lbls if c >= m] … … 965 1027 966 1028 if results2items and not resultHistory2items: 967 attrs = [orange.EnumVariable('clustering label propagation', values=list(set([l for l in lblhistory[1]])))] 1029 attrs = [orange.EnumVariable('clustering label propagation', \ 1030 values=list(set([l for l \ 1031 in lblhistory[1]])))] 968 1032 dom = orange.Domain(attrs, 0) 969 1033 data = orange.ExampleTable(dom, [[l] for l in lblhistory[1]]) 970 self.net.items = data if self.net.items == None else orange.ExampleTable([self.net.items, data]) 1034 if self.net.items is None: 1035 self.net.items = data 1036 else: 1037 self.net.items = orange.ExampleTable([self.net.items, data]) 971 1038 if resultHistory2items: 972 attrs = [orange.EnumVariable('c'+ str(i), values=list(set([l for l in lblhistory[0]]))) for i,labels in enumerate(lblhistory)] 1039 attrs = [orange.EnumVariable('c'+ str(i), \ 1040 values=list(set([l for l in lblhistory[0]]))) for i,labels \ 1041 in enumerate(lblhistory)] 973 1042 dom = orange.Domain(attrs, 0) 974 1043 # transpose history 975 1044 data = map(list, zip(*lblhistory)) 976 1045 data = orange.ExampleTable(dom, data) 977 self.net.items = data if self.net.items == None else orange.ExampleTable([self.net.items, data]) 1046 if self.net.items is None: 1047 self.net.items = data 1048 else: 1049 self.net.items = orange.ExampleTable([self.net.items, data]) 978 1050 979 1051 return labels
Note: See TracChangeset
for help on using the changeset viewer.