algos_common_helper.pxi.in 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """
  2. Template for each `dtype` helper function using 1-d template
  3. WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
  4. """
  5. # ----------------------------------------------------------------------
  6. # ensure_dtype
  7. # ----------------------------------------------------------------------
  8. def ensure_platform_int(object arr):
  9. # GH3033, GH1392
  10. # platform int is the size of the int pointer, e.g. np.intp
  11. if util.is_array(arr):
  12. if (<ndarray>arr).descr.type_num == cnp.NPY_INTP:
  13. return arr
  14. else:
  15. # equiv: arr.astype(np.intp)
  16. return cnp.PyArray_Cast(<ndarray>arr, cnp.NPY_INTP)
  17. else:
  18. return np.array(arr, dtype=np.intp)
  19. def ensure_object(object arr):
  20. if util.is_array(arr):
  21. if (<ndarray>arr).descr.type_num == NPY_OBJECT:
  22. return arr
  23. else:
  24. # equiv: arr.astype(object)
  25. return cnp.PyArray_Cast(<ndarray>arr, NPY_OBJECT)
  26. else:
  27. return np.array(arr, dtype=np.object_)
  28. {{py:
  29. # name, c_type, dtype
  30. dtypes = [('float64', 'FLOAT64', 'float64'),
  31. # ('float32', 'FLOAT32', 'float32'), # disabling bc unused
  32. ('int8', 'INT8', 'int8'),
  33. ('int16', 'INT16', 'int16'),
  34. ('int32', 'INT32', 'int32'),
  35. ('int64', 'INT64', 'int64'),
  36. ('uint64', 'UINT64', 'uint64'),
  37. # Disabling uint and complex dtypes because we do not use them
  38. # (and compiling them increases wheel size) (except uint64)
  39. # ('uint8', 'UINT8', 'uint8'),
  40. # ('uint16', 'UINT16', 'uint16'),
  41. # ('uint32', 'UINT32', 'uint32'),
  42. # ('complex64', 'COMPLEX64', 'complex64'),
  43. # ('complex128', 'COMPLEX128', 'complex128')
  44. ]
  45. def get_dispatch(dtypes):
  46. for name, c_type, dtype in dtypes:
  47. yield name, c_type, dtype
  48. }}
  49. {{for name, c_type, dtype in get_dispatch(dtypes)}}
  50. def ensure_{{name}}(object arr):
  51. if util.is_array(arr):
  52. if (<ndarray>arr).descr.type_num == NPY_{{c_type}}:
  53. return arr
  54. else:
  55. # equiv: arr.astype(np.{{dtype}})
  56. return cnp.PyArray_Cast(<ndarray>arr, cnp.NPY_{{c_type}})
  57. else:
  58. return np.asarray(arr, dtype=np.{{dtype}})
  59. {{endfor}}