From 82bf3ed435f55d2af05baf8a07d4677da03718eb Mon Sep 17 00:00:00 2001 From: cyliang368 Date: Sun, 25 Aug 2024 13:10:49 -0400 Subject: [PATCH 1/4] create unit tests for r.proj --- raster/r.proj/testsuite/test_rproj.py | 179 ++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 raster/r.proj/testsuite/test_rproj.py diff --git a/raster/r.proj/testsuite/test_rproj.py b/raster/r.proj/testsuite/test_rproj.py new file mode 100644 index 00000000000..cebde9d571e --- /dev/null +++ b/raster/r.proj/testsuite/test_rproj.py @@ -0,0 +1,179 @@ +""" +Name: r.texture test +Purpose: Tests r.texture and its flags/options. + +Author: Sunveer Singh, Google Code-in 2017 + Chung-Yuan Liang, modified in 2024 +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. +""" + +from grass.gunittest.case import TestCase +from grass.gunittest.checkers import text_to_keyvalue +from grass.gunittest.gmodules import call_module +import os + +raster_info = """north=35.8096296297222 +south=35.6874074075 +east=-78.608 +west=-78.7746666666667 +nsres=0.000740740740740727 +ewres=0.000666666666666686 +rows=165 +cols=250 +cells=41250""" + + +class TestRasterreport(TestCase): + input = "elevation" + + @classmethod + def setUpClass(cls): + cls.runModule("g.proj", project="nc_latlong", epsg="4326", flags="c") + cls.runModule("g.mapset", mapset="PERMANENT", project="nc_latlong") + + @classmethod + def tearDownClass(cls): + dbase = call_module("g.gisenv", get="GISDBASE") + os.system(f"rm -rf {dbase}/nc_latlong") + + def run_rproj_test(self, method, statics): + """The main function to run r.proj check rsults according to the method + + Parameters + ---------- + method : str + The method to be used for r.proj + statics : str + The expected statics of the output raster + """ + output = method + ## Get the boundary and set up region for the projected map + stdout = call_module( + "r.proj", + project="nc_spm_08_grass7", + mapset="PERMANENT", + input=self.input, + method=method, + flags="g", + ) + settings = dict([line.split("=") for line in stdout.split()]) + + call_module( + "g.region", + n=settings["n"], + s=settings["s"], + e=settings["e"], + w=settings["w"], + rows=settings["rows"], + cols=settings["cols"], + flags="a", + res=1, + ) + + ## Project the map + self.assertModule( + "r.proj", + project="nc_spm_08_grass7", + mapset="PERMANENT", + input=self.input, + output=output, + method=method, + quiet=True, + ) + + ## Validate the output + self.assertRasterFitsUnivar(output, reference=statics, precision=1e-2) + self.assertRasterFitsInfo(output, reference=raster_info, precision=1e-2) + + def test_nearest(self): + """Testing method nearest""" + ## Set up variables and validation values + method = "nearest" + statics = """n=40929 + min=55.5787925720215 + max=156.038833618164 + mean=110.377588991481 + variance=412.79107041757""" + + self.run_rproj_test(method, statics) + + def test_bilinear(self): + """Testing method bilinear""" + ## Set up variables and validation values + method = "bilinear" + statics = """n=40846 + min=56.4586868286133 + max=156.053405761719 + mean=110.388441504306 + variance=411.490915468646""" + + self.run_rproj_test(method, statics) + + def test_bicubic(self): + """Testing method bicubic""" + ## Set up variables and validation values + method = "bicubic" + statics = """n=40678 + min=56.2927856445312 + max=156.06169128418 + mean=110.417365826772 + variance=411.302683091198""" + + self.run_rproj_test(method, statics) + + def test_lanczos(self): + """Testing method lanczos""" + ## Set up variables and validation values + method = "lanczos" + statics = """n=40587 + min=56.2883224487305 + max=156.066925048828 + mean=110.423209871101 + variance=411.631827344207""" + + self.run_rproj_test(method, statics) + + def test_bilinear_f(self): + """Testing method bilinear_f""" + ## Set up variables and validation values + method = "bilinear_f" + statics = """n=40929 + min=55.5787925720215 + max=156.053405761719 + mean=110.376596053808 + variance=412.568369942694""" + + self.run_rproj_test(method, statics) + + def test_bicubic_f(self): + """Testing method bicubic_f""" + ## Set up variables and validation values + method = "bicubic_f" + statics = """n=40929 + min=55.5787925720215 + max=156.06169128418 + mean=110.37642902228 + variance=412.706934718806""" + + self.run_rproj_test(method, statics) + + def test_lanczos_f(self): + """Testing method lanczos_f""" + ## Set up variables and validation values + method = "lanczos_f" + statics = """n=40929 + min=55.5787925720215 + max=156.066925048828 + mean=110.376264598194 + variance=412.710534286582""" + + self.run_rproj_test(method, statics) + + +if __name__ == "__main__": + from grass.gunittest.main import test + + test() From 5a7d0eb57de7a828b4e5ccf0fb8b1d818324bece Mon Sep 17 00:00:00 2001 From: cyliang368 Date: Sun, 25 Aug 2024 14:24:58 -0400 Subject: [PATCH 2/4] change the source project --- raster/r.proj/testsuite/test_rproj.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/raster/r.proj/testsuite/test_rproj.py b/raster/r.proj/testsuite/test_rproj.py index cebde9d571e..34885effd60 100644 --- a/raster/r.proj/testsuite/test_rproj.py +++ b/raster/r.proj/testsuite/test_rproj.py @@ -25,6 +25,8 @@ cols=250 cells=41250""" +src_project = "nc_spm_full_v2alpha2" + class TestRasterreport(TestCase): input = "elevation" @@ -53,7 +55,7 @@ def run_rproj_test(self, method, statics): ## Get the boundary and set up region for the projected map stdout = call_module( "r.proj", - project="nc_spm_08_grass7", + project=src_project, mapset="PERMANENT", input=self.input, method=method, @@ -76,7 +78,7 @@ def run_rproj_test(self, method, statics): ## Project the map self.assertModule( "r.proj", - project="nc_spm_08_grass7", + project=src_project, mapset="PERMANENT", input=self.input, output=output, From 861270bdf5dc7145197159de79d9c89db68e1e62 Mon Sep 17 00:00:00 2001 From: cyliang368 Date: Sun, 25 Aug 2024 16:32:59 -0400 Subject: [PATCH 3/4] update the validation values according to the map in the project --- raster/r.proj/testsuite/test_rproj.py | 68 ++++++++++++++------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/raster/r.proj/testsuite/test_rproj.py b/raster/r.proj/testsuite/test_rproj.py index 34885effd60..5d45bf1d228 100644 --- a/raster/r.proj/testsuite/test_rproj.py +++ b/raster/r.proj/testsuite/test_rproj.py @@ -26,6 +26,7 @@ cells=41250""" src_project = "nc_spm_full_v2alpha2" +dst_project = "nc_latlong" class TestRasterreport(TestCase): @@ -33,13 +34,14 @@ class TestRasterreport(TestCase): @classmethod def setUpClass(cls): - cls.runModule("g.proj", project="nc_latlong", epsg="4326", flags="c") - cls.runModule("g.mapset", mapset="PERMANENT", project="nc_latlong") + cls.runModule("g.proj", project=dst_project, epsg="4326", flags="c") + cls.runModule("g.mapset", mapset="PERMANENT", project=dst_project) @classmethod def tearDownClass(cls): + cls.runModule("g.mapset", mapset="PERMANENT", project=src_project) dbase = call_module("g.gisenv", get="GISDBASE") - os.system(f"rm -rf {dbase}/nc_latlong") + os.system(f"rm -rf {dbase}/{dst_project}") def run_rproj_test(self, method, statics): """The main function to run r.proj check rsults according to the method @@ -94,11 +96,11 @@ def test_nearest(self): """Testing method nearest""" ## Set up variables and validation values method = "nearest" - statics = """n=40929 + statics = """n=40930 min=55.5787925720215 max=156.038833618164 - mean=110.377588991481 - variance=412.79107041757""" + mean=110.377538633405 + variance=412.751942806146""" self.run_rproj_test(method, statics) @@ -106,11 +108,11 @@ def test_bilinear(self): """Testing method bilinear""" ## Set up variables and validation values method = "bilinear" - statics = """n=40846 - min=56.4586868286133 - max=156.053405761719 - mean=110.388441504306 - variance=411.490915468646""" + statics = """n=40845 + min=56.3932914733887 + max=156.053298950195 + mean=110.389074372679 + variance=411.487781666933""" self.run_rproj_test(method, statics) @@ -118,11 +120,11 @@ def test_bicubic(self): """Testing method bicubic""" ## Set up variables and validation values method = "bicubic" - statics = """n=40678 - min=56.2927856445312 - max=156.06169128418 - mean=110.417365826772 - variance=411.302683091198""" + statics = """n=40677 + min=56.2407836914062 + max=156.061599731445 + mean=110.41701776258 + variance=411.382636894393""" self.run_rproj_test(method, statics) @@ -130,11 +132,11 @@ def test_lanczos(self): """Testing method lanczos""" ## Set up variables and validation values method = "lanczos" - statics = """n=40587 - min=56.2883224487305 - max=156.066925048828 - mean=110.423209871101 - variance=411.631827344207""" + statics = """n=40585 + min=56.2350921630859 + max=156.066345214844 + mean=110.421826400841 + variance=411.6875834341575""" self.run_rproj_test(method, statics) @@ -142,11 +144,11 @@ def test_bilinear_f(self): """Testing method bilinear_f""" ## Set up variables and validation values method = "bilinear_f" - statics = """n=40929 + statics = """n=40930 min=55.5787925720215 - max=156.053405761719 - mean=110.376596053808 - variance=412.568369942694""" + max=156.053298950195 + mean=110.376211041027 + variance=412.553041205029""" self.run_rproj_test(method, statics) @@ -154,11 +156,11 @@ def test_bicubic_f(self): """Testing method bicubic_f""" ## Set up variables and validation values method = "bicubic_f" - statics = """n=40929 + statics = """n=40930 min=55.5787925720215 - max=156.06169128418 - mean=110.37642902228 - variance=412.706934718806""" + max=156.061599731445 + mean=110.375897704515 + variance=412.693308000461""" self.run_rproj_test(method, statics) @@ -166,11 +168,11 @@ def test_lanczos_f(self): """Testing method lanczos_f""" ## Set up variables and validation values method = "lanczos_f" - statics = """n=40929 + statics = """n=40930 min=55.5787925720215 - max=156.066925048828 - mean=110.376264598194 - variance=412.710534286582""" + max=156.066345214844 + mean=110.375715222838 + variance=412.695433658258""" self.run_rproj_test(method, statics) From 7980a8b3ca8c7823d2cfe08126143ed5af104d27 Mon Sep 17 00:00:00 2001 From: cyliang368 Date: Tue, 27 Aug 2024 18:16:45 -0400 Subject: [PATCH 4/4] remove shell cmd, update doc, modify precision --- raster/r.proj/testsuite/test_rproj.py | 35 +++++++++++++++------------ 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/raster/r.proj/testsuite/test_rproj.py b/raster/r.proj/testsuite/test_rproj.py index 5d45bf1d228..5f3834be881 100644 --- a/raster/r.proj/testsuite/test_rproj.py +++ b/raster/r.proj/testsuite/test_rproj.py @@ -1,19 +1,22 @@ -""" -Name: r.texture test -Purpose: Tests r.texture and its flags/options. - -Author: Sunveer Singh, Google Code-in 2017 - Chung-Yuan Liang, modified in 2024 -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. -""" +#!/usr/bin/env python + +############################################################################## +# MODULE: r.proj +# +# AUTHOR(S): Chung-Yuan Liang +# +# PURPOSE: Unit tests for r.proj +# +# COPYRIGHT: (C) 2024 Chung-Yuan Liang and the GRASS Development Team +# +# This program is free software under the GNU General Public +# License (>=v2). Read the file COPYING that comes with GRASS +# for details. +############################################################################## from grass.gunittest.case import TestCase -from grass.gunittest.checkers import text_to_keyvalue from grass.gunittest.gmodules import call_module -import os +import shutil raster_info = """north=35.8096296297222 south=35.6874074075 @@ -41,7 +44,7 @@ def setUpClass(cls): def tearDownClass(cls): cls.runModule("g.mapset", mapset="PERMANENT", project=src_project) dbase = call_module("g.gisenv", get="GISDBASE") - os.system(f"rm -rf {dbase}/{dst_project}") + shutil.rmtree(f"{dbase}/{dst_project}") def run_rproj_test(self, method, statics): """The main function to run r.proj check rsults according to the method @@ -89,8 +92,8 @@ def run_rproj_test(self, method, statics): ) ## Validate the output - self.assertRasterFitsUnivar(output, reference=statics, precision=1e-2) - self.assertRasterFitsInfo(output, reference=raster_info, precision=1e-2) + self.assertRasterFitsUnivar(output, reference=statics, precision=1e-7) + self.assertRasterFitsInfo(output, reference=raster_info, precision=1e-7) def test_nearest(self): """Testing method nearest"""