pnm 1.0.3
peclet.pnm — pore-network extraction from SDF geometry (Kokkos)
Loading...
Searching...
No Matches
verify_segmentation.py
Go to the documentation of this file.
1"""Verify the SDF segmentation / geometry import.
2
3Run: PYTHONPATH=<pnm/build> python scripts/verify_segmentation.py <sdf.vti>
4Writes the labelled volume (segmentation.vti) and the pore-pair edge list (network.edges).
5"""
6import argparse
7import os
8
9import numpy as np
10
11import peclet.pnm as pnm
12from vti import save_vti # scripts/vti.py — the (Nz,Ny,Nx) VTI writer beside this script
13
14def verify_segmentation(input_file, output_file, edge_file):
15 print(f"Reading {input_file}...")
16 # New Binding returns: (numpy_array_3d, origin_zyx, spacing_zyx)
17 sdf_3d, origin, spacing = pnm.SDFReader.read_vti(input_file)
18
19 # Resolution/Shape is now inherent in the array
20 shape = sdf_3d.shape
21 print(f"Grid Shape (Nz, Ny, Nx): {shape}")
22
23 print("Running Segmentation...")
24 seg_3d = pnm.segment_volume(sdf_3d, spacing) # int32 (Nz,Ny,Nx), same shape as the SDF
25
26 # Stats logic
27 unique_labels = np.unique(seg_3d)
28 pores = unique_labels[unique_labels > 0]
29 solids = unique_labels[unique_labels < 0]
30
31 print(f"Total Labels: {len(unique_labels)}")
32 print(f"Pore IDs: {len(pores)}")
33 print(f"Solid IDs: {len(solids)}")
34
35 # Save output using the general saver
36 print(f"Saving segmented volume to {output_file}...")
37 save_vti(output_file,
38 fields={"Labels": seg_3d, "SDF": sdf_3d},
39 spacing=spacing,
40 origin=origin)
41
42 print("Extracting Topology...")
43 connections = pnm.extract_topology(seg_3d) # (M,2) int32
44 print(f"Found {len(connections)} connections "
45 f"({int(((connections[:, 0] > 0) & (connections[:, 1] > 0)).sum())} pore-pore).")
46
47 with open(edge_file, "w") as f:
48 for u, v in connections:
49 f.write(f"{u} {v}\n")
50 print(f"Saved {edge_file}")
51
52if __name__ == "__main__":
53 parser = argparse.ArgumentParser(description="PNM Segmentation Verification")
54 parser.add_argument("input", help="Input SDF VTI file")
55 parser.add_argument("-o", "--output", default="segmentation.vti", help="Output Labels VTI")
56 parser.add_argument("-e", "--edges", default="network.edges", help="Output edge list")
57
58 args = parser.parse_args()
59
60 if os.path.exists(args.input):
61 verify_segmentation(args.input, args.output, args.edges)
62 else:
63 print(f"Error: {args.input} not found.")