Skip to content

Commit

Permalink
made suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
evalott100 committed Sep 17, 2024
1 parent a7b1e4d commit 806cc49
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
7 changes: 2 additions & 5 deletions src/ophyd_async/core/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def __add__(self, right: "Table") -> "Table":
}
)

@property
def numpy_dtype(self) -> np.dtype:
dtype = []
for field_name, field_value in self.model_fields.items():
Expand All @@ -81,18 +80,16 @@ def numpy_dtype(self) -> np.dtype:

return np.dtype(dtype)

@property
def numpy_table(self):
# It would be nice to be able to use np.transpose for this,
# but it defaults to the largest dtype for everything.
dtype = self.numpy_dtype
dtype = self.numpy_dtype()
transposed_list = [
np.array(tuple(row), dtype=dtype) for row in zip(*self.numpy_columns)
np.array(tuple(row), dtype=dtype) for row in zip(*self.numpy_columns())
]
transposed = np.array(transposed_list, dtype=dtype)
return transposed

@property
def numpy_columns(self) -> list[np.ndarray]:
"""Columns in the table can be lists of string enums or numpy arrays.
Expand Down
2 changes: 1 addition & 1 deletion src/ophyd_async/fastcs/panda/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class SeqTrigger(str, Enum):
),
Field(default_factory=lambda: np.array([], dtype=np.bool_)),
]
TriggerStr = Annotated[list[SeqTrigger], Field(default_factory=list)]
TriggerStr = Annotated[Sequence[SeqTrigger], Field(default_factory=list)]


class SeqTable(Table):
Expand Down
6 changes: 3 additions & 3 deletions tests/fastcs/panda/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def _assert_col_equal(column1, column2):
_assert_col_equal(column1, column2)

assert np.array_equal(
seq_table_from_pva_dict.numpy_columns,
seq_table_from_pva_dict.numpy_columns(),
[
np.array([1, 2, 3, 4], dtype=np.int32),
np.array(
Expand Down Expand Up @@ -245,7 +245,7 @@ def _assert_col_equal(column1, column2):
np.array([True, False, True, False], dtype=np.bool_),
],
)
dtype = seq_table_from_pva_dict.numpy_dtype
dtype = seq_table_from_pva_dict.numpy_dtype()
assert dtype == np.dtype(
[
("repeats", np.int32),
Expand All @@ -269,7 +269,7 @@ def _assert_col_equal(column1, column2):
)

assert np.array_equal(
seq_table_from_pva_dict.numpy_table,
seq_table_from_pva_dict.numpy_table(),
np.array(
[
(
Expand Down
21 changes: 11 additions & 10 deletions tests/fastcs/panda/test_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,15 @@ def full_seq_table(trigger):
outf2=np.array([1], dtype=np.bool_),
)

with pytest.raises(ValidationError) as exc:
full_seq_table(np.array(["A"], dtype="U32"))
assert (
"Input should be 'Immediate', 'BITA=0', 'BITA=1', 'BITB=0', 'BITB=1', "
"'BITC... [type=enum, input_value='A', input_type=str_]"
) in str(exc)
for attempted_table in [
np.array(["A"], dtype="U32"),
np.array(["Immediate"], dtype="U32"),
{"Immediate"},
]:
with pytest.raises(ValidationError) as exc:
full_seq_table(attempted_table)
assert "Input should be an instance of Sequence" in str(exc)

with pytest.raises(ValidationError) as exc:
full_seq_table(["A"])
assert (
Expand All @@ -152,9 +155,7 @@ def full_seq_table(trigger):
) in str(exc)

# Pydantic is able to infer type from these
table = full_seq_table({"Immediate"})
assert table.trigger == ["Immediate"] == [SeqTrigger.IMMEDIATE]
table = full_seq_table({SeqTrigger.IMMEDIATE})
table = full_seq_table([SeqTrigger.IMMEDIATE])
assert table.trigger == ["Immediate"] == [SeqTrigger.IMMEDIATE]
full_seq_table(np.array(["Immediate"], dtype="U32"))
table = full_seq_table(["Immediate"])
assert table.trigger == ["Immediate"] == [SeqTrigger.IMMEDIATE]

0 comments on commit 806cc49

Please sign in to comment.