Quick start

CIF to pore graph, pocket types, windows, and textural properties with two core calls

The full mofography pipeline: from a MOF crystal (a) through distance grid, watershed, PBC-aware segmentation, and pore graph construction (paper Figure 2)

Overview

Two functions do the heavy lifting. Everything else reads their output.

  1. dgrid_from_atoms_rust(atoms, spacing) computes the distance grid (distance from each voxel to the nearest framework surface).
  2. do_periodic_segmentation_rust(dgrid, atoms, ...) finds pockets, handles periodic boundaries, and detects windows. It returns one dictionary (seg).

From seg you get a pore graph, pocket types, windows, and textural properties (LCD, PLD, surface area, void volume). This page runs all of it on IRMOF-1. Parameter safe ranges live on Tuning and safe ranges.

Imports and structure

Show code
import numpy as np
import mofography as mgr
from mofography.cif_io import read_cif_to_ase  # fast Rust CIF reader, returns ase.Atoms (see the Fast Rust CIF reader page)

atoms = read_cif_to_ase("IRMOF-1.cif")   # or examples/TestCIFs/P1-IRMOF-1.cif from the repo root; ase.io.read works too
print(f"{atoms.get_chemical_formula()}: {len(atoms)} atoms")
print("Cell (Å):", atoms.cell.cellpar()[:3].round(2))
C192H96O104Zn32: 424 atoms
Cell (Å): [25.83 25.83 25.83]

1. Distance grid

Show code
spacing = 0.5  # Å. Halving it gives 8x more voxels.
dgrid = mgr.dgrid_from_atoms_rust(atoms, spacing=spacing).astype(np.float32)
print(f"Grid {dgrid.shape}, largest void radius {dgrid.max():.2f} Å")
Grid (52, 52, 52), largest void radius 7.54 Å

radii=None uses Bondi van der Waals radii. For other grid backends see Distance grid and Distance grid methods.

2. Periodic segmentation

Show code
seg = mgr.do_periodic_segmentation_rust(
    dgrid, atoms,
    spacing=spacing,             # always pass it; otherwise it is inferred
    mask_thickness=0.05,         # Å, voxels closer than this to the framework are background
    h=0.2,                       # Å, maxima persistence
    min_distance=1.5,            # Å, minimum separation between maxima
    threshold_abs=1.5,           # Å, minimum height of a maximum
    watershed_backend="rust_det",
    compute_connections=True,    # windows + connections
    return_dgrid=True,           # keep dgrid inside seg for later steps
    disable_tqdm=True,
)
print(f"Pockets (PBC-merged): {len(seg['maxima_pbc'])}")
print(f"Unique regions:       {len(seg['unique_regions'])}")
print(f"Window connections:   {len(seg['connections'])}")
print(f"PBC groups:           {len(seg['pbc_groups'])}")
Pockets (PBC-merged): 8
Unique regions:       27
Window connections:   300
PBC groups:           8

Those are the defaults (written out for clarity). seg also holds regions_pbc (labelled grid), maxima_info, all_maxima and input_params. See Region segmentation and PBC considerations.

Note

Messages starting with [pocket_maxima_check] mean a pocket’s recorded maximum is a bit lower than the true peak of its region (a few tenths of an Å). They are informational. See the tuning page if you want to remove them.

3. Pore graph

Pockets become nodes. Windows between pockets become edges.

Show code
rag = mgr.rag_from_connections(seg["connections"], seg["all_maxima"])
print(rag)
Graph with 125 nodes and 54 edges

It is a networkx graph. See Pore graph.

4. Pocket types

Show code
seg = mgr.enrich_segmentation_with_pocket_psds(seg, atoms)   # per-pocket PSDs, reused by typing
typed = mgr.assign_pocket_types(seg, atoms, method="env_cluster")
print(f"{typed.n_types} types across {typed.n_pockets} pockets")
for t in typed.type_summary:
    print(f"  type {t['type_id']}: {t['n_pockets']} pockets, "
          f"max radius {t['max_r_min_a']:.2f}-{t['max_r_max_a']:.2f} Å")

