Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i.pca: added tests for the module #3550

Merged
merged 8 commits into from
Apr 2, 2024
Merged

Conversation

HamedElgizery
Copy link
Contributor

This PR adds a test suite using gunittest for the i.pca module.

i.pca is tested against the sample data used in CI.

The expected results (from r.info and r.univar) are saved in the data directory, and are used to validate the correctness of the module.

imagery/i.pca/testsuite/test_pca.py Show resolved Hide resolved
imagery/i.pca/testsuite/test_pca.py Outdated Show resolved Hide resolved
imagery/i.pca/testsuite/test_pca.py Outdated Show resolved Hide resolved
imagery/i.pca/testsuite/test_pca.py Outdated Show resolved Hide resolved
imagery/i.pca/testsuite/test_pca.py Outdated Show resolved Hide resolved
imagery/i.pca/testsuite/test_pca.py Outdated Show resolved Hide resolved
imagery/i.pca/testsuite/test_pca.py Outdated Show resolved Hide resolved
imagery/i.pca/testsuite/test_pca.py Outdated Show resolved Hide resolved
Copy link
Member

@ninsbl ninsbl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! In general, this looks very good, and I know it is more than skill-tests for GSoC in other projects.
Nevertheless, I made some suggestions for changes, Please have a look at them.

Most importantly, please re-format the added Pyhton file using black. See: https://github.com/OSGeo/grass/actions/runs/8512975348/job/23315779012#step:8:257 for how it is used in CI. If the file does not comply to black formating standards for Python, CI will fail and the PR cannot be merged,

You can install black with: pip install black==23.1.0

imagery/i.pca/testsuite/data/lsat7_2002_pca_1_univar.out Outdated Show resolved Hide resolved
imagery/i.pca/testsuite/test_pca.py Outdated Show resolved Hide resolved
imagery/i.pca/testsuite/test_pca.py Outdated Show resolved Hide resolved
imagery/i.pca/testsuite/test_pca.py Outdated Show resolved Hide resolved
imagery/i.pca/testsuite/test_pca.py Outdated Show resolved Hide resolved
@ninsbl ninsbl added this to the 8.4.0 milestone Apr 1, 2024
@ninsbl ninsbl self-assigned this Apr 1, 2024
imagery/i.pca/testsuite/test_pca.py Outdated Show resolved Hide resolved
imagery/i.pca/testsuite/test_pca.py Show resolved Hide resolved
@HamedElgizery
Copy link
Contributor Author

HamedElgizery commented Apr 2, 2024

@ninsbl
I have updated the script, reformatted it with black, and removed the data directory.

ninsbl
ninsbl previously approved these changes Apr 2, 2024
Copy link
Member

@ninsbl ninsbl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me...

imagery/i.pca/testsuite/test_pca.py Outdated Show resolved Hide resolved
@ninsbl ninsbl merged commit 315f7ef into OSGeo:main Apr 2, 2024
26 checks passed
@neteler
Copy link
Member

neteler commented Apr 2, 2024

I just tried it locally, runs ok but yields a SyntaxWarning: invalid escape sequence in line 39:

