I am working on a small piece of code that starts with an interpolated surface I made previsouly. The interpolation filled in gaps in the surface with nan's. Part of my processing involves looking at a local window around a particular point, and calculating some measures using the local surface. I would ideally like this code to only be able to do any calculations if the entire local surface does not contain nan values. The code iterates through the original large surface and checks to see if the local window about a point has a nan.
I know this is not the most efficent way to go about doing it, time-efficiency is not something I have to worry about.
Here is what I have so far:
for in in range(startz,endx):
imin = i - half_tile
imax = i + half_tile +1
for j in range(starty,endy):
jmin = i - half_tile
jmax = i + half_tile +1
#Test the local surface for nan's
z = surface[imin:imax,jmin:jmax]
Test = np.isnan(sum(z))
#conditional statement
if Test:
print 'We have a nan'
#set measures I want to calculate to zero
else:
print 'We have a complete window'
#do a set of calculations
the variable surface
is the interpolated surface I created originally. The half_tile
variables are just defining the size of the local window I want to use. The startx,endx,starty,endy
are defining the size of the original surface to iterate through.
Where I am running into issues is that my conditional statement doesn't seem to be working. It will tell me that the local window that I am evaluating doesn't have any nan's in it, but then the rest of my code (which I didn't show here) will not work because it says there are nan's in the array.
An example of this might be:
[[ 7.07494104 7.04592032 7.01689961 6.98787889 6.95885817 6.92983745
6.90081674 6.87179602 6.8427753 6.81375458 6.78473387 6.75571315
6.72669243]
[ 7.10077447 7.07175376 7.04273304 7.01371232 6.98469161 6.95567089
6.92665017 6.89762945 6.86860874 6.83958802 6.8105673 6.78154658
6.75252587]
[ 7.12660791 7.09758719 7.06856647 7.03954576 7.01052504 6.98150432
6.9524836 6.92346289 6.89444217 6.86542145 6.83640073 6.80738002
6.7783593 ]
[ 7.15244134 7.12342063 7.09439991 7.06537919 7.03635847 7.00733776
6.97831704 6.94929632 6.9202148 6.89105825 6.86190169 6.83274514
6.80358859]
[ 7.17804068 7.14888413 7.11972758 7.09057103 7.06141448 7.03225793
7.00310137 6.97394482 6.94478827 6.91563172 6.88647517 6.85731862
nan]]
Here is an example of the local window that my code is evaluating. In my code this would be z
. The entire array has good values except for the last value which is a nan
.
The "checking" function in my code is not picking up that there is a nan in the array. The conditional statement is returning a false
when it should be a true
to indicate that there is a nan
present. I am missing anything fundamental in the way I am checking the array? or are my methods just totally wrong?
isnan() returns an array with true or false for each element in array. you need np.any() in addition to isnan(). see below example
import numpy as np
a = np.array([[1,2,3,4],[1,2,3,np.NaN]])
print np.isnan(a)
print np.any(np.isnan(a))
results in
[[False False False False]
[False False False True]]
True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With