Not the fastest method, but you can generate a robust solution using scipy
for n-dimensional arrays and n-dimensional patterns.
import scipy
from scipy.ndimage import label
#=================
# Helper functions
#=================
# Nested list to nested tuple helper function
# from https://stackoverflow.com/questions/27049998/convert-a-mixed-nested-list-to-a-nested-tuple
def to_tuple(L):
return tuple(to_tuple(i) if isinstance(i, list) else i for i in L)
# Helper function to convert array to set of tuples
def arr2set(arr):
return set(to_tuple(arr.tolist()))
#===============
# Main algorithm
#===============
# First pass: filter for exact matches
a1 = scipy.zeros_like(a, dtype=bool)
freq_dict = {}
notnan = ~scipy.isnan(pattern)
for i in scipy.unique(pattern[notnan]):
a1 = a1 + (a == i)
freq_dict[i] = (pattern == i).sum()
# Minimise amount of pattern checking by choosing least frequent occurrence
check_val = freq_dict.keys()[scipy.argmin(freq_dict.values())]
# Get set of indices of pattern
pattern_inds = scipy.transpose(scipy.nonzero(scipy.ones_like(pattern)*notnan))
check_ind = scipy.transpose(scipy.nonzero(pattern == check_val))[0]
pattern_inds = pattern_inds - check_ind
pattern_inds_set = arr2set(pattern_inds)
# Label different regions found in first pass which may contains pattern
label_arr, n = label(a1)
found_inds_list = []
pattern_size = len(pattern_inds)
for i in range(1, n+1):
arr_inds = scipy.transpose(scipy.nonzero(label_arr == i))
bbox_inds = [ind for ind in arr_inds if a[tuple(ind)] == check_val]
for ind in bbox_inds:
check_inds_set = arr2set(arr_inds - ind)
if len(pattern_inds_set - check_inds_set) == 0:
found_inds_list.append(tuple(scipy.transpose(pattern_inds + ind)))
# Replace values
for inds in found_inds_list:
a[inds] = replace_value
Generate a random test array, pattern, and final replace value for a 4D case
replace_value = scipy.random.rand() # Final value that you want to replace everything with
nan = scipy.nan # Use this for places in the rectangular pattern array that you don't care about checking
# Generate random data
a = scipy.random.random([12,12,12,12])*12
pattern = scipy.random.random([3,3,3,3])*12
# Put the pattern in random places
for i in range(4):
j1, j2, j3, j4 = scipy.random.choice(xrange(10), 4, replace=True)
a[j1:j1+3, j2:j2+3, j3:j3+3, j4:j4+3] = pattern
a_org = scipy.copy(a)
# Randomly insert nans in the pattern
for i in range(20):
j1, j2, j3, j4 = scipy.random.choice(xrange(3), 4, replace=True)
pattern[j1, j2, j3, j4] = nan
After running the main algorithm...
>>> print found_inds_list[-1]
(array([ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11], dtype=int64), array([1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 1, 1,
1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1,
1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3],
dtype=int64), array([5, 5, 5, 6, 6, 6, 7, 7, 5, 5, 6, 6, 7, 7, 7, 5, 5, 6, 6, 7, 5, 5,
5, 6, 6, 7, 7, 5, 5, 6, 7, 7, 7, 5, 5, 5, 6, 6, 6, 7, 7, 7, 5, 5,
5, 6, 6, 7, 5, 5, 5, 6, 6, 6, 7, 5, 5, 6, 6, 6, 7, 7, 7],
dtype=int64), array([1, 2, 3, 1, 2, 3, 1, 3, 1, 3, 1, 3, 1, 2, 3, 2, 3, 1, 2, 2, 1, 2,
3, 1, 2, 1, 3, 1, 2, 2, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2,
3, 1, 3, 1, 1, 2, 3, 1, 2, 3, 2, 1, 3, 1, 2, 3, 1, 2, 3],
dtype=int64))
>>>
>>> replace_value # Display value that's going to be replaced
0.9263912485289564
>>>
>>> print a_org[9:12, 1:4, 5:8, 1:4] # Display original rectangular window of replacement
[[[[ 9.68507479 1.77585089 5.06069382]
[10.63768984 11.41148096 1.13120712]
[ 6.83684611 2.46838238 11.40490158]]
[[ 9.17344668 11.21669704 7.60737639]
[ 3.14870787 6.22857282 5.61295454]
[ 4.32709261 8.00493326 9.96124294]]
[[ 4.16785078 10.66054365 2.95677408]
[11.53789218 2.70725911 11.98647139]
[ 5.00346525 4.75230895 4.05213149]]]
[[[11.23856096 8.45979355 7.53268864]
[ 6.14703327 11.90052117 5.48127994]
[ 2.16777734 10.27373562 7.75420214]]
[[10.04726853 11.44895046 7.78071007]
[ 0.79030038 3.69735083 1.51921116]
[11.29782542 2.58494314 9.8714708 ]]
[[ 7.9356587 1.48053473 9.71362122]
[ 5.11866341 3.43895455 6.86491947]
[ 8.33774813 5.66923131 2.27884056]]]
[[[ 0.75091443 2.02917445 5.68207987]
[ 4.58299978 7.14960394 9.13853129]
[10.60912932 4.52190424 0.6557605 ]]
[[ 0.54393627 8.02341744 11.69489975]
[ 9.09878676 10.60836714 2.41188805]
[ 9.13098333 6.12284334 8.9349382 ]]
[[ 5.84489355 10.19848245 1.65080169]
[ 2.75161562 1.05154552 0.17804374]
[ 3.3166642 10.74081484 5.13601563]]]]
>>>
>>> print a[9:12, 1:4, 5:8, 1:4] # Same window in the replaced array
[[[[ 0.92639125 0.92639125 0.92639125]
[ 0.92639125 0.92639125 0.92639125]
[ 0.92639125 2.46838238 0.92639125]]
[[ 0.92639125 11.21669704 0.92639125]
[ 0.92639125 6.22857282 0.92639125]
[ 0.92639125 0.92639125 0.92639125]]
[[ 4.16785078 0.92639125 0.92639125]
[ 0.92639125 0.92639125 11.98647139]
[ 5.00346525 0.92639125 4.05213149]]]
[[[ 0.92639125 0.92639125 0.92639125]
[ 0.92639125 0.92639125 5.48127994]
[ 0.92639125 10.27373562 0.92639125]]
[[ 0.92639125 0.92639125 7.78071007]
[ 0.79030038 0.92639125 1.51921116]
[ 0.92639125 0.92639125 0.92639125]]
[[ 0.92639125 0.92639125 0.92639125]
[ 0.92639125 0.92639125 0.92639125]
[ 0.92639125 0.92639125 0.92639125]]]
[[[ 0.92639125 0.92639125 0.92639125]
[ 0.92639125 7.14960394 0.92639125]
[ 0.92639125 4.52190424 0.6557605 ]]
[[ 0.92639125 0.92639125 0.92639125]
[ 0.92639125 0.92639125 0.92639125]
[ 9.13098333 0.92639125 8.9349382 ]]
[[ 0.92639125 10.19848245 0.92639125]
[ 0.92639125 0.92639125 0.92639125]
[ 0.92639125 0.92639125 0.92639125]]]]
>>>
>>> print pattern # The pattern that was matched and replaced
[[[[ 9.68507479 1.77585089 5.06069382]
[10.63768984 11.41148096 1.13120712]
[ 6.83684611 nan 11.40490158]]
[[ 9.17344668 nan 7.60737639]
[ 3.14870787 nan 5.61295454]
[ 4.32709261 8.00493326 9.96124294]]
[[ nan 10.66054365 2.95677408]
[11.53789218 2.70725911 nan]
[ nan 4.75230895 nan]]]
[[[11.23856096 8.45979355 7.53268864]
[ 6.14703327 11.90052117 nan]
[ 2.16777734 nan 7.75420214]]
[[10.04726853 11.44895046 nan]
[ nan 3.69735083 nan]
[11.29782542 2.58494314 9.8714708 ]]
[[ 7.9356587 1.48053473 9.71362122]
[ 5.11866341 3.43895455 6.86491947]
[ 8.33774813 5.66923131 2.27884056]]]
[[[ 0.75091443 2.02917445 5.68207987]
[ 4.58299978 nan 9.13853129]
[10.60912932 nan nan]]
[[ 0.54393627 8.02341744 11.69489975]
[ 9.09878676 10.60836714 2.41188805]
[ nan 6.12284334 nan]]
[[ 5.84489355 nan 1.65080169]
[ 2.75161562 1.05154552 0.17804374]
[ 3.3166642 10.74081484 5.13601563]]]]