[GRASS-user] log(0)-error in r.mapcalculator

Dear List,

I keep getting a log(0) error in r.mapcalculator, even if I enlarge the data. This seems to be a bug as I controlled the same data with R and get (non-infinity) values. Does r.mapcalculator eventually truncate the results of formulas to some integer values?

logit-expression: log(((A/100)/(1-(A/100)))

To avoid error of log(0) I enlarged the data by 0.00001, 1 and 5 but keep getting the error.

I run it out of R with:
param<-list(amap="r_slope_perc", formula="log(((A+0.00001)/100)/(1-(A/100))) ", outfile="r_slope_perc_logit")
execGRASS("r.mapcalculator", parameters=param, flags=c("overwrite"))

(r.mapcalculator amap="r_slope_perc", formula="log(((A+0.00001)/100)/(1-(A/100)))" outfile="r_slope_perc_logit" --overwrite)

Any ideas why this happens?

Patrick

On Thu, Nov 8, 2012 at 10:50 AM, Patrick S. <patrick_GIS@gmx.ch> wrote:

Dear List,

I keep getting a log(0) error in r.mapcalculator, even if I enlarge the
data. This seems to be a bug as I controlled the same data with R and get
(non-infinity) values. Does r.mapcalculator eventually truncate the results
of formulas to some integer values?

logit-expression: log(((A/100)/(1-(A/100)))

In r.mapcalc, the numbers are treated as integers. Try instead log(((A
/ 100.0) / (1.0 - (A / 100.0)))
Changing e.g. 1 to 1.0 forces r.mapcalc to do floating point calculations.

HTH,

Markus M

To avoid error of log(0) I enlarged the data by 0.00001, 1 and 5 but keep
getting the error.

I run it out of R with:
param<-list(amap="r_slope_perc", formula="log(((A+0.00001)/100)/(1-(A/100)))
", outfile="r_slope_perc_logit")
execGRASS("r.mapcalculator", parameters=param, flags=c("overwrite"))

(r.mapcalculator amap="r_slope_perc",
formula="log(((A+0.00001)/100)/(1-(A/100)))" outfile="r_slope_perc_logit"
--overwrite)

Any ideas why this happens?

Patrick
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

Thank you so much, Markus!

That was the missing hint and it works now!
However, I just went through the documentation, which says: "/F/ means that the functions always results in a floating point value" and Function "log" has "F".

This is somehow misleading and rather should be an "*". Can I correct that somewhere without access-rights (to contribute) or would I rather report this?

Patrick

  that On 11/08/2012 07:34 PM, Markus Metz wrote:

On Thu, Nov 8, 2012 at 10:50 AM, Patrick S. <patrick_GIS@gmx.ch> wrote:

Dear List,

I keep getting a log(0) error in r.mapcalculator, even if I enlarge the
data. This seems to be a bug as I controlled the same data with R and get
(non-infinity) values. Does r.mapcalculator eventually truncate the results
of formulas to some integer values?

logit-expression: log(((A/100)/(1-(A/100)))

In r.mapcalc, the numbers are treated as integers. Try instead log(((A
/ 100.0) / (1.0 - (A / 100.0)))
Changing e.g. 1 to 1.0 forces r.mapcalc to do floating point calculations.

HTH,

Markus M

To avoid error of log(0) I enlarged the data by 0.00001, 1 and 5 but keep
getting the error.

I run it out of R with:
param<-list(amap="r_slope_perc", formula="log(((A+0.00001)/100)/(1-(A/100)))
", outfile="r_slope_perc_logit")
execGRASS("r.mapcalculator", parameters=param, flags=c("overwrite"))

(r.mapcalculator amap="r_slope_perc",
formula="log(((A+0.00001)/100)/(1-(A/100)))" outfile="r_slope_perc_logit"
--overwrite)

Any ideas why this happens?

Patrick
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

On Fri, Nov 9, 2012 at 9:27 AM, Patrick S. <patrick_GIS@gmx.ch> wrote:

Thank you so much, Markus!

That was the missing hint and it works now!
However, I just went through the documentation, which says: "F means that
the functions always results in a floating point value" and Function "log"
has "F".

The documentation is correct. The problem arose because
((A/100)/(1-(A/100)) returns an integer value if A is an integer, i.e.
the truncation happened before log() is called.

Markus M

that On 11/08/2012 07:34 PM, Markus Metz wrote:

On Thu, Nov 8, 2012 at 10:50 AM, Patrick S. <patrick_GIS@gmx.ch> wrote:

Dear List,

I keep getting a log(0) error in r.mapcalculator, even if I enlarge the
data. This seems to be a bug as I controlled the same data with R and get
(non-infinity) values. Does r.mapcalculator eventually truncate the results
of formulas to some integer values?

logit-expression: log(((A/100)/(1-(A/100)))

In r.mapcalc, the numbers are treated as integers. Try instead log(((A
/ 100.0) / (1.0 - (A / 100.0)))
Changing e.g. 1 to 1.0 forces r.mapcalc to do floating point calculations.

HTH,

Markus M

To avoid error of log(0) I enlarged the data by 0.00001, 1 and 5 but keep
getting the error.

I run it out of R with:
param<-list(amap="r_slope_perc", formula="log(((A+0.00001)/100)/(1-(A/100)))
", outfile="r_slope_perc_logit")
execGRASS("r.mapcalculator", parameters=param, flags=c("overwrite"))

(r.mapcalculator amap="r_slope_perc",
formula="log(((A+0.00001)/100)/(1-(A/100)))" outfile="r_slope_perc_logit"
--overwrite)

Any ideas why this happens?

Patrick
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

Markus,

I understand your arguments, but "A" is the slope of r.slope.aspect and has floating point values as input for the formula. I just created a testcase to be able to report on the behavior in detail. As you can see below the results are truncated to integer as soon as I add a term to "A" before the devision, but it remains unclear to me why adding a float-value reduces the attributes to integer-level as well.

This is a somewhat tricky behavior as it is usual to add a small constant to all values to avoid log(0)-errors.

Patrick

####TESTING
#algorithm:
param<-list(amap="r_slope_perc", formula=##varies in testing-see below##", outfile="r_test_logit") ###formula has been replaced for each
execGRASS("r.mapcalculator", parameters=param, flags=c("overwrite"))
x_perc<-readRAST6("r_slope_perc")
x_log<-readRAST6("r_test_logit")
xperc<-x_perc$r_slope_perc
xlog<-x_log$r_test_logit

summary(xperc)
summary(xlog)

sum(xperclog(((A+1)/100)/(1-(A+1/100)))==0, na.rm=T)

###Testcase1: formula= log(((A/100)/(1-(A/100))
> summary(xperc)
    Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
   0.000 3.162 8.602 13.540 18.440 229.800 9852
> summary(xlog)
    Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
    -4.9 -3.1 -2.2 -2.2 -1.4 9.5 564240
> sum(xperc==0, na.rm=T)
[1] 550958

###Testcase2: formula= log(((A+1)/100)/(1-(A+1/100)))
> summary(xperc)
    Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
   0.000 3.162 8.602 13.540 18.440 229.800 9852
> summary(xlog)
    Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
      -5 -5 -5 -4 -5 -3 5369270
> sum(xperc==0, na.rm=T)
[1] 550958

###Testcase3: formula= log(((A+0.01)/100)/(1-(A+0.01/100)))
> summary(xperc)
    Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
   0.000 3.162 8.602 13.540 18.440 229.800 9852
> summary(xlog)
    Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
      -9 -9 -9 -8 -9 -4 5369270

> sum(xperc==0, na.rm=T)
[1] 550958

On 11/09/2012 10:22 AM, Markus Metz wrote:

The documentation is correct. The problem arose because
((A/100)/(1-(A/100)) returns an integer value if A is an integer, i.e.
the truncation happened before log() is called.

Markus M

Patrick S. wrote:

###Testcase2: formula= log(((A+1)/100)/(1-(A+1/100)))

Note that

  (A+1/100) = A+(1/100) = A

as 1/100 will use integer division.

--
Glynn Clements <glynn@gclements.plus.com>