gklearn.utils.utils

class SpecialLabel[source]

Bases: enum.Enum

can be used to define special labels.

DUMMY = 1
compute_distance_matrix(gram_matrix)[source]
compute_gram_matrices_by_class(ds_name, kernel_options, save_results=True, dir_save='', irrelevant_labels=None, edge_required=False)[source]
direct_product(G1, G2, node_label, edge_label)[source]

Return the direct/tensor product of directed graphs G1 and G2.

G1, G2 : NetworkX graph
The original graphs.
node_label : string
node attribute used as label. The default node label is ‘atom’.
edge_label : string
edge attribute used as label. The default edge label is ‘bond_type’.
gt : NetworkX graph
The direct product graph of G1 and G2.

This method differs from networkx.tensor_product in that this method only adds nodes and edges in G1 and G2 that have the same labels to the direct product graph.

[1]Thomas Gärtner, Peter Flach, and Stefan Wrobel. On graph kernels: Hardness results and efficient alternatives. Learning Theory and Kernel Machines, pages 129–143, 2003.
direct_product_graph(G1, G2, node_labels, edge_labels)[source]

Return the direct/tensor product of directed graphs G1 and G2.

G1, G2 : NetworkX graph
The original graphs.
node_labels : list
A list of node attributes used as labels.
edge_labels : list
A list of edge attributes used as labels.
gt : NetworkX graph
The direct product graph of G1 and G2.

This method differs from networkx.tensor_product in that this method only adds nodes and edges in G1 and G2 that have the same labels to the direct product graph.

[1]Thomas Gärtner, Peter Flach, and Stefan Wrobel. On graph kernels: Hardness results and efficient alternatives. Learning Theory and Kernel Machines, pages 129–143, 2003.
dummy_edge()[source]
/*!
  • @brief Returns a dummy edge.
  • @return ID of dummy edge.

*/

dummy_node()[source]
/*!
  • @brief Returns a dummy node.
  • @return ID of dummy node.

*/

find_all_paths(G, length, is_directed)[source]

Find all paths with a certain length in a graph. A recursive depth first search is applied.

G : NetworkX graphs
The graph in which paths are searched.
length : integer
The length of paths.
path : list of list
List of paths retrieved, where each path is represented by a list of nodes.
find_paths(G, source_node, length)[source]

Find all paths with a certain length those start from a source node. A recursive depth first search is applied.

G : NetworkX graphs
The graph in which paths are searched.
source_node : integer
The number of the node from where all paths start.
length : integer
The length of paths.
path : list of list
List of paths retrieved, where each path is represented by a list of nodes.
floydTransformation(G, edge_weight=None)[source]

Transform graph G to its corresponding shortest-paths graph using Floyd-transformation.

G : NetworkX graph
The graph to be tramsformed.
edge_weight : string
edge attribute corresponding to the edge weight. The default edge weight is bond_type.
S : NetworkX graph
The shortest-paths graph corresponding to G.
[1]Borgwardt KM, Kriegel HP. Shortest-path kernels on graphs. InData Mining, Fifth IEEE International Conference on 2005 Nov 27 (pp. 8-pp). IEEE.
getSPGraph(G, edge_weight=None)[source]

Transform graph G to its corresponding shortest-paths graph.

G : NetworkX graph
The graph to be tramsformed.
edge_weight : string
edge attribute corresponding to the edge weight.
S : NetworkX graph
The shortest-paths graph corresponding to G.

For an input graph G, its corresponding shortest-paths graph S contains the same set of nodes as G, while there exists an edge between all nodes in S which are connected by a walk in G. Every edge in S between two nodes is labeled by the shortest distance between these two nodes.

[1]Borgwardt KM, Kriegel HP. Shortest-path kernels on graphs. InData Mining, Fifth IEEE International Conference on 2005 Nov 27 (pp. 8-pp). IEEE.
getSPLengths(G1)[source]
get_edge_labels(Gn, edge_label)[source]

Get edge labels of dataset Gn.

get_graph_kernel_by_name(name, node_labels=None, edge_labels=None, node_attrs=None, edge_attrs=None, ds_infos=None, kernel_options={})[source]
get_mlti_dim_edge_attrs(G, attr_names)[source]
get_mlti_dim_node_attrs(G, attr_names)[source]
get_node_labels(Gn, node_label)[source]

Get node labels of dataset Gn.

get_shortest_paths(G, weight, directed)[source]

Get all shortest paths of a graph.

G : NetworkX graphs
The graphs whose paths are calculated.
weight : string/None
edge attribute used as weight to calculate the shortest path.
directed: boolean
Whether graph is directed.
sp : list of list
List of shortest paths of the graph, where each path is represented by a list of nodes.
graph_deepcopy(G)[source]

Deep copy a graph, including deep copy of all nodes, edges and attributes of the graph, nodes and edges.

It is the same as the NetworkX function graph.copy(), as far as I know.

graph_isIdentical(G1, G2)[source]

Check if two graphs are identical, including: same nodes, edges, node labels/attributes, edge labels/attributes.

  1. The type of graphs has to be the same.
  2. Global/Graph attributes are neglected as they may contain names for graphs.
normalize_gram_matrix(gram_matrix)[source]
undefined_node()[source]
/*!
  • @brief Returns an undefined node.
  • @return ID of undefined node.

*/

untotterTransformation(G, node_label, edge_label)[source]

Transform graph G according to Mahé et al.’s work to filter out tottering patterns of marginalized kernel and tree pattern kernel.

G : NetworkX graph
The graph to be tramsformed.
node_label : string
node attribute used as label. The default node label is ‘atom’.
edge_label : string
edge attribute used as label. The default edge label is ‘bond_type’.
gt : NetworkX graph
The transformed graph corresponding to G.
[1]Pierre Mahé, Nobuhisa Ueda, Tatsuya Akutsu, Jean-Luc Perret, and Jean-Philippe Vert. Extensions of marginalized graph kernels. In Proceedings of the twenty-first international conference on Machine learning, page 70. ACM, 2004.