Skip to main content

sandx-graph

Graph intelligence engine — knowledge graph construction, neighborhood consensus, semantic linkage.

CI Python 3.10+ License: Apache 2.0

Part of the SandX Lab computational infrastructure ecosystem.


What It Does

sandx-graph is the graph reasoning layer that operates downstream of sandx-er. It constructs knowledge graphs from resolved entity clusters and computes neighborhood consensus — a measure of how strongly each node's local neighborhood agrees.

sandx-er clusters  →  GraphBuilder  →  KnowledgeGraph  →  ConsensusEngine  →  consensus scores

Status

v0.1 — Working

Component Status
GraphBuilder — construct graphs from clusters, DataFrames, similarity matrices Working
KnowledgeGraph — undirected weighted graph with adjacency traversal Working
ConsensusEngine — BFS neighborhood consensus computation Working
NetworkX export Working (optional dep)
PyPI package Working

Installation

pip install sandx-graph

Or from source:

git clone https://github.com/sandxlab/sandx-graph
cd sandx-graph
pip install -e ".[dev]"

For NetworkX export:

pip install "sandx-graph[networkx]"

Demo

pip install sandx-graph
python -m examples.graph_consensus

Constructs a 5-node knowledge graph of tech companies, scores neighborhood consensus, and prints the weighted edge list — no external data required.

Quick Start

From sandx-er resolution output

import pandas as pd
from sandx_er import EntityResolver
from sandx_graph import GraphBuilder, ConsensusEngine

# Resolve records into entity clusters
records = pd.DataFrame({
    "name": ["Acme Corp", "Acme Corp.", "GlobalTech Inc", "Global Tech"],
    "city": ["Boston", "Boston", "New York", "New York"],
})
er = EntityResolver(blocking="lsh", similarity="jaccard", threshold=0.4)
result = er.resolve(records)

# Build knowledge graph from resolved clusters
builder = GraphBuilder()
graph = builder.from_clusters(result.clusters)
print(graph)  # KnowledgeGraph(n_nodes=2, n_edges=0)

# Add relationship edges (here via similarity matrix)
import numpy as np
ids = [c.canonical_id for c in result.clusters]
sim = np.array([[1.0, 0.3], [0.3, 1.0]])
graph = builder.from_similarity_matrix(ids, sim, threshold=0.5)

From DataFrames

import pandas as pd
from sandx_graph import GraphBuilder, ConsensusEngine

nodes_df = pd.DataFrame({"node_id": ["e1", "e2", "e3"], "label": ["Acme", "GlobalTech", "Initech"]})
edges_df = pd.DataFrame({"source": ["e1", "e2"], "target": ["e2", "e3"], "weight": [0.85, 0.62]})

builder = GraphBuilder()
graph = builder.from_dataframe(nodes_df, edges_df)

# Compute neighborhood consensus
engine = ConsensusEngine(graph)
score = engine.compute("e1", depth=2)
print(score)
# ConsensusScore(node='e1', score=0.735, support=2, conflict=0)

# Batch over all nodes
all_scores = engine.compute_all(depth=1)
stats = engine.summary(depth=1)
print(stats)
# {'mean': 0.735, 'median': 0.735, 'std': 0.115, 'min': 0.620, 'max': 0.850}

Consensus Score

ConsensusEngine runs BFS from a node up to a given depth, collecting all edge weights encountered. The consensus score is the weighted mean of those edges.

Score Interpretation
→ 1.0 Node connected to high-confidence, strongly agreeing neighbors
→ 0.5 Mixed neighborhood — some support, some conflict
→ 0.0 Weak or conflicting edges throughout the neighborhood

Isolated nodes (degree 0) return score 1.0 by convention.

API Reference

GraphBuilder

Method Description
from_clusters(clusters) One node per sandx-er EntityCluster; no edges
from_dataframe(nodes_df, edges_df, ...) Build from node/edge DataFrames
from_similarity_matrix(ids, similarity, threshold) Build from pairwise similarity matrix

KnowledgeGraph

Attribute / Method Description
n_nodes, n_edges Graph size
nodes Dict of node_id → attribute dict
edges List of (source, target, weight) triples
neighbors(node_id) Adjacent node IDs
neighbors_weighted(node_id) (neighbor_id, weight) pairs
degree(node_id) Number of incident edges
has_node(node_id), has_edge(a, b) Membership checks
to_dataframe() Edge list as pandas DataFrame
to_networkx() Export to NetworkX Graph

ConsensusEngine

Method Description
compute(node_id, depth=2) Consensus score for one node
compute_all(depth=2) Scores for all nodes
summary(depth=1) Mean/median/std/min/max over all nodes

Related

License

Apache 2.0 — see LICENSE

Release files for sandx-graph 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sandx-graph 0.1.1
File Size Uploaded
sandx_graph-0.1.1.tar.gz 14.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sandx-graph 0.1.1
File Interpreter ABI Platform
sandx_graph-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 26.0 kB

Release files / sandx_graph-0.1.1.tar.gz

Download URL sandx_graph-0.1.1.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
cac1431600a663e8ac09586f5828b56f72f5aafd5b22b88e8383050b2d53754b
BLAKE2b-256 checksum
How to use checksums
afb82578ccf663d7c25232d8dbed7b7c2bf942a983509e348950083ec0994165
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / sandx_graph-0.1.1-py3-none-any.whl

Download URL sandx_graph-0.1.1-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
80a1d3c465f65be6e501d98787cea4d8134eab2203630038d51963337cd5fc88
BLAKE2b-256 checksum
How to use checksums
b9f3db424b65610866e678fff98243e506d13027ab40e23edba05268ff5d364a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page