source: grass-addons/grass7/raster/r.forestfrag/testsuite/r_forestfrag_trivial.py

Last change on this file was 68752, checked in by neteler, 8 years ago

svn propset

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-python
File size: 4.6 KB
Line 
1#!/usr/bin/env python
2
3############################################################################
4#
5# MODULE: r_forestfrag_trivial
6# AUTHOR: Vaclav Petras
7# PURPOSE: Test using example from the Riitters et al. 2000 paper
8# COPYRIGHT: (C) 2016 by Vaclav Petras and the GRASS Development Team
9#
10# This program is free software under the GNU General Public
11# License (>=v2). Read the file COPYING that comes with GRASS
12# for details.
13#
14#############################################################################
15
16# This test led to discovery of #3067 (r68717) which was an r.mapcalc
17# row indexing bug.
18
19from grass.gunittest.case import TestCase
20from grass.gunittest.main import test
21
22
23FOREST_RIITTERS = """\
24north: 10
25south: 8
26east: 20
27west: 18
28rows: 3
29cols: 3
301 0 1
311 1 0
321 1 0
33"""
34
35FRAG_RIITTERS = """\
36north: 10
37south: 8
38east: 20
39west: 18
40rows: 3
41cols: 3
42null: N
43N N N
44N 4 N
45N N N
46"""
47
48PF_RIITTERS = """\
49north: 10
50south: 8
51east: 20
52west: 18
53rows: 3
54cols: 3
55null: N
56N N N
57N 0.67 N
58N N N
59"""
60
61PFF_RIITTERS = """\
62north: 10
63south: 8
64east: 20
65west: 18
66rows: 3
67cols: 3
68null: N
69N N N
70N 0.45 N
71N N N
72"""
73
74
75class TestForestFragTrivial(TestCase):
76 # TODO: replace by unified handing of maps
77 to_remove = []
78 forest = 'rff_test_forest_trivial'
79 forest_frag = 'rff_test_forest_frag_trivial'
80 forest_frag_ref = 'rff_test_forest_frag_trivial_ref'
81
82 def setUp(self):
83 self.use_temp_region()
84
85 def tearDown(self):
86 self.del_temp_region()
87 if 0 and self.to_remove:
88 self.runModule('g.remove', flags='f', type='raster',
89 name=','.join(self.to_remove), verbose=True)
90
91 def forest_frag_general(self, forest, window, reference):
92 self.runModule('r.in.ascii', input='-', stdin_=forest,
93 output=self.forest)
94 self.to_remove.append(self.forest)
95 self.runModule('g.region', raster=self.forest)
96 self.assertRasterMinMax(self.forest,
97 refmin=0, refmax=1)
98 self.runModule('r.in.ascii', input='-', stdin_=reference,
99 output=self.forest_frag_ref)
100 self.to_remove.append(self.forest_frag_ref)
101 pf_ref = self.forest_frag_ref + '_pf'
102 self.runModule('r.in.ascii', input='-', stdin_=PF_RIITTERS,
103 output=pf_ref)
104 self.to_remove.append(pf_ref)
105 pff_ref = self.forest_frag_ref + '_pff'
106 self.runModule('r.in.ascii', input='-', stdin_=PFF_RIITTERS,
107 output=pff_ref)
108 self.to_remove.append(pff_ref)
109 # just check if the reference is all right
110 theoretical_min = 0
111 theoretical_max = 6
112 self.assertRasterMinMax(self.forest_frag_ref,
113 refmin=theoretical_min,
114 refmax=theoretical_max)
115 ref_univar = dict(null_cells=8)
116 self.assertRasterFitsUnivar(raster=self.forest_frag_ref,
117 reference=ref_univar, precision=0)
118
119 # actually run the module
120 pf = self.forest_frag + '_pf'
121 pff = self.forest_frag + '_pff'
122 self.assertModule('r.forestfrag', input=self.forest,
123 output=self.forest_frag, window=window,
124 pf=pf, pff=pff)
125 self.assertRasterExists(self.forest_frag)
126 self.to_remove.append(self.forest_frag)
127 self.assertRasterExists(pf)
128 self.to_remove.append(pf)
129 self.assertRasterExists(pff)
130 self.to_remove.append(pff)
131
132 # check the basic properties
133 self.assertRasterMinMax(self.forest_frag,
134 refmin=theoretical_min,
135 refmax=theoretical_max)
136 ref_univar = dict(null_cells=0)
137 self.assertRasterFitsUnivar(raster=self.forest_frag,
138 reference=ref_univar, precision=0)
139
140 self.runModule('g.region', zoom=pff_ref)
141
142 # check cell by cell
143
144 self.assertRastersNoDifference(actual=pf,
145 reference=pf_ref,
146 precision=0.01)
147
148 self.assertRastersNoDifference(actual=pff,
149 reference=pff_ref,
150 precision=0.01)
151
152 self.assertRastersNoDifference(actual=self.forest_frag,
153 reference=self.forest_frag_ref,
154 precision=0) # it's CELL type
155
156
157 def test_riitters(self):
158 self.forest_frag_general(FOREST_RIITTERS, 3, FRAG_RIITTERS)
159
160
161if __name__ == '__main__':
162 test()
Note: See TracBrowser for help on using the repository browser.