Python Forum

Full Version: Permanent Nan in NP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
need to conduct some analysis of the image, do not go into the questions what this formula is, it does not matter.
import numpy as np
from PIL import Image
#np.seterr(divide='ignore', invalid='ignore')
image=Image.open('E://test.jpg')
imgr,imgg,imgb=image.split()
r=np.asarray(imgr).astype('float64')
g=np.asarray(imgg).astype('float64')
b=np.asarray(imgb).astype('float64')
vari=(g-r)/(g+r-b)
nan=0
a=[]
for i in range (len(vari)):
    for j in range(len(vari[i])):
        if np.isnan(vari[i][j]):
            print ('NAN object',i,j)
            a=[]
            if not np.isnan(vari[i-1][j-1]):
                a.append(vari[i-1][j-1])
            if not np.isnan(vari[i-1][j]):
                a.append(vari[i-1][j])
            if not np.isnan(vari[i-1][j+1]):
                a.append(vari[i-1][j+1])#1st
            if not np.isnan(vari[i][j-1]):
                a.append(vari[i][j-1])
            if not np.isnan(vari[i][j+1]):
                a.append(vari[i][j+1])
            if not np.isnan(vari[i+1][j-1]):
                a.append(vari[i+1][j-1])
            if not np.isnan(vari[i+1][j]):
                a.append(vari[i+1][j])
            if not np.isnan(vari[i+1][j+1]):
                a.append(vari[i+1][j+1])
            a=np.asarray(a)
            print ('array',a,'mean',np.mean(a))
            vari[i][j]=0   
            nan=nan+1
print('TOTAL NAN',nan)
for i in range (len(vari)):
    for j in range(len(vari[i])):
        if np.isnan(vari[i][j]):
            print ('new NAN object',i,j)
s=0
vari=vari.ravel()
vari2=list(vari)
for i in range(len(vari2)):
    s=s+vari[i]
Code explanations: I need to calculate np.std and np.mean of full 2d array. But there is NaN alltime, i tried to delete Nan in loop, and check sum, but it is NaN too
would you please attach image (test.jpg) so that the code can be run?
(Jun-26-2018, 02:50 PM)Larz60+ Wrote: [ -> ]would you please attach image (test.jpg) so that the code can be run?
https://ibb.co/caLfkT
sorry, there was error first time, there must be image, thank you

(Jun-26-2018, 02:50 PM)Larz60+ Wrote: [ -> ]would you please attach image (test.jpg) so that the code can be run?
I hope u can help me, if u got question about code u can ask me
I'll give it a go.
(Jun-26-2018, 03:40 PM)Larz60+ Wrote: [ -> ]I'll give it a go.
import numpy as np
from PIL import Image
import math
#np.seterr(divide='ignore', invalid='ignore')
image=Image.open('E:/test.jpg')
imgr,imgg,imgb=image.split()
r=np.asarray(imgr).astype('float64')
g=np.asarray(imgg).astype('float64')
b=np.asarray(imgb).astype('float64')
vari=(g-r)/(g+r-b)
nan=0
a=[]
for i in range (len(vari)):
    for j in range(len(vari[i])):
        if not np.isnan(vari[i][j]):
            b=float(vari[i][j])
            a.append(b)
        else:
            vari[i][j]=1
            nan=nan+1
print('len vari',len(vari)*len(vari[0]))
print('len a (no NaN elements)',len(a))
print('TOTAL NAN',nan)
for i in range (len(vari)):
    for j in range(len(vari[i])):
        if np.isnan(vari[i][j]):
            print ('new NAN object',i,j)
s=0
vari=vari.ravel()
for i in range (len(a)):
    if math.isnan(a[i]):
        print ('new NAN object in a massiv',i,j)
for i in range(len(a)):
    s=s+a[i]
print ('summa iz a',s)
s2=0
for i in range(len(vari)):
    s2=s2+vari[i]
print ('summa',s2)
this is new version, sum is nan
There are warnings about divide by zero errors

I modified the code a bit so as to find the image file on my computer, but nothing else:
import numpy as np
from PIL import Image
import math
import os
#np.seterr(divide='ignore', invalid='ignore')

path = os.path.dirname(__file__)
os.chdir(path)
path = os.getcwd()

