def frequentGraph(self): graphDemo = np.array([[2, 11, 10, 11], [11, 1, 0, 11], [10, 0, 1, 10], [11, 11, 10, 1]]) graphDemo2 = np.array([[2, 11, 10, 15], [11, 1, 0, 15], [10, 0, 1, 16], [15, 15, 16, 3]]) # print(self.tempTrees) # self.exploreFFSM(self.freqEdges2matrix()) # self.exploreGenericTree(self.freqEdges2matrix(),[],self.tempTrees) # print("canonical",self.canonicalForm(graphDemo)) # exit(0) results, S = self.exploreGenericTree(self.tempTrees, {}) # print("final results",results[0]) # try: print("S-final", S) print("final result", results.keys()) if len(results.items()) == 0: return [] with open('result-mico.txt', 'w') as f: for k in results.keys(): f.write('$' + k.replace('\n', '')) for g, v in results[k].items(): f.write('\n#' + str(g) + '\n') for topo in v: f.write(' '.join(str(x) for x in topo)) f.write('\n') numNodeGraphs = np.array([ len(np.where(string2matrix(k) > 0)[0]) for k, v in results.items() ]) # print("numNodeGraphs",numNodeGraphs) indicesFreq = np.where(numNodeGraphs == numNodeGraphs.max())[0] # return results[0] # print("indicesFreq",indicesFreq) return [string2matrix(list(results.keys())[i]) for i in indicesFreq] # except: # return [] # freqGraphs = [for k in freqGraphs] # for k,v in results: # print("result extend") # print(extendOneNode(graphDemo,graphDemo2)) # print(np.where(graphDemo == 0)) # eg = ExpansionGraph(self.tempTrees) # print("ca",self.canonicalForm(graphDemo)) # print(self.extendFFSM(graphDemo,graphDemo2)) # print(self.joinCase3bFFSM(graphDemo,graphDemo2)) # print(self.extend(graphDemo,np.array([[0,0,0,0,1]]))) # canonicalForm(graphDemo) return True
def expand(self): # print("canEdges",self.canEdges) # print("associative edges",self.associativeEdges) # print("isLethal",self.checkLethal()) # exit(0) # for k,v in self.spaceGraphs.items(): # for i,g in v.items(): # plotGraph(g[0]) # for k,v in self.spaceGraphs.items(): # for idGraph,g in v.items(): # # print("g",g[0]) # for i in range(g[0].shape[0]): # g[0][i,i] = self.graphs[idGraph][g[0][i,i],g[0][i,i]] # # print("g after",g[0]) # plotGraph(g[0],isShowedID=False) if self.checkLethal(): return {} # print("before eliminate",self.canEdges) self.eliminateAssEdges() # print("eliminated",self.canEdges) # exit(0) self.searchGraph(self.matrixAdj, self.canEdges) print("end searchGraphs") frequents = {} for k, v in self.spaceGraphs.items(): # print("kFreq",k) if len(v.items()) >= self.theta: # print("frequentExpansion",k) frequents[k] = v eqGraphClasses = {} canTree = canonicalForm(self.matrixAdj)['code'] if len(frequents.items()) > 0: for k, v in frequents.items(): subGraph = string2matrix(k) # print("subgraph",subGraph) # print("can subgraph",canonicalForm(subGraph)) # print("tree",self.matrixAdj) # print("canTree",canTree) # print("beforeCanical",k,v) cam = canonicalForm(subGraph) # print("afterCanical",cam['tree'],v) # print('camCode',cam['code']) if cam['code'] == canTree: # print("eqItem",k) eqGraphClasses[k] = v # exit(0) print("eqGraphClass", eqGraphClasses) # print("current form tree",canonicalForm(self.matrixAdj)) return eqGraphClasses
def extractResultGraph(results): for k,v in results.items(): numNodeGraphs = np.array([string2matrix(k).shape[0] for k,v in results.items()]) indicesFreq = np.where(numNodeGraphs == numNodeGraphs.max())[0] return [string2matrix(list(results.keys())[i]) for i in indicesFreq]
def exploreGenericTree(self, C: dict, R: dict): # print("C in\n",C.keys()) # print("Temptrees len", len(tempTrees.items())) print("timestamp", datetime.datetime.now()) Q = {} # print("reprGroup",C) for idCan, can in enumerate(C.items()): X = string2matrix(can[0]) # print('begin frequent trees') nextCan = {k: C[k] for k in list(C.keys())[idCan + 1:]} encodeX = np.array2string(X) S = self.frequentTrees2(X, C[encodeX]) # print("After S-freq",S.keys()) # S - R canonicalS = { canonicalForm(string2matrix(k))['code']: k for k in S.keys() } # print("SSS before",S.keys()) # print("RRBefore",R.keys()) for kR in R.keys(): canonicalKR = canonicalForm(string2matrix(kR))['code'] if canonicalKR in canonicalS.keys(): if canonicalS[canonicalKR] in S: # print('del duplicate',canonicalKR) del S[canonicalS[canonicalKR]] # remove duplicate # for kR in R.keys(): # if kR in S: # del S[kR] # print("tempTree after",self.tempTrees) # if len(S) != 0: # print("afterFrequentTRee size S: ",S.keys()) self.countGen += 1 print("Loop in generic tree: ", self.countGen) U, V = self.exploreGenericTree(S, R.copy()) # print("S empty",S) # print("R empty",R) # print("U empty",U) # print("V empty",V) # print("X ok ex",X) # print("encode X",encodeX) for k, v in U.items(): Q[k] = v R[encodeX] = C[encodeX] for k, v in V.items(): R[k] = v if self.hasNoExternalAssEdge(X, C[encodeX]): print("MaxExpansion", X) # print("topoX",C[encodeX]) eg = ExpansionGraph(X.copy(), C[encodeX], self.graphs, self.freqEdges, self.theta) expansionX = eg.expand() # print("beforeExpansionX",X) # print("expansion X",expansionX) for k, v in expansionX.items(): Q[k] = v # exit(0) # del eg # print("Q",len(Q.items)) # print("R",R) return Q, R
def frequentTrees(self, X, restCan, C): S = [] newTempTrees = {} print("timestamp frequent trees", datetime.datetime.now()) # print("X in",X) # print("C X in",C) for Y in [string2matrix(k) for k in restCan]: candidates = [] # print("X",X) # print("Y",Y) if np.array_equal(X[:-1, :-1], Y[:-1, :-1]) and not np.array_equal( X[-1], Y[-1]): candidates.append(self.joinCase3bFFSM(X.copy(), Y.copy())) candidates.append(self.extendCase3bFFSM(X.copy())) # print("XX",X) # print("YY",Y) # print("can join",candidates) # if X.shape[0] < 3: # extensions = extendOneNode(X,Y) #self.extendFFSM(X,Y) # if len(extensions) > 0: # candidates.extend(extensions) # print("candidates",len(candidates)) extensions = extendOneNode(X, Y) if len(extensions) > 0: candidates.extend(extensions) for joinedTree in candidates: # print("preJoinedTreeX",X) # print("preJoinedTReeY",Y) # print("joinedTree",joinedTree) indexAddNode = np.where(joinedTree[-1] > 0)[0][0] # print("indexAddNode",np.where(joinedTree[-1] > 0)[0]) # embedJoinedTree = np.array2string(joinedTree) cam = canonicalForm(joinedTree) embedCamTree = np.array2string(cam['tree']) for i in C[np.array2string(X)].keys(): topo = [] for subNodes in C[np.array2string(X)][i]: linkedNode = subNodes[indexAddNode] for j in np.where(self.graphs[i][linkedNode] > 0)[0]: if self.graphs[i][linkedNode, j] == joinedTree[ -1, indexAddNode] and j not in subNodes: # print("prevNodes",subNodes) newSubNodes = np.append( subNodes, np.array([j])) # print("newSubNodes",newSubNodes) # print("camTree",cam["index"]) reindicedNodes = np.array([ newSubNodes[idNode] for idNode in cam['index'] ]) # print("newReindexNode",reindicedNodes) topo.append(reindicedNodes) # for subGraph in C[np.array2string(X)][i]: # linkedNode = subGraph[indexAddNode,indexAddNode] # node is extended # for j in np.where(self.graphs[i][linkedNode] > 0)[0]: # get neighbor of linked node # if self.graphs[i][linkedNode,j] == joinedTree[-1,indexAddNode] and j not in subGraph.diagonal(): # pad = np.zeros((1,subGraph.shape[0]+1),dtype=int) # pad[0,indexAddNode] = joinedTree[-1,indexAddNode] # pad[0,-1] = j # topo.append(self.extend(subGraph,pad)) # cam = self.canonicalForm() if len(topo) > 0: # print("topo",topo) if embedCamTree not in newTempTrees: newTempTrees[embedCamTree] = {} # S.append(joinedTree) newTempTrees[embedCamTree][i] = topo # print("CamJoined",cam) # print("embedCamTRee",embedCamTree) temp = {} for k, v in newTempTrees.items(): if len(v.items()) >= self.theta: # temp[np.array2string(canonicalForm(string2matrix(k),code=False))] = v temp[k] = v # print(temp) return temp