GRASS nc_spm_08_grass7/user1:testsuite > python test_pca.py 
/home/mneteler/software/grass_main/imagery/i.pca/testsuite/test_pca.py:39: SyntaxWarning: invalid escape sequence '\,'
  lsat7_2002_pca_info_out = """north=228513
PC1   4334.35 ( 0.2824, 0.3342, 0.5092,-0.0087, 0.5264, 0.5217) [83.04%]
PC2    588.31 ( 0.2541, 0.1885, 0.2923,-0.7428,-0.5110,-0.0403) [11.27%]
PC3    239.22 ( 0.3801, 0.3819, 0.2681, 0.6238,-0.4000,-0.2980) [ 4.58%]
PC4     32.85 ( 0.1752,-0.0191,-0.4053, 0.1593,-0.4435, 0.7632) [ 0.63%]
PC5     20.73 (-0.6170,-0.2514, 0.6059, 0.1734,-0.3235, 0.2330) [ 0.40%]
PC6      4.08 (-0.5475, 0.8021,-0.2282,-0.0607,-0.0208, 0.0252) [ 0.08%]

Computing covariance matrix...
 100%
Calculating principal components...
 100%
Rescaling to range 0,255...
 100%
Eigen values, (vectors), and [percent importance]:

.
----------------------------------------------------------------------
Ran 1 test in 4.239s
OK

@lucadelu
Copy link
Contributor

lucadelu commented Apr 2, 2024

I didn't get it with python 3.11

GRASS nc_spm_08_grass7/user1:~ > python /tmp/test_pca.py 
PC1   4334.35 ( 0.2824, 0.3342, 0.5092,-0.0087, 0.5264, 0.5217) [83.04%]
PC2    588.31 ( 0.2541, 0.1885, 0.2923,-0.7428,-0.5110,-0.0403) [11.27%]
PC3    239.22 ( 0.3801, 0.3819, 0.2681, 0.6238,-0.4000,-0.2980) [ 4.58%]
PC4     32.85 ( 0.1752,-0.0191,-0.4053, 0.1593,-0.4435, 0.7632) [ 0.63%]
PC5     20.73 (-0.6170,-0.2514, 0.6059, 0.1734,-0.3235, 0.2330) [ 0.40%]
PC6      4.08 (-0.5475, 0.8021,-0.2282,-0.0607,-0.0208, 0.0252) [ 0.08%]

Computing covariance matrix...
 100%
Calculating principal components...
 100%
Rescaling to range 0,255...
 100%
Eigen values, (vectors), and [percent importance]:

.
----------------------------------------------------------------------
Ran 1 test in 2.570s
OK

@nilason
Copy link
Contributor

nilason commented Apr 2, 2024

I just tried it locally, runs ok but yields a SyntaxWarning: invalid escape sequence in line 39:

GRASS nc_spm_08_grass7/user1:testsuite > python test_pca.py 
/home/mneteler/software/grass_main/imagery/i.pca/testsuite/test_pca.py:39: SyntaxWarning: invalid escape sequence '\,'
  lsat7_2002_pca_info_out = """north=228513
PC1   4334.35 ( 0.2824, 0.3342, 0.5092,-0.0087, 0.5264, 0.5217) [83.04%]

This is likely a Python 3.12 issue.