Creating region-wise PSD plots...
Unique region labels found: [1 2 3 4 5 6 7 8]
Found 8 unique regions/pockets
Region 1: 1000 samples, mean pore size: 11.62 Å, std: 0.50 Å
Region 2: 1000 samples, mean pore size: 14.10 Å, std: 1.10 Å
Region 3: 1000 samples, mean pore size: 11.64 Å, std: 0.40 Å
Region 4: 1000 samples, mean pore size: 11.23 Å, std: 0.22 Å
Region 5: 1000 samples, mean pore size: 11.65 Å, std: 0.32 Å
Region 6: 1000 samples, mean pore size: 14.12 Å, std: 1.06 Å
Region 7: 1000 samples, mean pore size: 14.07 Å, std: 1.15 Å
Region 8: 1000 samples, mean pore size: 14.87 Å, std: 0.95 Å
2 types across 8 pockets
  type 0: 4 pockets, max radius 5.63-5.85 Å
  type 1: 4 pockets, max radius 7.18-7.54 Å

More in Pocket type detection.

5. Windows

Each entry of seg["connections"] is [region_pair, flag, "internal"|"periodic", centre, radius]. Keep those that touch a unique region to avoid counting windows from the padding.

Show code
unique = seg["unique_regions"]
windows = [c for c in seg["connections"] if np.any(np.isin(c[0], unique))]
radii = np.array([float(np.ravel(c[4])[0]) for c in windows])
print(f"{len(windows)} windows in the cell; radii {radii.min():.2f}-{radii.max():.2f} Å")
108 windows in the cell; radii 3.68-3.96 Å

See Connections and windows.

6. Textural properties

LCD and PLD come from the winding method, which tests percolation across periodic images. Surface area and void volume are computed on the same seg.

Show code
lcd, pld, dfsd, _ = mgr.get_pore_dia_mgr_winding(atoms, dgrid=dgrid, results=seg)
print(f"LCD {lcd:.2f} Å | PLD {pld:.2f} Å | DFSD {dfsd:.2f} Å")

probe = 1.2  # Å, helium; use a probe radius below PLD/2 or nothing percolates
sa = mgr.compute_surface_area_mc_winding(seg, atoms, probe_radius=probe)
vv = mgr.compute_void_volume_from_results_winding(seg, atoms, probe_radius=probe)
print(f"Surface area: {sa['accessible_surface_area']:.0f} m²/g "
      f"(total {sa['total_surface_area']:.0f})")
print(f"Void volume:  {vv['accessible_void_volume']:.3f} cm³/g "
      f"(total {vv['total_void_volume']:.3f})")
LCD 15.08 Å | PLD 7.91 Å | DFSD 15.08 Å
Surface area: 3813 m²/g (total 3813)
Void volume:  0.927 cm³/g (total 0.927)

Details: Surface area and Accessibility.

7. Visualize

Show code
fig = mgr.show_mof_plotly(seg, atoms, typed_result=typed, framework_atoms=atoms, stylize=True)
fig.write_image("tutorial_images/quick_start_structure.png", width=1100, height=800)  # static preview for the docs
No node props provided, using default set: [position, scaled_position, element and atomic_number]
No edge properties provided. Use the default set: [bond_order, bond_type]
[TIMING batched_mesh_for_atoms] 472 atoms, 1024 bonds | spheres: 0.00s, cylinders: 0.00s, concat: 0.00s | total faces: 212480
No node props provided, using default set: [position, scaled_position, element and atomic_number]
No edge properties provided. Use the default set: [bond_order, bond_type]

IRMOF-1: framework, pore surface, pocket types, and windows
fig = mgr.show_mof_plotly(seg, atoms, typed_result=typed, framework_atoms=atoms, stylize=True)
fig.show()   # rotatable Plotly figure in a notebook
# or a standalone page:
mgr.show_mof_threejs(seg, atoms, typed_result=typed, output_html="mof.html")

Open the interactive version

Notes

  • mgr.quick_cluster(atoms) is a legacy one-call helper that builds a figure set (and opens a browser renderer). Use the explicit steps above in scripts.
  • Cast dgrid to float32 before segmentation, as above.
  • Defaults are a good start for most MOFs. If pocket counts look wrong, go to Tuning and safe ranges.

Next