[GRASS-dev] Python assertion error in GRASS GIS script
Nikos Alexandris
nik at nikosalexandris.net
Mon Oct 8 09:44:19 PDT 2018
Dears,
I try to understand why the following assertion fails to work, as
expected, in a Python GRASS GIS script?
The following fragment of code
```
fraction_categories = grass.parse_command('r.category',
map=fractions,
delimiter='\t')
fractions_sum = sum([float(x)
if not math.isnan(float(x))
else 0
for x
in fraction_categories.values()])
msg = "Fractions: {f}".format(f=fraction_categories)
g.message(_(msg))
g.message(_("Sum: {s}".format(s=fractions_sum)))
```
gives
```
Fractions: {u'24': u'nan', u'25': u'0.0159235666814', u'26':
u'0.0891719747445', u'27': u'nan', u'20': u'0.0238853509712', u'21':
u'0.0286624211654', u'23': u'0.143312100132', u'29': u'nan', u'40': u'nan',
u'41': u'0.0318471333627', u'1': u'nan', u'3': u'nan', u'2':
u'0.0159235669187', u'5': u'nan', u'4': u'nan', u'7': u'nan', u'6': u'nan',
u'9': u'nan', u'8': u'nan', u'11': u'0.00159235669186', u'10': u'nan',
u'12': u'0.162420386604', u'15': u'0.366242033672', u'16': u'nan', u'18':
u'0.0573248423308', u'31': u'nan', u'36': u'nan', u'35':
u'0.0636942667255', u'32': u'nan'}
Sum: 1.0
```
Yet the following assertion does not work as expected
```
assert fractions_sum < 1.000000000000001, "Sum of fractions is > 1"
```
and returns
```
Traceback (most recent call last):
File "/osgeo/grasstrunk/dist.x86_64-pc-linux-gnu/scripts/r.estimap", line 3979, in <module>
sys.exit(main())
File "/osgeo/grasstrunk/dist.x86_64-pc-linux-gnu/scripts/r.estimap", line 3964, in main
**supply_parameters)
File "/osgeo/grasstrunk/dist.x86_64-pc-linux-gnu/scripts/r.estimap", line 2864, in compute_supply
assert fractions_sum < 1.000000000000001, "Sum of fractions is > 1"
AssertionError: Sum of fractions is > 1
```
The following _does_ work, in Python (inside a GRASS GIS session),
```
import os, sys, subprocess
import datetime, time
import csv
import math
import atexit
import grass.script as grass
from grass.exceptions import CalledModuleError
from grass.pygrass.modules.shortcuts import general as g
from grass.pygrass.modules.shortcuts import raster as r
from grass.pygrass.modules.shortcuts import vector as v
fractions_sum = 1.
assert fractions_sum < 1.000000000000001, "Sum of fractions is > 1"
```
Nikos
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/grass-dev/attachments/20181008/d86cb7c9/attachment.sig>
More information about the grass-dev
mailing list