pysdic.core.property_container.PropertyContainer#

Note

This class is internal: it is not exported in the pysdic namespace. It is used to store the properties of the objects of the package (for example the properties of the points of a point cloud), and its methods are available through these objects.

PropertyContainer class#

class PropertyContainer(N, properties=None, *, force_2d=False)[source]#

Bases: JsonSerializerMixin, Hdf5SerializerMixin, NpzSerializerMixin

A container for properties associated with points or other entities.

Given \(N\) entities, each property is a named NumPy array of shape \((N, \dots)\), where \(\dots\) represents the attributes of one entity. For example:

  • a scalar property has shape \((N,)\) or \((N, 1)\),

  • a 3D vector property has shape \((N, 3)\),

  • a tensor property may have additional dimensions, e.g. \((N, 3, 3)\).

The container exposes a dictionary-like interface ([], in, keys(), …) but it is not a dict: all the properties must share the same first dimension \(N\), and their shapes are validated when they are set.

Note

The arrays are copied when they are set, and returned as read-only views. Modifying the input array after setting it, or trying to write into a returned array, cannot change the content of the container.

Parameters:
  • N (Integral) – The number of entities \(N\), i.e. the first dimension of all the property arrays.

  • properties (Dict[str, ArrayLike], optional) – Initial properties, mapping property names to arrays of shape \((N, \dots)\). By default the container is empty.

  • force_2d (bool, optional (default: False)) – If True, all properties are stored as 2D arrays of shape \((N, P)\). 1D arrays of shape \((N,)\) are reshaped to \((N, 1)\) and arrays with more than two dimensions are rejected.

Notes

The number of entities \(N\) is fixed at the creation of the container: the methods that change it (filter(), concatenate()) return a new container. A container shared by several objects (for example two point clouds built on the same container) can therefore never become inconsistent with them. The properties can still be added, modified or deleted: all the references observe these changes.

Use validate() to check the integrity of the container.

Instantiation#

The PropertyContainer class can be instantiated in two ways:

  • With the number of entities \(N\) and optional initial properties: PropertyContainer(N, properties).

  • From a dictionary of properties using the classmethod from_dict() (\(N\) is inferred from the first property).

PropertyContainer.from_dict(data, *[, N, ...])

Create a container from a dictionary of properties.

Implemented methods and attributes#

Attributes#

PropertyContainer.N

[Get] The number of entities \(N\), i.e. the first dimension of all the property arrays.

PropertyContainer.force_2d

[Get/Set] Whether all the properties are stored as 2D arrays of shape \((N, P)\).

PropertyContainer.n_properties

[Get] The number of properties stored in the container.

Dictionary-like interface#

The container can be used as a dictionary: container[key], container[key] = value, del container[key], key in container, len(container) and for key in container. The arrays are copied when they are set and returned as read-only views.

PropertyContainer.get(key)

Get the values of a property.

PropertyContainer.set(key, value, *[, ...])

Set the values of a property (the input is copied).

PropertyContainer.delete(key)

Delete a property.

PropertyContainer.clear()

Remove all the properties.

PropertyContainer.has(key)

Check if a property exists.

PropertyContainer.keys()

Get the names of the properties.

PropertyContainer.items()

Get the properties as (name, values) pairs.

PropertyContainer.values()

Get the values of the properties.

Inspection and validation#

PropertyContainer.get_dtype(key)

Get the data type of a property.

PropertyContainer.get_shape(key)

Get the shape \((N, \dots)\) of a property.

PropertyContainer.get_ndim(key)

Get the number of dimensions of a property.

PropertyContainer.check(key, *[, shape, dtype])

Check if a property exists and has the expected shape and data type.

PropertyContainer.validate()

Validate the integrity of the container.

Reshaping a property#

PropertyContainer.flatten(key, *[, order])

Flatten a property from shape \((N, \dots)\) to shape \((N, P)\), where \(P\) is the product of the attribute dimensions.

PropertyContainer.reshape(key, new_shape)

Reshape the attributes of a property, keeping the first dimension \(N\).

Structure methods#

PropertyContainer.copy()

Create a copy of the container (the arrays are copied).

PropertyContainer.concatenate(other, *[, fill])

Concatenate two containers along the first dimension.

PropertyContainer.update(other, *[, conflict])

Combine the properties of two containers with the same \(N\).

PropertyContainer.filter(mask)

Keep only the entities selected by a boolean mask, in a new container (this container is unchanged).

Serialization methods#

The container is converted to a dictionary of arrays with to_dict() (and created back with from_dict()). It inherits the serialization methods of the mixins pysdic.core.serialization.JsonSerializerMixin, pysdic.core.serialization.Hdf5SerializerMixin and pysdic.core.serialization.NpzSerializerMixin.

PropertyContainer.to_dict()

Convert the container to a dictionary.

PropertyContainer.to_json_file(file_path, *)

Serialize the object to a JSON file.

PropertyContainer.from_json_file(file_path, ...)

Deserialize a JSON file to an object of the class.

PropertyContainer.to_hdf5_file(file_path, *)

Serialize the object to an HDF5 file.

PropertyContainer.from_hdf5_file(file_path, ...)

Deserialize an object from an HDF5 file.

PropertyContainer.to_npz_file(file_path, *)

Serialize the object to an NPZ file.

PropertyContainer.from_npz_file(file_path, ...)

Deserialize an object from an NPZ file.