Skip to content

Commit 61872c7

Browse files
Copilotalexlib
andcommitted
Optimize array operations to reduce copies and use vectorization
Co-authored-by: alexlib <747110+alexlib@users.noreply.github.com>
1 parent 8675a97 commit 61872c7

3 files changed

Lines changed: 22 additions & 11 deletions

File tree

openpiv/filters.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,12 @@ def replace_outliers(
174174
# regardless the grid_mask (which is a user-provided masked region)
175175

176176

177-
if not isinstance(u, np.ma.MaskedArray):
177+
# Only create masked array if needed
178+
if isinstance(u, np.ma.MaskedArray):
179+
grid_mask = u.mask.copy()
180+
else:
178181
u = np.ma.masked_array(u, mask=np.ma.nomask)
179-
180-
# store grid_mask for reinforcement
181-
grid_mask = u.mask.copy()
182+
grid_mask = np.ma.nomask
182183

183184
u[flags] = np.nan
184185
v[flags] = np.nan

openpiv/pyprocess.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,11 @@ def find_all_first_peaks(corr):
335335
ind = corr.reshape(corr.shape[0], -1).argmax(-1)
336336
peaks = np.array(np.unravel_index(ind, corr.shape[-2:]))
337337
peaks = np.vstack((peaks[0], peaks[1])).T
338-
index_list = [(i, v[0], v[1]) for i, v in enumerate(peaks)]
338+
# Vectorized index list creation instead of list comprehension
339+
n = peaks.shape[0]
340+
index_list = np.column_stack((np.arange(n), peaks))
339341
peaks_max = np.nanmax(corr, axis = (-2, -1))
340-
return np.array(index_list), np.array(peaks_max)
342+
return index_list, peaks_max
341343

342344

343345
def find_all_second_peaks(corr, width = 2):
@@ -766,7 +768,12 @@ def normalize_intensity(window):
766768
intensity normalized to -1 +1 and clipped if some pixels are
767769
extra low/high
768770
"""
769-
window = window.astype(np.float32)
771+
# Convert to float32 only if needed, otherwise work in-place
772+
if window.dtype != np.float32:
773+
window = window.astype(np.float32)
774+
else:
775+
window = window.copy() # Still need a copy to avoid modifying input
776+
770777
window -= window.mean(axis=(-2, -1),
771778
keepdims=True, dtype=np.float32)
772779
tmp = window.std(axis=(-2, -1), keepdims=True)

openpiv/validation.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,13 @@ def global_std(
110110
# def reject_outliers(data, m=2):
111111
# return data[abs(data - np.mean(data)) < m * np.std(data)]
112112

113-
# create nan filled arrays where masks
114-
# if u,v, are non-masked, ma.copy() adds false masks
115-
tmpu = np.ma.copy(u).filled(np.nan)
116-
tmpv = np.ma.copy(v).filled(np.nan)
113+
# Avoid unnecessary copy operations - work with masked arrays directly
114+
if np.ma.is_masked(u):
115+
tmpu = np.where(u.mask, np.nan, u.data)
116+
tmpv = np.where(v.mask, np.nan, v.data)
117+
else:
118+
tmpu = u
119+
tmpv = v
117120

118121
ind = np.logical_or(np.abs(tmpu - np.nanmean(tmpu)) > std_threshold * np.nanstd(tmpu),
119122
np.abs(tmpv - np.nanmean(tmpv)) > std_threshold * np.nanstd(tmpv))

0 commit comments

Comments
 (0)