1- from pathlib import PosixPath
21from typing import Any , Dict , List
32
43import numpy as np
@@ -91,7 +90,7 @@ def test_is_interval() -> None:
9190 ([0 , 5 , - 2 , 40 , np .inf ], ValueError , "Values must be strictly increasing." ),
9291 ],
9392) # type: ignore
94- def test_is_interval_with_error (interval , type_error , match ) -> None :
93+ def test_is_interval_with_error (interval : List [ Any ] , type_error : TypeError , match : str ) -> None :
9594
9695 with pytest .raises (type_error , match = match ):
9796 _ = grouped_stats .is_interval (interval )
@@ -104,28 +103,86 @@ def test_is_interval_with_error(interval, type_error, match) -> None:
104103 ({"slope3" : [0 , 1 , 2 ], "slope4" : [0 , 1 , 2 ]}), # different keys
105104 ],
106105) # type: ignore
107- def test_grouped_stats_errors (slope_raster , aggregated_vars , bins_test , statistics ) -> None :
106+ def test_grouped_stats_errors (
107+ slope_raster : Raster , aggregated_vars : Dict [str , Any ], bins_test : Dict [str , Any ], statistics : List [Any ]
108+ ) -> None :
108109 group_by_test = {"slope1" : slope_raster , "slope2" : slope_raster }
109110 with pytest .raises (ValueError , match = "One bins/mask/segmentation entry per input array required." ):
110111 _ = grouped_stats .grouped_stats (group_by_test , bins_test , aggregated_vars , statistics )
111112
112113
113- def test_grouped_stats_interval (slope_raster : Raster , aggregated_vars : Dict [str , Any ], statistics : List [Any ]) -> None :
114+ def test_grouped_stats_interval (
115+ slope_raster : Raster , aggregated_vars : Dict [str , Any ], statistics : List [Any ], elev_raster : Raster
116+ ) -> None :
114117 group_by_test = {"slope1" : slope_raster }
115118 expected_df = pd .DataFrame (
116119 {
117120 ("raster" , "mean" ): [3 , 8 , 15.5 ],
118121 ("raster" , "min" ): [1 , 6 , 11 ],
119122 },
120123 index = pd .CategoricalIndex (
121- pd .IntervalIndex .from_breaks ([- 0.001 , 5.0 , 10.0 , np .inf ], closed = "right" ),
124+ pd .IntervalIndex .from_breaks ([0.0 , 5.0 , 10.0 , np .inf ], closed = "right" ),
122125 ordered = True ,
123126 name = "groupby_slope1" ,
124127 ),
125128 )
126- df_test = grouped_stats .grouped_stats (group_by_test , {"slope1" : [0 , 5 , 10 , np .inf ]}, aggregated_vars , statistics )
129+
130+ crs , _ , transform_test = grouped_stats .init_binnings_attributes (elev_raster )
131+
132+ expected_mask = {
133+ "groupby_slope1" : {
134+ "slope1_0.0_5.0" : Raster .from_array (
135+ np .array (
136+ [
137+ [True , True , True , True , True ],
138+ [False , False , False , False , False ],
139+ [False , False , False , False , False ],
140+ [False , False , False , False , False ],
141+ ]
142+ ),
143+ transform = transform_test ,
144+ crs = crs ,
145+ nodata = None ,
146+ ),
147+ "slope1_5.0_10.0" : Raster .from_array (
148+ np .array (
149+ [
150+ [False , False , False , False , False ],
151+ [True , True , True , True , True ],
152+ [False , False , False , False , False ],
153+ [False , False , False , False , False ],
154+ ]
155+ ),
156+ transform = transform_test ,
157+ crs = crs ,
158+ nodata = None ,
159+ ),
160+ "slope1_10.0_inf" : Raster .from_array (
161+ np .array (
162+ [
163+ [False , False , False , False , False ],
164+ [False , False , False , False , False ],
165+ [True , True , True , True , True ],
166+ [True , True , True , True , True ],
167+ ]
168+ ),
169+ transform = transform_test ,
170+ crs = crs ,
171+ nodata = None ,
172+ ),
173+ }
174+ }
175+
176+ df_test , masks_test = grouped_stats .grouped_stats (
177+ group_by_test , {"slope1" : [0 , 5 , 10 , np .inf ]}, aggregated_vars , statistics
178+ )
127179 assert_frame_equal (df_test , expected_df )
128180
181+ assert all (
182+ expected_mask ["groupby_slope1" ][group ] == masks_test ["groupby_slope1" ][group ] # type: ignore
183+ for group in expected_mask ["groupby_slope1" ]
184+ )
185+
129186
130187def test_grouped_stats_mask (
131188 slope_raster : Raster , aggregated_vars : Dict [str , Any ], mask_raster : Raster , statistics : List [Any ] # type: ignore
@@ -141,62 +198,51 @@ def test_grouped_stats_mask(
141198 ),
142199 )
143200
144- df_test = grouped_stats .grouped_stats (group_by_test , {"slope1" : mask_raster }, aggregated_vars , statistics )
201+ df_test , masks_test = grouped_stats .grouped_stats (
202+ group_by_test , {"slope1" : mask_raster }, aggregated_vars , statistics
203+ )
145204 assert_frame_equal (df_test , expected_df )
146205
206+ expected_mask = {"groupby_slope1" : None }
207+
208+ assert expected_mask == masks_test
209+
147210
148211def test_grouped_stats_segm (
149212 slope_raster : Raster , aggregated_vars : Dict [str , Any ], segm_raster : Raster , statistics : List [Any ]
150213) -> None :
151- group_by_test = {"slope1 " : slope_raster }
214+ group_by_test = {"segm " : slope_raster }
152215
153216 expected_df = pd .DataFrame (
154217 {
155218 ("raster" , "mean" ): [8.125000 , 11.285714 , 13.200000 ],
156219 ("raster" , "min" ): [1 , 3 , 4 ],
157220 },
158221 index = pd .CategoricalIndex (
159- [1 , 2 , 3 ], categories = [1 , 2 , 3 ], ordered = False , dtype = "category" , name = "groupby_slope1 "
222+ [1 , 2 , 3 ], categories = [1 , 2 , 3 ], ordered = False , dtype = "category" , name = "groupby_segm "
160223 ),
161224 )
162- df_test = grouped_stats .grouped_stats (group_by_test , {"slope1 " : segm_raster }, aggregated_vars , statistics )
225+ df_test , masks_test = grouped_stats .grouped_stats (group_by_test , {"segm " : segm_raster }, aggregated_vars , statistics )
163226 assert_frame_equal (df_test , expected_df )
164227
228+ expected_mask = {"groupby_segm" : None }
229+
230+ assert expected_mask == masks_test
231+
165232
166233def test_intersection_stats (
167234 slope_raster : Raster , aggregated_vars : Dict [str , Any ], segm_raster : Raster , statistics : List [Any ]
168235) -> None :
169236 group_by_test = {"slope1" : slope_raster , "slope2" : slope_raster }
170237 bins_test = {"slope1" : [0 , 5 , 10 , np .inf ], "slope2" : segm_raster }
171238
172- df_test = grouped_stats .grouped_stats (group_by_test , bins_test , aggregated_vars , statistics )
239+ df_test , _ = grouped_stats .grouped_stats (group_by_test , bins_test , aggregated_vars , statistics )
173240
174241 assert df_test .loc [(pd .Interval (10.0 , np .inf ), 3 ), ("raster" , "mean" )] == (15 + 19 + 20 ) / 3
175242 assert df_test .loc [(pd .Interval (10.0 , np .inf ), 3 ), ("raster" , "min" )] == 15
176243
177- assert df_test .loc [(pd .Interval (- 0.001 , 5 ), 1 ), ("raster" , "mean" )] == (1 + 2 + 5 ) / 3
178- assert df_test .loc [(pd .Interval (- 0.001 , 5 ), 1 ), ("raster" , "min" )] == 1
244+ assert df_test .loc [(pd .Interval (0.0 , 5 ), 1 ), ("raster" , "mean" )] == (1 + 2 + 5 ) / 3
245+ assert df_test .loc [(pd .Interval (0.0 , 5 ), 1 ), ("raster" , "min" )] == 1
179246
180247 assert df_test .loc [(pd .Interval (10.0 , np .inf ), 2 ), ("raster" , "mean" )] == (16 + 12 + 17 + 18 ) / 4
181248 assert df_test .loc [(pd .Interval (10.0 , np .inf ), 2 ), ("raster" , "min" )] == 12
182-
183-
184- def test_savings_csv_and_tiff (
185- slope_raster : Raster ,
186- aggregated_vars : Dict [str , Any ],
187- segm_raster : Raster ,
188- statistics : List [Any ],
189- tmp_path : PosixPath ,
190- ) -> None :
191- group_by_test = {"slope1" : slope_raster , "slope2" : slope_raster }
192- bins_test = {"slope1" : [0 , 5 , 10 , np .inf ], "slope2" : segm_raster }
193-
194- _ = grouped_stats .grouped_stats (
195- group_by_test , bins_test , aggregated_vars , statistics , save_csv = tmp_path , save_masks = tmp_path
196- )
197-
198- for interval in ["0_5.0" , "10.0_inf" , "5.0_10.0" ]:
199- assert tmp_path / "groupbyslope1" / f"slope1_{ interval } .tif"
200- for seg in [1 , 2 , 3 ]:
201- assert tmp_path / "groupbyslope2" / f"slope2_{ seg } .tif"
202- assert tmp_path / "grouped_stats.csv"
0 commit comments