Skip to content

Commit

Permalink
Performance improvements to ndfilters.trimmed_mean_filter(). (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
byrdie authored Aug 7, 2024
1 parent 47d6142 commit c6185e0
Showing 1 changed file with 9 additions and 28 deletions.
37 changes: 9 additions & 28 deletions ndfilters/_trimmed_mean.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,12 @@ def _trimmed_mean(

(proportion,) = args

kernel_size = array.size

lowercut = int(proportion * kernel_size)
uppercut = kernel_size - lowercut

for i in range(lowercut):
j_min = i
for j in range(i + 1, kernel_size):
if array[j] < array[j_min]:
j_min = j
if j_min != i:
array[i], array[j_min] = array[j_min], array[i]

for i in range(uppercut + 1):
j_max = i
for j in range(i + 1, kernel_size - lowercut):
if array[~j] > array[~j_max]:
j_max = j
if j_max != i:
array[~i], array[~j_max] = array[~j_max], array[~i]

sum_values = 0
num_values = 0
for i in range(lowercut, uppercut):
sum_values += array[i]
num_values += 1

return sum_values / num_values
nobs = array.size
lowercut = int(proportion * nobs)
uppercut = nobs - lowercut
if lowercut > uppercut: # pragma: nocover
raise ValueError("Proportion too big.")

array = np.partition(array, (lowercut, uppercut - 1))

return np.mean(array[lowercut:uppercut])

0 comments on commit c6185e0

Please sign in to comment.