|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 |
|
3 |
| -cimport cython |
4 |
| -from cython cimport Py_ssize_t |
| 3 | +import cython |
| 4 | +from cython import Py_ssize_t |
5 | 5 |
|
6 |
| -import numpy as np |
7 |
| -from numpy cimport (ndarray, |
8 |
| - int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, |
| 6 | +from numpy cimport (int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, |
9 | 7 | uint32_t, uint64_t, float32_t, float64_t)
|
10 | 8 |
|
11 | 9 |
|
12 |
| -cdef double NaN = <double> np.NaN |
13 |
| -cdef double nan = NaN |
| 10 | +ctypedef fused reshape_t: |
| 11 | + uint8_t |
| 12 | + uint16_t |
| 13 | + uint32_t |
| 14 | + uint64_t |
| 15 | + int8_t |
| 16 | + int16_t |
| 17 | + int32_t |
| 18 | + int64_t |
| 19 | + float32_t |
| 20 | + float64_t |
| 21 | + object |
14 | 22 |
|
15 |
| -include "reshape_helper.pxi" |
| 23 | + |
| 24 | +@cython.wraparound(False) |
| 25 | +@cython.boundscheck(False) |
| 26 | +def unstack(reshape_t[:, :] values, uint8_t[:] mask, |
| 27 | + Py_ssize_t stride, Py_ssize_t length, Py_ssize_t width, |
| 28 | + reshape_t[:, :] new_values, uint8_t[:, :] new_mask): |
| 29 | + """ |
| 30 | + transform long sorted_values to wide new_values |
| 31 | +
|
| 32 | + Parameters |
| 33 | + ---------- |
| 34 | + values : typed ndarray |
| 35 | + mask : boolean ndarray |
| 36 | + stride : int |
| 37 | + length : int |
| 38 | + width : int |
| 39 | + new_values : typed ndarray |
| 40 | + result array |
| 41 | + new_mask : boolean ndarray |
| 42 | + result mask |
| 43 | + """ |
| 44 | + cdef: |
| 45 | + Py_ssize_t i, j, w, nulls, s, offset |
| 46 | + |
| 47 | + if reshape_t is not object: |
| 48 | + # evaluated at compile-time |
| 49 | + with nogil: |
| 50 | + for i in range(stride): |
| 51 | + |
| 52 | + nulls = 0 |
| 53 | + for j in range(length): |
| 54 | + |
| 55 | + for w in range(width): |
| 56 | + |
| 57 | + offset = j * width + w |
| 58 | + |
| 59 | + if mask[offset]: |
| 60 | + s = i * width + w |
| 61 | + new_values[j, s] = values[offset - nulls, i] |
| 62 | + new_mask[j, s] = 1 |
| 63 | + else: |
| 64 | + nulls += 1 |
| 65 | + |
| 66 | + else: |
| 67 | + # object-dtype, identical to above but we cannot use nogil |
| 68 | + for i in range(stride): |
| 69 | + |
| 70 | + nulls = 0 |
| 71 | + for j in range(length): |
| 72 | + |
| 73 | + for w in range(width): |
| 74 | + |
| 75 | + offset = j * width + w |
| 76 | + |
| 77 | + if mask[offset]: |
| 78 | + s = i * width + w |
| 79 | + new_values[j, s] = values[offset - nulls, i] |
| 80 | + new_mask[j, s] = 1 |
| 81 | + else: |
| 82 | + nulls += 1 |
| 83 | + |
| 84 | + |
| 85 | +unstack_uint8 = unstack["uint8_t"] |
| 86 | +unstack_uint16 = unstack["uint16_t"] |
| 87 | +unstack_uint32 = unstack["uint32_t"] |
| 88 | +unstack_uint64 = unstack["uint64_t"] |
| 89 | +unstack_int8 = unstack["int8_t"] |
| 90 | +unstack_int16 = unstack["int16_t"] |
| 91 | +unstack_int32 = unstack["int32_t"] |
| 92 | +unstack_int64 = unstack["int64_t"] |
| 93 | +unstack_float32 = unstack["float32_t"] |
| 94 | +unstack_float64 = unstack["float64_t"] |
| 95 | +unstack_object = unstack["object"] |
0 commit comments