Skip to content

Files (2D)

Read and write dw2d meshes in the 2-D .rec format, through iorec. The names and arrays mirror dw3d.io's.

A mesh is (points, segments, labels), as dw2d.MeshReconstructionAlgorithm.construct_mesh_from_segmentation_mask returns it: points (n, 2) float64, one point per row, in the mask's index order (row, column) (as dw3d's points are in the mask's (z, y, x)), in pixels unless built in physical units; segments (m, 2) int64, the two vertex indices of each edge; labels (m, 2) int64, the two materials each edge separates, smaller first (0 is the medium). The files store the points as given.

The file layout is iorec's 2-D .rec (iorec.load_2d_rec, iorec.save_2d_rec). Binary: the number of points (uint64), the points (2 float64 each), the number of edges (uint64), then per edge its 2 vertex indices (int64) and its 2 materials (int32), little-endian. Text: the point count, one line of 2 coordinates per point, the edge count, one v1 v2 m1 m2 line per edge; every value is written exactly (the shortest text that reads back to the same float64). Either layout may be followed by the metadata appendix (version 1, as dw3d writes it), which records dimension = 2 when written by dw2d's algorithm, so that a reader can tell a 2-D file from a 3-D one without probing its layout (dw3d.io.rec_dimension), and, since 0.8.0, orientation = boundary_of_first_label (ORIENTATION_RECORDED): each edge is the positively oriented boundary of its first label in (row, column), the rule of dw3d's triangles.

load_rec

load_rec(filename: str | Path) -> tuple[NDArray[np.float64], NDArray[np.int64], NDArray[np.int64]]

Read a 2-D .rec (or .arec) mesh file, binary or text (detected from the content).

A trailing metadata appendix, if any, is ignored; read it with load_rec_appendix.

Parameters:

Name Type Description Default
filename str | Path

the path to the file on disk.

required

Raises:

Type Description
FileNotFoundError

if the file does not exist.

ValueError

if the path is not a .rec or .arec file.

Returns:

Name Type Description
tuple tuple[NDArray[float64], NDArray[int64], NDArray[int64]]

(points, segments, labels): points (n, 2) float64, segments (m, 2) int64 and labels (m, 2) int64.

load_rec_appendix

load_rec_appendix(filename: str | Path) -> dict[str, Any] | None

Read the trailing metadata appendix of a .rec/.arec file, if present, with dw2d's keys typed.

Returns:

Type Description
dict[str, Any] | None

dict[str, Any] | None: the appendix fields (dimension an int, spacing a tuple of floats, ...), or None when the file has none.

save_rec

save_rec(filename: str | Path, points: NDArray[float64], segments: NDArray[int64], labels: NDArray[int64], binary_mode: bool = False, metadata: dict[str, Any] | None = None) -> None

Write a 2-D mesh to a .rec file, binary or text.

Binary mode is the more compact; text mode can be read by eye. The layouts are in this module's docstring.

Parameters:

Name Type Description Default
filename str | Path

where to write the file.

required
points NDArray[float64]

(n, 2) vertex coordinates, written as given (float64).

required
segments NDArray[int64]

(m, 2) vertex indices of each edge.

required
labels NDArray[int64]

(m, 2) the two materials each edge separates.

required
binary_mode bool

write the binary layout. Defaults to False (text).

False
metadata dict[str, Any] | None

appendix fields (dimension, axis_order, coordinate_frame, spacing, spacing_unit, image_border_vertices, ...), written as a version-1 appendix after the geometry. Defaults to None, which writes no appendix (the file dw2d 0.1.5 wrote, byte for byte, in binary).

None

Raises:

Type Description
ValueError

if points is not (n, 2), or segments or labels not (m, 2).