The PlainMesh class#
- class pylife.mesh.PlainMesh(pandas_obj)[source]
Access plain 2D and 3D point-cloud mesh data.
A plain mesh represents independent points with coordinates in mm. It does not encode element connectivity; therefore the DataFrame index is preserved but not interpreted by the accessor.
Signal contract:
x: Coordinate in mm along the global x-axis.y: Coordinate in mm along the global y-axis.z: Optional coordinate in mm along the global z-axis. If missing, the mesh is treated as two-dimensional.
- Parameters:
pandas_obj (pandas.DataFrame) – DataFrame carrying the coordinate columns. The index may be any pandas index and is preserved by returned objects.
- Raises:
AttributeError – If at least one of the columns
xoryis missing.
See also
pylife.mesh.meshsignal.MeshAccess meshes with node-element connectivity.
pandas.api.extensions.register_dataframe_accessorRegister pandas DataFrame accessors.
Notes
If column
zexists but all values are equal, the mesh is considered two-dimensional fordimensions.- broadcast(parameter, droplevel=None)
Broadcast the parameter to the object of
self.- Parameters:
parameter (scalar, numpy.ndarray, pandas.Series or pandas.DataFrame) – The operand to align with the object of
self.droplevel (list of str, optional) – Names of index levels that are dropped from the returned
parameterbefore it is returned. Use it when a parameter must not be resolved along those levels, e.g. when a material parameter is constant over all load steps. Default isNone, which keeps all levels.
- Returns:
parameter (scalar, pandas.Series or pandas.DataFrame) – The broadcast operand, index aligned with
obj.obj (pandas.Series or pandas.DataFrame) – The broadcast object of
self, index aligned withparameter.
Examples
The behavior of the Broadcaster is best illustrated by examples:
Broadcasting
pandas.Seriesto a scalar results in a scalar and apandas.Series.obj = pd.Series([1.0, 2.0], index=pd.Index(['foo', 'bar'], name='idx')) obj
idx foo 1.0 bar 2.0 dtype: float64
parameter, obj = Broadcaster(obj).broadcast(5.0) parameter
array(5.)
objidx foo 1.0 bar 2.0 dtype: float64
Broadcasting
pandas.DataFrameto a scalar results in apandas.DataFrameand apandas.Series.obj = pd.DataFrame({ 'foo': [1.0, 2.0], 'bar': [3.0, 4.0] }, index=pd.Index([1, 2], name='idx')) obj
foo bar idx 1 1.0 3.0 2 2.0 4.0 parameter, obj = Broadcaster(obj).broadcast(5.0) parameter
idx 1 5.0 2 5.0 dtype: float64
objfoo bar idx 1 1.0 3.0 2 2.0 4.0 Broadcasting
pandas.DataFrameto a apandas.Seriesresults in apandas.DataFrameand apandas.Series, if and only if the index name of the object isNone.obj = pd.Series([1.0, 2.0], index=pd.Index(['tau', 'chi'])) obj
tau 1.0 chi 2.0 dtype: float64
parameter = pd.Series([3.0, 4.0], index=pd.Index(['foo', 'bar'], name='idx')) parameter
idx foo 3.0 bar 4.0 dtype: float64
parameter, obj = Broadcaster(obj).broadcast(parameter) parameter
idx foo 3.0 bar 4.0 dtype: float64
objtau chi idx foo 1.0 2.0 bar 1.0 2.0
- property coordinates
Return the coordinate columns of the accessed DataFrame.
- Returns:
Coordinate columns
xandyand, for three-dimensional meshes,z. Values are coordinates in mm and the returned DataFrame carries the same index as the accessed object.- Return type:
- property dimensions
Return the spatial dimension of the mesh.
- Returns:
Spatial dimension, either
2for a planar mesh or3for a mesh with varyingzcoordinates. Coordinates are interpreted in mm.- Return type:
Notes
If column
zis missing or all values in columnzare equal, the mesh is considered two-dimensional.
- fail_if_key_missing(keys_to_check, msg=None)
Raise an exception if any key is missing in the signal.
- Parameters:
- Raises:
AttributeError – If the underlying object is neither a
pandas.DataFramenor apandas.Series, or if any ofkeys_to_checkis not provided by the signal.
See also
get_missing_keysReturn the missing keys instead of raising.
Notes
This is the canonical way to assert a signal contract in the
_validate()implementation of a derived class.
- classmethod from_parameters(**kwargs)
Make a signal instance from a parameter set.
This is a convenience function to instantiate a signal from individual parameters rather than from a pandas object.
- Parameters:
**kwargs (scalar or array_like) – The signal keys and their values. If more than one value is given and the values are iterable, a
pandas.DataFramebacked signal is created, otherwise apandas.Seriesbacked one.- Returns:
An instance of the signal class the method is called on.
- Return type:
Examples
For a signal class like
@pd.api.extensions.register_dataframe_accessor('foo_signal') class FooSignal(PylifeSignal): pass
the following two expressions are equivalent
pd.Series({'foo': 1.0, 'bar': 2.0}).foo_signal FooSignal.from_parameters(foo=1.0, bar=2.0)
- get_missing_keys(keys_to_check)
Get a list of keys that are missing in the signal.
- Parameters:
keys_to_check (list of str) – The keys that need to be available in the signal.
- Returns:
The keys of
keys_to_checkthat the signal does not provide, in the order in which they were given. An empty list means that the signal is complete.- Return type:
- Raises:
AttributeError – If the underlying object is neither a
pandas.DataFramenor apandas.Series.
See also
fail_if_key_missingRaise instead of returning the missing keys.
Notes
This method is meant to be used in the
_validate()implementation of a derived class when a missing key is not necessarily an error, e.g. when a default value can be derived.
- keys()
Get the keys the signal provides.
- Returns:
The keys of the underlying pandas object.
- Return type:
- Raises:
AttributeError – If the underlying object is neither a
pandas.DataFramenor apandas.Series.
Notes
For a
pandas.DataFramebacked signal the columns are returned, for apandas.Seriesbacked one the index.
- to_pandas()
Expose the pandas object of the signal.
- Returns:
The pandas object representing the signal.
- Return type:
Notes
The default implementation just returns the object given when instantiating the signal class. Derived classes may return a modified or augmented object, if they store some extra information.
By default the object is not copied. So make a copy yourself, if you intend to modify it.