Skip to content

Commit f22ad8b

Browse files
mikeprosserniMike Prosser
andauthored
Refactor stream_readers and stream_writers into smaller files (#816)
* stream_readers refactor draft * cleanup * cleanup * stream_writers refactor * cleanup * cleanup --------- Co-authored-by: Mike Prosser <[email protected]>
1 parent 859c18d commit f22ad8b

39 files changed

+8601
-8331
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ machine.
8080
To build the documentation install the optional docs packages and run sphinx. For example:
8181

8282
```sh
83-
$ poetry install -E docs
83+
$ poetry install --with docs
8484
$ poetry run sphinx-build -b html docs docs\_build
8585
```
8686

generated/nidaqmx/stream_readers.py

Lines changed: 7 additions & 2766 deletions
Large diffs are not rendered by default.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
NI-DAQmx stream readers.
3+
4+
This package provides classes for reading samples from NI-DAQmx tasks.
5+
"""
6+
7+
from __future__ import annotations
8+
9+
from nidaqmx import DaqError
10+
11+
from .analog_single_channel_reader import AnalogSingleChannelReader
12+
from .analog_multi_channel_reader import AnalogMultiChannelReader
13+
from .analog_unscaled_reader import AnalogUnscaledReader
14+
from .counter_reader import CounterReader
15+
from .digital_single_channel_reader import DigitalSingleChannelReader
16+
from .digital_multi_channel_reader import DigitalMultiChannelReader
17+
from .power_readers import (
18+
PowerSingleChannelReader,
19+
PowerMultiChannelReader,
20+
PowerBinaryReader,
21+
)
22+
23+
__all__ = [
24+
'AnalogSingleChannelReader',
25+
'AnalogMultiChannelReader',
26+
'AnalogUnscaledReader',
27+
'CounterReader',
28+
'DigitalSingleChannelReader',
29+
'DigitalMultiChannelReader',
30+
'PowerSingleChannelReader',
31+
'PowerMultiChannelReader',
32+
'PowerBinaryReader',
33+
]
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
from __future__ import annotations
2+
3+
from nidaqmx import DaqError
4+
5+
from nidaqmx.error_codes import DAQmxErrors
6+
7+
class ChannelReaderBase:
8+
"""
9+
Defines base class for all NI-DAQmx stream readers.
10+
"""
11+
12+
def __init__(self, task_in_stream):
13+
"""
14+
Args:
15+
task_in_stream: Specifies the input stream associated with
16+
an NI-DAQmx task from which to read samples.
17+
"""
18+
self._in_stream = task_in_stream
19+
self._task = task_in_stream._task
20+
self._handle = task_in_stream._task._handle
21+
self._interpreter = task_in_stream._task._interpreter
22+
23+
self._verify_array_shape = True
24+
25+
@property
26+
def verify_array_shape(self):
27+
"""
28+
bool: Indicates whether the size and shape of the user-defined
29+
NumPy arrays passed to read methods are verified. Defaults
30+
to True when this object is instantiated.
31+
32+
Setting this property to True may marginally adversely
33+
impact the performance of read methods.
34+
"""
35+
return self._verify_array_shape
36+
37+
@verify_array_shape.setter
38+
def verify_array_shape(self, val):
39+
self._verify_array_shape = val
40+
41+
def _verify_array(self, data, number_of_samples_per_channel,
42+
is_many_chan, is_many_samp):
43+
"""
44+
Verifies that the shape of the specified NumPy array can be used
45+
to read multiple samples from the current task which contains
46+
one or more channels, if the "verify_array_shape" property is
47+
set to True.
48+
49+
Args:
50+
data (numpy.ndarray): Specifies the NumPy array to verify.
51+
number_of_samples_per_channel (int): Specifies the number of
52+
samples per channel requested.
53+
is_many_chan (bool): Specifies if the read method is a many
54+
channel version.
55+
is_many_samp (bool): Specifies if the read method is a many
56+
samples version.
57+
"""
58+
if not self._verify_array_shape:
59+
return
60+
61+
channels_to_read = self._in_stream.channels_to_read
62+
number_of_channels = len(channels_to_read.channel_names)
63+
64+
array_shape: tuple[int, ...] | None = None
65+
if is_many_chan:
66+
if is_many_samp:
67+
array_shape = (number_of_channels,
68+
number_of_samples_per_channel)
69+
else:
70+
array_shape = (number_of_channels,)
71+
else:
72+
if is_many_samp:
73+
array_shape = (number_of_samples_per_channel,)
74+
75+
if array_shape is not None and data.shape != array_shape:
76+
raise DaqError(
77+
'Read cannot be performed because the NumPy array passed into '
78+
'this function is not shaped correctly. You must pass in a '
79+
'NumPy array of the correct shape based on the number of '
80+
'channels in task and the number of samples per channel '
81+
'requested.\n\n'
82+
'Shape of NumPy Array provided: {}\n'
83+
'Shape of NumPy Array required: {}'
84+
.format(data.shape, array_shape),
85+
DAQmxErrors.UNKNOWN, task_name=self._task.name)
86+
87+
def _verify_array_digital_lines(
88+
self, data, is_many_chan, is_many_line):
89+
"""
90+
Verifies that the shape of the specified NumPy array can be used
91+
to read samples from the current task which contains one or more
92+
channels that have one or more digital lines per channel, if the
93+
"verify_array_shape" property is set to True.
94+
95+
Args:
96+
data (numpy.ndarray): Specifies the NumPy array to verify.
97+
is_many_chan (bool): Specifies if the read method is a
98+
many channel version.
99+
is_many_line (bool): Specifies if the read method is a
100+
many line version.
101+
"""
102+
if not self._verify_array_shape:
103+
return
104+
105+
channels_to_read = self._in_stream.channels_to_read
106+
number_of_channels = len(channels_to_read.channel_names)
107+
number_of_lines = self._in_stream.di_num_booleans_per_chan
108+
109+
array_shape: tuple[int, ...] | None = None
110+
if is_many_chan:
111+
if is_many_line:
112+
array_shape = (number_of_channels, number_of_lines)
113+
else:
114+
array_shape = (number_of_channels,)
115+
else:
116+
if is_many_line:
117+
array_shape = (number_of_lines,)
118+
119+
if array_shape is not None and data.shape != array_shape:
120+
raise DaqError(
121+
'Read cannot be performed because the NumPy array passed into '
122+
'this function is not shaped correctly. You must pass in a '
123+
'NumPy array of the correct shape based on the number of '
124+
'channels in task and the number of digital lines per '
125+
'channel.\n\n'
126+
'Shape of NumPy Array provided: {}\n'
127+
'Shape of NumPy Array required: {}'
128+
.format(data.shape, array_shape),
129+
DAQmxErrors.UNKNOWN, task_name=self._task.name)

0 commit comments

Comments
 (0)