Skip to content

wrappers

Element dataclass

A minimalist raw data container.

Source code in src/dxtr/utils/wrappers.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
@dataclass
class Element():
    """A minimalist raw data container.
    """
    name: str
    count: int
    properties: list[str] = field(default_factory=list)
    rawdata: list[str] = field(default=list, repr=False)

    @property
    def dim(self) -> int:
        return ELEMENTS.index(self.name)

    @property
    def is_simplex(self) -> bool:
        return _cells_are_simplices(self.rawdata, self.dim)

    def set_rawdata(self, rawdata: str, first_line: int) -> None:
        """Sets the raw data for the element.

        Parameters
        ----------
        rawdata : str
            The raw data as a string.
        first_line : int
            The first line of the raw data to set.
        """
        last_line = first_line + self.count
        self.rawdata = rawdata[first_line: last_line]

    def extract_positions(self) -> Optional[np.ndarray[float]]:
        """Extracts position vectors of the vertices from the raw data.

        Returns
        -------
        np.ndarray or None
            A Nx3 array of floats. Where N = number of vertices.
        """

        if not _position_in(self.properties): return None

        positions = [[coord for coord in line.split(' ')[:3]] 
                     for line in self.rawdata]

        return np.asarray(positions, dtype=float)

    def extract_simplices(self) -> Optional[list[list[int]]]:
        """Extracts highest order simplices from the raw data.

        Returns
        -------
        list of list of int or None
            A list of lists of indices of the vertices forming the summits
            of all the highest order simplices in the structure.
        """

        if not _vertices_in(self.properties): 
            return None
        elif not _cells_are_simplices(self.rawdata, self.dim):
            return _simplicies_from_cells(self.rawdata)
        else:
            vtx_nbr = self.dim + 1
            return [[int(vtx_id) for vtx_id in line.split(' ')[1: vtx_nbr+1]] 
                for line in self.rawdata]

extract_positions()

Extracts position vectors of the vertices from the raw data.

Returns:

Type Description
ndarray or None

A Nx3 array of floats. Where N = number of vertices.

Source code in src/dxtr/utils/wrappers.py
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
def extract_positions(self) -> Optional[np.ndarray[float]]:
    """Extracts position vectors of the vertices from the raw data.

    Returns
    -------
    np.ndarray or None
        A Nx3 array of floats. Where N = number of vertices.
    """

    if not _position_in(self.properties): return None

    positions = [[coord for coord in line.split(' ')[:3]] 
                 for line in self.rawdata]

    return np.asarray(positions, dtype=float)

extract_simplices()

Extracts highest order simplices from the raw data.

Returns:

Type Description
list of list of int or None

A list of lists of indices of the vertices forming the summits of all the highest order simplices in the structure.

Source code in src/dxtr/utils/wrappers.py
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
def extract_simplices(self) -> Optional[list[list[int]]]:
    """Extracts highest order simplices from the raw data.

    Returns
    -------
    list of list of int or None
        A list of lists of indices of the vertices forming the summits
        of all the highest order simplices in the structure.
    """

    if not _vertices_in(self.properties): 
        return None
    elif not _cells_are_simplices(self.rawdata, self.dim):
        return _simplicies_from_cells(self.rawdata)
    else:
        vtx_nbr = self.dim + 1
        return [[int(vtx_id) for vtx_id in line.split(' ')[1: vtx_nbr+1]] 
            for line in self.rawdata]

set_rawdata(rawdata, first_line)

Sets the raw data for the element.

Parameters:

Name Type Description Default
rawdata str

The raw data as a string.

required
first_line int

The first line of the raw data to set.

required
Source code in src/dxtr/utils/wrappers.py
117
118
119
120
121
122
123
124
125
126
127
128
def set_rawdata(self, rawdata: str, first_line: int) -> None:
    """Sets the raw data for the element.

    Parameters
    ----------
    rawdata : str
        The raw data as a string.
    first_line : int
        The first line of the raw data to set.
    """
    last_line = first_line + self.count
    self.rawdata = rawdata[first_line: last_line]

