NetLogo / GIS-Extension

the GIS (Geographic Information Systems) extension bundled with NetLogo
Other
28 stars 19 forks source link

importing GIS data can result in NaN values #5

Open SethTisue opened 12 years ago

SethTisue commented 12 years ago

Tina Balke reports:

Dear Netlogo Team, I came across a possible bug in Netlogo concerning the min and max methods in combination with is-number? when using the GIS extension. In the simulation I use population figures as patch variable (the name of this variable is "population"). I've loaded GIS raster information into Netlogo and used "gis:apply-raster gis:load-dataset "data/sez_01_rer_grid.asc" population" to match the GIS data with the patch variable. When querying the max value of population using "show max [ population ] of patches with [is-number? population]" I get NaN as a result, despite the is-number? statement. Using "show max [ population ] of patches with [population >= 0]" instead works.

SethTisue commented 12 years ago

Hmmm... I'm not sure how NetLogo ought to handle this.

When not using the GIS extension, NaN as a value is outlawed in NetLogo. Any operation producing NaN immediately causes an error. It's a case that (as Tina has discovered) the is-number? primitive doesn't even bother to check for, since it isn't supposed to arise in the first place.

GIS data, though, may contain NaN values that the GIS extension lets through. So how should NetLogo treat them?

It may seem rather bizarre that is-number? is true of NaN, I mean, how much more clear could "Not a Number" be that it isn't a number? But, if it isn't a number, what is it? It may not be a number per se, there's a very strong sense in which it is nonetheless numeric. It isn't some other kind of value, and it only arises in contexts where a number is normally expected.

NetLogo isn't the only language in which the equivalent of is-number? returns true on NaN. For example in JavaScript we have:

> typeof(NaN)
'number'

and in order to distinguish NaN from actual numbers, a separate isNaN function is provided. Java is similar; NaN is a distinct value, but not a distinct type. new java.lang.Double(1.0).getClass and new java.lang.Double(Double.NAN).getClass both return an identical result.

Perhaps the GIS extension shouldn't be letting the NaN values through in the first place, and should be replacing them with some valid but non-numeric NetLogo value, such as false. That solution isn't available in statically typed languages, but in a dynamically typed language, it would work.

erussell commented 12 years ago

It's been a while, but I think the reason I went with NaN instead of some other non-numeric value was that I couldn't think of a good non-numeric value that wouldn't cause problems in at least one use case. I can think of at least two good improvements to the current situation:

  1. Provide a gis:is-nan? primitive in the GIS extension to explicitly test for NaN values, and explain in its documentation why is-number? NaN reports true
  2. Allow the user to configure the representation of missing values. By default, the extension would use the current behavior so as not to break existing code. But users could also say gis:set-nan-representation false if they wanted to.
alexredplanet commented 2 years ago

This issue still persists. The workaround shown in the docs using a check of ifelse (value <= 0) or (value >= 0) where value could be NaN reports an error in NetLogo6.2.2 of "can't use <= to compare between a number and not a number". The most reliable way I have found is to define a set of values you know you are looking for, and then use a member? test. This will allow one to filter null values without throwing any errors.

fabio782 commented 1 year ago

i think this may work let resol 1e-4 ifelse (abs value > resol)...