Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements to ndfilters.trimmed_mean_filter(). #16

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
Loading