ADCT Python
Python client API implementation for publishing ADCT data
Loading...
Searching...
No Matches
Classes | Functions | Variables
adctk.adc_types Namespace Reference

Classes

class  Field
 Field details indicate how to handle a data returned from builder lookups. More...
 
class  KeyType
 The builder type (section or map value) or unknown. More...
 
class  ScalarType
 field types for scientific data encode/decode with json. More...
 

Functions

bool is_string_type (ScalarType st)
 return True if st is of the single string subclassifications
 
str scalar_to_json (ScalarType st, array_type=False)
 
 scalar_from_json (str name, strict=False)
 convert json type string used in add functions to ScalarType
 
 scalar_from_numpy (numpy.dtype dt)
 convert numpy dtype to adc scalar type
 
ScalarType get_common_scalar (list|tuple|collections.abc.Set slt, int depth, list[int] maxdepth)
 get the common scalar type from a possibly nested tuple/set/list
 
numpy.ndarray forsplitcomplex (numpy.ndarray a)
 
numpy.ndarray csplitcomplex (numpy.ndarray a)
 
 get_list_format_numpy (numpy.ndarray a)
 build a nested list out of numpy array, accounting for int must fit in int64 or be string encoded and accounting for complex numbers.
 
 get_list_format (list|tuple|collections.abc.Set|numpy.ndarray slt, ScalarType st=None)
 build a nested list out of homogeneous python or numpy nest
 
 typecode_to_scalar (array.array v)
 
 type_to_scalar (type t, value=None)
 convert python type to ScalarType numpy array types are supported if value is given.
 
bool fits_int64 (int v)
 
bool fits_uint64 (int v)
 

Variables

int ADC_SUPPORT_GPU_FLOATS = 0
 Set to 1 if 8/16 bit float types for gpus are supported.
 
int ADC_SUPPORT_EXTENDED_FLOATS = 1
 Set to 1 if 80 bit floats for cpus are supported.
 
int ADC_SUPPORT_QUAD_FLOATS = 0
 Set to 1 if 128 bit floats for cpus are supported.
 

Function Documentation

◆ csplitcomplex()

numpy.ndarray adctk.adc_types.csplitcomplex ( numpy.ndarray  a)
Convert a numpy array of complex to a flat array of real pairs
304def csplitcomplex(a: numpy.ndarray) -> numpy.ndarray:
305 """Convert a numpy array of complex to a flat array of real pairs"""
306 t = a.real.dtype
307 c = numpy.ravel(a)
308 top = 2*c.size
309 r = numpy.ndarray((top), dtype=t)
310 for i in range(0,c.size):
311 r[2*i] = c[i].real
312 r[2*i+1] = c[i].imag
313 return r
314
315# pylint: disable-next=too-many-return-statements

◆ fits_int64()

bool adctk.adc_types.fits_int64 ( int  v)
Returns
True if v can be stored as json signed int 64 bit
506def fits_int64(v: int) -> bool:
507 """! @return True if v can be stored as json signed int 64 bit"""
508 if v is not None and type(v) == int:
509 if -9223372036854775808 <= v <= 9223372036854775807:
510 return True
511 return False
512 raise ValueError("argument must be int")
513

◆ fits_uint64()

bool adctk.adc_types.fits_uint64 ( int  v)
Returns
True if v can be stored as json unsigned int 64 bit
514def fits_uint64(v: int) -> bool:
515 """! @return True if v can be stored as json unsigned int 64 bit"""
516 if v is not None and type(v) == int:
517 if 0 <= v <= 18446744073709551615:
518 return True
519 return False
520 raise ValueError("argument must be int")

◆ forsplitcomplex()

numpy.ndarray adctk.adc_types.forsplitcomplex ( numpy.ndarray  a)
Convert a numpy array of fortran complex to a flat array of real pairs
293def forsplitcomplex(a: numpy.ndarray) -> numpy.ndarray:
294 """Convert a numpy array of fortran complex to a flat array of real pairs"""
295 t = a.real.dtype
296 c = numpy.ravel(a, order='F')
297 top = 2*c.size
298 r = numpy.ndarray((top), dtype=t)
299 for i in range(0,c.size):
300 r[2*i] = c[i].real
301 r[2*i+1] = c[i].imag
302 return r
303

