From 5daf0f3b483a2f7aa2ab573183ffd8263099ded3 Mon Sep 17 00:00:00 2001 From: Christopher Lorton Date: Tue, 17 Dec 2024 21:17:05 +0000 Subject: [PATCH 1/2] Promote methods to laser_core --- src/laser_core/__init__.py | 11 +++++++++++ src/laser_core/random.py | 2 ++ tests/test_laserframe.py | 2 +- tests/test_propertyset.py | 2 +- tests/test_sortedqueue.py | 2 +- 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/laser_core/__init__.py b/src/laser_core/__init__.py index d3ec452..3140ac5 100644 --- a/src/laser_core/__init__.py +++ b/src/laser_core/__init__.py @@ -1 +1,12 @@ __version__ = "0.2.0" + +from .laserframe import LaserFrame +from .propertyset import PropertySet +from .sortedqueue import SortedQueue + +__all__ = [ + "LaserFrame", + "PropertySet", + "SortedQueue", + "__version__", +] diff --git a/src/laser_core/random.py b/src/laser_core/random.py index 2c49e45..83dcce5 100644 --- a/src/laser_core/random.py +++ b/src/laser_core/random.py @@ -13,6 +13,8 @@ import numba as nb import numpy as np +__all__ = ["get_seed", "prng", "seed"] + _seed: np.uint32 = None _prng: np.random.Generator = None diff --git a/tests/test_laserframe.py b/tests/test_laserframe.py index 7821134..4160369 100644 --- a/tests/test_laserframe.py +++ b/tests/test_laserframe.py @@ -42,7 +42,7 @@ import numpy as np import pytest -from laser_core.laserframe import LaserFrame +from laser_core import LaserFrame class TestLaserFrame(unittest.TestCase): diff --git a/tests/test_propertyset.py b/tests/test_propertyset.py index 895a4a3..1275dac 100644 --- a/tests/test_propertyset.py +++ b/tests/test_propertyset.py @@ -7,7 +7,7 @@ import numpy as np -from laser_core.propertyset import PropertySet +from laser_core import PropertySet class TestPropertySet(unittest.TestCase): diff --git a/tests/test_sortedqueue.py b/tests/test_sortedqueue.py index 50b1789..4c19c90 100644 --- a/tests/test_sortedqueue.py +++ b/tests/test_sortedqueue.py @@ -6,7 +6,7 @@ import numpy as np import pytest -from laser_core.sortedqueue import SortedQueue +from laser_core import SortedQueue class TestSortedQueue(unittest.TestCase): From 0eb5e2e44a57dc7034ac97d268960c2a04aedeee Mon Sep 17 00:00:00 2001 From: Christopher Lorton Date: Tue, 17 Dec 2024 21:17:16 +0000 Subject: [PATCH 2/2] Implement add_array_property --- src/laser_core/laserframe.py | 30 ++++++++++++++++++++++++++++-- tests/test_laserframe.py | 20 ++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/laser_core/laserframe.py b/src/laser_core/laserframe.py index dc16cc3..1b059dc 100644 --- a/src/laser_core/laserframe.py +++ b/src/laser_core/laserframe.py @@ -77,7 +77,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: @@ -98,7 +98,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 @@ -120,6 +120,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: """ diff --git a/tests/test_laserframe.py b/tests/test_laserframe.py index 4160369..cc9031a 100644 --- a/tests/test_laserframe.py +++ b/tests/test_laserframe.py @@ -96,6 +96,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, 100) assert pop.count == 100