Implementation:Neuml Txtai NetworkX Graph
| Knowledge Sources | |
|---|---|
| Domains | Graph Networks, Community Detection, Network Analysis |
| Last Updated | 2026-02-10 01:00 GMT |
Overview
Concrete tool for NetworkX-backed graph network operations with community detection provided by txtai.
Description
The NetworkX class is a full graph implementation backed by the NetworkX library. It extends the base Graph class, providing concrete implementations for all node and edge operations, graph algorithms (centrality, PageRank, shortest path), community detection (Louvain, greedy modularity, label propagation), and openCypher query execution via the Query class. The class supports serialization through both a modern serialization format and a legacy TAR-based format for backwards compatibility. It implements a custom distance function for shortest path computation that converts similarity weights to distances while filtering near-duplicates. The class also provides dictionary-based import/export via NetworkX's json_graph module for interoperability.
Usage
Use NetworkX as the default graph backend in txtai when working with in-memory graph networks. It is the primary graph implementation for building knowledge graphs, running community detection for topic modeling, executing openCypher queries, and computing graph-based analytics such as centrality and PageRank. It is suitable for moderate-scale graphs that fit in memory.
Code Reference
Source Location
- Repository: Neuml_Txtai
- File:
src/python/txtai/graph/networkx.py
Signature
class NetworkX(Graph):
def __init__(self, config)
def create(self)
def count(self)
def scan(self, attribute=None, data=False)
def node(self, node)
def addnode(self, node, **attrs)
def addnodes(self, nodes)
def removenode(self, node)
def hasnode(self, node)
def attribute(self, node, field)
def addattribute(self, node, field, value)
def removeattribute(self, node, field)
def edgecount(self)
def edges(self, node)
def addedge(self, source, target, **attrs)
def addedges(self, edges)
def hasedge(self, source, target=None)
def centrality(self)
def pagerank(self)
def showpath(self, source, target)
def isquery(self, queries)
def parse(self, query)
def search(self, query, limit=None, graph=False)
def communities(self, config)
def load(self, path)
def save(self, path)
def loaddict(self, data)
def savedict(self)
def louvain(self, config)
def distance(self, source, target, attrs)
def loadtar(self, path)
Import
from txtai.graph.networkx import NetworkX
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| config | dict | Yes | Graph configuration dictionary inherited from the base Graph class. Community detection configuration keys include algorithm ("louvain", "greedy", or "lpa"), resolution (default 100), and level ("first" or "best" for Louvain, default "best").
|
| node | int/str | Yes (for node ops) | Node identifier for node retrieval, addition, or removal operations. |
| source | int/str | Yes (for edge/path ops) | Source node id for edge operations and shortest path computation. |
| target | int/str | No | Target node id for edge operations and path computation. When None in hasedge, checks for any edge on source.
|
| query | str or dict | Yes (for search) | An openCypher query string or a parsed query dictionary for graph search operations. |
| limit | int | No | Maximum number of results to return from search. |
| path | str | Yes (for load/save) | Filesystem path for graph serialization. |
Outputs
| Name | Type | Description |
|---|---|---|
| create() | nx.Graph | A new empty NetworkX Graph instance used as the backend. |
| count() | int | Total number of nodes in the graph. |
| scan() | iterator | Node iterator, optionally filtered by attribute presence, with optional attribute data. |
| node() | dict or None | Node attribute dictionary, or None if node not found. |
| edges() | dict or None | Dictionary of {target_node: edge_attributes} sorted by weight descending, or None if no edges. |
| centrality() | dict | Dictionary of {node_id: centrality_score} sorted by score descending. |
| pagerank() | dict | Dictionary of {node_id: pagerank_score} sorted by score descending. |
| showpath() | list | Ordered list of node ids along the shortest path, using custom distance weighting. |
| search() | list or Graph | List of row dicts (default) or a filtered subgraph when graph=True.
|
| communities() | list | List of community node sets from the selected detection algorithm. |
| distance() | float | Distance value between 0.0 and 1.0 (clamped to 1.0 for near-duplicates below 0.15 threshold). |
Usage Examples
from txtai.graph.networkx import NetworkX
# Create a NetworkX-backed graph
config = {
"topics": {
"algorithm": "louvain",
"resolution": 100,
"level": "best",
"terms": 4
},
"limit": 15,
"minscore": 0.1
}
graph = NetworkX(config)
graph.initialize()
# Add nodes and edges
graph.addnode(0, id="doc1", text="Machine learning")
graph.addnode(1, id="doc2", text="Deep learning")
graph.addedge(0, 1, weight=0.85)
# Run graph algorithms
centrality = graph.centrality()
pagerank = graph.pagerank()
path = graph.showpath(0, 1)
# Execute an openCypher query
results = graph.search("MATCH (n) WHERE n.text = 'Machine learning' RETURN n")
# Community detection
communities = graph.communities({"algorithm": "louvain", "resolution": 100})
# Serialize
graph.save("/path/to/graph")
graph.load("/path/to/graph")