[docs]defstellar_kernel_distance(files,filterfunction:Callable=None,kernel="shortest_path",):""" Compute a pairwise distance matrix between graphs based on `grakel` kernels. Parameters ---------- files : str A path pointing to the directory containing starGraphs. filterfunction : Callable, optional A customizable filter function for pulling a subset of cosmic graphs. Default is None. kernel : str, optional The kernel to be used for computing pairwise distances. Default is "shortest_path". Returns ------- keys : np.array A list of the keys for the models being compared. distance_matrix : np.ndarray A pairwise distance matrix between the persistence landscapes of the starGraphs. """starGraphs=_load_starGraphs(files,filterfunction)# Assign class 0 label to all nodesnx_graphs=[]forsGinstarGraphs.values():G=sG.graphnode_labels={node:0fornodeinG.nodes()}nx.set_node_attributes(G,node_labels,"class")nx_graphs.append(G)keys=list(starGraphs.keys())# Convert to GraKel GraphsG=graph_from_networkx(nx_graphs,node_labels_tag="class")gk=GraphKernel(kernel=kernel,normalize=True)# Distance as 1 - similaritydistance_matrix=1-gk.fit_transform(G)returnnp.array(keys),distance_matrix
[docs]def_load_starGraphs(dir:str,graph_filter:Callable=None)->dict:""" Load starGraphs in a given directory. This function only returns diagrams for starGraphs that satisfy the constraint given by `graph_filter`. Parameters ---------- dir : str The directory containing the graphs, from which diagrams can be extracted. graph_filter : Callable, optional Default to None (ie no filter). Only select graph object based on filter function criteria (returns 1 to include and 0 to exclude) Returns ------- dict A dictionary mapping the graph object file paths to the corresponding persistence diagram object. """assertos.path.isdir(dir),"Invalid graph Directory"assertlen(os.listdir(dir))>0,"Graph directory appears to be empty!"ifgraph_filterisNone:graph_filter=nofilterfunctionstarGraphs={}forfileinos.listdir(dir):iffile.endswith(".pkl"):graph_file=os.path.join(dir,file)withopen(graph_file,"rb")asf:graph_object=pickle.load(f)ifgraph_filter(graph_object):ifgraph_object.starGraphisnotNone:starGraphs[graph_file]=graph_object.starGraphassert(len(starGraphs)>0),"You haven't produced any valid starGraphs. \ Your filter function may be too stringent."returnstarGraphs