Skip to content

Graph Utilities & Ops

The k3_node.ops module collects the low-level graph-algebra, sparse-matrix, structure-checking, synthetic-graph-generation, and cheminformatics helper functions used throughout K3-Node's layers, models, and datasets. It's a superset of the legacy k3_node.utils namespace (kept for backward compatibility) — new code should import from k3_node.ops.

from k3_node import ops as k3_ops

Graph Algebra

Dense/sparse adjacency-matrix normalization utilities, mirroring torch_geometric/Spektral-style graph filters — mainly used internally by spectral layers (ChebConv, GCN2Conv, ...).

degree_matrix

k3_node.ops.conv.degree_matrix(A)

degree_power

k3_node.ops.conv.degree_power(A, k)

normalized_adjacency

k3_node.ops.conv.normalized_adjacency(A, symmetric=True)

normalized_laplacian

k3_node.ops.conv.normalized_laplacian(A, symmetric=True)

laplacian

k3_node.ops.conv.laplacian(A)

gcn_filter

k3_node.ops.conv.gcn_filter(A, symmetric=True)

normalize_A

k3_node.ops.graph.normalize_A(A)

degrees

k3_node.ops.graph.degrees(A)

get_source_target

k3_node.ops.graph.get_source_target(a)


Sparse / Dense Matrix Operations

Backend-agnostic matrix-multiply helpers that transparently handle mixed sparse/dense and batched ("modal") tensors.

dot

k3_node.ops.matmul.dot(a, b)

mixed_mode_dot

k3_node.ops.matmul.mixed_mode_dot(a, b)

k3_node.ops.matmul.modal_dot(a, b, transpose_a=False, transpose_b=False)

polyval

k3_node.ops.numpy.polyval(p, x)

get_unique

k3_node.ops.numpy.get_unique(inputs)


Graph Structure Utilities

Edge-index bookkeeping helpers, mirroring torch_geometric.utils.

coalesce

k3_node.utils.graph.coalesce(edge_index, edge_attr=None, num_nodes=None, is_sorted=False, sort_by_row=True, reduce='add')

Sorts edge_index and removes duplicate edges, summing duplicate edge attributes.

subgraph

k3_node.utils.graph.subgraph(subset, edge_index, edge_attr=None, relabel_nodes=False, num_nodes=None, return_edge_mask=False)

Returns the induced subgraph of nodes in subset.

edge_index_to_adjacency_matrix

k3_node.utils.graph.edge_index_to_adjacency_matrix(edge_index)

contains_isolated_nodes

k3_node.utils.graph.contains_isolated_nodes(edge_index, num_nodes=None)

Returns True if the graph contains isolated nodes.

has_self_loops

k3_node.utils.graph.has_self_loops(edge_index)

Returns True if the graph contains self-loops.

is_undirected

k3_node.utils.graph.is_undirected(edge_index, edge_attr=None, num_nodes=None)

Returns True if the graph is undirected.


Synthetic Graph Generators

Reference-graph generators used in tests and tutorials.

erdos_renyi_graph

k3_node.utils.random.erdos_renyi_graph(num_nodes, edge_prob, directed=False)

Returns the edge_index of a random Erdos-Renyi graph.

barabasi_albert_graph

k3_node.utils.random.barabasi_albert_graph(num_nodes, num_edges)

Returns the edge_index of a Barabasi-Albert preferential attachment model.

stochastic_blockmodel_graph

k3_node.utils.random.stochastic_blockmodel_graph(block_sizes, edge_probs, directed=False)

Returns the edge_index of a stochastic blockmodel graph.


Cheminformatics (SMILES / RDKit)

Conversions between SMILES strings, RDKit Mol objects, and K3-Node Data graphs — used by the molecular datasets (MoleculeNet, QM9, QM7) and chemistry models.

from_smiles

k3_node.utils.smiles.from_smiles(smiles, with_hydrogen=False, kekulize=False)

Converts a SMILES string to a :class:k3_node.data.Data instance.

to_smiles

k3_node.utils.smiles.to_smiles(data, kekulize=False)

Converts a :class:k3_node.data.Data instance to a SMILES string.

from_rdmol

k3_node.utils.smiles.from_rdmol(mol)

Converts an :class:rdkit.Chem.Mol instance to a :class:k3_node.data.Data instance.

to_rdmol

k3_node.utils.smiles.to_rdmol(data, kekulize=False)

Converts a :class:k3_node.data.Data instance to an :class:rdkit.Chem.Mol instance.


Neural Network Ops

segment_softmax

Numerically-stable, segment-wise (per-graph or per-node-neighborhood) softmax — the core primitive behind every attention-based conv layer (GATConv, TransformerConv, ...).

k3_node.utils.keras.segment_softmax(x, indices, n_nodes=None)