image = Image.open(os.path.abspath('../images/test.jpg'))
# image=Image.open('E:/test.jpg')

imgr,imgg,imgb=image.split()
r=np.asarray(imgr).astype('float64')
g=np.asarray(imgg).astype('float64')
b=np.asarray(imgb).astype('float64')
vari=(g-r)/(g+r-b)
nan=0
a=[]
for i in range (len(vari)):
    for j in range(len(vari[i])):
        if not np.isnan(vari[i][j]):
            b=float(vari[i][j])
            a.append(b)
        else:
            vari[i][j]=1
            nan=nan+1
print('len vari',len(vari)*len(vari[0]))
print('len a (no NaN elements)',len(a))
print('TOTAL NAN',nan)
for i in range (len(vari)):
    for j in range(len(vari[i])):
        if np.isnan(vari[i][j]):
            print ('new NAN object',i,j)
s=0
vari=vari.ravel()
for i in range (len(a)):
    if math.isnan(a[i]):
        print ('new NAN object in a massiv',i,j)
for i in range(len(a)):
    s=s+a[i]
print ('summa iz a',s)
s2=0
for i in range(len(vari)):
    s2=s2+vari[i]
print ('summa',s2)
run:
Output:
Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. m:/python/e-h/f/forum/g-m/ImageSoftware/src/June26-2018.py:18: RuntimeWarning: divide by zero encountered in true_divide vari=(g-r)/(g+r-b) m:/python/e-h/f/forum/g-m/ImageSoftware/src/June26-2018.py:18: RuntimeWarning: invalid value encountered in true_divide vari=(g-r)/(g+r-b) len vari 2666000 len a (no NaN elements) 2665987 TOTAL NAN 13 summa iz a nan m:/python/e-h/f/forum/g-m/ImageSoftware/src/June26-2018.py:46: RuntimeWarning: invalid value encountered in double_scalars s2=s2+vari[i] summa nan
(Jun-26-2018, 05:15 PM)Larz60+ Wrote: [ -> ]There are warnings about divide by zero errors I modified the code a bit so as to find the image file on my computer, but nothing else:
 import numpy as np from PIL import Image import math import os #np.seterr(divide='ignore', invalid='ignore') path = os.path.dirname(__file__) os.chdir(path) path = os.getcwd() image = Image.open(os.path.abspath('../images/test.jpg')) # image=Image.open('E:/test.jpg') imgr,imgg,imgb=image.split() r=np.asarray(imgr).astype('float64') g=np.asarray(imgg).astype('float64') b=np.asarray(imgb).astype('float64') vari=(g-r)/(g+r-b) nan=0 a=[] for i in range (len(vari)): for j in range(len(vari[i])): if not np.isnan(vari[i][j]): b=float(vari[i][j]) a.append(b) else: vari[i][j]=1 nan=nan+1 print('len vari',len(vari)*len(vari[0])) print('len a (no NaN elements)',len(a)) print('TOTAL NAN',nan) for i in range (len(vari)): for j in range(len(vari[i])): if np.isnan(vari[i][j]): print ('new NAN object',i,j) s=0 vari=vari.ravel() for i in range (len(a)): if math.isnan(a[i]): print ('new NAN object in a massiv',i,j) for i in range(len(a)): s=s+a[i] print ('summa iz a',s) s2=0 for i in range(len(vari)): s2=s2+vari[i] print ('summa',s2) 
run:
Output:
Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. m:/python/e-h/f/forum/g-m/ImageSoftware/src/June26-2018.py:18: RuntimeWarning: divide by zero encountered in true_divide vari=(g-r)/(g+r-b) m:/python/e-h/f/forum/g-m/ImageSoftware/src/June26-2018.py:18: RuntimeWarning: invalid value encountered in true_divide vari=(g-r)/(g+r-b) len vari 2666000 len a (no NaN elements) 2665987 TOTAL NAN 13 summa iz a nan m:/python/e-h/f/forum/g-m/ImageSoftware/src/June26-2018.py:46: RuntimeWarning: invalid value encountered in double_scalars s2=s2+vari[i] summa nan
yes i know about it, but in a array there is no NaN elements, and sum is 0
You are abusing both numpy and Python
vari[~np.isnan(vari)].tolist()
will give you the list of non-NaN values in your array.

Iteration over indices is extremely un-Pythonic
Thank you all!