Skip to content

Commit

Permalink
r.univar: init max value with smallest possible value (#3512) (#3531)
Browse files Browse the repository at this point in the history
Manual backport of 68ca15d
  • Loading branch information
nilason committed Mar 25, 2024
1 parent b076b0c commit 64b42c7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion raster/r.univar/r.univar_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ static void process_raster(univar_stat *stats, int *fd, int *fdz,
double *max = G_malloc(n_alloc * sizeof(double));

for (i = 0; i < n_alloc; i++) {
max[i] = DBL_MIN;
max[i] = -DBL_MAX;
min[i] = DBL_MAX;
}

Expand Down
44 changes: 44 additions & 0 deletions raster/r.univar/testsuite/test_r_univar.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def tearDownClass(cls):
def tearDown(self):
self.runModule("g.remove", flags="f", type="raster", name="map_a")
self.runModule("g.remove", flags="f", type="raster", name="map_b")
self.runModule("g.remove", flags="f", type="raster", name="map_negative")
self.runModule("g.remove", flags="f", type="raster", name="zone_map")
self.runModule("g.remove", flags="f", type="raster", name="zone_map_with_gap")

Expand All @@ -39,6 +40,11 @@ def setUp(self):
expression="zone_map_with_gap = if(row()> 20, 2, 9)",
overwrite=True,
)
self.runModule(
"r.mapcalc",
expression="map_negative = -double(10) - row() - col()",
overwrite=True,
)

def test_1(self):
# Output of r.univar
Expand Down Expand Up @@ -223,6 +229,44 @@ def test_multiple_3(self):
sep="=",
)

def test_negative(self):
"""
check map with only negative values
:return:
"""

univar_string = """n=8100
null_cells=0
cells=8100
min=-190
max=-12
range=178
mean=-101
mean_of_abs=101
stddev=36.7400780256838
variance=1349.83333333333
coeff_var=-36.3763148769146
sum=-818100"""

self.runModule("g.region", res=10)
self.assertModuleKeyValue(
module="r.univar",
map="map_negative",
flags="rg",
reference=univar_string,
precision=6,
sep="=",
)
self.assertModuleKeyValue(
module="r.univar",
map="map_negative",
flags="rg",
nprocs=4,
reference=univar_string,
precision=6,
sep="=",
)

def test_1_zone(self):
"""
one map and zone
Expand Down

0 comments on commit 64b42c7

Please sign in to comment.