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.
import numpy as npfrom ase.io import readimport mofography as mgratoms = read("../examples/TestCIFs/P1-IRMOF-1.cif") # docs/ is the working directoryatoms_bonded = mgr.update_with_covalent_bonds(atoms) # adds bonds for plottingspacing =0.5dgrid = 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.
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.
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.
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 pximport plotly.graph_objects as gocscale = px.colors.qualitative.Pastel_rframe_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.