Skip to content

Commit

Permalink
Implement add_array_property
Browse files Browse the repository at this point in the history
  • Loading branch information
clorton committed Dec 17, 2024
1 parent 065a60d commit c30ae23
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/laser_core/laserframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def add_scalar_property(self, name: str, dtype=np.uint32, default=0) -> None:
Add a scalar property to the class.
This method initializes a new scalar property for the class instance. The property is
stored as a NumPy array with a specified data type and default value.
stored as a 1-D NumPy array (scalar / entry) with a specified data type and default value.
Parameters:
Expand All @@ -81,7 +81,7 @@ def add_vector_property(self, name: str, length: int, dtype=np.uint32, default=0
"""
Adds a vector property to the object.
This method initializes a new property with the given name as a NumPy array.
This method initializes a new property with the given name as a 2-D NumPy array (vector per entry).
The array will have a shape of (length, self._capacity) and will be filled
with the specified default value. The data type of the array elements is
Expand All @@ -103,6 +103,32 @@ def add_vector_property(self, name: str, length: int, dtype=np.uint32, default=0
setattr(self, name, np.full((length, self._capacity), default, dtype=dtype))
return

def add_array_property(self, name: str, shape: tuple, dtype=np.uint32, default=0) -> None:
"""
Adds an array property to the object.
This method initializes a new property with the given name as a multi-dimensional NumPy array.
The array will have the given shape (note that there is no implied dimension of size self._capacity),
datatype (default is np.uint32), and default value (default is 0).
Parameters:
name (str): The name of the property to be added.
shape (tuple): The shape of the array.
dtype (data-type, optional): The desired data-type for the array, default is np.uint32.
default (scalar, optional): The default value to fill the array with, default is 0.
Returns:
None
"""

# initialize the property to a NumPy array with given shape, dtype, and default value
setattr(self, name, np.full(shape, default, dtype=dtype))
return

@property
def count(self) -> int:
"""
Expand Down
20 changes: 20 additions & 0 deletions tests/test_laserframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ def test_add_vectory_property_with_value(self):
assert np.all(pop.events == 1)
assert pop.events.shape == (365, 1024)

def test_add_array_property(self):
pop = LaserFrame(1024)
pop.add_array_property("events", (365, 1024))
assert np.all(pop.events == 0)
assert pop.events.shape == (365, 1024)

def test_add_array_property_with_value(self):
pop = LaserFrame(1024)
pop.add_array_property("events", (365, 1024), default=42)
assert np.all(pop.events == 42)
assert pop.events.shape == (365, 1024)

def test_add_array_property_with_dtype(self):
pop = LaserFrame(1024)
default = np.float32(-3.14159265)
pop.add_array_property("events", (365, 1024), dtype=np.float32, default=default)
assert np.all(pop.events == default)
assert pop.events.shape == (365, 1024)
assert pop.events.dtype == np.float32

def test_add_agents(self):
pop = LaserFrame(1024)
istart, iend = pop.add(100)
Expand Down

0 comments on commit c30ae23

Please sign in to comment.