◆ get_common_scalar()

ScalarType adctk.adc_types.get_common_scalar ( list|tuple|collections.abc.Set  slt,
int  depth,
list[int]  maxdepth 
)

get the common scalar type from a possibly nested tuple/set/list

Parameters
sltdata nest with single scalar type at the leaf locations.
depthcurrent recursion depth, 0 at the application level.
maxdepthis a one-element array output argument.
223 maxdepth: list[int]) -> ScalarType:
224 """! @brief get the common scalar type from a possibly nested tuple/set/list
225
226 @param slt data nest with single scalar type at the leaf locations.
227 @param depth current recursion depth, 0 at the application level.
228 @param maxdepth is a one-element array output argument.
229 """
230 imin =[0]
231 imax =[0]
232 dtypes = set()
233 pt = __get_common_py_type(slt, depth, maxdepth, imin, imax, dtypes)
234 if pt is None:
235 return ScalarType.cp_none
236 if pt == int:
237 if fits_int64(imin[0]) and fits_int64(imax[0]):
238 return ScalarType.cp_int64
239 return ScalarType.cp_number_str
240 return type_to_scalar(pt, next(iter(slt)))
241
242# pylint: disable-next=R0917,R0913,R0912

◆ get_list_format()

adctk.adc_types.get_list_format ( list|tuple|collections.abc.Set|numpy.ndarray  slt,
ScalarType   st = None 
)

build a nested list out of homogeneous python or numpy nest

352def get_list_format(slt: list|tuple|collections.abc.Set|numpy.ndarray, st: ScalarType = None):
353 """! @brief build a nested list out of homogeneous python or numpy nest"""
354 if slt is None:
355 return None
356 if isinstance(slt, numpy.ndarray):
357 return get_list_format_numpy(slt)
358 result = []
359 for i in slt:
360 if isinstance(i, list|tuple|collections.abc.Set):
361 it = get_list_format(i, st)
362 else:
363 # fixme: it = pyval(py_or_numpy). is there a numpy func for this
364 match st:
365 case ScalarType.cp_uint64:
366 it = str(int(i))
367 case ScalarType.cp_number_str:
368 it = i
369 case _:
370 it = i
371
372 result.append(it)
373 return result
374

◆ get_list_format_numpy()

adctk.adc_types.get_list_format_numpy ( numpy.ndarray  a)

build a nested list out of numpy array, accounting for int must fit in int64 or be string encoded and accounting for complex numbers.

316def get_list_format_numpy(a: numpy.ndarray):
317 """! @brief build a nested list out of numpy array, accounting for
318 int must fit in int64 or be string encoded and accounting for
319 complex numbers.
320 """
321 if a is None:
322 return None
323 if __py_json_numpy_ok(a):
324
325 return numpy.ravel(a).tolist()
326 # cp_uint64 cp_c_f32 cp_c_f64 cp_c_f80 cp_c_f128
327 if a.dtype == numpy.dtype('uint64'):
328 a_copy = a.astype(numpy.str_)
329 return numpy.ravel(a_copy).tolist()
330 if a.dtype == numpy.dtype('complex64'):
331 if a.flags['F_CONTIGUOUS']:
332 r_view = forsplitcomplex(a)
333 return r_view.tolist()
334 r_view = csplitcomplex(a)
335 return r_view.tolist()
336 if a.dtype == numpy.dtype('complex128'):
337 if a.flags['F_CONTIGUOUS']:
338 r_view = forsplitcomplex(a)
339 return r_view.tolist()
340 r_view = csplitcomplex(a)
341 return r_view.tolist()
342 if a.dtype == numpy.dtype('complex256'):
343 if a.flags['F_CONTIGUOUS']:
344 r_view = forsplitcomplex(a)
345 a_copy = r_view.astype(numpy.str_)
346 return a_copy.tolist()
347 r_view = csplitcomplex(a)
348 a_copy = r_view.astype(numpy.str_)
349 return a_copy.tolist()
350 return ["NOT_YET_SUPPORTED(numpy array):"+a.dtype.name]
351

