Graph & NetworkX Integration¶
- jlnn.symbolic.graph.build_networkx_graph(root_node: Node, graph: DiGraph | None = None) DiGraph[source]¶
Recursively builds a directed graph (NetworkX DiGraph) from a compiled JLNN tree.
This function transforms a hierarchy of Node objects into a graph structure, where vertices represent logical operations (gates) or variables (predicates) and edges represent a flow of truth values. Each vertex contains metadata about the node type and a color for subsequent visualization.
- Parameters:
root_node (Node) – The root node of the model (typically model.root).
graph (Optional[nx.DiGraph]) – An existing graph instance. If None, a new one will be created.
- Returns:
A NetworkX graph where vertex ids correspond to Python Node object ids.
- Return type:
nx.DiGraph
- jlnn.symbolic.graph.from_networkx_to_jlnn(graph: DiGraph, root_id: Any, rngs: Any) Node[source]¶
Reconstructs the functional JLNN computational tree from the NetworkX graph.
This function allows “importing” logical structures defined externally in the graph editor or generated by another algorithm. It requires that the vertices have the ‘node_type’ and ‘label’ attributes.
- Parameters:
graph (nx.DiGraph) – Source graph.
root_id – ID of the root vertex in the NetworkX graph.
rngs (nnx.Rngs) – Generators for initializing gate weights.
- Returns:
The reconstructed root node of the model.
- Return type:
- jlnn.symbolic.graph.get_node_attributes(graph: DiGraph) Dict[str, Dict[str, Any]][source]¶
Extracts visual attributes of vertices for libraries like matplotlib or pyvis.
- Parameters:
graph (nx.DiGraph) – Graph generated by the build_networkx_graph function.
- Returns:
Dictionary mapping vertex ids to their properties (label, color).
- Return type:
Dict
- jlnn.symbolic.graph.to_dot(graph: DiGraph) str[source]¶
Converts a NetworkX graph to DOT (Graphviz) format for advanced plotting.
- Parameters:
graph (nx.DiGraph) – Graph to convert.
- Returns:
String in DOT format.
- Return type:
str
This module enables bidirectional conversion between the internal structure of JLNN and the graph library NetworkX. This is essential for model visualization and integration with existing knowledge graphs.
Export and Visualization¶
The function build_networkx_graph transforms the hierarchy of NNX modules into a directed acyclic graph (DAG). Each node in the graph carries metadata about its type and label.
Import topology¶
Thanks to the function from_networkx_to_jlnn, it is possible to create a JLNN model directly from a NetworkX graph structure, allowing the construction of logical networks without needing to write textual formulas.