Skip to content

Commit 8f5e747

Browse files
Copilotalexlib
andcommitted
Further optimize array operations in validation and pyprocess modules
Co-authored-by: alexlib <747110+alexlib@users.noreply.github.com>
1 parent 61872c7 commit 8f5e747

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

openpiv/pyprocess.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -365,18 +365,19 @@ def find_all_second_peaks(corr, width = 2):
365365
ind = indexes[:, 0]
366366
x = indexes[:, 1]
367367
y = indexes[:, 2]
368-
iini = x - width
369-
ifin = x + width + 1
370-
jini = y - width
371-
jfin = y + width + 1
372-
iini[iini < 0] = 0 # border checking
373-
ifin[ifin > corr.shape[1]] = corr.shape[1]
374-
jini[jini < 0] = 0
375-
jfin[jfin > corr.shape[2]] = corr.shape[2]
376-
# create a masked view of the corr
377-
tmp = corr.view(np.ma.MaskedArray)
368+
iini = np.maximum(x - width, 0)
369+
ifin = np.minimum(x + width + 1, corr.shape[1])
370+
jini = np.maximum(y - width, 0)
371+
jfin = np.minimum(y + width + 1, corr.shape[2])
372+
373+
# Create a masked view of the corr - vectorized masking
374+
tmp = corr.copy() # Need copy to avoid modifying input
375+
# Create mask for each window efficiently
378376
for i in ind:
379-
tmp[i, iini[i]:ifin[i], jini[i]:jfin[i]] = np.ma.masked
377+
tmp[i, iini[i]:ifin[i], jini[i]:jfin[i]] = np.nan
378+
379+
# Convert to masked array where nans are masked
380+
tmp = np.ma.masked_invalid(tmp)
380381
indexes, peaks = find_all_first_peaks(tmp)
381382
return indexes, peaks
382383

openpiv/validation.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,10 @@ def local_median_val(
226226
# f = np.ones((2*size+1, 2*size+1))
227227
# f[size,size] = 0
228228

229+
# Convert to regular array with nans for masked values - avoid extra copies
229230
if np.ma.is_masked(u):
230-
masked_u = np.where(~u.mask, u.data, np.nan)
231-
masked_v = np.where(~v.mask, v.data, np.nan)
231+
masked_u = np.where(u.mask, np.nan, u.data)
232+
masked_v = np.where(v.mask, np.nan, v.data)
232233
else:
233234
masked_u = u
234235
masked_v = v
@@ -300,8 +301,8 @@ def local_norm_median_val(
300301
301302
"""
302303
if np.ma.is_masked(u):
303-
masked_u = np.where(~u.mask, u.data, np.nan)
304-
masked_v = np.where(~v.mask, v.data, np.nan)
304+
masked_u = np.where(u.mask, np.nan, u.data)
305+
masked_v = np.where(v.mask, np.nan, v.data)
305306
else:
306307
masked_u = u
307308
masked_v = v

0 commit comments

Comments
 (0)