◆ is_string_type()

bool adctk.adc_types.is_string_type ( ScalarType  st)

return True if st is of the single string subclassifications

86def is_string_type(st: ScalarType) -> bool:
87 """! return True if st is of the single string subclassifications"""
88 if st in ( ScalarType.cp_cstr,
89 ScalarType.cp_json_str,
90 ScalarType.cp_yaml_str,
91 ScalarType.cp_xml_str,
92 ScalarType.cp_path):
93 return True
94 return False
95

◆ scalar_from_json()

adctk.adc_types.scalar_from_json ( str  name,
  strict = False 
)

convert json type string used in add functions to ScalarType

Parameters
nameconvert json scalar type label to enum.
strictif True, raises an exception when name is invalid array_* type prefix is stripped, if present in name.
104def scalar_from_json(name: str, strict=False):
105 """! convert json type string used in add functions to ScalarType
106
107 @param name convert json scalar type label to enum.
108 @param strict if True, raises an exception when name is invalid
109 array_* type prefix is stripped, if present in name.
110 """
111 if name.startswith("array_"):
112 name = name[6:]
113 if strict:
114 return ScalarType.__members__["cp_"+name]
115 try:
116 x = ScalarType.__members__["cp_"+name]
117 except:
118 x = ScalarType.cp_none
119
120 return x
121
122# mapping of numpy types to adc type.
123# notes:
124# - numpy 2.3 and older do not support precision higher than c long double (64 or 80bit)
125# - currently this assumes an x86_64 or similar environment for int sizing (32 bit
126# platforms/build need to be detected and supported)
127# - datetime64 and deltatime from numpy are not supported yet. fixme

◆ scalar_from_numpy()

adctk.adc_types.scalar_from_numpy ( numpy.dtype  dt)

convert numpy dtype to adc scalar type

  • numpy 2.3 and older do not support precision higher than c long double (64 or 80bit)
  • currently this assumes an x86_64 or similar environment for int sizing
  • 32 bit platforms/builds may need to be detected and supported, if they still exist
208def scalar_from_numpy(dt: numpy.dtype):
209 """! convert numpy dtype to adc scalar type
210 - numpy 2.3 and older do not support precision higher than c long double (64 or 80bit)
211 - currently this assumes an x86_64 or similar environment for int sizing
212 - 32 bit platforms/builds may need to be detected and supported, if they still exist
213 """
214 __scalar_from_numpy_conditional()
215 if dt in __scalar_from_numpy:
216 return __scalar_from_numpy[dt]
217
220 raise ValueError("unknown numpy type mapping")
221

◆ scalar_to_json()

str adctk.adc_types.scalar_to_json ( ScalarType  st,
  array_type = False 
)
Convert ScalarType to json type string used in add functions
96def scalar_to_json(st: ScalarType, array_type=False) -> str:
97 """Convert ScalarType to json type string used in add functions"""
98 r = st.name[3:]
99 if array_type:
100 r = f"array_{r}"
101
102 return r
103

◆ type_to_scalar()

adctk.adc_types.type_to_scalar ( type  t,
  value = None 
)

convert python type to ScalarType numpy array types are supported if value is given.

int type is supported only if a value is given. Semantic variations on string content are all mapped to cp_cstr.

