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

ESROGUE-684 - Frame.getNumpy now allows user defined data type #1027

Open
wants to merge 18 commits into
base: pre-release
Choose a base branch
from

Conversation

bengineerd
Copy link
Contributor

@bengineerd bengineerd commented Oct 15, 2024

Description

Changes to Frame.getNumpy()

The Frame.getNumpy() method now takes an additional dtype argument, which allows a numpy type to be specified for the returned array.

array = frame.getNumpy(0, frame.getPayload(), np.uint32)

Additionally, the getNumpy() method now has default arguments for all parameters

offset = 0
count = 0, 
dtype = np.uint8

Allowing for calls such as

# Read the entire frame into a np.uint8 array
array = frame.getNumpy()

# Read the entire frame into a np.uint32 array
array = frame.getNumpy(dtype=np.uint32)

The count argument defaults to 0, which functionally will return the entire array starting at offset.

The offset and count parameters are still specified in bytes, not in the dtype size.

Other changes

The offset argument of Frame.readPy() and Frame.writePy() has also been given a default of 0 when called from python.

A new Frame.getBa() method has been added that will allocate the bytearray internally and return it. This makes things a bit more concise in python when reading from a Frame.

# New
ba = frame.getBa() # Create a bytearray, fill it, and return it

#Old
ba = bytearray(frame.getPayload())
frame.read(ba)

The getBa() method takes an offset and count which function the same as in getNumpy() with the same defaults.

Also, a new Frame.getMemoryview() method has been added that allocates a bytearray but returns it as a memoryview. This allows for efficient slicing of the frame data without any copying. In most cases where copyless slicing is needed, a getNumpy() would be preferred, but getMemoryview() is useful for efficiently parsing complex header structures.

memory_view = frame.getMemoryview()

# Parse the header (4 bytes), length (2 bytes), and checksum (2 bytes)
header, length, checksum = struct.unpack_from('IHH', memory_view, 0)

@bengineerd bengineerd marked this pull request as ready for review October 16, 2024 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant