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,....]
CodePudding user response:
I'm not sure about what do you need. If you need to compute how many tries the code does before to see another value less than 0.6, this can be easly done creating a variable that is increased at each iteration and set to 0 when you find a "correct" value.
If you need to compute how much time it runs before to find another value less than 0.6 you need to use the time
library. The istruction t1 = time.time()
saves in t1 the amount of seconds elapsed since epoch (epoch is January 1, 1970, 00:00:00) as floating point number (see here to more details), so when you start the research you save your time in one variable (say t1), when you find a value in the array that is less than 0.6 you save in another variable the current time (say t2), and at this point to know how much time is elapsed you need to compute (t2-t1); then save the value of t2 in t1 and continue like this.
EDIT: with your edit I have understood what you need.
Try this, but remember to add import time
:
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 = []
t0 = None
t1 = None
timestamps = []
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)
t1=time.time()
if(t0==None):
t0=t1
timestamps.append(t1-t0)
indices_to_check.extend(
get_neighbor_indices(pos, arr.shape)
)
return result,timestamps
....
T,timestamps=iterate_array(0,1, P1, lambda x : x < 0.6)
print(T)
print(timestamps)