def _setTargetPartitionWeights(self, num_partitions): target = 1.0 / float(num_partitions) for k in range(0, num_partitions): self.targetweights.append(target) self.partweights = putils.genArray(num_partitions * self._nconst, 0)
def fromNetworkxGraph(self, G, num_partitions, partvec=None, hyperedgeExpansionMode='no_expansion'): ''' Populates the PaToH data from a networkx graph ''' if (isinstance(G, nx.Graph) == False): return False cliques = self._getGraphCliques(G) if cliques is None: return False self._c = G.number_of_nodes() self._n = len(cliques) #xpins stores the index starts of each net (clique) #pins stores the node ides in each clique indexed by xpins self.xpins = putils.genArray(self._n + 1, 0) self.pins = [] # _nconst = number of weights for each vertex/cell # ASSUME _nconst = 1 # node weights = cwhgts self.cwghts = putils.genArray(self._c * self._nconst, 1) # edge weights need to be converted to nwghts self.nwghts = putils.genArray(self._n, 1) for cliqueID, clique in enumerate(cliques): self.xpins[cliqueID] = len(self.pins) node_weights = [] clique_edge_weights = [] clique_node_weights = [] edges_added = [] for node in clique: # set the node weight try: weight = G.node[node]['weight'] #node_weights.append(weight) clique_node_weights.append(weight) self.cwghts[node] = weight except Exception as err: #print('node weight exception', err) #node_weights.append(1) pass for edgeID in G.neighbors(node): # make sure each edge is counted only once edge_str = str(node) if edgeID > node: edge_str = edge_str + '_' + str(edgeID) else: edge_str = str(edgeID) + '_' + edge_str if edge_str in edges_added: continue else: edges_added.append(edge_str) try: eWeight = G.edge[node][edgeID]['weight'] clique_edge_weights.append(eWeight) except Exception as err: print('clique edge error', err) self.pins.append(node) # compute clique edge expansion if '_complete' in hyperedgeExpansionMode: hyperedgeWeight = self._hyperedgeExpansionComplete( clique_node_weights, hyperedgeExpansionMode) else: hyperedgeWeight = self._hyperedgeExpansion( clique_node_weights, hyperedgeExpansionMode) #print('hyperedge', len(clique), hyperedgeExpansionMode, hyperedgeWeight) self.nwghts[cliqueID] = hyperedgeWeight # add last ID self.xpins[self._n] = len(self.pins) if partvec is not None: self.useFixCells = 1 self.partvec = partvec self._setTargetPartitionWeights(num_partitions) self._exportArrays()
def fromNetworkxGraph(self, nxGraph, baseval=1, parttab=None, vlbltab=None): if isinstance(nxGraph, nx.Graph) == False: print('Error, cannot load networkx graph from datatype', type(metisGraph).__name__) return False # number_of_nodes # size() ? number of edges self.vertnbr = nxGraph.number_of_nodes() self.edgenbr = nxGraph.size() * 2 self.baseval = baseval self.verttab = putils.genArray(nxGraph.number_of_nodes() + 1) self.edgetab = putils.genArray(nxGraph.size() * 2) self.edlotab = putils.genArray(nxGraph.size() * 2) self.velotab = putils.genArray(nxGraph.number_of_nodes()) if (vlbltab is None): self.vlbltab = putils.genArray(nxGraph.number_of_nodes()) #self.vlbltab = [] else: if len(vlbltab) == self.vertnbr: self.vlbltab = vlbltab else: self.vlbltab = putils.genArray(nxGraph.number_of_nodes()) if parttab is None: self.parttab = putils.genArray(nxGraph.number_of_nodes(), -1) else: if len(parttab) == self.vertnbr: self.parttab = parttab else: self.parttab = putils.genArray(nxGraph.number_of_nodes(), -1) vtabID = 0 nodes = sorted(nxGraph.nodes()) vertCount = 0 for vertexID in range(self.baseval, len(nodes) + self.baseval): vertex = nodes[vertexID - self.baseval] adjustedID = vertexID - self.baseval self.vlbltab[vertCount] = nodes[ vertexID - self. baseval] # store the lable for this vertex as vertCount != adjustID vertCount += 1 #vertex.printData(False) self.verttab[adjustedID] = vtabID vWeight = 1 try: vWeight = int(nxGraph.node[vertex]['weight']) except KeyError as ke: pass self.velotab[adjustedID] = vWeight indexedEdges = {} edgeIndeces = sorted(nxGraph.neighbors(vertex)) edgeCount = 0 for edgeID in edgeIndeces: edgeWeight = 1 try: edgeWeight = int( nxGraph.edge[adjustedID][edgeID]['weight']) except Exception as e: edgeWeight = 1 self.edgetab[vtabID + edgeCount] = edgeID - self.baseval self.edlotab[vtabID + edgeCount] = edgeWeight #print('edge:', vertex, edgeID - self.baseval) edgeCount += 1 vtabID += len(edgeIndeces) self.verttab[nxGraph.number_of_nodes()] = vtabID # update vertex IDs updateEdgeIDSUsingLabels = False if updateEdgeIDSUsingLabels: lblmap = {} for newVertID in range(0, len(self.vlbltab)): oldVertID = self.vlbltab[newVertID] lblmap[oldVertID] = newVertID for i in range(0, len(self.edgetab)): newVal = lblmap[self.edgetab[i]] self.edgetab[i] = newVal self._exportArrays()