88
99
1010@pytest .fixture (scope = "module" )
11- def data ():
12- precip , metadata = get_precipitation_fields (
11+ def dataset ():
12+ precip_dataset = get_precipitation_fields (
1313 num_prev_files = 0 , num_next_files = 0 , return_raw = False , metadata = True
1414 )
15- precip = precip .filled ()
16- precip , metadata = square_domain (precip , metadata , "crop" )
17- return precip , metadata
15+ precip_dataset = square_domain (precip_dataset , "crop" )
16+ return precip_dataset
1817
1918
2019rainfarm_arg_names = (
@@ -35,7 +34,7 @@ def data():
3534
3635@pytest .mark .parametrize (rainfarm_arg_names , rainfarm_arg_values )
3736def test_rainfarm_shape (
38- data ,
37+ dataset ,
3938 alpha ,
4039 ds_factor ,
4140 threshold ,
@@ -44,13 +43,13 @@ def test_rainfarm_shape(
4443 kernel_type ,
4544):
4645 """Test that the output of rainfarm is consistent with the downscaling factor."""
47- precip , metadata = data
48- window = metadata [ "xpixelsize " ] * ds_factor
49- precip_lr , __ = aggregate_fields_space (precip , metadata , window )
46+ precip_var = dataset . attrs [ "precip_var" ]
47+ window = dataset . x . attrs [ "stepsize " ] * ds_factor
48+ precip_lr_dataset = aggregate_fields_space (dataset , window )
5049
5150 rainfarm = downscaling .get_method ("rainfarm" )
52- precip_hr = rainfarm (
53- precip_lr ,
51+ precip_hr_dataset = rainfarm (
52+ precip_lr_dataset ,
5453 alpha = alpha ,
5554 ds_factor = ds_factor ,
5655 threshold = threshold ,
@@ -59,9 +58,15 @@ def test_rainfarm_shape(
5958 kernel_type = kernel_type ,
6059 )
6160
62- assert precip_hr .ndim == precip .ndim
63- assert precip_hr .shape [0 ] == precip .shape [0 ]
64- assert precip_hr .shape [1 ] == precip .shape [1 ]
61+ assert precip_hr_dataset [precip_var ].values .ndim == dataset [precip_var ].values .ndim
62+ assert (
63+ precip_hr_dataset [precip_var ].values .shape [0 ]
64+ == dataset [precip_var ].values .shape [0 ]
65+ )
66+ assert (
67+ precip_hr_dataset [precip_var ].values .shape [1 ]
68+ == dataset [precip_var ].values .shape [1 ]
69+ )
6570
6671
6772rainfarm_arg_values = [
@@ -74,7 +79,7 @@ def test_rainfarm_shape(
7479
7580@pytest .mark .parametrize (rainfarm_arg_names , rainfarm_arg_values )
7681def test_rainfarm_aggregate (
77- data ,
82+ dataset ,
7883 alpha ,
7984 ds_factor ,
8085 threshold ,
@@ -83,22 +88,24 @@ def test_rainfarm_aggregate(
8388 kernel_type ,
8489):
8590 """Test that the output of rainfarm is equal to original when aggregated."""
86- precip , metadata = data
87- window = metadata [ "xpixelsize " ] * ds_factor
88- precip_lr , __ = aggregate_fields_space (precip , metadata , window )
91+ precip_var = dataset . attrs [ "precip_var" ]
92+ window = dataset . x . attrs [ "stepsize " ] * ds_factor
93+ precip_lr_dataset = aggregate_fields_space (dataset , window )
8994
9095 rainfarm = downscaling .get_method ("rainfarm" )
91- precip_hr = rainfarm (
92- precip_lr ,
96+ precip_hr_dataset = rainfarm (
97+ precip_lr_dataset ,
9398 alpha = alpha ,
9499 ds_factor = ds_factor ,
95100 threshold = threshold ,
96101 return_alpha = return_alpha ,
97102 spectral_fusion = spectral_fusion ,
98103 kernel_type = kernel_type ,
99104 )
100- precip_low = aggregate_fields (precip_hr , ds_factor , axis = (0 , 1 ))
105+ precip_low_dataset = aggregate_fields (precip_hr_dataset , ds_factor , dim = ("y" , "x" ))
106+ precip_lr = precip_lr_dataset [precip_var ].values
101107 precip_lr [precip_lr < threshold ] = 0.0
108+ precip_low = precip_low_dataset [precip_var ].values
102109
103110 np .testing .assert_array_almost_equal (precip_lr , precip_low )
104111
@@ -108,7 +115,7 @@ def test_rainfarm_aggregate(
108115
109116@pytest .mark .parametrize (rainfarm_arg_names , rainfarm_arg_values )
110117def test_rainfarm_alpha (
111- data ,
118+ dataset ,
112119 alpha ,
113120 ds_factor ,
114121 threshold ,
@@ -117,13 +124,12 @@ def test_rainfarm_alpha(
117124 kernel_type ,
118125):
119126 """Test that rainfarm computes and returns alpha."""
120- precip , metadata = data
121- window = metadata ["xpixelsize" ] * ds_factor
122- precip_lr , __ = aggregate_fields_space (precip , metadata , window )
127+ window = dataset .x .attrs ["stepsize" ] * ds_factor
128+ precip_lr_dataset = aggregate_fields_space (dataset , window )
123129
124130 rainfarm = downscaling .get_method ("rainfarm" )
125- precip_hr = rainfarm (
126- precip_lr ,
131+ precip_hr_dataset = rainfarm (
132+ precip_lr_dataset ,
127133 alpha = alpha ,
128134 ds_factor = ds_factor ,
129135 threshold = threshold ,
@@ -132,8 +138,8 @@ def test_rainfarm_alpha(
132138 kernel_type = kernel_type ,
133139 )
134140
135- assert len (precip_hr ) == 2
141+ assert len (precip_hr_dataset ) == 2
136142 if alpha is None :
137- assert not precip_hr [1 ] == alpha
143+ assert not precip_hr_dataset [1 ] == alpha
138144 else :
139- assert precip_hr [1 ] == alpha
145+ assert precip_hr_dataset [1 ] == alpha
0 commit comments