graintrace.tess_to_gnn#

Parse Neper .tess files into torch_geometric graph representations.

graintrace.tess_to_gnn.encode_signed_index(value)[source]#

Encode a signed 1-based .tess index as a signed 0-based one.

Neper writes edge and face references as signed 1-based integers, where the sign is the traversal direction. The obvious shift sign * (abs(v) - 1) is lossy: integers have no -0, so +1 and -1 both collapse to 0 and the direction of index 0 is destroyed. Negative entries are therefore stored as the bitwise complement ~(abs(v) - 1), which keeps the mapping one-to-one over the whole range.

Parameters:

value (int) – a signed, 1-based index as written in the .tess file.

Returns:

the encoded signed 0-based index. Decode it with decode_signed_index().

Return type:

int

graintrace.tess_to_gnn.decode_signed_index(value)[source]#

Decode an entry of face_edges or cell_to_faces.

Inverse of encode_signed_index(). Note that abs() is not a valid decoder: the complement of index 0 is -1, whose absolute value names a different entity.

Parameters:

value (int) – one encoded entry of face_edges or cell_to_faces.

Returns:

the 0-based index, and whether the entity is traversed in reverse (the entry was negative in the .tess file).

Return type:

tuple[int, bool]

class graintrace.tess_to_gnn.NeperTessToGraphNN(tess_path, geometry_cell_file=None, geometry_face_file=None, device='cpu', dtype=torch.float64)[source]#

Bases: object

Parse a Neper .tess file and build a graph representation (pytorch_geometric Data) with grains as nodes and faces as edges.

install_neper()[source]#

Placeholder hook for installing NEPER (not implemented).

load_geometry_information(cell_file, face_file)[source]#

Placeholder hook for loading precomputed cell/face geometry (not implemented).

run_neper_for_geometry_information(tess_path)[source]#

Placeholder hook for computing geometry via NEPER (not implemented).

parse_tess()[source]#

Parse the Neper .tess file.

validate_topology(verbose=True, strict=True)[source]#

Validate the graph connectivity: each face must belong to 1 or 2 cells.

Parameters:
  • verbose (bool) – print the per-category face counts. Printing only – whether a broken tessellation is tolerated is strict’s job.

  • strict (bool) – raise on a face claimed by more than two cells, or by none. Independent of verbose, so a quiet call will not hand back a verdict on a broken tessellation and let the caller sail past it. Pass strict=False to inspect the report instead.

Returns:

lists of face indices under the keys internal (2 cells), boundary (1 cell), nonmanifold (>2 cells) and isolated (0 cells).

Return type:

dict

Raises:

ValueError – if strict and any face is non-manifold or isolated.

register_default_features()[source]#

Register default node/edge features. Add more as needed.

register_dataframe_features(data, verbose=True)[source]#

Register every column in a pandas DataFrame as a node feature.

Parameters:
Return type:

None

activate_all_registered_features()[source]#

Activate all currently registered node and edge features.

Return type:

None

build_cell_graph()[source]#

Construct a torch_geometric Data graph using selected features.

visualize_graph_2D(graph, node_attr=None, edge_attr=None, cmap='viridis', outpath='graph_2D.png', show_node_labels=True, show_edge_labels=True)[source]#

Render a 2D NetworkX plot of the cell-cell graph and save it to outpath.

visualize_graph_3D(graph, outpath='graph_3D.png', node_attr=None, edge_attr=None, cmap='viridis', show_node_labels=False, show_edge_labels=False)[source]#

3D visualization of the cell-cell graph using centroid positions if available. Falls back to seed positions if centroids are not yet computed.