|
| 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