430def type_to_scalar(t: type, value=None):
431 """! @brief convert python type to ScalarType
432 numpy array types are supported if value is given.
433 int type is supported only if a value is given.
434 Semantic variations on string content are all mapped to cp_cstr.
435 """
436 if t is None:
437 return ScalarType.cp_none
438 if t == int:
439 if value is None:
440 raise ValueError("type_to_scalar(int) needs value argument")
441 if fits_int64(value):
442 return ScalarType.cp_int64
443 return ScalarType.cp_number_str
444 if t == str:
445 return ScalarType.cp_cstr
446 if t == bool:
447
448 return ScalarType.cp_bool
449 if t == float:
450 return ScalarType.cp_f64
451 if t == complex:
452 return ScalarType.cp_c_f64
453 if t == decimal.Decimal:
454 return ScalarType.cp_number_str
455 if t in (list, tuple, collections.abc.Set):
456 if not value:
457 raise ValueError("type_to_scalar needs value argument for list/set/tuple")
458 md = [0]
459 return get_common_scalar(value, 0, md )
460 if t == numpy.ndarray and not value:
461 raise ValueError("type_to_scalar needs value argument")
462 if value is not None and type(value).__module__ == numpy.__name__:
463 if isinstance(value, numpy.ndarray):
464 return scalar_from_numpy(value.dtype)
465 # print(value.shape)
466 # print(value.itemsize)
467 # print(value.dtype)
468
469 match value:
470 case numpy.float64() | numpy.float32() | numpy.float16() | \
471 numpy.int64() | numpy.int32() | numpy.int16() | numpy.int8() | \
472 numpy.uint32() | numpy.uint16() | numpy.uint8() |numpy.bool() | \
473 numpy.str_() | numpy.bytes_() | \
474 numpy.uint64() | \
475 numpy.complex64() | numpy.complex128() | \
476 numpy.complex192() | numpy.complex256():
477 return scalar_from_numpy(value.dtype)
478 return ScalarType.cp_none
479
480# pylint: too-few-public-methods

◆ typecode_to_scalar()

adctk.adc_types.typecode_to_scalar ( array.array  v)
375def typecode_to_scalar(v: array.array):
376 tc = v.typecode
377 match tc:
378 case 'b':
379 return ScalarType.cp_char
380 case 'B':
381 return ScalarType.cp_char8
382 case 'u':
383 if v.itemsize == 4:
384 return ScalarType.cp_char32
385 return ScalarType.cp_char16
386 case 'w':
387 return ScalarType.cp_char32
388 case 'h':
389 if v.itemsize == 2:
390 return ScalarType.cp_int16
391 return ScalarType.cp_int32
392 case 'H':
393 if v.itemsize == 2:
394 return ScalarType.cp_uint16
395 return ScalarType.cp_uint32
396 case 'i':
397 if v.itemsize == 2:
398 return ScalarType.cp_int16
399 return ScalarType.cp_int32
400 case 'I':
401 if v.itemsize == 2:
402 return ScalarType.cp_uint16
403 return ScalarType.cp_uint32
404 case 'l':
405 if v.itemsize == 4:
406 return ScalarType.cp_int32
407 return ScalarType.cp_int64
408 case 'L':
409 if v.itemsize == 4:
410 return ScalarType.cp_uint32
411 return ScalarType.cp_uint64
412 case 'q':
413 if v.itemsize == 8:
414 return ScalarType.cp_int64
415 return ScalarType.cp_number_str
416 case 'Q':
417 if v.itemsize == 8:
418 return ScalarType.cp_uint64
419 return ScalarType.cp_number_str
420 case 'f':
421 if v.itemsize == 4:
422 return ScalarType.cp_f32
423 return ScalarType.cp_f64
424 case 'd':
425 if v.itemsize == 8:
426 return ScalarType.cp_f64
427 return ScalarType.cp_number_str
428
429# pylint: disable-next=too-many-return-statements,too-many-branches

Variable Documentation

◆ ADC_SUPPORT_EXTENDED_FLOATS

int adctk.adc_types.ADC_SUPPORT_EXTENDED_FLOATS = 1

Set to 1 if 80 bit floats for cpus are supported.

ADC_SUPPORT_EXTENDED_FLOATS should be defined by build-time configuration

◆ ADC_SUPPORT_GPU_FLOATS

int adctk.adc_types.ADC_SUPPORT_GPU_FLOATS = 0

Set to 1 if 8/16 bit float types for gpus are supported.

ADC_SUPPORT_GPU_FLOATS should be defined by build-time configuration

◆ ADC_SUPPORT_QUAD_FLOATS

int adctk.adc_types.ADC_SUPPORT_QUAD_FLOATS = 0

Set to 1 if 128 bit floats for cpus are supported.

ADC_SUPPORT_QUAD_FLOATS should be defined by build-time configuration