24. Graph API¶
This page documents the graph API. For workflows, see Graph how-to.
24.1 What it is for¶
The graph brick constructs similarity graphs and derives graph-based feature views. [1][2]
24.2 Examples¶
Build a kNN graph:
import numpy as np
from modssc.graph import GraphBuilderSpec, build_graph
X = np.random.randn(20, 8).astype(np.float32)
spec = GraphBuilderSpec(scheme="knn", metric="cosine", k=3)
G = build_graph(X, spec=spec, seed=0, cache=False)
print(G.n_nodes, G.n_edges)
Generate graph views:
from modssc.graph import GraphFeaturizerSpec, graph_to_views
from modssc.graph.artifacts import NodeDataset
node_ds = NodeDataset(X=X, y=np.zeros((20,), dtype=np.int64), graph=G, masks={})
fspec = GraphFeaturizerSpec(views=("attr", "diffusion"))
views = graph_to_views(node_ds, spec=fspec, seed=0, cache=False)
print(list(views.views.keys()))
Specs and artifacts are defined in src/modssc/graph/specs.py and src/modssc/graph/artifacts.py. [3][4]
24.3 API reference¶
Graph utilities for ModSSC.
This package provides:
- Graph construction: build a similarity graph from feature vectors (kNN, epsilon-ball, and anchor graphs).
- Graph featurization: derive tabular views from a graph (attribute, diffusion, and structural embeddings).
- Cache and fingerprints for reproducibility.
The graph representation is backend-agnostic. Optional backends exist (sklearn, faiss).
24.4
DatasetViews
dataclass
¶
One or more tabular views derived from a dataset.
Source code in src/modssc/graph/artifacts.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
24.5
GraphArtifact
dataclass
¶
Canonical graph representation.
24.5.0.1 Notes¶
For reproducible experiments, graph construction should be fingerprinted and cached
(see :func:modssc.graph.build_graph).
Source code in src/modssc/graph/artifacts.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
24.6
GraphBuilderSpec
dataclass
¶
Graph construction specification.
24.6.0.1 Notes¶
This spec is designed to be serializable (via :meth:to_dict) and stable,
so that it can be fingerprinted for reproducibility.
Adds: - anchor scheme (approximate kNN via anchors) - faiss backend (optional) - chunk_size knob (for chunked numpy computations and resumable work dirs)
Source code in src/modssc/graph/specs.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
24.7
GraphFeaturizerSpec
dataclass
¶
Featurization spec to produce inductive views from a graph.
24.7.0.1 Views¶
attr: returns the original attribute matrix X diffusion: returns a simple diffusion of attributes over the graph struct: returns structural embeddings (DeepWalk/Node2Vec-style) computed from the graph only (X is ignored).
24.7.0.2 Notes¶
- The struct view is deterministic given the seed.
- For large graphs, struct view may require optional dependencies.
Source code in src/modssc/graph/specs.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
24.8
GraphWeightsSpec
dataclass
¶
Specification for edge weights.
24.8.0.1 Parameters¶
kind: - "binary": all edges weight 1 - "heat": exp(-d^2/(2*sigma^2)) - "cosine": convert cosine distances into similarities (1 - d) sigma: Used only for kind="heat".
Source code in src/modssc/graph/specs.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
24.9
NodeDataset
dataclass
¶
Node classification dataset for transductive methods.
Source code in src/modssc/graph/artifacts.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
24.10
build_graph(X, *, spec, seed=0, dataset_fingerprint=None, preprocess_fingerprint=None, cache=True, cache_dir=None, edge_shard_size=None, resume=True)
¶
Build a graph from a dense feature matrix.
24.10.0.1 Parameters¶
X:
A 2D dense array-like of shape (n_nodes, n_features).
spec:
GraphBuilderSpec controlling scheme/backend/weights/normalization.
seed:
Seed used for deterministic components (notably the anchor scheme).
dataset_fingerprint:
Optional precomputed fingerprint for X (useful when X is already cached upstream).
preprocess_fingerprint:
Optional fingerprint of the preprocessing pipeline.
cache:
Whether to cache the built graph on disk.
cache_dir:
Override the default cache directory.
edge_shard_size:
If provided, store the edge arrays in sharded .npz files with at most this many
edges per shard.
resume:
If True and cache=True, partial numpy chunk computations are resumed from the
cache entry work directory when available.
24.10.0.2 Returns¶
GraphArtifact
Source code in src/modssc/graph/construction/api.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
24.11
graph_to_views(dataset, *, spec, seed=0, cache=None, cache_dir=None)
¶
Compute one or more views from a (graph, X) dataset.
Source code in src/modssc/graph/featurization/api.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |