I am trying to perform an object-based classification using a python-based script with rsgislib. Although, when converting to dB, an error is appearing. Any ideas why is this happening?

 Create and Calculate dB columns
ratDataset = gdal.Open(clumpsImg, gdal.GA_Update)
HHPow = rat.readColumn(ratDataset, 'HHPow')
HVPow = rat.readColumn(ratDataset, 'HVPow')
HHHVPow = rat.readColumn(ratDataset, 'HHHVPow') 

HHdB = numpy.where(HHPow>0, 10*numpy.log10(HHPow), 0)
HVdB = numpy.where(HVPow>0, 10*numpy.log10(HVPow), 0)
HHHVdB = numpy.where(HHHVPow>0, 10*numpy.log10(HHHVPow), 0)

rat.writeColumn(ratDataset, "HHdB", HHdB)
rat.writeColumn(ratDataset, "HVdB", HVdB)
rat.writeColumn(ratDataset, "HHHVdB", HHHVdB)
ratDataset = None

And I keep getting the following error:

Writing Stats (Mean, ) to Output RAT

/data/3_to_5_copy/16_PerformObjClass.py:63: RuntimeWarning: divide by zero encountered in log10 HHdB = numpy.where(HHPow>0, 10numpy.log10(HHPow), 0) /data/3_to_5_copy/16_PerformObjClass.py:64: RuntimeWarning: divide by zero encountered in log10 HVdB = numpy.where(HVPow>0, 10numpy.log10(HVPow), 0) /data/3_to_5_copy/16_PerformObjClass.py:65: RuntimeWarning: divide by zero encountered in log10 HHHVdB = numpy.where(HHHVPow>0, 10*numpy.log10(HHHVPow), 0) Creating output image using input image

  • 1
    The problem is for your numpy.log10, even non filtered data by numpy.where are computed so, if a 0 value is in your rat columns HHPow, HVPow or HHHVPow, the warning appear. Jul 28, 2020 at 14:03
  • 1
    do you have any ideas how can i fix it please? i am totally a beginner and i am not sure
    – 1milenac
    Jul 28, 2020 at 19:50

2 Answers 2

/data/3_to_5_copy/16_PerformObjClass.py:63: RuntimeWarning: divide by zero encountered in log10 HHdB = numpy.where(HHPow>0, 10numpy.log10(HHPow), 0) 

This error occurs when trying to compute a mathematical impossibility - like 1 divided by 0. Here you are trying to take the log of a number which returns a non-defined answer.

These runtime Warnings are merely warning messages but do not stop the code from working as it should. There is no need to "correct" this


Adjust your code this way:

HHdB = numpy.where(HHPow>0,
                   10 * numpy.log10(HHPow, where=HHPow>0),

Option where in log10 is used to prevent such warning or other errors:

where: array_like, optional

This condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.

For instance if the condition is HHPow>0, the log is calculated when it is True for the element, else the element is not processed.

In case the output array is created by Numpy, like here, the returned value for non-processed elements is undetermined, in my case this is -inf. In order to give a default value for the unprocessed elements, you need to use numpy.where as you did.

  • Note you still need to provide the where parameter to log10. The reason is when numpy.where is used, it picks the values from one of the two arrays provided. But the arrays are not computed on the fly, they are completely computed upfront. So log10 is computed for all elements and the warning is issued.

Instead of a default 0, you could also use numpy.nan, which is processed correctly by pyplot.

More: log10 documentation and where documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.