diff --git a/imagery/i.cca/main.c b/imagery/i.cca/main.c index 22ccd2058a0..6aee7b0e631 100644 --- a/imagery/i.cca/main.c +++ b/imagery/i.cca/main.c @@ -156,10 +156,10 @@ int main(int argc, char *argv[]) cov[i] = G_alloc_matrix(bands, bands); } - outbandmax = (CELL *)G_calloc(nclass, sizeof(CELL)); - outbandmin = (CELL *)G_calloc(nclass, sizeof(CELL)); - datafds = (int *)G_calloc(nclass, sizeof(int)); - outfds = (int *)G_calloc(nclass, sizeof(int)); + outbandmax = (CELL *)G_calloc(bands, sizeof(CELL)); + outbandmin = (CELL *)G_calloc(bands, sizeof(CELL)); + datafds = (int *)G_calloc(bands, sizeof(int)); + outfds = (int *)G_calloc(bands, sizeof(int)); /* Here is where the information regarding @@ -169,13 +169,14 @@ int main(int argc, char *argv[]) */ samptot = 0; - for (i = 1; i <= nclass; i++) { - nsamp[i] = sigs.sig[i - 1].npoints; + for (i = 0; i < nclass; i++) { + nsamp[i] = sigs.sig[i].npoints; samptot += nsamp[i]; - for (j = 1; j <= bands; j++) { - mu[i][j] = sigs.sig[i - 1].mean[j - 1]; - for (k = 1; k <= j; k++) - cov[i][j][k] = cov[i][k][j] = sigs.sig[i - 1].var[j - 1][k - 1]; + for (j = 0; j < bands; j++) { + mu[i][j] = sigs.sig[i].mean[j]; + for (k = 0; k <= j; k++) { + cov[i][j][k] = cov[i][k][j] = sigs.sig[i].var[j][k]; + } } } @@ -200,14 +201,13 @@ int main(int argc, char *argv[]) } /* open the cell maps */ - for (i = 1; i <= bands; i++) { + for (i = 0; i < bands; i++) { outbandmax[i] = (CELL)0; outbandmin[i] = (CELL)0; - datafds[i] = - Rast_open_old(refs.file[i - 1].name, refs.file[i - 1].mapset); + datafds[i] = Rast_open_old(refs.file[i].name, refs.file[i].mapset); - sprintf(tempname, "%s.%d", out_opt->answer, i); + sprintf(tempname, "%s.%d", out_opt->answer, i + 1); outfds[i] = Rast_open_c_new(tempname); } @@ -219,7 +219,7 @@ int main(int argc, char *argv[]) Rast_init_colors(&color_tbl); /* close the cell maps */ - for (i = 1; i <= bands; i++) { + for (i = 0; i < bands; i++) { Rast_close(datafds[i]); Rast_close(outfds[i]); diff --git a/imagery/i.cca/testsuite/test_i_cca.py b/imagery/i.cca/testsuite/test_i_cca.py new file mode 100644 index 00000000000..4049274e5f8 --- /dev/null +++ b/imagery/i.cca/testsuite/test_i_cca.py @@ -0,0 +1,92 @@ +""" +Name: i.cca tests +Purpose: Test correctness of generated outputs + +Author: Maris Nartiss +Copyright: (C) 2023 by Maris Nartiss 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. +""" + +from grass.script.core import tempname + +from grass.gunittest.case import TestCase +from grass.gunittest.main import test + + +class OutputMatchTest(TestCase): + """ + Compare values to output generated by pre-7.0 version of the module. + Comparison values were obtained with pre-ccmath rewrite version + 664305e4b8de924c8dfb5385e8bf6c18fbf649be + """ + + @classmethod + def setUpClass(cls): + """Ensures expected computational region and generated data""" + cls.use_temp_region() + cls.runModule("g.region", raster="lsat7_2000_20") + cls.group_name = tempname(10) + cls.subgroup_name = "vis" + cls.runModule( + "i.group", + group=cls.group_name, + subgroup=cls.subgroup_name, + input="lsat7_2000_20,lsat7_2000_30,lsat7_2000_40", + ) + cls.rasters = [] + cls.signatures = [] + + @classmethod + def tearDownClass(cls): + """Remove the temporary region and generated data""" + cls.del_temp_region() + cls.runModule( + "g.remove", flags="f", type="group", name=cls.group_name, quiet=True + ) + for r in cls.rasters: + cls.runModule("g.remove", flags="f", type="raster", name=r, quiet=True) + for s in cls.signatures: + cls.runModule("i.signatures", type="sig", remove=s, quiet=True) + + def test_output_values(self): + """Test correctness of i.cca output""" + sig_name = tempname(10) + self.assertModule( + "i.cluster", + classes=4, + group=self.group_name, + subgroup=self.subgroup_name, + signaturefile=sig_name, + quiet=True, + ) + self.signatures.append(sig_name) + out_prefix = tempname(10) + self.assertModule( + "i.cca", + group=self.group_name, + subgroup=self.subgroup_name, + signature=sig_name, + output=out_prefix, + quiet=True, + ) + self.assertRasterExists(f"{out_prefix}.1") + self.rasters.append(f"{out_prefix}.1") + self.assertRasterExists(f"{out_prefix}.2") + self.rasters.append(f"{out_prefix}.2") + self.assertRasterExists(f"{out_prefix}.3") + self.rasters.append(f"{out_prefix}.3") + self.assertRasterMinMax( + map=f"{out_prefix}.1", refmin=-10, refmax=9, msg="Wrong calculated value" + ) + self.assertRasterMinMax( + map=f"{out_prefix}.2", refmin=-34, refmax=-3, msg="Wrong calculated value" + ) + self.assertRasterMinMax( + map=f"{out_prefix}.3", refmin=-24, refmax=5, msg="Wrong calculated value" + ) + + +if __name__ == "__main__": + test()