qbiocode.apps.quvine.api.sgns module#

SGNS embedding core: views -> walks -> corpus -> word2vec.

These functions are extracted verbatim from Pipeline._run_single_iteration and its private helpers so that both the Hydra pipeline and the quvine.embed() API share a single, behavior-preserving code path.

Reproducibility invariants that MUST be preserved (the cluster pipeline depends on them):

  • Per-root RNG seed: cfg.experiment.base_seed + 10000 * it + idx where idx indexes sorted(roots).

  • Embedding row order is list(graph.nodes) (never sorted).

  • train_embeddings reads the top-level cfg.min_count.

  • joblib Parallel(backend='loky', batch_size=1, prefer='processes').

Note on the kinds argument: when a subset of walk kinds is requested, only those walkers run, which changes the per-root RNG stream relative to a full multi-kind run. Calling run_sgns with kinds=list(cfg.walks.kinds) (as the pipeline does) reproduces the original behavior exactly.

Summary#

Functions:

build_corpora

Run walks over every root and compile a per-walk-kind token corpus.

build_views

Build constrained views for a single root.

chunkify

Yield successive chunk_size-sized chunks of seq.

process_root

Build views + walks for a single root with deterministic seeding.

process_root_chunk

Process a batch of roots inside a single worker process.

run_sgns

One-shot SGNS embedding: build_corpora -> train_embeddings.

run_walks_for_root

Run every configured walk kind over each view for a single root.

train_embeddings

Train one SGNS (word2vec) embedding per walk kind.

Reference#

chunkify(seq, chunk_size)[source]#

Yield successive chunk_size-sized chunks of seq.

build_views(cfg, graph, root, rng)[source]#

Build constrained views for a single root.

run_walks_for_root(cfg, graph, root, views, rng)[source]#

Run every configured walk kind over each view for a single root.

process_root(cfg, graph, root, node2idx, it)[source]#

Build views + walks for a single root with deterministic seeding.

process_root_chunk(cfg, graph, roots, node2idx, it)[source]#

Process a batch of roots inside a single worker process.

build_corpora(cfg, graph, it=0, *, n_jobs=1, chunk_size=30)[source]#

Run walks over every root and compile a per-walk-kind token corpus.

Returns a dict {walk_kind: corpus} where each corpus is a flat list of walks (each walk a list of node-id strings).

Return type:

Dict[str, List[List[str]]]

train_embeddings(cfg, graph, all_corpora)[source]#

Train one SGNS (word2vec) embedding per walk kind. Rows in node order.

run_sgns(cfg, graph, it=0, *, kinds=None, n_jobs=1, chunk_size=30)[source]#

One-shot SGNS embedding: build_corpora -> train_embeddings.

Parameters:
  • cfg – OmegaConf config (reads walks.*, views.*, train.*, min_count, experiment.base_seed).

  • graph (Graph) – NetworkX graph.

  • it (int) – Iteration index used in the per-root seed.

  • kinds (Optional[List[str]]) – Walk kinds to compute. None uses cfg.walks.kinds.

  • n_jobs (int) – joblib parallelism over roots.

  • chunk_size (int) – joblib parallelism over roots.

Return type:

Dict[str, ndarray]

Returns:

{walk_kind: embedding} with each embedding (n_nodes, dim) in list(graph.nodes) order.