Skip to content

Commit 5fa4f9f

Browse files
committed
Updates to formatting and updates QM and QDM tests to reflect change in experimental default distribution.
1 parent b743235 commit 5fa4f9f

File tree

4 files changed

+155
-61
lines changed

4 files changed

+155
-61
lines changed

tests/test_cdft.py

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,51 @@ def test__apply_CDFt_mapping(self):
138138
)
139139

140140
# Systematic tests of tas
141-
debiaser = CDFt.from_variable("tas", running_window_mode = False, running_window_mode_over_years_of_cm_future = False)
141+
debiaser = CDFt.from_variable(
142+
"tas",
143+
running_window_mode=False,
144+
running_window_mode_over_years_of_cm_future=False,
145+
)
142146

143147
n = 10000
144148
np.random.seed(1234)
145149
for mean_obs in [0, 5]:
146-
for scale_obs in [1., 2.]:
147-
for bias in [0, 1, 2, 10]:
148-
for scale_bias in [1., 1.5, 2.]:
149-
for trend in [0., 10., 20.]:
150-
for trend_scale in [1., 2.]:
151-
152-
obs = np.random.normal(size = n)*scale_obs + mean_obs
153-
cm_hist = np.random.normal(size = n)*scale_obs*scale_bias + mean_obs + bias
154-
cm_fut = np.random.normal(size = n)*scale_obs*scale_bias*trend_scale + mean_obs + bias + trend
155-
156-
debiased_cm_fut = debiaser.apply_location(obs, cm_hist, cm_fut)
157-
assert np.abs(np.mean(debiased_cm_fut) - trend - mean_obs ) < 0.5
158-
assert np.abs(np.std(debiased_cm_fut)/trend_scale - scale_obs) < 0.1
150+
for scale_obs in [1.0, 2.0]:
151+
for bias in [0, 1, 2, 10]:
152+
for scale_bias in [1.0, 1.5, 2.0]:
153+
for trend in [0.0, 10.0, 20.0]:
154+
for trend_scale in [1.0, 2.0]:
155+
156+
obs = np.random.normal(size=n) * scale_obs + mean_obs
157+
cm_hist = (
158+
np.random.normal(size=n) * scale_obs * scale_bias
159+
+ mean_obs
160+
+ bias
161+
)
162+
cm_fut = (
163+
np.random.normal(size=n)
164+
* scale_obs
165+
* scale_bias
166+
* trend_scale
167+
+ mean_obs
168+
+ bias
169+
+ trend
170+
)
171+
172+
debiased_cm_fut = debiaser.apply_location(
173+
obs, cm_hist, cm_fut
174+
)
175+
assert (
176+
np.abs(np.mean(debiased_cm_fut) - trend - mean_obs)
177+
< 0.5
178+
)
179+
assert (
180+
np.abs(
181+
np.std(debiased_cm_fut) / trend_scale
182+
- scale_obs
183+
)
184+
< 0.1
185+
)
159186

160187
def test__get_threshold(self):
161188
x, y, z = np.arange(-100, 200), np.arange(-100, 200), np.arange(-100, 200)

tests/test_quantile_delta_mapping.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ def test_from_variable(self):
107107
assert tas.trend_preservation == "absolute"
108108

109109
tasmin = QuantileDeltaMapping.from_variable("tasmin")
110-
assert tasmin.distribution == scipy.stats.beta
110+
assert tasmin.distribution == scipy.stats.norm
111111
assert tasmin.trend_preservation == "absolute"
112112

113113
tasmax = QuantileDeltaMapping.from_variable("tasmax")
114-
assert tasmax.distribution == scipy.stats.beta
114+
assert tasmax.distribution == scipy.stats.norm
115115
assert tasmax.trend_preservation == "absolute"
116116

117117
def test__init__(self):
@@ -205,24 +205,45 @@ def test__apply_debiasing_steps_tas(self):
205205
)
206206

