Skip to content
Snippets Groups Projects
Commit 20673f53 authored by Jean-Baptiste Bayle's avatar Jean-Baptiste Bayle
Browse files

Add `collapsed()` method on containers

parent b13aeb09
No related branches found
No related tags found
1 merge request!12Resolve "Collapse arrays of identical values to scalar"
...@@ -46,6 +46,15 @@ class ForEachObject(abc.ABC): ...@@ -46,6 +46,15 @@ class ForEachObject(abc.ABC):
""" """
return self.__class__(lambda mosa: transformation(mosa, self[mosa])) return self.__class__(lambda mosa: transformation(mosa, self[mosa]))
def collapsed(self):
"""Turn a numpy arrays containing identical elements into a scalar.
This method can be used to optimize computations when constant time series are involved.
"""
return self.transformed(lambda _, x:
x[0] if isinstance(x, numpy.ndarray) and numpy.all(x == x[0]) else x
)
def write(self, hdf5, dataset): def write(self, hdf5, dataset):
"""Write values in dataset on HDF5 file. """Write values in dataset on HDF5 file.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment