gklearn.utils.utils

class SpecialLabel(value)[source]

Bases: Enum

can be used to define special labels.

DUMMY = 1
check_json_serializable(obj, deep: bool = False) bool[source]

Check if an object is JSON serializable.

Parameters

objobject

The object to be checked.

deepbool, optional

Whether to check the object recursively when obj is iterable. The default is False.

Returns

bool

True if the object is JSON serializable, False otherwise.

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]
compute_vertex_kernels(g1, g2, node_kernels, node_labels=[], node_attrs=[])[source]

Compute kernels between each pair of vertices in two graphs.

Parameters

g1, g2NetworkX graph

The kernels bewteen pairs of vertices in these two graphs are computed.

node_kernelsdict

A dictionary of kernel functions for nodes, including 3 items: ‘symb’ for symbolic node labels, ‘nsymb’ for non-symbolic node labels, ‘mix’ for both labels. The first 2 functions take two node labels as parameters, and the ‘mix’ function takes 4 parameters, a symbolic and a non-symbolic label for each the two nodes. Each label is in form of 2-D dimension array (n_samples, n_features). Each function returns a number as the kernel value. Ignored when nodes are unlabeled. This argument is designated to conjugate gradient method and fixed-point iterations.

node_labelslist, optional

The list of the name strings of the node labels. The default is [].

node_attrslist, optional

The list of the name strings of the node attributes. The default is [].

Returns

vk_dictdict

Vertex kernels keyed by vertices.

Notes

This function is used by ``gklearn.kernels.FixedPoint’’ and ``gklearn.kernels.StructuralSP’’. The method is borrowed from FCSP [1].

References

Parallelization of shortest path graph kernels on multi-core cpus and gpus. Proceedings of the Programmability Issues for Heterogeneous Multicores (MultiProg), Vienna, Austria, 2014.

direct_product(G1, G2, node_label, edge_label)[source]

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

Parameters

G1, G2NetworkX graph

The original graphs.

node_labelstring

node attribute used as label. The default node label is ‘atom’.

edge_labelstring

edge attribute used as label. The default edge label is ‘bond_type’.

Return

gtNetworkX graph

The direct product graph of G1 and G2.

Notes

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.

References

direct_product_graph(G1, G2, node_labels, edge_labels)[source]

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

Parameters

G1, G2NetworkX graph

The original graphs.

node_labelslist

A list of node attributes used as labels.

edge_labelslist

A list of edge attributes used as labels.

Return

gtNetworkX graph

The direct product graph of G1 and G2.

Notes

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.

References

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.

Parameters

GNetworkX graphs

The graph in which paths are searched.

lengthinteger

The length of paths.

Return

pathlist 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.

Parameters

GNetworkX graphs

The graph in which paths are searched.

source_nodeinteger

The number of the node from where all paths start.

lengthinteger

The length of paths.

Return

pathlist 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.

Parameters

GNetworkX graph

The graph to be tramsformed.

edge_weightstring

edge attribute corresponding to the edge weight. The default edge weight is bond_type.

Return

SNetworkX graph

The shortest-paths graph corresponding to G.

References

getSPGraph(G, edge_weight=None)[source]

Transform graph G to its corresponding shortest-paths graph.

Parameters

GNetworkX graph

The graph to be transformed.

edge_weightstring

edge attribute corresponding to the edge weight.

Return

SNetworkX graph

The shortest-paths graph corresponding to G.

Notes

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.

References

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={}, **kwargs)[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.

Parameters

GNetworkX graphs

The graphs whose paths are calculated.

weightstring/None

edge attribute used as weight to calculate the shortest path.

directed: boolean

Whether graph is directed.

Return

splist of list

List of shortest paths of the graph, where each path is represented by a list of nodes.

get_sp_graph(G, edge_weight=None)[source]

Transform graph G to its corresponding shortest-paths graph.

Parameters

GNetworkX graph

The graph to be transformed.

edge_weightstring

edge attribute corresponding to the edge weight.

Return

SNetworkX graph

The shortest-paths graph corresponding to G.

Notes

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.

References

graph_deepcopy(G)[source]

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

Note

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

  • This function only supports Networkx.Graph and Networkx.DiGraph.

graph_isIdentical(G1, G2)[source]

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

Notes

  1. The type of graphs has to be the same.

  2. Global/Graph attributes are neglected as they may contain names for graphs.

is_basic_python_type(obj, type_list: list | None = None, deep: bool = False) bool[source]

Check if an object is a basic type in Python.

Parameters

objobject

The object to be checked.

type_listlist, optional

The list of basic types in Python. The default is None, which means the default basic types are used. The default basic types include int, float, complex, str, bool, NoneType, list, tuple, dict, set, frozenset, range, slice.

deepbool, optional

Whether to check the object recursively when obj is iterable. The default is False.

Returns

bool

True if the object is a basic type in Python, False otherwise.

normalize_gram_matrix(gram_matrix)[source]
nx_permute_nodes(G, random_state=None)[source]

Permute node indices in a NetworkX graph.

Parameters

GTYPE

DESCRIPTION.

random_stateTYPE, optional

DESCRIPTION. The default is None.

Returns

G_newTYPE

DESCRIPTION.

Notes

  • This function only supports Networkx.Graph and Networkx.DiGraph.

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.

Parameters

GNetworkX graph

The graph to be tramsformed.

node_labelstring

node attribute used as label. The default node label is ‘atom’.

edge_labelstring

edge attribute used as label. The default edge label is ‘bond_type’.

Return

gtNetworkX graph

The transformed graph corresponding to G.

References