207207
# Systematic tests of tas
208-
debiaser = QuantileDeltaMapping.from_variable("tas", running_window_mode = False, running_window_mode_over_years_of_cm_future = False)
209-
208+
debiaser = QuantileDeltaMapping.from_variable(
209+
"tas",
210+
running_window_mode=False,
211+
running_window_mode_over_years_of_cm_future=False,
212+
)
213+
210214
n = 10000
211215
np.random.seed(1234)
212216
for mean_obs in [0, 5]:
213-
for scale_obs in [1., 2.]:
214-
for bias in [0, 1, 2, 10]:
215-
for scale_bias in [1., 1.5, 2.]:
216-
for trend in [0., 10., 20.]:
217-
for trend_scale in [1., 2.]:
218-
219-
obs = np.random.normal(size = n)*scale_obs + mean_obs
220-
cm_hist = np.random.normal(size = n)*scale_obs*scale_bias + mean_obs + bias
221-
cm_fut = np.random.normal(size = n)*scale_obs*scale_bias*trend_scale + mean_obs + bias + trend
222-
223-
debiased_cm_fut = debiaser.apply_location(obs, cm_hist, cm_fut)
224-
assert np.abs(np.mean(debiased_cm_fut) - trend - mean_obs ) < 0.5
225-
#assert np.abs(np.std(debiased_cm_fut)/trend_scale - scale_obs) < 0.5
217+
for scale_obs in [1.0, 2.0]:
218+
for bias in [0, 1, 2, 10]:
219+
for scale_bias in [1.0, 1.5, 2.0]:
220+
for trend in [0.0, 10.0, 20.0]:
221+
for trend_scale in [1.0, 2.0]:
222+
223+
obs = np.random.normal(size=n) * scale_obs + mean_obs
224+
cm_hist = (
225+
np.random.normal(size=n) * scale_obs * scale_bias
226+
+ mean_obs
227+
+ bias
228+
)
229+
cm_fut = (
230+
np.random.normal(size=n)
231+
* scale_obs
232+
* scale_bias
233+
* trend_scale
234+
+ mean_obs
235+
+ bias
236+
+ trend
237+
)
238+
239+
debiased_cm_fut = debiaser.apply_location(
240+
obs, cm_hist, cm_fut
241+
)
242+
assert (
243+
np.abs(np.mean(debiased_cm_fut) - trend - mean_obs)
244+
< 0.5
245+
)
246+
# assert np.abs(np.std(debiased_cm_fut)/trend_scale - scale_obs) < 0.5
226247

227248
def test__apply_debiasing_steps_pr(self):
228249
pr = QuantileDeltaMapping.from_variable("pr")

tests/test_quantile_mapping.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ def test_from_variable(self):
7777
assert tas.detrending == "additive"
7878

7979
tasmin = QuantileMapping.from_variable("tasmin")
80-
assert tasmin.distribution == scipy.stats.beta
80+
assert tasmin.distribution == scipy.stats.norm
8181
assert tasmin.detrending == "additive"
8282

8383
tasmax = QuantileMapping.from_variable("tasmax")
84-
assert tasmax.distribution == scipy.stats.beta
84+
assert tasmax.distribution == scipy.stats.norm
8585
assert tasmax.detrending == "additive"
8686

