Skip to content
Open
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
9 changes: 9 additions & 0 deletions src/pyrecest/utils/history_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from math import nan
from typing import Any

import numpy as np

from pyrecest import backend

# pylint: disable=no-name-in-module,no-member
Expand Down Expand Up @@ -46,6 +48,13 @@ def _validate_bool_flag(value: Any, name: str) -> bool:


def _is_invalid_padded_history_dtype(dtype: Any) -> bool:
try:
dtype_kind = np.dtype(dtype).kind
except (TypeError, ValueError):
dtype_kind = None
if dtype_kind is not None:
return dtype_kind in {"b", "c", "O", "U", "S", "M", "m"}

dtype_name = str(dtype).lower()
return (
"bool" in dtype_name
Expand Down
34 changes: 34 additions & 0 deletions tests/test_history_recorder_unsigned_dtype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import numpy as np
import pytest

from pyrecest import backend
from pyrecest.utils.history_recorder import HistoryRecorder


def _as_numpy(value):
return backend.to_numpy(value)


def _swapped_uint16_dtype():
return np.dtype("uint16").newbyteorder("S")


def test_padded_history_accepts_non_native_unsigned_integer_arrays():
recorder = HistoryRecorder()
initial = np.array([1, 2], dtype=_swapped_uint16_dtype())

history = recorder.register("counts", initial, pad_with_nan=True)

assert _as_numpy(history).tolist() == [[1.0], [2.0]]

updated = recorder.record("counts", np.array([3], dtype=_swapped_uint16_dtype()))

expected = np.array([[1.0, 3.0], [2.0, np.nan]])
assert np.allclose(_as_numpy(updated), expected, equal_nan=True)


def test_padded_history_still_rejects_unicode_arrays():
recorder = HistoryRecorder()

with pytest.raises(TypeError, match="padded history values must be real numeric"):
recorder.register("bad", np.array(["1"], dtype=np.dtype("U1")), pad_with_nan=True)
Loading