Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array property #98

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/laser_core/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
__version__ = "0.2.0"

from .laserframe import LaserFrame
from .propertyset import PropertySet
from .sortedqueue import SortedQueue

__all__ = [
"LaserFrame",
"PropertySet",
"SortedQueue",
"__version__",
]
30 changes: 28 additions & 2 deletions src/laser_core/laserframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand All @@ -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:
"""
Expand Down
2 changes: 2 additions & 0 deletions src/laser_core/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 21 additions & 1 deletion tests/test_laserframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_propertyset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import numpy as np

from laser_core.propertyset import PropertySet
from laser_core import PropertySet


class TestPropertySet(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_sortedqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading