User007
User007

Reputation: 1559

RuntimeWarning: invalid value encountered in power

This is part of the code I am working on. Look at the last line.

def xyz2srgb(xyz):
    """Convert from XYZ colorspace to sRGB"""
    xyz = xyz.T
    srgb = N.zeros_like(xyz)
    srgb[0] = 3.241*xyz[0] - 1.5374*xyz[1] - 0.4986*xyz[2]
    srgb[1] = -0.9692*xyz[0] + 1.876*xyz[1] + 0.0416*xyz[2]
    srgb[2] = 0.0556*xyz[0] - 0.204*xyz[1] + 1.057*xyz[2]

    srgb /= float(srgb.max())

    a = 0.055
    srgb = N.where(srgb <= 0.0031308,
                12.92*srgb,
                (1+a)*srgb**(1/2.4)-a)

It throws an error:

 ./example.py:29: RuntimeWarning: invalid value encountered in power
  (1+a)*srgb**(1/2.4)-a)

Is this some range limit? I found a similar thread on SO: Numpy error: invalid value encountered in power

Any clue? Thanks.

Upvotes: 2

Views: 18123

Answers (2)

Abdelkarim Boskri
Abdelkarim Boskri

Reputation: 1

For these error you can use the float in the exponent. for example :

>>> test = np.linspace(0,10,1000);)

>>> expos = 2*np.power(test,**6.**)+2*np.power(test,**4.**)

Best regards

Upvotes: 0

DSM
DSM

Reputation: 353339

You should print out the values of srgb. Probably it's not a range error, it's that the negative coefficients here

srgb[0] = 3.241*xyz[0] - 1.5374*xyz[1] - 0.4986*xyz[2]
srgb[1] = -0.9692*xyz[0] + 1.876*xyz[1] + 0.0416*xyz[2]
srgb[2] = 0.0556*xyz[0] - 0.204*xyz[1] + 1.057*xyz[2]

make it entirely possible that elements of srgb can be negative, and unless you're already working in complex numbers, you'll raise a warning when you take the power:

>>> import numpy
>>> numpy.array([-2.0])
array([-2.])
>>> numpy.array([-2.0])**(1/2.4)
__main__:1: RuntimeWarning: invalid value encountered in power
array([ nan])
>>> numpy.array([-2.0+0j])**(1/2.4)
array([ 0.34548198+1.28935629j])

The arguments to where will be evaluated before they're passed, so the powers will be taken before the branch on the srgb <= 0.0031308 condition divides up the values, and the warning is triggered.

Upvotes: 4

Related Questions