Markus, please test with adding an r before triple quote (file test_pca.py:39):

 lsat7_2002_pca_info_out = r"""north=228513

@neteler
Copy link
Member

neteler commented Apr 2, 2024

Yes, python 3.12 here:

python --version
Python 3.12.2

The warning then disappears but it then fails:

GRASS nc_spm_08_grass7/user1:testsuite > python test_pca.py
PC1   4334.35 ( 0.2824, 0.3342, 0.5092,-0.0087, 0.5264, 0.5217) [83.04%]
PC2    588.31 ( 0.2541, 0.1885, 0.2923,-0.7428,-0.5110,-0.0403) [11.27%]
PC3    239.22 ( 0.3801, 0.3819, 0.2681, 0.6238,-0.4000,-0.2980) [ 4.58%]
PC4     32.85 ( 0.1752,-0.0191,-0.4053, 0.1593,-0.4435, 0.7632) [ 0.63%]
PC5     20.73 (-0.6170,-0.2514, 0.6059, 0.1734,-0.3235, 0.2330) [ 0.40%]
PC6      4.08 (-0.5475, 0.8021,-0.2282,-0.0607,-0.0208, 0.0252) [ 0.08%]

Computing covariance matrix...
 100%
Calculating principal components...
 100%
Rescaling to range 0,255...
 100%
Eigen values, (vectors), and [percent importance]:

F
======================================================================
FAIL: test_pca_sample (__main__.TestReport.test_pca_sample)
Testing pca sample
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/mneteler/software/grass_main/imagery/i.pca/testsuite/test_pca.py", line 153, in test_pca_sample
    self.assertRasterFitsInfo(
  File "/home/mneteler/software/grass_main/dist.x86_64-pc-linux-gnu/etc/python/grass/gunittest/case.py", line 315, in assertRasterFitsInfo
    self.assertModuleKeyValue(
  File "/home/mneteler/software/grass_main/dist.x86_64-pc-linux-gnu/etc/python/grass/gunittest/case.py", line 271, in assertModuleKeyValue
    self.fail(self._formatMessage(msg, stdMsg))
AssertionError: r.info map=lsat7_2002_pca.1 -g -r -e difference:
mismatch values (key, reference, actual): [('comments', ['\\"Eigen values', '(vectors)', 'and [percent importance]:PC1   4334.35 ( 0.2824', 0.3342, 0.5092, -0.0087, 0.5264, '0.5217) [83.04%]PC2    588.31 ( 0.2541', 0.1885, 0.2923, -0.7428, -0.511, '-0.0403) [11.27%]PC3    239.22 ( 0.3801', 0.3819, 0.2681, 0.6238, -0.4, '-0.2980) [ 4.58%]PC4     32.85 ( 0.1752', -0.0191, -0.4053, 0.1593, -0.4435, '0.7632) [ 0.63%]PC5     20.73 (-0.6170', -0.2514, 0.6059, 0.1734, -0.3235, '0.2330) [ 0.40%]PC6      4.08 (-0.5475', 0.8021, -0.2282, -0.0607, -0.0208, '0.0252) [ 0.08%]i.pca input=\\"lsat7_2002_10', 'lsat7_2002_20', 'lsat7_2002_30', 'lsat7_2002_40\\', 'lsat7_2002_50', 'lsat7_2002_70\\" output=\\"lsat7_2002_pca\\" rescale=0', '255 \\percent=99"'], ['"Eigen values', '(vectors)', 'and [percent importance]:PC1   4334.35 ( 0.2824', 0.3342, 0.5092, -0.0087, 0.5264, '0.5217) [83.04%]PC2    588.31 ( 0.2541', 0.1885, 0.2923, -0.7428, -0.511, '-0.0403) [11.27%]PC3    239.22 ( 0.3801', 0.3819, 0.2681, 0.6238, -0.4, '-0.2980) [ 4.58%]PC4     32.85 ( 0.1752', -0.0191, -0.4053, 0.1593, -0.4435, '0.7632) [ 0.63%]PC5     20.73 (-0.6170', -0.2514, 0.6059, 0.1734, -0.3235, '0.2330) [ 0.40%]PC6      4.08 (-0.5475', 0.8021, -0.2282, -0.0607, -0.0208, '0.0252) [ 0.08%]i.pca input="lsat7_2002_10', 'lsat7_2002_20', 'lsat7_2002_30', 'lsat7_2002_40\\', 'lsat7_2002_50', 'lsat7_2002_70" output="lsat7_2002_pca" rescale=0', '255 \\percent=99"'])]
command: r.info map=lsat7_2002_pca.1 -g -r -e {'map': 'lsat7_2002_pca.1', 'flags': 'gre'}

----------------------------------------------------------------------
Ran 1 test in 1.190s
FAILED (failures=1)

@HamedElgizery
Copy link
Contributor Author

The warning then disappears but it then fails:

GRASS nc_spm_08_grass7/user1:testsuite > python test_pca.py
PC1   4334.35 ( 0.2824, 0.3342, 0.5092,-0.0087, 0.5264, 0.5217) [83.04%]
PC2    588.31 ( 0.2541, 0.1885, 0.2923,-0.7428,-0.5110,-0.0403) [11.27%]
PC3    239.22 ( 0.3801, 0.3819, 0.2681, 0.6238,-0.4000,-0.2980) [ 4.58%]
PC4     32.85 ( 0.1752,-0.0191,-0.4053, 0.1593,-0.4435, 0.7632) [ 0.63%]
PC5     20.73 (-0.6170,-0.2514, 0.6059, 0.1734,-0.3235, 0.2330) [ 0.40%]
PC6      4.08 (-0.5475, 0.8021,-0.2282,-0.0607,-0.0208, 0.0252) [ 0.08%]

Computing covariance matrix...
 100%
Calculating principal components...
 100%
Rescaling to range 0,255...
 100%
Eigen values, (vectors), and [percent importance]:

F
======================================================================
FAIL: test_pca_sample (__main__.TestReport.test_pca_sample)
Testing pca sample
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/mneteler/software/grass_main/imagery/i.pca/testsuite/test_pca.py", line 153, in test_pca_sample
    self.assertRasterFitsInfo(
  File "/home/mneteler/software/grass_main/dist.x86_64-pc-linux-gnu/etc/python/grass/gunittest/case.py", line 315, in assertRasterFitsInfo
    self.assertModuleKeyValue(
  File "/home/mneteler/software/grass_main/dist.x86_64-pc-linux-gnu/etc/python/grass/gunittest/case.py", line 271, in assertModuleKeyValue
    self.fail(self._formatMessage(msg, stdMsg))
AssertionError: r.info map=lsat7_2002_pca.1 -g -r -e difference:
mismatch values (key, reference, actual): [('comments', ['\\"Eigen values', '(vectors)', 'and [percent importance]:PC1   4334.35 ( 0.2824', 0.3342, 0.5092, -0.0087, 0.5264, '0.5217) [83.04%]PC2    588.31 ( 0.2541', 0.1885, 0.2923, -0.7428, -0.511, '-0.0403) [11.27%]PC3    239.22 ( 0.3801', 0.3819, 0.2681, 0.6238, -0.4, '-0.2980) [ 4.58%]PC4     32.85 ( 0.1752', -0.0191, -0.4053, 0.1593, -0.4435, '0.7632) [ 0.63%]PC5     20.73 (-0.6170', -0.2514, 0.6059, 0.1734, -0.3235, '0.2330) [ 0.40%]PC6      4.08 (-0.5475', 0.8021, -0.2282, -0.0607, -0.0208, '0.0252) [ 0.08%]i.pca input=\\"lsat7_2002_10', 'lsat7_2002_20', 'lsat7_2002_30', 'lsat7_2002_40\\', 'lsat7_2002_50', 'lsat7_2002_70\\" output=\\"lsat7_2002_pca\\" rescale=0', '255 \\percent=99"'], ['"Eigen values', '(vectors)', 'and [percent importance]:PC1   4334.35 ( 0.2824', 0.3342, 0.5092, -0.0087, 0.5264, '0.5217) [83.04%]PC2    588.31 ( 0.2541', 0.1885, 0.2923, -0.7428, -0.511, '-0.0403) [11.27%]PC3    239.22 ( 0.3801', 0.3819, 0.2681, 0.6238, -0.4, '-0.2980) [ 4.58%]PC4     32.85 ( 0.1752', -0.0191, -0.4053, 0.1593, -0.4435, '0.7632) [ 0.63%]PC5     20.73 (-0.6170', -0.2514, 0.6059, 0.1734, -0.3235, '0.2330) [ 0.40%]PC6      4.08 (-0.5475', 0.8021, -0.2282, -0.0607, -0.0208, '0.0252) [ 0.08%]i.pca input="lsat7_2002_10', 'lsat7_2002_20', 'lsat7_2002_30', 'lsat7_2002_40\\', 'lsat7_2002_50', 'lsat7_2002_70" output="lsat7_2002_pca" rescale=0', '255 \\percent=99"'])]
command: r.info map=lsat7_2002_pca.1 -g -r -e {'map': 'lsat7_2002_pca.1', 'flags': 'gre'}

----------------------------------------------------------------------
Ran 1 test in 1.190s
FAILED (failures=1)

It is probably because of the quotations inside the string...

Can you try to change the variable "lsat7_2002_pca_info_out" to:

    lsat7_2002_pca_info_out = r"""north=228513
        south=214975.5
        east=645012
        west=629992.5
        nsres=28.5
        ewres=28.5
        rows=475
        cols=527
        cells=250325
        datatype=CELL
        ncats=0
        comments="Eigen values, (vectors), and [percent importance]:PC1   4334.35 ( 0.2824, 0.3342, 0.5092,-0.0087, 0.5264, 0.5217) [83.04%]PC2    588.31 ( 0.2541, 0.1885, 0.2923,-0.7428,-0.5110,-0.0403) [11.27%]PC3    239.22 ( 0.3801, 0.3819, 0.2681, 0.6238,-0.4000,-0.2980) [ 4.58%]PC4     32.85 ( 0.1752,-0.0191,-0.4053, 0.1593,-0.4435, 0.7632) [ 0.63%]PC5     20.73 (-0.6170,-0.2514, 0.6059, 0.1734,-0.3235, 0.2330) [ 0.40%]PC6      4.08 (-0.5475, 0.8021,-0.2282,-0.0607,-0.0208, 0.0252) [ 0.08%]i.pca input="lsat7_2002_10,lsat7_2002_20,lsat7_2002_30,lsat7_2002_40\,lsat7_2002_50,lsat7_2002_70" output="lsat7_2002_pca" rescale=0,255 \percent=99" """

@nilason
Copy link
Contributor

nilason commented Apr 2, 2024

diff --git a/imagery/i.pca/testsuite/test_pca.py b/imagery/i.pca/testsuite/test_pca.py
index 9da7991f87..7999feba8e 100644
--- a/imagery/i.pca/testsuite/test_pca.py
+++ b/imagery/i.pca/testsuite/test_pca.py
@@ -36,7 +36,7 @@ class TestReport(TestCase):
             output="lsat7_2002_pca",
         )
 
-        lsat7_2002_pca_info_out = """north=228513
+        lsat7_2002_pca_info_out = r"""north=228513
             south=214975.5
             east=645012
             west=629992.5
@@ -47,7 +47,7 @@ class TestReport(TestCase):
             cells=250325
             datatype=CELL
             ncats=0
-            comments=\"Eigen values, (vectors), and [percent importance]:PC1   4334.35 ( 0.2824, 0.3342, 0.5092,-0.0087, 0.5264, 0.5217) [83.04%]PC2    588.31 ( 0.2541, 0.1885, 0.2923,-0.7428,-0.5110,-0.0403) [11.27%]PC3    239.22 ( 0.3801, 0.3819, 0.2681, 0.6238,-0.4000,-0.2980) [ 4.58%]PC4     32.85 ( 0.1752,-0.0191,-0.4053, 0.1593,-0.4435, 0.7632) [ 0.63%]PC5     20.73 (-0.6170,-0.2514, 0.6059, 0.1734,-0.3235, 0.2330) [ 0.40%]PC6      4.08 (-0.5475, 0.8021,-0.2282,-0.0607,-0.0208, 0.0252) [ 0.08%]i.pca input=\"lsat7_2002_10,lsat7_2002_20,lsat7_2002_30,lsat7_2002_40\,lsat7_2002_50,lsat7_2002_70\" output=\"lsat7_2002_pca\" rescale=0,255 \percent=99" """
+            comments="Eigen values, (vectors), and [percent importance]:PC1   4334.35 ( 0.2824, 0.3342, 0.5092,-0.0087, 0.5264, 0.5217) [83.04%]PC2    588.31 ( 0.2541, 0.1885, 0.2923,-0.7428,-0.5110,-0.0403) [11.27%]PC3    239.22 ( 0.3801, 0.3819, 0.2681, 0.6238,-0.4000,-0.2980) [ 4.58%]PC4     32.85 ( 0.1752,-0.0191,-0.4053, 0.1593,-0.4435, 0.7632) [ 0.63%]PC5     20.73 (-0.6170,-0.2514, 0.6059, 0.1734,-0.3235, 0.2330) [ 0.40%]PC6      4.08 (-0.5475, 0.8021,-0.2282,-0.0607,-0.0208, 0.0252) [ 0.08%]i.pca input="lsat7_2002_10,lsat7_2002_20,lsat7_2002_30,lsat7_2002_40\,lsat7_2002_50,lsat7_2002_70" output="lsat7_2002_pca" rescale=0,255 \percent=99" """
 
         lsat7_2002_pca_univar_out = [
             """n=250325

works for both 3.12 and 3.11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imagery module Python Related code is in Python tests Related to Test Suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants