Skip to content

Commit

Permalink
sdss/yao#17: buffer size does not match expected size
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Feb 25, 2024
1 parent 90b7cd8 commit e7a907e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Next version

* [yao #17](sdss/yao#17): Deal with case when the returned buffer does not match the expected size.


## 0.14.3 - January 12, 2024

### 🔧 Fixed
Expand Down
29 changes: 23 additions & 6 deletions archon/controller/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,8 +1157,7 @@ async def fetch(
notifier: Optional[Callable[[str], None]] = None,
*,
return_buffer: Literal[False],
) -> numpy.ndarray:
...
) -> numpy.ndarray: ...

@overload
async def fetch(
Expand All @@ -1167,17 +1166,15 @@ async def fetch(
notifier: Optional[Callable[[str], None]] = None,
*,
return_buffer: Literal[True],
) -> tuple[numpy.ndarray, int]:
...
) -> tuple[numpy.ndarray, int]: ...

@overload
async def fetch(
self,
buffer_no: int = -1,
notifier: Optional[Callable[[str], None]] = None,
return_buffer: bool = False,
) -> numpy.ndarray:
...
) -> numpy.ndarray: ...

async def fetch(
self,
Expand Down Expand Up @@ -1267,6 +1264,26 @@ async def fetch(
# Convert to uint16 array and reshape.
dtype = f"<u{bytes_per_pixel}" # Buffer is little-endian
arr = numpy.frombuffer(frame, dtype=dtype)

# See yao issue #17. In some cases the buffer size is 2 pixels short. For
# now if the buffer size does not match what we expect, just pad with zeros.
expected_size = height * width
if expected_size != arr.size:
message = (
"Buffer data size does not match expected size. "
f"Buffer size is {arr.size}; expected size is {expected_size}. "
"Padding with zeros."
)
notifier(message)
warnings.warn(message, ArchonUserWarning)

arr0 = arr.copy()
arr = numpy.zeros(expected_size, dtype=arr.dtype)
arr[: arr0.size] = arr0

else:
notifier(f"Buffer size is {arr.size}; expected size is {expected_size}.")

arr = arr.reshape(height, width)

# Turn off FETCHING bit
Expand Down

0 comments on commit e7a907e

Please sign in to comment.