scivianna.utils package

Scivianna Utils Module

The utils module provides utility functions and helper classes used throughout Scivianna for mesh processing, file handling, serialization, and visualization tools.

Files

File

Description

__init__.py

Package initialization file

color_tools.py

Color manipulation utilities (RGBA handling, colormap generation)

extruded_mesh.py

Tools for visualizing extruded 2D meshes in 3D

file_cleaner.py

Utilities for cleaning input files when the visualizer is closed

interface_tools.py

Helper functions for interface implementations

polygon_sorter.py

Algorithms for sorting and organizing polygon data

polygonize_tools.py

Tools for converting grid data to polygon representations

serialization.py

Save/load functionality for panel states and layouts

structured_mesh.py

Utilities for structured mesh handling

Key Utilities

Color Tools (color_tools.py)

  • RGBA color array manipulation

  • Colormap generation and application

  • Alpha/transparency adjustments

  • Color interpolation

Extruded Mesh (extruded_mesh.py)

Converts 2D meshes to 3D extruded representations:

  • Takes 2D polygon data

  • Extrudes along Z-axis

  • Generates 3D visualization coordinates

Polygonize Tools (polygonize_tools.py)

Converts raster/grid data to vector polygons:

  • Contour extraction from scalar fields

  • Polygon boundary detection

  • Mesh cell polygonization

Serialization (serialization.py)

Save and load Scivianna states:

  • GridStack layout serialization

  • Panel state persistence

  • ZIP-based save files

from scivianna.utils.serialization import save_gridstack_to_zip, load_gridstack_from_zip

# Save layout
save_gridstack_to_zip(layout, "my_save.zip")

# Load layout
layout = load_gridstack_from_zip("my_save.zip")

Structured Mesh (structured_mesh.py)

Utilities for structured mesh operations:

  • Grid generation

  • Coordinate transformations

  • Cell indexing utilities

Interface Tools (interface_tools.py)

Common helpers for interface implementations:

  • Default panel creation

  • Interface enumeration

  • File validation

Polygon Sorter (polygon_sorter.py)

Algorithms for organizing polygon data:

  • Spatial sorting for rendering order

  • Adjacency-based ordering

  • Performance optimization for large datasets

Usage Examples

Color Manipulation

from scivianna.utils.color_tools import rgba_to_hex, hex_to_rgba

# Convert between formats
hex_color = rgba_to_hex([255, 128, 0, 255])  # "#ff8000"
rgba = hex_to_rgba("#ff8000")  # [255, 128, 0, 255]

Submodules

scivianna.utils.color_tools module

scivianna.utils.color_tools.get_edges_colors(face_colors: ndarray) ndarray

Returnds the edge colors from the face colors

Parameters:

face_colors (np.ndarray) – Numpy array containing a list of RBG colors ranging from 0 to 255

Returns:

Face colors

Return type:

np.ndarray

scivianna.utils.color_tools.interpolate_cmap_at_values(cmap_name: str, values: ndarray) ndarray

Returns a numpy array containing the RGBA 255 colors per value in values

Parameters:
  • cmap_name (str) – Name of the cmaps to get from scivianna.utils.color_tools.color_maps

  • values (np.ndarray) – Values to interpolate

Returns:

RGBA 255 colors per value in values

Return type:

np.ndarray

scivianna.utils.extruded_mesh module

scivianna.utils.file_cleaner module

scivianna.utils.file_cleaner.mark_for_deletion(path: Path)

Marks a file to be deleted when the visualizer is closed.

Parameters:

path (Path) – File path

scivianna.utils.interface_tools module

class scivianna.utils.interface_tools.GenericInterfaceEnum(*values)

Bases: Enum

MED = 'Medcoupling'
scivianna.utils.interface_tools.get_interface_default_panel(interface: GenericInterfaceEnum | str, title: str = '')

Returns the default panel of built-in implemented code interfaces. Returns None if interface is unknown.

Parameters:

interface (GenericInterfaceEnum) – GenericInterfaceEnum representing the code interface

Returns:

Panel of the code interface, or None

Return type:

VisualizationPanel

scivianna.utils.interface_tools.load_available_interfaces() Dict[str, Type[GenericInterface]]

Loads the built-in available interfaces

Returns:

Available interfaces linked to their name

Return type:

Dict[str, Type[GenericInterface]]

scivianna.utils.polygon_sorter module

class scivianna.utils.polygon_sorter.PolygonSorter

Bases: object

Object used to sort a list of polygons per field

reset_indexes(*args, **kwargs)

Clears the saved sort indexes.

sort_from_value(data: Data2D)

Sorts a set of polygons per field

Parameters:

data (Data2D) – Data2D object containing the geometry properties

sort_list(data: Data2D)

Sort the value and color list of a Data2D object in the same order as the past sort_polygon_list order. If no sort_polygon_list was called, it is called instead.

