Skip to content

Periodic meshes

vertax.meshes.pbc_mesh

Periodic Boundary Condition on a mesh, useful for bulk tissue dynamics with no explicit boundaries.

PbcMesh

PbcMesh()

Bases: Mesh

Periodic Boundary Condition on a mesh.

For a PbcMesh, vertices is a 2D array of floats of size (nb_vertices, 2) ; with the coordinates of the vertices (in ]0, width[ x ]0, height[ ). The floats use JAX's default float dtype: float32, or float64 when jax_enable_x64 is enabled before the mesh is built.

edges is a 2D array of integers of size (nb_half_edges, 8), with:

  • id of previous half-edge,
  • id of next half-edge,
  • id of twin half-edge,
  • id of source vertex,
  • id of target vertex,
  • id of the face containing the half-edge.
  • x-offset (either -1, 0 or 1) depending on the target vertex crossing a boundary.
  • y-offset (either -1, 0 or 1) depending on the target vertex crossing a boundary.

faces is a 1D array of integers of size (nb_faces) containing the id of a half-edge belonging to this face.

Do not call the constructor directly, use the dedicated class methods such as:

  • from_random_seeds,
  • from_seeds,
  • from_image,
  • from_mask,
  • create_empty.

MAX_EDGES_IN_ANY_FACE instance-attribute

MAX_EDGES_IN_ANY_FACE: int = 20

Optimization parameter : must be more than the estimated maximum number of edges in a face. Base value is 20.

from_random_seeds classmethod

from_random_seeds(nb_seeds: int, width: float, height: float, random_key: int) -> Self

Create a Periodic Boundary Conditions Mesh from a Voronoi diagram based on random seeds.

Parameters:

Name Type Description Default
nb_seeds int

Number of random seeds to use.

required
width float

Width of the rectangular domains the seeds will be in.

required
height float

Height of the rectangular domains the seeds will be in.

required
random_key int

Set the random key for reproducibility.

required

Returns:

Type Description
Self

The corresponding mesh.

from_seeds classmethod

from_seeds(seeds: Array, width: float, height: float) -> Self

Create a Periodic Boundary Conditions Mesh from a Voronoi diagram based on a list of seeds.

The seeds are assumed to have positive x and y positions.

Parameters:

Name Type Description Default
seeds Array

jax float array of seed positions of shape (nbSeeds, 2).

required
width float

width of the box containing the seeds.

required
height float

height of the box containing the seeds.

required

from_image classmethod

from_image(image: NDArray, gpu: bool | None = None) -> Self

Create a rudimentary mesh with periodic boundary conditions from an image.

To do that, we perform a segmentation using Cellpose and we try to fill the holes. The result will probably be imperfect and it will always be better if you provide directly a mask (with no holes) with PbcMesh.from_mask.

The mask is made periodic by mirroring its edges.

Parameters:

Name Type Description Default
image NDArray

The image which will act as a template for the mesh.

required
gpu bool | None

Device used by Cellpose for the segmentation. None (default) uses a CUDA GPU when one is available and the CPU otherwise (never Apple's GPU); True and False force the GPU and the CPU.

None

Returns:

Type Description
Self

The corresponding mesh.

Warning

Junction extraction is unreliable on real masks, and this method is currently unsupported for them. It builds the mesh with mask_analysis.find_vertices_edges_faces, which keeps a label triple only where exactly one 2x2 window carries it. A real trijunction is generally a small cluster of windows, so genuine junctions are discarded and spurious short cycles close in their place.

MEASURED on examples/image.tif, the image shipped with this method's own example: V - E + F = 55 and 472 faces from 64 segmented cells. On a raw microscopy crop, V - E + F = 6. A correct planar mesh gives 0.

For a segmented tissue, use vertax.trace instead, which extracts junctions on the pixel-corner lattice and is tested against a stored reference.

The gap is real and vertax.trace does not close it. trace produces a clamped patch -- a finite tissue fragment with a boundary -- not a periodic mesh. There is currently no working route from an image to a periodic mesh. Substituting the corner-lattice extraction here is the intended fix; it is deferred, and its three preconditions are recorded in the freeze checklist.

from_mask classmethod

from_mask(mask: NDArray) -> Self

Create a rudimentary mesh with periodic boundary conditions from a mask with no holes.

The mask is made periodic by mirroring its edges.

Parameters:

Name Type Description Default
mask NDArray

The mask with no holes which will act as a template for the mesh.

required

Returns:

Type Description
Self

The corresponding mesh.

Warning

Junction extraction is unreliable on real masks, and this method is currently unsupported for them. It builds the mesh with mask_analysis.find_vertices_edges_faces, which keeps a label triple only where exactly one 2x2 window carries it. A real trijunction is generally a small cluster of windows, so genuine junctions are discarded and spurious short cycles close in their place.

MEASURED on examples/image.tif, the image shipped with this method's own example: V - E + F = 55 and 472 faces from 64 segmented cells. On a raw microscopy crop, V - E + F = 6. A correct planar mesh gives 0.

For a segmented tissue, use vertax.trace instead, which extracts junctions on the pixel-corner lattice and is tested against a stored reference.

The gap is real and vertax.trace does not close it. trace produces a clamped patch -- a finite tissue fragment with a boundary -- not a periodic mesh. There is currently no working route from an image to a periodic mesh. Substituting the corner-lattice extraction here is the intended fix; it is deferred, and its three preconditions are recorded in the freeze checklist.

create_empty classmethod

create_empty() -> Self

Create an empty mesh. Use if you know what you're doing !

copy_mesh classmethod

copy_mesh(other_mesh: Self) -> Self

Copy all parameters from another mesh in a new mesh.

save_mesh

save_mesh(path: str) -> None

Save mesh to a file.

All PBCMesh data is saved.

Parameters:

Name Type Description Default
path str

Path to the saved file. The extension is .npz, a numpy format.

required

load_mesh classmethod

load_mesh(path: str) -> Self

Load a mesh from a file.

All PBCMesh data is reloaded.

Parameters:

Name Type Description Default
path str

Path to the mesh file (.npz), numpy format.

required

Returns:

Type Description
Self

The mesh loaded from the numpy .npz file.

save_mesh_txt

save_mesh_txt(directory: str, vertices_filename: str = 'vertTable.txt', edges_filename: str = 'heTable.txt', faces_filename: str = 'faceTable.txt', vertices_params_filename: str = 'vertParamsTable.txt', edges_params_filename: str = 'heParamsTable.txt', faces_params_filename: str = 'faceParamsTable.txt', constants_filename: str = 'constants.txt') -> None

Save a mesh in separate text files that can be read by numpy.

Save the vertices, edges and faces tables, the vertex, edge and face parameter tables and the constants (width, height and MAX_EDGES_IN_ANY_FACE), one text file each.

Parameters:

Name Type Description Default
directory str

Path to the directory where to save the files.

required
vertices_filename str

Filename for the vertices table. Defaults to "vertTable.txt".

'vertTable.txt'
edges_filename str

Filename for the half-edges table. Defaults to "heTable.txt".

'heTable.txt'
faces_filename str

Filename for the faces table. Defaults to "faceTable.txt".

'faceTable.txt'
vertices_params_filename str

Filename for the vertices parameters table. Defaults to "vertParamsTable.txt".

'vertParamsTable.txt'
edges_params_filename str

Filename for the half-edges parameters table. Defaults to "heParamsTable.txt".

'heParamsTable.txt'
faces_params_filename str

Filename for the faces parameters table. Defaults to "faceParamsTable.txt".

'faceParamsTable.txt'
constants_filename str

Filename for width/height/MAX_EDGES_IN_ANY_FACE. Defaults to "constants.txt".

'constants.txt'

load_mesh_txt classmethod

load_mesh_txt(directory: str, vertices_filename: str = 'vertTable.txt', edges_filename: str = 'heTable.txt', faces_filename: str = 'faceTable.txt', vertices_params_filename: str = 'vertParamsTable.txt', edges_params_filename: str = 'heParamsTable.txt', faces_params_filename: str = 'faceParamsTable.txt', constants_filename: str = 'constants.txt') -> Self

Load a mesh from text files.

Load the vertices, edges and faces tables, the vertex, edge and face parameter tables and the constants (width, height and MAX_EDGES_IN_ANY_FACE), one text file each.

Parameters:

Name Type Description Default
directory str

Directory where the text files are stored.

required
vertices_filename str

Filename for the vertices table. Defaults to "vertTable.txt".

'vertTable.txt'
edges_filename str

Filename for the half-edges table. Defaults to "heTable.txt".

'heTable.txt'
faces_filename str

Filename for the faces table. Defaults to "faceTable.txt".

'faceTable.txt'
vertices_params_filename str

Filename for the vertices parameters table. Defaults to "vertParamsTable.txt".

'vertParamsTable.txt'
edges_params_filename str

Filename for the half-edges parameters table. Defaults to "heParamsTable.txt".

'heParamsTable.txt'
faces_params_filename str

Filename for the faces parameters table. Defaults to "faceParamsTable.txt".

'faceParamsTable.txt'
constants_filename str

Filename for width/height/MAX_EDGES_IN_ANY_FACE. Defaults to "constants.txt".

'constants.txt'

Returns:

Type Description
Self

The loaded mesh.

get_length

get_length(half_edge_id: Array) -> Array

Get the length of given edges.

get_length_with_offset

get_length_with_offset(half_edge_id: Array) -> Array

Get the length of given edges along with its offsets in an array (length, offset x, offset y).

get_perimeter

get_perimeter(face_id: Array) -> Array

Get the perimeter of given faces.

get_area

get_area(face_id: Array) -> Array

Get the area of given faces.

update_boundary_conditions

update_boundary_conditions() -> None

Force periodic boundary conditions again after an update.

Most likely you'll never have to use it yourself, as it can be made automatically with a PbcBilevelOptimizer.