ni / nidaqmx-python

A Python API for interacting with NI-DAQmx
Other
428 stars 154 forks source link

Task.read does not return short reads for multi-channel power #529

Open bkeryan opened 6 months ago

bkeryan commented 6 months ago

Task.read doesn't slice the array for multi-channel power reads.

Test case (from tests/component/test_task_read_ai.py, under development):

def test___power_multi_channel_finite___read_too_many_sample___returns_valid_2d_channels_samples_truncated(
    pwr_multi_channel_task: nidaqmx.Task,
) -> None:
    samples_to_acquire = 5
    pwr_multi_channel_task.timing.cfg_samp_clk_timing(rate=1000.0, sample_mode=AcquisitionType.FINITE, samps_per_chan=samples_to_acquire)
    num_channels = pwr_multi_channel_task.number_of_channels
    samples_to_read = 10

    data = pwr_multi_channel_task.read(samples_to_read)

    _assert_equal_2d(
        [[e.voltage for e in d] for d in data],
        [
            [_get_voltage_setpoint_for_chan(chan_index) for _ in range(samples_to_acquire)]
            for chan_index in range(num_channels)
        ],
        abs=POWER_EPSILON,
    )
    _assert_equal_2d(
        [[e.current for e in d] for d in data],
        [
            [_get_current_setpoint_for_chan(chan_index) for _ in range(samples_to_acquire)]
            for chan_index in range(num_channels)
        ],
        abs=POWER_EPSILON,
    )

Result:

_ test___power_multi_channel_finite___read_too_many_sample___returns_valid_2d_channels_samples_truncated[library_init_kwargs] _

pwr_multi_channel_task = Task(name=_unnamedTask<F>)

    def test___power_multi_channel_finite___read_too_many_sample___returns_valid_2d_channels_samples_truncated(
        pwr_multi_channel_task: nidaqmx.Task,
    ) -> None:
        samples_to_acquire = 5
        pwr_multi_channel_task.timing.cfg_samp_clk_timing(rate=1000.0, sample_mode=AcquisitionType.FINITE, samps_per_chan=samples_to_acquire)
        num_channels = pwr_multi_channel_task.number_of_channels
        samples_to_read = 10

        data = pwr_multi_channel_task.read(samples_to_read)

>       _assert_equal_2d(
            [[e.voltage for e in d] for d in data],
            [
                [_get_voltage_setpoint_for_chan(chan_index) for _ in range(samples_to_acquire)]
                for chan_index in range(num_channels)
            ],
            abs=POWER_EPSILON,
        )

tests\component\test_task_read_ai.py:317:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

data = [[1.0, 1.0, 1.0, 1.0, 1.0, 2.0, ...], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ...]]
expected = [[1.0, 1.0, 1.0, 1.0, 1.0], [2.0, 2.0, 2.0, 2.0, 2.0]], abs = 0.001

    def _assert_equal_2d(data: List[List[float]], expected: List[List[float]], abs: float) -> None:
        # pytest.approx() does not support nested data structures.
        assert len(data) == len(expected)
        for i in range(len(data)):
>           assert data[i] == pytest.approx(expected[i], abs=abs)
E           assert [1.0, 1.0, 1....1.0, 2.0, ...] == approx([1.0 ±....0 ± 1.0e-03])
E
E             Impossible to compare lists with different sizes.
E             Lengths: 5 and 10

tests\component\test_task_read_ai.py:152: AssertionError