Skip to content

Commit

Permalink
Use numpy to check for interger type in NodeList (#252)
Browse files Browse the repository at this point in the history
The getitem function of NodeList differentiates between integers and nodes as
argument. This guarantees a concistent behaviour. Wether the agrument is an
integer or not needs to be checked. This check now also supports all numpy
types and therefore is more versatile.
  • Loading branch information
tobiasah committed Aug 10, 2023
1 parent 10906cb commit d5fbcbb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# zhinst-toolkit Changelog

## Version 0.6.1
* Fix #252. ListNodes now also supports numpy interger types as index argument.

## Version 0.6.0
* Revert full support of `fnmatch` wildcards and instead use the LabOne wildcard support.
This means only `*` symbols are supported. A `*` in the middle of the path matches
Expand Down
4 changes: 3 additions & 1 deletion src/zhinst/toolkit/nodetree/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from collections import namedtuple
from collections.abc import Sequence
from enum import IntEnum
import numpy as np

from zhinst.toolkit.nodetree.helper import (
NodeDict,
Expand Down Expand Up @@ -1127,7 +1128,8 @@ def __getitem__(
...

def __getitem__(self, item):
if isinstance(item, int):
# User numpy check here to ensure numpy types are handled correctly (#252)
if np.issubdtype(type(item), np.integer):
return self._elements[item]
return Node(self._root, self._tree + (str(item),))

Expand Down
11 changes: 8 additions & 3 deletions tests/test_nodetree.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import gc

import pytest
from numpy import array as nparray
import numpy as np

from zhinst.toolkit.driver.devices import HDAWG
from zhinst.toolkit.nodetree import Node, NodeTree
Expand Down Expand Up @@ -381,7 +381,7 @@ def test_get_deep(connection):
assert data == array("l", [123, 10, 32, 10, 125, 10])
# HF2 node
connection.get.return_value = OrderedDict(
[("/dev1234/demods/0/rate", nparray([1674.10717773]))]
[("/dev1234/demods/0/rate", np.array([1674.10717773]))]
)
timestamp, data = tree.demods[0].sample(deep=True)
assert data == 1674.10717773
Expand Down Expand Up @@ -424,7 +424,7 @@ def test_get_wildcard(connection):

# HF2 support
connection.get.return_value = OrderedDict(
[("/dev1234/demods/0/impedance", nparray([125]))]
[("/dev1234/demods/0/impedance", np.array([125]))]
)
result = tree.demods()
assert result[tree.demods[0].impedance] == 125
Expand Down Expand Up @@ -1026,6 +1026,11 @@ def test_nodelist_is_node(connection, hdawg):
assert bar[0] == hdawg
assert bar == Node(nt, ("foobar",))

def test_nodelist_numpy(connection, hdawg):
nt = NodeTree(connection, "DEV1234")
bar = NodeList([hdawg, hdawg], nt, ("foobar",))
for test_index in np.arange(2):
assert bar[test_index] == hdawg

def test_nodelist_hash(connection, hdawg):
nt = NodeTree(connection, "DEV1234")
Expand Down

0 comments on commit d5fbcbb

Please sign in to comment.