Skip to content

Commit 4f23cd7

Browse files
authored
Clean up niscope usage documentation (#1981)
* Fix links in docs * Fix links for README * Back out usage links change * Remove duplicated documentation * Regenerate niscope README * correct argument name mistake * Clarify more performance as "faster fetching". May help users searching how to fetch fast, as well. * Fix use of bolding of methods in usage doc * Correct mistake in numpy usage codeblock. Each returned wfm was only 400 samples * Add some more comments to the new example * Remove fetch_into codeblock and reference example * Regenerate * Reword sentence about fetch performance * Reword new example description
1 parent 9b5cc3a commit 4f23cd7

File tree

4 files changed

+72
-150
lines changed

4 files changed

+72
-150
lines changed

docs/_static/niscope_usage.inc

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -21,81 +21,7 @@ The following is a basic example of using the **niscope** module to open a sessi
2121
# Find all record number 3
2222
rec3 = [wfm for wfm in waveforms if wfm.record == 3]
2323

24-
The waveform returned from `fetch <niscope/class.html#fetch>`_ is a flat list of Python objects
25-
26-
- Attributes:
27-
28-
- **relative_initial_x** (float) the time (in seconds) from the trigger to the first sample in the fetched waveform
29-
- **absolute_initial_x** (float) timestamp (in seconds) of the first fetched sample. This timestamp is comparable between records and acquisitions; devices that do not support this parameter use 0 for this output.
30-
- **x_increment** (float) the time between points in the acquired waveform in seconds
31-
- **channel** (str) channel name this waveform was acquired from
32-
- **record** (int) record number of this waveform
33-
- **gain** (float) the gain factor of the given channel; useful for scaling binary data with the following formula:
34-
35-
voltage = binary data * gain factor + offset
36-
37-
- **offset** (float) the offset factor of the given channel; useful for scaling binary data with the following formula:
38-
39-
voltage = binary data * gain factor + offset
40-
41-
- **samples** (array of float) floating point array of samples. Length will be of the actual samples acquired
42-
43-
- Such that all record 0 waveforms are first. For example, with a channel list of 0,1, you would have the following index values:
44-
45-
- index 0 = record 0, channel 0
46-
- index 1 = record 0, channel 1
47-
- index 2 = record 1, channel 0
48-
- index 3 = record 1, channel 1
49-
- etc.
50-
51-
52-
If you need more performance or need to work with `SciPy <https://www.scipy.org/>`_, you can use the `fetch_into()` method instead of `fetch()`. This
53-
method takes an already allocated `numpy <http://www.numpy.org/>`_ array and puts the acquired samples in it. Data types supported:
54-
55-
- `numpy.float64`
56-
- `numpy.int8`
57-
- `numpy.in16`
58-
- `numpy.int32`
59-
60-
.. code-block:: python
61-
62-
voltage_range = 1.0
63-
record_length = 2000
64-
channels = [0, 1]
65-
num_channels = len(channels)
66-
num_records = 5
67-
wfm = numpy.ndarray(num_channels * record_length, dtype=numpy.int8)
68-
session.configure_vertical(voltage_range, niscope.VerticalCoupling.AC)
69-
session.configure_horizontal_timing(50000000, record_length, 50.0, num_records, True)
70-
with session.initiate():
71-
waveform_infos = session.channels[channels].fetch_into(wfm=wfm, num_records=num_records)
72-
73-
The waveform_infos returned from `fetch_into <niscope/class.html#fetch-into>`_ is a 1D list of Python objects
74-
75-
- Attributes:
76-
77-
- **relative_initial_x** (float) the time (in seconds) from the trigger to the first sample in the fetched waveform
78-
- **absolute_initial_x** (float) timestamp (in seconds) of the first fetched sample. This timestamp is comparable between records and acquisitions; devices that do not support this parameter use 0 for this output.
79-
- **x_increment** (float) the time between points in the acquired waveform in seconds
80-
- **channel** (str) channel name this waveform was asquire from
81-
- **record** (int) record number of this waveform
82-
- **gain** (float) the gain factor of the given channel; useful for scaling binary data with the following formula:
83-
84-
voltage = binary data * gain factor + offset
85-
86-
- **offset** (float) the offset factor of the given channel; useful for scaling binary data with the following formula:
87-
88-
voltage = binary data * gain factor + offset
89-
90-
- **samples** (numpy array of datatype used) floating point array of samples. Length will be of the actual samples acquired
91-
92-
- Such that all record 0 waveforms are first. For example, with a channel list of 0,1, you would have the following index values:
93-
94-
- index 0 = record 0, channel 0
95-
- index 1 = record 0, channel 1
96-
- index 2 = record 1, channel 0
97-
- index 3 = record 1, channel 1
98-
- etc.
24+
If you need faster fetch performance, or to directly interface with `SciPy <https://www.scipy.org/>`_, you can use the **fetch_into()** method instead of **fetch()**. See the fetch_into example.
9925

10026

10127
`Other usage examples can be found on GitHub. <https://github.com/ni/nimi-python/tree/master/src/niscope/examples>`_

docs/niscope/examples.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ niscope_fetch_forever.py
2121
:encoding: utf8
2222
:caption: `(niscope_fetch_forever.py) <https://github.com/ni/nimi-python/blob/master/src/niscope/examples/niscope_fetch_forever.py>`_
2323

24+
niscope_fetch_info.py
25+
---------------------
26+
27+
.. literalinclude:: ../../src/niscope/examples/niscope_fetch_info.py
28+
:language: python
29+
:linenos:
30+
:encoding: utf8
31+
:caption: `(niscope_fetch_info.py) <https://github.com/ni/nimi-python/blob/master/src/niscope/examples/niscope_fetch_info.py>`_
32+
2433
niscope_read.py
2534
---------------
2635

generated/niscope/README.rst

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -142,81 +142,7 @@ The following is a basic example of using the **niscope** module to open a sessi
142142
# Find all record number 3
143143
rec3 = [wfm for wfm in waveforms if wfm.record == 3]
144144
145-
The waveform returned from `fetch <niscope/class.html#fetch>`_ is a flat list of Python objects
146-
147-
- Attributes:
148-
149-
- **relative_initial_x** (float) the time (in seconds) from the trigger to the first sample in the fetched waveform
150-
- **absolute_initial_x** (float) timestamp (in seconds) of the first fetched sample. This timestamp is comparable between records and acquisitions; devices that do not support this parameter use 0 for this output.
151-
- **x_increment** (float) the time between points in the acquired waveform in seconds
152-
- **channel** (str) channel name this waveform was acquired from
153-
- **record** (int) record number of this waveform
154-
- **gain** (float) the gain factor of the given channel; useful for scaling binary data with the following formula:
155-
156-
voltage = binary data * gain factor + offset
157-
158-
- **offset** (float) the offset factor of the given channel; useful for scaling binary data with the following formula:
159-
160-
voltage = binary data * gain factor + offset
161-
162-
- **samples** (array of float) floating point array of samples. Length will be of the actual samples acquired
163-
164-
- Such that all record 0 waveforms are first. For example, with a channel list of 0,1, you would have the following index values:
165-
166-
- index 0 = record 0, channel 0
167-
- index 1 = record 0, channel 1
168-
- index 2 = record 1, channel 0
169-
- index 3 = record 1, channel 1
170-
- etc.
171-
172-
173-
If you need more performance or need to work with `SciPy <https://www.scipy.org/>`_, you can use the `fetch_into()` method instead of `fetch()`. This
174-
method takes an already allocated `numpy <http://www.numpy.org/>`_ array and puts the acquired samples in it. Data types supported:
175-
176-
- `numpy.float64`
177-
- `numpy.int8`
178-
- `numpy.in16`
179-
- `numpy.int32`
180-
181-
.. code-block:: python
182-
183-
voltage_range = 1.0
184-
record_length = 2000
185-
channels = [0, 1]
186-
num_channels = len(channels)
187-
num_records = 5
188-
wfm = numpy.ndarray(num_channels * record_length, dtype=numpy.int8)
189-
session.configure_vertical(voltage_range, niscope.VerticalCoupling.AC)
190-
session.configure_horizontal_timing(50000000, record_length, 50.0, num_records, True)
191-
with session.initiate():
192-
waveform_infos = session.channels[channels].fetch_into(wfm=wfm, num_records=num_records)
193-
194-
The waveform_infos returned from `fetch_into <niscope/class.html#fetch-into>`_ is a 1D list of Python objects
195-
196-
- Attributes:
197-
198-
- **relative_initial_x** (float) the time (in seconds) from the trigger to the first sample in the fetched waveform
199-
- **absolute_initial_x** (float) timestamp (in seconds) of the first fetched sample. This timestamp is comparable between records and acquisitions; devices that do not support this parameter use 0 for this output.
200-
- **x_increment** (float) the time between points in the acquired waveform in seconds
201-
- **channel** (str) channel name this waveform was asquire from
202-
- **record** (int) record number of this waveform
203-
- **gain** (float) the gain factor of the given channel; useful for scaling binary data with the following formula:
204-
205-
voltage = binary data * gain factor + offset
206-
207-
- **offset** (float) the offset factor of the given channel; useful for scaling binary data with the following formula:
208-
209-
voltage = binary data * gain factor + offset
210-
211-
- **samples** (numpy array of datatype used) floating point array of samples. Length will be of the actual samples acquired
212-
213-
- Such that all record 0 waveforms are first. For example, with a channel list of 0,1, you would have the following index values:
214-
215-
- index 0 = record 0, channel 0
216-
- index 1 = record 0, channel 1
217-
- index 2 = record 1, channel 0
218-
- index 3 = record 1, channel 1
219-
- etc.
145+
If you need faster fetch performance, or to directly interface with `SciPy <https://www.scipy.org/>`_, you can use the **fetch_into()** method instead of **fetch()**. See the fetch_into example.
220146

221147

222148
`Other usage examples can be found on GitHub. <https://github.com/ni/nimi-python/tree/master/src/niscope/examples>`_
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/python
2+
3+
import argparse
4+
import niscope
5+
import numpy
6+
import pprint
7+
import sys
8+
9+
pp = pprint.PrettyPrinter(indent=4, width=80)
10+
11+
12+
def example(resource_name, channels, options, length, voltage):
13+
# fetch_into() allows you to preallocate and reuse the destination of the fetched waveforms, which can result in better performance at the expense of the usability of fetch().
14+
channels = [ch.strip() for ch in channels.split(",")]
15+
num_channels = len(channels)
16+
num_records = 5
17+
total_num_wfms = num_channels * num_records
18+
# preallocate a single array for all samples in all waveforms
19+
# Supported array types are: numpy.float64, numpy.int8, numpy.int16, numpy.int32
20+
# int8, int16, int32 are for fetching unscaled data, which is the fastest way to fetch.
21+
# Gain and Offset are stored in the returned WaveformInfo objects and can be applied to the data by the user later.
22+
wfm = numpy.ndarray(length * total_num_wfms, dtype=numpy.float64)
23+
with niscope.Session(resource_name=resource_name, options=options) as session:
24+
session.configure_vertical(range=voltage, coupling=niscope.VerticalCoupling.AC)
25+
session.configure_horizontal_timing(min_sample_rate=50000000, min_num_pts=length, ref_position=50.0, num_records=num_records, enforce_realtime=True)
26+
with session.initiate():
27+
waveforms = session.channels[channels].fetch_into(waveform=wfm, num_records=num_records)
28+
for i in range(len(waveforms)):
29+
print(f'Waveform {i} information:')
30+
print(f'{waveforms[i]}\n\n')
31+
print(f'Samples: {waveforms[i].samples.tolist()}')
32+
33+
34+
def _main(argsv):
35+
parser = argparse.ArgumentParser(description='Fetches data directly into a preallocated numpy array.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
36+
parser.add_argument('-n', '--resource-name', default='PXI1Slot2', help='Resource name of an NI digitizer.')
37+
parser.add_argument('-c', '--channels', default='0', help='Channel(s) to use')
38+
parser.add_argument('-l', '--length', default=100, type=int, help='Measure record length')
39+
parser.add_argument('-v', '--voltage', default=1.0, type=float, help='Voltage range (V)')
40+
parser.add_argument('-op', '--option-string', default='', type=str, help='Option string')
41+
args = parser.parse_args(argsv)
42+
example(args.resource_name, args.channels, args.option_string, args.length, args.voltage)
43+
44+
45+
def main():
46+
_main(sys.argv[1:])
47+
48+
49+
def test_example():
50+
options = {'simulate': True, 'driver_setup': {'Model': '5164', 'BoardType': 'PXIe', }, }
51+
example('PXI1Slot2', '0, 1', options, 100, 1.0)
52+
53+
54+
def test_main():
55+
cmd_line = ['--option-string', 'Simulate=1, DriverSetup=Model:5164; BoardType:PXIe', ]
56+
_main(cmd_line)
57+
58+
59+
if __name__ == '__main__':
60+
main()
61+

0 commit comments

Comments
 (0)