read_many_sample cannot use views
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 30/100
Hướng nghiên cứu
Không có tệp hoặc bài kiểm thử nào được nêu tên. Hãy bắt đầu với các phương thức stream_reader read_many_sample và hành vi fill_mode của DAQmxReadAnalogF64 được mô tả ở đây; tái hiện các ví dụ về NumPy slice và continuous-acquisition, sau đó định nghĩa done là một cách tiếp cận được chọn tương thích với view, kèm các ví dụ và trade-off được ghi lại.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
This is an improvement suggestion.
The read_many_sample methods of stream_reader classes have odd requirements for the data array. The array shape must be (n_channels, n_samples) and must be C_CONTIGUOUS. In the language of nidaq-mx, the data array must be 'non-interleaved'. This causes extra difficulties when using continuous acquisition mode. In this case, the exact number of samples may not be always known. As an example, this prevents the use of array views as an argument to read_many_sample. Let's review the root of this problem, show some example code, and introduce some possible solutions.
Numpy arrays can be either C_CONTIGUOUS (or row-major, C-order) or F_CONTIGUOUS (or column-major). This controls the layout in memory for multidimensional arrays. For 2D arrays, C_CONTIGUOUS arrays store the elements one row after the other. Ie: arr[i,j] is next to arr[i, j+1] in memory. See: https://docs.scipy.org/doc/numpy/glossary.html#term-row-major. This means that changing the number of rows can be done without reorganizing the entire array. Furthermore, a slice containing all rows but not all columns is not C_CONTIGUOUS, whereas a slice containing all columns but some rows is not. Since nidaqmx maps rows onto channels and samples onto columns, a slice onto some samples is not C_CONTIGUOUS, and cannot be used as an argument for stream_reader.AnalogMultiChannelReader.read_many_samples. This also means the transfer from the buffer to the numpy array is not in native order, thus not so efficient.
import numpy as np
n_chan, n_samp = 4, 100
c = np.zeros((n_chan, n_samp), order="C") # default order
assert c.flags.c_contiguous
c_cols = c[:,10:15]
assert not c_cols.flags.c_contiguous
As an example demonstration, let's perform continuous acquisition. This example requires a temporary buffer for each call. This exact example can be achieved using finite acquisition, but the pattern can be useful for other applications:
import numpy as np
import nidaqmx as ni
from nidaqmx.constants import VoltageUnits, AcquisitionType, READ_ALL_AVAILABLE
from nidaqmx.stream_readers import AnalogMultiChannelReader
from time import sleep
##### SETUP
n_tot = 100000
sample_rate = 200000
with ni.Task("signals") as task:
task.ai_channels.add_ai_voltage_chan(
"DevT/ai0:1",
min_val=-10, max_val=10,
)
n_channels = task.number_of_channels
task.timing.cfg_samp_clk_timing(
rate=sample_rate,
sample_mode=AcquisitionType.CONTINUOUS,
samps_per_chan=n_tot,
)
reader = AnalogMultiChannelReader(task.in_stream)
read_buffer = np.ones((n_channels, n_tot))*-1000 # impossible output
i = 0
##### START
task.start()
while not task.is_task_done() and i < n_tot:
sleep(0.01) # pretend to be busy with other tasks
n = reader._in_stream.avail_samp_per_chan
if n == 0: continue
n = min(n, n_tot-i) # prevent reading too many samples
##### READ
tmp = np.ones((n_channels, n)) * -1001
r = reader.read_many_sample(
tmp,
number_of_samples_per_channel=n
)
read_buffer[:,i:i+n] = tmp
i += r
##### STOP AND CHECK RESULTS
task.stop()
assert np.all(read_buffer > -1000)
If views could be used the intervening tmp array can be dropped. The code could then be:
# [setup as previously]
##### READ
i += reader.read_many_sample(
read_buffer[:, i:i+n], # read directly into array using a view
number_of_samples_per_channel=n
)
# [stop and check results as previously]
Enabling this compatibility would simplify client code (it would "just work") and should improve interoperability with numpy-based code, such as memory-mapped arrays, hdf5py, etc.
Here are a few possible paths for a solution, all of which have potential drawbacks:
- Change array shape to
(n_samples, n_channels); - Change requirement for numpy arrays to F-order`;
- Enable choosing between F-order and C-order. This can (maybe?) be delegated to the reader object. The underlying
DAQmxReadAnalogF64seem to support both cases through thefill_modeargument.
The tests were performed using the following configuration:
python=3.8.2
numpy=1.18.1
nidaqmx-python=0.5.7
NI-DAQmx=19.6
on windows 10. A simulated device (USB-6356) was created in NI-MAX, with name DevT.
- Ngôn ngữ chính
- Python
- Star
- 590
- Fork
- 200
- Merge trung bình
- 1 ngày 8 giờ
- Pull request đã merge (30 ngày)
- 7
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của ni/nidaqmx-python
-
Add PEP 723 dependency metadata to example programsCó thể đã có người làm @shrishsk đã nhận 3 ngày trước. Đang mởenhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
ni/nidaqmx-python#1030 ·
-
tech debt
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 38/100
ni/nidaqmx-python#945 ·
-
bug
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
ni/nidaqmx-python#941 ·
-
Potential typos in the APICó thể đã có người làm @KostaIlic2 đã nhận 215 ngày trước. Đang mởbug
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 42/100
ni/nidaqmx-python#939 ·
-
Add gRPC examplesĐang mởenhancement
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
ni/nidaqmx-python#917 · 1 bình luận · 1 reaction ·
Tất cả issue của ni/nidaqmx-python
Issue tương tự
-
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 66/100
AlexsLemonade/scpca-docs#518 ·
-
bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 95/100
rocketride-org/rocketride-server#2705 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Metabuilder-Labs/tokenjam#786 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
prototype(verifier_token): map disallowed warning to contraindicated before composite precedence checkCó thể đã có người làm @drk-ram đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
agentrust-io/trace-spec#582 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area:cli priority:low refactor size:small
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
RonaldHensbergen/composable-data-stack#906 ·
Maintainer thường phản hồi trong vòng 1 ngày