11
22import numpy as np
33import pytest
4- import yirgacheffe as yg
54
6- from prepare_layers .make_food_current_map import TileInfo , process_tile , balance_crop_and_pasture_differences , \
5+ from prepare_layers .make_food_current_map import balance_crop_and_pasture_differences , \
76 CROP_CODE , PASTURE_CODE , remove_land_cover , add_land_cover
87
9- @pytest .mark .parametrize ("initial_crop_diff,initial_pasture_diff,expected_crop_diff,expected_pasture_diff" , [
10- (0.0 , 0.0 , 0.0 , 0.0 ),
11- (0.8 , 0.0 , 0.8 , 0.0 ),
12- (- 0.8 , 0.0 , - 0.8 , 0.0 ),
13- (0.0 , 0.8 , 0.0 , 0.8 ),
14- (0.0 , - 0.8 , 0.0 , - 0.8 ),
15- (0.4 , 0.2 , 0.4 , 0.2 ),
16- (- 0.4 , - 0.2 , - 0.4 , - 0.2 ),
17- ])
8+ @pytest .mark .parametrize (
9+ [
10+ "initial_crop_diff" ,
11+ "initial_pasture_diff" ,
12+ "expected_crop_diff" ,
13+ "expected_pasture_diff"
14+ ],
15+ [
16+ (0.0 , 0.0 , 0.0 , 0.0 ),
17+ (0.8 , 0.0 , 0.8 , 0.0 ),
18+ (- 0.8 , 0.0 , - 0.8 , 0.0 ),
19+ (0.0 , 0.8 , 0.0 , 0.8 ),
20+ (0.0 , - 0.8 , 0.0 , - 0.8 ),
21+ (0.4 , 0.2 , 0.4 , 0.2 ),
22+ (- 0.4 , - 0.2 , - 0.4 , - 0.2 ),
23+ ]
24+ )
1825def test_balance_not_needed (
1926 initial_crop_diff : float ,
2027 initial_pasture_diff : float ,
@@ -24,30 +31,35 @@ def test_balance_not_needed(
2431 result_crop_diff , result_pasture_diff = balance_crop_and_pasture_differences (
2532 initial_crop_diff ,
2633 initial_pasture_diff ,
27- dict () ,
34+ {} ,
2835 )
2936 assert expected_crop_diff == result_crop_diff
3037 assert expected_pasture_diff == result_pasture_diff
3138
3239
33- def test_one_sided_balance () -> None :
40+ def test_brokend_balance () -> None :
41+ # Testing that we spot if we've said to remove land where there isn't any
3442 lcc_data_map = {
3543 CROP_CODE : np .zeros ((10 , 10 )),
3644 PASTURE_CODE : np .zeros ((10 , 10 )),
3745 }
38- result_crop_diff , result_pasture_diff = balance_crop_and_pasture_differences (
39- 0.5 ,
40- - 0.1 ,
41- lcc_data_map ,
42- )
43- # assert expected_crop_diff == result_crop_diff
44- # assert expected_pasture_diff == result_pasture_diff
45- # assert (lcc_data_map[CROP_CODE] == crop_cell_value).all()
46- # assert (lcc_data_map[PASTURE_CODE] == pasture_cell_value).all()
46+ with pytest .raises (ValueError ):
47+ _ = balance_crop_and_pasture_differences (
48+ 0.5 ,
49+ - 0.1 ,
50+ lcc_data_map ,
51+ )
4752
4853
4954@pytest .mark .parametrize (
50- "initial_crop_diff,initial_pasture_diff,expected_crop_diff,expected_pasture_diff,crop_cell_value,pasture_cell_value" ,
55+ [
56+ "initial_crop_diff" ,
57+ "initial_pasture_diff" ,
58+ "expected_crop_diff" ,
59+ "expected_pasture_diff" ,
60+ "crop_cell_value" ,
61+ "pasture_cell_value"
62+ ],
5163 [
5264 (0.25 , - 0.5 , 0.0 , - 0.25 , 0.25 , 0.75 ),
5365 ]
@@ -77,7 +89,14 @@ def test_simple_balance_transfer(
7789
7890
7991@pytest .mark .parametrize (
80- "initial_crop_diff,initial_pasture_diff,expected_crop_diff,expected_pasture_diff,crop_cell_value,pasture_cell_value" ,
92+ [
93+ "initial_crop_diff" ,
94+ "initial_pasture_diff" ,
95+ "expected_crop_diff" ,
96+ "expected_pasture_diff" ,
97+ "crop_cell_value" ,
98+ "pasture_cell_value"
99+ ],
81100 [
82101 (0.25 , - 0.5 , 0.0 , - 0.25 , 0.5 , 0.5 ),
83102 (0.5 , - 0.25 , 0.25 , 0.0 , 0.5 , 0.5 ),
@@ -110,9 +129,16 @@ def test_simple_half_balance_transfer(
110129 assert (expected_pasture_map == lcc_data_map [PASTURE_CODE ]).all ()
111130
112131
113- @pytest .mark .parametrize ("crop_diff,expected_crop_cell,expected_other_cell" , [
114- (- 0.5 , 0.5 , 0.5 ),
115- ])
132+ @pytest .mark .parametrize (
133+ [
134+ "crop_diff" ,
135+ "expected_crop_cell" ,
136+ "expected_other_cell" ,
137+ ],
138+ [
139+ (- 0.5 , 0.5 , 0.5 ),
140+ ]
141+ )
116142def test_remove_land_simple (
117143 crop_diff : float ,
118144 expected_crop_cell : float ,
@@ -138,13 +164,22 @@ def test_remove_land_simple(
138164 assert (expected_other_map == lcc_data_map [1 ]).all ()
139165
140166
141- @pytest .mark .parametrize ("crop_diff,pnv_value,expected_crop_cell,expected_other_1_cell,expected_other_2_cell" , [
142- (- 0.5 , 1 , 0.0 , 1.0 , 0.0 ),
143- (- 0.75 , 1 , 0.0 , 1.0 , 0.0 ), # too much
144- (- 0.25 , 1 , 0.5 , 0.5 , 0.0 ),
145- (- 0.5 , 2 , 0.0 , 0.0 , 1.0 ),
146- (- 0.25 , 2 , 0.5 , 0.0 , 0.5 ),
147- ])
167+ @pytest .mark .parametrize (
168+ [
169+ "crop_diff" ,
170+ "pnv_value" ,
171+ "expected_crop_cell" ,
172+ "expected_other_1_cell" ,
173+ "expected_other_2_cell" ,
174+ ],
175+ [
176+ (- 0.5 , 1 , 0.0 , 1.0 , 0.0 ),
177+ (- 0.75 , 1 , 0.0 , 1.0 , 0.0 ), # too much
178+ (- 0.25 , 1 , 0.5 , 0.5 , 0.0 ),
179+ (- 0.5 , 2 , 0.0 , 0.0 , 1.0 ),
180+ (- 0.25 , 2 , 0.5 , 0.0 , 0.5 ),
181+ ]
182+ )
148183def test_remove_land_less_simple (
149184 crop_diff : float ,
150185 pnv_value : int ,
@@ -189,7 +224,6 @@ def test_add_land_simple(
189224 1 : np .ones ((10 , 10 )),
190225 CROP_CODE : np .zeros ((10 , 10 )),
191226 }
192- pnv_data = np .full ((10 , 10 ), 1 )
193227
194228 add_land_cover (
195229 CROP_CODE ,
@@ -217,7 +251,6 @@ def test_add_land_avoid_excluded(
217251 PASTURE_CODE : np .array ([[i % 2 ] * 10 for i in range (10 )]).astype (float ),
218252 CROP_CODE : np .zeros ((10 , 10 )),
219253 }
220- pnv_data = np .full ((10 , 10 ), 1 )
221254
222255 add_land_cover (
223256 CROP_CODE ,
0 commit comments