Pore graph (region adjacency graph)

A segmentation gives you pockets and the windows between them. The pore graph (also called the region adjacency graph, RAG) turns that into a networkx graph: pockets are nodes and windows are edges. This page builds the graph two ways, reads its attributes, and plots it on top of the framework.

It assumes you have seen the Quick start. Parameter defaults and safe ranges are on Tuning and safe ranges.

Structure, grid, and segmentation

Show code
import numpy as np
from ase.io import read
import mofography as mgr

atoms = read("../examples/TestCIFs/P1-IRMOF-1.cif")   # docs/ is the working directory
atoms_bonded = mgr.update_with_covalent_bonds(atoms)    # adds bonds for plotting

spacing = 0.5
dgrid = mgr.dgrid_from_atoms_rust(atoms, spacing=spacing).astype(np.float32)

do_periodic_segmentation_rust finds the pockets, merges periodic images, and detects the windows in one call. See the Quick start for what each key holds.

Show code
seg = mgr.do_periodic_segmentation_rust(
    dgrid, atoms, spacing=spacing, return_dgrid=True, disable_tqdm=True,
)
regions = seg["regions_pbc"]
maxima = seg["maxima_pbc"]
pbc_groups = seg["pbc_groups"]

print(len(maxima), "pockets;", len(pbc_groups), "PBC groups")

Building the graph

There are two ways to build the graph from a segmentation.

Option Function What you get
1 get_rag_from_regions_and_grid The compact graph: one node per PBC-merged pocket (8 for IRMOF-1), built from the regions and the distance grid. Use it when you want one node per pocket for analysis.
2 rag_from_connections Go-to builder. One node per maximum including the periodic images in the padded cell (125 for IRMOF-1), with edges taken directly from the detected windows. It is what the Quick start uses, and it lets you control which windows become edges.
Note

Option 1 needs regions_pbc and maxima_pbc from the same segmentation, with one maximum per region. It fails on the unmerged regions array, whose region count does not match the maxima.

Option 1: from regions and the grid

Show code
rag = mgr.get_rag_from_regions_and_grid(regions, dgrid, atoms, maxima)
print(rag.number_of_nodes(), "nodes,", rag.number_of_edges(), "edges")
8 nodes, 12 edges

Option 2: from the window list

seg["connections"] holds the windows. See Connections and windows for how they are detected.

Show code
rag_c = mgr.rag_from_connections(seg["connections"], seg["all_maxima"])
print(rag_c.number_of_nodes(), "nodes (with periodic images),", rag_c.number_of_edges(), "edges")
125 nodes (with periodic images), 54 edges

Reading graph attributes

The graph is a standard networkx object, so every networkx function works on it. Nodes and edges:

Show code
import networkx as nx

print("nodes:", list(rag.nodes()))
print("edges:", list(rag.edges())[:8], "...")
nodes: [np.int64(8), np.int64(3), np.int64(2), np.int64(1), np.int64(7), np.int64(6), np.int64(5), np.int64(4)]
edges: [(np.int64(8), np.int64(3)), (np.int64(8), np.int64(1)), (np.int64(8), np.int64(5)), (np.int64(3), np.int64(2)), (np.int64(3), np.int64(7)), (np.int64(2), np.int64(1)), (np.int64(2), np.int64(4)), (np.int64(1), np.int64(6))] ...

Turn on data=True to get the attributes. Node 0 is reserved for the background, so pockets start at 1:

Show code
rag.nodes(data=True)[1]
{'labels': [np.int64(1)],
 'pixel count': 9104,
 'total color': array([18444.41698402, 18444.41698402, 18444.41698402]),
 'mean color': array([2.02596847, 2.02596847, 2.02596847]),
 'centroid': array([12.66761538, 12.66761538, 12.66761538]),
 'centroid_indices': (np.float64(25.5), np.float64(25.5), np.float64(25.5)),
 'maxima': array([ 0.        , 12.41923077, 24.83846154]),
 'maxima_indices': array([ 0, 25, 50]),
 'maxima_radii': np.float32(5.4933887)}

Pull one attribute for every node with nx.get_node_attributes. Use nx.set_node_attributes to write one. See the networkx documentation; edges work the same way.

Show code
nx.get_node_attributes(rag, "maxima")
{np.int64(8): array([25.33523077, 25.33523077,  0.        ]),
 np.int64(3): array([ 0.        , 24.83846154, 12.916     ]),
 np.int64(2): array([ 0.        , 12.916     , 12.41923077]),
 np.int64(1): array([ 0.        , 12.41923077, 24.83846154]),
 np.int64(7): array([12.916     , 25.33523077, 12.41923077]),
 np.int64(6): array([12.916, 12.916,  0.   ]),
 np.int64(5): array([12.41923077,  0.        ,  0.49676923]),
 np.int64(4): array([11.92246154, 12.916     , 12.41923077])}

Adding properties

Add the periodic-boundary information to the graph. Each node gets a pbc_group key naming the group its periodic images belong to:

Show code
rag = mgr.add_pbc_to_rag(rag, seg["pbc_groups"])
rag.nodes[1]
{'labels': [np.int64(1)],
 'pixel count': 9104,
 'total color': array([18444.41698402, 18444.41698402, 18444.41698402]),
 'mean color': array([2.02596847, 2.02596847, 2.02596847]),
 'centroid': array([12.66761538, 12.66761538, 12.66761538]),
 'centroid_indices': (np.float64(25.5), np.float64(25.5), np.float64(25.5)),
 'maxima': array([ 0.        , 12.41923077, 24.83846154]),
 'maxima_indices': array([ 0, 25, 50]),
 'maxima_radii': np.float32(5.4933887),
 'pbc_group': np.int64(0)}

Pocket types are added in the same way; see Pocket type detection.

Visualizing the pore graph

To draw the graph, use plot_connections_as_it_is. It plots the detected connections (seg["connections"]) and the pockets they join, including the periodic images, so what you see is exactly what the graph was built from. Like every other mofography plotting function it returns traces, so it combines with the framework, cell, surface, and maxima traces. Build those first:

Show code
import plotly.express as px
import plotly.graph_objects as go

cscale = px.colors.qualitative.Pastel_r

frame_trace = mgr.plot_framework_as_mesh(atoms_bonded, stylize=True)
cell_trace = mgr.plot_cell(atoms_bonded)
# isovalue is a distance from the framework (Å); keep it well above mask_thickness (0.05)
surface_trace = mgr.plot_surface_dgrid(
    dgrid, isovalue=1.75, ase_atoms=atoms_bonded, opacity=0.7,
    region_labels=regions, cscale=cscale, stylize=True,
)
maxima_trace = mgr.plot_maxima(dgrid, maxima, ase_atoms=atoms_bonded)
connections_traces = mgr.plot_connections_as_it_is(
    seg["connections"], seg["all_maxima"], dgrid.shape, atoms_bonded
)

Combine them into one figure. renderer="png" keeps this page light; use renderer="notebook_connected" in Jupyter for an interactive view.

Show code
fig = go.Figure(
    data=[*frame_trace, cell_trace, surface_trace, maxima_trace, *connections_traces]
).update(layout=mgr.white_layout)
fig.show(renderer="png", width=900)

IRMOF-1 pore graph (nodes: pockets, edges: windows) over the framework and the 1.75 Å distance surface.

Where to go next