io
pleat.io ¶
File I/O for the .heg half-edge graph format (YAML-based) and the
CirclePack .p format.
CirclePackData
dataclass
¶
CirclePackData(
nodecount: int,
geometry: str | None,
alpha: int | None,
beta: int | None,
gamma: int | None,
flowers: dict[int, list[int]],
radii: ndarray | None,
centers: ndarray | None,
)
Structured contents of a CirclePack .p file.
All vertex indices are 0-indexed internally even though the file format
stores them 1-indexed. Use :func:parse_p_file to read and
:func:write_p_file to write this dataclass verbatim.
graph_to_dict ¶
graph_to_dict(
G: HalfEdgeGraph,
attributes_to_save: tuple[str, ...] = (
"pos",
"length",
"in_angle",
"color_key",
),
) -> dict
Serialise a half-edge graph to a JSON/YAML-friendly nested dict.
Vertices, half-edges, and faces are each given an opaque string label
(v0, h0, f0, ...). Cross-references are stored by label.
Numpy arrays in attributes are converted to plain Python lists, and numpy
scalars to float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
HalfEdgeGraph
|
The graph to serialise. |
required |
attributes_to_save
|
tuple[str, ...]
|
Attribute keys to copy onto each element. Other attributes are dropped. |
('pos', 'length', 'in_angle', 'color_key')
|
Returns:
| Type | Description |
|---|---|
dict
|
|
dict
|
func: |
Source code in pleat/io.py
dict_to_graph ¶
Inverse of :func:graph_to_dict: reconstruct a graph from its serialised dict.
The returned graph is always an :class:EuclideanPositionHEG regardless of
the source graph's class (TODO: persist the class).
Source code in pleat/io.py
save_graph ¶
save_graph(
filename: str,
graph: HalfEdgeGraph,
overwrite: bool = False,
extra_attributes_to_save: (
str | tuple[str, ...] | None
) = None,
attributes_to_save: tuple[str, ...] = (
"pos",
"length",
"in_angle",
"color_key",
),
) -> None
Save graph to a .heg (YAML) file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Output path; |
required |
graph
|
HalfEdgeGraph
|
The graph to save. |
required |
overwrite
|
bool
|
If False and the file already exists, raise instead of overwriting. |
False
|
extra_attributes_to_save
|
str | tuple[str, ...] | None
|
Convenience: attribute key(s) to save in addition to attributes_to_save. |
None
|
attributes_to_save
|
tuple[str, ...]
|
Attribute keys to persist (see :func: |
('pos', 'length', 'in_angle', 'color_key')
|
Source code in pleat/io.py
load_graph ¶
Load a graph previously saved by :func:save_graph.
Source code in pleat/io.py
parse_p_file ¶
Parse a CirclePack .p file into a :class:CirclePackData dataclass.
Source code in pleat/io.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | |
write_p_file ¶
Write a :class:CirclePackData verbatim to a CirclePack .p file.
Vertex labels and the per-vertex neighbor list order in data.flowers
are preserved exactly, so :func:parse_p_file ∘ :func:write_p_file
round-trips losslessly (modulo float precision).
Source code in pleat/io.py
load_circlepack ¶
Load a CirclePack .p file into an :class:EuclideanPositionHEG.
Populates v['pos'] and v['radius'] on every vertex when the file
provides RADII (and CENTERS, where applicable). For hyperbolic packings
the file's RADII are interpreted as x-radii; the returned graph stores
the corresponding euclidean (Poincaré-disk) center and radius, matching
:func:pleat.circle_packing.pack_hyperbolic's output convention. If a
hyperbolic file omits CENTERS, the layout is computed via
:func:pleat.circle_packing._layout_hyperbolic.
The returned graph's geometry is set to :class:EuclideanGeometry or
:class:PoincareDiskModel based on the file's GEOMETRY line; unknown or
missing values default to :class:EuclideanGeometry.
Source code in pleat/io.py
save_circlepack ¶
Save an :class:EuclideanPositionHEG to a CirclePack .p file.
Emits FLOWERS for the triangulation, plus RADII / CENTERS if every vertex
has radius / pos attributes, plus GEOMETRY based on G.geometry.
Hyperbolic packings (G.geometry is PoincareDiskModel) are emitted with
x-radii in the RADII section, matching CirclePack's convention.