Skip to content

Commit

Permalink
Fix _send_temperatures() for Seneca device
Browse files Browse the repository at this point in the history
As the Seneca device now returns its temperatures as a numpy array,
unfortunately the _send_temperatures() function no longer works with it:

```
Traceback (most recent call last):
  File "/home/alex/code/FINESSE/finesse/gui/temp_control.py", line 265, in _poll_dp9800
    pub.sendMessage(f"device.{TEMPERATURE_MONITOR_TOPIC}.data.request")
  File "/home/alex/code/FINESSE/.venv/lib/python3.11/site-packages/pubsub/core/publisher.py", line 216, in sendMessage
    topicObj.publish(**msgData)
  File "/home/alex/code/FINESSE/.venv/lib/python3.11/site-packages/pubsub/core/topicobj.py", line 452, in publish
    self.__sendMessage(msgData, topicObj, msgDataSubset)
  File "/home/alex/code/FINESSE/.venv/lib/python3.11/site-packages/pubsub/core/topicobj.py", line 482, in __sendMessage
    listener(data, self, allData)
  File "/home/alex/code/FINESSE/.venv/lib/python3.11/site-packages/pubsub/core/listener.py", line 237, in __call__
    cb(**kwargs)
  File "/home/alex/code/FINESSE/finesse/hardware/__init__.py", line 47, in _send_temperatures
    temperatures = _try_get_temperatures() or _DEFAULT_TEMPS
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
```

Seemingly unlike Python's built-in collections, you can't check whether
a numpy array is empty by looking at its "truthiness".

Fix by using an explicit if-statement.
  • Loading branch information
alexdewar committed Dec 4, 2023
1 parent ae0a41f commit 49dedb2
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion finesse/hardware/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def _try_get_temperatures() -> Sequence | None:

def _send_temperatures() -> None:
"""Send the current temperatures (or NaNs) via pubsub."""
temperatures = _try_get_temperatures() or _DEFAULT_TEMPS
temperatures = _try_get_temperatures()
if temperatures is None:
temperatures = _DEFAULT_TEMPS

Check warning on line 49 in finesse/hardware/__init__.py

View check run for this annotation

Codecov / codecov/patch

finesse/hardware/__init__.py#L47-L49

Added lines #L47 - L49 were not covered by tests

time = datetime.utcnow()
pub.sendMessage(
f"device.{TEMPERATURE_MONITOR_TOPIC}.data.response",
Expand Down

0 comments on commit 49dedb2

Please sign in to comment.