Property dataclass

A minimalist container to store property name, type and eventually data.

Source code in src/dxtr/utils/wrappers.py
166
167
168
169
170
171
172
173
@dataclass
class Property():
    """A minimalist container to store property name, type and eventually data.
    """
    name: str
    type: str
    dim: int = field(default=None, repr=False)
    values: Any = field(default=None, repr=False)

UGrid

Bases: UnstructuredGrid

The pyvista.UnstructuredGrid with a useful classmethod on top.

Notes

The only add-on compared to the mother class is the generate_from class method that enables to directly instantiate an UnstructuredGrid from the dxtr own data structures.

Source code in src/dxtr/utils/wrappers.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class UGrid(pv.UnstructuredGrid):
    """The pyvista.UnstructuredGrid with a useful classmethod on top.

    Notes
    -----
    The only add-on compared to the mother class is the 
    `generate_from` class method that enables to directly instantiate
    an UnstructuredGrid from the `dxtr` own data structures.
    """

    def __init__(self, cells, cell_types, points, *args, **kwargs):
        super().__init__(cells, cell_types, points, *args, **kwargs)

    @classmethod
    def generate_from(cls, obj: SimplicialComplex | Cochain, 
                      **kwargs) -> Optional[UGrid]:
        """Instantiates `UnstructuredGrid` from `SimplicialComplex` or `Cochain`.

        Parameters
        ----------
        obj : SimplicialComplex or Cochain
            The object we want to transform into a `pv.UnstructuredGrid`.

        Other Parameters
        ----------------
        scaling_factor : float, optional
            A scaling factor to apply to the `Cochain` values to ease the 
            visualization. Default is 1.
        for_visualization : bool, optional
            If True, scalar-valued 1-`Cochain` are converted into vectors in 
            order to be drawn in pyvista. Default is False.

        Returns
        -------
        UGrid or None
            The desired `pv.UnstructuredGrid`.
        """

        ugrid_from = {SimplicialComplex: _ugrid_from_simplicialcomplex,
                      SimplicialManifold: _ugrid_from_simplicialcomplex,
                      Cochain: _ugrid_from_cochain}

        return ugrid_from[type(obj)](obj, **kwargs)

generate_from(obj, **kwargs) classmethod

Instantiates UnstructuredGrid from SimplicialComplex or Cochain.

Parameters:

Name Type Description Default
obj SimplicialComplex or Cochain

The object we want to transform into a pv.UnstructuredGrid.

required

Other Parameters:

Name Type Description
scaling_factor float

A scaling factor to apply to the Cochain values to ease the visualization. Default is 1.

for_visualization bool

If True, scalar-valued 1-Cochain are converted into vectors in order to be drawn in pyvista. Default is False.

Returns:

Type Description
UGrid or None

The desired pv.UnstructuredGrid.

Source code in src/dxtr/utils/wrappers.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
@classmethod
def generate_from(cls, obj: SimplicialComplex | Cochain, 
                  **kwargs) -> Optional[UGrid]:
    """Instantiates `UnstructuredGrid` from `SimplicialComplex` or `Cochain`.

    Parameters
    ----------
    obj : SimplicialComplex or Cochain
        The object we want to transform into a `pv.UnstructuredGrid`.

    Other Parameters
    ----------------
    scaling_factor : float, optional
        A scaling factor to apply to the `Cochain` values to ease the 
        visualization. Default is 1.
    for_visualization : bool, optional
        If True, scalar-valued 1-`Cochain` are converted into vectors in 
        order to be drawn in pyvista. Default is False.

    Returns
    -------
    UGrid or None
        The desired `pv.UnstructuredGrid`.
    """

    ugrid_from = {SimplicialComplex: _ugrid_from_simplicialcomplex,
                  SimplicialManifold: _ugrid_from_simplicialcomplex,
                  Cochain: _ugrid_from_cochain}

    return ugrid_from[type(obj)](obj, **kwargs)