Skip to content

Commit e697de8

Browse files
authored
Support for complex types (#2098)
* Implementation of Complex Numbers * Inclusion of test case for WriteWaveformComplexF64 * Implementation of f32 and i16 and respective test cases * Updated changelog and included examples * Updated the metadata * Remove log file * Updated functions.py file and few minor cahnges * Update Changelog.md file * Updated minor change. * Updated few minor fix * Updated the waveform type as ctype * Updated the seesion lock parameter for new arb functions * Updated Code Review comments * Change log code review comments included * Removed RFSG specific changes and include complextype only if needed * Removed RFSG specific changes * Updated matchers and complextype file to be generated bsaed on condition if complex is used * Updating spacing issue * Updating the complex variables and code review comments incorporated * Test case name change * Updating minor fixes * Updating the test cases * Update i16 functions * Update duplicate entries * remove zone.identifier file changes. * Updaed comments for are_complex_parameters_used * Updated the numpy api names and description. * Updating default value of complext type as None rather than 'none' * Moving the common lines in complex matchers into a function * Updaing the library.py.mako file to import complex type * Code Reivew Changes * Updaed code review comments * Updaing only necessary fields in COMPLEX_NUMBER_PARAMETERS
1 parent 3941e98 commit e697de8

25 files changed

+801
-24
lines changed

build/helper/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
from build.helper.metadata_add_all import add_all_metadata # noqa: F401
4444

45+
from build.helper.metadata_filters import are_complex_parameters_used # noqa: F401
4546
from build.helper.metadata_filters import filter_codegen_attributes # noqa: F401
4647
from build.helper.metadata_filters import filter_codegen_attributes_public_only # noqa: F401
4748
from build.helper.metadata_filters import filter_codegen_enums # noqa: F401

build/helper/codegen_helper.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ def get_ctype_variable_declaration_snippet(parameter, parameters, ivi_dance_step
256256
else:
257257
module_name = '_visatype'
258258

259+
# Use _complextype.py file for complex parameter
260+
if parameter['complex_type'] is not None:
261+
module_name = '_complextype'
262+
259263
if parameter['is_string'] is True:
260264
definitions = _get_ctype_variable_definition_snippet_for_string(parameter, parameters, ivi_dance_step, module_name)
261265
elif parameter['is_buffer'] is True:
@@ -362,7 +366,12 @@ def _get_ctype_variable_definition_snippet_for_scalar(parameter, parameters, ivi
362366
definition = '{}.{}({}) # case S150'.format(module_name, parameter['ctypes_type'], parameter['python_name'])
363367
elif corresponding_buffer_parameters and corresponding_buffer_parameters[0]['direction'] == 'in': # We are only looking at the first one to see if it is 'in'. Assumes all are the same here, assert below if not
364368
# Parameter denotes the size of another (the "corresponding") parameter.
365-
definitions.append(parameter['ctypes_variable_name'] + ' = {0}.{1}(0 if {2} is None else len({2})) # case S160'.format(module_name, parameter['ctypes_type'], corresponding_buffer_parameters[0]['python_name']))
369+
# Interleaved array length is going to be double the length of number of samples.
370+
# This is used for complex waveforms, where the real and imaginary parts are interleaved in the array.
371+
if corresponding_buffer_parameters[0]['complex_type'] == 'interleaved':
372+
definitions.append(parameter['ctypes_variable_name'] + ' = {0}.{1}(0 if {2} is None else len({2}) // 2) # case S160'.format(module_name, parameter['ctypes_type'], corresponding_buffer_parameters[0]['python_name']))
373+
else:
374+
definitions.append(parameter['ctypes_variable_name'] + ' = {0}.{1}(0 if {2} is None else len({2})) # case S160'.format(module_name, parameter['ctypes_type'], corresponding_buffer_parameters[0]['python_name']))
366375
else:
367376
if corresponding_buffer_parameters[0]['size']['mechanism'] == 'ivi-dance': # We are only looking at the first one. Assumes all are the same here, assert below if not
368377
# Verify all corresponding_buffer_parameters are 'out' and 'ivi-dance'
@@ -426,7 +435,10 @@ def _get_ctype_variable_definition_snippet_for_buffers(parameter, parameters, iv
426435
definition = None
427436

428437
if parameter['numpy'] is True and use_numpy_array is True:
429-
definition = '_get_ctypes_pointer_for_buffer(value={}) # case B510'.format(parameter['python_name'])
438+
if parameter['complex_type'] is None:
439+
definition = '_get_ctypes_pointer_for_buffer(value={}) # case B510'.format(parameter['python_name'])
440+
else:
441+
definition = '_get_ctypes_pointer_for_buffer(value={}, library_type={}.{}) # case B510'.format(parameter['python_name'], module_name, parameter['ctypes_type'])
430442
elif parameter['direction'] == 'in':
431443
if custom_type is not None:
432444
definition = '_get_ctypes_pointer_for_buffer([{0}.{1}(c) for c in {2}], library_type={0}.{1}) # case B540'.format(module_name, parameter['ctypes_type'], parameter['python_name'])

build/helper/helper.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,28 @@
77

88
# noqa statements because we want to format the table in a readable way
99
_type_map = {
10-
'ViConstString': { 'array_type': None, 'python_type': 'str', 'numpy_type': None, }, # noqa: E201, E202, E241
11-
'ViString': { 'array_type': None, 'python_type': 'str', 'numpy_type': None, }, # noqa: E201, E202, E241
12-
'ViInt8': { 'array_type': 'b', 'python_type': 'int', 'numpy_type': 'int8', }, # noqa: E201, E202, E241
13-
'ViUInt8': { 'array_type': 'B', 'python_type': 'int', 'numpy_type': 'uint8', }, # noqa: E201, E202, E241
14-
'ViInt16': { 'array_type': 'h', 'python_type': 'int', 'numpy_type': 'int16', }, # noqa: E201, E202, E241
15-
'ViUInt16': { 'array_type': 'H', 'python_type': 'int', 'numpy_type': 'uint16', }, # noqa: E201, E202, E241
16-
'ViInt32': { 'array_type': 'l', 'python_type': 'int', 'numpy_type': 'int32', }, # noqa: E201, E202, E241
17-
'ViUInt32': { 'array_type': 'L', 'python_type': 'int', 'numpy_type': 'uint32', }, # noqa: E201, E202, E241
18-
'ViInt64': { 'array_type': 'q', 'python_type': 'int', 'numpy_type': 'int64', }, # noqa: E201, E202, E241
19-
'ViUInt64': { 'array_type': 'Q', 'python_type': 'int', 'numpy_type': 'uint64', }, # noqa: E201, E202, E241
20-
'ViReal32': { 'array_type': 'f', 'python_type': 'float', 'numpy_type': 'float32', }, # noqa: E201, E202, E241
21-
'ViReal64': { 'array_type': 'd', 'python_type': 'float', 'numpy_type': 'float64', }, # noqa: E201, E202, E241
22-
'ViStatus': { 'array_type': None, 'python_type': 'int', 'numpy_type': None, }, # noqa: E201, E202, E241
23-
'ViSession': { 'array_type': None, 'python_type': 'int', 'numpy_type': None, }, # noqa: E201, E202, E241
24-
'ViAttr': { 'array_type': None, 'python_type': 'int', 'numpy_type': None, }, # noqa: E201, E202, E241
25-
'ViChar': { 'array_type': None, 'python_type': 'int', 'numpy_type': None, }, # noqa: E201, E202, E241
26-
'ViChar[]': { 'array_type': None, 'python_type': 'str', 'numpy_type': None, }, # noqa: E201, E202, E241
27-
'ViBoolean': { 'array_type': None, 'python_type': 'bool', 'numpy_type': None, }, # noqa: E201, E202, E241
28-
'ViRsrc': { 'array_type': None, 'python_type': 'str', 'numpy_type': 'bool_', }, # noqa: E201, E202, E241
10+
'ViConstString': { 'array_type': None, 'python_type': 'str', 'numpy_type': None, }, # noqa: E201, E202, E241
11+
'ViString': { 'array_type': None, 'python_type': 'str', 'numpy_type': None, }, # noqa: E201, E202, E241
12+
'ViInt8': { 'array_type': 'b', 'python_type': 'int', 'numpy_type': 'int8', }, # noqa: E201, E202, E241
13+
'ViUInt8': { 'array_type': 'B', 'python_type': 'int', 'numpy_type': 'uint8', }, # noqa: E201, E202, E241
14+
'ViInt16': { 'array_type': 'h', 'python_type': 'int', 'numpy_type': 'int16', }, # noqa: E201, E202, E241
15+
'ViUInt16': { 'array_type': 'H', 'python_type': 'int', 'numpy_type': 'uint16', }, # noqa: E201, E202, E241
16+
'ViInt32': { 'array_type': 'l', 'python_type': 'int', 'numpy_type': 'int32', }, # noqa: E201, E202, E241
17+
'ViUInt32': { 'array_type': 'L', 'python_type': 'int', 'numpy_type': 'uint32', }, # noqa: E201, E202, E241
18+
'ViInt64': { 'array_type': 'q', 'python_type': 'int', 'numpy_type': 'int64', }, # noqa: E201, E202, E241
19+
'ViUInt64': { 'array_type': 'Q', 'python_type': 'int', 'numpy_type': 'uint64', }, # noqa: E201, E202, E241
20+
'ViReal32': { 'array_type': 'f', 'python_type': 'float', 'numpy_type': 'float32', }, # noqa: E201, E202, E241
21+
'ViReal64': { 'array_type': 'd', 'python_type': 'float', 'numpy_type': 'float64', }, # noqa: E201, E202, E241
22+
'ViStatus': { 'array_type': None, 'python_type': 'int', 'numpy_type': None, }, # noqa: E201, E202, E241
23+
'ViSession': { 'array_type': None, 'python_type': 'int', 'numpy_type': None, }, # noqa: E201, E202, E241
24+
'ViAttr': { 'array_type': None, 'python_type': 'int', 'numpy_type': None, }, # noqa: E201, E202, E241
25+
'ViChar': { 'array_type': None, 'python_type': 'int', 'numpy_type': None, }, # noqa: E201, E202, E241
26+
'ViChar[]': { 'array_type': None, 'python_type': 'str', 'numpy_type': None, }, # noqa: E201, E202, E241
27+
'ViBoolean': { 'array_type': None, 'python_type': 'bool', 'numpy_type': None, }, # noqa: E201, E202, E241
28+
'ViRsrc': { 'array_type': None, 'python_type': 'str', 'numpy_type': 'bool_', }, # noqa: E201, E202, E241
29+
'NIComplexNumber': { 'array_type': None, 'python_type': None, 'numpy_type': 'complex128', }, # noqa: E201, E202, E241
30+
'NIComplexNumberF32': { 'array_type': None, 'python_type': None, 'numpy_type': 'complex64', }, # noqa: E201, E202, E241
31+
'NIComplexI16': { 'array_type': None, 'python_type': None, 'numpy_type': 'int16', }, # noqa: E201, E202, E241
2932
}
3033

3134

build/helper/metadata_add_all.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ def _add_ctypes_type(parameter, config):
126126
parameter['ctypes_type_library_call'] = module_name + parameter['ctypes_type']
127127

128128

129+
def _add_complex_type(parameter):
130+
'''Adds a complex_type parameter to the metadata for complex numbers'''
131+
if 'complex_type' not in parameter:
132+
parameter['complex_type'] = None
133+
134+
129135
def _add_numpy_info(parameter, parameters, config):
130136
'''Adds the following numpy-related information:
131137
@@ -450,6 +456,7 @@ def add_all_function_metadata(functions, config):
450456
_add_python_type(p, config)
451457
_add_ctypes_variable_name(p)
452458
_add_ctypes_type(p, config)
459+
_add_complex_type(p)
453460
_add_numpy_info(p, functions[f]['parameters'], config)
454461
_add_default_value_name(p)
455462
_add_default_value_name_for_docs(p, config['module_name'])

build/helper/metadata_filters.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'skip_all_except_numpy_parameters': False,
2020
'mechanism': 'fixed, passed-in, len',
2121
'python_api_list': True,
22+
'skip_all_except_complex_type_parameters': False,
2223
},
2324
ParameterUsageOptions.SESSION_METHOD_PASSTHROUGH_CALL: {
2425
'skip_session_handle': True,
@@ -33,6 +34,7 @@
3334
'skip_all_except_numpy_parameters': False,
3435
'mechanism': 'fixed, passed-in, len',
3536
'python_api_list': True,
37+
'skip_all_except_complex_type_parameters': False,
3638
},
3739
ParameterUsageOptions.SESSION_NUMPY_INTO_METHOD_DECLARATION: {
3840
'skip_session_handle': True,
@@ -47,6 +49,7 @@
4749
'skip_all_except_numpy_parameters': False,
4850
'mechanism': 'fixed, passed-in',
4951
'python_api_list': False,
52+
'skip_all_except_complex_type_parameters': False,
5053
},
5154
ParameterUsageOptions.INTERPRETER_NUMPY_INTO_METHOD_DECLARATION: {
5255
'skip_session_handle': True,
@@ -61,6 +64,7 @@
6164
'skip_all_except_numpy_parameters': False,
6265
'mechanism': 'fixed, passed-in',
6366
'python_api_list': False,
67+
'skip_all_except_complex_type_parameters': False,
6468
},
6569
ParameterUsageOptions.SESSION_METHOD_CALL: {
6670
'skip_session_handle': True,
@@ -75,6 +79,7 @@
7579
'skip_all_except_numpy_parameters': False,
7680
'mechanism': 'fixed, passed-in',
7781
'python_api_list': True,
82+
'skip_all_except_complex_type_parameters': False,
7883
},
7984
ParameterUsageOptions.DOCUMENTATION_SESSION_METHOD: {
8085
'skip_session_handle': True,
@@ -89,6 +94,7 @@
8994
'skip_all_except_numpy_parameters': False,
9095
'mechanism': 'any',
9196
'python_api_list': True,
97+
'skip_all_except_complex_type_parameters': False,
9298
},
9399
ParameterUsageOptions.LIBRARY_METHOD_DECLARATION: {
94100
'skip_session_handle': False,
@@ -103,6 +109,7 @@
103109
'skip_all_except_numpy_parameters': False,
104110
'mechanism': 'any',
105111
'python_api_list': True,
112+
'skip_all_except_complex_type_parameters': False,
106113
},
107114
ParameterUsageOptions.LIBRARY_METHOD_CALL: {
108115
'skip_session_handle': False,
@@ -117,6 +124,7 @@
117124
'skip_all_except_numpy_parameters': False,
118125
'mechanism': 'any',
119126
'python_api_list': True,
127+
'skip_all_except_complex_type_parameters': False,
120128
},
121129
ParameterUsageOptions.GRPC_REQUEST_PARAMETERS: {
122130
'skip_session_handle': False,
@@ -131,6 +139,7 @@
131139
'skip_all_except_numpy_parameters': False,
132140
'mechanism': 'any',
133141
'python_api_list': True,
142+
'skip_all_except_complex_type_parameters': False,
134143
},
135144
ParameterUsageOptions.CTYPES_ARGTYPES: {
136145
'skip_session_handle': False,
@@ -145,6 +154,7 @@
145154
'skip_all_except_numpy_parameters': False,
146155
'mechanism': 'any',
147156
'python_api_list': True,
157+
'skip_all_except_complex_type_parameters': False,
148158
},
149159
ParameterUsageOptions.INTERPRETER_METHOD_DECLARATION: {
150160
'skip_session_handle': True,
@@ -159,6 +169,7 @@
159169
'skip_all_except_numpy_parameters': False,
160170
'mechanism': 'fixed, passed-in, len',
161171
'python_api_list': True,
172+
'skip_all_except_complex_type_parameters': False,
162173
},
163174
ParameterUsageOptions.INPUT_PARAMETERS: {
164175
'skip_session_handle': True,
@@ -173,6 +184,7 @@
173184
'skip_all_except_numpy_parameters': False,
174185
'mechanism': 'any',
175186
'python_api_list': True,
187+
'skip_all_except_complex_type_parameters': False,
176188
},
177189
ParameterUsageOptions.LIBRARY_OUTPUT_PARAMETERS: {
178190
'skip_session_handle': True,
@@ -187,6 +199,7 @@
187199
'skip_all_except_numpy_parameters': False,
188200
'mechanism': 'any',
189201
'python_api_list': True,
202+
'skip_all_except_complex_type_parameters': False,
190203
},
191204
ParameterUsageOptions.API_OUTPUT_PARAMETERS: {
192205
'skip_session_handle': True,
@@ -201,6 +214,7 @@
201214
'skip_all_except_numpy_parameters': False,
202215
'mechanism': 'any',
203216
'python_api_list': False,
217+
'skip_all_except_complex_type_parameters': False,
204218
},
205219
ParameterUsageOptions.API_NUMPY_OUTPUT_PARAMETERS: {
206220
'skip_session_handle': True,
@@ -215,6 +229,7 @@
215229
'skip_all_except_numpy_parameters': False,
216230
'mechanism': 'any',
217231
'python_api_list': False,
232+
'skip_all_except_complex_type_parameters': False,
218233
},
219234
ParameterUsageOptions.GRPC_OUTPUT_PARAMETERS: {
220235
'skip_session_handle': True,
@@ -229,6 +244,7 @@
229244
'skip_all_except_numpy_parameters': False,
230245
'mechanism': 'any',
231246
'python_api_list': False,
247+
'skip_all_except_complex_type_parameters': False,
232248
},
233249
ParameterUsageOptions.NUMPY_PARAMETERS: {
234250
'skip_session_handle': True,
@@ -243,6 +259,7 @@
243259
'skip_all_except_numpy_parameters': True,
244260
'mechanism': 'any',
245261
'python_api_list': True,
262+
'skip_all_except_complex_type_parameters': False,
246263
},
247264
ParameterUsageOptions.IVI_DANCE_PARAMETER: {
248265
'skip_session_handle': True,
@@ -257,6 +274,7 @@
257274
'skip_all_except_numpy_parameters': False,
258275
'mechanism': 'ivi-dance, ivi-dance-with-a-twist',
259276
'python_api_list': True,
277+
'skip_all_except_complex_type_parameters': False,
260278
},
261279
ParameterUsageOptions.LEN_PARAMETER: {
262280
'skip_session_handle': True,
@@ -271,6 +289,7 @@
271289
'skip_all_except_numpy_parameters': False,
272290
'mechanism': 'len',
273291
'python_api_list': True,
292+
'skip_all_except_complex_type_parameters': False,
274293
},
275294
ParameterUsageOptions.INPUT_ENUM_PARAMETERS: {
276295
'skip_session_handle': True,
@@ -285,6 +304,22 @@
285304
'skip_all_except_numpy_parameters': False,
286305
'mechanism': 'any',
287306
'python_api_list': True,
307+
'skip_all_except_complex_type_parameters': False,
308+
},
309+
ParameterUsageOptions.COMPLEX_NUMBER_PARAMETERS: {
310+
'skip_session_handle': True,
311+
'skip_input_parameters': False,
312+
'skip_output_parameters': False,
313+
'but_keep_output_numpy_array_parameters': False,
314+
'skip_size_parameter': False,
315+
'reordered_for_default_values': False,
316+
'skip_repeated_capability_parameter': False,
317+
'skip_non_enum_parameter': False,
318+
'skip_numpy_parameters': False,
319+
'skip_all_except_numpy_parameters': False,
320+
'mechanism': 'any',
321+
'python_api_list': True,
322+
'skip_all_except_complex_type_parameters': True,
288323
},
289324
}
290325

@@ -349,6 +384,8 @@ def filter_parameters(parameters, parameter_usage_options):
349384
skip = False
350385
if not options_to_use['python_api_list'] and not x['use_in_python_api']:
351386
skip = True
387+
if options_to_use['skip_all_except_complex_type_parameters'] and x['complex_type'] is None:
388+
skip = True
352389
if not skip:
353390
parameters_to_use.append(x)
354391

@@ -451,3 +488,13 @@ def filter_codegen_enums(enums):
451488
return {k: v for k, v in enums.items() if v['codegen_method'] != 'no'}
452489

453490

491+
def are_complex_parameters_used(functions):
492+
'''Returns bool based on whether any complex parameters are used in the functions metadata.'''
493+
are_complex_parameters_used = False
494+
complex_parameters = []
495+
for k, v in functions.items():
496+
complex_parameters = filter_parameters(v['parameters'], ParameterUsageOptions.COMPLEX_NUMBER_PARAMETERS)
497+
if complex_parameters != []:
498+
are_complex_parameters_used = True
499+
break
500+
return are_complex_parameters_used

build/helper/parameter_usage_options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ class ParameterUsageOptions(AutoNumber):
6262
'''Get the len parameter'''
6363
INPUT_ENUM_PARAMETERS = ()
6464
'''Get any input parameters whose type is enum'''
65+
COMPLEX_NUMBER_PARAMETERS = ()
66+
'''Get all parameters of complex type'''
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
${template_parameters['encoding_tag']}
2+
# This file was generated
3+
<%
4+
import build.helper as helper
5+
config = template_parameters['metadata'].config
6+
module_name = config['module_name']
7+
%>\
8+
import ctypes
9+
import ${module_name}._visatype as _visatype
10+
11+
12+
class NIComplexNumber(ctypes.Structure):
13+
_fields_ = [("real", _visatype.ViReal64), ("imag", _visatype.ViReal64)]
14+
15+
16+
class NIComplexNumberF32(ctypes.Structure):
17+
_fields_ = [("real", _visatype.ViReal32), ("imag", _visatype.ViReal32)]
18+
19+
20+
class NIComplexI16(ctypes.Structure):
21+
_fields_ = [("real", _visatype.ViInt16), ("imag", _visatype.ViInt16)]

build/templates/_library.py.mako

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ driver_name = config['driver_name']
1212
1313
functions = config['functions']
1414
functions = helper.filter_library_functions(functions)
15+
are_complex_parameters_used = helper.are_complex_parameters_used(functions)
1516
%>\
1617

1718
import ctypes
1819
import ${module_name}.errors as errors
1920
import threading
2021

22+
% if are_complex_parameters_used:
23+
from ${module_name}._complextype import * # noqa: F403
24+
% endif
2125
from ${module_name}._visatype import * # noqa: F403,H303
2226
% for c in config['custom_types']:
2327

0 commit comments

Comments
 (0)