Skip to content

Commit 0d9166a

Browse files
committed
Format
1 parent 165b7c0 commit 0d9166a

2 files changed

Lines changed: 58 additions & 76 deletions

File tree

src/scmdata/plotting.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,10 @@ def plumeplot( # pragma: no cover
205205

206206
if not pre_calculated:
207207
quantiles = [v for qv in quantiles_plumes for v in qv[0]]
208-
_pdf = type(self)(
209-
self.quantiles_over(quantile_over, quantiles=quantiles)
210-
)
208+
_pdf = type(self)(self.quantiles_over(quantile_over, quantiles=quantiles))
211209
else:
212210
_pdf = self
213211

214-
215212
if ax is None:
216213
ax = plt.figure().add_subplot(111)
217214

@@ -240,9 +237,7 @@ def plumeplot( # pragma: no cover
240237
try:
241238
pkwargs["color"] = _palette[hue_value]
242239
except KeyError as exc:
243-
error_msg = "{} not in palette: {}".format(
244-
hue_value, palette
245-
)
240+
error_msg = "{} not in palette: {}".format(hue_value, palette)
246241
raise KeyError(error_msg) from exc
247242

248243
elif hue_value in _palette:
@@ -252,8 +247,12 @@ def plumeplot( # pragma: no cover
252247
label = "{:.0f}th - {:.0f}th".format(q[0] * 100, q[1] * 100)
253248
p = ax.fill_between(
254249
xaxis,
255-
_get_1d_or_raise(hsdf.filter(quantile=q[0]), hue_var, style_var),
256-
_get_1d_or_raise(hsdf.filter(quantile=q[1]), hue_var, style_var),
250+
_get_1d_or_raise(
251+
hsdf.filter(quantile=q[0]), hue_var, style_var
252+
),
253+
_get_1d_or_raise(
254+
hsdf.filter(quantile=q[1]), hue_var, style_var
255+
),
257256
label=label,
258257
**pkwargs
259258
)
@@ -283,7 +282,9 @@ def plumeplot( # pragma: no cover
283282

284283
p = ax.plot(
285284
xaxis,
286-
_get_1d_or_raise(hsdf.filter(quantile=q[0]), hue_var, style_var),
285+
_get_1d_or_raise(
286+
hsdf.filter(quantile=q[0]), hue_var, style_var
287+
),
287288
label=label,
288289
linewidth=linewidth,
289290
**pkwargs
@@ -359,12 +360,7 @@ def _get_1d_or_raise(in_scmrun, hue_var, style_var):
359360
"Please process your data to create unique quantile timeseries "
360361
"before calling :meth:`plumeplot`.\n"
361362
"Found: {}".format(
362-
quantile,
363-
hue_var,
364-
hue_var_value,
365-
style_var,
366-
style_var_value,
367-
in_scmrun,
363+
quantile, hue_var, hue_var_value, style_var, style_var_value, in_scmrun,
368364
)
369365
)
370366
raise ValueError(error_msg)

tests/integration/test_plotting_integration.py

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import re
22
from unittest.mock import MagicMock, call
33

4-
import numpy as np
5-
import pytest
64
import matplotlib.axes
75
import matplotlib.pyplot as plt
6+
import numpy as np
7+
import pytest
88

99
from 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

1816
def 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+
)
161158
def 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

Comments
 (0)