This code scans the neighbouring elements with a given criterion. For example, it starts from
0.26373153 and picks 0.58720689 since the criterion says to select elements less than 0.6. Similarly, it moves from 0.58720689 to 0.54531058. The current output along with the desired output is attached.
How do I also get a time output for each iteration? Suppose the time loop starts at t=0 for 0.26373153 and then t>0 for the next values: 0.58720689,0.54531058,...
import numpy as np
def get_neighbor_indices(position, dimensions):
'''
dimensions is a shape of np.array
'''
i, j = position
indices = [(i+1,j), (i-1,j), (i,j+1), (i,j-1)]
return [
(i,j) for i,j in indices
if i>=0 and i<dimensions[0]
and j>=0 and j<dimensions[1]
]
def iterate_array(init_i, init_j, arr, condition_func):
'''
arr is an instance of np.array
condition_func is a function (value) => boolean
'''
indices_to_check = [(init_i,init_j)]
checked_indices = set()
result = []
while indices_to_check:
pos = indices_to_check.pop()
if pos in checked_indices:
continue
item = arr[pos]
checked_indices.add(pos)
if condition_func(item):
result.append(item)
indices_to_check.extend(
get_neighbor_indices(pos, arr.shape)
)
return result
#P1 = np.random.rand(10,10)
P1=np.array([[ 1.40591794, 0.26373153, 0.98327887, 11.26958535, 1.25191783],
[ 0.54531058, 0.58720689, 0.54674676, 3.89351201, 3.73486589],
[ 0.50904881, 0.16939308, 0.27069582, 0.61941143, 0.88792361],
[ 0.61828522, 0.30061379, 0.62551028, 0.28315714, 0.989013 ],
[ 0.39175302, 0.30969749, 1.59701676, 2.11862101, 0.81709991]])
T=iterate_array(0,1, P1, lambda x : x < 0.6)
print(T)
The current output is
[0.26373153, 0.58720689, 0.54531058, 0.50904881, 0.16939308, 0.27069582, 0.54674676, 0.30061379, 0.30969749, 0.39175302]
In addition, the desired output with the time stamp for each value should look like below. These values are just to demonstrate since I don't really know what the actual time loop will display.
[0,0.01,0.013,....]