Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update r.stats to match r.report
Browse files Browse the repository at this point in the history
kritibirda26 committed Aug 12, 2024
1 parent 7482b9a commit e8e5721
Showing 3 changed files with 16,286 additions and 29 deletions.
54 changes: 25 additions & 29 deletions raster/r.stats/stats.c
Original file line number Diff line number Diff line change
@@ -328,12 +328,10 @@ int print_cell_stats(char *fmt, int with_percents, int with_counts,
if (node->values[i] == NULL_CELL) {
switch (format) {
case JSON:
json_object_set_null(category, "value");
json_object_set_null(category, "category");
json_object_set_string(
category, "category",
category, "label",
Rast_get_c_cat(&null_cell, &labels[i]));
json_object_set_string(category, "description",
names[i]);
break;
case PLAIN:
fprintf(stdout, "%s%s", i ? fs : "", no_data_str);
@@ -346,14 +344,14 @@ int print_cell_stats(char *fmt, int with_percents, int with_counts,
else if (raw_output || !is_fp[i] || as_int) {
switch (format) {
case JSON:
json_object_set_number(category, "value",
json_object_set_number(category, "category",
(long)node->values[i]);
json_object_set_string(
category, "category",
Rast_get_c_cat((CELL *)&(node->values[i]),
&labels[i]));
json_object_set_string(category, "description",
names[i]);
if (with_labels && !is_fp[i]) {
json_object_set_string(
category, "label",
Rast_get_c_cat((CELL *)&(node->values[i]),
&labels[i]));
}
break;
case PLAIN:
fprintf(stdout, "%s%ld", i ? fs : "",
@@ -384,7 +382,7 @@ int print_cell_stats(char *fmt, int with_percents, int with_counts,
double average = (dLow + dHigh) / 2.0;
switch (format) {
case JSON:
json_object_set_number(category, "category_average",
json_object_set_number(category, "average",
average);
break;
case PLAIN:
@@ -398,8 +396,10 @@ int print_cell_stats(char *fmt, int with_percents, int with_counts,
else {
switch (format) {
case JSON:
json_object_set_number(category, "low", dLow);
json_object_set_number(category, "high", dHigh);
json_object_dotset_number(category, "range.low",
dLow);
json_object_dotset_number(category, "range.high",
dHigh);
break;
case PLAIN:
/* print intervals */
@@ -415,21 +415,17 @@ int print_cell_stats(char *fmt, int with_percents, int with_counts,
}
switch (format) {
case JSON:
if (cat_ranges) {
json_object_set_string(
category, "category",
labels[i].labels[node->values[i]]);
}
else {
json_object_set_string(
category, "category_low",
Rast_get_d_cat(&dLow, &labels[i]));
json_object_set_string(
category, "category_high",
Rast_get_d_cat(&dHigh, &labels[i]));
if (with_labels) {
if (cat_ranges) {
json_object_set_string(
category, "label",
labels[i].labels[node->values[i]]);
}
else {
json_object_set_string(category, "label",
"from to");
}
}
json_object_set_string(category, "description",
names[i]);
break;
case PLAIN:
if (with_labels) {
@@ -451,7 +447,7 @@ int print_cell_stats(char *fmt, int with_percents, int with_counts,
}

if (format == JSON) {
json_object_set_value(object, "labels", categories_value);
json_object_set_value(object, "categories", categories_value);
}

if (with_areas) {
16,082 changes: 16,082 additions & 0 deletions raster/r.stats/test_suite/test_json2.json

Large diffs are not rendered by default.

179 changes: 179 additions & 0 deletions raster/r.stats/test_suite/test_r_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
"""
Name: r.report test
Purpose: Tests r.report and its flags/options.
Author: Sunveer Singh, Google Code-in 2017
Copyright: (C) 2017 by Sunveer Singh and the GRASS Development Team
Licence: This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.
"""

import json
import os
from itertools import zip_longest

from grass.gunittest.case import TestCase

from grass.gunittest.gmodules import SimpleModule


class TestRasterreport(TestCase):
outfile = "test_out.csv"

@classmethod
def setUpClass(cls):
"""Use temporary region settings"""
cls.use_temp_region()
cls.runModule("g.region", raster="elevation")

@classmethod
def tearDownClass(cls):
"""!Remove the temporary region"""
cls.del_temp_region()
if os.path.isfile(cls.outfile):
os.remove(cls.outfile)

def _assert_stats_equal(self, reference, data):
for item1, item2 in zip_longest(reference, data):
self.assertCountEqual(list(item1.keys()), list(item2.keys()))
for category1, category2 in zip_longest(
item1["categories"], item2["categories"]
):
if "category" in category1:
self.assertEqual(category1["category"], category2["category"])
if "label" in category1:
self.assertEqual(category1["label"], category2["label"])
if "range" in category1:
self.assertAlmostEqual(
category1["range"]["low"], category2["range"]["low"], places=6
)
self.assertAlmostEqual(
category1["range"]["high"], category2["range"]["high"], places=6
)
self.assertEqual(item1["count"], item2["count"])

def test_json(self):
"""Test JSON format"""
reference = [
{
"categories": [
{"category": 1, "label": "CARY"},
{"category": 27511, "label": "CARY"},
],
"count": 105800,
},
{
"categories": [
{"category": 1, "label": "CARY"},
{"category": 27513, "label": "CARY"},
],
"count": 20530,
},
{
"categories": [
{"category": 1, "label": "CARY"},
{"category": 27518, "label": "CARY"},
],
"count": 134519,
},
{
"categories": [
{"category": 2, "label": "GARNER"},
{"category": 27529, "label": "GARNER"},
],
"count": 141572,
},
{
"categories": [
{"category": 3, "label": "APEX"},
{"category": 27539, "label": "APEX"},
],
"count": 25444,
},
{
"categories": [
{"category": 4, "label": "RALEIGH-CITY"},
{"category": 27601, "label": "RALEIGH"},
],
"count": 45468,
},
{
"categories": [
{"category": 4, "label": "RALEIGH-CITY"},
{"category": 27604, "label": "RALEIGH"},
],
"count": 47389,
},
{
"categories": [
{"category": 4, "label": "RALEIGH-CITY"},
{"category": 27605, "label": "RALEIGH"},
],
"count": 23677,
},
{
"categories": [
{"category": 4, "label": "RALEIGH-CITY"},
{"category": 27608, "label": "RALEIGH"},
],
"count": 43980,
},
{
"categories": [
{"category": 5, "label": "RALEIGH-SOUTH"},
{"category": 27603, "label": "RALEIGH"},
],
"count": 429179,
},
{
"categories": [
{"category": 5, "label": "RALEIGH-SOUTH"},
{"category": 27606, "label": "RALEIGH"},
],
"count": 662642,
},
{
"categories": [
{"category": 5, "label": "RALEIGH-SOUTH"},
{"category": 27610, "label": "RALEIGH"},
],
"count": 135811,
},
{
"categories": [
{"category": 6, "label": "RALEIGH-WEST"},
{"category": 27607, "label": "RALEIGH"},
],
"count": 208989,
},
]
module = SimpleModule(
"r.stats", flags="cl", input="towns,zipcodes", format="json"
)
self.runModule(module)
data = json.loads(module.outputs.stdout)
self.assertEqual(reference, data)

def test_json2(self):
"""Test JSON format with more options"""
reference_file = os.path.normpath(
os.path.join(__file__, "..", "test_json2.json")
)
with open(reference_file, "r") as f:
reference = json.load(f)
module = SimpleModule(
"r.stats",
flags="cl",
input="towns,elevation",
format="json",
)
self.runModule(module)
data = json.loads(module.outputs.stdout)
self._assert_stats_equal(reference, data)


if __name__ == "__main__":
from grass.gunittest.main import test

test()

0 comments on commit e8e5721

Please sign in to comment.