Skip to content

Commit

Permalink
test: add tests for different dtype, shape and Signals
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Guijarro committed Oct 31, 2024
1 parent ed7d823 commit 1cc52ef
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions ophyd/tests/test_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import threading
import time

import numpy
import pytest

from ophyd import get_cl
Expand Down Expand Up @@ -693,3 +694,75 @@ def test_import_ro_signal_class():
from ophyd.signal import SignalRO as SignalRoFromModule

assert SignalRoFromPkg is SignalRoFromModule


def test_signal_dtype_shape_info(fake_motor_ioc, cleanup):
pvs = fake_motor_ioc.pvs
sig = EpicsSignal(write_pv=pvs["setpoint"], read_pv=pvs["readback"], name="motor")
sig_desc = sig.describe()["motor"]
assert sig_desc["dtype"] == "number"
assert sig_desc["shape"] == []

sig = EpicsSignal(
write_pv=pvs["setpoint"], read_pv=pvs["readback"], name="motor", dtype=float
)
sig_desc = sig.describe()["motor"]
assert sig_desc["dtype"] == "number"
assert sig_desc["dtype_numpy"] == "float64"
assert sig_desc["shape"] == []

original = Signal(name="original")
original_desc = original.describe()["original"]
assert original_desc["dtype"] == "number"
assert original_desc["dtype_numpy"] == "float64"
assert original_desc["shape"] == []
assert pytest.approx(original.get()) == 0.0
original = Signal(name="original", value=1)
original_desc = original.describe()["original"]
assert original_desc["dtype"] == "integer"
assert original_desc["dtype_numpy"] == "int64"
assert original_desc["shape"] == []
assert original.get() == 1
original = Signal(name="original", value="On")
original_desc = original.describe()["original"]
assert original_desc["dtype"] == "string"
assert original_desc["dtype_numpy"] == ""
assert original_desc["shape"] == []
assert original.get() == "On"
original = Signal(name="original", dtype=numpy.uint16, shape=(2, 2))
original_desc = original.describe()["original"]
assert original_desc["dtype"] == "array"
assert original_desc["dtype_numpy"] == "uint16"
assert original_desc["shape"] == [2, 2]
with pytest.raises(RuntimeError):
original.get()
original = Signal(
name="original",
value=numpy.array([[1, 2], [3, 4]]),
dtype=numpy.uint16,
shape=(2, 2),
)
original_desc = original.describe()["original"]
assert original_desc["dtype"] == "array"
assert original_desc["dtype_numpy"] == "uint16"
assert original_desc["shape"] == [2, 2]
test_arr = numpy.array([1, 2, 3, 4], dtype=numpy.uint16)
test_arr.shape = (2, 2)
numpy.testing.assert_equal(original.get(), test_arr)

class TestDerivedSignal(DerivedSignal):
def forward(self, value):
return value / 2

def inverse(self, value):
return 2 * value

derived = TestDerivedSignal(
derived_from=original, name="derived", dtype=numpy.uint32, shape=(2, 2)
)

derived_desc = derived.describe()["derived"]
assert derived_desc["dtype"] == "array"
assert derived_desc["dtype_numpy"] == "uint32"
assert derived_desc["shape"] == [2, 2]
assert derived_desc["derived_from"] == original.name

0 comments on commit 1cc52ef

Please sign in to comment.