8787
def test_apply(self):
@@ -119,16 +119,39 @@ def test_apply(self):
119119
n = 10000
120120
np.random.seed(1234)
121121
for mean_obs in [0, 5]:
122-
for scale_obs in [1., 2.]:
123-
for bias in [0, 1, 2, 10]:
124-
for scale_bias in [1., 1.5, 2.]:
125-
for trend in [0., 10., 20.]:
126-
for trend_scale in [1., 2.]:
127-
128-
obs = np.random.normal(size = n)*scale_obs + mean_obs
129-
cm_hist = np.random.normal(size = n)*scale_obs*scale_bias + mean_obs + bias
130-
cm_fut = np.random.normal(size = n)*scale_obs*scale_bias*trend_scale + mean_obs + bias + trend
131-
132-
debiased_cm_fut = debiaser.apply_location(obs, cm_hist, cm_fut)
133-
assert np.abs(np.mean(debiased_cm_fut) - trend - mean_obs ) < 0.5
134-
assert np.abs(np.std(debiased_cm_fut)/trend_scale - scale_obs) < 0.1
122+
for scale_obs in [1.0, 2.0]:
123+
for bias in [0, 1, 2, 10]:
124+
for scale_bias in [1.0, 1.5, 2.0]:
125+
for trend in [0.0, 10.0, 20.0]:
126+
for trend_scale in [1.0, 2.0]:
127+
128+
obs = np.random.normal(size=n) * scale_obs + mean_obs
129+
cm_hist = (
130+
np.random.normal(size=n) * scale_obs * scale_bias
131+
+ mean_obs
132+
+ bias
133+
)
134+
cm_fut = (
135+
np.random.normal(size=n)
136+
* scale_obs
137+
* scale_bias
138+
* trend_scale
139+
+ mean_obs
140+
+ bias
141+
+ trend
142+
)
143+
144+
debiased_cm_fut = debiaser.apply_location(
145+
obs, cm_hist, cm_fut
146+
)
147+
assert (
148+
np.abs(np.mean(debiased_cm_fut) - trend - mean_obs)
149+
< 0.5
150+
)
151+
assert (
152+
np.abs(
153+
np.std(debiased_cm_fut) / trend_scale
154+
- scale_obs
155+
)
156+
< 0.1
157+
)

tests/test_scaled_distribution_mapping.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,47 @@ def test_from_variable(self):
7878
tasmax = ScaledDistributionMapping.from_variable("tasmax")
7979
assert tasmax.distribution == scipy.stats.norm
8080
assert tasmax.mapping_type == "absolute"
81-
81+
8282
def test_absolute_sdm_apply_location(self):
8383
# Systematic tests of absolute SDM
8484
debiaser = ScaledDistributionMapping.from_variable("tas")
85-
85+
8686
n = 10000
8787
np.random.seed(1234)
8888
for mean_obs in [0, 5]:
89-
for scale_obs in [1., 2.]:
90-
for bias in [0, 1, 2, 10]:
91-
for scale_bias in [1., 1.5, 2.]:
92-
for trend in [0., 10., 20.]:
93-
for trend_scale in [1., 2.]:
94-
95-
obs = np.random.normal(size = n)*scale_obs + mean_obs
96-
cm_hist = np.random.normal(size = n)*scale_obs*scale_bias + mean_obs + bias
97-
cm_fut = np.random.normal(size = n)*scale_obs*scale_bias*trend_scale + mean_obs + bias + trend
98-
99-
debiased_cm_fut = debiaser.apply_location(obs, cm_hist, cm_fut)
100-
assert np.abs(np.mean(debiased_cm_fut) - trend - mean_obs ) < 0.5
101-
assert np.abs(np.std(debiased_cm_fut)/trend_scale - scale_obs) < 0.1
89+
for scale_obs in [1.0, 2.0]:
90+
for bias in [0, 1, 2, 10]:
91+
for scale_bias in [1.0, 1.5, 2.0]:
92+
for trend in [0.0, 10.0, 20.0]:
93+
for trend_scale in [1.0, 2.0]:
94+
95+
obs = np.random.normal(size=n) * scale_obs + mean_obs
96+
cm_hist = (
97+
np.random.normal(size=n) * scale_obs * scale_bias
98+
+ mean_obs
99+
+ bias
100+
)
101+
cm_fut = (
102+
np.random.normal(size=n)
103+
* scale_obs
104+
* scale_bias
105+
* trend_scale
106+
+ mean_obs
107+
+ bias
108+
+ trend
109+
)
110+
111+
debiased_cm_fut = debiaser.apply_location(
112+
obs, cm_hist, cm_fut
113+
)
114+
assert (
115+
np.abs(np.mean(debiased_cm_fut) - trend - mean_obs)
116+
< 0.5
117+
)
118+
assert (
119+
np.abs(
120+
np.std(debiased_cm_fut) / trend_scale
121+
- scale_obs
122+
)
123+
< 0.1
124+
)

0 commit comments

Comments
 (0)