_utils.pxd 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Authors: Gilles Louppe <g.louppe@gmail.com>
  2. # Peter Prettenhofer <peter.prettenhofer@gmail.com>
  3. # Arnaud Joly <arnaud.v.joly@gmail.com>
  4. # Jacob Schreiber <jmschreiber91@gmail.com>
  5. # Nelson Liu <nelson@nelsonliu.me>
  6. #
  7. # License: BSD 3 clause
  8. # See _utils.pyx for details.
  9. cimport numpy as cnp
  10. from ._tree cimport Node
  11. from ..neighbors._quad_tree cimport Cell
  12. ctypedef cnp.npy_float32 DTYPE_t # Type of X
  13. ctypedef cnp.npy_float64 DOUBLE_t # Type of y, sample_weight
  14. ctypedef cnp.npy_intp SIZE_t # Type for indices and counters
  15. ctypedef cnp.npy_int32 INT32_t # Signed 32 bit integer
  16. ctypedef cnp.npy_uint32 UINT32_t # Unsigned 32 bit integer
  17. cdef enum:
  18. # Max value for our rand_r replacement (near the bottom).
  19. # We don't use RAND_MAX because it's different across platforms and
  20. # particularly tiny on Windows/MSVC.
  21. # It corresponds to the maximum representable value for
  22. # 32-bit signed integers (i.e. 2^31 - 1).
  23. RAND_R_MAX = 2147483647
  24. # safe_realloc(&p, n) resizes the allocation of p to n * sizeof(*p) bytes or
  25. # raises a MemoryError. It never calls free, since that's __dealloc__'s job.
  26. # cdef DTYPE_t *p = NULL
  27. # safe_realloc(&p, n)
  28. # is equivalent to p = malloc(n * sizeof(*p)) with error checking.
  29. ctypedef fused realloc_ptr:
  30. # Add pointer types here as needed.
  31. (DTYPE_t*)
  32. (SIZE_t*)
  33. (unsigned char*)
  34. (WeightedPQueueRecord*)
  35. (DOUBLE_t*)
  36. (DOUBLE_t**)
  37. (Node*)
  38. (Cell*)
  39. (Node**)
  40. cdef realloc_ptr safe_realloc(realloc_ptr* p, size_t nelems) except * nogil
  41. cdef cnp.ndarray sizet_ptr_to_ndarray(SIZE_t* data, SIZE_t size)
  42. cdef SIZE_t rand_int(SIZE_t low, SIZE_t high,
  43. UINT32_t* random_state) noexcept nogil
  44. cdef double rand_uniform(double low, double high,
  45. UINT32_t* random_state) noexcept nogil
  46. cdef double log(double x) noexcept nogil
  47. # =============================================================================
  48. # WeightedPQueue data structure
  49. # =============================================================================
  50. # A record stored in the WeightedPQueue
  51. cdef struct WeightedPQueueRecord:
  52. DOUBLE_t data
  53. DOUBLE_t weight
  54. cdef class WeightedPQueue:
  55. cdef SIZE_t capacity
  56. cdef SIZE_t array_ptr
  57. cdef WeightedPQueueRecord* array_
  58. cdef bint is_empty(self) noexcept nogil
  59. cdef int reset(self) except -1 nogil
  60. cdef SIZE_t size(self) noexcept nogil
  61. cdef int push(self, DOUBLE_t data, DOUBLE_t weight) except -1 nogil
  62. cdef int remove(self, DOUBLE_t data, DOUBLE_t weight) noexcept nogil
  63. cdef int pop(self, DOUBLE_t* data, DOUBLE_t* weight) noexcept nogil
  64. cdef int peek(self, DOUBLE_t* data, DOUBLE_t* weight) noexcept nogil
  65. cdef DOUBLE_t get_weight_from_index(self, SIZE_t index) noexcept nogil
  66. cdef DOUBLE_t get_value_from_index(self, SIZE_t index) noexcept nogil
  67. # =============================================================================
  68. # WeightedMedianCalculator data structure
  69. # =============================================================================
  70. cdef class WeightedMedianCalculator:
  71. cdef SIZE_t initial_capacity
  72. cdef WeightedPQueue samples
  73. cdef DOUBLE_t total_weight
  74. cdef SIZE_t k
  75. cdef DOUBLE_t sum_w_0_k # represents sum(weights[0:k]) = w[0] + w[1] + ... + w[k-1]
  76. cdef SIZE_t size(self) noexcept nogil
  77. cdef int push(self, DOUBLE_t data, DOUBLE_t weight) except -1 nogil
  78. cdef int reset(self) except -1 nogil
  79. cdef int update_median_parameters_post_push(
  80. self, DOUBLE_t data, DOUBLE_t weight,
  81. DOUBLE_t original_median) noexcept nogil
  82. cdef int remove(self, DOUBLE_t data, DOUBLE_t weight) noexcept nogil
  83. cdef int pop(self, DOUBLE_t* data, DOUBLE_t* weight) noexcept nogil
  84. cdef int update_median_parameters_post_remove(
  85. self, DOUBLE_t data, DOUBLE_t weight,
  86. DOUBLE_t original_median) noexcept nogil
  87. cdef DOUBLE_t get_median(self) noexcept nogil