11import re
22from unittest .mock import MagicMock , call
33
4- import numpy as np
5- import pytest
64import matplotlib .axes
75import matplotlib .pyplot as plt
6+ import numpy as np
7+ import pytest
88
99from scmdata import ScmRun
1010
11-
12- sample_quantiles_plumes = pytest .mark .parametrize ("quantiles_plumes" , (
13- (((0.05 , 0.95 ), 0.5 ), ((0.5 ,), 1.0 ),),
14- (((0.17 , 0.83 ), 0.7 ),),
15- ))
11+ sample_quantiles_plumes = pytest .mark .parametrize (
12+ "quantiles_plumes" , ((((0.05 , 0.95 ), 0.5 ), ((0.5 ,), 1.0 ),), (((0.17 , 0.83 ), 0.7 ),),)
13+ )
1614
1715
1816def test_plumeplot_default (plumeplot_scmrun ):
@@ -34,8 +32,7 @@ def test_plumeplot_pre_calculated(plumeplot_scmrun, quantiles_plumes):
3432 plumeplot_scmrun .quantiles_over ("ensemble_member" , quantiles = quantiles )
3533 )
3634 summary_stats .plumeplot (
37- quantiles_plumes = quantiles_plumes ,
38- pre_calculated = True ,
35+ quantiles_plumes = quantiles_plumes , pre_calculated = True ,
3936 )
4037
4138
@@ -66,21 +63,19 @@ def test_plumeplot_non_unique_lines(plumeplot_scmrun):
6663
6764 error_msg = re .escape (
6865 "More than one timeseries for "
69- "quantile: {}, "
70- "scenario: {}, "
71- "variable: {}.\n "
72- "Please process your data to create unique quantile timeseries "
73- "before calling :meth:`plumeplot`.\n "
74- "Found: {}" .format (
75- quantile ,
76- scenario ,
77- variable ,
78- summary_stats .filter (
79- quantile = quantile ,
80- scenario = scenario ,
81- variable = variable
82- ),
83- )
66+ "quantile: {}, "
67+ "scenario: {}, "
68+ "variable: {}.\n "
69+ "Please process your data to create unique quantile timeseries "
70+ "before calling :meth:`plumeplot`.\n "
71+ "Found: {}" .format (
72+ quantile ,
73+ scenario ,
74+ variable ,
75+ summary_stats .filter (
76+ quantile = quantile , scenario = scenario , variable = variable
77+ ),
78+ )
8479 )
8580 with pytest .raises (ValueError , match = error_msg ):
8681 summary_stats .plumeplot (pre_calculated = True )
@@ -122,7 +117,9 @@ def test_plumeplot_args(plumeplot_scmrun):
122117 if value .get_label () == "{:.0f}th" .format (q [0 ] * 100 ):
123118 has_a_match = True
124119 else :
125- if value .get_label () == "{:.0f}th - {:.0f}th" .format (q [0 ] * 100 , q [1 ] * 100 ):
120+ if value .get_label () == "{:.0f}th - {:.0f}th" .format (
121+ q [0 ] * 100 , q [1 ] * 100
122+ ):
126123 has_a_match = True
127124
128125 assert has_a_match
@@ -154,10 +151,10 @@ def test_plumeplot_args(plumeplot_scmrun):
154151
155152@pytest .mark .parametrize ("linewidth" , (2 , 2.5 ))
156153@pytest .mark .parametrize ("time_axis" , ("year" , None ))
157- @pytest .mark .parametrize ("quantiles_plumes" , (
158- ((( 0.05 , 0.95 ), 0.5 ), (( 0.5 ,), 1.0 ),) ,
159- (((0. 17 , 0.83 ), 0.7 ), ((0.4 ,), 1.0 ),),
160- ))
154+ @pytest .mark .parametrize (
155+ "quantiles_plumes" ,
156+ (((( 0.05 , 0.95 ), 0.5 ), (( 0.5 ,), 1.0 ),), ((( 0. 17 , 0.83 ), 0.7 ), ((0.4 ,), 1.0 ), ),),
157+ )
161158def test_plumeplot_values (plumeplot_scmrun , quantiles_plumes , time_axis , linewidth ):
162159 mock_ax = MagicMock ()
163160
@@ -182,7 +179,6 @@ def test_plumeplot_values(plumeplot_scmrun, quantiles_plumes, time_axis, linewid
182179
183180 xaxis = summary_stats .timeseries (time_axis = time_axis ).columns .tolist ()
184181
185-
186182 def _is_in_calls (call_to_check , call_args_list ):
187183 pargs_to_check = call_to_check [1 ]
188184 kargs_to_check = call_to_check [2 ]
@@ -208,53 +204,47 @@ def _is_in_calls(call_to_check, call_args_list):
208204
209205 return in_call
210206
211-
212207 def _get_with_empty_check (idf_filtered ):
213208 if idf_filtered .empty :
214209 raise ValueError ("Empty" )
215210
216211 return idf_filtered .values .squeeze ()
217212
218-
219213 def _make_fill_between_call (idf , cm , scen , quant_alpha ):
220214 quantiles = quant_alpha [0 ]
221215 alpha = quant_alpha [1 ]
222216
223217 return call (
224218 xaxis ,
225- _get_with_empty_check (idf .filter (climate_model = cm , scenario = scen , quantile = quantiles [0 ])),
226- _get_with_empty_check (idf .filter (climate_model = cm , scenario = scen , quantile = quantiles [1 ])),
219+ _get_with_empty_check (
220+ idf .filter (climate_model = cm , scenario = scen , quantile = quantiles [0 ])
221+ ),
222+ _get_with_empty_check (
223+ idf .filter (climate_model = cm , scenario = scen , quantile = quantiles [1 ])
224+ ),
227225 alpha = alpha ,
228226 color = palette [cm ],
229227 label = "{:.0f}th - {:.0f}th" .format (quantiles [0 ] * 100 , quantiles [1 ] * 100 ),
230228 )
231229
232-
233230 def _make_plot_call (idf , cm , scen , quant_alpha ):
234231 quantiles = quant_alpha [0 ]
235232 alpha = quant_alpha [1 ]
236233
237234 return call (
238235 xaxis ,
239- _get_with_empty_check (idf .filter (climate_model = cm , scenario = scen , quantile = quantiles [0 ])),
236+ _get_with_empty_check (
237+ idf .filter (climate_model = cm , scenario = scen , quantile = quantiles [0 ])
238+ ),
240239 color = palette [cm ],
241240 linestyle = dashes [scen ],
242241 linewidth = linewidth ,
243242 label = "{:.0f}th" .format (quantiles [0 ] * 100 ),
244243 alpha = alpha ,
245244 )
246245
247-
248- cm_scen_combos = (
249- summary_stats
250- .meta [["climate_model" , "scenario" ]]
251- .drop_duplicates ()
252- )
253- cm_scen_combos = [
254- v [1 ].values .tolist ()
255- for v in cm_scen_combos .iterrows ()
256- ]
257-
246+ cm_scen_combos = summary_stats .meta [["climate_model" , "scenario" ]].drop_duplicates ()
247+ cm_scen_combos = [v [1 ].values .tolist () for v in cm_scen_combos .iterrows ()]
258248
259249 plume_qa = [q for q in quantiles_plumes if len (q [0 ]) == 2 ]
260250 fill_between_calls = [
@@ -264,11 +254,12 @@ def _make_plot_call(idf, cm, scen, quant_alpha):
264254 ]
265255
266256 # debug by looking at mock_ax.fill_between.call_args_list
267- assert all ([
268- _is_in_calls (c , mock_ax .fill_between .call_args_list )
269- for c in fill_between_calls
270- ])
271-
257+ assert all (
258+ [
259+ _is_in_calls (c , mock_ax .fill_between .call_args_list )
260+ for c in fill_between_calls
261+ ]
262+ )
272263
273264 line_qa = [q for q in quantiles_plumes if len (q [0 ]) == 1 ]
274265 plot_calls = [
@@ -278,10 +269,7 @@ def _make_plot_call(idf, cm, scen, quant_alpha):
278269 ]
279270
280271 # debug by looking at mock_ax.plot.call_args_list
281- assert all ([
282- _is_in_calls (c , mock_ax .plot .call_args_list )
283- for c in plot_calls
284- ])
272+ assert all ([_is_in_calls (c , mock_ax .plot .call_args_list ) for c in plot_calls ])
285273
286274
287275# sensible error if missing style etc.
@@ -292,10 +280,8 @@ def test_error_missing_palette(plumeplot_scmrun):
292280 )
293281
294282 # missing definitions raise
295- palette_miss = {'a_scenario_2' : 'red' , 'b_scenario' : 'green' }
296- error_msg = re .escape (
297- "a_scenario not in palette: {}" .format (palette_miss )
298- )
283+ palette_miss = {"a_scenario_2" : "red" , "b_scenario" : "green" }
284+ error_msg = re .escape ("a_scenario not in palette: {}" .format (palette_miss ))
299285 with pytest .raises (KeyError , match = error_msg ):
300286 plumeplot_scmrun .plumeplot (palette = palette_miss )
301287
0 commit comments