Parameters:

arr (List[Any]) – List to sort

scivianna.utils.polygonize_tools module

class scivianna.utils.polygonize_tools.PolygonCoords(x_coords: List[float] | ndarray, y_coords: List[float] | ndarray)

Bases: object

Object ontaining the X and Y coordinates of a polygon

rotate(origin: Tuple[float, float], angle: float)

Rotate the PolygonElement by the angle around the origin

Parameters:
  • origin (Tuple[float, float]) – Rotation origin

  • angle (float) – Angle (in radians)

translate(dx: float, dy: float)

Translates the PolygonCoords by (dx, dy)

Parameters:
  • dx (float) – Horizontal offset

  • dy (float) – Vertical offset

x_coords: ndarray

X coordinate of each vertex of a polygon

y_coords: ndarray

Y coordinate of each vertex of a polygon

class scivianna.utils.polygonize_tools.PolygonElement(exterior_polygon: PolygonCoords, holes: List[PolygonCoords], cell_id: str)

Bases: object

Object containing the exterior polygon and the holes of a polygonal object

cell_id: str

Cell, associated to the polygon, id

compo: str

Composition in the polygon

exterior_polygon: PolygonCoords

Polygon that surrounds a polygonal object

holes: List[PolygonCoords]

List of polygonal holes in a polygon object

rotate(origin: Tuple[float, float], angle: float)

Rotate the PolygonElement by the angle around the origin

Parameters:
  • origin (Tuple[float, float]) – Rotation origin

  • angle (float) – Angle (in radians)

to_shapely(z_coord: float = None) Polygon

Returns a shapely Polygon version of self at the vertical coordinate z_coord

Parameters:

z_coord (float, optional) – If provided, the polygon is 3D at the given height, by default None

Returns:

Shapely polygon

Return type:

shapely.Polygon

translate(dx: float, dy: float)

Translates the PolygonElement by (dx, dy)

Parameters:
  • dx (float) – Horizontal offset

  • dy (float) – Vertical offset

scivianna.utils.polygonize_tools.numpy_2D_array_to_polygons(x: List[float] | ndarray, y: List[float] | ndarray, arr: ndarray, simplify: bool) List[PolygonElement]

Converts a 2D array mapping the cell id to a list of PolygonElements using the python module rasterio

Parameters:
  • x (Union[List[float], np.ndarray]) – Points coordinates along the X axis

  • y (Union[List[float], np.ndarray]) – Points coordinates along the Y acis

  • arr (np.ndarray) – 2D cell index mapping

  • simplify (bool) – Simplify the polygons to smoothen the edges

Returns:

List of PolygonElements

Return type:

List[PolygonElement]

scivianna.utils.serialization module

scivianna.utils.structured_mesh module

class scivianna.utils.structured_mesh.CarthesianStructuredMesh(x_coords: ndarray, y_coords: ndarray, z_coords: ndarray)

Bases: StructuredMesh

Carthesian structured mesh

class scivianna.utils.structured_mesh.CylindricalStructuredMesh(r_coords: ndarray, theta_coords: ndarray, z_coords: ndarray)

Bases: StructuredMesh

Cylindrical structured mesh

class scivianna.utils.structured_mesh.SphericalStructuredMesh(r_coords: ndarray, theta_coords: ndarray, phi_coords: ndarray)

Bases: StructuredMesh

Spherical structured mesh

class scivianna.utils.structured_mesh.StructuredMesh

Bases: object

Generic structured mesh class built to help used to define their own structured mesh based geometries/results

compute_2D_slice(origin: Tuple[float, float, float], u: Tuple[float, float, float], v: Tuple[float, float, float]) List[PolygonElement]

Computes the PolygonElement list for a slice of the mesh

Parameters:
  • origin (Tuple[float, float, float]) – Slice origin

  • u (Tuple[float, float, float]) – First axis vector

  • v (Tuple[float, float, float]) – Second axis vector

Returns:

List of polygon elements defining the cut

Return type:

List[PolygonElement]

Raises:

ValueError – U and V are either parallel or one is of zero length.

get_cells_values(name: str, cell_ids: List[int]) ndarray

Returns a field values for a list of cell indexes

Parameters:
  • name (str) – field name

  • cell_ids (List[int]) – cells indexes

Returns:

List of values per cell

Return type:

np.ndarray

Raises:

RuntimeError – Requested a field before defining it

grids: Dict[str, ndarray]

Numpy array allocating values to cells, coordinates (x, y, z) are expected.

mesh: pv.StructuredGrid

Pyvista structured grid

set_values(name: str, grid: ndarray)

Setting a Numpy array grid to the given name. The numpy array must be of size (nx, ny, nz) and the data are called in the XYZ order.

Parameters:
  • name (str) – Field name

  • grid (np.ndarray) – Field value

Module contents