-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcython_util.pyx
209 lines (174 loc) · 6.44 KB
/
cython_util.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import numpy as np
def create_counts_mins_cy(long nbin_0, long nbin_1, float[:, :] Points,
long[:] xy, float init_val):
"""
Given a bin point cloud, return the binwise number of items and min z val.
Parameters
----------
nbin_0 : long
Number of bins along the zeroth axis
nbin_1 : long
Number of bins along the first axis
Points : float[:, :]
Pointcloud, Nx3 array of type np.float32
xy : long[:]
Bin index for each point, must be same as numbe rof points.
init_val : float
Initial values in mins array. Pick something larger than largest z
in Points. In mins output, all bins that don't have any points in them
will take on this value.
Returns:
--------
counts : long[:]
Array with the counts for each bin. Length nbin_0*nbin_1
mins : float[:]
Array with the min z value for each bin. Length nbin_0*nbin_1
"""
counts = np.zeros(nbin_0 * nbin_1, dtype=np.int64)
cdef long[:] counts_view = counts
mins = init_val * np.ones(nbin_0 * nbin_1, dtype=np.float32)
cdef float[:] mins_view = mins
for i in range(len(xy)):
counts_view[xy[i]] += 1
if mins_view[xy[i]] > Points[i, 2]:
mins_view[xy[i]] = Points[i, 2]
return counts, mins
def create_counts_sums_cy(long nbin_0, long nbin_1, float[:, :] Points,
long[:] xy):
"""
Return the binwise number of points and sum of the z values
Parameters
----------
nbin_0 : long
Number of bins along the zeroth axis
nbin_1 : long
Number of bins along the first axis
Points : float[:, :]
Pointcloud, Nx3 array of type np.float32
xy : long[:]
Bin index for each point, must be same as numbe rof points.
Returns:
--------
counts : long[:]
Array with the counts for each bin. Length nbin_0*nbin_1
sums : float[:]
Array with the sum of z values for each bin. Length nbin_0*nbin_1
"""
counts = np.zeros(nbin_0 * nbin_1, dtype=np.int64)
cdef long[:] counts_view = counts
sums = np.zeros(nbin_0 * nbin_1, dtype=np.float32)
cdef float[:] sums_view = sums
for i in range(len(xy)):
counts_view[xy[i]] += 1
sums_view[xy[i]] += Points[i, 2]
return counts, sums
def create_counts_means_M2_cy(long nbin_0, long nbin_1, float[:, :] Points,
long[:] xy):
"""
Return the binwise number of points, mean, and M2 estimate of the sum of
squared deviations (using Welford's algorithm)
Parameters
----------
nbin_0 : long
Number of bins along the zeroth axis
nbin_1 : long
Number of bins along the first axis
Points : float[:, :]
Pointcloud, Nx3 array of type np.float32
xy : long[:]
Bin index for each point, must be same as numbe rof points.
Returns:
--------
counts : float[:]
Array with the counts for each bin. Length nbin_0*nbin_1
means : float[:]
Array with the mean of z values for each bin. Length nbin_0*nbin_1
m2s : float[:]
Array with the M2 estimate of the sum of squared deviations
Length nbin_0*nbin_1
"""
# Chose to make this float for division purposes but could have unexpected
# results, check if so
counts = np.zeros(nbin_0 * nbin_1, dtype=np.float32)
cdef float[:] counts_view = counts
means = np.zeros(nbin_0 * nbin_1, dtype=np.float32)
cdef float[:] means_view = means
m2s = np.zeros(nbin_0 * nbin_1, dtype=np.float32)
cdef float[:] m2s_view = m2s
cdef float delta
cdef float delta2
for i in range(len(xy)):
counts_view[xy[i]] += 1
delta = Points[i, 2] - means_view[xy[i]]
means_view[xy[i]] += delta / counts_view[xy[i]]
delta2 = Points[i, 2] - means_view[xy[i]]
m2s += delta*delta2
return counts, means, m2s
def create_counts_hists_cy(long nbin_0, long nbin_1, long[:] h_ind, long[:] xy,
long nbin_h):
"""
Return the binwise number of points and sum of the z values
Parameters
----------
nbin_0 : long
Number of bins along the zeroth axis
nbin_1 : long
Number of bins along the first axis
h_ind : long[:]
Z value histogram index for each point. Smallest value must be >=0 and
largest value must be <= nbin_h-1.
xy : long[:]
Bin index for each point, must be same as number of points.
nbin_h : long
The number of z histogram bins there are.
Returns:
--------
counts : long[:]
Array with the counts for each bin. Length nbin_0*nbin_1
hists : long[:, :]
Array with the historam of z values for each bin. Zeroth dim
Length nbin_0*nbin_1. 1st dim length nbin_h
"""
counts = np.zeros(nbin_0 * nbin_1, dtype=np.int64)
cdef long[:] counts_view = counts
hists = np.zeros((nbin_0 * nbin_1, nbin_h), dtype=np.int64)
cdef long[:, :] hists_view = hists
for i in range(len(xy)):
counts_view[xy[i]] += 1
hists_view[xy[i], h_ind[i]] += 1
return counts, hists
def binwise_max_cy(long nbin_0, long nbin_1, float[:, :] Points, long[:] xy,
float init_val):
"""
Given a bin point cloud, return the binwise number of items and min z val.
Parameters
----------
nbin_0 : long
Number of bins along the zeroth axis
nbin_1 : long
Number of bins along the first axis
Points : float[:, :]
Pointcloud, Nx3 array of type np.float32
xy : long[:]
Bin index for each point, must be same as number of points.
init_val : float
Initial values in maxs array. Pick something smaller than smallest z
in Points.
Returns:
--------
inds : intptr_t[:]
Array with the index of the maximum point for each bin.
Length nbin_0*nbin_1
mins : float[:]
Array with the min z value for each bin. Length nbin_0*nbin_1
"""
# Initialize inds to a value 1 greater than largest point index
inds = len(xy) * np.ones(nbin_0 * nbin_1, dtype=np.int64)
cdef long[:] inds_view = inds
maxs = init_val * np.ones(nbin_0 * nbin_1, dtype=np.float32)
cdef float[:] maxs_view = maxs
for i in range(len(xy)):
if maxs_view[xy[i]] < Points[i, 2]:
maxs_view[xy[i]] = Points[i, 2]
inds_view[xy[i]] = i
return inds